Skip to content

Commit bce8d90

Browse files
Karsten1987bmagyar
andauthored
adapt component parser to new xml schema (#209)
* adapt component parser to new xml schema * Update hardware_interface/src/component_parser.cpp * Fix build & touch up docs Co-authored-by: Bence Magyar <[email protected]>
1 parent 3d542bb commit bce8d90

File tree

3 files changed

+222
-284
lines changed

3 files changed

+222
-284
lines changed

hardware_interface/include/hardware_interface/component_info.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,9 @@ struct ComponentInfo
5454
*/
5555
std::string name;
5656
/**
57-
* \brief type of the component: sensor or actuator.
57+
* \brief type of the component: sensor or joint.
5858
*/
5959
std::string type;
60-
/**
61-
* \brief component's class, which will be dynamically loaded.
62-
*/
63-
std::string class_type;
6460
/**
6561
* \brief name of the command interfaces that can be set, e.g. "position", "velocity", etc.
6662
* Used by joints.

hardware_interface/src/component_parser.cpp

Lines changed: 43 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ namespace
2727
constexpr const auto kRobotTag = "robot";
2828
constexpr const auto kROS2ControlTag = "ros2_control";
2929
constexpr const auto kHardwareTag = "hardware";
30-
constexpr const auto kClassTypeTag = "classType";
30+
constexpr const auto kClassTypeTag = "plugin";
3131
constexpr const auto kParamTag = "param";
3232
constexpr const auto kJointTag = "joint";
3333
constexpr const auto kSensorTag = "sensor";
3434
constexpr const auto kTransmissionTag = "transmission";
35-
constexpr const auto kCommandInterfaceTypeTag = "commandInterfaceType";
36-
constexpr const auto kStateInterfaceTypeTag = "stateInterfaceType";
35+
constexpr const auto kCommandInterfaceTag = "command_interface";
36+
constexpr const auto kStateInterfaceTag = "state_interface";
3737
constexpr const auto kMinTag = "min";
3838
constexpr const auto kMaxTag = "max";
39+
constexpr const auto kNameAttribute = "name";
40+
constexpr const auto kTypeAttribute = "type";
3941
} // namespace
4042

4143
namespace hardware_interface
@@ -118,11 +120,11 @@ std::unordered_map<std::string, std::string> parse_parameters_from_xml(
118120

119121
while (params_it) {
120122
// Fill the map with parameters
121-
attr = params_it->FindAttribute("name");
123+
attr = params_it->FindAttribute(kNameAttribute);
122124
if (!attr) {
123125
throw std::runtime_error("no parameter name attribute set in param tag");
124126
}
125-
const std::string parameter_name = params_it->Attribute("name");
127+
const std::string parameter_name = params_it->Attribute(kNameAttribute);
126128
const std::string parameter_value = get_text_for_element(params_it, parameter_name);
127129
parameters[parameter_name] = parameter_value;
128130

@@ -139,54 +141,36 @@ std::unordered_map<std::string, std::string> parse_parameters_from_xml(
139141
* \return std::vector< std::__cxx11::string > list of interface types
140142
* \throws std::runtime_error if the interfaceType text not set in a tag
141143
*/
142-
std::vector<InterfaceInfo> parse_interfaces_from_xml(
143-
const tinyxml2::XMLElement * interfaces_it, const char * interfaceTag)
144+
hardware_interface::InterfaceInfo parse_interfaces_from_xml(
145+
const tinyxml2::XMLElement * interfaces_it)
144146
{
145-
std::vector<InterfaceInfo> interfaces;
146-
147-
while (interfaces_it) {
148-
hardware_interface::InterfaceInfo interface;
149-
150-
// Joint interfaces have a name attribute
151-
if (std::string(interfaceTag) == "commandInterfaceType") {
152-
const std::string interface_name = get_attribute_value(
153-
interfaces_it, "name",
154-
std::string(interfaceTag));
155-
interface.name = interface_name;
156-
157-
// Optional min/max attributes
158-
std::unordered_map<std::string, std::string> interface_params =
159-
parse_parameters_from_xml(interfaces_it->FirstChildElement(kParamTag));
160-
std::unordered_map<std::string, std::string>::const_iterator interface_param =
161-
interface_params.find(kMinTag);
162-
if (interface_param != interface_params.end()) {
163-
interface.min = interface_param->second;
164-
}
165-
interface_param = interface_params.find(kMaxTag);
166-
if (interface_param != interface_params.end()) {
167-
interface.max = interface_param->second;
168-
}
169-
}
170-
// State interfaces have an element to define the type, not a name attribute
171-
if (std::string(interfaceTag) == "stateInterfaceType") {
172-
const std::string interface_type = get_text_for_element(
173-
interfaces_it,
174-
std::string(interfaceTag) + " type ");
175-
interface.name = interface_type;
176-
}
177-
178-
interfaces.push_back(interface);
179-
interfaces_it = interfaces_it->NextSiblingElement(interfaceTag);
147+
hardware_interface::InterfaceInfo interface;
148+
149+
const std::string interface_name = get_attribute_value(
150+
interfaces_it, kNameAttribute, interfaces_it->Name());
151+
interface.name = interface_name;
152+
153+
// Optional min/max attributes
154+
std::unordered_map<std::string, std::string> interface_params =
155+
parse_parameters_from_xml(interfaces_it->FirstChildElement(kParamTag));
156+
auto interface_param =
157+
interface_params.find(kMinTag);
158+
if (interface_param != interface_params.end()) {
159+
interface.min = interface_param->second;
160+
}
161+
interface_param = interface_params.find(kMaxTag);
162+
if (interface_param != interface_params.end()) {
163+
interface.max = interface_param->second;
180164
}
181165

182-
return interfaces;
166+
return interface;
183167
}
184168

185169
/**
186170
* \brief Search XML snippet from URDF for information about a control component.
187171
*
188172
* \param component_it pointer to the iterator where component info should be found
189-
* \return robot_control_components::ComponentInfo filled with information about component
173+
* \return ComponentInfo filled with information about component
190174
* \throws std::runtime_error if a component attribute or tag is not found
191175
*/
192176
ComponentInfo parse_component_from_xml(const tinyxml2::XMLElement * component_it)
@@ -195,29 +179,23 @@ ComponentInfo parse_component_from_xml(const tinyxml2::XMLElement * component_it
195179

196180
// Find name, type and class of a component
197181
component.type = component_it->Name();
198-
component.name = get_attribute_value(component_it, "name", component.type);
199-
200-
const auto * classType_it = component_it->FirstChildElement(kClassTypeTag);
201-
if (!classType_it) {
202-
throw std::runtime_error("no class type tag found in " + component.name);
203-
}
204-
component.class_type = get_text_for_element(classType_it, component.name + " " + kClassTypeTag);
182+
component.name = get_attribute_value(component_it, kNameAttribute, component.type);
205183

206-
// Parse commandInterfaceType tags
207-
const auto * command_interfaces_it = component_it->FirstChildElement(kCommandInterfaceTypeTag);
208-
if (command_interfaces_it) {
209-
component.command_interfaces = parse_interfaces_from_xml(
210-
command_interfaces_it, kCommandInterfaceTypeTag);
184+
// Parse all command interfaces
185+
const auto * command_interfaces_it = component_it->FirstChildElement(kCommandInterfaceTag);
186+
while (command_interfaces_it) {
187+
component.command_interfaces.push_back(parse_interfaces_from_xml(command_interfaces_it));
188+
command_interfaces_it = command_interfaces_it->NextSiblingElement(kCommandInterfaceTag);
211189
}
212190

213-
// Parse stateInterfaceType tags
214-
const auto * state_interfaces_it = component_it->FirstChildElement(kStateInterfaceTypeTag);
215-
if (state_interfaces_it) {
216-
component.state_interfaces = parse_interfaces_from_xml(
217-
state_interfaces_it, kStateInterfaceTypeTag);
191+
// Parse state interfaces
192+
const auto * state_interfaces_it = component_it->FirstChildElement(kStateInterfaceTag);
193+
while (state_interfaces_it) {
194+
component.state_interfaces.push_back(parse_interfaces_from_xml(state_interfaces_it));
195+
state_interfaces_it = state_interfaces_it->NextSiblingElement(kStateInterfaceTag);
218196
}
219197

220-
// Parse paramter tags
198+
// Parse parameters
221199
const auto * params_it = component_it->FirstChildElement(kParamTag);
222200
if (params_it) {
223201
component.parameters = parse_parameters_from_xml(params_it);
@@ -230,14 +208,14 @@ ComponentInfo parse_component_from_xml(const tinyxml2::XMLElement * component_it
230208
* \brief Parse a control resource from an "ros2_control" tag.
231209
*
232210
* \param ros2_control_it pointer to ros2_control element with informtions about resource.
233-
* \return robot_control_components::ComponentInfo filled with information about the robot
211+
* \return ComponentInfo filled with information about the robot
234212
* \throws std::runtime_error if a attributes or tag are not found
235213
*/
236214
HardwareInfo parse_resource_from_xml(const tinyxml2::XMLElement * ros2_control_it)
237215
{
238216
HardwareInfo hardware;
239-
hardware.name = get_attribute_value(ros2_control_it, "name", kROS2ControlTag);
240-
hardware.type = get_attribute_value(ros2_control_it, "type", kROS2ControlTag);
217+
hardware.name = get_attribute_value(ros2_control_it, kNameAttribute, kROS2ControlTag);
218+
hardware.type = get_attribute_value(ros2_control_it, kTypeAttribute, kROS2ControlTag);
241219

242220
// Parse everything under ros2_control tag
243221
hardware.hardware_class_type = "";

0 commit comments

Comments
 (0)