task-se3-equality.cpp 6.69 KB
Newer Older
1
//
2
// Copyright (c) 2017-2020 CNRS, NYU, MPI Tübingen, Inria
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
    using namespace std;
27
28
    using namespace math;
    using namespace trajectories;
29
    using namespace pinocchio;
30
31
32
33
34
35

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

      m_mask.resize(6);
59
      m_mask.fill(1.);
60
      setMask(m_mask);
61
62

      m_local_frame = true;
63
64
    }

65
66
67
    void TaskSE3Equality::setMask(math::ConstRefVector mask)
    {
      TaskMotion::setMask(mask);
68
69
70
71
72
73
      int n = dim();
      m_constraint.resize(n, (unsigned int)m_J.cols());
      m_p_error_masked_vec.resize(n);
      m_v_error_masked_vec.resize(n);
      m_drift_masked.resize(n);
      m_a_des_masked.resize(n);
74
75
    }

76
77
    int TaskSE3Equality::dim() const
    {
Andrea Del Prete's avatar
Andrea Del Prete committed
78
      return (int)m_mask.sum();
79
80
    }

81
    const Vector & TaskSE3Equality::Kp() const { return m_Kp; }
82

83
    const Vector & TaskSE3Equality::Kd() const { return m_Kd; }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

    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
99
      m_ref = ref;
100
101
102
103
104
      vectorToSE3(ref.pos, m_M_ref);
      m_v_ref = Motion(ref.vel);
      m_a_ref = Motion(ref.acc);
    }

andreadelprete's avatar
andreadelprete committed
105
106
107
108
109
    const TrajectorySample & TaskSE3Equality::getReference() const
    {
      return m_ref;
    }

110
    const Vector & TaskSE3Equality::position_error() const
111
    {
112
      return m_p_error_masked_vec;
113
114
    }

115
    const Vector & TaskSE3Equality::velocity_error() const
116
    {
117
      return m_v_error_masked_vec;
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    }

    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
140
141
    const Vector & TaskSE3Equality::getDesiredAcceleration() const
    {
142
      return m_a_des_masked;
andreadelprete's avatar
andreadelprete committed
143
144
145
146
    }

    Vector TaskSE3Equality::getAcceleration(ConstRefVector dv) const
    {
147
      return m_constraint.matrix()*dv + m_drift_masked;
andreadelprete's avatar
andreadelprete committed
148
149
    }

150
151
152
153
154
    Index TaskSE3Equality::frame_id() const
    {
      return m_frame_id;
    }

155
156
157
    const ConstraintBase & TaskSE3Equality::getConstraint() const
    {
      return m_constraint;
158
159
    }

160
161
162
163
164
    void TaskSE3Equality::useLocalFrame(bool local_frame)
    {
      m_local_frame = local_frame;
    }

Olivier Stasse's avatar
Olivier Stasse committed
165
166
167
    const ConstraintBase & TaskSE3Equality::compute(const double ,
                                                    ConstRefVector ,
                                                    ConstRefVector ,
168
                                                    Data & data)
169
170
    {
      SE3 oMi;
andreadelprete's avatar
andreadelprete committed
171
      Motion v_frame;
172
173
      m_robot.framePosition(data, m_frame_id, oMi);
      m_robot.frameVelocity(data, m_frame_id, v_frame);
andreadelprete's avatar
andreadelprete committed
174
      m_robot.frameClassicAcceleration(data, m_frame_id, m_drift);
175

176
177
178
      // @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);
179
180

      errorInSE3(oMi, m_M_ref, m_p_error);          // pos err in local frame
181
182
      SE3ToVector(m_M_ref, m_p_ref);
      SE3ToVector(oMi, m_p);
183

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

187
      if (m_local_frame) {
188
189
        m_p_error_vec = m_p_error.toVector();
        m_v_error =  m_wMl.actInv(m_v_ref) - v_frame;  // vel err in local frame
190

191
        // desired acc in local frame
192
193
        m_a_des = m_Kp.cwiseProduct(m_p_error_vec)
                  + m_Kd.cwiseProduct(m_v_error.toVector())
194
195
                  + m_wMl.actInv(m_a_ref).toVector();
      } else {
196
197
        m_p_error_vec = m_wMl.toActionMatrix() *   // pos err in local world-oriented frame
            m_p_error.toVector();
198

199
200
201
202
        cout<<"m_p_error_vec="<<m_p_error_vec.head<3>().transpose()<<endl;
        cout<<"oMi-m_M_ref  ="<<-(oMi.translation()-m_M_ref.translation()).transpose()<<endl;

        m_v_error = m_v_ref - m_wMl.act(v_frame);  // vel err in local world-oriented frame
203
204
205

        m_drift = m_wMl.act(m_drift);

206
        // desired acc in local world-oriented frame
207
208
        m_a_des = m_Kp.cwiseProduct(m_p_error_vec)
                  + m_Kd.cwiseProduct(m_v_error.toVector())
209
210
                  + m_a_ref.toVector();

211
212
213
        // 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;
214
215
216
217
218
      }

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

220
221
222
223
      int idx = 0;
      for (int i = 0; i < 6; i++) {
        if (m_mask(i) != 1.) continue;

224
225
        m_constraint.matrix().row(idx) = m_J.row(i);
        m_constraint.vector().row(idx) = (m_a_des - m_drift.toVector()).row(i);
226
227
228
229
        m_a_des_masked(idx)            = m_a_des(i);
        m_drift_masked(idx)            = m_drift.toVector()(i);
        m_p_error_masked_vec(idx)      = m_p_error_vec(i);
        m_v_error_masked_vec(idx)      = m_v_error_vec(i);
230
231
232

        idx += 1;
      }
233
      
234
235
236
237
      return m_constraint;
    }
  }
}