model.hxx 11.2 KB
Newer Older
1
//
2
// Copyright (c) 2015-2018 CNRS
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
52
53
    {
      os << "  Joint "<< model.names[i] << ": parent=" << model.parents[i]  << std::endl;
    }
    
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
61
62
63
64
65
66
67
68
  typename ModelTpl<Scalar,Options,JointCollectionTpl>::JointIndex
  ModelTpl<Scalar,Options,JointCollectionTpl>::addJoint(const JointIndex parent,
                                                     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

jcarpent's avatar
jcarpent committed
74
75
76
77
    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());
78
    
79
    JointIndex idx = (JointIndex)(njoints++);
80
    
jcarpent's avatar
jcarpent committed
81
    joints         .push_back(JointModel(joint_model.derived()));
82
83
    JointModelDerived & jmodel = boost::get<JointModelDerived>(joints.back());
    jmodel.setIndexes(idx,nq,nv);
jcarpent's avatar
jcarpent committed
84
    
85
    inertias       .push_back(Inertia::Zero());
86
    parents        .push_back(parent);
jcarpent's avatar
jcarpent committed
87
    jointPlacements.push_back(joint_placement);
88
    names          .push_back(joint_name);
jcarpent's avatar
jcarpent committed
89
90
    nq += joint_model.nq();
    nv += joint_model.nv();
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?).
94
    // As efficiency of ModelTpl::addJoint is not critical, the dynamic bottomRows is used here.
95
    effortLimit.conservativeResize(nv);
96
    jmodel.jointVelocitySelector(effortLimit) = max_effort;
97
    velocityLimit.conservativeResize(nv);
98
    jmodel.jointVelocitySelector(velocityLimit) = max_velocity;
99
100
101
102
    lowerPositionLimit.conservativeResize(nq);
    jmodel.jointConfigSelector(lowerPositionLimit) = min_config;
    upperPositionLimit.conservativeResize(nq);
    jmodel.jointConfigSelector(upperPositionLimit) = max_config;
103
    
104
    neutralConfiguration.conservativeResize(nq);
105
106
    typedef NeutralStep<LieGroupMap,ConfigVectorType> NeutralVisitor;
    NeutralStepAlgo<NeutralVisitor,JointModelDerived>::run(jmodel,neutralConfiguration);
107

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

120
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
121
  template<typename JointModelDerived>
122
123
124
125
126
  typename ModelTpl<Scalar,Options,JointCollectionTpl>::JointIndex
  ModelTpl<Scalar,Options,JointCollectionTpl>::addJoint(const JointIndex parent,
                                                     const JointModelBase<JointModelDerived> & joint_model,
                                                     const SE3 & joint_placement,
                                                     const std::string & joint_name)
127
  {
128
    VectorXs max_effort, max_velocity, min_config, max_config;
129

130
131
132
133
    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());
134

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

153
154
155
156
157
  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)
158
  {
jcarpent's avatar
jcarpent committed
159
160
    const Inertia & iYf = Y.se3Action(body_placement);
    inertias[joint_index] += iYf;
161
    nbodies++;
162
  }
163

164
165
166
167
168
169
  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)
170
  {
jcarpent's avatar
jcarpent committed
171
    if(previousFrame < 0) {
172
173
      // FIXED_JOINT is required because the parent can be the universe and its
      // type is FIXED_JOINT
174
      previousFrame = (int)getFrameId(names[parentJoint], (FrameType)(JOINT | FIXED_JOINT));
175
    }
176
177
    assert((size_t)previousFrame < frames.size() && "Frame index out of bound");
    return addFrame(Frame(body_name, parentJoint, (FrameIndex)previousFrame, body_placement, BODY));
178
  }
jcarpent's avatar
jcarpent committed
179
  
180
181
182
183
  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
184
  {
185
    return getFrameId(name, BODY);
186
187
  }
  
188
189
190
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline bool ModelTpl<Scalar,Options,JointCollectionTpl>::
  existBodyName(const std::string & name) const
191
  {
192
    return existFrame(name, BODY);
193
194
  }

195
196
197
198
  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
199
  {
jcarpent's avatar
jcarpent committed
200
201
    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
202
    assert((res<INT_MAX) && "Id superior to int range. Should never happen.");
203
    return ModelTpl::JointIndex(res);
204
205
  }
  
206
207
208
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline bool ModelTpl<Scalar,Options,JointCollectionTpl>::
  existJointName(const std::string & name) const
209
210
211
212
  {
    return (names.end() != std::find(names.begin(),names.end(),name));
  }

213
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
214
  inline const std::string &
215
216
  ModelTpl<Scalar,Options,JointCollectionTpl>::
  getJointName(const JointIndex index) const
217
  {
218
    assert( index < (ModelTpl::JointIndex)joints.size() );
219
220
221
    return names[index];
  }

222
223
224
225
  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
226
  {
227
228
229
230
    typename container::aligned_vector<Frame>::const_iterator it
    = std::find_if(frames.begin()
                   ,frames.end()
                   ,details::FilterFrame(name, type));
jcarpent's avatar
jcarpent committed
231
232
    assert(it != frames.end() && "Frame not found");
    assert((std::find_if( boost::next(it), frames.end(), details::FilterFrame(name, type)) == frames.end())
233
        && "Several frames match the filter");
234
    return FrameIndex(it - frames.begin());
235
  }
236
  
237
238
239
  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
240
  {
241
242
    return std::find_if(frames.begin(), frames.end(),
                        details::FilterFrame(name, type)) != frames.end();
243
244
  }

245
246
247
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline int ModelTpl<Scalar,Options,JointCollectionTpl>::
  addFrame(const Frame & frame)
248
  {
249
    if(!existFrame(frame.name, frame.type))
250
    {
251
      frames.push_back(frame);
252
253
      nframes++;
      return nframes - 1;
254
255
256
    }
    else
    {
257
      return -1;
258
    }
259
  }
jcarpent's avatar
jcarpent committed
260
  
261
262
263
  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
264
265
266
267
  {
    for(JointIndex parent = parents[joint_id]; parent>0; parent = parents[parent])
      subtrees[parent].push_back(joint_id);
  }
268

269
} // namespace pinocchio
270

jcarpent's avatar
jcarpent committed
271
272
/// @endcond

273
#endif // ifndef __pinocchio_model_hxx__