model.hxx 13.6 KB
Newer Older
1
//
2
// Copyright (c) 2015-2018 CNRS
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Copyright (c) 2015 Wandercraft, 86 rue de Paris 91400 Orsay, France.
//
// This file is part of Pinocchio
// Pinocchio 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.
//
// Pinocchio 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
// Pinocchio If not, see
// <http://www.gnu.org/licenses/>.

#ifndef __se3_model_hxx__
#define __se3_model_hxx__

#include "pinocchio/spatial/fwd.hpp"
jcarpent's avatar
jcarpent committed
23
#include "pinocchio/tools/string-generator.hpp"
24
#include "pinocchio/multibody/liegroup/liegroup-algo.hpp"
25

26
#include <boost/bind.hpp>
27
#include <boost/utility.hpp>
28

jcarpent's avatar
jcarpent committed
29
30
/// @cond DEV

31
32
namespace se3
{
33
34
35
36
37
38
39
40
41
42
43
44
  namespace details
  {
    struct FilterFrame {
      const std::string& name;
      const FrameType & typeMask;
      FilterFrame (const std::string& name, const FrameType& typeMask)
        : name (name), typeMask (typeMask) {}
      bool operator() (const Frame& frame) const
      { return (typeMask & frame.type) && (name == frame.name); }
    };
  }

45
46
  inline std::ostream& operator<< (std::ostream & os, const Model & model)
  {
47
48
    os << "Nb joints = " << model.njoints << " (nq="<< model.nq<<",nv="<<model.nv<<")" << std::endl;
    for(Model::Index i=0;i<(Model::Index)(model.njoints);++i)
jcarpent's avatar
jcarpent committed
49
50
51
52
    {
      os << "  Joint "<< model.names[i] << ": parent=" << model.parents[i]  << std::endl;
    }
    
53
54
    return os;
  }
55
  
jcarpent's avatar
jcarpent committed
56
57
58
59
  template<typename JointModelDerived>
  Model::JointIndex Model::addJoint(const Model::JointIndex parent,
                                    const JointModelBase<JointModelDerived> & joint_model,
                                    const SE3 & joint_placement,
60
                                    const std::string & joint_name,
jcarpent's avatar
jcarpent committed
61
62
63
                                    const Eigen::VectorXd & max_effort,
                                    const Eigen::VectorXd & max_velocity,
                                    const Eigen::VectorXd & min_config,
64
                                    const Eigen::VectorXd & max_config
jcarpent's avatar
jcarpent committed
65
                                    )
66
  {
67
68
    assert( (njoints==(int)joints.size())&&(njoints==(int)inertias.size())
           &&(njoints==(int)parents.size())&&(njoints==(int)jointPlacements.size()) );
jcarpent's avatar
jcarpent committed
69
    assert((joint_model.nq()>=0) && (joint_model.nv()>=0));
70

jcarpent's avatar
jcarpent committed
71
72
73
74
    assert(max_effort.size() == joint_model.nv()
           && max_velocity.size() == joint_model.nv()
           && min_config.size() == joint_model.nq()
           && max_config.size() == joint_model.nq());
75
76
77
    
    Model::JointIndex idx = (Model::JointIndex) (njoints++);
    
jcarpent's avatar
jcarpent committed
78
    joints         .push_back(JointModel(joint_model.derived()));
79
80
    JointModelDerived & jmodel = boost::get<JointModelDerived>(joints.back());
    jmodel.setIndexes(idx,nq,nv);
jcarpent's avatar
jcarpent committed
81
    
82
    inertias       .push_back(Inertia::Zero());
83
    parents        .push_back(parent);
jcarpent's avatar
jcarpent committed
84
    jointPlacements.push_back(joint_placement);
85
    names          .push_back(joint_name);
jcarpent's avatar
jcarpent committed
86
87
    nq += joint_model.nq();
    nv += joint_model.nv();
88

89
90
91
    // Optimal efficiency here would be using the static-dim bottomRows, while specifying the dimension in argument in the case where D::NV is Eigen::Dynamic.
    // However, this option is not compiling in Travis (why?).
    // As efficiency of Model::addJoint is not critical, the dynamic bottomRows is used here.
92
    effortLimit.conservativeResize(nv);
93
    jmodel.jointVelocitySelector(effortLimit) = max_effort;
94
    velocityLimit.conservativeResize(nv);
95
    jmodel.jointVelocitySelector(velocityLimit) = max_velocity;
96
97
98
99
    lowerPositionLimit.conservativeResize(nq);
    jmodel.jointConfigSelector(lowerPositionLimit) = min_config;
    upperPositionLimit.conservativeResize(nq);
    jmodel.jointConfigSelector(upperPositionLimit) = max_config;
100
    
101
    neutralConfiguration.conservativeResize(nq);
102
    NeutralStepAlgo<LieGroupTpl,JointModelDerived>::run(jmodel,neutralConfiguration);
103

104
    rotorInertia.conservativeResize(nv);
105
    jmodel.jointVelocitySelector(rotorInertia).setZero();
106
    rotorGearRatio.conservativeResize(nv);
107
    jmodel.jointVelocitySelector(rotorGearRatio).setZero();
jcarpent's avatar
jcarpent committed
108
    
jcarpent's avatar
jcarpent committed
109
110
111
112
    // Init and add joint index to its parent subtrees.
    subtrees.push_back(IndexVector(1));
    subtrees[idx][0] = idx;
    addJointIndexToParentSubtrees(idx);
113
114
115
    return idx;
  }

116
117
118
119
120
121
122
123
124
  template<typename JointModelDerived>
  Model::JointIndex Model::addJoint(const Model::JointIndex parent,
                                    const JointModelBase<JointModelDerived> & joint_model,
                                    const SE3 & joint_placement,
                                    const std::string & joint_name
                                    )
  {
    Eigen::VectorXd max_effort, max_velocity, min_config, max_config;

125
126
127
128
129
    max_effort = Eigen::VectorXd::Constant(joint_model.nv(), std::numeric_limits<double>::max());
    max_velocity = Eigen::VectorXd::Constant(joint_model.nv(), std::numeric_limits<double>::max());
    min_config = Eigen::VectorXd::Constant(joint_model.nq(), std::numeric_limits<double>::max());
    max_config = Eigen::VectorXd::Constant(joint_model.nq(), std::numeric_limits<double>::max());

130
    return addJoint(parent, joint_model, joint_placement, joint_name, max_effort, max_velocity, min_config, max_config);
131
132
  }
  
133
134
  inline int Model::addJointFrame (const JointIndex& jidx,
                                         int         fidx)
135
136
  {
    if (fidx < 0) {
137
138
      // FIXED_JOINT is required because the parent can be the universe and its
      // type is FIXED_JOINT
139
      fidx = (int)getFrameId(names[parents[jidx]], (FrameType)(JOINT | FIXED_JOINT));
140
    }
141
    assert((size_t)fidx < frames.size() && "Frame index out of bound");
142
    // Add a the joint frame attached to itself to the frame vector - redundant information but useful.
143
    return addFrame(Frame(names[jidx],jidx,(FrameIndex)fidx,SE3::Identity(),JOINT));
144
145
  }

146

jcarpent's avatar
jcarpent committed
147
148
  inline void Model::appendBodyToJoint(const Model::JointIndex joint_index,
                                       const Inertia & Y,
149
                                       const SE3 & body_placement)
150
  {
jcarpent's avatar
jcarpent committed
151
152
    const Inertia & iYf = Y.se3Action(body_placement);
    inertias[joint_index] += iYf;
153
    nbodies++;
154
  }
155

156
157
158
159
  inline int Model::addBodyFrame (const std::string & body_name,
                                  const JointIndex  & parentJoint,
                                  const SE3         & body_placement,
                                        int           previousFrame)
160
161
  {
    if (previousFrame < 0) {
162
163
      // FIXED_JOINT is required because the parent can be the universe and its
      // type is FIXED_JOINT
164
      previousFrame = (int)getFrameId(names[parentJoint], (FrameType)(JOINT | FIXED_JOINT));
165
    }
166
167
    assert((size_t)previousFrame < frames.size() && "Frame index out of bound");
    return addFrame(Frame(body_name, parentJoint, (FrameIndex)previousFrame, body_placement, BODY));
168
  }
jcarpent's avatar
jcarpent committed
169
  
170
  inline Model::JointIndex Model::getBodyId (const std::string & name) const
171
  {
172
    return getFrameId(name, BODY);
173
174
175
176
  }
  
  inline bool Model::existBodyName (const std::string & name) const
  {
177
    return existFrame(name, BODY);
178
179
180
  }


181
  inline Model::JointIndex Model::getJointId (const std::string & name) const
182
  {
jcarpent's avatar
jcarpent committed
183
184
    typedef std::vector<std::string>::iterator::difference_type it_diff_t;
    it_diff_t res = std::find(names.begin(),names.end(),name) - names.begin();
185
    assert( (res<INT_MAX) && "Id superior to int range. Should never happen.");
186
    return Model::JointIndex(res);
187
188
189
190
191
192
193
  }
  
  inline bool Model::existJointName (const std::string & name) const
  {
    return (names.end() != std::find(names.begin(),names.end(),name));
  }

194
  inline const std::string& Model::getJointName (const JointIndex index) const
195
  {
196
    assert( index < (Model::JointIndex)joints.size() );
197
198
199
    return names[index];
  }

200
  inline Model::FrameIndex Model::getFrameId ( const std::string & name, const FrameType & type ) const
201
  {
202
    container::aligned_vector<Frame>::const_iterator it = std::find_if( frames.begin()
203
                                                        , frames.end()
204
                                                        , details::FilterFrame (name, type)
205
                                                        );
206
207
208
    assert (it != frames.end() && "Frame not found");
    assert ((std::find_if( boost::next(it), frames.end(), details::FilterFrame (name, type)) == frames.end())
        && "Several frames match the filter");
209
    return Model::FrameIndex(it - frames.begin());
210
211
  }

212
  inline bool Model::existFrame ( const std::string & name, const FrameType & type) const
213
  {
214
215
    return std::find_if( frames.begin(), frames.end(),
        details::FilterFrame (name, type)) != frames.end();
216
217
218
  }


219
  inline int Model::addFrame ( const Frame & frame )
220
  {
221
    if( !existFrame(frame.name, frame.type) )
222
    {
223
      frames.push_back(frame);
224
225
      nframes++;
      return nframes - 1;
226
227
228
    }
    else
    {
229
      return -1;
230
    }
231
  }
jcarpent's avatar
jcarpent committed
232
233
234
235
236
237
  
  inline void Model::addJointIndexToParentSubtrees(const JointIndex joint_id)
  {
    for(JointIndex parent = parents[joint_id]; parent>0; parent = parents[parent])
      subtrees[parent].push_back(joint_id);
  }
238
239


240
  inline Data::Data (const Model & model)
241
    :joints(0)
242
    ,a((std::size_t)model.njoints)
243
    ,oa((std::size_t)model.njoints)
244
245
    ,a_gf((std::size_t)model.njoints)
    ,v((std::size_t)model.njoints)
246
    ,ov((std::size_t)model.njoints)
247
    ,f((std::size_t)model.njoints)
248
    ,of((std::size_t)model.njoints)
249
    ,h((std::size_t)model.njoints)
250
    ,oh((std::size_t)model.njoints)
251
252
    ,oMi((std::size_t)model.njoints)
    ,liMi((std::size_t)model.njoints)
253
254
    ,tau(model.nv)
    ,nle(model.nv)
255
    ,g(model.nv)
256
257
    ,oMf((std::size_t)model.nframes)
    ,Ycrb((std::size_t)model.njoints)
258
    ,dYcrb((std::size_t)model.njoints)
259
    ,M(model.nv,model.nv)
260
    ,Minv(model.nv,model.nv)
261
    ,C(model.nv,model.nv)
262
    ,dFdq(6,model.nv)
263
    ,dFdv(6,model.nv)
264
    ,dFda(6,model.nv)
265
266
267
    ,SDinv(6,model.nv)
    ,UDinv(6,model.nv)
    ,IS(6,model.nv)
268
269
    ,vxI((std::size_t)model.njoints)
    ,Ivx((std::size_t)model.njoints)
jcarpent's avatar
jcarpent committed
270
271
    ,oYcrb((std::size_t)model.njoints)
    ,doYcrb((std::size_t)model.njoints)
272
    ,ddq(model.nv)
273
    ,Yaba((std::size_t)model.njoints)
274
275
    ,u(model.nv)
    ,Ag(6,model.nv)
276
    ,dAg(6,model.nv)
277
278
279
    ,Fcrb((std::size_t)model.njoints)
    ,lastChild((std::size_t)model.njoints)
    ,nvSubtree((std::size_t)model.njoints)
280
281
    ,U(model.nv,model.nv)
    ,D(model.nv)
282
    ,Dinv(model.nv)
283
284
285
286
    ,tmp(model.nv)
    ,parents_fromRow((std::size_t)model.nv)
    ,nvSubtree_fromRow((std::size_t)model.nv)
    ,J(6,model.nv)
287
    ,dJ(6,model.nv)
288
289
290
    ,dVdq(6,model.nv)
    ,dAdq(6,model.nv)
    ,dAdv(6,model.nv)
291
292
    ,dtau_dq(Eigen::MatrixXd::Zero(model.nv,model.nv))
    ,dtau_dv(Eigen::MatrixXd::Zero(model.nv,model.nv))
293
294
    ,ddq_dq(Eigen::MatrixXd::Zero(model.nv,model.nv))
    ,ddq_dv(Eigen::MatrixXd::Zero(model.nv,model.nv))
295
296
297
298
299
    ,iMf((std::size_t)model.njoints)
    ,com((std::size_t)model.njoints)
    ,vcom((std::size_t)model.njoints)
    ,acom((std::size_t)model.njoints)
    ,mass((std::size_t)model.njoints)
300
    ,Jcom(3,model.nv)
301
302
    ,JMinvJt()
    ,llt_JMinvJt()
303
    ,lambda_c()
304
305
306
    ,sDUiJt(model.nv,model.nv)
    ,torque_residual(model.nv)
    ,dq_after(model.nv)
307
    ,impulse_c()
308
    ,staticRegressor(3,4*(model.njoints-1))
309
310
  {
    /* Create data strcture associated to the joints */
311
    for(Model::Index i=0;i<(Model::JointIndex)(model.njoints);++i) 
312
      joints.push_back(CreateJointData::run(model.joints[i]));
313
314

    /* Init for CRBA */
315
    M.fill(0); Minv.setZero();
316
    for(Model::Index i=0;i<(Model::Index)(model.njoints);++i ) { Fcrb[i].resize(6,model.nv); }
317
    computeLastChild(model);
318
319
320
    
    /* Init for Coriolis */
    C.fill(0.);
321
322

    /* Init for Cholesky */
323
    U.setIdentity();
324
    computeParents_fromRow(model);
325
326

    /* Init Jacobian */
jcarpent's avatar
jcarpent committed
327
328
    J.setZero();
    Ag.setZero();
329
330
331
332
    
    /* Init universe states relatively to itself */
    
    a[0].setZero();
333
    oa[0].setZero();
334
    v[0].setZero();
335
    ov[0].setZero();
336
    a_gf[0] = -model.gravity;
337
    f[0].setZero();
338
    h[0].setZero();
339
340
    oMi[0].setIdentity();
    liMi[0].setIdentity();
341
    oMf[0].setIdentity();
342
343
344
345
346
347
  }

  inline void Data::computeLastChild (const Model & model)
  {
    typedef Model::Index Index;
    std::fill(lastChild.begin(),lastChild.end(),-1);
348
    for( int i=model.njoints-1;i>=0;--i )
349
350
351
352
353
354
355
356
357
358
359
360
361
    {
      if(lastChild[(Index)i] == -1) lastChild[(Index)i] = i;
      const Index & parent = model.parents[(Index)i];
      lastChild[parent] = std::max(lastChild[(Index)i],lastChild[parent]);
      
      nvSubtree[(Index)i]
      = idx_v(model.joints[(Index)lastChild[(Index)i]]) + nv(model.joints[(Index)lastChild[(Index)i]])
      - idx_v(model.joints[(Index)i]);
    }
  }

  inline void Data::computeParents_fromRow (const Model & model)
  {
362
    for( Model::Index joint=1;joint<(Model::Index)(model.njoints);joint++)
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    {
      const Model::Index & parent = model.parents[joint];
      const int nvj    = nv   (model.joints[joint]);
      const int idx_vj = idx_v(model.joints[joint]);
      
      if(parent>0) parents_fromRow[(Model::Index)idx_vj] = idx_v(model.joints[parent])+nv(model.joints[parent])-1;
      else         parents_fromRow[(Model::Index)idx_vj] = -1;
      nvSubtree_fromRow[(Model::Index)idx_vj] = nvSubtree[joint];
      
      for(int row=1;row<nvj;++row)
      {
        parents_fromRow[(Model::Index)(idx_vj+row)] = idx_vj+row-1;
        nvSubtree_fromRow[(Model::Index)(idx_vj+row)] = nvSubtree[joint]-row;
      }
    }
  }

} // namespace se3

jcarpent's avatar
jcarpent committed
382
383
/// @endcond

384
#endif // ifndef __se3_model_hxx__