Skip to content

Commit 2c3aad1

Browse files
authored
New version of component parser (ros-controls#127)
* Added new version of parser. Files checkout from ros-controls#122 * SensorHardware can handle multiple sensor types
1 parent b9ea092 commit 2c3aad1

File tree

10 files changed

+1440
-13
lines changed

10 files changed

+1440
-13
lines changed

hardware_interface/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ find_package(ament_cmake REQUIRED)
1414
find_package(control_msgs REQUIRED)
1515
find_package(rclcpp REQUIRED)
1616
find_package(rcpputils REQUIRED)
17+
find_package(tinyxml2_vendor REQUIRED)
18+
find_package(TinyXML2 REQUIRED)
1719

1820
add_library(
1921
hardware_interface
@@ -41,6 +43,21 @@ ament_target_dependencies(
4143
# which is appropriate when building the dll but not consuming it.
4244
target_compile_definitions(hardware_interface PRIVATE "HARDWARE_INTERFACE_BUILDING_DLL")
4345

46+
add_library(
47+
component_parser
48+
src/component_parser.cpp
49+
)
50+
target_include_directories(
51+
component_parser
52+
PUBLIC
53+
include
54+
)
55+
ament_target_dependencies(
56+
component_parser
57+
TinyXML2
58+
)
59+
target_compile_definitions(component_parser PRIVATE "HARDWARE_INTERFACE_BUILDING_DLL")
60+
4461
add_library(
4562
components
4663
SHARED
@@ -63,6 +80,7 @@ install(
6380

6481
install(
6582
TARGETS
83+
component_parser
6684
components
6785
hardware_interface
6886
RUNTIME DESTINATION bin
@@ -106,12 +124,21 @@ if(BUILD_TESTING)
106124
ament_add_gmock(test_component_interfaces test/test_component_interfaces.cpp)
107125
target_include_directories(test_component_interfaces PRIVATE include)
108126
target_link_libraries(test_component_interfaces components hardware_interface)
127+
128+
ament_add_gmock(test_component_parser test/test_component_parser.cpp)
129+
target_link_libraries(test_component_parser component_parser)
130+
ament_target_dependencies(test_component_parser TinyXML2)
109131
endif()
110132

133+
ament_export_dependencies(
134+
rclcpp
135+
rcpputils
136+
)
111137
ament_export_include_directories(
112138
include
113139
)
114140
ament_export_libraries(
141+
component_parser
115142
components
116143
hardware_interface
117144
)
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright 2020 ros2_control Development Team
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef HARDWARE_INTERFACE__COMPONENT_PARSER_HPP_
16+
#define HARDWARE_INTERFACE__COMPONENT_PARSER_HPP_
17+
18+
#include <tinyxml2.h>
19+
#include <string>
20+
#include <unordered_map>
21+
#include <vector>
22+
23+
#include "hardware_interface/components/component_info.hpp"
24+
#include "hardware_interface/hardware_info.hpp"
25+
#include "hardware_interface/visibility_control.h"
26+
27+
namespace hardware_interface
28+
{
29+
30+
/**
31+
* \brief Search XML snippet from URDF for information about a control component.
32+
*
33+
* \param urdf string with robot's URDF
34+
* \return vector filled with information about robot's control resources
35+
* \throws std::runtime_error if a robot attribute or tag is not found
36+
*/
37+
HARDWARE_INTERFACE_PUBLIC
38+
std::vector<HardwareInfo> parse_control_resources_from_urdf(const std::string & urdf);
39+
40+
/**
41+
* \brief Parse a control resource from an "ros2_control" tag.
42+
*
43+
* \param ros2_control_it pointer to ros2_control element with informtions about resource.
44+
* \return robot_control_components::ComponentInfo filled with information about the robot
45+
* \throws std::runtime_error if a attributes or tag are not found
46+
*/
47+
HARDWARE_INTERFACE_PUBLIC
48+
HardwareInfo parse_resource_from_xml(const tinyxml2::XMLElement * ros2_control_it);
49+
50+
/**
51+
* \brief Gets value of the attribute on an XMLelement.
52+
* If attribute is not found throws an error.
53+
*
54+
* \param element_it XMLElement iterator to search for the attribute
55+
* \param attribute_name atribute name to serach for and return value
56+
* \param tag_name parent tag name where attribute is searched for (used for error output)
57+
* \return attribute value
58+
* \throws std::runtime_error if attribute is not found
59+
*/
60+
HARDWARE_INTERFACE_PUBLIC
61+
std::string get_attribute_value(
62+
const tinyxml2::XMLElement * element_it,
63+
const char * attribute_name, const char * tag_name);
64+
65+
/**
66+
* \brief Gets value of the text between tags.
67+
*
68+
* \param element_it XMLElement iterator to search for the text.
69+
* \param tag_name parent tag name where text is searched for (used for error output)
70+
* \return text of for the tag
71+
* \throws std::runtime_error if text is not found
72+
*/
73+
HARDWARE_INTERFACE_PUBLIC
74+
std::string get_text_for_element(
75+
const tinyxml2::XMLElement * element_it,
76+
const std::string & tag_name);
77+
78+
/**
79+
* \brief Gets value of the attribute on an XMLelement.
80+
* If attribute is not found throws an error.
81+
*
82+
* \param element_it XMLElement iterator to search for the attribute
83+
* \param attribute_name atribute name to serach for and return value
84+
* \param tag_name parent tag name where attribute is searched for (used for error output)
85+
* \return attribute value
86+
* \throws std::runtime_error if attribute is not found
87+
*/
88+
HARDWARE_INTERFACE_PUBLIC
89+
std::string get_attribute_value(
90+
const tinyxml2::XMLElement * element_it,
91+
const char * attribute_name, std::string tag_name);
92+
93+
/**
94+
* \brief Search XML snippet from URDF for information about a control component.
95+
*
96+
* \param component_it pointer to the iterator where component info should be found
97+
* \return robot_control_components::ComponentInfo filled with information about component
98+
* \throws std::runtime_error if a component attribute or tag is not found
99+
*/
100+
HARDWARE_INTERFACE_PUBLIC
101+
components::ComponentInfo parse_component_from_xml(const tinyxml2::XMLElement * component_it);
102+
103+
/**
104+
* \brief Search XML snippet for definition of interfaceTypes.
105+
*
106+
* \param interfaces_it pointer to the interator over interfaces
107+
* \param interfaceTag interface type tag (command or state)
108+
* \return std::vector< std::__cxx11::string > list of interface types
109+
* \throws std::runtime_error if the interfaceType text not set in a tag
110+
*/
111+
HARDWARE_INTERFACE_PUBLIC
112+
std::vector<std::string> parse_interfaces_from_xml(
113+
const tinyxml2::XMLElement * interfaces_it, const char * interfaceTag);
114+
115+
/**
116+
* \brief Search XML snippet from URDF for parameters.
117+
*
118+
* \param params_it pointer to the iterator where parameters info should be found
119+
* \return std::map< std::__cxx11::string, std::__cxx11::string > key-value map with parameters
120+
* \throws std::runtime_error if a component attribute or tag is not found
121+
*/
122+
HARDWARE_INTERFACE_PUBLIC
123+
std::unordered_map<std::string, std::string> parse_parameters_from_xml(
124+
const tinyxml2::XMLElement * params_it);
125+
126+
} // namespace hardware_interface
127+
#endif // HARDWARE_INTERFACE__COMPONENT_PARSER_HPP_

hardware_interface/include/hardware_interface/hardware_info.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ struct HardwareInfo
5050
* \brief map of joints provided by the hardware where the key is the joint name.
5151
* Required for Actuator and System Hardware.
5252
*/
53-
std::unordered_map<std::string, components::ComponentInfo> joints;
53+
std::vector<components::ComponentInfo> joints;
5454
/**
5555
* \brief map of joints provided by the hardware where the key is the joint name.
5656
* Required for Sensor and optional for System Hardware.
5757
*/
58-
std::unordered_map<std::string, components::ComponentInfo> sensors;
58+
std::vector<components::ComponentInfo> sensors;
5959
/**
6060
* \brief map of transmissions to calcualte ration between joints and physical actuators.
6161
* Optional for Actuator and System Hardware.
6262
*/
63-
std::unordered_map<std::string, components::ComponentInfo> transmissions;
63+
std::vector<components::ComponentInfo> transmissions;
6464
};
6565

6666
} // namespace hardware_interface

hardware_interface/include/hardware_interface/sensor_hardware.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class SensorHardware final
5656
hardware_interface_status get_status() const;
5757

5858
HARDWARE_INTERFACE_PUBLIC
59-
return_type read_sensor(std::shared_ptr<components::Sensor> sensor);
59+
return_type read_sensors(const std::vector<std::shared_ptr<components::Sensor>> & sensors);
6060

6161
private:
6262
std::unique_ptr<SensorHardwareInterface> impl_;

hardware_interface/include/hardware_interface/sensor_hardware_interface.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define HARDWARE_INTERFACE__SENSOR_HARDWARE_INTERFACE_HPP_
1717

1818
#include <memory>
19+
#include <vector>
1920

2021
#include "hardware_interface/components/sensor.hpp"
2122
#include "hardware_interface/hardware_info.hpp"
@@ -82,12 +83,13 @@ class SensorHardwareInterface
8283
* \brief Read data from the hardware into sensors using "set_state" function in the Sensor class.
8384
* This function is always called by the resource manager.
8485
*
85-
* \param sensors sensor where data from the hardware are stored.
86+
* \param sensors list of sensors where data from the hardware are stored.
8687
* \return return_type:OK if everything worked as expected, return_type::ERROR otherwise.
8788
*/
8889
HARDWARE_INTERFACE_PUBLIC
8990
virtual
90-
return_type read_sensor(std::shared_ptr<components::Sensor> sensor) const = 0;
91+
return_type read_sensors(const std::vector<std::shared_ptr<components::Sensor>> & sensors) const =
92+
0;
9193
};
9294

9395
} // namespace hardware_interface

hardware_interface/package.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99

1010
<buildtool_depend>ament_cmake</buildtool_depend>
1111

12+
<depend>control_msgs</depend>
1213
<depend>rclcpp</depend>
1314
<depend>rcpputils</depend>
14-
<depend>control_msgs</depend>
15+
<depend>tinyxml2_vendor</depend>
1516

1617
<test_depend>ament_cmake_gmock</test_depend>
1718
<test_depend>ament_lint_auto</test_depend>

0 commit comments

Comments
 (0)