task-se3-equality.cpp 4.96 KB
Newer Older
1
2
3
//
// Copyright (c) 2017 CNRS
//
jcarpent's avatar
jcarpent committed
4
5
// This file is part of tsid
// tsid is free software: you can redistribute it
6
7
8
// 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.
jcarpent's avatar
jcarpent committed
9
// tsid is distributed in the hope that it will be
10
11
12
13
// 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
jcarpent's avatar
jcarpent committed
14
// tsid If not, see
15
16
17
// <http://www.gnu.org/licenses/>.
//

jcarpent's avatar
jcarpent committed
18
19
20
#include "tsid/math/utils.hpp"
#include "tsid/tasks/task-se3-equality.hpp"
#include "tsid/robots/robot-wrapper.hpp"
21

jcarpent's avatar
jcarpent committed
22
namespace tsid
23
24
25
{
  namespace tasks
  {
26
27
    using namespace math;
    using namespace trajectories;
28
29
30
31
32
33
34
    using namespace se3;

    TaskSE3Equality::TaskSE3Equality(const std::string & name,
                                     RobotWrapper & robot,
                                     const std::string & frameName):
      TaskMotion(name, robot),
      m_frame_name(frameName),
andreadelprete's avatar
andreadelprete committed
35
36
      m_constraint(name, 6, robot.nv()),
      m_ref(12, 6)
37
38
39
40
41
42
43
44
    {
      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();
45
46
47
48
49
50
      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);
51
52
53
54
55
56
57
58
59
60
61
62
      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;
    }

63
    const Vector & TaskSE3Equality::Kp() const { return m_Kp; }
64

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

    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)
    {
andreadelprete's avatar
andreadelprete committed
81
      m_ref = ref;
82
83
84
85
86
      vectorToSE3(ref.pos, m_M_ref);
      m_v_ref = Motion(ref.vel);
      m_a_ref = Motion(ref.acc);
    }

andreadelprete's avatar
andreadelprete committed
87
88
89
90
91
    const TrajectorySample & TaskSE3Equality::getReference() const
    {
      return m_ref;
    }

92
    const Vector & TaskSE3Equality::position_error() const
93
    {
94
      return m_p_error_vec;
95
96
    }

97
    const Vector & TaskSE3Equality::velocity_error() const
98
    {
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
      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;
    }

andreadelprete's avatar
andreadelprete committed
122
123
124
125
126
127
128
    const Vector & TaskSE3Equality::getDesiredAcceleration() const
    {
      return m_a_des;
    }

    Vector TaskSE3Equality::getAcceleration(ConstRefVector dv) const
    {
129
      return m_constraint.matrix()*dv + m_drift.toVector();
andreadelprete's avatar
andreadelprete committed
130
131
    }

132
133
134
135
136
    Index TaskSE3Equality::frame_id() const
    {
      return m_frame_id;
    }

137
138
139
    const ConstraintBase & TaskSE3Equality::getConstraint() const
    {
      return m_constraint;
140
141
    }

142
143
144
    const ConstraintBase & TaskSE3Equality::compute(const double t,
                                                    ConstRefVector q,
                                                    ConstRefVector v,
andreadelprete's avatar
andreadelprete committed
145
                                                    const Data & data)
146
147
    {
      SE3 oMi;
andreadelprete's avatar
andreadelprete committed
148
      Motion v_frame;
149
150
      m_robot.framePosition(data, m_frame_id, oMi);
      m_robot.frameVelocity(data, m_frame_id, v_frame);
andreadelprete's avatar
andreadelprete committed
151
      m_robot.frameClassicAcceleration(data, m_frame_id, m_drift);
152
153
154
155
156
157
158

      // 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

159
160
      m_p_error_vec = m_p_error.toVector();
      m_v_error_vec = m_v_error.toVector();
161
      SE3ToVector(m_M_ref, m_p_ref);
162
      m_v_ref_vec = m_v_ref.toVector();
163
      SE3ToVector(oMi, m_p);
164
165
166
167
168
169
170
      m_v = v_frame.toVector();

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

171
      // desired acc in local frame
andreadelprete's avatar
andreadelprete committed
172
173
174
      m_a_des = - m_Kp.cwiseProduct(m_p_error.toVector())
                - m_Kd.cwiseProduct(m_v_error.toVector())
                + m_wMl.actInv(m_a_ref).toVector();
175

176
177
      // @todo Since Jacobian computation is cheaper in world frame
      // we could do all computations in world frame
178
179
180
      m_robot.frameJacobianLocal(data, m_frame_id, m_J);

      m_constraint.setMatrix(m_J);
andreadelprete's avatar
andreadelprete committed
181
      m_constraint.setVector(m_a_des - m_drift.toVector());
182
183
184
185
186
      return m_constraint;
    }
    
  }
}