task-se3-equality.cpp 4.46 KB
Newer Older
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
//
// Copyright (c) 2017 CNRS
//
// This file is part of PinInvDyn
// PinInvDyn is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version
// 3 of the License, or (at your option) any later version.
// PinInvDyn is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Lesser Public License for more details. You should have
// received a copy of the GNU Lesser General Public License along with
// PinInvDyn If not, see
// <http://www.gnu.org/licenses/>.
//

#include <pininvdyn/tasks/task-se3-equality.hpp>

namespace pininvdyn
{
  namespace tasks
  {
    using namespace pininvdyn::math;
    using namespace se3;

    TaskSE3Equality::TaskSE3Equality(const std::string & name,
                                     RobotWrapper & robot,
                                     const std::string & frameName):
      TaskMotion(name, robot),
      m_frame_name(frameName),
      m_constraint(name, 6, robot.nv())
    {
      assert(m_robot.model().existFrame(frameName));
      m_frame_id = m_robot.model().getFrameId(frameName);

      m_v_ref.setZero();
      m_a_ref.setZero();
      m_M_ref.setIdentity();
      m_wMl.setIdentity();
41
42
43
44
45
46
      m_p_error_vec.setZero(6);
      m_v_error_vec.setZero(6);
      m_p.resize(12);
      m_v.resize(6);
      m_p_ref.resize(12);
      m_v_ref_vec.resize(6);
47
48
49
50
51
52
53
54
55
56
57
58
      m_Kp.setZero(6);
      m_Kd.setZero(6);
      m_a_des.setZero(6);
      m_J.setZero(6, robot.nv());
    }

    int TaskSE3Equality::dim() const
    {
      //return self._mask.sum ()
      return 6;
    }

59
    const Vector & TaskSE3Equality::Kp() const { return m_Kp; }
60

61
    const Vector & TaskSE3Equality::Kd() const { return m_Kd; }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

    void TaskSE3Equality::Kp(ConstRefVector Kp)
    {
      assert(Kp.size()==6);
      m_Kp = Kp;
    }

    void TaskSE3Equality::Kd(ConstRefVector Kd)
    {
      assert(Kd.size()==6);
      m_Kd = Kd;
    }

    void TaskSE3Equality::setReference(TrajectorySample & ref)
    {
      vectorToSE3(ref.pos, m_M_ref);
      m_v_ref = Motion(ref.vel);
      m_a_ref = Motion(ref.acc);
    }

82
    const Vector & TaskSE3Equality::position_error() const
83
    {
84
      return m_p_error_vec;
85
86
    }

87
    const Vector & TaskSE3Equality::velocity_error() const
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
      return m_v_error_vec;
    }

    const Vector & TaskSE3Equality::position() const
    {
      return m_p;
    }

    const Vector & TaskSE3Equality::velocity() const
    {
      return m_v;
    }

    const Vector & TaskSE3Equality::position_ref() const
    {
      return m_p_ref;
    }

    const Vector & TaskSE3Equality::velocity_ref() const
    {
      return m_v_ref_vec;
    }

    const ConstraintBase & TaskSE3Equality::getConstraint() const
    {
      return m_constraint;
115
116
    }

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    const ConstraintBase & TaskSE3Equality::compute(const double t,
                                                    ConstRefVector q,
                                                    ConstRefVector v,
                                                    Data & data)
    {
      SE3 oMi;
      Motion v_frame, drift;
      m_robot.framePosition(data, m_frame_id, oMi);
      m_robot.frameVelocity(data, m_frame_id, v_frame);
      m_robot.frameClassicAcceleration(data, m_frame_id, drift);

      // Transformation from local to world
      m_wMl.rotation(oMi.rotation());

      errorInSE3(oMi, m_M_ref, m_p_error);          // pos err in local frame
      m_v_error = v_frame - m_wMl.actInv(m_v_ref);  // vel err in local frame

134
135
136
137
138
139
140
141
142
143
144
145
      m_p_error_vec = m_p_error.toVector();
      m_v_error_vec = m_v_error.toVector();
      se3ToVector(m_M_ref, m_p_ref);
      m_v_ref_vec = m_v_ref.toVector();
      se3ToVector(oMi, m_p);
      m_v = v_frame.toVector();

#ifndef NDEBUG
//      PRINT_VECTOR(v_frame.toVector());
//      PRINT_VECTOR(m_v_ref.toVector());
#endif

146
147
148
149
150
      // desired acc in local frame
      m_a_des = - m_Kp.cwiseProduct(m_p_error.toVector_impl())
                - m_Kd.cwiseProduct(m_v_error.toVector_impl())
                + m_wMl.actInv(m_a_ref).toVector_impl();

151
152
      // @todo Since Jacobian computation is cheaper in world frame
      // we could do all computations in world frame
153
154
155
156
157
158
159
160
161
      m_robot.frameJacobianLocal(data, m_frame_id, m_J);

      m_constraint.setMatrix(m_J);
      m_constraint.setVector(m_a_des - drift.toVector_impl());
      return m_constraint;
    }
    
  }
}