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 namespace Qontrol {
30 
32 class RobotState {
33 public:
34  Eigen::VectorXd joint_position;
35  Eigen::VectorXd joint_velocity;
36  Eigen::VectorXd joint_acceleration;
37  Eigen::VectorXd joint_torque;
38  Eigen::VectorXd external_joint_torque;
39 
40  void resize(int size) {
41  joint_position.resize(size);
42  joint_velocity.resize(size);
43  joint_acceleration.resize(size);
44  joint_torque.resize(size);
45  external_joint_torque.resize(size);
46  }
47 };
48 
49 namespace Model {
50 
52 enum RobotModelImplType { PINOCCHIO, KDL };
53 
55 class GenericModel {
56 private:
57  std::string robot_name_;
58  RobotState robot_state_;
59 
60 protected:
61  GenericModel(){};
62  // Public methods
63  ~GenericModel(){};
64 public:
73  // static GenericModel loadModelFromFile(const std::string &urdf_path,
74  // std::string root_link = "",
75  // std::string tip_link = "") ;
76 
77  // static GenericModel loadModelFromString(const std::string &path,
78  // std::string root_link = "",
79  // std::string tip_link = "");
80 
81  // virtual bool loadFromString(const std::string &path,
82  // std::string root_link = "",
83  // std::string tip_link = "");
84 
85  // virtual bool loadFromFile(const std::string &path,
86  // std::string root_link = "",
87  // std::string tip_link = "");
88 
94  void setRobotState(const RobotState &robot_state);
95 
101  auto getRobotState() -> RobotState;
102 
107  virtual auto getTipFrameName() -> std::string = 0;
108 
113  virtual auto getRootFrameName() -> std::string = 0;
114 
119  virtual auto getNrOfDegreesOfFreedom() -> int = 0;
120 
125  virtual void setJointPosition(Eigen::VectorXd joint_position);
126 
131  virtual auto getJointPosition() -> Eigen::VectorXd;
132 
139  void setLowerJointPositionLimits(Eigen::VectorXd lim);
140 
147  void setUpperJointPositionLimits(Eigen::VectorXd lim);
148 
153  virtual auto getLowerJointPositionLimits() -> Eigen::VectorXd;
154 
159  virtual auto getUpperJointPositionLimits() -> Eigen::VectorXd;
160 
165  virtual void setJointVelocity(Eigen::VectorXd joint_velocity);
166 
171  virtual auto getJointVelocity() -> Eigen::VectorXd;
172 
179  void setJointVelocityLimits(Eigen::VectorXd lim);
180 
185  auto getJointVelocityLimits() -> Eigen::VectorXd;
186 
191  virtual void setJointAcceleration(Eigen::VectorXd joint_acceleration);
192 
198  virtual auto getJointAcceleration() -> Eigen::VectorXd;
199 
204  virtual void setJointTorque(Eigen::VectorXd joint_torque);
205 
211  virtual auto getJointTorque() -> Eigen::VectorXd;
212 
219  void setJointTorqueLimits(Eigen::VectorXd lim);
220 
225  auto getJointTorqueLimits() -> Eigen::VectorXd;
226 
231  virtual void setExternalJointTorque(Eigen::VectorXd external_joint_torque);
232 
238  virtual auto getExternalJointTorque() -> Eigen::VectorXd;
239 
246  virtual auto getJointGravityTorques() -> Eigen::VectorXd = 0;
247 
254  virtual auto getJointCoriolisTorques() -> Eigen::VectorXd = 0;
255 
263  virtual auto getJointInertiaMatrix() -> Eigen::MatrixXd = 0;
264 
272  virtual auto getInverseJointInertiaMatrix() -> Eigen::MatrixXd = 0;
273 
280  virtual auto getFramePose(const std::string &body_name)
281  -> Eigen::Isometry3d = 0;
282 
287  virtual auto getTipFramePose() -> Eigen::Isometry3d = 0;
288 
295  virtual auto getJacobian(const std::string &body_name) -> Eigen::MatrixXd = 0;
296 
302  virtual auto getJacobian(const std::string &body_name,
303  const Eigen::Isometry3d &frame)
304  -> Eigen::MatrixXd = 0;
305 
312  virtual auto getJacobianTimeDerivativeQdot(const std::string &body_name)
313  -> Eigen::VectorXd = 0;
314 
321  virtual auto getJacobianTimeDerivativeQdot(const std::string &body_name,
322  const Eigen::Isometry3d &frame)
323  -> Eigen::VectorXd = 0;
324 
331  virtual auto getFrameVelocity(const std::string &body_name)
332  -> Eigen::Matrix<double, 6, 1> = 0;
333 
340  virtual auto getFrameVelocity(const std::string &body_name,
341  const Eigen::Isometry3d &frame)
342  -> Eigen::Matrix<double, 6, 1> = 0;
343 
349  virtual auto getTipFrameVelocity() -> Eigen::Matrix<double, 6, 1> = 0;
350 
351  virtual auto getJointAccelerationFromTorques(Eigen::VectorXd torque) -> Eigen::VectorXd = 0;
352 
360  virtual auto
361  getFrameAccelerationFromJointAcceleration(const std::string &body_name)
362  -> Eigen::Matrix<double, 6, 1> = 0;
363 
371  virtual auto
372  getFrameAccelerationFromJointAcceleration(const std::string &body_name,
373  const Eigen::Isometry3d &frame)
374  -> Eigen::Matrix<double, 6, 1> = 0;
375 
383  virtual auto getFrameAccelerationFromTorques(const std::string &body_name)
384  -> Eigen::Matrix<double, 6, 1> = 0;
385 
393  virtual auto getFrameAccelerationFromTorques(const std::string &body_name,
394  const Eigen::Isometry3d &frame)
395  -> Eigen::Matrix<double, 6, 1> = 0;
396 
397  // virtual Eigen::Matrix<double, 6, 1> getTipFrameAcceleration() = 0;
398 
406  virtual auto getExternalWrench(const std::string &body_name)
407  -> Eigen::Matrix<double, 6, 1> = 0;
408 
409  virtual auto getExternalWrench(const std::string &body_name,
410  const Eigen::Isometry3d &frame)
411  -> Eigen::Matrix<double, 6, 1> = 0;
412 
419  static auto skew(Eigen::Vector3d vector) -> Eigen::Matrix3d;
420 
421  auto integrate(Eigen::VectorXd q,Eigen::VectorXd v,double dt) -> Eigen::VectorXd;
422  // Protected methods
423 protected:
424  Eigen::VectorXd lower_joint_position_limits;
425  Eigen::VectorXd upper_joint_position_limits;
426  Eigen::VectorXd joint_velocity_limits;
427  Eigen::VectorXd joint_torque_limits;
428  // Private methods
429 private:
430 };
431 
437 template <RobotModelImplType model_lib> class RobotModel {};
438 
439 } // namespace Model
440 } // namespace Qontrol
Qontrol::Model::GenericModel::getJointVelocity
virtual auto getJointVelocity() -> Eigen::VectorXd
Get the robot current joint velocity.
Definition: GenericModel.cpp:46
Qontrol::Model::GenericModel::setLowerJointPositionLimits
void setLowerJointPositionLimits(Eigen::VectorXd lim)
Set the robot lower joint position limits.
Definition: GenericModel.cpp:26
Qontrol::Model::GenericModel::getLowerJointPositionLimits
virtual auto getLowerJointPositionLimits() -> Eigen::VectorXd
Get the robot lower joint position limits.
Definition: GenericModel.cpp:34
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:32
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:30
Qontrol::Model::GenericModel::setJointAcceleration
virtual void setJointAcceleration(Eigen::VectorXd joint_acceleration)
Set the robot current joint acceleration.
Definition: GenericModel.cpp:58
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:17
Qontrol::Model::GenericModel::setJointVelocityLimits
void setJointVelocityLimits(Eigen::VectorXd lim)
Set the robot joint velocity limts.
Definition: GenericModel.cpp:50
Qontrol::Model::GenericModel::getJointTorqueLimits
auto getJointTorqueLimits() -> Eigen::VectorXd
Get the robot joint torque limits.
Definition: GenericModel.cpp:79
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:94
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:75
Qontrol::Model::GenericModel::setRobotState
void setRobotState(const RobotState &robot_state)
Load the robot model given a urdf path.
Definition: GenericModel.cpp:8
Qontrol::Model::GenericModel::setJointTorque
virtual void setJointTorque(Eigen::VectorXd joint_torque)
Set the robot current joint torque.
Definition: GenericModel.cpp:67
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:84
Qontrol::Model::GenericModel::getJointAcceleration
virtual auto getJointAcceleration() -> Eigen::VectorXd
Get the robot current joint acceleration.
Definition: GenericModel.cpp:62
Qontrol::Model::GenericModel::getJointVelocityLimits
auto getJointVelocityLimits() -> Eigen::VectorXd
Get the robot joint velocity limts.
Definition: GenericModel.cpp:54
Qontrol::Model::GenericModel::getJointPosition
virtual auto getJointPosition() -> Eigen::VectorXd
get the robot current joint position
Definition: GenericModel.cpp:21
Qontrol::Model::GenericModel::setJointVelocity
virtual void setJointVelocity(Eigen::VectorXd joint_velocity)
Set the robot current joint velocity.
Definition: GenericModel.cpp:42
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:89
Qontrol::Model::GenericModel::getRobotState
auto getRobotState() -> RobotState
Get the robot state (position, velocity, acceleration, and external torque)
Definition: GenericModel.cpp:12
Qontrol::Model::GenericModel::getUpperJointPositionLimits
virtual auto getUpperJointPositionLimits() -> Eigen::VectorXd
Get the robot upper joint position limits.
Definition: GenericModel.cpp:38
Qontrol::Model::RobotModel
Decalaration of a template specialization for the model library.
Definition: GenericModel.hpp:437
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:71
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
Definition: GenericModel.hpp:55