6#include <Eigen/Geometry>
14namespace tinyrobotics {
23 template <
typename Scalar,
int nq>
26 for (
int i = 0; i < nq; i++) {
28 Eigen::Transform<Scalar, 3, Eigen::Isometry> T =
29 m.
links[m.
q_idx[i]].joint.get_parent_to_child_transform(q(i));
36 for (
int i = nq - 1; i >= 0; i--) {
44 m.
data.mass_matrix.setZero();
45 for (
int i = 0; i < nq; i++) {
53 m.
data.mass_matrix(j, i) = m.
data.mass_matrix(i, j);
57 return m.
data.mass_matrix;
66 template <
typename Scalar,
int nq>
68 const Eigen::Matrix<Scalar, nq, 1>& q,
69 const Eigen::Matrix<Scalar, nq, 1>& dq) {
72 mass_matrix<Scalar, nq>(m, q);
74 m.
data.kinetic_energy = 0.5 * dq.transpose() * m.
data.mass_matrix * dq;
75 return m.
data.kinetic_energy;
85 template <
typename Scalar,
int nq>
88 m.
data.potential_energy = 0;
90 for (
int i = 0; i < m.
n_links; i++) {
92 Eigen::Transform<Scalar, 3, Eigen::Isometry> Hbi_c =
94 Eigen::Matrix<Scalar, 3, 1> rMIi_c = Hbi_c.translation();
95 m.
data.potential_energy += -m.
links[i].mass * m.
gravity.transpose() * rMIi_c;
97 return m.
data.potential_energy;
108 template <
typename Scalar,
int nq>
110 const Eigen::Matrix<Scalar, nq, 1>& q,
111 const Eigen::Matrix<Scalar, nq, 1>& dq) {
119 return m.
data.kinetic_energy + m.
data.potential_energy;
130 template <
typename Scalar,
int nq>
133 const std::vector<Eigen::Matrix<Scalar, 6, 6>>& Xup,
134 const std::vector<Eigen::Matrix<Scalar, 6, 1>>& f_in,
135 const std::vector<Eigen::Matrix<Scalar, 6, 1>>& f_ext) {
136 std::vector<Eigen::Matrix<Scalar, 6, 1>> f_out(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
137 std::vector<Eigen::Matrix<Scalar, 6, 6>> Xa(nq, Eigen::Matrix<Scalar, 6, 6>::Zero());
140 for (
int i = 0; i < nq; i++) {
146 Xa[i] = Xup[i] * Xa[m.
parent[i]];
148 f_out[i] += Xa[i].transpose().inverse() * f_ext[i];
162 template <
typename Scalar,
int nq>
164 const Eigen::Matrix<Scalar, nq, 1>& q,
165 const Eigen::Matrix<Scalar, nq, 1>& qd,
166 const Eigen::Matrix<Scalar, nq, 1>& tau,
167 const std::vector<Eigen::Matrix<Scalar, 6, 1>>& f_ext = {}) {
170 for (
int i = 0; i < nq; i++) {
173 Eigen::Matrix<Scalar, 6, 1> vJ = m.
links[m.
q_idx[i]].joint.S * qd(i);
175 m.
data.T = m.
links[m.
q_idx[i]].joint.get_parent_to_child_transform(q(i));
177 m.
data.Xup[i] = homogeneous_to_spatial(m.
data.T.inverse());
181 m.
data.c[i] = Eigen::Matrix<Scalar, 6, 1>::Zero();
192 if (f_ext.size() != 0) {
197 for (
int i = nq - 1; i >= 0; i--) {
200 m.
data.u[i] = Scalar(tau(i) - m.
links[m.
q_idx[i]].joint.S.transpose() * m.
data.pA[i]);
202 Eigen::Matrix<Scalar, 6, 6> Ia = m.
data.IA[i] - (m.
data.U[i] / m.
data.d[i]) * m.
data.U[i].transpose();
203 Eigen::Matrix<Scalar, 6, 1> pa =
211 for (
int i = 0; i < nq; i++) {
233 template <
typename Scalar,
int nq>
235 const Eigen::Matrix<Scalar, nq, 1>& q,
236 const Eigen::Matrix<Scalar, nq, 1>& qd,
237 const Eigen::Matrix<Scalar, nq, 1>& tau,
238 const std::vector<Eigen::Matrix<Scalar, 6, 1>>& f_ext = {}) {
240 for (
int i = 0; i < nq; i++) {
242 Eigen::Matrix<Scalar, 6, 1> vJ = m.
links[m.
q_idx[i]].joint.S * qd(i);
244 Eigen::Transform<Scalar, 3, Eigen::Isometry> T =
245 m.
links[m.
q_idx[i]].joint.get_parent_to_child_transform(q(i));
247 m.
data.Xup[i] = homogeneous_to_spatial(T.inverse());
251 m.
data.avp[i] = m.
data.Xup[i] * -m.
data.spatial_gravity;
263 if (f_ext.size() != 0) {
268 for (
int i = nq - 1; i >= 0; i--) {
276 for (
int i = nq - 1; i >= 0; i--) {
283 for (
int i = 0; i < nq; i++) {
287 while (m.
parent[j] != -1) {
291 m.
data.mass_matrix(j, i) = m.
data.mass_matrix(i, j);
296 Eigen::Matrix<Scalar, nq, 1> qdd = m.
data.mass_matrix.ldlt().solve(tau - m.
data.C);
309 template <
typename Scalar,
int nq>
311 const Eigen::Matrix<Scalar, nq, 1>& q,
312 const Eigen::Matrix<Scalar, nq, 1>& qd,
313 const Eigen::Matrix<Scalar, nq, 1>& qdd,
314 const std::vector<Eigen::Matrix<Scalar, 6, 1>>& f_ext = {}) {
316 for (
int i = 0; i < nq; i++) {
319 Eigen::Matrix<Scalar, 6, 1> vJ = m.
links[m.
q_idx[i]].joint.S * qd(i);
321 Eigen::Transform<Scalar, 3, Eigen::Isometry> T =
322 m.
links[m.
q_idx[i]].joint.get_parent_to_child_transform(q(i));
324 m.
data.Xup[i] = homogeneous_to_spatial(T.inverse());
340 if (f_ext.size() != 0) {
345 for (
int i = nq - 1; i >= 0; i--) {
Eigen::Matrix< Scalar, nq, 1 > forward_dynamics(Model< Scalar, nq > &m, const Eigen::Matrix< Scalar, nq, 1 > &q, const Eigen::Matrix< Scalar, nq, 1 > &qd, const Eigen::Matrix< Scalar, nq, 1 > &tau, const std::vector< Eigen::Matrix< Scalar, 6, 1 > > &f_ext={})
Compute the forward dynamics of the tinyrobotics model via Articulated-Body Algorithm.
Definition: Dynamics.hpp:163
Scalar total_energy(Model< Scalar, nq > &m, const Eigen::Matrix< Scalar, nq, 1 > &q, const Eigen::Matrix< Scalar, nq, 1 > &dq)
Compute the total energy of the tinyrobotics model.
Definition: Dynamics.hpp:109
Scalar kinetic_energy(Model< Scalar, nq > &m, const Eigen::Matrix< Scalar, nq, 1 > &q, const Eigen::Matrix< Scalar, nq, 1 > &dq)
Compute the kinetic_energy of the tinyrobotics model.
Definition: Dynamics.hpp:67
Scalar potential_energy(Model< Scalar, nq > &m, const Eigen::Matrix< Scalar, nq, 1 > &q)
Compute the potential_energy of the tinyrobotics model.
Definition: Dynamics.hpp:86
Eigen::Matrix< Scalar, nq, 1 > forward_dynamics_crb(Model< Scalar, nq > &m, const Eigen::Matrix< Scalar, nq, 1 > &q, const Eigen::Matrix< Scalar, nq, 1 > &qd, const Eigen::Matrix< Scalar, nq, 1 > &tau, const std::vector< Eigen::Matrix< Scalar, 6, 1 > > &f_ext={})
Compute the forward dynamics of the tinyrobotics model via Composite-Rigid-Body Algorithm.
Definition: Dynamics.hpp:234
Eigen::Matrix< Scalar, nq, nq > mass_matrix(Model< Scalar, nq > &m, const Eigen::Matrix< Scalar, nq, 1 > &q)
Compute the mass matrix of the tinyrobotics model.
Definition: Dynamics.hpp:24
std::vector< Eigen::Matrix< Scalar, 6, 1 > > apply_external_forces(const Model< Scalar, nq > &m, const std::vector< Eigen::Matrix< Scalar, 6, 6 > > &Xup, const std::vector< Eigen::Matrix< Scalar, 6, 1 > > &f_in, const std::vector< Eigen::Matrix< Scalar, 6, 1 > > &f_ext)
Apply external forces to the tinyrobotics model.
Definition: Dynamics.hpp:131
Eigen::Matrix< Scalar, nq, 1 > inverse_dynamics(Model< Scalar, nq > &m, const Eigen::Matrix< Scalar, nq, 1 > &q, const Eigen::Matrix< Scalar, nq, 1 > &qd, const Eigen::Matrix< Scalar, nq, 1 > &qdd, const std::vector< Eigen::Matrix< Scalar, 6, 1 > > &f_ext={})
Compute the inverse dynamics of a tinyrobotics model.
Definition: Dynamics.hpp:310
Contains functions for computing various kinematic quantities of a tinyrobotics model.
Eigen::Matrix< Scalar, 6, 6 > homogeneous_to_spatial(const Eigen::Transform< Scalar, 3, Eigen::Isometry > &H)
Spatial coordinate transform from homogeneous transformation matrix.
Definition: Math.hpp:110
Eigen::Matrix< Scalar, 6, 6 > cross_motion(const Eigen::Matrix< Scalar, 6, 1 > &v)
Computes the 6x6 cross-product matrix for a motion vector and a force vector.
Definition: Math.hpp:70
Eigen::Matrix< Scalar, 6, 6 > cross_spatial(const Eigen::Matrix< Scalar, 6, 1 > &v)
Computes the 6x6 cross-product matrix for a 6D spatial vector.
Definition: Math.hpp:56
Contains struct for representing a tinyrobotics model.
A tinyrobotics model.
Definition: Model.hpp:30
std::vector< int > parent
Vector of parent link indices of the links which are non-fixed.
Definition: Model.hpp:56
int n_links
Number of links in the model.
Definition: Model.hpp:35
Data< Scalar, nq > data
Stores the results of the models algorithms.
Definition: Model.hpp:65
Eigen::Matrix< Scalar, 3, 1 > gravity
Gravitational acceleration experienced by model.
Definition: Model.hpp:59
int base_link_idx
Index of the base link in the models links vector.
Definition: Model.hpp:50
std::vector< int > q_idx
Vector of indices of links in the models link vector that have a non-fixed joint.
Definition: Model.hpp:53
std::vector< Link< Scalar > > links
Vector of links in the model.
Definition: Model.hpp:44