Skip to content

Commit da326fd

Browse files
committed
make states and commands of component interfaces private
1 parent da354d8 commit da326fd

File tree

5 files changed

+139
-79
lines changed

5 files changed

+139
-79
lines changed

hardware_interface/include/hardware_interface/actuator_interface.hpp

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,14 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod
182182

183183
/**
184184
* Override this method to export custom StateInterfaces which are not defined in the URDF file.
185+
* Those interfaces will be added to the unlisted_state_interfaces_ map.
185186
*
186187
* Note method name is going to be changed to export_state_interfaces() as soon as the deprecated
187188
* version is removed.
188189
*
189-
* \return vector of shared pointers to the created and stored StateInterfaces
190+
* \return vector of descriptions to the unlisted StateInterfaces
190191
*/
191-
virtual std::vector<std::shared_ptr<StateInterface>> export_state_interfaces_2()
192+
virtual std::vector<hardware_interface::InterfaceDescription> export_state_interfaces_2()
192193
{
193194
// return empty vector by default.
194195
return {};
@@ -203,8 +204,24 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod
203204
*/
204205
virtual std::vector<std::shared_ptr<StateInterface>> on_export_state_interfaces()
205206
{
206-
std::vector<std::shared_ptr<StateInterface>> state_interfaces = export_state_interfaces_2();
207-
state_interfaces.reserve(joint_state_interfaces_.size());
207+
// import the unlisted interfaces
208+
std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
209+
export_state_interfaces_2();
210+
211+
std::vector<std::shared_ptr<StateInterface>> state_interfaces;
212+
state_interfaces.reserve(
213+
unlisted_interface_descriptions.size() + joint_state_interfaces_.size());
214+
215+
// add InterfaceDescriptions and create the StateInterfaces from the descriptions and add to
216+
// maps.
217+
for (const auto & description : unlisted_interface_descriptions)
218+
{
219+
auto name = description.get_name();
220+
unlisted_state_interfaces_.insert(std::make_pair(name, description));
221+
auto state_interface = std::make_shared<StateInterface>(description);
222+
actuator_states_.insert(std::make_pair(name, state_interface));
223+
state_interfaces.push_back(state_interface);
224+
}
208225

209226
for (const auto & [name, descr] : joint_state_interfaces_)
210227
{
@@ -241,13 +258,14 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod
241258

242259
/**
243260
* Override this method to export custom CommandInterfaces which are not defined in the URDF file.
261+
* Those interfaces will be added to the unlisted_command_interfaces_ map.
244262
*
245263
* Note method name is going to be changed to export_command_interfaces() as soon as the
246264
* deprecated version is removed.
247265
*
248-
* \return vector of shared pointers to the created and stored StateInterfaces
266+
* \return vector of descriptions to the unlisted CommandInterfaces
249267
*/
250-
virtual std::vector<std::shared_ptr<CommandInterface>> export_command_interfaces_2()
268+
virtual std::vector<hardware_interface::InterfaceDescription> export_command_interfaces_2()
251269
{
252270
// return empty vector by default.
253271
return {};
@@ -262,9 +280,24 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod
262280
*/
263281
virtual std::vector<std::shared_ptr<CommandInterface>> on_export_command_interfaces()
264282
{
265-
std::vector<std::shared_ptr<CommandInterface>> command_interfaces =
283+
// import the unlisted interfaces
284+
std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
266285
export_command_interfaces_2();
267-
command_interfaces.reserve(joint_command_interfaces_.size());
286+
287+
std::vector<std::shared_ptr<CommandInterface>> command_interfaces;
288+
command_interfaces.reserve(
289+
unlisted_interface_descriptions.size() + joint_command_interfaces_.size());
290+
291+
// add InterfaceDescriptions and create the CommandInterfaces from the descriptions and add to
292+
// maps.
293+
for (const auto & description : unlisted_interface_descriptions)
294+
{
295+
auto name = description.get_name();
296+
unlisted_command_interfaces_.insert(std::make_pair(name, description));
297+
auto command_interface = std::make_shared<CommandInterface>(description);
298+
actuator_commands_.insert(std::make_pair(name, command_interface));
299+
command_interfaces.push_back(command_interface);
300+
}
268301

269302
for (const auto & [name, descr] : joint_command_interfaces_)
270303
{
@@ -398,14 +431,16 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod
398431
std::unordered_map<std::string, InterfaceDescription> joint_state_interfaces_;
399432
std::unordered_map<std::string, InterfaceDescription> joint_command_interfaces_;
400433

401-
std::unordered_map<std::string, std::shared_ptr<StateInterface>> actuator_states_;
402-
std::unordered_map<std::string, std::shared_ptr<CommandInterface>> actuator_commands_;
434+
std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
435+
std::unordered_map<std::string, InterfaceDescription> unlisted_command_interfaces_;
403436

404437
rclcpp_lifecycle::State lifecycle_state_;
405438

406439
private:
407440
rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface_;
408441
rclcpp::Logger actuator_logger_;
442+
std::unordered_map<std::string, std::shared_ptr<StateInterface>> actuator_states_;
443+
std::unordered_map<std::string, std::shared_ptr<CommandInterface>> actuator_commands_;
409444
};
410445

411446
} // namespace hardware_interface

hardware_interface/include/hardware_interface/sensor_interface.hpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,14 @@ class SensorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
166166

167167
/**
168168
* Override this method to export custom StateInterfaces which are not defined in the URDF file.
169+
* Those interfaces will be added to the unlisted_state_interfaces_ map.
169170
*
170171
* Note method name is going to be changed to export_state_interfaces() as soon as the deprecated
171172
* version is removed.
172173
*
173-
* \return vector of shared pointers to the created and stored StateInterfaces
174+
* \return vector of descriptions to the unlisted StateInterfaces
174175
*/
175-
virtual std::vector<std::shared_ptr<StateInterface>> export_state_interfaces_2()
176+
virtual std::vector<hardware_interface::InterfaceDescription> export_state_interfaces_2()
176177
{
177178
// return empty vector by default.
178179
return {};
@@ -187,8 +188,24 @@ class SensorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
187188
*/
188189
virtual std::vector<std::shared_ptr<StateInterface>> on_export_state_interfaces()
189190
{
190-
std::vector<std::shared_ptr<StateInterface>> state_interfaces = export_state_interfaces_2();
191-
state_interfaces.reserve(sensor_state_interfaces_.size());
191+
// import the unlisted interfaces
192+
std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
193+
export_state_interfaces_2();
194+
195+
std::vector<std::shared_ptr<StateInterface>> state_interfaces;
196+
state_interfaces.reserve(
197+
unlisted_interface_descriptions.size() + sensor_state_interfaces_.size());
198+
199+
// add InterfaceDescriptions and create the StateInterfaces from the descriptions and add to
200+
// maps.
201+
for (const auto & description : unlisted_interface_descriptions)
202+
{
203+
auto name = description.get_name();
204+
unlisted_state_interfaces_.insert(std::make_pair(name, description));
205+
auto state_interface = std::make_shared<StateInterface>(description);
206+
sensor_states_.insert(std::make_pair(name, state_interface));
207+
state_interfaces.push_back(state_interface);
208+
}
192209

193210
for (const auto & [name, descr] : sensor_state_interfaces_)
194211
{
@@ -264,14 +281,14 @@ class SensorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
264281
HardwareInfo info_;
265282

266283
std::unordered_map<std::string, InterfaceDescription> sensor_state_interfaces_;
267-
268-
std::unordered_map<std::string, std::shared_ptr<StateInterface>> sensor_states_;
284+
std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
269285

270286
rclcpp_lifecycle::State lifecycle_state_;
271287

272288
private:
273289
rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface_;
274290
rclcpp::Logger sensor_logger_;
291+
std::unordered_map<std::string, std::shared_ptr<StateInterface>> sensor_states_;
275292
};
276293

277294
} // namespace hardware_interface

hardware_interface/include/hardware_interface/system_interface.hpp

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,14 @@ class SystemInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
201201

202202
/**
203203
* Override this method to export custom StateInterfaces which are not defined in the URDF file.
204+
* Those interfaces will be added to the unlisted_state_interfaces_ map.
204205
*
205206
* Note method name is going to be changed to export_state_interfaces() as soon as the deprecated
206207
* version is removed.
207208
*
208-
* \return vector of shared pointers to the created and stored StateInterfaces
209+
* \return vector of descriptions to the unlisted StateInterfaces
209210
*/
210-
virtual std::vector<std::shared_ptr<StateInterface>> export_state_interfaces_2()
211+
virtual std::vector<hardware_interface::InterfaceDescription> export_state_interfaces_2()
211212
{
212213
// return empty vector by default.
213214
return {};
@@ -222,10 +223,25 @@ class SystemInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
222223
*/
223224
std::vector<std::shared_ptr<StateInterface>> on_export_state_interfaces()
224225
{
225-
std::vector<std::shared_ptr<StateInterface>> state_interfaces = export_state_interfaces_2();
226+
// import the unlisted interfaces
227+
std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
228+
export_state_interfaces_2();
229+
230+
std::vector<std::shared_ptr<StateInterface>> state_interfaces;
226231
state_interfaces.reserve(
227-
joint_state_interfaces_.size() + sensor_state_interfaces_.size() +
228-
gpio_state_interfaces_.size());
232+
unlisted_interface_descriptions.size() + joint_state_interfaces_.size() +
233+
sensor_state_interfaces_.size() + gpio_state_interfaces_.size());
234+
235+
// add InterfaceDescriptions and create the StateInterfaces from the descriptions and add to
236+
// maps.
237+
for (const auto & description : unlisted_interface_descriptions)
238+
{
239+
auto name = description.get_name();
240+
unlisted_state_interfaces_.insert(std::make_pair(name, description));
241+
auto state_interface = std::make_shared<StateInterface>(description);
242+
system_states_.insert(std::make_pair(name, state_interface));
243+
state_interfaces.push_back(state_interface);
244+
}
229245

230246
for (const auto & [name, descr] : joint_state_interfaces_)
231247
{
@@ -274,13 +290,14 @@ class SystemInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
274290

275291
/**
276292
* Override this method to export custom CommandInterfaces which are not defined in the URDF file.
293+
* Those interfaces will be added to the unlisted_command_interfaces_ map.
277294
*
278295
* Note method name is going to be changed to export_command_interfaces() as soon as the
279296
* deprecated version is removed.
280297
*
281-
* \return vector of shared pointers to the created and stored StateInterfaces
298+
* \return vector of descriptions to the unlisted CommandInterfaces
282299
*/
283-
virtual std::vector<std::shared_ptr<CommandInterface>> export_command_interfaces_2()
300+
virtual std::vector<hardware_interface::InterfaceDescription> export_command_interfaces_2()
284301
{
285302
// return empty vector by default.
286303
return {};
@@ -295,9 +312,25 @@ class SystemInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
295312
*/
296313
std::vector<std::shared_ptr<CommandInterface>> on_export_command_interfaces()
297314
{
298-
std::vector<std::shared_ptr<CommandInterface>> command_interfaces =
315+
// import the unlisted interfaces
316+
std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
299317
export_command_interfaces_2();
300-
command_interfaces.reserve(joint_command_interfaces_.size() + gpio_command_interfaces_.size());
318+
319+
std::vector<std::shared_ptr<CommandInterface>> command_interfaces;
320+
command_interfaces.reserve(
321+
unlisted_interface_descriptions.size() + joint_command_interfaces_.size() +
322+
gpio_command_interfaces_.size());
323+
324+
// add InterfaceDescriptions and create the CommandInterfaces from the descriptions and add to
325+
// maps.
326+
for (const auto & description : unlisted_interface_descriptions)
327+
{
328+
auto name = description.get_name();
329+
unlisted_command_interfaces_.insert(std::make_pair(name, description));
330+
auto command_interface = std::make_shared<CommandInterface>(description);
331+
system_commands_.insert(std::make_pair(name, command_interface));
332+
command_interfaces.push_back(command_interface);
333+
}
301334

302335
for (const auto & [name, descr] : joint_command_interfaces_)
303336
{
@@ -443,14 +476,16 @@ class SystemInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
443476
std::unordered_map<std::string, InterfaceDescription> gpio_state_interfaces_;
444477
std::unordered_map<std::string, InterfaceDescription> gpio_command_interfaces_;
445478

446-
std::unordered_map<std::string, std::shared_ptr<StateInterface>> system_states_;
447-
std::unordered_map<std::string, std::shared_ptr<CommandInterface>> system_commands_;
479+
std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
480+
std::unordered_map<std::string, InterfaceDescription> unlisted_command_interfaces_;
448481

449482
rclcpp_lifecycle::State lifecycle_state_;
450483

451484
private:
452485
rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface_;
453486
rclcpp::Logger system_logger_;
487+
std::unordered_map<std::string, std::shared_ptr<StateInterface>> system_states_;
488+
std::unordered_map<std::string, std::shared_ptr<CommandInterface>> system_commands_;
454489
};
455490

456491
} // namespace hardware_interface

hardware_interface/src/component_parser.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -939,21 +939,4 @@ std::vector<InterfaceDescription> parse_command_interface_descriptions_from_hard
939939
return component_command_interface_descriptions;
940940
}
941941

942-
std::vector<InterfaceDescription> parse_gpio_command_interface_descriptions_from_hardware_info(
943-
const HardwareInfo & hw_info)
944-
{
945-
std::vector<InterfaceDescription> gpio_command_interface_descriptions;
946-
gpio_command_interface_descriptions.reserve(hw_info.gpios.size());
947-
948-
for (const auto & gpio : hw_info.gpios)
949-
{
950-
for (const auto & command_interface : gpio.command_interfaces)
951-
{
952-
gpio_command_interface_descriptions.emplace_back(
953-
InterfaceDescription(gpio.name, command_interface));
954-
}
955-
}
956-
return gpio_command_interface_descriptions;
957-
}
958-
959942
} // namespace hardware_interface

hardware_interface/test/test_component_interfaces_custom_export.cpp

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,23 @@ class DummyActuatorDefault : public hardware_interface::ActuatorInterface
5757
{
5858
std::string get_name() const override { return "DummyActuatorDefault"; }
5959

60-
std::vector<std::shared_ptr<hardware_interface::StateInterface>> export_state_interfaces_2()
61-
override
60+
std::vector<hardware_interface::InterfaceDescription> export_state_interfaces_2() override
6261
{
63-
std::vector<std::shared_ptr<hardware_interface::StateInterface>> interfaces;
64-
auto unlisted_state_interface = std::make_shared<hardware_interface::StateInterface>(
65-
info_.joints[0].name, "some_unlisted_interface", nullptr);
66-
actuator_states_.insert(
67-
std::make_pair(unlisted_state_interface->get_name(), unlisted_state_interface));
62+
std::vector<hardware_interface::InterfaceDescription> interfaces;
63+
hardware_interface::InterfaceInfo info;
64+
info.name = "some_unlisted_interface";
65+
hardware_interface::InterfaceDescription unlisted_state_interface(info_.joints[0].name, info);
6866
interfaces.push_back(unlisted_state_interface);
6967

7068
return interfaces;
7169
}
7270

73-
std::vector<std::shared_ptr<hardware_interface::CommandInterface>> export_command_interfaces_2()
74-
override
71+
std::vector<hardware_interface::InterfaceDescription> export_command_interfaces_2() override
7572
{
76-
std::vector<std::shared_ptr<hardware_interface::CommandInterface>> interfaces;
77-
auto unlisted_state_interface = std::make_shared<hardware_interface::CommandInterface>(
78-
info_.joints[0].name, "some_unlisted_interface", nullptr);
79-
actuator_commands_.insert(
80-
std::make_pair(unlisted_state_interface->get_name(), unlisted_state_interface));
73+
std::vector<hardware_interface::InterfaceDescription> interfaces;
74+
hardware_interface::InterfaceInfo info;
75+
info.name = "some_unlisted_interface";
76+
hardware_interface::InterfaceDescription unlisted_state_interface(info_.joints[0].name, info);
8177
interfaces.push_back(unlisted_state_interface);
8278

8379
return interfaces;
@@ -100,14 +96,12 @@ class DummySensorDefault : public hardware_interface::SensorInterface
10096
{
10197
std::string get_name() const override { return "DummySensorDefault"; }
10298

103-
std::vector<std::shared_ptr<hardware_interface::StateInterface>> export_state_interfaces_2()
104-
override
99+
std::vector<hardware_interface::InterfaceDescription> export_state_interfaces_2() override
105100
{
106-
std::vector<std::shared_ptr<hardware_interface::StateInterface>> interfaces;
107-
auto unlisted_state_interface = std::make_shared<hardware_interface::StateInterface>(
108-
info_.sensors[0].name, "some_unlisted_interface", nullptr);
109-
sensor_states_.insert(
110-
std::make_pair(unlisted_state_interface->get_name(), unlisted_state_interface));
101+
std::vector<hardware_interface::InterfaceDescription> interfaces;
102+
hardware_interface::InterfaceInfo info;
103+
info.name = "some_unlisted_interface";
104+
hardware_interface::InterfaceDescription unlisted_state_interface(info_.sensors[0].name, info);
111105
interfaces.push_back(unlisted_state_interface);
112106

113107
return interfaces;
@@ -124,27 +118,23 @@ class DummySystemDefault : public hardware_interface::SystemInterface
124118
{
125119
std::string get_name() const override { return "DummySystemDefault"; }
126120

127-
std::vector<std::shared_ptr<hardware_interface::StateInterface>> export_state_interfaces_2()
128-
override
121+
std::vector<hardware_interface::InterfaceDescription> export_state_interfaces_2() override
129122
{
130-
std::vector<std::shared_ptr<hardware_interface::StateInterface>> interfaces;
131-
auto unlisted_state_interface = std::make_shared<hardware_interface::StateInterface>(
132-
info_.joints[0].name, "some_unlisted_interface", nullptr);
133-
system_states_.insert(
134-
std::make_pair(unlisted_state_interface->get_name(), unlisted_state_interface));
123+
std::vector<hardware_interface::InterfaceDescription> interfaces;
124+
hardware_interface::InterfaceInfo info;
125+
info.name = "some_unlisted_interface";
126+
hardware_interface::InterfaceDescription unlisted_state_interface(info_.joints[0].name, info);
135127
interfaces.push_back(unlisted_state_interface);
136128

137129
return interfaces;
138130
}
139131

140-
std::vector<std::shared_ptr<hardware_interface::CommandInterface>> export_command_interfaces_2()
141-
override
132+
std::vector<hardware_interface::InterfaceDescription> export_command_interfaces_2() override
142133
{
143-
std::vector<std::shared_ptr<hardware_interface::CommandInterface>> interfaces;
144-
auto unlisted_state_interface = std::make_shared<hardware_interface::CommandInterface>(
145-
info_.joints[0].name, "some_unlisted_interface", nullptr);
146-
system_commands_.insert(
147-
std::make_pair(unlisted_state_interface->get_name(), unlisted_state_interface));
134+
std::vector<hardware_interface::InterfaceDescription> interfaces;
135+
hardware_interface::InterfaceInfo info;
136+
info.name = "some_unlisted_interface";
137+
hardware_interface::InterfaceDescription unlisted_state_interface(info_.joints[0].name, info);
148138
interfaces.push_back(unlisted_state_interface);
149139

150140
return interfaces;

0 commit comments

Comments
 (0)