Skip to content

Commit 720e11f

Browse files
committed
code review suggestions and:
* components check for export of interfaces * change intefaces to allow custom export and test
1 parent 4f81b55 commit 720e11f

19 files changed

+673
-356
lines changed

hardware_interface/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ if(BUILD_TESTING)
7676
target_link_libraries(test_component_interfaces hardware_interface)
7777
ament_target_dependencies(test_component_interfaces ros2_control_test_assets)
7878

79+
ament_add_gmock(test_component_interfaces_custom_export test/test_component_interfaces_custom_export.cpp)
80+
target_link_libraries(test_component_interfaces_custom_export hardware_interface)
81+
ament_target_dependencies(test_component_interfaces_custom_export ros2_control_test_assets)
82+
7983
ament_add_gmock(test_component_parser test/test_component_parser.cpp)
8084
target_link_libraries(test_component_parser hardware_interface)
8185
ament_target_dependencies(test_component_parser ros2_control_test_assets)

hardware_interface/include/hardware_interface/actuator.hpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,11 @@ class Actuator final
6464
HARDWARE_INTERFACE_PUBLIC
6565
const rclcpp_lifecycle::State & error();
6666

67-
[[deprecated(
68-
"Replaced by vector<std::shared_ptr<StateInterface>> on_export_state_interfaces() method. "
69-
"Exporting is handled by the Framework.")]] HARDWARE_INTERFACE_PUBLIC
70-
std::vector<StateInterface>
71-
export_state_interfaces();
72-
7367
HARDWARE_INTERFACE_PUBLIC
74-
std::vector<std::shared_ptr<StateInterface>> on_export_state_interfaces();
75-
76-
[[deprecated(
77-
"Replaced by vector<std::shared_ptr<CommandInterface>> on_export_state_interfaces() method. "
78-
"Exporting is handled by the Framework.")]] HARDWARE_INTERFACE_PUBLIC
79-
std::vector<CommandInterface>
80-
export_command_interfaces();
68+
std::vector<StateInterfaceSharedPtr> export_state_interfaces();
8169

8270
HARDWARE_INTERFACE_PUBLIC
83-
std::vector<std::shared_ptr<CommandInterface>> on_export_command_interfaces();
71+
std::vector<CommandInterfaceSharedPtr> export_command_interfaces();
8472

8573
HARDWARE_INTERFACE_PUBLIC
8674
return_type prepare_command_mode_switch(

hardware_interface/include/hardware_interface/actuator_interface.hpp

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <map>
2020
#include <memory>
2121
#include <string>
22+
#include <unordered_map>
2223
#include <utility>
2324
#include <vector>
2425

@@ -136,20 +137,18 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod
136137

137138
/// Exports all state interfaces for this hardware interface.
138139
/**
139-
* Default implementation for exporting the StateInterfaces. The StateInterfaces are created
140-
* according to the InterfaceDescription. The memory accessed by the controllers and hardware is
141-
* assigned here and resides in the system_interface.
142-
*
143-
* If overwritten:
144-
* The state interfaces have to be created and transferred according
145-
* to the hardware info passed in for the configuration.
140+
* Old way of exporting the StateInterfaces. If a empty vector is returned then
141+
* the on_export_state_interfaces() method is called. If a vector with StateInterfaces is returned
142+
* then the exporting of the StateInterfaces is only done with this function and the ownership is
143+
* transferred to the resource manager. The set_command(...), get_command(...), ..., can then not
144+
* be used.
146145
*
147146
* Note the ownership over the state interfaces is transferred to the caller.
148147
*
149148
* \return vector of state interfaces
150149
*/
151150
[[deprecated(
152-
"Replaced by vector<std::shared_ptr<StateInterface>> on_export_state_interfaces() method. "
151+
"Replaced by vector<StateInterfaceSharedPtr> on_export_state_interfaces() method. "
153152
"Exporting is "
154153
"handled "
155154
"by the Framework.")]] virtual std::vector<StateInterface>
@@ -160,33 +159,41 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod
160159
return {};
161160
}
162161

163-
std::vector<std::shared_ptr<StateInterface>> on_export_state_interfaces()
162+
/**
163+
* Default implementation for exporting the StateInterfaces. The StateInterfaces are created
164+
* according to the InterfaceDescription. The memory accessed by the controllers and hardware is
165+
* assigned here and resides in the actuator_interface.
166+
*
167+
* \return vector of shared pointers to the created and stored StateInterfaces
168+
*/
169+
virtual std::vector<StateInterfaceSharedPtr> on_export_state_interfaces()
164170
{
165-
std::vector<std::shared_ptr<StateInterface>> state_interfaces;
171+
std::vector<StateInterfaceSharedPtr> state_interfaces;
166172
state_interfaces.reserve(joint_state_interfaces_.size());
167173

168174
for (const auto & [name, descr] : joint_state_interfaces_)
169175
{
170-
actuator_states_.insert(std::make_pair(name, std::make_shared<StateInterface>(descr)));
171-
state_interfaces.push_back(actuator_states_.at(name));
176+
auto state_interface = std::make_shared<StateInterface>(descr);
177+
actuator_states_.insert(std::make_pair(name, state_interface));
178+
state_interfaces.push_back(state_interface);
172179
}
173180
return state_interfaces;
174181
}
182+
175183
/// Exports all command interfaces for this hardware interface.
176184
/**
177-
* Default implementation for exporting the CommandInterfaces. The CommandInterfaces are created
178-
* according to the InterfaceDescription. The memory accessed by the controllers and hardware is
179-
* assigned here and resides in the system_interface.
180-
*
181-
* The command interfaces have to be created and transferred according
182-
* to the hardware info passed in for the configuration.
185+
* Old way of exporting the CommandInterfaces. If a empty vector is returned then
186+
* the on_export_command_interfaces() method is called. If a vector with CommandInterfaces is
187+
* returned then the exporting of the CommandInterfaces is only done with this function and the
188+
* ownership is transferred to the resource manager. The set_command(...), get_command(...), ...,
189+
* can then not be used.
183190
*
184191
* Note the ownership over the state interfaces is transferred to the caller.
185192
*
186-
* \return vector of command interfaces
193+
* \return vector of state interfaces
187194
*/
188195
[[deprecated(
189-
"Replaced by vector<std::shared_ptr<CommandInterface>> on_export_command_interfaces() method. "
196+
"Replaced by vector<CommandInterfaceSharedPtr> on_export_command_interfaces() method. "
190197
"Exporting is "
191198
"handled "
192199
"by the Framework.")]] virtual std::vector<CommandInterface>
@@ -197,15 +204,23 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod
197204
return {};
198205
}
199206

200-
std::vector<std::shared_ptr<CommandInterface>> on_export_command_interfaces()
207+
/**
208+
* Default implementation for exporting the CommandInterfaces. The CommandInterfaces are created
209+
* according to the InterfaceDescription. The memory accessed by the controllers and hardware is
210+
* assigned here and resides in the actuator_interface.
211+
*
212+
* \return vector of shared pointers to the created and stored CommandInterfaces
213+
*/
214+
virtual std::vector<CommandInterfaceSharedPtr> on_export_command_interfaces()
201215
{
202-
std::vector<std::shared_ptr<CommandInterface>> command_interfaces;
216+
std::vector<CommandInterfaceSharedPtr> command_interfaces;
203217
command_interfaces.reserve(joint_command_interfaces_.size());
204218

205219
for (const auto & [name, descr] : joint_command_interfaces_)
206220
{
207-
actuator_commands_.insert(std::make_pair(name, std::make_shared<CommandInterface>(descr)));
208-
command_interfaces.push_back(actuator_commands_.at(name));
221+
auto command_interface = std::make_shared<CommandInterface>(descr);
222+
actuator_commands_.insert(std::make_pair(name, command_interface));
223+
command_interfaces.push_back(command_interface);
209224
}
210225

211226
return command_interfaces;
@@ -315,9 +330,8 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod
315330
std::map<std::string, InterfaceDescription> joint_state_interfaces_;
316331
std::map<std::string, InterfaceDescription> joint_command_interfaces_;
317332

318-
private:
319-
std::map<std::string, std::shared_ptr<StateInterface>> actuator_states_;
320-
std::map<std::string, std::shared_ptr<CommandInterface>> actuator_commands_;
333+
std::unordered_map<std::string, StateInterfaceSharedPtr> actuator_states_;
334+
std::unordered_map<std::string, CommandInterfaceSharedPtr> actuator_commands_;
321335

322336
rclcpp_lifecycle::State lifecycle_state_;
323337
};

hardware_interface/include/hardware_interface/component_parser.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,5 @@ HARDWARE_INTERFACE_PUBLIC
7878
std::vector<InterfaceDescription> parse_gpio_command_interface_descriptions_from_hardware_info(
7979
const HardwareInfo & hw_info);
8080

81-
HARDWARE_INTERFACE_PUBLIC
82-
bool parse_bool(const std::string & bool_string);
83-
8481
} // namespace hardware_interface
8582
#endif // HARDWARE_INTERFACE__COMPONENT_PARSER_HPP_

hardware_interface/include/hardware_interface/handle.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define HARDWARE_INTERFACE__HANDLE_HPP_
1717

1818
#include <limits>
19+
#include <memory>
1920
#include <string>
2021
#include <utility>
2122
#include <variant>
@@ -43,7 +44,7 @@ class Handle
4344
}
4445

4546
explicit Handle(const InterfaceDescription & interface_description)
46-
: prefix_name_(interface_description.prefix_name),
47+
: prefix_name_(interface_description.prefix_name_),
4748
interface_name_(interface_description.interface_info.name)
4849
{
4950
// As soon as multiple datatypes are used in HANDLE_DATATYPE
@@ -135,6 +136,8 @@ class StateInterface : public Handle
135136
using Handle::Handle;
136137
};
137138

139+
using StateInterfaceSharedPtr = std::shared_ptr<StateInterface>;
140+
138141
class CommandInterface : public Handle
139142
{
140143
public:
@@ -155,6 +158,8 @@ class CommandInterface : public Handle
155158
using Handle::Handle;
156159
};
157160

161+
using CommandInterfaceSharedPtr = std::shared_ptr<CommandInterface>;
162+
158163
} // namespace hardware_interface
159164

160165
#endif // HARDWARE_INTERFACE__HANDLE_HPP_

hardware_interface/include/hardware_interface/hardware_info.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ struct TransmissionInfo
108108
*/
109109
struct InterfaceDescription
110110
{
111-
InterfaceDescription(const std::string & prefix_name_in, const InterfaceInfo & interface_info_in)
112-
: prefix_name(prefix_name_in), interface_info(interface_info_in)
111+
InterfaceDescription(const std::string & prefix_name, const InterfaceInfo & interface_info_in)
112+
: prefix_name_(prefix_name), interface_info(interface_info_in)
113113
{
114114
}
115115

116116
/**
117117
* Name of the interface defined by the user.
118118
*/
119-
std::string prefix_name;
119+
std::string prefix_name_;
120120

121121
/**
122122
* What type of component is exported: joint, sensor or gpio
@@ -128,7 +128,7 @@ struct InterfaceDescription
128128
*/
129129
InterfaceInfo interface_info;
130130

131-
std::string get_name() const { return prefix_name + "/" + interface_info.name; }
131+
std::string get_name() const { return prefix_name_ + "/" + interface_info.name; }
132132

133133
std::string get_interface_type() const { return interface_info.name; }
134134
};

hardware_interface/include/hardware_interface/sensor.hpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,8 @@ class Sensor final
6565
HARDWARE_INTERFACE_PUBLIC
6666
const rclcpp_lifecycle::State & error();
6767

68-
[[deprecated(
69-
"Replaced by vector<std::shared_ptr<StateInterface>> on_export_state_interfaces() method. "
70-
"Exporting is handled by the Framework.")]] HARDWARE_INTERFACE_PUBLIC
71-
std::vector<StateInterface>
72-
export_state_interfaces();
73-
7468
HARDWARE_INTERFACE_PUBLIC
75-
std::vector<std::shared_ptr<StateInterface>> on_export_state_interfaces();
69+
std::vector<StateInterfaceSharedPtr> export_state_interfaces();
7670

7771
HARDWARE_INTERFACE_PUBLIC
7872
std::string get_name() const;

hardware_interface/include/hardware_interface/sensor_interface.hpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <map>
2020
#include <memory>
2121
#include <string>
22+
#include <unordered_map>
2223
#include <utility>
2324
#include <vector>
2425

@@ -121,20 +122,18 @@ class SensorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
121122

122123
/// Exports all state interfaces for this hardware interface.
123124
/**
124-
* Default implementation for exporting the StateInterfaces. The StateInterfaces are created
125-
* according to the InterfaceDescription. The memory accessed by the controllers and hardware is
126-
* assigned here and resides in the system_interface.
127-
*
128-
* If overwritten:
129-
* The state interfaces have to be created and transferred according
130-
* to the hardware info passed in for the configuration.
125+
* Old way of exporting the StateInterfaces. If a empty vector is returned then
126+
* the on_export_state_interfaces() method is called. If a vector with StateInterfaces is returned
127+
* then the exporting of the StateInterfaces is only done with this function and the ownership is
128+
* transferred to the resource manager. The set_command(...), get_command(...), ..., can then not
129+
* be used.
131130
*
132131
* Note the ownership over the state interfaces is transferred to the caller.
133132
*
134133
* \return vector of state interfaces
135134
*/
136135
[[deprecated(
137-
"Replaced by vector<std::shared_ptr<StateInterface>> on_export_state_interfaces() method. "
136+
"Replaced by vector<StateInterfaceSharedPtr> on_export_state_interfaces() method. "
138137
"Exporting is handled "
139138
"by the Framework.")]] virtual std::vector<StateInterface>
140139
export_state_interfaces()
@@ -144,17 +143,25 @@ class SensorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
144143
return {};
145144
}
146145

147-
std::vector<std::shared_ptr<StateInterface>> on_export_state_interfaces()
146+
/**
147+
* Default implementation for exporting the StateInterfaces. The StateInterfaces are created
148+
* according to the InterfaceDescription. The memory accessed by the controllers and hardware is
149+
* assigned here and resides in the sensor_interface.
150+
*
151+
* \return vector of shared pointers to the created and stored StateInterfaces
152+
*/
153+
virtual std::vector<StateInterfaceSharedPtr> on_export_state_interfaces()
148154
{
149-
std::vector<std::shared_ptr<StateInterface>> state_interfaces;
155+
std::vector<StateInterfaceSharedPtr> state_interfaces;
150156
state_interfaces.reserve(sensor_state_interfaces_.size());
151157

152158
for (const auto & [name, descr] : sensor_state_interfaces_)
153159
{
154160
// TODO(Manuel) maybe check for duplicates otherwise only the first appearance of "name" is
155161
// inserted
156-
sensor_states_.insert(std::make_pair(name, std::make_shared<StateInterface>(descr)));
157-
state_interfaces.push_back(sensor_states_.at(name));
162+
auto state_interface = std::make_shared<StateInterface>(descr);
163+
sensor_states_.insert(std::make_pair(name, state_interface));
164+
state_interfaces.push_back(state_interface);
158165
}
159166

160167
return state_interfaces;
@@ -205,8 +212,7 @@ class SensorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
205212

206213
std::map<std::string, InterfaceDescription> sensor_state_interfaces_;
207214

208-
private:
209-
std::map<std::string, std::shared_ptr<StateInterface>> sensor_states_;
215+
std::unordered_map<std::string, StateInterfaceSharedPtr> sensor_states_;
210216

211217
rclcpp_lifecycle::State lifecycle_state_;
212218
};

hardware_interface/include/hardware_interface/system.hpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,11 @@ class System final
6565
HARDWARE_INTERFACE_PUBLIC
6666
const rclcpp_lifecycle::State & error();
6767

68-
[[deprecated(
69-
"Replaced by vector<std::shared_ptr<StateInterface>> on_export_state_interfaces() method. "
70-
"Exporting is handled by the Framework.")]] HARDWARE_INTERFACE_PUBLIC
71-
std::vector<StateInterface>
72-
export_state_interfaces();
73-
7468
HARDWARE_INTERFACE_PUBLIC
75-
std::vector<std::shared_ptr<StateInterface>> on_export_state_interfaces();
76-
77-
[[deprecated(
78-
"Replaced by vector<std::shared_ptr<CommandInterface>> on_export_state_interfaces() method. "
79-
"Exporting is handled by the Framework.")]] HARDWARE_INTERFACE_PUBLIC
80-
std::vector<CommandInterface>
81-
export_command_interfaces();
69+
std::vector<StateInterfaceSharedPtr> export_state_interfaces();
8270

8371
HARDWARE_INTERFACE_PUBLIC
84-
std::vector<std::shared_ptr<CommandInterface>> on_export_command_interfaces();
72+
std::vector<CommandInterfaceSharedPtr> export_command_interfaces();
8573

8674
HARDWARE_INTERFACE_PUBLIC
8775
return_type prepare_command_mode_switch(

0 commit comments

Comments
 (0)