14 #include <boost/asio/io_service.hpp>
15 #include <boost/function.hpp>
16 #include <boost/thread/thread.hpp>
22 #include <rclcpp/rclcpp.hpp>
23 #include <type_traits>
119 explicit Model(
const rclcpp::Node::SharedPtr& n =
nullptr,
unsigned int* n_threads =
nullptr);
166 std::vector<std::vector<unsigned int>>
mu;
224 std::vector<Math::SpatialTransform>
X_J;
226 std::vector<Math::SpatialVector>
c_J;
234 std::vector<Math::SpatialTransform>
X_T;
257 std::vector<Math::SpatialVector>
c;
260 std::vector<Math::SpatialMatrix>
IA;
266 std::vector<Math::SpatialVector>
U;
281 std::vector<Math::SpatialMatrix>
I_add;
282 std::vector<Math::SpatialMatrix>
I_H;
283 std::vector<Math::SpatialMatrix>
Ic_H;
287 std::vector<Math::Momentum>
hc;
349 std::unique_ptr<boost::asio::io_service::work>
work;
356 rcl_interfaces::msg::SetParametersResult
paramCb(
const std::vector<rclcpp::Parameter>& parameters);
362 for (
unsigned int i = 0; i <
threads.size(); ++i)
365 if ((retcode = pthread_setschedparam(
threads[i]->native_handle(), policy, ¶m)) != 0)
367 if (retcode == EPERM)
369 std::cerr <<
"Model.setThreadParameters: Failed setting thread parameters. Invalid permissions" << std::endl;
371 else if (retcode == ESRCH)
373 std::cerr <<
"Model.setThreadParameters: Failed setting thread parameters. No thread with the ID could be found" << std::endl;
375 else if (retcode == EINVAL)
377 std::cerr <<
"Model.setThreadParameters: Failed setting thread parameters. policy is not a recognized policy, or param does not make sense for "
383 std::cerr <<
"Model.setThreadParameters: Failed setting thread parameters. pthread_setschedparam returned with unknown error code " << retcode
429 std::string body_name =
"");
440 std::string body_name =
"");
456 return std::numeric_limits<unsigned int>::max();
474 catch (
const std::out_of_range& e)
485 std::map<std::string, unsigned int>::const_iterator iter =
mBodyNameMap.begin();
489 if (iter->second == body_id)
513 if ((
id > 0) && (
id <
mBodies.size()))
542 unsigned int parent_id =
lambda[id];
544 while (
mBodies[parent_id].mIsVirtual)
546 parent_id =
lambda[parent_id];
563 unsigned int child_id = id;
564 unsigned int parent_id =
lambda[id];
566 if (
mBodies[parent_id].mIsVirtual)
568 while (
mBodies[parent_id].mIsVirtual)
570 child_id = parent_id;
571 parent_id =
lambda[child_id];
573 return X_T[child_id];
588 throw RdlException(
"Error: setting of parent transform not supported for fixed bodies!");
591 unsigned int child_id = id;
592 unsigned int parent_id =
lambda[id];
594 if (
mBodies[parent_id].mIsVirtual)
596 while (
mBodies[parent_id].mIsVirtual)
598 child_id = parent_id;
599 parent_id =
lambda[child_id];
601 X_T[child_id] = transform;
618 unsigned int q_index =
mJoints[i].q_index;
631 unsigned int q_index =
mJoints[i].q_index;
633 Q[q_index] = quat[0];
634 Q[q_index + 1] = quat[1];
635 Q[q_index + 2] = quat[2];
656 if (id_1 == 0 || id_2 == 0)
664 if (chain_1_size <= chain_2_size)
667 for (
unsigned int i = 1; i < chain_1_size; i++)
680 for (
unsigned int i = 1; i < chain_2_size; i++)
705 if (rbi(0, 0) <= 0 || rbi(1, 1) <= 0 || rbi(2, 2) <= 0)
707 std::cerr <<
"\033[1;31m Invalid inertia: Each element of the trace must be > 0 \033[0m" << std::endl;
711 if (rbi(0, 0) >= (rbi(1, 1) + rbi(2, 2)))
713 std::cerr <<
"\033[1;31m Invalid inertia: Triangle inequality not satisfied \033[0m\n" << std::endl;
717 if (rbi(1, 1) >= (rbi(2, 2) + rbi(0, 0)))
719 std::cerr <<
"\033[1;31m Invalid inertia: Triangle inequality not satisfied \033[0m\n" << std::endl;
723 if (rbi(2, 2) >= (rbi(1, 1) + rbi(0, 0)))
725 std::cerr <<
"\033[1;31m Invalid inertia: Triangle inequality not satisfied \033[0m\n" << std::endl;
729 if (!(rbi - rbi.transpose()).isZero(1.e-8))
731 std::cerr <<
"\033[1;31m Invalid inertia: Inertia matrix is not symmetric \033[0m\n" << std::endl;
735 Eigen::EigenSolver<Eigen::Matrix3d> solver(rbi);
736 Eigen::EigenSolver<Eigen::Matrix3d>::EigenvalueType eivals = solver.eigenvalues();
738 for (
unsigned int i = 0; i < eivals.rows(); i++)
740 if (eivals[i].real() <= 0)
742 std::cerr <<
"\033[1;31m Invalid inertia: Inertia matrix is not positive definite \033[0m\n" << std::endl;
Definition: rdl_eigenmath.hpp:104
Definition: MotionVector.hpp:21
Quaternion that are used for singularity free joints.
Definition: rdl_eigenmath.hpp:614
A custom exception.
Definition: RdlExceptions.hpp:18
std::shared_ptr< ReferenceFrame > ReferenceFramePtr
Definition: ReferenceFrame.hpp:68
std::vector< SpatialMotion, Eigen::aligned_allocator< SpatialMotion > > SpatialMotionV
Definition: SpatialMotion.hpp:263
std::vector< SpatialForce, Eigen::aligned_allocator< SpatialForce > > SpatialForceV
Definition: SpatialForce.hpp:252
std::vector< MotionVector, Eigen::aligned_allocator< MotionVector > > MotionVectorV
Definition: MotionVector.hpp:347
std::vector< SpatialInertia, Eigen::aligned_allocator< SpatialInertia > > SpatialInertiaV
Definition: SpatialRigidBodyInertia.hpp:144
Eigen::VectorXd VectorNd
Definition: rdl_eigenmath.hpp:20
std::vector< SpatialAcceleration, Eigen::aligned_allocator< SpatialAcceleration > > SpatialAccelerationV
Definition: SpatialAcceleration.hpp:154
std::vector< RigidBodyInertia, Eigen::aligned_allocator< RigidBodyInertia > > RigidBodyInertiaV
Definition: RigidBodyInertia.hpp:285
Eigen::MatrixXd MatrixNd
Definition: rdl_eigenmath.hpp:21
Namespace for all structures of the RobotDynamics library.
Definition: examples.hpp:19
@ JointTypeSpherical
3 DoF joint using Quaternions for joint positional variables and
Definition: Joint.hpp:189
std::shared_ptr< Model > ModelPtr
Definition: Model.hpp:750
Describes all properties of a single body.
Definition: Body.hpp:88
Math::Matrix3d mInertia
Inertia matrix at the center of mass.
Definition: Body.hpp:350
bool mIsVirtual
Definition: Body.hpp:356
CustomJoint is a struct used to create a joint with user defined parameters. This is accomplished by ...
Definition: Joint.hpp:657
Describes a joint relative to the predecessor body.
Definition: Joint.hpp:223
Contains all information about the rigid body model.
Definition: Model.hpp:112
Model(const rclcpp::Node::SharedPtr &n=nullptr, unsigned int *n_threads=nullptr)
Constructor.
Definition: Model.cpp:35
Math::SpatialMotionV v
Definition: Model.hpp:211
std::vector< unsigned int > mJointUpdateOrder
Definition: Model.hpp:230
std::vector< Math::SpatialVector > c
The velocity dependent spatial acceleration.
Definition: Model.hpp:257
std::vector< std::vector< unsigned int > > lambda_chain
Definition: Model.hpp:161
std::vector< Math::Vector3d > multdof3_u
Definition: Model.hpp:248
boost::asio::io_service io_service
Definition: Model.hpp:348
std::vector< CustomJoint * > mCustomJoints
Definition: Model.hpp:251
Math::SpatialTransform GetJointFrame(unsigned int id)
Returns the joint frame transformtion, i.e. the second argument to Model::addBody().
Definition: Model.hpp:556
std::vector< FixedBody > mFixedBodies
All bodies that are attached to a body via a fixed joint.
Definition: Model.hpp:310
~Model()
Definition: Model.hpp:121
unsigned int qdot_size
The size of the.
Definition: Model.hpp:191
boost::thread_group thread_pool
Definition: Model.hpp:351
bool IsBodyId(unsigned int id) const
Definition: Model.hpp:511
unsigned int getCommonMovableParentId(unsigned int id_1, unsigned int id_2) const
Definition: Model.hpp:639
std::vector< Math::SpatialMatrix > I_H
Definition: Model.hpp:282
Math::MotionVector gravity
the cartesian vector of the gravity
Definition: Model.hpp:202
ReferenceFramePtr cobFrame
Definition: Model.hpp:346
Math::MatrixNd ndof0_mat
Definition: Model.hpp:293
std::vector< Math::SpatialTransform > X_J
Definition: Model.hpp:224
std::unique_ptr< boost::asio::io_service::work > work
Definition: Model.hpp:349
rclcpp::Node::SharedPtr node
Definition: Model.hpp:353
Math::SpatialForceV pA
The spatial bias force.
Definition: Model.hpp:263
std::vector< ReferenceFramePtr > bodyFrames
Definition: Model.hpp:145
std::vector< Body > mBodies
All bodies 0 ... N_B, including the base.
Definition: Model.hpp:333
double fluidDensity
Definition: Model.hpp:203
std::vector< Math::SpatialTransform > X_lambda
Transformation from the parent body to the current body.
Definition: Model.hpp:307
rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr callback_handle
Definition: Model.hpp:354
Math::SpatialForceV f
Internal forces on the body (used only InverseDynamics())
Definition: Model.hpp:275
Math::Quaternion GetQuaternion(unsigned int i, const Math::VectorNd &Q) const
Gets the quaternion for body i (only valid if body i is connected by a JointTypeSpherical joint)
Definition: Model.hpp:615
std::vector< unsigned int > mFixedJointCount
The number of fixed joints that have been declared before each joint.
Definition: Model.hpp:238
std::vector< Math::Matrix3d > multdof3_Dinv
Definition: Model.hpp:247
unsigned int GetParentBodyId(unsigned int id)
Determines id the actual parent body.
Definition: Model.hpp:535
bool validateRigidBodyInertia(const Body &body)
Definition: Model.hpp:696
Math::MotionVectorV S_o
Definition: Model.hpp:221
std::vector< Math::SpatialMatrix > I_add
Definition: Model.hpp:281
void setupRosParameters()
Call this after the model is fully populated to declare ros parameters that define the mass parameter...
Definition: Model.cpp:615
Math::VectorNd d
Temporary variable D_i (RBDA p. 130)
Definition: Model.hpp:269
ReferenceFramePtr worldFrame
Definition: Model.hpp:141
unsigned int GetBodyId(const char *body_name) const
Returns the id of a body that was passed to addBody()
Definition: Model.hpp:452
void SetQuaternion(unsigned int i, const Math::Quaternion &quat, Math::VectorNd &Q) const
Sets the quaternion for body i (only valid if body i is connected by a JointTypeSpherical joint)
Definition: Model.hpp:628
Math::MotionVectorV S
Definition: Model.hpp:220
ReferenceFramePtr comFrame
Definition: Model.hpp:343
Math::MotionVector a_f
Definition: Model.hpp:204
std::vector< boost::thread * > threads
Definition: Model.hpp:350
std::vector< unsigned int > lambda
Definition: Model.hpp:160
bool setThreadParameters(int policy, struct sched_param param)
Definition: Model.hpp:358
Math::VectorNd ndof0_vec
Definition: Model.hpp:296
Math::VectorNd nbodies0_vec
Definition: Model.hpp:297
unsigned int addBody(const unsigned int parent_id, const Math::SpatialTransform &joint_frame, const Joint &joint, const Body &body, std::string body_name="")
Connects a given body to the model.
Definition: Model.cpp:328
std::map< std::string, unsigned int > mBodyNameMap
Human readable names for the bodies.
Definition: Model.hpp:336
std::vector< Math::Matrix63 > multdof3_S
Motion subspace for joints with 3 degrees of freedom.
Definition: Model.hpp:244
std::vector< Math::SpatialTransform > X_T
Transformations from the parent body to the frame of the joint.
Definition: Model.hpp:234
std::vector< Math::Matrix63 > multdof3_U
Definition: Model.hpp:246
Math::MotionVector v_f
Definition: Model.hpp:205
std::vector< ReferenceFramePtr > bodyCenterOfBuoyancyFrames
Definition: Model.hpp:154
std::vector< Math::SpatialMatrix > IA
The spatial inertia of the bodies.
Definition: Model.hpp:260
unsigned int num_branch_ends
Definition: Model.hpp:340
ReferenceFramePtr getReferenceFrame(const std::string &frameName) const
Get a fixed or moveable body's reference frame.
Definition: Model.hpp:467
std::shared_ptr< Model > SharedPtr
Definition: Model.hpp:113
unsigned int addBodySphericalJoint(const unsigned int parent_id, const Math::SpatialTransform &joint_frame, const Joint &joint, const Body &body, std::string body_name="")
std::vector< ReferenceFramePtr > fixedBodyFrames
Definition: Model.hpp:158
Math::SpatialForceV f_b
Definition: Model.hpp:276
std::vector< Math::Matrix63 > multdof3_S_o
Definition: Model.hpp:245
Math::SpatialAccelerationV a
The spatial velocity of the bodies.
Definition: Model.hpp:209
unsigned int fixed_body_discriminator
Value that is used to discriminate between fixed and movable bodies.
Definition: Model.hpp:324
Math::RigidBodyInertiaV Ic
Definition: Model.hpp:279
std::vector< ReferenceFramePtr > bodyCenteredFrames
Definition: Model.hpp:152
Math::MatrixNd three_x_qd0
Definition: Model.hpp:294
void operator=(const Model &)=delete
std::vector< Math::SpatialVector > U
Temporary variable U_i (RBDA p. 130)
Definition: Model.hpp:266
bool IsFixedBodyId(unsigned int body_id) const
Checks whether the body is rigidly attached to another body.
Definition: Model.hpp:502
void crawlChainKinematics(unsigned int b_id, std::atomic< unsigned int > *branch_ends, const Math::VectorNd *Q, const Math::VectorNd *QDot, const Math::VectorNd *QDDot)
Definition: Model.cpp:1534
unsigned int dof_count
number of degrees of freedoms of the model
Definition: Model.hpp:173
unsigned int q_size
The size of the -vector. For models without spherical joints the value is the same as Model::dof_coun...
Definition: Model.hpp:182
Model(const Model &)=delete
Math::VectorNd q0_vec
Definition: Model.hpp:295
std::string GetBodyName(unsigned int body_id) const
Returns the name of a body for a given body id.
Definition: Model.hpp:483
std::vector< Joint > mJoints
All joints.
Definition: Model.hpp:218
std::vector< std::vector< unsigned int > > mu
Definition: Model.hpp:166
std::vector< Math::Momentum > hc
Definition: Model.hpp:287
std::vector< unsigned int > lambda_q
Definition: Model.hpp:163
std::vector< Math::SpatialVector > c_J
Definition: Model.hpp:226
unsigned int appendBody(const Math::SpatialTransform &joint_frame, const Joint &joint, const Body &body, std::string body_name="")
Adds a Body to the model such that the previously added Body is the Parent.
Definition: Model.cpp:593
Math::SpatialInertiaV Ib_c
Definition: Model.hpp:285
unsigned int addBodyCustomJoint(const unsigned int parent_id, const Math::SpatialTransform &joint_frame, CustomJoint *custom_joint, const Body &body, std::string body_name="")
Definition: Model.cpp:598
std::map< std::string, ReferenceFramePtr > referenceFrameMap
Definition: Model.hpp:339
rcl_interfaces::msg::SetParametersResult paramCb(const std::vector< rclcpp::Parameter > ¶meters)
Definition: Model.cpp:952
void SetJointFrame(unsigned int id, const Math::SpatialTransform &transform)
Sets the joint frame transformtion, i.e. the second argument to Model::addBody().
Definition: Model.hpp:584
Math::VectorNd u
Temporary variable u (RBDA p. 130)
Definition: Model.hpp:272
double mass
Definition: Model.hpp:194
unsigned int previously_added_body_id
Id of the previously added body, required for Model::appendBody()
Definition: Model.hpp:199
Math::SpatialInertiaV I
Definition: Model.hpp:278
std::vector< Math::SpatialMatrix > Ic_H
Definition: Model.hpp:283
std::vector< unsigned int > multdof3_w_index
Definition: Model.hpp:249
Math::SpatialMotionV v_J
Definition: Model.hpp:228
double volume
Definition: Model.hpp:196