File size: 4,473 Bytes
ef17c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*
  Author(s):  Simon Leonard, Anton Deguet
  Created on: 2017-03-07

  (C) Copyright 2017 Johns Hopkins University (JHU), All Rights Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---
*/

#include <sawIntuitiveResearchKit/robManipulatorPSMSnake.h>

robManipulatorPSMSnake::robManipulatorPSMSnake(const std::vector<robKinematics *> linkParms,
                                               const vctFrame4x4<double> &Rtw0)
    : robManipulator(linkParms, Rtw0)
{
}

robManipulatorPSMSnake::robManipulatorPSMSnake(const std::string &robotfilename,
                                               const vctFrame4x4<double> &Rtw0)
    : robManipulator(robotfilename, Rtw0)
{
}

robManipulatorPSMSnake::robManipulatorPSMSnake(const vctFrame4x4<double> &Rtw0)
    : robManipulator(Rtw0)
{
}

void robManipulatorPSMSnake::Resize(void)
{
    if (m.E.cols() == links.size()) {
        return;
    }

    // Ex = f
    m.E.SetSize(2, links.size(), VCT_COL_MAJOR);
    m.E.SetAll(0.0);
    m.f.SetSize(2, 1, VCT_COL_MAJOR);
    m.f.SetAll(0.0);
    m.E.at(0, 4) = 1.0;     m.E.at(0, 7) = -1.0;
    m.E.at(1, 5) = 1.0;     m.E.at(1, 6) = -1.0;

    // || Ax - B ||
    m.A.SetSize(6, links.size(), VCT_COL_MAJOR);
    m.A.SetAll(0.0);
    m.b.SetSize(6, 1, VCT_COL_MAJOR);
    m.b.SetAll(0.0);
}

vctReturnDynamicVector<double>
robManipulatorPSMSnake::ConstrainedRMRC(const vctDynamicVector<double> & q,
                                        const vctFixedSizeVector<double, 6> & vw)
{
    JacobianSpatial(q, m.A);
    m.b.Column(0) = vw;
    m.lsei.Allocate(m.E, m.A, m.G);
    m.lsei.Solve(m.E, m.f, m.A, m.b, m.G, m.h);
    return vctReturnDynamicVector<double>(m.lsei.GetX().Column(0));
}

robManipulator::Errno
robManipulatorPSMSnake::InverseKinematics(vctDynamicVector<double> & q,
                                          const vctFrame4x4<double> & Rts,
                                          double tolerance,
                                          size_t Niterations,
                                          double CMN_UNUSED(LAMBDA))
{
    if (q.size() != links.size()) {
        CMN_LOG_RUN_ERROR << CMN_LOG_DETAILS
                          << ": robManipulatorPSMSnake::InverseKinematics: expected " << links.size() << " joints values. "
                          << " Got " << q.size()
                          << std::endl;
        return robManipulator::EFAILURE;
    }

    if (links.size() == 0) {
        CMN_LOG_RUN_ERROR << CMN_LOG_DETAILS
                          << ": robManipulatorPSMSnake::InverseKinematics: the manipulator has no links."
                          << std::endl;
        return robManipulator::EFAILURE;
    }

    Resize();

    double ndq = 1.0;               // norm of the iteration error
    size_t i = 0;
    // loop until Niter are executed or the error is bellow the tolerance
    for (i = 0; i < Niterations && tolerance < ndq; i++) {

        // Evaluate the forward kinematics
        vctFrame4x4<double,VCT_ROW_MAJOR> Rt = ForwardKinematics( q );

        // compute the translation error
        vctFixedSizeVector<double,3> dt( Rts[0][3] - Rt[0][3],
                                         Rts[1][3] - Rt[1][3],
                                         Rts[2][3] - Rt[2][3] );

        // compute the orientation error
        // This is the orientation error
        vctFixedSizeVector<double,3> dr = 0.5 * ( (Rt.Rotation().Column(0) % Rts.Rotation().Column(0)) +
                                                  (Rt.Rotation().Column(1) % Rts.Rotation().Column(1)) +
                                                  (Rt.Rotation().Column(2) % Rts.Rotation().Column(2)) );

        // combine both errors in one R^6 vector
        vctFixedSizeVector<double,6> e( dt[0], dt[1], dt[2], dr[0], dr[1], dr[2] );

        vctDynamicVector<double> dq = ConstrainedRMRC( q, e );

        // compute the L2 norm of the error
        ndq = dq.Norm();

        // update the solution
        q.Add(dq);
    }

    NormalizeAngles(q);

    if (i == Niterations) {
      return robManipulator::EFAILURE;
    }
    return robManipulator::ESUCCESS;
}