13 #include <geometry_msgs/PointStamped.h> 22 #include "rdl_msgs/ChangeOrientationFrame.h" 23 #include "rdl_msgs/ChangePointFrame.h" 24 #include "rdl_msgs/ChangePointArrayFrame.h" 25 #include "rdl_msgs/ChangePoseFrame.h" 26 #include "rdl_msgs/Change3DVectorFrame.h" 27 #include "rdl_msgs/Change3DVectorArrayFrame.h" 28 #include "rdl_msgs/ChangeTwistFrame.h" 29 #include "rdl_msgs/ChangeTwistArrayFrame.h" 30 #include "rdl_msgs/ChangeWrenchArrayFrame.h" 31 #include "rdl_msgs/ChangeWrenchFrame.h" 32 #include "rdl_msgs/GetTransform.h" 33 #include "rdl_msgs/GetTwist.h" 34 #include "rdl_msgs/GetBodyGravityWrench.h" 35 #include "rdl_msgs/GetRobotCenterOfMass.h" 36 #include "rdl_msgs/RobotState.h" 51 if (msg.position.size() !=
model->q_size)
53 std::cout <<
"kinematics_interface: position vector has incorrect size. Should be " <<
model->q_size << std::endl;
57 for (
unsigned int i = 0; i < msg.position.size(); i++)
59 q[i] = msg.position[i];
62 if (msg.velocity.size() !=
model->qdot_size)
68 for (
unsigned int i = 0; i <
model->qdot_size; i++)
70 qdot[i] = msg.velocity[i];
77 bool hasEnding(std::string
const& fullString, std::string
const& ending)
79 if (fullString.length() >= ending.length())
81 return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
93 if (!std::strcmp(name.c_str(),
"world"))
95 return model->worldFrame;
99 return model->referenceFrameMap.at(name);
102 catch (
const std::out_of_range& e)
109 std::string parent_body_name = name.substr(0, name.size() - 4);
112 unsigned int parent_body_id =
model->GetBodyId(parent_body_name.c_str());
115 if (parent_body_id == std::numeric_limits<unsigned int>::max())
120 return model->bodyCenteredFrames[parent_body_id];
127 bool changePoseFrame(rdl_msgs::ChangePoseFrameRequest& req, rdl_msgs::ChangePoseFrameResponse& resp)
130 if (old_frame ==
nullptr)
132 resp.report =
"No reference frame found with name " + req.pose_in.header.frame_id;
133 resp.success =
false;
138 if (new_frame ==
nullptr)
140 resp.report =
"No reference frame found with name " + req.new_reference_frame;
141 resp.success =
false;
146 req.pose_in.pose.orientation.w);
153 resp.pose_out.header.frame_id = new_frame->getName();
154 resp.pose_out.pose.orientation.x = quat.
x();
155 resp.pose_out.pose.orientation.y = quat.
y();
156 resp.pose_out.pose.orientation.z = quat.
z();
157 resp.pose_out.pose.orientation.w = quat.
w();
158 resp.pose_out.pose.position.x = p.
x();
159 resp.pose_out.pose.position.y = p.
y();
160 resp.pose_out.pose.position.z = p.
z();
166 bool changeOrientationFrame(rdl_msgs::ChangeOrientationFrameRequest& req, rdl_msgs::ChangeOrientationFrameResponse& resp)
169 if (old_frame ==
nullptr)
171 resp.report =
"No reference frame found with name " + req.orientation_in.header.frame_id;
172 resp.success =
false;
177 if (new_frame ==
nullptr)
179 resp.report =
"No reference frame found with name " + req.new_reference_frame;
180 resp.success =
false;
185 req.orientation_in.quaternion.w);
189 resp.orientation_out.header.frame_id = new_frame->getName();
190 resp.orientation_out.quaternion.x = quat.
x();
191 resp.orientation_out.quaternion.y = quat.
y();
192 resp.orientation_out.quaternion.z = quat.
z();
193 resp.orientation_out.quaternion.w = quat.
w();
199 bool changePointFrame(rdl_msgs::ChangePointFrameRequest& req, rdl_msgs::ChangePointFrameResponse& resp)
202 if (old_frame ==
nullptr)
204 resp.report =
"No reference frame found with name " + req.point_in.header.frame_id;
205 resp.success =
false;
210 if (new_frame ==
nullptr)
212 resp.report =
"No reference frame found with name " + req.new_reference_frame;
213 resp.success =
false;
221 resp.point_out.header.frame_id = new_frame->getName();
222 resp.point_out.point.x = p.
x();
223 resp.point_out.point.y = p.
y();
224 resp.point_out.point.z = p.
z();
230 bool changePointArrayFrame(rdl_msgs::ChangePointArrayFrameRequest& req, rdl_msgs::ChangePointArrayFrameResponse& resp)
232 if (req.point_array_in.size() != req.new_reference_frame.size())
234 resp.success =
false;
235 resp.report =
"Size of point array doesn't match size of new reference frame array";
239 geometry_msgs::PointStamped point;
240 for (
unsigned int i = 0; i < req.point_array_in.size(); i++)
243 if (old_frame ==
nullptr)
245 resp.report =
"No reference frame found with name " + req.point_array_in[i].header.frame_id;
246 resp.success =
false;
251 if (new_frame ==
nullptr)
253 resp.report =
"No reference frame found with name " + req.new_reference_frame[i];
254 resp.success =
false;
262 point.header.frame_id = new_frame->getName();
263 point.point.x = p.
x();
264 point.point.y = p.
y();
265 point.point.z = p.
z();
267 resp.point_array_out.push_back(point);
275 bool change3DVectorFrame(rdl_msgs::Change3DVectorFrameRequest& req, rdl_msgs::Change3DVectorFrameResponse& resp)
278 if (old_frame ==
nullptr)
280 resp.report =
"No reference frame found with name " + req.vector_in.header.frame_id;
281 resp.success =
false;
286 if (new_frame ==
nullptr)
288 resp.report =
"No reference frame found with name " + req.new_reference_frame;
289 resp.success =
false;
297 resp.vector_out.header.frame_id = new_frame->getName();
298 resp.vector_out.vector.x = v.x();
299 resp.vector_out.vector.y = v.y();
300 resp.vector_out.vector.z = v.z();
308 geometry_msgs::Vector3Stamped vector;
309 for (
unsigned int i = 0; i < req.vector_array_in.size(); i++)
312 if (old_frame ==
nullptr)
314 resp.report =
"No reference frame found with name " + req.vector_array_in[i].header.frame_id;
315 resp.success =
false;
320 if (new_frame ==
nullptr)
322 resp.report =
"No reference frame found with name " + req.new_reference_frame[i];
323 resp.success =
false;
331 vector.header.frame_id = new_frame->getName();
332 vector.vector.x = v.x();
333 vector.vector.y = v.y();
334 vector.vector.z = v.z();
336 resp.vector_array_out.push_back(vector);
344 bool changeWrenchFrame(rdl_msgs::ChangeWrenchFrameRequest& req, rdl_msgs::ChangeWrenchFrameResponse& resp)
348 if (old_frame ==
nullptr)
350 resp.report =
"No reference frame found with name " + req.wrench_in.header.frame_id;
351 resp.success =
false;
356 if (new_frame ==
nullptr)
358 resp.report =
"No reference frame found with name " + req.new_reference_frame;
359 resp.success =
false;
364 req.wrench_in.wrench.force.x, req.wrench_in.wrench.force.y, req.wrench_in.wrench.force.z);
368 resp.wrench_out.header.frame_id = new_frame->getName();
369 resp.wrench_out.wrench.torque.x = force.
mx();
370 resp.wrench_out.wrench.torque.y = force.
my();
371 resp.wrench_out.wrench.torque.z = force.
mz();
372 resp.wrench_out.wrench.force.x = force.
fx();
373 resp.wrench_out.wrench.force.y = force.
fy();
374 resp.wrench_out.wrench.force.z = force.
fz();
380 bool changeWrenchArrayFrame(rdl_msgs::ChangeWrenchArrayFrameRequest& req, rdl_msgs::ChangeWrenchArrayFrameResponse& resp)
382 if (req.wrench_array_in.size() != req.new_reference_frame.size())
384 resp.report =
"Size of wrench array does not match size of new reference frame array";
385 resp.success =
false;
389 geometry_msgs::WrenchStamped wrench;
390 for (
unsigned int i = 0; i < req.wrench_array_in.size(); i++)
394 if (old_frame ==
nullptr)
396 resp.report =
"No reference frame found with name " + req.wrench_array_in[i].header.frame_id;
397 resp.success =
false;
402 if (new_frame ==
nullptr)
404 resp.report =
"No reference frame found with name " + req.new_reference_frame[i];
405 resp.success =
false;
410 req.wrench_array_in[i].wrench.torque.z, req.wrench_array_in[i].wrench.force.x, req.wrench_array_in[i].wrench.force.y,
411 req.wrench_array_in[i].wrench.force.z);
415 wrench.header.frame_id = new_frame->getName();
416 wrench.wrench.torque.x = force.
mx();
417 wrench.wrench.torque.y = force.
my();
418 wrench.wrench.torque.z = force.
mz();
419 wrench.wrench.force.x = force.
fx();
420 wrench.wrench.force.y = force.
fy();
421 wrench.wrench.force.z = force.
fz();
422 resp.wrench_array_out.push_back(wrench);
429 bool changeTwistFrame(rdl_msgs::ChangeTwistFrameRequest& req, rdl_msgs::ChangeTwistFrameResponse& resp)
432 if (old_frame ==
nullptr)
434 resp.report =
"No reference frame found with name " + req.twist_in.header.frame_id;
435 resp.success =
false;
440 if (new_frame ==
nullptr)
442 resp.report =
"No reference frame found with name " + req.new_reference_frame;
443 resp.success =
false;
448 req.twist_in.twist.linear.y, req.twist_in.twist.linear.z);
453 resp.twist_out.header.frame_id = new_frame->getName();
454 resp.twist_out.twist.angular.x = twist.
wx();
455 resp.twist_out.twist.angular.y = twist.
wy();
456 resp.twist_out.twist.angular.z = twist.
wz();
457 resp.twist_out.twist.linear.x = twist.
vx();
458 resp.twist_out.twist.linear.y = twist.
vy();
459 resp.twist_out.twist.linear.z = twist.
vz();
465 bool changeTwistArrayFrame(rdl_msgs::ChangeTwistArrayFrameRequest& req, rdl_msgs::ChangeTwistArrayFrameResponse& resp)
467 if (req.twist_array_in.size() != req.new_reference_frame.size())
469 resp.report =
"Size of twist array does not match size of new reference frame array";
470 resp.success =
false;
474 geometry_msgs::TwistStamped twist_msg;
475 for (
unsigned int i = 0; i < req.twist_array_in.size(); i++)
478 if (old_frame ==
nullptr)
480 resp.report =
"No reference frame found with name " + req.twist_array_in[i].header.frame_id;
481 resp.success =
false;
486 if (new_frame ==
nullptr)
488 resp.report =
"No reference frame found with name " + req.new_reference_frame[i];
489 resp.success =
false;
494 req.twist_array_in[i].twist.linear.x, req.twist_array_in[i].twist.linear.y, req.twist_array_in[i].twist.linear.z);
499 twist_msg.header.frame_id = new_frame->getName();
500 twist_msg.twist.angular.x = twist.
wx();
501 twist_msg.twist.angular.y = twist.
wy();
502 twist_msg.twist.angular.z = twist.
wz();
503 twist_msg.twist.linear.x = twist.
vx();
504 twist_msg.twist.linear.y = twist.
vy();
505 twist_msg.twist.linear.z = twist.
vz();
506 resp.twist_array_out.push_back(twist_msg);
514 bool getTwist(rdl_msgs::GetTwistRequest& req, rdl_msgs::GetTwistResponse& resp)
517 if (body_frame ==
nullptr)
519 resp.report =
"No reference frame found with name " + req.body_frame;
520 resp.success =
false;
525 if (base_frame ==
nullptr)
527 resp.report =
"No reference frame found with name " + req.base_frame;
528 resp.success =
false;
533 if (expressed_in_frame ==
nullptr)
535 resp.report =
"No reference frame found with name " + req.expressed_in_frame;
536 resp.success =
false;
542 resp.twist_out.header.frame_id = expressed_in_frame->getName();
543 resp.twist_out.twist.angular.x = m.
wx();
544 resp.twist_out.twist.angular.y = m.
wy();
545 resp.twist_out.twist.angular.z = m.
wz();
546 resp.twist_out.twist.linear.x = m.
vx();
547 resp.twist_out.twist.linear.y = m.
vy();
548 resp.twist_out.twist.linear.z = m.
vz();
553 bool getTransform(rdl_msgs::GetTransformRequest& req, rdl_msgs::GetTransformResponse& resp)
557 if (body_frame ==
nullptr)
559 resp.report =
"No reference frame found with name " + req.from_reference_frame;
560 resp.success =
false;
565 if (base_frame ==
nullptr)
567 resp.report =
"No reference frame found with name " + req.to_reference_frame;
568 resp.success =
false;
576 resp.transform.header.frame_id = body_frame->getName();
577 resp.transform.child_frame_id = base_frame->getName();
578 resp.transform.transform.rotation.x = orientation.
x();
579 resp.transform.transform.rotation.y = orientation.
y();
580 resp.transform.transform.rotation.z = orientation.
z();
581 resp.transform.transform.rotation.w = orientation.
w();
583 resp.transform.transform.translation.x = X.
r.x();
584 resp.
transform.transform.translation.y = X.
r.y();
585 resp.
transform.transform.translation.z = X.
r.z();
590 bool getRobotCenterOfMass(rdl_msgs::GetRobotCenterOfMassRequest& req, rdl_msgs::GetRobotCenterOfMassResponse& resp)
598 resp.com.point.x = p_com.
x();
599 resp.com.point.y = p_com.
y();
600 resp.com.point.z = p_com.
z();
603 resp.com_vel.vector.x = v_com.x();
604 resp.com_vel.vector.y = v_com.y();
605 resp.com_vel.vector.z = v_com.z();
610 bool getBodyGravityWrench(rdl_msgs::GetBodyGravityWrenchRequest& req, rdl_msgs::GetBodyGravityWrenchResponse& resp)
612 unsigned int body_id =
model->GetBodyId(req.body.c_str());
614 if (body_id == std::numeric_limits<unsigned int>::max())
616 resp.report =
"No body found with name " + req.body;
617 resp.success =
false;
626 resp.wrench.wrench.torque.x = wrench.
mx();
627 resp.wrench.wrench.torque.y = wrench.
my();
628 resp.wrench.wrench.torque.z = wrench.
mz();
630 resp.wrench.wrench.force.x = wrench.
fx();
631 resp.wrench.wrench.force.y = wrench.
fy();
632 resp.wrench.wrench.force.z = wrench.
fz();
637 int main(
int argc,
char* argv[])
639 ros::init(argc, argv,
"rdl_kinematics_interface");
641 std::string joint_states_topic;
642 nh.
param<std::string>(
"joint_states_topic", joint_states_topic,
"joint_states");
644 std::string urdf_string;
645 if (!nh.
getParam(
"robot_description", urdf_string))
657 q = RobotDynamics::Math::VectorNd::Zero(
model->q_size);
658 qdot = RobotDynamics::Math::VectorNd::Zero(
model->qdot_size);
671 catch (
const std::out_of_range& e)
673 ROS_ERROR_STREAM(
"Error determining q_index for " << pair.first <<
": " << e.what());
EIGEN_STRONG_INLINE double & x()
RobotDynamics::Math::VectorNd qdot
bool changePoseFrame(rdl_msgs::ChangePoseFrameRequest &req, rdl_msgs::ChangePoseFrameResponse &resp)
RobotDynamics::ReferenceFramePtr getFrame(const std::string &name)
EIGEN_STRONG_INLINE double & w()
ReferenceFramePtr getReferenceFrame() const
bool changeWrenchArrayFrame(rdl_msgs::ChangeWrenchArrayFrameRequest &req, rdl_msgs::ChangeWrenchArrayFrameResponse &resp)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
std::shared_ptr< Model > ModelPtr
Math::SpatialMotion calcSpatialVelocity(Model &model, const Math::VectorNd &Q, const Math::VectorNd &QDot, ReferenceFramePtr body_frame, ReferenceFramePtr relative_body_frame, ReferenceFramePtr expressedInFrame=nullptr, const bool update_kinematics=true)
void robotStateCallback(const rdl_msgs::RobotState &msg)
EIGEN_STRONG_INLINE double & vz()
void transform(const SpatialTransform &X)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool change3DVectorArrayFrame(rdl_msgs::Change3DVectorArrayFrameRequest &req, rdl_msgs::Change3DVectorArrayFrameResponse &resp)
EIGEN_STRONG_INLINE double & fy()
static Quaternion toQuaternion(const Vector3d &axis, double angle_rad)
bool getRobotCenterOfMass(rdl_msgs::GetRobotCenterOfMassRequest &req, rdl_msgs::GetRobotCenterOfMassResponse &resp)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
RobotDynamics::ModelPtr model
EIGEN_STRONG_INLINE double & wx()
EIGEN_STRONG_INLINE double & wy()
std::map< std::string, unsigned int > joint_name_q_index
EIGEN_STRONG_INLINE double & fx()
bool changePointFrame(rdl_msgs::ChangePointFrameRequest &req, rdl_msgs::ChangePointFrameResponse &resp)
EIGEN_STRONG_INLINE double & vx()
bool changePointArrayFrame(rdl_msgs::ChangePointArrayFrameRequest &req, rdl_msgs::ChangePointArrayFrameResponse &resp)
EIGEN_STRONG_INLINE double & fz()
std::shared_ptr< ReferenceFrame > ReferenceFramePtr
EIGEN_STRONG_INLINE double & my()
bool hasEnding(std::string const &fullString, std::string const &ending)
virtual void changeFrame(ReferenceFramePtr desiredFrame)
EIGEN_STRONG_INLINE double & y()
bool changeWrenchFrame(rdl_msgs::ChangeWrenchFrameRequest &req, rdl_msgs::ChangeWrenchFrameResponse &resp)
int main(int argc, char *argv[])
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
bool getTwist(rdl_msgs::GetTwistRequest &req, rdl_msgs::GetTwistResponse &resp)
void transform(const RobotDynamics::Math::SpatialTransform &X)
void calcBodyGravityWrench(Model &model, unsigned int body_id, RobotDynamics::Math::SpatialForce &gravity_wrench)
const std::string & getNamespace() const
bool getTransform(rdl_msgs::GetTransformRequest &req, rdl_msgs::GetTransformResponse &resp)
EIGEN_STRONG_INLINE double & mz()
EIGEN_STRONG_INLINE double & wz()
bool change3DVectorFrame(rdl_msgs::Change3DVectorFrameRequest &req, rdl_msgs::Change3DVectorFrameResponse &resp)
bool changeTwistArrayFrame(rdl_msgs::ChangeTwistArrayFrameRequest &req, rdl_msgs::ChangeTwistArrayFrameResponse &resp)
bool parseJointBodyNameMapFromString(const char *model_xml_string, std::map< std::string, std::string > &jointBodyMap)
EIGEN_STRONG_INLINE double & vy()
void updateKinematicsCustom(Model &model, const Math::VectorNd *Q, const Math::VectorNd *QDot=nullptr, const Math::VectorNd *QDDot=nullptr)
EIGEN_STRONG_INLINE double & x()
void calcCenterOfMass(Model &model, const Math::VectorNd &q, Math::Vector3d &com, bool update_kinematics=true)
EIGEN_STRONG_INLINE double & y()
bool changeTwistFrame(rdl_msgs::ChangeTwistFrameRequest &req, rdl_msgs::ChangeTwistFrameResponse &resp)
bool getBodyGravityWrench(rdl_msgs::GetBodyGravityWrenchRequest &req, rdl_msgs::GetBodyGravityWrenchResponse &resp)
bool getParam(const std::string &key, std::string &s) const
EIGEN_STRONG_INLINE double & mx()
RobotDynamics::Math::VectorNd q
EIGEN_STRONG_INLINE double & z()
#define ROS_ERROR_STREAM(args)
bool changeOrientationFrame(rdl_msgs::ChangeOrientationFrameRequest &req, rdl_msgs::ChangeOrientationFrameResponse &resp)
EIGEN_STRONG_INLINE double & z()
std::map< std::string, std::string > joint_to_body_name_map
bool urdfReadFromString(const char *model_xml_string, ModelPtr model, bool floating_base, bool verbose=false)