Qontrol
GenericModel.hpp
1 // This file is part of Qontrol, a quadratic optimization library to
2 // control robot.
3 //
4 // Copyright (C) 2023 Lucas Joseph <lucas.joseph@inria.fr>
5 //
6 // Qontrol is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 3 of
14 // the License, or (at your option) any later version.
15 //
16 // Qontrol is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Qontrol. If not, see <http://www.gnu.org/licenses/>.
24 
25 #pragma once
26 #include "Qontrol/Utils/Log.hpp"
27 #include "Qontrol/Utils/Exception.hpp"
28 #include <Eigen/Dense>
29 #include "Qontrol/Problem/ReferenceFrame.hpp"
30 
31 namespace Qontrol
32 {
33 
35  class RobotState
36  {
37  public:
38  Eigen::VectorXd joint_position;
39  Eigen::VectorXd joint_velocity;
40  Eigen::VectorXd joint_acceleration;
41  Eigen::VectorXd joint_torque;
42  Eigen::VectorXd external_joint_torque;
43 
44  void resize(int size)
45  {
46  joint_position.resize(size);
47  joint_velocity.resize(size);
48  joint_acceleration.resize(size);
49  joint_torque.resize(size);
50  external_joint_torque.resize(size);
51  }
52  };
53 
54  enum ModelImpl
55  {
56  PINOCCHIO,
57  KDL
58  };
59  namespace Model
60  {
61 
66  {
67  private:
68  std::string robot_name_;
69  RobotState robot_state_;
70 
71  protected:
72  GenericModel() {};
73  // Public methods
74  ~GenericModel() {};
75 
76  public:
85  // static GenericModel loadModelFromFile(const std::string &urdf_path,
86  // std::string root_link = "",
87  // std::string tip_link = "") ;
88 
89  // static GenericModel loadModelFromString(const std::string &path,
90  // std::string root_link = "",
91  // std::string tip_link = "");
92 
93  // virtual bool loadFromString(const std::string &path,
94  // std::string root_link = "",
95  // std::string tip_link = "");
96 
97  // virtual bool loadFromFile(const std::string &path,
98  // std::string root_link = "",
99  // std::string tip_link = "");
100 
106  void setRobotState(const RobotState &robot_state);
107 
113  auto getRobotState() -> RobotState;
114 
119  virtual auto getTipFrameName() -> std::string = 0;
120 
125  virtual auto getRootFrameName() -> std::string = 0;
126 
131  virtual auto getNrOfDegreesOfFreedom() -> int = 0;
132 
137  virtual void setJointPosition(Eigen::VectorXd joint_position);
138 
143  virtual auto getJointPosition() -> Eigen::VectorXd;
144 
151  void setLowerJointPositionLimits(Eigen::VectorXd lim);
152 
159  void setUpperJointPositionLimits(Eigen::VectorXd lim);
160 
165  virtual auto getLowerJointPositionLimits() -> Eigen::VectorXd;
166 
171  virtual auto getUpperJointPositionLimits() -> Eigen::VectorXd;
172 
177  virtual void setJointVelocity(Eigen::VectorXd joint_velocity);
178 
183  virtual auto getJointVelocity() -> Eigen::VectorXd;
184 
191  void setJointVelocityLimits(Eigen::VectorXd lim);
192 
197  auto getJointVelocityLimits() -> Eigen::VectorXd;
198 
203  virtual void setJointAcceleration(Eigen::VectorXd joint_acceleration);
204 
210  virtual auto getJointAcceleration() -> Eigen::VectorXd;
211 
216  virtual void setJointTorque(Eigen::VectorXd joint_torque);
217 
223  virtual auto getJointTorque() -> Eigen::VectorXd;
224 
231  void setJointTorqueLimits(Eigen::VectorXd lim);
232 
237  auto getJointTorqueLimits() -> Eigen::VectorXd;
238 
243  virtual void setExternalJointTorque(Eigen::VectorXd external_joint_torque);
244 
250  virtual auto getExternalJointTorque() -> Eigen::VectorXd;
251 
258  virtual auto getJointGravityTorques() -> Eigen::VectorXd = 0;
259 
269  virtual auto getJointFrictionAndDamping() -> Eigen::VectorXd = 0;
280  void setVelocityGain(Eigen::VectorXd v);
281 
282  auto getVelocityGain() -> Eigen::VectorXd;
283 
284  auto getVelocityGainDiag() -> Eigen::MatrixXd;
285 
286  auto getVelocityGainDiagInv() -> Eigen::MatrixXd;
287 
294  virtual auto getJointCoriolisTorques() -> Eigen::VectorXd = 0;
295 
303  virtual auto getJointInertiaMatrix() -> Eigen::MatrixXd = 0;
304 
312  virtual auto getInverseJointInertiaMatrix() -> Eigen::MatrixXd = 0;
313 
320  virtual auto getFramePose(const std::string &body_name)
321  -> Eigen::Isometry3d = 0;
322 
327  virtual auto getTipFramePose() -> Eigen::Isometry3d = 0;
328 
335  virtual auto getJacobian(const std::string &body_name) -> Eigen::MatrixXd = 0;
336 
342  virtual auto getJacobian(const std::string &body_name,
343  const Eigen::Isometry3d &frame)
344  -> Eigen::MatrixXd = 0;
345 
346  virtual auto getJacobian(const std::string &body_name, ReferenceFrame ref_frame)
347  -> Eigen::MatrixXd = 0;
348 
355  virtual auto getJacobianTimeDerivativeQdot(const std::string &body_name)
356  -> Eigen::VectorXd = 0;
357 
364  virtual auto getJacobianTimeDerivativeQdot(const std::string &body_name,
365  const Eigen::Isometry3d &frame)
366  -> Eigen::VectorXd = 0;
367 
368  virtual auto getJacobianTimeDerivativeQdot(const std::string &body_name, ReferenceFrame ref_frame)
369  -> Eigen::VectorXd = 0;
370 
377  virtual auto getFrameVelocity(const std::string &body_name)
378  -> Eigen::Matrix<double, 6, 1> = 0;
379 
386  virtual auto getFrameVelocity(const std::string &body_name,
387  const Eigen::Isometry3d &frame)
388  -> Eigen::Matrix<double, 6, 1> = 0;
389 
395  virtual auto getTipFrameVelocity() -> Eigen::Matrix<double, 6, 1> = 0;
396 
397  virtual auto getJointAccelerationFromTorques(Eigen::VectorXd torque) -> Eigen::VectorXd = 0;
398 
406  virtual auto
407  getFrameAccelerationFromJointAcceleration(const std::string &body_name)
408  -> Eigen::Matrix<double, 6, 1> = 0;
409 
417  virtual auto
418  getFrameAccelerationFromJointAcceleration(const std::string &body_name,
419  const Eigen::Isometry3d &frame)
420  -> Eigen::Matrix<double, 6, 1> = 0;
421 
429  virtual auto getFrameAccelerationFromTorques(const std::string &body_name)
430  -> Eigen::Matrix<double, 6, 1> = 0;
431 
439  virtual auto getFrameAccelerationFromTorques(const std::string &body_name,
440  const Eigen::Isometry3d &frame)
441  -> Eigen::Matrix<double, 6, 1> = 0;
442 
443  // virtual Eigen::Matrix<double, 6, 1> getTipFrameAcceleration() = 0;
444 
452  virtual auto getExternalWrench(const std::string &body_name)
453  -> Eigen::Matrix<double, 6, 1> = 0;
454 
455  virtual auto getExternalWrench(const std::string &body_name,
456  const Eigen::Isometry3d &frame)
457  -> Eigen::Matrix<double, 6, 1> = 0;
458 
465  static auto skew(Eigen::Vector3d vector) -> Eigen::Matrix3d;
466 
467  auto integrate(Eigen::VectorXd q, Eigen::VectorXd v, double dt) -> Eigen::VectorXd;
468  // Protected methods
469  protected:
470  Eigen::VectorXd lower_joint_position_limits;
471  Eigen::VectorXd upper_joint_position_limits;
472  Eigen::VectorXd joint_velocity_limits;
473  Eigen::VectorXd joint_torque_limits;
474 
475  Eigen::VectorXd velocity_gains;
476  Eigen::MatrixXd velocity_gains_diag;
477  Eigen::MatrixXd velocity_gains_diag_inv;
478  // Private methods
479  private:
480  };
481 
487  template <ModelImpl model_lib>
489  {
490  };
491 
492  } // namespace Model
493 } // namespace Qontrol
Qontrol::Model::GenericModel::getJointVelocity
virtual auto getJointVelocity() -> Eigen::VectorXd
Get the robot current joint velocity.
Definition: GenericModel.cpp:57
Qontrol::Model::GenericModel::setLowerJointPositionLimits
void setLowerJointPositionLimits(Eigen::VectorXd lim)
Set the robot lower joint position limits.
Definition: GenericModel.cpp:32
Qontrol::Model::GenericModel::getLowerJointPositionLimits
virtual auto getLowerJointPositionLimits() -> Eigen::VectorXd
Get the robot lower joint position limits.
Definition: GenericModel.cpp:42
Qontrol::Model::GenericModel::getTipFramePose
virtual auto getTipFramePose() -> Eigen::Isometry3d=0
Get the pose of the tip frame wrt. the root frame.
Qontrol::Model::GenericModel::getRootFrameName
virtual auto getRootFrameName() -> std::string=0
Get the name of the root frame in the model.
Qontrol::RobotState
Robot state.
Definition: GenericModel.hpp:35
Qontrol::Model::GenericModel::getJacobianTimeDerivativeQdot
virtual auto getJacobianTimeDerivativeQdot(const std::string &body_name) -> Eigen::VectorXd=0
Get the vector representing the terme in the equation .
Qontrol::Model::GenericModel::setUpperJointPositionLimits
void setUpperJointPositionLimits(Eigen::VectorXd lim)
Set the robot upper joint position limits.
Definition: GenericModel.cpp:37
Qontrol::Model::GenericModel::setJointAcceleration
virtual void setJointAcceleration(Eigen::VectorXd joint_acceleration)
Set the robot current joint acceleration.
Definition: GenericModel.cpp:72
Qontrol::Model::GenericModel::getFramePose
virtual auto getFramePose(const std::string &body_name) -> Eigen::Isometry3d=0
Get the pose of the frame called in the URDF wrt. the root frame.
Qontrol::Model::GenericModel::setJointPosition
virtual void setJointPosition(Eigen::VectorXd joint_position)
Set the robot current joint position.
Definition: GenericModel.cpp:21
Qontrol::Model::GenericModel::setJointVelocityLimits
void setJointVelocityLimits(Eigen::VectorXd lim)
Set the robot joint velocity limts.
Definition: GenericModel.cpp:62
Qontrol::Model::GenericModel::getJointTorqueLimits
auto getJointTorqueLimits() -> Eigen::VectorXd
Get the robot joint torque limits.
Definition: GenericModel.cpp:98
Qontrol::Model::GenericModel::getJacobian
virtual auto getJacobian(const std::string &body_name) -> Eigen::MatrixXd=0
Get the Jacobian matrix expressed at the frame name in the URDF wrt. the root frame.
Qontrol::Model::GenericModel::getNrOfDegreesOfFreedom
virtual auto getNrOfDegreesOfFreedom() -> int=0
Get the robot number of degrees of freedom.
Qontrol::Model::GenericModel::skew
static auto skew(Eigen::Vector3d vector) -> Eigen::Matrix3d
compute the skew symetry matrix of a vector. Usefull to perform cross products
Definition: GenericModel.cpp:139
Qontrol::Model::GenericModel::getFrameAccelerationFromJointAcceleration
virtual auto getFrameAccelerationFromJointAcceleration(const std::string &body_name) -> Eigen::Matrix< double, 6, 1 >=0
Get the acceleration of the frame named in the URDF wrt. the root frame given the robot current join...
Qontrol::Model::GenericModel::getTipFrameVelocity
virtual auto getTipFrameVelocity() -> Eigen::Matrix< double, 6, 1 >=0
The twist representing the velocity of the tip frame wrt. the root frame.
Qontrol::Model::GenericModel::getInverseJointInertiaMatrix
virtual auto getInverseJointInertiaMatrix() -> Eigen::MatrixXd=0
Get the matrix representing the inverse of the joint space inertia matrix associated to the term in ...
Qontrol::Model::GenericModel::setJointTorqueLimits
void setJointTorqueLimits(Eigen::VectorXd lim)
Set the robot joint torque limits.
Definition: GenericModel.cpp:93
Qontrol::Model::GenericModel::setRobotState
void setRobotState(const RobotState &robot_state)
Load the robot model given a urdf path.
Definition: GenericModel.cpp:10
Qontrol::Model::GenericModel::setJointTorque
virtual void setJointTorque(Eigen::VectorXd joint_torque)
Set the robot current joint torque.
Definition: GenericModel.cpp:83
Qontrol::Model::GenericModel::getJointInertiaMatrix
virtual auto getJointInertiaMatrix() -> Eigen::MatrixXd=0
Get the matrix representing the joint space inertia matrix associated to the term in the equation of...
Qontrol::Model::GenericModel::setExternalJointTorque
virtual void setExternalJointTorque(Eigen::VectorXd external_joint_torque)
Set the robot current external joint torque.
Definition: GenericModel.cpp:104
Qontrol::Model::GenericModel::getJointAcceleration
virtual auto getJointAcceleration() -> Eigen::VectorXd
Get the robot current joint acceleration.
Definition: GenericModel.cpp:77
Qontrol::Model::GenericModel::getJointVelocityLimits
auto getJointVelocityLimits() -> Eigen::VectorXd
Get the robot joint velocity limts.
Definition: GenericModel.cpp:67
Qontrol::Model::GenericModel::getJointPosition
virtual auto getJointPosition() -> Eigen::VectorXd
get the robot current joint position
Definition: GenericModel.cpp:26
Qontrol::Model::GenericModel::setJointVelocity
virtual void setJointVelocity(Eigen::VectorXd joint_velocity)
Set the robot current joint velocity.
Definition: GenericModel.cpp:52
Qontrol::Model::GenericModel::getTipFrameName
virtual auto getTipFrameName() -> std::string=0
Get the name of the tip frame in the model.
Qontrol::Model::GenericModel::getExternalJointTorque
virtual auto getExternalJointTorque() -> Eigen::VectorXd
Get the robot current external joint torque.
Definition: GenericModel.cpp:133
Qontrol::Model::GenericModel::getRobotState
auto getRobotState() -> RobotState
Get the robot state (position, velocity, acceleration, and external torque)
Definition: GenericModel.cpp:15
Qontrol::Model::GenericModel::getUpperJointPositionLimits
virtual auto getUpperJointPositionLimits() -> Eigen::VectorXd
Get the robot upper joint position limits.
Definition: GenericModel.cpp:47
Qontrol::Model::RobotModel
Decalaration of a template specialization for the model library.
Definition: GenericModel.hpp:488
Qontrol::Model::GenericModel::getExternalWrench
virtual auto getExternalWrench(const std::string &body_name) -> Eigen::Matrix< double, 6, 1 >=0
Get the external wrench applied on the named in the URDF wrt. the root frame given the external join...
Qontrol::Model::GenericModel::getFrameAccelerationFromTorques
virtual auto getFrameAccelerationFromTorques(const std::string &body_name) -> Eigen::Matrix< double, 6, 1 >=0
Get the acceleration of the frame named in the URDF wrt. the root frame given the robot current join...
Qontrol::Model::GenericModel::getFrameVelocity
virtual auto getFrameVelocity(const std::string &body_name) -> Eigen::Matrix< double, 6, 1 >=0
Get the twist representing the velocity of the frame named in the URDF wrt. the root frame.
Qontrol::Model::GenericModel::getJointTorque
virtual auto getJointTorque() -> Eigen::VectorXd
Get the robot current joint torque.
Definition: GenericModel.cpp:88
Qontrol::Model::GenericModel::getJointCoriolisTorques
virtual auto getJointCoriolisTorques() -> Eigen::VectorXd=0
Get the vector representing the gravity effect associated to the term in the equation of motion: ....
Qontrol::Model::GenericModel
A virtual implementation of a model base library.
Definition: GenericModel.hpp:65