model.hxx 11.7 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/utils/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
  namespace details
  {
    struct FilterFrame {
      const std::string& name;
      const FrameType & typeMask;
jcarpent's avatar
jcarpent committed
38
39
40
      FilterFrame(const std::string& name, const FrameType& typeMask)
        : name(name), typeMask(typeMask) {}
      bool operator()(const Frame& frame) const
41
42
43
      { return (typeMask & frame.type) && (name == frame.name); }
    };
  }
44
45
46
47
  
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  const typename ModelTpl<Scalar,Options,JointCollectionTpl>::
  Vector3 ModelTpl<Scalar,Options,JointCollectionTpl>::gravity981(0,0,-9.81);
48

49
50
51
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
  inline std::ostream& operator<<(std::ostream & os,
                                  const ModelTpl<Scalar,Options,JointCollectionTpl> & model)
52
  {
53
    typedef typename ModelTpl<Scalar,Options,JointCollectionTpl>::Index Index;
54
    
55
    os << "Nb joints = " << model.njoints << " (nq="<< model.nq<<",nv="<<model.nv<<")" << std::endl;
56
    for(Index i=0;i<(Index)(model.njoints);++i)
jcarpent's avatar
jcarpent committed
57
58
59
60
    {
      os << "  Joint "<< model.names[i] << ": parent=" << model.parents[i]  << std::endl;
    }
    
61
62
    return os;
  }
63
  
64
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
jcarpent's avatar
jcarpent committed
65
  template<typename JointModelDerived>
66
67
68
69
70
71
72
73
74
75
  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
                                                     )
76
  {
77
78
    assert( (njoints==(int)joints.size())&&(njoints==(int)inertias.size())
           &&(njoints==(int)parents.size())&&(njoints==(int)jointPlacements.size()) );
jcarpent's avatar
jcarpent committed
79
    assert((joint_model.nq()>=0) && (joint_model.nv()>=0));
80

jcarpent's avatar
jcarpent committed
81
82
83
84
    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());
85
    
86
    JointIndex idx = (JointIndex)(njoints++);
87
    
jcarpent's avatar
jcarpent committed
88
    joints         .push_back(JointModel(joint_model.derived()));
89
90
    JointModelDerived & jmodel = boost::get<JointModelDerived>(joints.back());
    jmodel.setIndexes(idx,nq,nv);
jcarpent's avatar
jcarpent committed
91
    
92
    inertias       .push_back(Inertia::Zero());
93
    parents        .push_back(parent);
jcarpent's avatar
jcarpent committed
94
    jointPlacements.push_back(joint_placement);
95
    names          .push_back(joint_name);
jcarpent's avatar
jcarpent committed
96
97
    nq += joint_model.nq();
    nv += joint_model.nv();
98

99
100
    // 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?).
101
    // As efficiency of ModelTpl::addJoint is not critical, the dynamic bottomRows is used here.
102
    effortLimit.conservativeResize(nv);
103
    jmodel.jointVelocitySelector(effortLimit) = max_effort;
104
    velocityLimit.conservativeResize(nv);
105
    jmodel.jointVelocitySelector(velocityLimit) = max_velocity;
106
107
108
109
    lowerPositionLimit.conservativeResize(nq);
    jmodel.jointConfigSelector(lowerPositionLimit) = min_config;
    upperPositionLimit.conservativeResize(nq);
    jmodel.jointConfigSelector(upperPositionLimit) = max_config;
110
    
111
    neutralConfiguration.conservativeResize(nq);
112
113
    typedef NeutralStep<LieGroupMap,ConfigVectorType> NeutralVisitor;
    NeutralStepAlgo<NeutralVisitor,JointModelDerived>::run(jmodel,neutralConfiguration);
114

115
    rotorInertia.conservativeResize(nv);
116
    jmodel.jointVelocitySelector(rotorInertia).setZero();
117
    rotorGearRatio.conservativeResize(nv);
118
    jmodel.jointVelocitySelector(rotorGearRatio).setZero();
jcarpent's avatar
jcarpent committed
119
    
jcarpent's avatar
jcarpent committed
120
121
122
123
    // Init and add joint index to its parent subtrees.
    subtrees.push_back(IndexVector(1));
    subtrees[idx][0] = idx;
    addJointIndexToParentSubtrees(idx);
124
125
126
    return idx;
  }

127
  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
128
  template<typename JointModelDerived>
129
130
131
132
133
  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)
134
  {
135
    VectorXs max_effort, max_velocity, min_config, max_config;
136

137
138
139
140
    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());
141

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

160
161
162
163
164
  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)
165
  {
jcarpent's avatar
jcarpent committed
166
167
    const Inertia & iYf = Y.se3Action(body_placement);
    inertias[joint_index] += iYf;
168
    nbodies++;
169
  }
170

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

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

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

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

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

276
277
} // namespace se3

jcarpent's avatar
jcarpent committed
278
279
/// @endcond

280
#endif // ifndef __se3_model_hxx__