model.hxx 11.4 KB
Newer Older
1
//
2
// Copyright (c) 2015-2019 CNRS INRIA
3
4
5
// Copyright (c) 2015 Wandercraft, 86 rue de Paris 91400 Orsay, France.
//

6
7
#ifndef __pinocchio_model_hxx__
#define __pinocchio_model_hxx__
8
9

#include "pinocchio/spatial/fwd.hpp"
jcarpent's avatar
jcarpent committed
10
#include "pinocchio/utils/string-generator.hpp"
11
#include "pinocchio/multibody/liegroup/liegroup-algo.hpp"
12

13
#include <boost/bind.hpp>
14
#include <boost/utility.hpp>
15

jcarpent's avatar
jcarpent committed
16
17
/// @cond DEV

18
namespace pinocchio
19
{
20
21
  namespace details
  {
22
23
24
    struct FilterFrame
    {
      const std::string & name;
25
      const FrameType & typeMask;
26
27
28
29
30
31
32
      
      FilterFrame(const std::string& name, const FrameType & typeMask)
      : name(name), typeMask(typeMask)
      {}
      
      template<typename Scalar, int Options>
      bool operator()(const FrameTpl<Scalar,Options> & frame) const
33
      { return (typeMask & frame.type) && (name == frame.name); }
34
      
35
36
    };
  }
37
38
39
  
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  const typename ModelTpl<Scalar,Options,JointCollectionTpl>::
40
  Vector3 ModelTpl<Scalar,Options,JointCollectionTpl>::gravity981((Scalar)0,(Scalar)0,(Scalar)-9.81);
41

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

jcarpent's avatar
jcarpent committed
75
76
77
78
    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());
79
    
80
    JointIndex idx = (JointIndex)(njoints++);
81
    
jcarpent's avatar
jcarpent committed
82
    joints         .push_back(JointModel(joint_model.derived()));
83
84
    JointModelDerived & jmodel = boost::get<JointModelDerived>(joints.back());
    jmodel.setIndexes(idx,nq,nv);
jcarpent's avatar
jcarpent committed
85
    
86
87
88
89
90
91
92
    const int joint_nq = jmodel.nq();
    const int joint_idx_q = jmodel.idx_q();
    const int joint_nv = jmodel.nv();
    const int joint_idx_v = jmodel.idx_v();
    
    assert(joint_idx_q >= 0);
    assert(joint_idx_v >= 0);
Justin Carpentier's avatar
Justin Carpentier committed
93

94
    inertias       .push_back(Inertia::Zero());
95
    parents        .push_back(parent);
jcarpent's avatar
jcarpent committed
96
    jointPlacements.push_back(joint_placement);
97
    names          .push_back(joint_name);
98
99
100
    
    nq += joint_nq; nqs.push_back(joint_nq); idx_qs.push_back(joint_idx_q);
    nv += joint_nv; nvs.push_back(joint_nv); idx_vs.push_back(joint_idx_v);
101

102
    if(joint_nq > 0 && joint_nv > 0)
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    {
      effortLimit.conservativeResize(nv);
      jmodel.jointVelocitySelector(effortLimit) = max_effort;
      velocityLimit.conservativeResize(nv);
      jmodel.jointVelocitySelector(velocityLimit) = max_velocity;
      lowerPositionLimit.conservativeResize(nq);
      jmodel.jointConfigSelector(lowerPositionLimit) = min_config;
      upperPositionLimit.conservativeResize(nq);
      jmodel.jointConfigSelector(upperPositionLimit) = max_config;
      
      rotorInertia.conservativeResize(nv);
      jmodel.jointVelocitySelector(rotorInertia).setZero();
      rotorGearRatio.conservativeResize(nv);
      jmodel.jointVelocitySelector(rotorGearRatio).setZero();
    }
jcarpent's avatar
jcarpent committed
118
    
jcarpent's avatar
jcarpent committed
119
120
121
122
    // Init and add joint index to its parent subtrees.
    subtrees.push_back(IndexVector(1));
    subtrees[idx][0] = idx;
    addJointIndexToParentSubtrees(idx);
123
124
125
126
127
    
    // Init and add joint index to the supports
    supports.push_back(supports[parent]);
    supports[idx].push_back(idx);
    
128
129
130
    return idx;
  }

131
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
132
  template<typename JointModelDerived>
133
134
  typename ModelTpl<Scalar,Options,JointCollectionTpl>::JointIndex
  ModelTpl<Scalar,Options,JointCollectionTpl>::addJoint(const JointIndex parent,
135
136
137
                                                        const JointModelBase<JointModelDerived> & joint_model,
                                                        const SE3 & joint_placement,
                                                        const std::string & joint_name)
138
  {
139
    VectorXs max_effort, max_velocity, min_config, max_config;
140

141
142
143
144
    max_effort = VectorXs::Constant(joint_model.nv(), std::numeric_limits<Scalar>::max());
    max_velocity = VectorXs::Constant(joint_model.nv(), std::numeric_limits<Scalar>::max());
    min_config = VectorXs::Constant(joint_model.nq(), -std::numeric_limits<Scalar>::max());
    max_config = VectorXs::Constant(joint_model.nq(), std::numeric_limits<Scalar>::max());
145

146
    return addJoint(parent, joint_model, joint_placement, joint_name, max_effort, max_velocity, min_config, max_config);
147
148
  }
  
149
150
151
152
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline int ModelTpl<Scalar,Options,JointCollectionTpl>::
  addJointFrame(const JointIndex & jidx,
                int fidx)
153
  {
jcarpent's avatar
jcarpent committed
154
    if(fidx < 0) {
155
156
      // FIXED_JOINT is required because the parent can be the universe and its
      // type is FIXED_JOINT
157
      fidx = (int)getFrameId(names[parents[jidx]], (FrameType)(JOINT | FIXED_JOINT));
158
    }
159
    assert((size_t)fidx < frames.size() && "Frame index out of bound");
160
    // Add a the joint frame attached to itself to the frame vector - redundant information but useful.
161
    return addFrame(Frame(names[jidx],jidx,(FrameIndex)fidx,SE3::Identity(),JOINT));
162
163
  }

164
165
166
167
168
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline void ModelTpl<Scalar,Options,JointCollectionTpl>::
  appendBodyToJoint(const ModelTpl::JointIndex joint_index,
                    const Inertia & Y,
                    const SE3 & body_placement)
169
  {
jcarpent's avatar
jcarpent committed
170
171
    const Inertia & iYf = Y.se3Action(body_placement);
    inertias[joint_index] += iYf;
172
    nbodies++;
173
  }
174

175
176
177
178
179
180
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline int ModelTpl<Scalar,Options,JointCollectionTpl>::
  addBodyFrame(const std::string & body_name,
               const JointIndex  & parentJoint,
               const SE3         & body_placement,
               int           previousFrame)
181
  {
jcarpent's avatar
jcarpent committed
182
    if(previousFrame < 0) {
183
184
      // FIXED_JOINT is required because the parent can be the universe and its
      // type is FIXED_JOINT
185
      previousFrame = (int)getFrameId(names[parentJoint], (FrameType)(JOINT | FIXED_JOINT));
186
    }
187
188
    assert((size_t)previousFrame < frames.size() && "Frame index out of bound");
    return addFrame(Frame(body_name, parentJoint, (FrameIndex)previousFrame, body_placement, BODY));
189
  }
jcarpent's avatar
jcarpent committed
190
  
191
192
193
194
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline typename ModelTpl<Scalar,Options,JointCollectionTpl>::JointIndex
  ModelTpl<Scalar,Options,JointCollectionTpl>::
  getBodyId(const std::string & name) const
195
  {
196
    return getFrameId(name, BODY);
197
198
  }
  
199
200
201
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline bool ModelTpl<Scalar,Options,JointCollectionTpl>::
  existBodyName(const std::string & name) const
202
  {
203
    return existFrame(name, BODY);
204
205
  }

206
207
208
209
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline typename  ModelTpl<Scalar,Options,JointCollectionTpl>::JointIndex
  ModelTpl<Scalar,Options,JointCollectionTpl>::
  getJointId(const std::string & name) const
210
  {
jcarpent's avatar
jcarpent committed
211
212
    typedef std::vector<std::string>::iterator::difference_type it_diff_t;
    it_diff_t res = std::find(names.begin(),names.end(),name) - names.begin();
jcarpent's avatar
jcarpent committed
213
    assert((res<INT_MAX) && "Id superior to int range. Should never happen.");
214
    return ModelTpl::JointIndex(res);
215
216
  }
  
217
218
219
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline bool ModelTpl<Scalar,Options,JointCollectionTpl>::
  existJointName(const std::string & name) const
220
221
222
223
  {
    return (names.end() != std::find(names.begin(),names.end(),name));
  }

224
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
225
  inline const std::string &
226
227
  ModelTpl<Scalar,Options,JointCollectionTpl>::
  getJointName(const JointIndex index) const
228
  {
229
    assert( index < (ModelTpl::JointIndex)joints.size() );
230
231
232
    return names[index];
  }

233
234
235
236
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline typename ModelTpl<Scalar,Options,JointCollectionTpl>::FrameIndex
  ModelTpl<Scalar,Options,JointCollectionTpl>::
  getFrameId(const std::string & name, const FrameType & type) const
237
  {
238
239
240
241
    typename container::aligned_vector<Frame>::const_iterator it
    = std::find_if(frames.begin()
                   ,frames.end()
                   ,details::FilterFrame(name, type));
jcarpent's avatar
jcarpent committed
242
243
    assert(it != frames.end() && "Frame not found");
    assert((std::find_if( boost::next(it), frames.end(), details::FilterFrame(name, type)) == frames.end())
244
        && "Several frames match the filter");
245
    return FrameIndex(it - frames.begin());
246
  }
247
  
248
249
250
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline bool ModelTpl<Scalar,Options,JointCollectionTpl>::
  existFrame(const std::string & name, const FrameType & type) const
251
  {
252
253
    return std::find_if(frames.begin(), frames.end(),
                        details::FilterFrame(name, type)) != frames.end();
254
255
  }

256
257
258
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline int ModelTpl<Scalar,Options,JointCollectionTpl>::
  addFrame(const Frame & frame)
259
  {
260
    if(!existFrame(frame.name, frame.type))
261
    {
262
      frames.push_back(frame);
263
264
      nframes++;
      return nframes - 1;
265
266
267
    }
    else
    {
268
      return -1;
269
    }
270
  }
jcarpent's avatar
jcarpent committed
271
  
272
273
274
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline void ModelTpl<Scalar,Options,JointCollectionTpl>::
  addJointIndexToParentSubtrees(const JointIndex joint_id)
jcarpent's avatar
jcarpent committed
275
276
277
  {
    for(JointIndex parent = parents[joint_id]; parent>0; parent = parents[parent])
      subtrees[parent].push_back(joint_id);
278
279
280
    
    // Also add joint_id to the universe
    subtrees[0].push_back(joint_id);
jcarpent's avatar
jcarpent committed
281
  }
282

283
} // namespace pinocchio
284

jcarpent's avatar
jcarpent committed
285
286
/// @endcond

287
#endif // ifndef __pinocchio_model_hxx__