89 using tinyxml2::XMLElement;
91 tinyxml2::XMLDocument doc;
92 std::string urdf_string =
urdf_config_->getURDFContents();
93 doc.Parse(urdf_string.c_str());
94 auto root = doc.RootElement();
97 std::string orig_urdf =
printXML(doc);
100 std::map<std::string, XMLElement*> transmission_elements;
101 for (XMLElement* element = root->FirstChildElement(
"transmission"); element !=
nullptr;
102 element = element->NextSiblingElement(element->Value()))
104 auto type_tag = element->FirstChildElement(
"type");
105 auto joint_tag = element->FirstChildElement(
"joint");
106 if (!type_tag || !type_tag->GetText() || !joint_tag || !joint_tag->Attribute(
"name"))
108 if (std::string(type_tag->GetText()) ==
"transmission_interface/SimpleTransmission")
109 transmission_elements[element->FirstChildElement(
"joint")->Attribute(
"name")] = element;
113 for (XMLElement* element = root->FirstChildElement(); element !=
nullptr; element = element->NextSiblingElement())
115 const std::string tag_name(element->Value());
116 if (tag_name ==
"link" && element->FirstChildElement(
"collision"))
118 XMLElement* inertial =
uniqueInsert(doc, *element,
"inertial");
119 uniqueInsert(doc, *inertial,
"mass", { {
"value",
"0.1" } });
120 uniqueInsert(doc, *inertial,
"origin", { {
"xyz",
"0 0 0" }, {
"rpy",
"0 0 0" } });
129 else if (tag_name ==
"joint")
131 const char* joint_type = element->Attribute(
"type");
132 const char* joint_name = element->Attribute(
"name");
133 if (!joint_type || !joint_name || strcmp(joint_type,
"fixed") == 0)
137 XMLElement* transmission;
138 auto it = transmission_elements.find(joint_name);
139 if (it != transmission_elements.end())
140 transmission = it->second;
143 transmission = doc.NewElement(
"transmission");
144 root->InsertEndChild(transmission);
145 transmission->SetAttribute(
"name", (std::string(
"trans_") + joint_name).c_str());
148 uniqueInsert(doc, *transmission,
"type", {},
"transmission_interface/SimpleTransmission");
151 auto* joint =
uniqueInsert(doc, *transmission,
"joint", { {
"name", joint_name } });
152 uniqueInsert(doc, *joint,
"hardwareInterface", {}, hw_interface.c_str());
154 auto actuator_name = joint_name + std::string(
"_motor");
155 auto* actuator =
uniqueInsert(doc, *transmission,
"actuator", { {
"name", actuator_name.c_str() } });
156 uniqueInsert(doc, *actuator,
"hardwareInterface", {}, hw_interface.c_str());
157 uniqueInsert(doc, *actuator,
"mechanicalReduction", {},
"1");
162 XMLElement* gazebo =
uniqueInsert(doc, *root,
"gazebo");
163 XMLElement* plugin =
uniqueInsert(doc, *gazebo,
"plugin", { {
"name",
"gazebo_ros_control",
true } });
167 std::string new_urdf =
printXML(doc);
169 return orig_urdf == new_urdf ? std::string() : new_urdf;
192 tinyxml2::XMLDocument doc;
193 doc.Parse(new_urdf_contents.c_str());
196 error_row = doc.ErrorLineNum();
197 error_description = doc.ErrorStr();
tinyxml2::XMLElement * uniqueInsert(tinyxml2::XMLDocument &doc, tinyxml2::XMLElement &element, const char *tag, const std::vector< XMLAttribute > &attributes={}, const char *text=nullptr)
Insert a new XML element with a given tag, attributes, and text.