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#include <map>
10#include <memory>
11#include <numeric>
12#include <sstream>
13
14#include "Data.hpp"
15#include "Joint.hpp"
16#include "Link.hpp"
17
21namespace tinyrobotics {
22
29 template <typename Scalar, int nq>
30 struct Model {
32 std::string name = "";
33
35 int n_links = 0;
36
38 int n_joints = 0;
39
41 int n_q = 0;
42
44 std::vector<Link<Scalar>> links = {};
45
47 std::vector<Joint<Scalar>> joints = {};
48
50 int base_link_idx = -1;
51
53 std::vector<int> q_idx = {};
54
56 std::vector<int> parent = {};
57
59 Eigen::Matrix<Scalar, 3, 1> gravity = {0, 0, -9.81};
60
62 Scalar mass = 0;
63
66
72 Link<Scalar> get_link(const std::string& name) const {
73 for (auto link : links) {
74 if (link.name == name) {
75 return link;
76 }
77 }
78 // No link was found
79 return Link<Scalar>();
80 }
81
88 Link<Scalar> get_parent_link(const std::string& name) const {
89 for (auto link : links) {
90 if (link.name == name) {
91 return links[link.parent];
92 }
93 }
94 // No link was found
95 throw std::runtime_error("Error! Parent of Link [" + name + "] not found!");
96 return Link<Scalar>();
97 }
98
105 Joint<Scalar> get_joint(const std::string& name) const {
106 for (auto link : links) {
107 if (link.joint.name == name) {
108 return link.joint;
109 }
110 }
111 // No joint was found, return an empty joint
112 return Joint<Scalar>();
113 }
114
119 const int spacing = 25;
120 const std::string separator(150, '*');
121 std::cout << separator << std::endl;
122 std::cout << "Model name : " << name << std::endl;
123 std::cout << "No. of links : " << links.size() << std::endl;
124 std::cout << "No. of joints : " << joints.size() << std::endl;
125 std::cout << "No. of actuated joints : " << n_q << std::endl;
126 std::cout << "Base link name : " << links[base_link_idx].name << std::endl << std::endl;
127 std::cout << "Model Mass : " << mass << std::endl;
128 std::cout << std::left << std::setw(spacing) << "Index" << std::setw(spacing) << "Link Name"
129 << std::setw(spacing) << "Joint Name [idx]" << std::setw(spacing) << "Joint Type"
130 << std::setw(spacing) << "Parent Name [idx]" << std::setw(spacing) << "Children Names"
131 << std::endl;
132 for (const auto& link : links) {
133 std::string children_names;
134 for (const auto& child_link_idx : link.child_links) {
135 children_names += links[child_link_idx].name + " ";
136 }
137 Link<Scalar> parent_link;
138 std::string parent_name = "";
139 int parent_idx = -1;
140 if (link.parent != -1) {
141 parent_link = links[link.parent];
142 parent_name = parent_link.name;
143 parent_idx = link.parent;
144 }
145 std::cout << std::left << std::setw(spacing) << link.idx << std::setw(spacing) << link.name
146 << std::setw(spacing) << link.joint.name + "[" + std::to_string(link.joint.idx) + "]"
147 << std::setw(spacing) << link.joint.get_type() << std::setw(spacing)
148 << parent_name + " [" + std::to_string(parent_idx) + "]" << std::setw(spacing)
149 << children_names << std::endl;
150 }
151 std::cout << separator << std::endl;
152 }
153
158 Eigen::Matrix<Scalar, nq, 1> home_configuration() const {
159 assert(n_q > 0);
160 Eigen::Matrix<Scalar, nq, 1> q(n_q);
161 q.setZero();
162 return q;
163 }
164
169 Eigen::Matrix<Scalar, nq, 1> random_configuration() const {
170 assert(n_q > 0);
171 Eigen::Matrix<Scalar, nq, 1> q(n_q);
172 q.setRandom();
173 q = M_PI * q;
174 return q;
175 }
176
182 template <typename NewScalar>
185 new_model.name = name;
186 new_model.n_links = n_links;
187 new_model.n_joints = n_joints;
188 new_model.n_q = n_q;
189 new_model.base_link_idx = base_link_idx;
190 new_model.gravity = gravity.template cast<NewScalar>();
191 for (auto& joint : joints) {
192 new_model.joints.push_back(joint.template cast<NewScalar>());
193 }
194 for (auto& link : links) {
195 new_model.links.push_back(link.template cast<NewScalar>());
196 }
197 new_model.data = data.template cast<NewScalar>();
198 return new_model;
199 }
200 };
201} // namespace tinyrobotics
202
203#endif
Contains the Data struct which stores the results of various model algorithms.
Contains struct for representing a joint in a tinyrobotics model.
A data struct for storing results of various model algorithms.
Definition: Data.hpp:18
Represents a joint in a tinyrobotics model which connects two links.
Definition: Joint.hpp:28
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
Joint< Scalar > get_joint(const std::string &name) const
Get the joint in the model by name.
Definition: Model.hpp:105
Data< Scalar, nq > data
Stores the results of the models algorithms.
Definition: Model.hpp:65
Scalar mass
Total mass of the model.
Definition: Model.hpp:62
std::vector< Joint< Scalar > > joints
Vector of joints in the model.
Definition: Model.hpp:47
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:88
int n_q
Number of configuration coordinates (degrees of freedom) in the model.
Definition: Model.hpp:41
Eigen::Matrix< Scalar, nq, 1 > random_configuration() const
Get a random configuration vector for the model.
Definition: Model.hpp:169
Link< Scalar > get_link(const std::string &name) const
Get a link in the model by name.
Definition: Model.hpp:72
void show_details()
Display details of the model.
Definition: Model.hpp:118
Eigen::Matrix< Scalar, 3, 1 > gravity
Gravitational acceleration experienced by model.
Definition: Model.hpp:59
Eigen::Matrix< Scalar, nq, 1 > home_configuration() const
Get a configuration vector for the model of all zeros.
Definition: Model.hpp:158
std::string name
Name of the model.
Definition: Model.hpp:32
int base_link_idx
Index of the base link in the models links vector.
Definition: Model.hpp:50
Model< NewScalar, nq > cast()
Casts the model to a new scalar type.
Definition: Model.hpp:183
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
int n_joints
Number of joints in the model.
Definition: Model.hpp:38
std::vector< Link< Scalar > > links
Vector of links in the model.
Definition: Model.hpp:44