15 #ifdef BOOST_MPL_LIMIT_VECTOR_SIZE
16 #pragma push_macro("BOOST_MPL_LIMIT_VECTOR_SIZE")
17 #undef BOOST_MPL_LIMIT_VECTOR_SIZE
18 #include <boost/property_tree/ptree.hpp>
19 #include <boost/property_tree/xml_parser.hpp>
20 #pragma pop_macro("BOOST_MPL_LIMIT_VECTOR_SIZE")
22 #include <boost/property_tree/ptree.hpp>
23 #include <boost/property_tree/xml_parser.hpp>
26 #include <dynamic-graph/factory.h>
35 namespace dg = ::dynamicgraph;
36 namespace pt = boost::property_tree;
38 using namespace dg::command;
45 return std::shared_ptr<RobotUtil>(
nullptr);
50 std::istringstream iss(robot_model);
52 boost::property_tree::read_xml(iss, tree_);
63 current_joint_name_ =
"";
64 go_through(tree_, 0, 0);
73 if (stage == 3) current_joint_name_ = pt.data();
80 if (
pos.first ==
"joint")
83 else if (
pos.first ==
"<xmlattr>") {
86 if (stage == 1) new_stage = 2;
90 else if (
pos.first ==
"name") {
91 if (stage == 2) new_stage = 3;
95 else if (
pos.first ==
"mimic") {
99 mimic_joints_.push_back(current_joint_name_);
104 go_through(
pos.second, level + 1, new_stage);
110 os <<
"Lower limits:" << std::endl;
111 os << lower << std::endl;
112 os <<
"Upper Limits:" << std::endl;
113 os << upper << std::endl;
119 os <<
"Right Foot Sole XYZ " << std::endl;
120 os << m_Right_Foot_Sole_XYZ << std::endl;
121 os <<
"Left Foot Frame Name:" << m_Left_Foot_Frame_Name << std::endl;
122 os <<
"Right Foot Frame Name:" << m_Right_Foot_Frame_Name << std::endl;
128 os <<
"Left Hand Frame Name:" << m_Left_Hand_Frame_Name << std::endl;
129 os <<
"Right Hand Frame Name:" << m_Right_Hand_Frame_Name << std::endl;
135 const Index &force_id) {
136 m_name_to_force_id[
name] = (
Index)force_id;
137 create_force_id_to_name_map();
139 set_force_id_right_foot(m_name_to_force_id[
name]);
140 else if (
name ==
"lf")
141 set_force_id_left_foot(m_name_to_force_id[
name]);
142 else if (
name ==
"lh")
143 set_force_id_left_hand(m_name_to_force_id[
name]);
144 else if (
name ==
"rh")
145 set_force_id_right_hand(m_name_to_force_id[
name]);
151 m_force_id_to_limits[(
Index)force_id].lower = lf;
152 m_force_id_to_limits[(
Index)force_id].upper = uf;
156 std::map<std::string, Index>::const_iterator it;
157 it = m_name_to_force_id.find(
name);
158 if (it != m_name_to_force_id.end())
return it->second;
166 std::map<Index, std::string>::const_iterator it;
167 it = m_force_id_to_name.find(idx);
168 if (it != m_force_id_to_name.end())
return it->second;
173 const std::string &default_rtn = get_name_from_id(idx);
177 std::map<std::string, Index>::const_iterator it;
178 for (it = m_name_to_force_id.begin(); it != m_name_to_force_id.end(); it++)
179 m_force_id_to_name[it->second] = it->first;
183 std::map<Index, ForceLimits>::const_iterator iter =
184 m_force_id_to_limits.find(force_id);
185 if (iter == m_force_id_to_limits.end())
191 std::map<Index, ForceLimits>::const_iterator iter =
192 m_force_id_to_limits.find(force_id);
193 if (iter == m_force_id_to_limits.end())
199 os <<
"Force Id to limits " << std::endl;
200 for (std::map<Index, ForceLimits>::const_iterator it =
201 m_force_id_to_limits.begin();
202 it != m_force_id_to_limits.end(); ++it) {
203 it->second.display(os);
206 os <<
"Name to force id:" << std::endl;
207 for (std::map<std::string, Index>::const_iterator it =
208 m_name_to_force_id.begin();
209 it != m_name_to_force_id.end(); ++it) {
210 os <<
"(" << it->first <<
"," << it->second <<
") ";
214 os <<
"Force id to Name:" << std::endl;
215 for (std::map<Index, std::string>::const_iterator it =
216 m_force_id_to_name.begin();
217 it != m_force_id_to_name.end(); ++it) {
218 os <<
"(" << it->first <<
"," << it->second <<
") ";
222 os <<
"Index for force sensors:" << std::endl;
223 os <<
"Left Hand (" << m_Force_Id_Left_Hand <<
") ,"
224 <<
"Right Hand (" << m_Force_Id_Right_Hand <<
") ,"
225 <<
"Left Foot (" << m_Force_Id_Left_Foot <<
") ,"
226 <<
"Right Foot (" << m_Force_Id_Right_Foot <<
") " << std::endl;
238 std::map<Index, JointLimits>::const_iterator iter = m_limits_map.find(
id);
243 const JointLimits &rtn = get_joint_limits_from_id(
id);
248 const Index &jointId) {
249 m_name_to_id[jointName] = (
Index)jointId;
250 create_id_to_name_map();
254 std::map<std::string, Index>::const_iterator it;
255 for (it = m_name_to_id.begin(); it != m_name_to_id.end(); it++)
256 m_id_to_name[it->second] = it->first;
260 std::map<std::string, Index>::const_iterator it = m_name_to_id.find(
name);
261 if (it == m_name_to_id.end())
return VoidIndex;
266 std::map<Index, std::string>::const_iterator iter = m_id_to_name.find(
id);
272 m_nbJoints = urdf_to_sot.size();
273 m_urdf_to_sot.resize(urdf_to_sot.size());
274 m_dgv_urdf_to_sot.resize(urdf_to_sot.size());
275 for (std::size_t idx = 0; idx < urdf_to_sot.size(); idx++) {
276 m_urdf_to_sot[(
Index)idx] = urdf_to_sot[(
Index)idx];
277 m_dgv_urdf_to_sot[(
Index)idx] =
278 static_cast<double>(urdf_to_sot[(
Index)idx]);
283 m_nbJoints = urdf_to_sot.size();
284 m_urdf_to_sot.resize(urdf_to_sot.size());
285 for (std::size_t idx = 0; idx < (std::size_t)urdf_to_sot.size(); idx++) {
286 m_urdf_to_sot[idx] = (std::size_t)urdf_to_sot[idx];
288 m_dgv_urdf_to_sot = urdf_to_sot;
292 if (m_nbJoints == 0) {
299 for (std::size_t idx = 0; idx < m_nbJoints; idx++)
300 q_sot[m_urdf_to_sot[idx]] = q_urdf[idx];
308 if (m_nbJoints == 0) {
313 for (std::size_t idx = 0; idx < m_nbJoints; idx++)
314 q_urdf[idx] = q_sot[m_urdf_to_sot[idx]];
324 if (m_nbJoints == 0) {
328 const Eigen::Quaterniond
q(q_urdf(6), q_urdf(3), q_urdf(4), q_urdf(5));
329 Eigen::Matrix3d oRb =
q.toRotationMatrix();
330 v_sot.head<3>() = oRb * v_urdf.head<3>();
331 v_sot.segment<3>(3) = oRb * v_urdf.segment<3>(3);
333 joints_urdf_to_sot(v_urdf.tail(m_nbJoints), v_sot.tail(m_nbJoints));
343 if (m_nbJoints == 0) {
348 const Eigen::Quaterniond
q(q_urdf(6), q_urdf(3), q_urdf(4), q_urdf(5));
349 Eigen::Matrix3d oRb =
q.toRotationMatrix();
350 v_urdf.head<3>() = oRb.transpose() * v_sot.head<3>();
351 v_urdf.segment<3>(3) = oRb.transpose() * v_sot.segment<3>(3);
353 joints_sot_to_urdf(v_sot.tail(m_nbJoints), v_urdf.tail(m_nbJoints));
358 assert(q_urdf.size() == 7);
359 assert(q_sot.size() == 6);
362 const double W = q_urdf[6];
363 const double X = q_urdf[3];
364 const double Y = q_urdf[4];
365 const double Z = q_urdf[5];
366 const Eigen::Matrix3d
R = Eigen::Quaterniond(W,
X,
Y,
Z).toRotationMatrix();
371 assert(q_urdf.size() == 7);
372 assert(q_sot.size() == 6);
374 const double r = q_sot[3];
375 const double p = q_sot[4];
376 const double y = q_sot[5];
377 const Eigen::AngleAxisd rollAngle(
r, Eigen::Vector3d::UnitX());
378 const Eigen::AngleAxisd pitchAngle(
p, Eigen::Vector3d::UnitY());
379 const Eigen::AngleAxisd yawAngle(
y, Eigen::Vector3d::UnitZ());
380 const Eigen::Quaternion<double>
quat = yawAngle * pitchAngle * rollAngle;
382 q_urdf[0] = q_sot[0];
383 q_urdf[1] = q_sot[1];
384 q_urdf[2] = q_sot[2];
385 q_urdf[3] =
quat.x();
386 q_urdf[4] =
quat.y();
387 q_urdf[5] =
quat.z();
388 q_urdf[6] =
quat.w();
398 joints_urdf_to_sot(q_urdf.tail(m_nbJoints), q_sot.tail(m_nbJoints));
407 joints_sot_to_urdf(q_sot.tail(m_nbJoints), q_urdf.tail(m_nbJoints));
411 m_force_util.display(os);
412 m_foot_util.display(os);
413 m_hand_util.display(os);
414 os <<
"Nb of joints: " << m_nbJoints << std::endl;
415 os <<
"Urdf file name: " << m_urdf_filename << std::endl;
418 os <<
"Map from urdf index to the Sot Index " << std::endl;
419 for (std::size_t
i = 0;
i < m_urdf_to_sot.size();
i++)
420 os <<
"(" <<
i <<
" : " << m_urdf_to_sot[
i] <<
") ";
423 os <<
"Joint name to joint id:" << std::endl;
424 for (std::map<std::string, Index>::const_iterator it = m_name_to_id.begin();
425 it != m_name_to_id.end(); ++it) {
426 os <<
"(" << it->first <<
"," << it->second <<
") ";
430 os <<
"Joint id to joint Name:" << std::endl;
431 for (std::map<Index, std::string>::const_iterator it = m_id_to_name.begin();
432 it != m_id_to_name.end(); ++it) {
433 os <<
"(" << it->first <<
"," << it->second <<
") ";
437 boost::property_tree::write_xml(os, property_tree_);
441 const std::string &lineId) {
442 logger_.stream(
t, lineId) <<
"[RobotUtil]" << msg <<
'\n';
446 assert(q_sot.size() == 6);
447 assert(
pos.size() == 3);
448 assert(
R.rows() == 3);
449 assert(
R.cols() == 3);
452 m = sqrt(
R(2, 1) *
R(2, 1) +
R(2, 2) *
R(2, 2));
453 p = atan2(-
R(2, 0),
m);
454 if (fabs(fabs(
p) -
M_PI / 2) < 0.001) {
456 y = -atan2(
R(0, 1),
R(1, 1));
458 y = atan2(
R(1, 0),
R(0, 0));
459 r = atan2(
R(2, 1),
R(2, 2));
472 assert(q_urdf.size() == 7);
473 assert(q_sot.size() == 6);
475 const double W = q_urdf[6];
476 const double X = q_urdf[3];
477 const double Y = q_urdf[4];
478 const double Z = q_urdf[5];
479 const Eigen::Matrix3d
R = Eigen::Quaterniond(W,
X,
Y,
Z).toRotationMatrix();
484 assert(q_urdf.size() == 7);
485 assert(q_sot.size() == 6);
487 const double r = q_sot[3];
488 const double p = q_sot[4];
489 const double y = q_sot[5];
490 const Eigen::AngleAxisd rollAngle(
r, Eigen::Vector3d::UnitX());
491 const Eigen::AngleAxisd pitchAngle(
p, Eigen::Vector3d::UnitY());
492 const Eigen::AngleAxisd yawAngle(
y, Eigen::Vector3d::UnitZ());
493 const Eigen::Quaternion<double>
quat = yawAngle * pitchAngle * rollAngle;
495 q_urdf[0] = q_sot[0];
496 q_urdf[1] = q_sot[1];
497 q_urdf[2] = q_sot[2];
498 q_urdf[3] =
quat.x();
499 q_urdf[4] =
quat.y();
500 q_urdf[5] =
quat.z();
501 q_urdf[6] =
quat.w();
509 std::shared_ptr<std::vector<std::string> >
res =
510 std::make_shared<std::vector<std::string> >();
512 std::map<std::string, RobotUtilShrPtr>::iterator it =
515 res->push_back(it->first);
523 std::map<std::string, RobotUtilShrPtr>::iterator it =
530 std::map<std::string, RobotUtilShrPtr>::iterator it =
537 std::map<std::string, RobotUtilShrPtr>::iterator it =