task-se3-equality.cpp 5.69 KB
Newer Older
1
//
2
// Copyright (c) 2017 CNRS, NYU, MPI Tübingen
3
//
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
      m_Kp.setZero(6);
      m_Kd.setZero(6);
      m_a_des.setZero(6);
      m_J.setZero(6, robot.nv());
55
      m_J_rotated.setZero(6, robot.nv());
56
57

      m_mask.resize(6);
58
      m_mask.fill(1.);
59
60

      m_local_frame = true;
61
62
63
64
65
66
67
68
    }

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

69
    const Vector & TaskSE3Equality::Kp() const { return m_Kp; }
70

71
    const Vector & TaskSE3Equality::Kd() const { return m_Kd; }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

    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
87
      m_ref = ref;
88
89
90
91
92
      vectorToSE3(ref.pos, m_M_ref);
      m_v_ref = Motion(ref.vel);
      m_a_ref = Motion(ref.acc);
    }

andreadelprete's avatar
andreadelprete committed
93
94
95
96
97
    const TrajectorySample & TaskSE3Equality::getReference() const
    {
      return m_ref;
    }

98
    const Vector & TaskSE3Equality::position_error() const
99
    {
100
      return m_p_error_vec;
101
102
    }

103
    const Vector & TaskSE3Equality::velocity_error() const
104
    {
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
      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
128
129
130
131
132
133
134
    const Vector & TaskSE3Equality::getDesiredAcceleration() const
    {
      return m_a_des;
    }

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

138
139
140
141
142
    Index TaskSE3Equality::frame_id() const
    {
      return m_frame_id;
    }

143
144
145
    const ConstraintBase & TaskSE3Equality::getConstraint() const
    {
      return m_constraint;
146
147
    }

148
149
150
151
152
    void TaskSE3Equality::useLocalFrame(bool local_frame)
    {
      m_local_frame = local_frame;
    }

Olivier Stasse's avatar
Olivier Stasse committed
153
154
155
    const ConstraintBase & TaskSE3Equality::compute(const double ,
                                                    ConstRefVector ,
                                                    ConstRefVector ,
andreadelprete's avatar
andreadelprete committed
156
                                                    const Data & data)
157
158
    {
      SE3 oMi;
andreadelprete's avatar
andreadelprete committed
159
      Motion v_frame;
160
161
      m_robot.framePosition(data, m_frame_id, oMi);
      m_robot.frameVelocity(data, m_frame_id, v_frame);
andreadelprete's avatar
andreadelprete committed
162
      m_robot.frameClassicAcceleration(data, m_frame_id, m_drift);
163

164
165
166
      // @todo Since Jacobian computation is cheaper in world frame
      // we could do all computations in world frame
      m_robot.frameJacobianLocal(data, m_frame_id, m_J);
167
168

      errorInSE3(oMi, m_M_ref, m_p_error);          // pos err in local frame
169
      m_p_error_vec = m_p_error.toVector();
170
171
      SE3ToVector(m_M_ref, m_p_ref);
      SE3ToVector(oMi, m_p);
172

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

176
177
      if (m_local_frame) {
        m_v_error = v_frame - m_wMl.actInv(m_v_ref);  // vel err in local frame
178

179
180
181
182
183
        // desired acc in local frame
        m_a_des = - m_Kp.cwiseProduct(m_p_error.toVector())
                  - m_Kd.cwiseProduct(m_v_error.toVector())
                  + m_wMl.actInv(m_a_ref).toVector();
      } else {
184
        m_v_error = m_wMl.act(v_frame) - m_v_ref;  // vel err in local world-oriented frame
185
186
187
188
189
190
191
192

        m_drift = m_wMl.act(m_drift);

        // desired acc in local oriented frame
        m_a_des = - m_Kp.cwiseProduct(m_p_error.toVector())
                  - m_Kd.cwiseProduct(m_v_error.toVector())
                  + m_a_ref.toVector();

193
194
195
        // Use an explicit temporary `m_J_rotated` here to avoid allocations.
        m_J_rotated.noalias() = m_wMl.toActionMatrix() * m_J;
        m_J = m_J_rotated;
196
197
198
199
200
      }

      m_v_error_vec = m_v_error.toVector();
      m_v_ref_vec = m_v_ref.toVector();
      m_v = v_frame.toVector();
201

202
203
      m_constraint.setMatrix(m_mask.asDiagonal() * m_J);
      m_constraint.setVector(m_mask.asDiagonal() * (m_a_des - m_drift.toVector()));
204
205
206
207
      return m_constraint;
    }
  }
}