|
16 | 16 |
|
17 | 17 | #include "dynamixel_hardware_interface/dynamixel_hardware_interface.hpp" |
18 | 18 |
|
| 19 | +#include <tinyxml2.h> |
| 20 | + |
19 | 21 | #include <chrono> |
20 | 22 | #include <cmath> |
21 | 23 | #include <limits> |
@@ -215,6 +217,43 @@ hardware_interface::CallbackReturn DynamixelHardware::on_init( |
215 | 217 | } |
216 | 218 | } |
217 | 219 |
|
| 220 | + auto urdf = info_.original_xml; |
| 221 | + tinyxml2::XMLDocument doc; |
| 222 | + if (doc.Parse(urdf.c_str()) != tinyxml2::XML_SUCCESS) { |
| 223 | + RCLCPP_ERROR(logger_, "Failed to parse URDF XML"); |
| 224 | + return hardware_interface::CallbackReturn::ERROR; |
| 225 | + } |
| 226 | + const auto * joint_element = doc.RootElement()->FirstChildElement("joint"); |
| 227 | + while (joint_element != nullptr) { |
| 228 | + const auto * name_attr = joint_element->FindAttribute("name"); |
| 229 | + const auto * calibration_element = joint_element->FirstChildElement("calibration"); |
| 230 | + if (calibration_element != nullptr) { |
| 231 | + const auto * rising_attr = calibration_element->FindAttribute("rising"); |
| 232 | + if ((rising_attr != nullptr) && (name_attr != nullptr)) { |
| 233 | + auto rising = std::atof(calibration_element->Attribute("rising")); |
| 234 | + std::string name = joint_element->Attribute("name"); |
| 235 | + auto itr = std::find_if( |
| 236 | + info_.joints.begin(), info_.joints.end(), |
| 237 | + [&name](const hardware_interface::ComponentInfo & joint) { |
| 238 | + return joint.name == name; |
| 239 | + }); |
| 240 | + if (itr != info_.gpios.end()) { |
| 241 | + auto params = info_.gpios[std::distance(info_.joints.begin(), itr)].parameters; |
| 242 | + if (std::find_if(params.begin(), params.end(), |
| 243 | + [](const std::pair<std::string, std::string> & p) { |
| 244 | + return p.first == "Homing Offset"; |
| 245 | + }) == params.end()) |
| 246 | + { |
| 247 | + info_.gpios[std::distance(info_.joints.begin(), itr)].parameters.emplace( |
| 248 | + "Homing Offset", |
| 249 | + std::to_string(std::round(rising * (180.0 / M_PI) / (360.0 / 4095)))); |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + } |
| 254 | + joint_element = joint_element->NextSiblingElement("joint"); |
| 255 | + } |
| 256 | + |
218 | 257 | torque_enabled_comm_id_id_.clear(); |
219 | 258 | for (const hardware_interface::ComponentInfo & gpio : info_.gpios) { |
220 | 259 | uint8_t id = static_cast<uint8_t>(stoi(gpio.parameters.at("ID"))); |
|
0 commit comments