tinyrobotics
Loading...
Searching...
No Matches
model.hpp
Go to the documentation of this file.
1#ifndef TR_MODEL_HPP
2#define TR_MODEL_HPP
3
4#include <Eigen/Core>
5#include <Eigen/Geometry>
6#include <fstream>
7#include <iomanip>
8#include <iostream>
9
10#include "joint.hpp"
11#include "link.hpp"
12
16namespace tinyrobotics {
17
24 template <typename Scalar, int nq>
25 struct Model {
26
28
30 std::string name = "";
31
33 int n_q = 0;
34
36 int base_link_idx = -1;
37
39 std::vector<int> q_map = {};
40
42 std::vector<int> parent = {};
43
45 Eigen::Matrix<Scalar, 3, 1> gravity = {0, 0, -9.81};
46
48 Scalar mass = 0;
49
51 std::vector<Link<Scalar>> links = {};
52
54
56 Eigen::Matrix<Scalar, nq, nq> mass_matrix = Eigen::Matrix<Scalar, nq, nq>::Zero();
57
59 Scalar potential_energy = 0;
60
62 std::vector<Eigen::Transform<Scalar, 3, Eigen::Isometry>> forward_kinematics = {};
63
65 std::vector<Eigen::Transform<Scalar, 3, Eigen::Isometry>> forward_kinematics_com = {};
66
68 Eigen::Matrix<Scalar, 3, 1> center_of_mass = Eigen::Matrix<Scalar, 3, 1>::Zero();
69
71
73 std::vector<Eigen::Matrix<Scalar, 6, 6>> Xup =
74 std::vector<Eigen::Matrix<Scalar, 6, 6>>(nq, Eigen::Matrix<Scalar, 6, 6>::Zero());
75
77 std::vector<Eigen::Matrix<Scalar, 6, 1>> S =
78 std::vector<Eigen::Matrix<Scalar, 6, 1>>(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
79
81 std::vector<Eigen::Matrix<Scalar, 6, 1>> v =
82 std::vector<Eigen::Matrix<Scalar, 6, 1>>(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
83
85 std::vector<Eigen::Matrix<Scalar, 6, 1>> c =
86 std::vector<Eigen::Matrix<Scalar, 6, 1>>(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
87
89 std::vector<Eigen::Matrix<Scalar, 6, 6>> IA =
90 std::vector<Eigen::Matrix<Scalar, 6, 6>>(nq, Eigen::Matrix<Scalar, 6, 6>::Zero());
91
93 std::vector<Eigen::Matrix<Scalar, 6, 1>> pA =
94 std::vector<Eigen::Matrix<Scalar, 6, 1>>(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
95
97 std::vector<Eigen::Matrix<Scalar, 6, 1>> U =
98 std::vector<Eigen::Matrix<Scalar, 6, 1>>(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
99
101 std::vector<Scalar> d = std::vector<Scalar>(nq, 0);
102
104 std::vector<Scalar> u = std::vector<Scalar>(nq, 0);
105
107 std::vector<Eigen::Matrix<Scalar, 6, 1>> a =
108 std::vector<Eigen::Matrix<Scalar, 6, 1>>(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
109
111 Eigen::Matrix<Scalar, 6, 1> vJ = Eigen::Matrix<Scalar, 6, 1>::Zero();
112
114 std::vector<Eigen::Matrix<Scalar, 6, 1>> fvp =
115 std::vector<Eigen::Matrix<Scalar, 6, 1>>(nq, Eigen::Matrix<Scalar, 6, 1>::Zero());
116
118 Eigen::Matrix<Scalar, nq, 1> C = Eigen::Matrix<Scalar, nq, 1>::Zero();
119
121 Eigen::Matrix<Scalar, 6, 1> fh = Eigen::Matrix<Scalar, 6, 1>::Zero();
122
124 std::vector<Eigen::Matrix<Scalar, 6, 6>> IC =
125 std::vector<Eigen::Matrix<Scalar, 6, 6>>(nq, Eigen::Matrix<Scalar, 6, 6>::Zero());
126
128 Eigen::Matrix<Scalar, 6, 1> spatial_gravity = Eigen::Matrix<Scalar, 6, 1>::Zero();
129
131 Eigen::Matrix<Scalar, 6, nq> J = Eigen::Matrix<Scalar, 6, nq>::Zero();
132
134 Eigen::Matrix<Scalar, nq, 1> ddq = Eigen::Matrix<Scalar, nq, 1>::Zero();
135
137 Eigen::Matrix<Scalar, nq, 1> tau = Eigen::Matrix<Scalar, nq, 1>::Zero();
138
144 Link<Scalar> get_link(const std::string& name) const {
145 for (auto link : links) {
146 if (link.name == name) {
147 return link;
148 }
149 }
150 // No link was found
151 return Link<Scalar>();
152 }
153
160 Link<Scalar> get_parent_link(const std::string& name) const {
161 for (auto link : links) {
162 if (link.name == name) {
163 return links[link.parent];
164 }
165 }
166 // No link was found
167 throw std::runtime_error("Error! Parent of Link [" + name + "] not found!");
168 return Link<Scalar>();
169 }
170
177 Joint<Scalar> get_joint(const std::string& name) const {
178 for (auto link : links) {
179 if (link.joint.name == name) {
180 return link.joint;
181 }
182 }
183 // No joint was found, return an empty joint
184 return Joint<Scalar>();
185 }
186
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;
197 std::cout << "Base link name : " << links[base_link_idx].name << 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"
202 << std::endl;
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 + " ";
207 }
208 Link<Scalar> parent_link;
209 std::string parent_name = "";
210 int parent_idx = -1;
211 if (link.parent != -1) {
212 parent_link = links[link.parent];
213 parent_name = parent_link.name;
214 parent_idx = link.parent;
215 }
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;
221 }
222 std::cout << separator << std::endl;
223 }
224
229 Eigen::Matrix<Scalar, nq, 1> home_configuration() const {
230 return Eigen::Matrix<Scalar, nq, 1>::Zero(n_q);
231 }
232
238 Eigen::Matrix<Scalar, nq, 1> random_configuration(Scalar range = M_PI) const {
239 return range * Eigen::Matrix<Scalar, nq, 1>::Random(n_q);
240 }
241
247 template <typename NewScalar>
250 new_model.name = name;
251 new_model.n_q = n_q;
252 new_model.base_link_idx = base_link_idx;
253 new_model.gravity = gravity.template cast<NewScalar>();
254 new_model.mass = NewScalar(mass);
255 for (auto& link : links) {
256 new_model.links.push_back(link.template cast<NewScalar>());
257 }
258 new_model.mass_matrix = mass_matrix.template cast<NewScalar>();
259 new_model.potential_energy = NewScalar(potential_energy);
260 new_model.C = C.template cast<NewScalar>();
261 new_model.fh = fh.template cast<NewScalar>();
262 new_model.spatial_gravity = spatial_gravity.template cast<NewScalar>();
263 new_model.J = J.template cast<NewScalar>();
264 new_model.forward_kinematics.resize(forward_kinematics.size());
265 new_model.forward_kinematics_com.resize(forward_kinematics_com.size());
266 for (int i = 0; i < forward_kinematics.size(); i++) {
267 new_model.forward_kinematics[i] = forward_kinematics[i].template cast<NewScalar>();
268 }
269 for (int i = 0; i < forward_kinematics_com.size(); i++) {
270 new_model.forward_kinematics_com[i] = forward_kinematics_com[i].template cast<NewScalar>();
271 }
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>();
285 }
286 return new_model;
287 }
288 };
289} // namespace tinyrobotics
290
291#endif
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
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