5 #include <urdf_model/model.h> 6 #include <urdf_parser/urdf_parser.h> 28 urdf::LinkSharedPtr urdf_root_link;
31 link_map = urdf_model->links_;
33 URDFJointMap joint_map;
34 joint_map = urdf_model->joints_;
36 vector<string> joint_names;
38 stack<urdf::LinkSharedPtr> link_stack;
39 stack<int> joint_index_stack;
42 link_stack.push(link_map[(urdf_model->getRoot()->name)]);
45 urdf::LinkConstSharedPtr root = urdf_model->getRoot();
53 double root_inertial_mass = root->inertial->mass;
55 root_inertial_position.
set(root->inertial->origin.position.x, root->inertial->origin.position.y, root->inertial->origin.position.z);
57 root->inertial->origin.rotation.getRPY(root_inertial_rpy[0], root_inertial_rpy[1], root_inertial_rpy[2]);
60 root_inertia_in_com_frame(0, 0) = root->inertial->ixx;
61 root_inertia_in_com_frame(0, 1) = root->inertial->ixy;
62 root_inertia_in_com_frame(0, 2) = root->inertial->ixz;
63 root_inertia_in_com_frame(1, 0) = root->inertial->ixy;
64 root_inertia_in_com_frame(1, 1) = root->inertial->iyy;
65 root_inertia_in_com_frame(1, 2) = root->inertial->iyz;
66 root_inertia_in_com_frame(2, 0) = root->inertial->ixz;
67 root_inertia_in_com_frame(2, 1) = root->inertial->iyz;
68 root_inertia_in_com_frame(2, 2) = root->inertial->izz;
71 root_inertial_inertia =
72 root_inertial_rpy ==
Vector3d(0., 0., 0.) ? root_inertia_in_com_frame : X_body_to_inertial.
E.transpose() * root_inertia_in_com_frame * X_body_to_inertial.
E;
73 Body root_link =
Body(root_inertial_mass, root_inertial_position, root_inertial_inertia);
85 cout <<
"+ Adding Root Body " << endl;
86 cout <<
" joint frame: " << root_joint_frame << endl;
89 cout <<
" joint type : floating" << endl;
93 cout <<
" joint type : fixed" << endl;
95 cout <<
" body inertia: " << endl << root_link.
mInertia << endl;
96 cout <<
" body mass : " << root_link.
mMass << endl;
97 cout <<
" body name : " << root->name << endl;
100 rdl_model->appendBody(root_joint_frame, root_joint, root_link, root->name);
103 if (link_stack.top()->child_joints.size() > 0)
105 joint_index_stack.push(0);
109 while (!link_stack.empty())
111 urdf::LinkSharedPtr cur_link = link_stack.top();
112 unsigned int joint_idx;
113 if (joint_index_stack.size() > 0)
115 joint_idx = joint_index_stack.top();
122 if (joint_idx < cur_link->child_joints.size())
124 urdf::JointSharedPtr cur_joint = cur_link->child_joints[joint_idx];
127 joint_index_stack.pop();
128 joint_index_stack.push(joint_idx + 1);
130 link_stack.push(link_map[cur_joint->child_link_name]);
131 joint_index_stack.push(0);
135 for (
unsigned int i = 1; i < joint_index_stack.size() - 1; i++)
139 cout <<
"joint '" << cur_joint->name <<
"' child link '" << link_stack.top()->name <<
"' type = " << cur_joint->type << endl;
142 joint_names.push_back(cur_joint->name);
147 joint_index_stack.pop();
152 for (j = 0; j < joint_names.size(); j++)
154 urdf::JointSharedPtr urdf_joint = joint_map[joint_names[j]];
155 urdf::LinkSharedPtr urdf_parent = link_map[urdf_joint->parent_link_name];
156 urdf::LinkSharedPtr urdf_child = link_map[urdf_joint->child_link_name];
159 unsigned int rdl_parent_id = 0;
160 if (rdl_model->mBodies.size() != 1 || rdl_model->mFixedBodies.size() != 0)
162 rdl_parent_id = rdl_model->GetBodyId(urdf_parent->name.c_str());
165 if (rdl_parent_id == std::numeric_limits<unsigned int>::max())
167 cerr <<
"Error while processing joint '" << urdf_joint->name <<
"': parent link '" << urdf_parent->name <<
"' could not be found." << endl;
172 if (urdf_joint->type == urdf::Joint::REVOLUTE || urdf_joint->type == urdf::Joint::CONTINUOUS)
174 rdl_joint =
Joint(
SpatialVector(urdf_joint->axis.x, urdf_joint->axis.y, urdf_joint->axis.z, 0., 0., 0.));
176 else if (urdf_joint->type == urdf::Joint::PRISMATIC)
178 rdl_joint =
Joint(
SpatialVector(0., 0., 0., urdf_joint->axis.x, urdf_joint->axis.y, urdf_joint->axis.z));
180 else if (urdf_joint->type == urdf::Joint::FIXED)
184 else if (urdf_joint->type == urdf::Joint::FLOATING)
187 rdl_joint =
Joint(
SpatialVector(0., 0., 0., 1., 0., 0.),
SpatialVector(0., 0., 0., 0., 1., 0.),
SpatialVector(0., 0., 0., 0., 0., 1.),
188 SpatialVector(1., 0., 0., 0., 0., 0.),
SpatialVector(0., 1., 0., 0., 0., 0.),
SpatialVector(0., 0., 1., 0., 0., 0.));
190 else if (urdf_joint->type == urdf::Joint::PLANAR)
194 cerr <<
"Error while processing joint '" << urdf_joint->name <<
"': planar joints not yet supported!" << endl;
201 urdf_joint->parent_to_joint_origin_transform.rotation.getRPY(joint_rpy[0], joint_rpy[1], joint_rpy[2]);
202 joint_translation.
set(urdf_joint->parent_to_joint_origin_transform.position.x, urdf_joint->parent_to_joint_origin_transform.position.y,
203 urdf_joint->parent_to_joint_origin_transform.position.z);
210 Matrix3d link_inertial_inertia = Matrix3d::Zero();
211 double link_inertial_mass = 0.;
214 if (urdf_child->inertial)
216 link_inertial_mass = urdf_child->inertial->mass;
218 link_inertial_position.
set(urdf_child->inertial->origin.position.x, urdf_child->inertial->origin.position.y, urdf_child->inertial->origin.position.z);
219 urdf_child->inertial->origin.rotation.getRPY(link_inertial_rpy[0], link_inertial_rpy[1], link_inertial_rpy[2]);
222 link_inertia_in_com_frame(0, 0) = urdf_child->inertial->ixx;
223 link_inertia_in_com_frame(0, 1) = urdf_child->inertial->ixy;
224 link_inertia_in_com_frame(0, 2) = urdf_child->inertial->ixz;
225 link_inertia_in_com_frame(1, 0) = urdf_child->inertial->ixy;
226 link_inertia_in_com_frame(1, 1) = urdf_child->inertial->iyy;
227 link_inertia_in_com_frame(1, 2) = urdf_child->inertial->iyz;
228 link_inertia_in_com_frame(2, 0) = urdf_child->inertial->ixz;
229 link_inertia_in_com_frame(2, 1) = urdf_child->inertial->iyz;
230 link_inertia_in_com_frame(2, 2) = urdf_child->inertial->izz;
233 link_inertial_inertia = link_inertial_rpy ==
Vector3d(0., 0., 0.) ? link_inertia_in_com_frame :
234 X_body_to_inertial.
E.transpose() * link_inertia_in_com_frame * X_body_to_inertial.
E;
237 Body rdl_body =
Body(link_inertial_mass, link_inertial_position, link_inertial_inertia);
241 cout <<
"+ Adding Body " << endl;
242 cout <<
" parent_id : " << rdl_parent_id << endl;
243 cout <<
" joint frame: " << rdl_joint_frame << endl;
244 cout <<
" joint dofs : " << rdl_joint.
mDoFCount << endl;
245 for (
unsigned int j = 0; j < rdl_joint.
mDoFCount; j++)
247 cout <<
" " << j <<
": " << rdl_joint.
mJointAxes[j].transpose() << endl;
249 cout <<
" body inertia: " << endl << rdl_body.
mInertia << endl;
250 cout <<
" body mass : " << rdl_body.
mMass << endl;
251 cout <<
" body name : " << urdf_child->name << endl;
254 if (urdf_joint->type == urdf::Joint::FLOATING)
256 Matrix3d zero_matrix = Matrix3d::Zero();
257 Body null_body(0., Vector3d::Zero(3), zero_matrix);
259 string trans_body_name = urdf_child->name +
"_Translate";
260 rdl_model->addBody(rdl_parent_id, rdl_joint_frame, joint_txtytz, null_body, trans_body_name);
263 rdl_model->appendBody(
SpatialTransform(), joint_euler_zyx, rdl_body, urdf_child->name);
267 rdl_model->addBody(rdl_parent_id, rdl_joint_frame, rdl_joint, rdl_body, urdf_child->name);
276 ifstream model_file(filename);
279 cerr <<
"Error opening file '" << filename <<
"'." << endl;
284 std::string model_xml_string;
285 model_file.seekg(0, std::ios::end);
286 model_xml_string.reserve(model_file.tellg());
287 model_file.seekg(0, std::ios::beg);
288 model_xml_string.assign((std::istreambuf_iterator<char>(model_file)), std::istreambuf_iterator<char>());
300 if (!doc.Parse(model_xml_string) && doc.Error())
302 std::cerr <<
"Can't parse urdf. Xml is invalid" << std::endl;
307 TiXmlElement* root = doc.RootElement();
309 for (TiXmlElement* joint = root->FirstChildElement(
"joint"); joint; joint = joint->NextSiblingElement(
"joint"))
311 if (!std::strcmp(joint->Attribute(
"type"),
"fixed") || !std::strcmp(joint->Attribute(
"type"),
"floating"))
316 joint_names.push_back(joint->Attribute(
"name"));
317 body_names.push_back(joint->FirstChildElement(
"child")->Attribute(
"link"));
330 std::vector<std::string> joint_names, body_names;
336 jointBodyMap.clear();
337 for (
unsigned int i = 0; i < joint_names.size(); i++)
339 jointBodyMap.insert(std::pair<std::string, std::string>(joint_names[i], body_names[i]));
347 std::vector<std::string> joint_names, body_names;
353 jointBodyMap.clear();
354 for (
unsigned int i = 0; i < joint_names.size(); i++)
356 jointBodyMap.insert(std::pair<std::string, std::string>(joint_names[i], body_names[i]));
365 for (
unsigned int i = 0; i < body_names.size(); i++)
367 q_indices.push_back(model.
mJoints[model.
GetBodyId(body_names[i].c_str())].q_index);
381 std::vector<std::string> joint_names, body_names;
392 ifstream model_file(filename);
395 cerr <<
"Error opening file '" << filename <<
"'." << endl;
400 string model_xml_string;
401 model_file.seekg(0, std::ios::end);
402 model_xml_string.reserve(model_file.tellg());
403 model_file.seekg(0, std::ios::beg);
404 model_xml_string.assign((std::istreambuf_iterator<char>(model_file)), std::istreambuf_iterator<char>());
415 urdf::ModelInterfaceSharedPtr urdf_model = urdf::parseURDF(model_xml_string);
419 cerr <<
"Error constructing model from urdf file." << endl;
423 model->gravity.SpatialVector::set(0., 0., 0., 0., 0., -9.81);
430 ifstream model_file(filename.c_str());
433 cerr <<
"Error opening file '" << filename <<
"'." << endl;
438 std::string model_xml_string;
439 model_file.seekg(0, std::ios::end);
440 model_xml_string.reserve(model_file.tellg());
441 model_file.seekg(0, std::ios::beg);
442 model_xml_string.assign((std::istreambuf_iterator<char>(model_file)), std::istreambuf_iterator<char>());
453 urdf::ModelInterfaceSharedPtr urdf_model = urdf::parseURDF(model_xml_string.c_str());
455 bool floating_base = std::strcmp(urdf_model->getRoot()->name.c_str(),
"world") ?
true :
false;
459 cerr <<
"Error constructing model from urdf file." << endl;
463 model->gravity.SpatialVector::set(0., 0., 0., 0., 0., -9.81);
std::vector< Joint > mJoints
map< string, urdf::LinkSharedPtr > URDFLinkMap
std::shared_ptr< Model > ModelPtr
SpatialTransform Xtrans(const Vector3d &r)
bool parseJointBodyNameMapFromFile(const char *filename, std::map< std::string, std::string > &jointBodyMap)
This will build a map of joint name to body name.
vector< urdf::LinkSharedPtr > URDFLinkVector
vector< urdf::JointSharedPtr > URDFJointVector
bool parseJointAndBodyNamesFromString(const std::string &model_xml_string, std::vector< std::string > &joint_names, std::vector< std::string > &body_names)
This will build vectors of joint name and body name pairs.
map< string, urdf::JointSharedPtr > URDFJointMap
unsigned int GetBodyId(const char *body_name) const
Matrix3d Matrix3dZero(0., 0., 0., 0., 0., 0., 0., 0., 0.)
bool construct_model(ModelPtr rdl_model, urdf::ModelInterfaceSharedPtr urdf_model, bool floating_base, bool verbose)
bool parseJointBodyNameMapFromString(const char *model_xml_string, std::map< std::string, std::string > &jointBodyMap)
This will build a map of joint name to body name.
Math::SpatialVector * mJointAxes
void set(const Eigen::Vector3d &v)
bool urdfReadFromFile(const char *filename, ModelPtr model, bool floating_base, bool verbose=false)
Read urdf from file path.
SpatialTransform XeulerXYZ(double roll, double pitch, double yaw)
bool urdfReadFromString(const char *model_xml_string, ModelPtr model, bool floating_base, bool verbose=false)
Read urdf from string contents.
SpatialTransform Xrot(double angle_rad, const Vector3d &axis)
bool parseJointAndQIndex(const RobotDynamics::Model &model, const std::vector< std::string > &body_names, std::vector< unsigned int > &q_indices)
This will build a vector of joint indices in the same order as the list of joints.