6#include <Eigen/Geometry>
14namespace tinyrobotics {
23 template <
typename Scalar,
int nq>
27 for (
int i = 0; i < nq; i++) {
32 for (
int i = nq - 1; i >= 0; i--) {
38 for (
int i = 0; i < nq; i++) {
43 m.
fh = m.
Xup[j].transpose() * m.
fh;
59 template <
typename Scalar,
int nq>
61 const Eigen::Matrix<Scalar, nq, 1>& q,
62 const Eigen::Matrix<Scalar, nq, 1>& dq) {
63 return Scalar(0.5) * dq.transpose() * mass_matrix<Scalar, nq>(m, q) * dq;
73 template <
typename Scalar,
int nq>
79 for (
auto const link : m.
links) {
93 template <
typename Scalar,
int nq>
95 const Eigen::Matrix<Scalar, nq, 1>& q,
96 const Eigen::Matrix<Scalar, nq, 1>& dq) {
108 template <
typename Scalar,
int nq>
111 const std::vector<Eigen::Matrix<Scalar, 6, 6>>& Xup,
112 const std::vector<Eigen::Matrix<Scalar, 6, 1>>& f_in,
113 const std::vector<Eigen::Matrix<Scalar, 6, 1>>& f_ext) {
114 std::vector<Eigen::Matrix<Scalar, 6, 1>> f_out(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
115 std::vector<Eigen::Matrix<Scalar, 6, 6>> Xa(nq, Eigen::Matrix<Scalar, 6, 6>::Zero());
118 for (
int i = 0; i < nq; i++) {
124 Xa[i] = Xup[i] * Xa[m.
parent[i]];
126 f_out[i] += Xa[i].transpose().inverse() * f_ext[i];
140 template <
typename Scalar,
int nq>
142 const Eigen::Matrix<Scalar, nq, 1>& q,
143 const Eigen::Matrix<Scalar, nq, 1>& dq,
144 const Eigen::Matrix<Scalar, nq, 1>& tau,
145 const std::vector<Eigen::Matrix<Scalar, 6, 1>>& f_ext = {}) {
146 for (
int i = 0; i < nq; i++) {
150 m.
Xup[i] = homogeneous_to_spatial(m.
links[m.
q_map[i]].joint.get_parent_to_child_transform(q(i)).inverse());
154 m.
c[i] = Eigen::Matrix<Scalar, 6, 1>::Zero();
165 if (!f_ext.empty()) {
169 for (
int i = nq - 1; i >= 0; i--) {
171 m.
d[i] = m.
links[m.
q_map[i]].joint.S.transpose() * m.
U[i];
172 m.
u[i] = Scalar(tau(i) - m.
links[m.
q_map[i]].joint.S.transpose() * m.
pA[i]);
174 Eigen::Matrix<Scalar, 6, 6> Ia = m.
IA[i] - (m.
U[i] / m.
d[i]) * m.
U[i].transpose();
175 Eigen::Matrix<Scalar, 6, 1> pa = m.
pA[i] + Ia * m.
c[i] + m.
U[i] * (m.
u[i] / m.
d[i]);
181 for (
int i = 0; i < nq; i++) {
188 m.
ddq(i) = (m.
u[i] - m.
U[i].transpose() * m.
a[i]) / m.
d[i];
203 template <
typename Scalar,
int nq>
205 const Eigen::Matrix<Scalar, nq, 1>& q,
206 const Eigen::Matrix<Scalar, nq, 1>& dq,
207 const Eigen::Matrix<Scalar, nq, 1>& tau,
208 const std::vector<Eigen::Matrix<Scalar, 6, 1>>& f_ext = {}) {
209 for (
int i = 0; i < nq; i++) {
213 m.
Xup[i] = homogeneous_to_spatial(m.
links[m.
q_map[i]].joint.get_parent_to_child_transform(q(i)).inverse());
228 if (!f_ext.empty()) {
232 for (
int i = nq - 1; i >= 0; i--) {
239 for (
int i = nq - 1; i >= 0; i--) {
245 for (
int i = 0; i < nq; i++) {
249 while (m.
parent[j] != -1) {
250 m.
fh = m.
Xup[j].transpose() * m.
fh;
268 template <
typename Scalar,
int nq>
270 const Eigen::Matrix<Scalar, nq, 1>& q,
271 const Eigen::Matrix<Scalar, nq, 1>& dq,
272 const Eigen::Matrix<Scalar, nq, 1>& ddq,
273 const std::vector<Eigen::Matrix<Scalar, 6, 1>>& f_ext = {}) {
274 for (
int i = 0; i < nq; i++) {
278 m.
Xup[i] = homogeneous_to_spatial(m.
links[m.
q_map[i]].joint.get_parent_to_child_transform(q(i)).inverse());
293 if (!f_ext.empty()) {
297 for (
int i = nq - 1; i >= 0; i--) {
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:94
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:60
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:74
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:109
Eigen::Matrix< Scalar, nq, 1 > forward_dynamics(Model< Scalar, nq > &m, const Eigen::Matrix< Scalar, nq, 1 > &q, const Eigen::Matrix< Scalar, nq, 1 > &dq, 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:141
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 > &dq, 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:204
Eigen::Matrix< Scalar, nq, 1 > inverse_dynamics(Model< Scalar, nq > &m, const Eigen::Matrix< Scalar, nq, 1 > &q, const Eigen::Matrix< Scalar, nq, 1 > &dq, const Eigen::Matrix< Scalar, nq, 1 > &ddq, const std::vector< Eigen::Matrix< Scalar, 6, 1 > > &f_ext={})
Compute the inverse dynamics of a tinyrobotics model.
Definition: dynamics.hpp:269
Contains functions for computing various kinematic quantities of a tinyrobotics model.
std::vector< Eigen::Transform< Scalar, 3, Eigen::Isometry > > forward_kinematics_com(Model< Scalar, nq > &model, const Eigen::Matrix< Scalar, nq, 1 > &q)
Computes the transform between center of mass of each link and the source link.
Definition: kinematics.hpp:126
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:95
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:57
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:43
Contains struct for representing a tinyrobotics model.
A tinyrobotics model.
Definition: model.hpp:25
std::vector< int > parent
Vector of parent link indices of the links which have a non-fixed joints.
Definition: model.hpp:42
std::vector< int > q_map
Map to indices of links in the models link vector that have a non-fixed joints.
Definition: model.hpp:39
Scalar potential_energy
Potential energy.
Definition: model.hpp:59
std::vector< Eigen::Matrix< Scalar, 6, 6 > > Xup
**************** Pre-allcoated variables for dynamics algorithms ****************
Definition: model.hpp:73
std::vector< Scalar > d
Joint force inertia terms for the robot links.
Definition: model.hpp:101
std::vector< Eigen::Matrix< Scalar, 6, 1 > > v
Spatial velocities of the robot links.
Definition: model.hpp:81
std::vector< Eigen::Transform< Scalar, 3, Eigen::Isometry > > forward_kinematics_com
Vector of forward kinematics com data.
Definition: model.hpp:65
std::vector< Eigen::Matrix< Scalar, 6, 1 > > U
Spatial force projections for the joints.
Definition: model.hpp:97
Eigen::Matrix< Scalar, 6, 1 > fh
Force term for intermediately storing the result.
Definition: model.hpp:121
Eigen::Matrix< Scalar, 6, 1 > spatial_gravity
Gravity vector in spatial coordinates.
Definition: model.hpp:128
Eigen::Matrix< Scalar, nq, 1 > ddq
Joint acceleration.
Definition: model.hpp:134
std::vector< Eigen::Matrix< Scalar, 6, 1 > > c
Spatial acceleration bias terms for the robot links.
Definition: model.hpp:85
Eigen::Matrix< Scalar, nq, nq > mass_matrix
**************** Pre-allcoated variables for kinematics algorithms ****************
Definition: model.hpp:56
std::vector< Eigen::Matrix< Scalar, 6, 6 > > IA
Articulated-body inertia matrices for the robot links.
Definition: model.hpp:89
Eigen::Matrix< Scalar, 3, 1 > gravity
Gravitational acceleration vector experienced by model.
Definition: model.hpp:45
std::vector< Eigen::Matrix< Scalar, 6, 1 > > fvp
Joint spatial forces.
Definition: model.hpp:114
Eigen::Matrix< Scalar, nq, 1 > tau
Joint torque/force.
Definition: model.hpp:137
std::vector< Eigen::Matrix< Scalar, 6, 1 > > pA
Articulated-body forces for the robot links.
Definition: model.hpp:93
Eigen::Matrix< Scalar, nq, 1 > C
Bias force vector.
Definition: model.hpp:118
std::vector< Scalar > u
Joint force bias terms for the robot links.
Definition: model.hpp:104
Eigen::Matrix< Scalar, 6, 1 > vJ
Joint spatial velocities.
Definition: model.hpp:111
std::vector< Eigen::Matrix< Scalar, 6, 1 > > a
Spatial accelerations of the robot links.
Definition: model.hpp:107
std::vector< Eigen::Matrix< Scalar, 6, 6 > > IC
Articulated-body inertias.
Definition: model.hpp:124
std::vector< Link< Scalar > > links
Vector of links in the model.
Definition: model.hpp:51