pinocchiorobot.hh 9.27 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
 * Copyright 2016,
 *
 * Maximilien Naveau
 * Olivier Stasse
 *
 * JRL, CNRS/AIST
 *
 * This file is part of jrl-walkgen.
 * jrl-walkgen 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.
 *
 * jrl-walkgen 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 jrl-walkgen.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Research carried out within the scope of the
 *  Joint Japanese-French Robotics Laboratory (JRL)
 */
/*! \file PinocchioRobot.hh
  \brief This object defines a humanoid robot model based on the PinocchioRobot
frame work */

#ifndef PinocchioRobot_HH
#define PinocchioRobot_HH

#include "pinocchio/spatial/se3.hpp"
Olivier Stasse's avatar
Olivier Stasse committed
33
#include "pinocchio/multibody/data.hpp"
34
#include "pinocchio/multibody/model.hpp"
35
#include "pinocchio/multibody/data.hpp"
36
#include <jrl/mal/matrixabstractlayer.hh>
Olivier Stasse's avatar
Olivier Stasse committed
37

38
39
40
namespace PatternGeneratorJRL
{
  struct PinocchioRobotFoot_t{
Olivier Stasse's avatar
Olivier Stasse committed
41
    pinocchio::JointIndex associatedAnkle ;
42
43
44
    double soleDepth ; // z axis
    double soleWidth ; // y axis
    double soleHeight ;// x axis
Olivier Stasse's avatar
Olivier Stasse committed
45
    Eigen::Vector3d anklePosition ;
46
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
47
48
49
50
51
52
  };
  typedef PinocchioRobotFoot_t PRFoot ;

  class PinocchioRobot
  {
  public:
mnaveau's avatar
mnaveau committed
53
54
    // overload the new[] eigen operator
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
55

mnaveau's avatar
mnaveau committed
56
57
    /// Constructor and Destructor
    PinocchioRobot();
58
    virtual ~PinocchioRobot();
59
60
61

    /// Functions computing kinematics and dynamics
    void computeInverseDynamics();
Olivier Stasse's avatar
Olivier Stasse committed
62
63
64
    void computeInverseDynamics(Eigen::VectorXd & q,
                                Eigen::VectorXd & v,
                                Eigen::VectorXd & a);
65
66

    void computeForwardKinematics();
Olivier Stasse's avatar
Olivier Stasse committed
67
    void computeForwardKinematics(Eigen::VectorXd & q);
68

69
70
71
72
73
74
75
    void RPYToSpatialFreeFlyer(Eigen::Vector3d & rpy,
                               Eigen::Vector3d & drpy,
                               Eigen::Vector3d & ddrpy,
                               Eigen::Quaterniond & quat,
                               Eigen::Vector3d & omega,
                               Eigen::Vector3d & domega);

76
77
    /// \brief ComputeSpecializedInverseKinematics :
    /// compute POSITION (not velocity) of the joints from end effector pose
78
79
80
81
82
83
84
85
86
87
88
    /// This is the implementation of the analitycal inverse kinematic extracted
    /// from the book of Kajita
    /// Authors Shuuji Kajita ; Hirohisa Hirukawa ; Kensuke Harada ; Kazuhito Yokoi
    /// ISBN 9782287877162 ; 9782287877155
    /// It needs a specific distribution of the joint to work.
    /// a test at the initialization of the class is [should be] done to check
    /// if everything is correct
    /// param root joint index, i.e. the waist or the shoulder
    /// param end joint index, i.e, the wrist or ankle indexes
    /// param root joint homogenous matrix position,
    /// param root joint homogenous matrix index,
89
    /// param 6D vector output, filled with zeros if the robot is not compatible
90
    ///
91
    virtual bool ComputeSpecializedInverseKinematics(
Olivier Stasse's avatar
Olivier Stasse committed
92
93
        const pinocchio::JointIndex &jointRoot,
        const pinocchio::JointIndex &jointEnd,
Olivier Stasse's avatar
Olivier Stasse committed
94
95
96
        const Eigen::Matrix4d & jointRootPosition,
        const Eigen::Matrix4d & jointEndPosition,
        Eigen::VectorXd &q);
97

98
    ///
99
100
    /// \brief testInverseKinematics :
    /// test if the robot has the good joint
101
    /// configuration to use the analitical inverse geometry
102
    /// to be overloaded if the user wants another inverse kinematics
103
104
    /// \return
    ///
105
106
107
108
109
110
111
112
113
    virtual bool testInverseKinematics();

    ///
    /// \brief initializeInverseKinematics :
    /// initialize the internal data for the inverse kinematic e.g. the femur
    /// length
    /// \return
    ///
    virtual void initializeInverseKinematics();
114

115
116
  public :
    /// tools :
Olivier Stasse's avatar
Olivier Stasse committed
117
118
119
    std::vector<pinocchio::JointIndex> jointsBetween
    ( pinocchio::JointIndex first, pinocchio::JointIndex second);
    std::vector<pinocchio::JointIndex> fromRootToIt (pinocchio::JointIndex it);
120
121

  private :
122
    // needed for the inverse geometry (ComputeSpecializedInverseKinematics)
Olivier Stasse's avatar
Olivier Stasse committed
123
124
125
126
    void getWaistFootKinematics(const Eigen::Matrix4d & jointRootPosition,
                                const Eigen::Matrix4d & jointEndPosition,
                                Eigen::VectorXd &q,
                                Eigen::Vector3d Dt);
127
    double ComputeXmax(double & Z);
Olivier Stasse's avatar
Olivier Stasse committed
128
129
130
    void getShoulderWristKinematics(const Eigen::Matrix4d & jointRootPosition,
                                    const Eigen::Matrix4d & jointEndPosition,
                                    Eigen::VectorXd &q,
131
132
                                    int side);
    void DetectAutomaticallyShoulders();
Olivier Stasse's avatar
Olivier Stasse committed
133
134
    void DetectAutomaticallyOneShoulder(pinocchio::JointIndex aWrist,
                                        pinocchio::JointIndex & aShoulder);
135
136
137
138
139


  public :
    /// Getters
    /// ///////
Olivier Stasse's avatar
Olivier Stasse committed
140
    inline pinocchio::Data * Data()
141
    {return m_robotData;}
Olivier Stasse's avatar
Olivier Stasse committed
142
    inline pinocchio::Data * DataInInitialePose()
mnaveau's avatar
mnaveau committed
143
    {return m_robotDataInInitialePose;}
Olivier Stasse's avatar
Olivier Stasse committed
144
    inline pinocchio::Model * Model()
145
146
147
148
149
150
151
    {return m_robotModel;}

    inline PRFoot * leftFoot()
    {return &m_leftFoot;}
    inline PRFoot * rightFoot()
    {return &m_rightFoot;}

Olivier Stasse's avatar
Olivier Stasse committed
152
    inline pinocchio::JointIndex leftWrist()
mnaveau's avatar
mnaveau committed
153
    {return m_leftWrist;}
Olivier Stasse's avatar
Olivier Stasse committed
154
    inline pinocchio::JointIndex rightWrist()
mnaveau's avatar
mnaveau committed
155
    {return m_rightWrist;}
156

Olivier Stasse's avatar
Olivier Stasse committed
157
    inline pinocchio::JointIndex chest()
158
    {return m_chest;}
Olivier Stasse's avatar
Olivier Stasse committed
159
    inline pinocchio::JointIndex waist()
160
161
162
163
164
    {return m_waist;}

    inline double mass()
    {return m_mass;}

Olivier Stasse's avatar
Olivier Stasse committed
165
    inline pinocchio::JointModelVector & getActuatedJoints()
166
167
    {return m_robotModel->joints;}

Olivier Stasse's avatar
Olivier Stasse committed
168
    inline Eigen::VectorXd currentConfiguration()
169
    {return m_qmal;}
Olivier Stasse's avatar
Olivier Stasse committed
170
    inline Eigen::VectorXd currentVelocity()
171
    {return m_vmal;}
Olivier Stasse's avatar
Olivier Stasse committed
172
    inline Eigen::VectorXd currentAcceleration()
173
174
175
176
177
    {return m_amal;}

    inline unsigned numberDof()
    {return m_robotModel->nv;}

Olivier Stasse's avatar
Olivier Stasse committed
178
    inline void zeroMomentumPoint(Eigen::Vector3d & zmp)
179
    {
180
      m_externalForces = m_robotData->liMi[1].act(m_robotData->f[1]);
181
182
      m_f = m_externalForces.linear() ;
      m_n = m_externalForces.angular() ;
183
184
185
186
187
      zmp(0) = -m_n(1)/m_f(2) ;
      zmp(1) =  m_n(0)/m_f(2) ;
      zmp(2) = 0.0 ; // by default
    }

Olivier Stasse's avatar
Olivier Stasse committed
188
    inline void positionCenterOfMass(Eigen::Vector3d & com)
189
190
191
192
193
194
    {
      m_com = m_robotData->com[0] ;
      com(0) = m_com(0) ;
      com(1) = m_com(1) ;
      com(2) = m_com(2) ;
    }
Olivier Stasse's avatar
Olivier Stasse committed
195
196
197
    inline void CenterOfMass(Eigen::Vector3d &   com,
                             Eigen::Vector3d &  dcom,
                             Eigen::Vector3d & ddcom)
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    {
      m_com = m_robotData->acom[0] ;
      ddcom(0) = m_com(0) ;
      ddcom(1) = m_com(1) ;
      ddcom(2) = m_com(2) ;

      m_com = m_robotData->vcom[0] ;
      dcom(0) = m_com(0) ;
      dcom(1) = m_com(1) ;
      dcom(2) = m_com(2) ;

      m_com = m_robotData->com[0] ;
      com(0) = m_com(0) ;
      com(1) = m_com(1) ;
      com(2) = m_com(2) ;
    }
214
215
216

    /// SETTERS
    /// ///////
Olivier Stasse's avatar
Olivier Stasse committed
217
    inline void currentConfiguration(Eigen::VectorXd conf)
218
    {m_qmal=conf;}
Olivier Stasse's avatar
Olivier Stasse committed
219
    inline void currentVelocity(Eigen::VectorXd vel)
220
    {m_vmal=vel;}
Olivier Stasse's avatar
Olivier Stasse committed
221
    inline void currentAcceleration(Eigen::VectorXd acc)
222
223
    {m_amal=acc;}

mnaveau's avatar
mnaveau committed
224
225
226
227
228
229
    /// Initialization functions
    /// ////////////////////////
    inline bool isInitialized()
    {
      return m_boolModel && m_boolData && m_boolLeftFoot && m_boolRightFoot;
    }
Olivier Stasse's avatar
Olivier Stasse committed
230
231
232
    bool checkModel(pinocchio::Model * robotModel);
    bool initializeRobotModelAndData(pinocchio::Model * robotModel,
                                     pinocchio::Data * robotData);
mnaveau's avatar
mnaveau committed
233
234
235
236
237
    bool initializeLeftFoot(PRFoot leftFoot);
    bool initializeRightFoot(PRFoot rightFoot);

    /// Attributes
    /// //////////
mnaveau's avatar
mnaveau committed
238
  private :
Olivier Stasse's avatar
Olivier Stasse committed
239
240
241
    pinocchio::Model * m_robotModel ;
    pinocchio::Data * m_robotDataInInitialePose ; // internal variable
    pinocchio::Data * m_robotData ;
mnaveau's avatar
mnaveau committed
242
243
    PRFoot m_leftFoot , m_rightFoot ;
    double m_mass ;
Olivier Stasse's avatar
Olivier Stasse committed
244
245
    pinocchio::JointIndex m_chest, m_waist, m_leftShoulder, m_rightShoulder ;
    pinocchio::JointIndex m_leftWrist , m_rightWrist ;
mnaveau's avatar
mnaveau committed
246

Olivier Stasse's avatar
Olivier Stasse committed
247
248
249
    Eigen::VectorXd m_qmal ;
    Eigen::VectorXd m_vmal ;
    Eigen::VectorXd m_amal ;
mnaveau's avatar
mnaveau committed
250
251
252
253
254
255
    Eigen::VectorXd m_q ;
    Eigen::VectorXd m_v ;
    Eigen::VectorXd m_a ;

    // tmp variables
    Eigen::Quaterniond m_quat ;
256
    Eigen::Matrix3d m_rot ;
Olivier Stasse's avatar
Olivier Stasse committed
257
    pinocchio::Force m_externalForces ; // external forces and torques
258
    Eigen::VectorXd m_tau ; // external forces and torques
mnaveau's avatar
mnaveau committed
259
260
    Eigen::Vector3d m_f,m_n; // external forces and torques
    Eigen::Vector3d m_com; // multibody CoM
261
262
    Eigen::Matrix3d m_S ;
    Eigen::Vector3d m_rpy,m_drpy,m_ddrpy,m_omega,m_domega ;
mnaveau's avatar
mnaveau committed
263

264
265
    // Variables extracted form the urdf used for the analitycal inverse
    // kinematic
266
267
268
    bool m_isLegInverseKinematic ;
    bool m_isArmInverseKinematic ;

269
    // length between the waist and the hip
Olivier Stasse's avatar
Olivier Stasse committed
270
    Eigen::Vector3d m_leftDt, m_rightDt ;
271
    double m_femurLength ;
272
273
    double m_tibiaLengthZ ;
    double m_tibiaLengthY ;
274
275


mnaveau's avatar
mnaveau committed
276
277
278
279
    bool m_boolModel     ;
    bool m_boolData      ;
    bool m_boolLeftFoot  ;
    bool m_boolRightFoot ;
mnaveau's avatar
mnaveau committed
280
281
282

  }; //PinocchioRobot
}// namespace PatternGeneratorJRL
283
#endif // PinocchioRobot_HH