libfranka 0.18.0
FCI C++ API
Loading...
Searching...
No Matches
robot_model.h
Go to the documentation of this file.
1// Copyright (c) 2025 Franka Robotics GmbH
2// Use of this source code is governed by the Apache-2.0 license, see LICENSE
3#pragma once
4
5#include <array>
6#include <pinocchio/algorithm/centroidal.hpp>
7#include <pinocchio/algorithm/compute-all-terms.hpp>
8#include <pinocchio/algorithm/crba.hpp>
9#include <pinocchio/algorithm/frames.hpp>
10#include <pinocchio/algorithm/jacobian.hpp>
11#include <pinocchio/algorithm/kinematics.hpp>
12#include <pinocchio/algorithm/rnea.hpp>
13#include <pinocchio/multibody/model.hpp>
14#include <pinocchio/parsers/urdf.hpp>
15
16#include <string>
17
19
25namespace franka {
26
30class RobotModel : public RobotModelBase {
31 public:
32 RobotModel(const std::string& urdf);
33
34 void coriolis(const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
35 const std::array<double, 7>& dq, // NOLINT(readability-identifier-length)
36 const std::array<double, 9>& i_total,
37 double m_total,
38 const std::array<double, 3>& f_x_ctotal,
39 std::array<double, 7>& c_ne) override;
40
41 void coriolis(const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
42 const std::array<double, 7>& dq, // NOLINT(readability-identifier-length)
43 const std::array<double, 9>& i_total,
44 double m_total,
45 const std::array<double, 3>& f_x_ctotal,
46 const std::array<double, 3>& g_earth,
47 std::array<double, 7>& c_ne) override;
48
49 void gravity(const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
50 const std::array<double, 3>& g_earth,
51 double m_total,
52 const std::array<double, 3>& f_x_ctotal,
53 std::array<double, 7>& g_ne) override;
54
55 void mass(const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
56 const std::array<double, 9>& i_total,
57 double m_total,
58 const std::array<double, 3>& f_x_ctotal,
59 std::array<double, 49>& m_ne) override;
60 std::array<double, 16> pose(
61 const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
62 int joint_index) override;
63 std::array<double, 16> poseEe(
64 const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
65 const std::array<double, 16>& f_t_ee) override;
66 std::array<double, 16> poseFlange(
67 const std::array<double, 7>& q) override; // NOLINT(readability-identifier-length)
68 std::array<double, 16> poseStiffness(
69 const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
70 const std::array<double, 16>& f_t_ee,
71 const std::array<double, 16>& ee_t_k) override;
72 std::array<double, 42> bodyJacobian(
73 const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
74 int joint_index) override;
75 std::array<double, 42> bodyJacobianFlange(
76 const std::array<double, 7>& q) override; // NOLINT(readability-identifier-length)
77 std::array<double, 42> bodyJacobianEe(
78 const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
79 const std::array<double, 16>& f_t_ee) override;
80 std::array<double, 42> bodyJacobianStiffness(
81 const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
82 const std::array<double, 16>& f_t_ee,
83 const std::array<double, 16>& ee_t_k) override;
84 std::array<double, 42> zeroJacobian(
85 const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
86 int joint_index) override;
87 std::array<double, 42> zeroJacobianFlange(
88 const std::array<double, 7>& q) override; // NOLINT(readability-identifier-length)
89 std::array<double, 42> zeroJacobianEe(
90 const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
91 const std::array<double, 16>& f_t_ee) override;
92 std::array<double, 42> zeroJacobianStiffness(
93 const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
94 const std::array<double, 16>& f_t_ee,
95 const std::array<double, 16>& ee_t_k) override;
96
97 private:
98 mutable pinocchio::Data data_;
99 mutable pinocchio::Data data_gravity_;
100
101 mutable Eigen::Matrix<double, 7, 1> q_eigen_;
102 mutable Eigen::Matrix<double, 7, 1> dq_eigen_;
103 mutable Eigen::Matrix<double, 7, 1> tau_eigen_;
104 mutable Eigen::Matrix<double, 7, 1> ddq_temp_eigen_;
105 mutable Eigen::Vector3d com_eigen_;
106 mutable Eigen::Matrix3d inertia_eigen_;
107
108 mutable std::array<double, 9> cached_i_total_;
109 mutable double cached_m_total_{-1.0};
110 mutable std::array<double, 3> cached_f_x_ctotal_;
111 mutable bool inertia_cache_valid_{false};
112
113 mutable pinocchio::Model pinocchio_model_;
119 pinocchio::Data computeForwardKinematics(
120 const std::array<double, 7>& q) const; // NOLINT(readability-identifier-length)
121
127 static std::array<double, 16> eigenToArray(const Eigen::Matrix4d& matrix);
128
134 static std::array<double, 42> eigenToArray(const Eigen::Matrix<double, 6, 7>& matrix);
135
141 pinocchio::Data initializeModelAndReturnData(const std::string& urdf);
142
150 pinocchio::FrameIndex addFrame(const std::string& name,
151 pinocchio::FrameIndex parent_frame_id,
152 const pinocchio::SE3& placement);
153
162 std::array<double, 42> computeJacobian(
163 const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
164 int frame_or_joint_index,
165 bool is_joint,
166 pinocchio::ReferenceFrame reference_frame);
167
175 std::array<double, 42> computeEeJacobian(
176 const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
177 const std::array<double, 16>& f_t_ee,
178 pinocchio::ReferenceFrame reference_frame);
179
188 std::array<double, 42> computeStiffnessJacobian(
189 const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
190 const std::array<double, 16>& f_t_ee,
191 const std::array<double, 16>& ee_t_k,
192 pinocchio::ReferenceFrame reference_frame);
193
200 void updateFramePlacements(const std::array<double, 16>& f_t_ee,
201 const std::array<double, 16>& ee_t_k = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
202 1, 0, 0, 0, 0, 1},
203 bool update_stiffness = false);
204
211 void updateInertiaIfNeeded(const std::array<double, 9>& i_total,
212 double m_total,
213 const std::array<double, 3>& f_x_ctotal) const;
214
218 void restoreOriginalInertia() const;
219
226 void computeGravityVector(
227 const std::array<double, 7>& q, // NOLINT(readability-identifier-length)
228 const std::array<double, 3>& g_earth,
229 std::array<double, 7>& g_ne) const;
230
231 pinocchio::Inertia initial_last_link_inertia_;
232 pinocchio::FrameIndex last_link_frame_index_;
233 pinocchio::JointIndex last_joint_index_;
234 void copyToEigenQ(const std::array<double, 7>& q) const; // NOLINT(readability-identifier-length)
235 void copyToEigenDQ(
236 const std::array<double, 7>& dq) const; // NOLINT(readability-identifier-length)
237 static void copyFromEigen(const Eigen::VectorXd& src, std::array<double, 7>& dst);
238 static void copyFromEigenMatrix(const Eigen::MatrixXd& src, std::array<double, 49>& dst);
239 pinocchio::FrameIndex ee_frame_index_;
240 pinocchio::FrameIndex stiffness_frame_index_;
241};
242
243} // namespace franka
Robot dynamic parameters computed from the URDF model with Pinocchio.
Definition robot_model_base.h:16
Implements of RobotModelBase using Pinocchio.
Definition robot_model.h:30
std::array< double, 42 > zeroJacobianEe(const std::array< double, 7 > &q, const std::array< double, 16 > &f_t_ee) override
Calculates the 6x7 zero Jacobian for the end effector frame, relative to the base frame.
std::array< double, 42 > bodyJacobianStiffness(const std::array< double, 7 > &q, const std::array< double, 16 > &f_t_ee, const std::array< double, 16 > &ee_t_k) override
Calculates the 6x7 body Jacobian for the stiffness frame, relative to that frame.
void gravity(const std::array< double, 7 > &q, const std::array< double, 3 > &g_earth, double m_total, const std::array< double, 3 > &f_x_ctotal, std::array< double, 7 > &g_ne) override
Calculates the gravity vector.
std::array< double, 16 > poseEe(const std::array< double, 7 > &q, const std::array< double, 16 > &f_t_ee) override
Calculates the homogeneous transformation matrix for the end effector, applying the end effector tran...
std::array< double, 16 > pose(const std::array< double, 7 > &q, int joint_index) override
Calculates the homogeneous transformation matrix for the specified joint or link.
std::array< double, 42 > zeroJacobianStiffness(const std::array< double, 7 > &q, const std::array< double, 16 > &f_t_ee, const std::array< double, 16 > &ee_t_k) override
Calculates the 6x7 zero Jacobian for the stiffness frame, relative to the base frame.
std::array< double, 42 > bodyJacobian(const std::array< double, 7 > &q, int joint_index) override
Calculates the 6x7 body Jacobian for the given joint, relative to that joint's frame.
std::array< double, 16 > poseStiffness(const std::array< double, 7 > &q, const std::array< double, 16 > &f_t_ee, const std::array< double, 16 > &ee_t_k) override
Calculates the homogeneous transformation matrix for the stiffness frame.
std::array< double, 16 > poseFlange(const std::array< double, 7 > &q) override
Calculates the homogeneous transformation matrix for the flange.
std::array< double, 42 > zeroJacobian(const std::array< double, 7 > &q, int joint_index) override
Calculates the 6x7 zero Jacobian for the given joint, relative to the base frame.
std::array< double, 42 > bodyJacobianFlange(const std::array< double, 7 > &q) override
Calculates the 6x7 body Jacobian for the flange frame, relative to that frame.
void mass(const std::array< double, 7 > &q, const std::array< double, 9 > &i_total, double m_total, const std::array< double, 3 > &f_x_ctotal, std::array< double, 49 > &m_ne) override
Calculates the 7x7 inertia matrix.
std::array< double, 42 > bodyJacobianEe(const std::array< double, 7 > &q, const std::array< double, 16 > &f_t_ee) override
Calculates the 6x7 body Jacobian for the end effector frame, relative to that frame.
std::array< double, 42 > zeroJacobianFlange(const std::array< double, 7 > &q) override
Calculates the 6x7 zero Jacobian for the flange frame, relative to the base frame.
void coriolis(const std::array< double, 7 > &q, const std::array< double, 7 > &dq, const std::array< double, 9 > &i_total, double m_total, const std::array< double, 3 > &f_x_ctotal, std::array< double, 7 > &c_ne) override
Calculates the Coriolis force vector (state-space equation): , in .
void coriolis(const std::array< double, 7 > &q, const std::array< double, 7 > &dq, const std::array< double, 9 > &i_total, double m_total, const std::array< double, 3 > &f_x_ctotal, const std::array< double, 3 > &g_earth, std::array< double, 7 > &c_ne) override
Calculates the Coriolis force vector with configurable gravity (recommended implementation).
Abstract interface class for robot dynamic parameters computed from the URDF model with Pinocchio.