1#ifndef TR_KINEMATICS_HPP
2#define TR_KINEMATICS_HPP
5#include <Eigen/Geometry>
13namespace tinyrobotics {
25 template <
typename Scalar,
int nq,
typename TargetLink>
27 if constexpr (std::is_integral<TargetLink>::value) {
28 return static_cast<int>(target_link);
30 else if constexpr (std::is_same<TargetLink, std::string>::value) {
31 return model.
get_link(target_link).idx;
34 throw std::invalid_argument(
"Invalid target_link type. Must be int or std::string.");
46 template <
typename Scalar,
int nq>
49 const Eigen::Matrix<Scalar, nq, 1>& q) {
51 for (
auto const link : model.
links) {
53 if (link.joint.idx != -1) {
55 model.
forward_kinematics[link.idx] * link.joint.get_joint_transform(q[link.joint.idx]);
57 if (link.parent != -1) {
76 template <
typename Scalar,
int nq,
typename TargetLink>
78 const Eigen::Matrix<Scalar, nq, 1>& q,
79 const TargetLink& target_link) {
83 Eigen::Transform<Scalar, 3, Eigen::Isometry> Htb = Eigen::Transform<Scalar, 3, Eigen::Isometry>::Identity();
86 if (current_link.
joint.idx != -1) {
87 Htb = Htb * current_link.
joint.get_joint_transform(q[current_link.
joint.idx]).inverse();
89 Htb = Htb * current_link.
joint.parent_transform.inverse();
108 template <
typename Scalar,
int nq,
typename TargetLink,
typename SourceLink>
110 const Eigen::Matrix<Scalar, nq, 1>& q,
111 const TargetLink& target_link,
112 const SourceLink& source_link) {
125 template <
typename Scalar,
int nq>
128 const Eigen::Matrix<Scalar, nq, 1>& q) {
132 Eigen::Transform<Scalar, 3, Eigen::Isometry>::Identity());
134 for (
auto const link : model.
links) {
154 template <
typename Scalar,
int nq,
typename TargetLink,
typename SourceLink =
int>
156 const Eigen::Matrix<Scalar, nq, 1>& q,
157 const TargetLink& target_link,
158 const SourceLink& source_link = 0) {
174 template <
typename Scalar,
int nq,
typename TargetLink>
176 const Eigen::Matrix<Scalar, nq, 1>& q,
177 const TargetLink& target_link) {
182 Eigen::Matrix<Scalar, 3, 1> zIBb;
183 Eigen::Matrix<Scalar, 3, 1> rIBb;
189 if (current_link.
joint.idx != -1) {
192 if (current_link.
joint.type == JointType::PRISMATIC) {
193 model.
J.block(0, current_link.
joint.idx, 3, 1) = zIBb;
194 model.
J.block(3, current_link.
joint.idx, 3, 1) = Eigen::Matrix<Scalar, 3, 1>::Zero();
196 else if (current_link.
joint.type == JointType::REVOLUTE) {
197 model.
J.block(0, current_link.
joint.idx, 3, 1) = (zIBb).cross(rTBb - rIBb);
198 model.
J.block(3, current_link.
joint.idx, 3, 1) = zIBb;
218 template <
typename Scalar,
int nq,
typename SourceLink =
int>
220 const Eigen::Matrix<Scalar, nq, 1>& q,
221 const SourceLink& source_link = 0) {
224 for (
auto link : model.
links) {
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
int get_link_idx(const Model< Scalar, nq > &model, const TargetLink &target_link)
Retrieves the index of the target link in the tinyrobotics model.
Definition: kinematics.hpp:26
Eigen::Matrix< Scalar, 6, nq > jacobian(Model< Scalar, nq > &model, const Eigen::Matrix< Scalar, nq, 1 > &q, const TargetLink &target_link)
Computes the geometric jacobian of the target link from the base link, in the base link frame.
Definition: kinematics.hpp:175
std::vector< Eigen::Transform< Scalar, 3, Eigen::Isometry > > forward_kinematics(Model< Scalar, nq > &model, const Eigen::Matrix< Scalar, nq, 1 > &q)
Computes the transform to all the links in the tinyrobotics model.
Definition: kinematics.hpp:47
Eigen::Matrix< Scalar, 3, 1 > center_of_mass(Model< Scalar, nq > &model, const Eigen::Matrix< Scalar, nq, 1 > &q, const SourceLink &source_link=0)
Computes the center of mass expressed in source link frame.
Definition: kinematics.hpp:219
Contains various math related functions.
Contains struct for representing a tinyrobotics model.
Represents a link in a tinyrobotics model.
Definition: link.hpp:16
int idx
Index of the link in the model's link vector.
Definition: link.hpp:22
Joint< Scalar > joint
Joint connecting the link to its parent link.
Definition: link.hpp:41
int parent
Index of the link's parent link in the model's link vector.
Definition: link.hpp:25
A tinyrobotics model.
Definition: model.hpp:25
Eigen::Matrix< Scalar, 6, nq > J
Jacobian.
Definition: model.hpp:131
Scalar mass
Total mass of the model.
Definition: model.hpp:48
std::vector< Eigen::Transform< Scalar, 3, Eigen::Isometry > > forward_kinematics_com
Vector of forward kinematics com data.
Definition: model.hpp:65
Link< Scalar > get_link(const std::string &name) const
Get a link in the model by name.
Definition: model.hpp:144
Eigen::Matrix< Scalar, 3, 1 > center_of_mass
center of mass position
Definition: model.hpp:68
int base_link_idx
Index of the base link in the models links vector.
Definition: model.hpp:36
std::vector< Eigen::Transform< Scalar, 3, Eigen::Isometry > > forward_kinematics
Vector of forward kinematics data.
Definition: model.hpp:62
std::vector< Link< Scalar > > links
Vector of links in the model.
Definition: model.hpp:51