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 <cstdint>
7#include "fsb_configuration.h"
8#include "fsb_body.h"
9#include "fsb_joint.h"
10#include "fsb_motion.h"
11#include "fsb_types.h"
12
13namespace fsb
14{
15
82
87{
88 std::array<bool, MaxSize::kDofs> set;
89 std::array<Real, MaxSize::kDofs> lower_position;
90 std::array<Real, MaxSize::kDofs> upper_position;
91 std::array<Real, MaxSize::kDofs> max_velocity;
92};
93
99{
100public:
104 BodyTree() = default;
105
107 static constexpr size_t kBaseIndex = 0U;
108
119 size_t add_body(
120 size_t parent_body_index, JointType joint_type, const Transform& parent_joint_transform,
121 const Body& body, BodyTreeError& err);
122
133 size_t add_massless_body(
134 size_t parent_body_index, JointType joint_type, const Transform& parent_joint_transform,
135 const MotionVector& origin_offset, BodyTreeError& err);
136
149 set_joint_position_limit(size_t joint_index, Real lower_position, Real upper_position);
150
159 BodyTreeError unset_joint_position_limit(size_t joint_index);
160
174 size_t joint_index, bool& is_set, Real& lower_position, Real& upper_position) const;
175
186 BodyTreeError set_joint_velocity_limit(size_t joint_index, Real max_velocity);
187
198 BodyTreeError get_joint_velocity_limit(size_t joint_index, Real& max_velocity) const;
199
208 BodyTreeError set_joint_reversed(size_t joint_index, bool reversed);
209
215 void set_gravity(const Vec3& gravity)
216 {
217 m_gravity = gravity;
218 }
219
227 Joint get_joint(size_t joint_index, BodyTreeError& err) const;
228
236 Body get_body(size_t body_index, BodyTreeError& err) const;
237
245 BodyTreeError set_body_mass_props(size_t body_index, const MassProps& mass_props);
246
254 BodyTreeError set_body_origin_offset(size_t body_index, const MotionVector& origin_offset);
255
264 set_parent_joint_transform(size_t body_index, const Transform& parent_joint_transform);
265
273 size_t get_body_dofs(size_t body_index, BodyTreeError& err) const;
274
280 [[nodiscard]] size_t get_num_bodies() const
281 {
282 return m_num_bodies;
283 }
284
290 [[nodiscard]] size_t get_num_dofs() const
291 {
292 return m_num_dofs;
293 }
294
300 [[nodiscard]] size_t get_num_coordinates() const
301 {
302 return m_num_coordinates;
303 }
304
310 [[nodiscard]] Vec3 get_gravity() const
311 {
312 return m_gravity;
313 }
314
321 [[nodiscard]] std::array<size_t, MaxSize::kBodies> get_leaves(size_t& num_leaves) const
322 {
323 std::array<size_t, MaxSize::kBodies> result = {};
324 num_leaves = 0U;
325 for (size_t index = 0; index < m_num_bodies; ++index)
326 {
327 if (m_bodies[index].is_leaf)
328 {
329 result[num_leaves] = index;
330 num_leaves++;
331 }
332 }
333 return result;
334 }
335
336private:
337 [[nodiscard]] BodyTreeError validate_massless_body_input(size_t parent_body_index) const;
338 [[nodiscard]] BodyTreeError
339 validate_add_body_input(size_t parent_body_index, JointType joint_type, const Body& body) const;
340
341 std::array<Body, MaxSize::kBodies> m_bodies = {};
342 std::array<Joint, MaxSize::kJoints> m_joints = {};
343 JointLimits m_limits = {};
344 Vec3 m_gravity = {};
345
346 size_t m_num_coordinates = 0U;
347 size_t m_num_dofs = 0U;
348 size_t m_num_bodies = 1U;
349 size_t m_num_joints = 0U;
350};
351
352// inline methods
353
354inline Joint BodyTree::get_joint(const size_t joint_index, BodyTreeError& err) const
355{
356 Joint joint = {};
357 if (joint_index < m_num_joints)
358 {
359 joint = m_joints[joint_index];
360 err = BodyTreeError::SUCCESS;
361 }
362 else
363 {
365 }
366 return joint;
367}
368
369inline Body BodyTree::get_body(const size_t body_index, BodyTreeError& err) const
370{
371 Body body = {};
372 if (body_index < m_num_bodies)
373 {
374 body = m_bodies[body_index];
375 err = BodyTreeError::SUCCESS;
376 }
377 else
378 {
380 }
381 return body;
382}
383
384inline BodyTreeError
385BodyTree::set_body_mass_props(const size_t body_index, const MassProps& mass_props)
386{
387 auto err = BodyTreeError::SUCCESS;
388 if (body_index < m_num_bodies)
389 {
390 m_bodies[body_index].mass_props = mass_props;
391 err = BodyTreeError::SUCCESS;
392 }
393 else
394 {
396 }
397 return err;
398}
399
400inline BodyTreeError
401BodyTree::set_body_origin_offset(const size_t body_index, const MotionVector& origin_offset)
402{
403 auto err = BodyTreeError::SUCCESS;
404 if (body_index < m_num_bodies)
405 {
406 m_bodies[body_index].origin_offset = origin_offset;
407 err = BodyTreeError::SUCCESS;
408 }
409 else
410 {
412 }
413 return err;
414}
415
417 const size_t body_index, const Transform& parent_joint_transform)
418{
419 auto err = BodyTreeError::SUCCESS;
420 if (body_index < m_num_bodies)
421 {
422 const size_t joint_index = m_bodies[body_index].joint_index;
423 m_joints[joint_index].parent_joint_transform = parent_joint_transform;
424 err = BodyTreeError::SUCCESS;
425 }
426 else
427 {
429 }
430 return err;
431}
432
437} // namespace fsb
438
439#endif
Body tree description of a kinematic chain.
Definition fsb_body_tree.h:99
BodyTreeError set_joint_position_limit(size_t joint_index, Real lower_position, Real upper_position)
Set joint position limits for a joint in the body tree.
Definition fsb_body_tree.cpp:295
void set_gravity(const Vec3 &gravity)
Set the gravity vector.
Definition fsb_body_tree.h:215
BodyTreeError unset_joint_position_limit(size_t joint_index)
Unset joint position limits for a joint in the body tree.
Definition fsb_body_tree.cpp:318
BodyTreeError set_joint_velocity_limit(size_t joint_index, Real max_velocity)
Set joint velocity limit for a joint in the body tree.
Definition fsb_body_tree.cpp:365
BodyTreeError get_joint_velocity_limit(size_t joint_index, Real &max_velocity) const
Get joint velocity limit for a joint in the body tree.
Definition fsb_body_tree.cpp:385
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:290
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:141
size_t get_num_coordinates() const
Get the number of generalized position coordinates.
Definition fsb_body_tree.h:300
size_t get_num_bodies() const
Get the number of bodies in tree.
Definition fsb_body_tree.h:280
BodyTree()=default
Construct a new BodyTree object.
static constexpr size_t kBaseIndex
Index of base body is always 0.
Definition fsb_body_tree.h:107
Vec3 get_gravity() const
Get the gravity vector.
Definition fsb_body_tree.h:310
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:239
BodyTreeError get_joint_position_limit(size_t joint_index, bool &is_set, Real &lower_position, Real &upper_position) const
Get joint position limits for a joint in the body tree.
Definition fsb_body_tree.cpp:338
std::array< size_t, MaxSize::kBodies > get_leaves(size_t &num_leaves) const
Get a list of body indexes that are leaves (bodies without children)
Definition fsb_body_tree.h:321
BodyTreeError set_joint_reversed(size_t joint_index, bool reversed)
Set the reversed flag for a joint in the body tree.
Definition fsb_body_tree.cpp:405
@ SUCCESS
Successful operation.
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:385
BodyTreeError set_body_origin_offset(size_t body_index, const MotionVector &origin_offset)
Set the body transform offset.
Definition fsb_body_tree.h:401
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:416
Body get_body(size_t body_index, BodyTreeError &err) const
Get body object from tree.
Definition fsb_body_tree.h:369
Joint get_joint(size_t joint_index, BodyTreeError &err) const
Get a joint object in the body tree.
Definition fsb_body_tree.h:354
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::kDofs.
@ 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.
@ UNSUPPORTED_JOINT_TYPE
Joint type is not supported for the operation.
@ 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:89
Limits for joint vectors.
Definition fsb_body_tree.h:87
std::array< bool, MaxSize::kDofs > set
Enable limits for each joint DoF.
Definition fsb_body_tree.h:88
std::array< Real, MaxSize::kDofs > upper_position
Maximum joint position limit.
Definition fsb_body_tree.h:90
std::array< Real, MaxSize::kDofs > lower_position
Minimum joint position limit.
Definition fsb_body_tree.h:89
std::array< Real, MaxSize::kDofs > max_velocity
Maximum joint velocity limit.
Definition fsb_body_tree.h:91
Joint definition structure.
Definition fsb_joint.h:96
Mass properties of a body.
Definition fsb_body.h:44
Motion vector.
Definition fsb_motion.h:37
Coordinate transform.
Definition fsb_motion.h:22
3D vector
Definition fsb_types.h:27