model.hxx 12.5 KB
Newer Older
1
//
jcarpent's avatar
jcarpent committed
2
// Copyright (c) 2015-2016 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

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

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

30
31
namespace se3
{
32
33
34
35
36
37
38
39
40
41
42
43
  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); }
    };
  }

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

jcarpent's avatar
jcarpent committed
70
71
72
73
    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());
74
75
76
    
    Model::JointIndex idx = (Model::JointIndex) (njoints++);
    
jcarpent's avatar
jcarpent committed
77
78
79
    joints         .push_back(JointModel(joint_model.derived()));
    boost::get<JointModelDerived>(joints.back()).setIndexes(idx,nq,nv);
    
80
    inertias       .push_back(Inertia::Zero());
81
    parents        .push_back(parent);
jcarpent's avatar
jcarpent committed
82
    jointPlacements.push_back(joint_placement);
83
    names          .push_back(joint_name);
jcarpent's avatar
jcarpent committed
84
85
    nq += joint_model.nq();
    nv += joint_model.nv();
86

87
88
89
90
91
92
93
    // 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.
    effortLimit.conservativeResize(nv);effortLimit.bottomRows(joint_model.nv()) = max_effort;
    velocityLimit.conservativeResize(nv);velocityLimit.bottomRows(joint_model.nv()) = max_velocity;
    lowerPositionLimit.conservativeResize(nq);lowerPositionLimit.bottomRows(joint_model.nq()) = min_config;
    upperPositionLimit.conservativeResize(nq);upperPositionLimit.bottomRows(joint_model.nq()) = max_config;
94
    
95
    neutralConfiguration.conservativeResize(nq);
jcarpent's avatar
jcarpent committed
96
    neutralConfiguration.tail(joint_model.nq()) = joint_model.neutralConfiguration();
97
98
99
100
101
102

    rotorMass.conservativeResize(nv);
    rotorGearRatio.conservativeResize(nv);

    rotorMass.tail(joint_model.nv()).setZero();
    rotorGearRatio.tail(joint_model.nv()).setZero();
jcarpent's avatar
jcarpent committed
103
    
jcarpent's avatar
jcarpent committed
104
105
106
107
    // Init and add joint index to its parent subtrees.
    subtrees.push_back(IndexVector(1));
    subtrees[idx][0] = idx;
    addJointIndexToParentSubtrees(idx);
108
109
110
    return idx;
  }

111
112
113
114
115
116
117
118
119
  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;

120
121
122
123
124
    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());

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

141

jcarpent's avatar
jcarpent committed
142
143
  inline void Model::appendBodyToJoint(const Model::JointIndex joint_index,
                                       const Inertia & Y,
144
                                       const SE3 & body_placement)
145
  {
jcarpent's avatar
jcarpent committed
146
147
    const Inertia & iYf = Y.se3Action(body_placement);
    inertias[joint_index] += iYf;
148
    nbodies++;
149
  }
150

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


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

189
  inline const std::string& Model::getJointName (const JointIndex index) const
190
  {
191
    assert( index < (Model::JointIndex)joints.size() );
192
193
194
    return names[index];
  }

195
  inline Model::FrameIndex Model::getFrameId ( const std::string & name, const FrameType & type ) const
196
  {
197
    container::aligned_vector<Frame>::const_iterator it = std::find_if( frames.begin()
198
                                                        , frames.end()
199
                                                        , details::FilterFrame (name, type)
200
                                                        );
201
202
203
    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");
204
    return Model::FrameIndex(it - frames.begin());
205
206
  }

207
  inline bool Model::existFrame ( const std::string & name, const FrameType & type) const
208
  {
209
210
    return std::find_if( frames.begin(), frames.end(),
        details::FilterFrame (name, type)) != frames.end();
211
212
213
  }


214
  inline int Model::addFrame ( const Frame & frame )
215
  {
216
    if( !existFrame(frame.name, frame.type) )
217
    {
218
      frames.push_back(frame);
219
220
      nframes++;
      return nframes - 1;
221
222
223
    }
    else
    {
224
      return -1;
225
    }
226
  }
jcarpent's avatar
jcarpent committed
227
228
229
230
231
232
  
  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);
  }
233
234


235
  inline Data::Data (const Model & model)
236
    :joints(0)
237
238
239
240
241
242
    ,a((std::size_t)model.njoints)
    ,a_gf((std::size_t)model.njoints)
    ,v((std::size_t)model.njoints)
    ,f((std::size_t)model.njoints)
    ,oMi((std::size_t)model.njoints)
    ,liMi((std::size_t)model.njoints)
243
244
    ,tau(model.nv)
    ,nle(model.nv)
245
246
    ,oMf((std::size_t)model.nframes)
    ,Ycrb((std::size_t)model.njoints)
247
    ,dYcrb((std::size_t)model.njoints)
248
249
    ,M(model.nv,model.nv)
    ,ddq(model.nv)
250
    ,Yaba((std::size_t)model.njoints)
251
252
    ,u(model.nv)
    ,Ag(6,model.nv)
253
    ,dAg(6,model.nv)
254
255
256
    ,Fcrb((std::size_t)model.njoints)
    ,lastChild((std::size_t)model.njoints)
    ,nvSubtree((std::size_t)model.njoints)
257
258
259
260
261
262
    ,U(model.nv,model.nv)
    ,D(model.nv)
    ,tmp(model.nv)
    ,parents_fromRow((std::size_t)model.nv)
    ,nvSubtree_fromRow((std::size_t)model.nv)
    ,J(6,model.nv)
263
    ,dJ(6,model.nv)
264
265
266
267
268
    ,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)
269
    ,Jcom(3,model.nv)
270
271
    ,JMinvJt()
    ,llt_JMinvJt()
272
    ,lambda_c()
273
274
275
    ,sDUiJt(model.nv,model.nv)
    ,torque_residual(model.nv)
    ,dq_after(model.nv)
276
    ,impulse_c()
277
    ,staticRegressor(3,4*(model.njoints-1))
278
279
  {
    /* Create data strcture associated to the joints */
280
    for(Model::Index i=0;i<(Model::JointIndex)(model.njoints);++i) 
281
      joints.push_back(CreateJointData::run(model.joints[i]));
282
283
284

    /* Init for CRBA */
    M.fill(0);
285
    for(Model::Index i=0;i<(Model::Index)(model.njoints);++i ) { Fcrb[i].resize(6,model.nv); }
286
    computeLastChild(model);
287
288

    /* Init for Cholesky */
289
    U.setIdentity();
290
    computeParents_fromRow(model);
291
292

    /* Init Jacobian */
jcarpent's avatar
jcarpent committed
293
294
    J.setZero();
    Ag.setZero();
295
296
297
298
299
    
    /* Init universe states relatively to itself */
    
    a[0].setZero();
    v[0].setZero();
300
    a_gf[0] = -model.gravity;
301
302
303
    f[0].setZero();
    oMi[0].setIdentity();
    liMi[0].setIdentity();
304
    oMf[0].setIdentity();
305
306
307
308
309
310
  }

  inline void Data::computeLastChild (const Model & model)
  {
    typedef Model::Index Index;
    std::fill(lastChild.begin(),lastChild.end(),-1);
311
    for( int i=model.njoints-1;i>=0;--i )
312
313
314
315
316
317
318
319
320
321
322
323
324
    {
      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)
  {
325
    for( Model::Index joint=1;joint<(Model::Index)(model.njoints);joint++)
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
    {
      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
345
346
/// @endcond

347
#endif // ifndef __se3_model_hxx__