5#include <Eigen/Geometry>
16namespace tinyrobotics {
24 template <
typename Scalar,
int nq>
45 Eigen::Matrix<Scalar, 3, 1>
gravity = {0, 0, -9.81};
51 std::vector<Link<Scalar>>
links = {};
56 Eigen::Matrix<Scalar, nq, nq>
mass_matrix = Eigen::Matrix<Scalar, nq, nq>::Zero();
68 Eigen::Matrix<Scalar, 3, 1>
center_of_mass = Eigen::Matrix<Scalar, 3, 1>::Zero();
73 std::vector<Eigen::Matrix<Scalar, 6, 6>>
Xup =
74 std::vector<Eigen::Matrix<Scalar, 6, 6>>(nq, Eigen::Matrix<Scalar, 6, 6>::Zero());
77 std::vector<Eigen::Matrix<Scalar, 6, 1>>
S =
78 std::vector<Eigen::Matrix<Scalar, 6, 1>>(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
81 std::vector<Eigen::Matrix<Scalar, 6, 1>>
v =
82 std::vector<Eigen::Matrix<Scalar, 6, 1>>(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
85 std::vector<Eigen::Matrix<Scalar, 6, 1>>
c =
86 std::vector<Eigen::Matrix<Scalar, 6, 1>>(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
89 std::vector<Eigen::Matrix<Scalar, 6, 6>>
IA =
90 std::vector<Eigen::Matrix<Scalar, 6, 6>>(nq, Eigen::Matrix<Scalar, 6, 6>::Zero());
93 std::vector<Eigen::Matrix<Scalar, 6, 1>>
pA =
94 std::vector<Eigen::Matrix<Scalar, 6, 1>>(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
97 std::vector<Eigen::Matrix<Scalar, 6, 1>>
U =
98 std::vector<Eigen::Matrix<Scalar, 6, 1>>(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
101 std::vector<Scalar>
d = std::vector<Scalar>(nq, 0);
104 std::vector<Scalar>
u = std::vector<Scalar>(nq, 0);
107 std::vector<Eigen::Matrix<Scalar, 6, 1>>
a =
108 std::vector<Eigen::Matrix<Scalar, 6, 1>>(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
111 Eigen::Matrix<Scalar, 6, 1>
vJ = Eigen::Matrix<Scalar, 6, 1>::Zero();
114 std::vector<Eigen::Matrix<Scalar, 6, 1>>
fvp =
115 std::vector<Eigen::Matrix<Scalar, 6, 1>>(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
118 Eigen::Matrix<Scalar, nq, 1>
C = Eigen::Matrix<Scalar, nq, 1>::Zero();
121 Eigen::Matrix<Scalar, 6, 1>
fh = Eigen::Matrix<Scalar, 6, 1>::Zero();
124 std::vector<Eigen::Matrix<Scalar, 6, 6>>
IC =
125 std::vector<Eigen::Matrix<Scalar, 6, 6>>(nq, Eigen::Matrix<Scalar, 6, 6>::Zero());
131 Eigen::Matrix<Scalar, 6, nq>
J = Eigen::Matrix<Scalar, 6, nq>::Zero();
134 Eigen::Matrix<Scalar, nq, 1>
ddq = Eigen::Matrix<Scalar, nq, 1>::Zero();
137 Eigen::Matrix<Scalar, nq, 1>
tau = Eigen::Matrix<Scalar, nq, 1>::Zero();
145 for (
auto link :
links) {
146 if (link.name ==
name) {
161 for (
auto link :
links) {
162 if (link.name ==
name) {
163 return links[link.parent];
167 throw std::runtime_error(
"Error! Parent of Link [" +
name +
"] not found!");
178 for (
auto link :
links) {
179 if (link.joint.name ==
name) {
191 const int spacing = 25;
192 const std::string separator(150,
'*');
193 std::cout << separator << std::endl;
194 std::cout <<
"Model name : " <<
name << std::endl;
195 std::cout <<
"No. of links : " <<
links.size() << std::endl;
196 std::cout <<
"No. of actuated joints : " <<
n_q << std::endl;
198 std::cout <<
"Model Mass : " <<
mass << std::endl;
199 std::cout << std::left << std::setw(spacing) <<
"Index" << std::setw(spacing) <<
"Link Name"
200 << std::setw(spacing) <<
"Joint Name [idx]" << std::setw(spacing) <<
"Joint Type"
201 << std::setw(spacing) <<
"Parent Name [idx]" << std::setw(spacing) <<
"Children Names"
203 for (
const auto& link :
links) {
204 std::string children_names;
205 for (
const auto& child_link_idx : link.child_links) {
206 children_names +=
links[child_link_idx].name +
" ";
209 std::string parent_name =
"";
211 if (link.parent != -1) {
212 parent_link =
links[link.parent];
213 parent_name = parent_link.
name;
214 parent_idx = link.parent;
216 std::cout << std::left << std::setw(spacing) << link.idx << std::setw(spacing) << link.name
217 << std::setw(spacing) << link.joint.name +
"[" + std::to_string(link.joint.idx) +
"]"
218 << std::setw(spacing) << link.joint.get_type() << std::setw(spacing)
219 << parent_name +
" [" + std::to_string(parent_idx) +
"]" << std::setw(spacing)
220 << children_names << std::endl;
222 std::cout << separator << std::endl;
230 return Eigen::Matrix<Scalar, nq, 1>::Zero(
n_q);
239 return range * Eigen::Matrix<Scalar, nq, 1>::Random(
n_q);
247 template <
typename NewScalar>
255 for (
auto& link :
links) {
256 new_model.
links.push_back(link.template cast<NewScalar>());
260 new_model.
C =
C.template cast<NewScalar>();
261 new_model.
fh =
fh.template cast<NewScalar>();
263 new_model.
J =
J.template cast<NewScalar>();
272 for (
int i = 0; i <
Xup.size(); i++) {
273 new_model.
Xup[i] =
Xup[i].template cast<NewScalar>();
274 new_model.
S[i] =
S[i].template cast<NewScalar>();
275 new_model.
v[i] =
v[i].template cast<NewScalar>();
276 new_model.
c[i] =
c[i].template cast<NewScalar>();
277 new_model.
IA[i] =
IA[i].template cast<NewScalar>();
278 new_model.
pA[i] =
pA[i].template cast<NewScalar>();
279 new_model.
U[i] =
U[i].template cast<NewScalar>();
280 new_model.
d[i] = NewScalar(
d[i]);
281 new_model.
u[i] = NewScalar(
u[i]);
282 new_model.
a[i] =
a[i].template cast<NewScalar>();
283 new_model.
fvp[i] =
fvp[i].template cast<NewScalar>();
284 new_model.
IC[i] =
IC[i].template cast<NewScalar>();
Contains struct for representing a joint in a tinyrobotics model.
Contains struct for representing a joint in a tinyrobotics model.
Represents a joint in a tinyrobotics model which connects two links.
Definition: joint.hpp:29
Represents a link in a tinyrobotics model.
Definition: link.hpp:16
std::string name
Name of the link.
Definition: link.hpp:19
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
Joint< Scalar > get_joint(const std::string &name) const
Get the joint in the model by name.
Definition: model.hpp:177
Eigen::Matrix< Scalar, 6, nq > J
Jacobian.
Definition: model.hpp:131
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
Scalar mass
Total mass of the model.
Definition: model.hpp:48
Link< Scalar > get_parent_link(const std::string &name) const
Get the parent link of a link in the model by name.
Definition: model.hpp:160
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
int n_q
Number of configuration coordinates (degrees of freedom) in the model.
Definition: model.hpp:33
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
Link< Scalar > get_link(const std::string &name) const
Get a link in the model by name.
Definition: model.hpp:144
Eigen::Matrix< Scalar, 6, 1 > fh
Force term for intermediately storing the result.
Definition: model.hpp:121
void show_details()
Display details of the model.
Definition: model.hpp:190
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 > center_of_mass
center of mass position
Definition: model.hpp:68
Eigen::Matrix< Scalar, 3, 1 > gravity
Gravitational acceleration vector experienced by model.
Definition: model.hpp:45
Eigen::Matrix< Scalar, nq, 1 > home_configuration() const
Get a configuration vector for the model of all zeros.
Definition: model.hpp:229
std::vector< Eigen::Matrix< Scalar, 6, 1 > > fvp
Joint spatial forces.
Definition: model.hpp:114
std::string name
**************** Model Information ****************
Definition: model.hpp:30
int base_link_idx
Index of the base link in the models links vector.
Definition: model.hpp:36
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
Model< NewScalar, nq > cast()
Casts the model to a new scalar type.
Definition: model.hpp:248
Eigen::Matrix< Scalar, nq, 1 > C
Bias force vector.
Definition: model.hpp:118
std::vector< Eigen::Transform< Scalar, 3, Eigen::Isometry > > forward_kinematics
Vector of forward kinematics data.
Definition: model.hpp:62
std::vector< Scalar > u
Joint force bias terms for the robot links.
Definition: model.hpp:104
std::vector< Eigen::Matrix< Scalar, 6, 1 > > S
Motion subspace matrices for the joints.
Definition: model.hpp:77
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
Eigen::Matrix< Scalar, nq, 1 > random_configuration(Scalar range=M_PI) const
Get a random configuration vector for the model.
Definition: model.hpp:238
std::vector< Link< Scalar > > links
Vector of links in the model.
Definition: model.hpp:51