FancySafeBot 0.0.1
A safe robotics library
Loading...
Searching...
No Matches
fsb_body_tree.h
1
2#ifndef FSB_BODY_TREE_H
3#define FSB_BODY_TREE_H
4
5#include <array>
6#include <cstddef>
7#include <cstdint>
8#include "fsb_configuration.h"
9#include "fsb_body.h"
10#include "fsb_joint.h"
11#include "fsb_motion.h"
12
13namespace fsb
14{
15
78
84{
85public:
89 BodyTree() = default;
90
92 static constexpr size_t base_index = 0U;
93
104 size_t add_body(
105 size_t parent_body_index, JointType joint_type, const Transform& parent_joint_transform,
106 const Body& body, BodyTreeError& err);
107
118 size_t add_massless_body(
119 size_t parent_body_index, JointType joint_type, const Transform& parent_joint_transform,
120 const MotionVector& origin_offset, BodyTreeError& err);
121
127 void set_gravity(const Vec3& gravity)
128 {
129 m_gravity = gravity;
130 }
131
139 Joint get_joint(size_t joint_index, BodyTreeError& err) const;
140
148 Body get_body(size_t body_index, BodyTreeError& err) const;
149
157 BodyTreeError set_body_mass_props(size_t body_index, const MassProps& mass_props);
158
166 BodyTreeError set_body_origin_offset(size_t body_index, const MotionVector& origin_offset);
167
176 set_parent_joint_transform(size_t body_index, const Transform& parent_joint_transform);
177
185 size_t get_body_dofs(size_t body_index, BodyTreeError& err) const;
186
192 [[nodiscard]] size_t get_num_bodies() const
193 {
194 return m_num_bodies;
195 }
196
202 [[nodiscard]] size_t get_num_dofs() const
203 {
204 return m_num_dofs;
205 }
206
212 [[nodiscard]] size_t get_num_coordinates() const
213 {
214 return m_num_coordinates;
215 }
216
222 [[nodiscard]] Vec3 get_gravity() const
223 {
224 return m_gravity;
225 }
226
233 [[ nodiscard ]] std::array<size_t, MaxSize::bodies> get_leaves(size_t& num_leaves) const
234 {
235 std::array<size_t, MaxSize::bodies> result = {};
236 num_leaves = 0U;
237 for (size_t index = 0; index < m_num_bodies; ++index)
238 {
239 if (m_bodies[index].is_leaf)
240 {
241 result[num_leaves] = index;
242 num_leaves++;
243 }
244 }
245 return result;
246 }
247
248private:
249 [[nodiscard]] BodyTreeError validate_massless_body_input(size_t parent_body_index) const;
250 [[nodiscard]] BodyTreeError
251 validate_add_body_input(size_t parent_body_index, JointType joint_type, const Body& body) const;
252
253 std::array<Body, MaxSize::bodies> m_bodies = {};
254 std::array<Joint, MaxSize::joints> m_joints = {};
255 Vec3 m_gravity = {};
256
257 size_t m_num_coordinates = 0U;
258 size_t m_num_dofs = 0U;
259 size_t m_num_bodies = 1U;
260 size_t m_num_joints = 0U;
261};
262
263// inline methods
264
265inline Joint BodyTree::get_joint(const size_t joint_index, BodyTreeError& err) const
266{
267 Joint joint = {};
268 if (joint_index < m_num_joints)
269 {
270 joint = m_joints[joint_index];
271 err = BodyTreeError::SUCCESS;
272 }
273 else
274 {
276 }
277 return joint;
278}
279
280inline Body BodyTree::get_body(const size_t body_index, BodyTreeError& err) const
281{
282 Body body = {};
283 if (body_index < m_num_bodies)
284 {
285 body = m_bodies[body_index];
286 err = BodyTreeError::SUCCESS;
287 }
288 else
289 {
291 }
292 return body;
293}
294
295inline BodyTreeError
296BodyTree::set_body_mass_props(const size_t body_index, const MassProps& mass_props)
297{
298 auto err = BodyTreeError::SUCCESS;
299 if (body_index < m_num_bodies)
300 {
301 m_bodies[body_index].mass_props = mass_props;
302 err = BodyTreeError::SUCCESS;
303 }
304 else
305 {
307 }
308 return err;
309}
310
311inline BodyTreeError
312BodyTree::set_body_origin_offset(const size_t body_index, const MotionVector& origin_offset)
313{
314 auto err = BodyTreeError::SUCCESS;
315 if (body_index < m_num_bodies)
316 {
317 m_bodies[body_index].origin_offset = origin_offset;
318 err = BodyTreeError::SUCCESS;
319 }
320 else
321 {
323 }
324 return err;
325}
326
328 const size_t body_index, const Transform& parent_joint_transform)
329{
330 auto err = BodyTreeError::SUCCESS;
331 if (body_index < m_num_bodies)
332 {
333 const size_t joint_index = m_bodies[body_index].joint_index;
334 m_joints[joint_index].parent_joint_transform = parent_joint_transform;
335 err = BodyTreeError::SUCCESS;
336 }
337 else
338 {
340 }
341 return err;
342}
343
348} // namespace fsb
349
350#endif
Body tree description of a kinematic chain.
Definition fsb_body_tree.h:84
void set_gravity(const Vec3 &gravity)
Set the gravity vector.
Definition fsb_body_tree.h:127
std::array< size_t, MaxSize::bodies > get_leaves(size_t &num_leaves) const
Get a list of body indexes that are leaves (bodies without children)
Definition fsb_body_tree.h:233
size_t add_body(size_t parent_body_index, JointType joint_type, const Transform &parent_joint_transform, const Body &body, BodyTreeError &err)
Add a body to the body tree.
Definition fsb_body_tree.cpp:189
size_t get_num_dofs() const
Get the number of body tree degrees of freedom.
Definition fsb_body_tree.h:202
size_t add_massless_body(size_t parent_body_index, JointType joint_type, const Transform &parent_joint_transform, const MotionVector &origin_offset, BodyTreeError &err)
Add a massless body to the body tree.
Definition fsb_body_tree.cpp:143
size_t get_num_coordinates() const
Get the number of generalized position coordinates.
Definition fsb_body_tree.h:212
size_t get_num_bodies() const
Get the number of bodies in tree.
Definition fsb_body_tree.h:192
BodyTree()=default
Construct a new BodyTree object.
Vec3 get_gravity() const
Get the gravity vector.
Definition fsb_body_tree.h:222
size_t get_body_dofs(size_t body_index, BodyTreeError &err) const
Get the number of degrees of freedom for a body.
Definition fsb_body_tree.cpp:237
static constexpr size_t base_index
Index of base body is always 0.
Definition fsb_body_tree.h:92
JointType
Joint type.
Definition fsb_joint.h:28
BodyTreeError set_body_mass_props(size_t body_index, const MassProps &mass_props)
Set the body mass properties object.
Definition fsb_body_tree.h:296
BodyTreeError set_body_origin_offset(size_t body_index, const MotionVector &origin_offset)
Set the body transform offset.
Definition fsb_body_tree.h:312
BodyTreeError set_parent_joint_transform(size_t body_index, const Transform &parent_joint_transform)
Set the parent joint transform object.
Definition fsb_body_tree.h:327
Body get_body(size_t body_index, BodyTreeError &err) const
Get body object from tree.
Definition fsb_body_tree.h:280
Joint get_joint(size_t joint_index, BodyTreeError &err) const
Get a joint object in the body tree.
Definition fsb_body_tree.h:265
BodyTreeError
Error list for body tree operations.
Definition fsb_body_tree.h:27
@ MAX_BODIES_REACHED
No more bodies can be added to the body tree. Maximum is specified by MaxSize::bodies.
@ MAX_JOINT_DOFS_REACHED
Joint requires more DoFs than is available. Maximum is specified by MaxSize::dofs.
@ MASS_ZERO_WITH_INERTIA
Body mass is set to zero but with non-zero inertia for a fixed parent joint.
@ INERTIA_ZERO
At least one of the inertia principal components are zero with a non-fixed parent joint.
@ MAX_JOINT_COORDINATES_REACHED
Joint requires more coordinates than is available. Maximum is specified by MaxSize::coordinates.
@ INERTIA_NOT_POS_DEF
Inertia is not positive definite.
@ MAX_JOINTS_REACHED
No more joints can be added to the body tree. Maximum is specified by MaxSize::joints.
@ BODY_NOT_IN_TREE
Index does not point to an existing body in tree.
@ PARENT_BODY_NONEXISTENT
Parent body index does not point to an existing body in tree.
@ MASS_ZERO
Body mass is zero with a non-fixed parent joint.
@ JOINT_NOT_IN_TREE
Index does not point to an existing joint in tree.
Link body parameters.
Definition fsb_body.h:90
Joint definition structure.
Definition fsb_joint.h:96
Mass properties of a body.
Definition fsb_body.h:45
Motion vector.
Definition fsb_motion.h:37
Coordinate transform.
Definition fsb_motion.h:22
3D vector
Definition fsb_types.h:27