-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathtest_hardware_management_srvs.cpp
445 lines (408 loc) · 19.8 KB
/
test_hardware_management_srvs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Copyright 2022 Stogl Robotics Consulting UG (haftungsbeschränkt)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
#include <string>
#include <vector>
#include "controller_manager_test_common.hpp"
#include "controller_manager/controller_manager.hpp"
#include "controller_manager_msgs/msg/hardware_component_state.hpp"
#include "controller_manager_msgs/srv/set_hardware_component_state.hpp"
#include "hardware_interface/types/lifecycle_state_names.hpp"
#include "lifecycle_msgs/msg/state.hpp"
#include "rclcpp/parameter.hpp"
using ::testing::_;
using ::testing::Return;
using hardware_interface::lifecycle_state_names::ACTIVE;
using hardware_interface::lifecycle_state_names::FINALIZED;
using hardware_interface::lifecycle_state_names::INACTIVE;
using hardware_interface::lifecycle_state_names::UNCONFIGURED;
using hardware_interface::lifecycle_state_names::UNKNOWN;
using ros2_control_test_assets::TEST_ACTUATOR_HARDWARE_COMMAND_INTERFACES;
using ros2_control_test_assets::TEST_ACTUATOR_HARDWARE_NAME;
using ros2_control_test_assets::TEST_ACTUATOR_HARDWARE_PLUGIN_NAME;
using ros2_control_test_assets::TEST_ACTUATOR_HARDWARE_STATE_INTERFACES;
using ros2_control_test_assets::TEST_ACTUATOR_HARDWARE_TYPE;
using ros2_control_test_assets::TEST_SENSOR_HARDWARE_COMMAND_INTERFACES;
using ros2_control_test_assets::TEST_SENSOR_HARDWARE_NAME;
using ros2_control_test_assets::TEST_SENSOR_HARDWARE_PLUGIN_NAME;
using ros2_control_test_assets::TEST_SENSOR_HARDWARE_STATE_INTERFACES;
using ros2_control_test_assets::TEST_SENSOR_HARDWARE_TYPE;
using ros2_control_test_assets::TEST_SYSTEM_HARDWARE_COMMAND_INTERFACES;
using ros2_control_test_assets::TEST_SYSTEM_HARDWARE_NAME;
using ros2_control_test_assets::TEST_SYSTEM_HARDWARE_PLUGIN_NAME;
using ros2_control_test_assets::TEST_SYSTEM_HARDWARE_STATE_INTERFACES;
using ros2_control_test_assets::TEST_SYSTEM_HARDWARE_TYPE;
using LFC_STATE = lifecycle_msgs::msg::State;
using namespace std::chrono_literals; // NOLINT
class TestControllerManagerHWManagementSrvs : public TestControllerManagerSrvs
{
public:
void SetUp() override
{
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
cm_ = std::make_shared<controller_manager::ControllerManager>(executor_, TEST_CM_NAME);
run_updater_ = false;
SetUpSrvsCMExecutor();
cm_->set_parameter(rclcpp::Parameter(
"hardware_components_initial_state.unconfigured",
std::vector<std::string>({TEST_SYSTEM_HARDWARE_NAME})));
cm_->set_parameter(rclcpp::Parameter(
"hardware_components_initial_state.inactive",
std::vector<std::string>({TEST_SENSOR_HARDWARE_NAME})));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
auto msg = std_msgs::msg::String();
msg.data = ros2_control_test_assets::minimal_robot_urdf;
cm_->robot_description_callback(msg);
}
void check_component_fileds(
const controller_manager_msgs::msg::HardwareComponentState & component,
const std::string & name, const std::string & type, const std::string & plugin_name,
const uint8_t state_id, const std::string & state_label)
{
EXPECT_EQ(component.name, name) << "Component has unexpected name.";
EXPECT_EQ(component.type, type)
<< "Component " << name << " from plugin " << plugin_name << " has wrong type.";
EXPECT_EQ(component.plugin_name, plugin_name)
<< "Component " << name << " (" << type << ") has unexpected plugin_name.";
EXPECT_EQ(component.state.id, state_id)
<< "Component " << name << " (" << type << ") from plugin " << plugin_name
<< " has wrong state_id.";
EXPECT_EQ(component.state.label, state_label)
<< "Component " << name << " (" << type << ") from plugin " << plugin_name
<< " has wrong state_label.";
}
void list_hardware_components_and_check(
const std::vector<uint8_t> & hw_state_ids, const std::vector<std::string> & hw_state_labels,
const std::vector<std::vector<std::vector<bool>>> & hw_itfs_available_status,
const std::vector<std::vector<std::vector<bool>>> & hw_itfs_claimed_status)
{
rclcpp::executors::SingleThreadedExecutor srv_executor;
rclcpp::Node::SharedPtr list_srv_node = std::make_shared<rclcpp::Node>("list_srv_client");
srv_executor.add_node(list_srv_node);
rclcpp::Client<controller_manager_msgs::srv::ListHardwareComponents>::SharedPtr list_client =
list_srv_node->create_client<controller_manager_msgs::srv::ListHardwareComponents>(
std::string(TEST_CM_NAME) + "/list_hardware_components");
auto request =
std::make_shared<controller_manager_msgs::srv::ListHardwareComponents::Request>();
auto result = call_service_and_wait(*list_client, request, srv_executor);
auto check_interfaces =
[](
const std::vector<controller_manager_msgs::msg::HardwareInterface> & interfaces,
const std::vector<std::string> & interface_names,
const std::vector<bool> is_available_status, const std::vector<bool> is_claimed_status)
{
for (auto i = 0ul; i < interfaces.size(); ++i)
{
auto it = std::find(interface_names.begin(), interface_names.end(), interfaces[i].name);
EXPECT_NE(it, interface_names.end());
EXPECT_EQ(interfaces[i].is_available, is_available_status[i])
<< "At " << interfaces[i].name;
EXPECT_EQ(interfaces[i].is_claimed, is_claimed_status[i]) << "At " << interfaces[i].name;
}
};
for (const auto & component : result->component)
{
if (component.name == TEST_ACTUATOR_HARDWARE_NAME)
{
check_component_fileds(
component, TEST_ACTUATOR_HARDWARE_NAME, TEST_ACTUATOR_HARDWARE_TYPE,
TEST_ACTUATOR_HARDWARE_PLUGIN_NAME, hw_state_ids[0], hw_state_labels[0]);
check_interfaces(
component.command_interfaces, TEST_ACTUATOR_HARDWARE_COMMAND_INTERFACES,
hw_itfs_available_status[0][0], hw_itfs_claimed_status[0][0]);
check_interfaces(
component.state_interfaces, TEST_ACTUATOR_HARDWARE_STATE_INTERFACES,
hw_itfs_available_status[0][1], hw_itfs_claimed_status[0][1]);
}
if (component.name == TEST_SENSOR_HARDWARE_NAME)
{
check_component_fileds(
component, TEST_SENSOR_HARDWARE_NAME, TEST_SENSOR_HARDWARE_TYPE,
TEST_SENSOR_HARDWARE_PLUGIN_NAME, hw_state_ids[1], hw_state_labels[1]);
check_interfaces(
component.command_interfaces, TEST_SENSOR_HARDWARE_COMMAND_INTERFACES,
hw_itfs_available_status[1][0], hw_itfs_claimed_status[1][0]);
check_interfaces(
component.state_interfaces, TEST_SENSOR_HARDWARE_STATE_INTERFACES,
hw_itfs_available_status[1][1], hw_itfs_claimed_status[1][1]);
}
if (component.name == TEST_SYSTEM_HARDWARE_NAME)
{
check_component_fileds(
component, TEST_SYSTEM_HARDWARE_NAME, TEST_SYSTEM_HARDWARE_TYPE,
TEST_SYSTEM_HARDWARE_PLUGIN_NAME, hw_state_ids[2], hw_state_labels[2]);
check_interfaces(
component.command_interfaces, TEST_SYSTEM_HARDWARE_COMMAND_INTERFACES,
hw_itfs_available_status[2][0], hw_itfs_claimed_status[2][0]);
check_interfaces(
component.state_interfaces, TEST_SYSTEM_HARDWARE_STATE_INTERFACES,
hw_itfs_available_status[2][1], hw_itfs_claimed_status[2][1]);
}
}
}
bool set_hardware_component_state(
const std::string & hardware_name, const uint8_t target_state_id,
const std::string & target_state_label)
{
rclcpp::executors::SingleThreadedExecutor srv_executor;
rclcpp::Node::SharedPtr mha_srv_node = std::make_shared<rclcpp::Node>("mha_srv_client");
srv_executor.add_node(mha_srv_node);
rclcpp::Client<controller_manager_msgs::srv::SetHardwareComponentState>::SharedPtr mha_client =
mha_srv_node->create_client<controller_manager_msgs::srv::SetHardwareComponentState>(
std::string(TEST_CM_NAME) + "/set_hardware_component_state");
auto request =
std::make_shared<controller_manager_msgs::srv::SetHardwareComponentState::Request>();
request->name = hardware_name;
request->target_state.id = target_state_id;
request->target_state.label = target_state_label;
auto result = call_service_and_wait(*mha_client, request, srv_executor);
return result->ok;
}
};
TEST_F(TestControllerManagerHWManagementSrvs, list_hardware_components)
{
// Default status after start:
// checks if "hardware_components_initial_state.unconfigured" and
// "hardware_components_initial_state.inactive" are correctly read
list_hardware_components_and_check(
// actuator, sensor, system
std::vector<uint8_t>(
{LFC_STATE::PRIMARY_STATE_ACTIVE, LFC_STATE::PRIMARY_STATE_INACTIVE,
LFC_STATE::PRIMARY_STATE_UNCONFIGURED}),
std::vector<std::string>({ACTIVE, INACTIVE, UNCONFIGURED}),
std::vector<std::vector<std::vector<bool>>>({
// is available
{{true, true}, {true, true, true}}, // actuator
{{}, {true}}, // sensor
{{false, false, false, false}, {false, false, false, false, false, false, false}}, // system
}),
std::vector<std::vector<std::vector<bool>>>({
// is claimed
{{false, false}, {false, false, false}}, // actuator
{{}, {false}}, // sensor
{{false, false, false, false}, {false, false, false, false, false, false, false}}, // system
}));
}
// TODO(destogl): Add tests for testing controller starting/stopping depending on hw state
TEST_F(TestControllerManagerHWManagementSrvs, selective_activate_deactivate_components_set_state)
{
using lifecycle_msgs::msg::State;
// Default status after start
list_hardware_components_and_check(
// actuator, sensor, system
std::vector<uint8_t>(
{LFC_STATE::PRIMARY_STATE_ACTIVE, LFC_STATE::PRIMARY_STATE_INACTIVE,
LFC_STATE::PRIMARY_STATE_UNCONFIGURED}),
std::vector<std::string>({ACTIVE, INACTIVE, UNCONFIGURED}),
std::vector<std::vector<std::vector<bool>>>({
// is available
{{true, true}, {true, true, true}}, // actuator
{{}, {true}}, // sensor
{{false, false, false, false}, {false, false, false, false, false, false, false}}, // system
}),
std::vector<std::vector<std::vector<bool>>>({
// is claimed
{{false, false}, {false, false, false}}, // actuator
{{}, {false}}, // sensor
{{false, false, false, false}, {false, false, false, false, false, false, false}}, // system
}));
// Activate sensor
set_hardware_component_state(TEST_SENSOR_HARDWARE_NAME, State::PRIMARY_STATE_ACTIVE, "");
list_hardware_components_and_check(
// actuator, sensor, system
std::vector<uint8_t>(
{LFC_STATE::PRIMARY_STATE_ACTIVE, LFC_STATE::PRIMARY_STATE_ACTIVE,
LFC_STATE::PRIMARY_STATE_UNCONFIGURED}),
std::vector<std::string>({ACTIVE, ACTIVE, UNCONFIGURED}),
std::vector<std::vector<std::vector<bool>>>({
// is available
{{true, true}, {true, true, true}}, // actuator
{{}, {true}}, // sensor
{{false, false, false, false}, {false, false, false, false, false, false, false}}, // system
}),
std::vector<std::vector<std::vector<bool>>>({
// is claimed
{{false, false}, {false, false, false}}, // actuator
{{}, {false}}, // sensor
{{false, false, false, false}, {false, false, false, false, false, false, false}}, // system
}));
// Activate system directly - it should do configure automatically; ID is determined from label
set_hardware_component_state(TEST_SYSTEM_HARDWARE_NAME, 0, ACTIVE);
list_hardware_components_and_check(
// actuator, sensor, system
std::vector<uint8_t>(
{LFC_STATE::PRIMARY_STATE_ACTIVE, LFC_STATE::PRIMARY_STATE_ACTIVE,
LFC_STATE::PRIMARY_STATE_ACTIVE}),
std::vector<std::string>({ACTIVE, ACTIVE, ACTIVE}),
std::vector<std::vector<std::vector<bool>>>({
// is available
{{true, true}, {true, true, true}}, // actuator
{{}, {true}}, // sensor
{{true, true, true, true}, {true, true, true, true, true, true, true}}, // system
}),
std::vector<std::vector<std::vector<bool>>>({
// is claimed
{{false, false}, {false, false, false}}, // actuator
{{}, {false}}, // sensor
{{false, false, false, false}, {false, false, false, false, false, false, false}}, // system
}));
// Deactivate actuator
set_hardware_component_state(TEST_ACTUATOR_HARDWARE_NAME, 0, INACTIVE);
list_hardware_components_and_check(
// actuator, sensor, system
std::vector<uint8_t>(
{LFC_STATE::PRIMARY_STATE_INACTIVE, LFC_STATE::PRIMARY_STATE_ACTIVE,
LFC_STATE::PRIMARY_STATE_ACTIVE}),
std::vector<std::string>({INACTIVE, ACTIVE, ACTIVE}),
std::vector<std::vector<std::vector<bool>>>({
// is available
{{true, true}, {true, true, true}}, // actuator
{{}, {true}}, // sensor
{{true, true, true, true}, {true, true, true, true, true, true, true}}, // system
}),
std::vector<std::vector<std::vector<bool>>>({
// is claimed
{{false, false}, {false, false, false}}, // actuator
{{}, {false}}, // sensor
{{false, false, false, false}, {false, false, false, false, false, false, false}}, // system
}));
// Double activate system
set_hardware_component_state(TEST_SYSTEM_HARDWARE_NAME, LFC_STATE::PRIMARY_STATE_ACTIVE, ACTIVE);
list_hardware_components_and_check(
// actuator, sensor, system
std::vector<uint8_t>(
{LFC_STATE::PRIMARY_STATE_INACTIVE, LFC_STATE::PRIMARY_STATE_ACTIVE,
LFC_STATE::PRIMARY_STATE_ACTIVE}),
std::vector<std::string>({INACTIVE, ACTIVE, ACTIVE}),
std::vector<std::vector<std::vector<bool>>>({
// is available
{{true, true}, {true, true, true}}, // actuator
{{}, {true}}, // sensor
{{true, true, true, true}, {true, true, true, true, true, true, true}}, // system
}),
std::vector<std::vector<std::vector<bool>>>({
// is claimed
{{false, false}, {false, false, false}}, // actuator
{{}, {false}}, // sensor
{{false, false, false, false}, {false, false, false, false, false, false, false}}, // system
}));
// Set sensor to UNCONFIGURED - inactive and cleanup transitions has to be automatically done
set_hardware_component_state(
TEST_SENSOR_HARDWARE_NAME, LFC_STATE::PRIMARY_STATE_UNCONFIGURED, UNCONFIGURED);
list_hardware_components_and_check(
// actuator, sensor, system
std::vector<uint8_t>(
{LFC_STATE::PRIMARY_STATE_INACTIVE, LFC_STATE::PRIMARY_STATE_UNCONFIGURED,
LFC_STATE::PRIMARY_STATE_ACTIVE}),
std::vector<std::string>({INACTIVE, UNCONFIGURED, ACTIVE}),
std::vector<std::vector<std::vector<bool>>>({
// is available
{{true, true}, {true, true, true}}, // actuator
{{}, {false}}, // sensor
{{true, true, true, true}, {true, true, true, true, true, true, true}}, // system
}),
std::vector<std::vector<std::vector<bool>>>({
// is claimed
{{false, false}, {false, false, false}}, // actuator
{{}, {false}}, // sensor
{{false, false, false, false}, {false, false, false, false, false, false, false}}, // system
}));
}
class TestControllerManagerHWManagementSrvsNotLoaded : public TestControllerManagerHWManagementSrvs
{
public:
void SetUp() override
{
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
cm_ = std::make_shared<controller_manager::ControllerManager>(executor_, TEST_CM_NAME);
run_updater_ = false;
SetUpSrvsCMExecutor();
cm_->set_parameter(rclcpp::Parameter(
"hardware_components_initial_state.not_loaded",
std::vector<std::string>({TEST_SYSTEM_HARDWARE_NAME})));
cm_->set_parameter(rclcpp::Parameter(
"hardware_components_initial_state.inactive",
std::vector<std::string>({TEST_SENSOR_HARDWARE_NAME, TEST_ACTUATOR_HARDWARE_NAME})));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
auto msg = std_msgs::msg::String();
msg.data = ros2_control_test_assets::minimal_robot_urdf;
cm_->robot_description_callback(msg);
}
};
TEST_F(TestControllerManagerHWManagementSrvsNotLoaded, test_component_not_loaded)
{
// Default status after start:
// checks if "configure_components_on_start" and "activate_components_on_start" are correctly read
// there is not system loaded therefore test should not break having only two members for checking
// results
list_hardware_components_and_check(
// actuator, sensor, system (not existing)
std::vector<uint8_t>(
{LFC_STATE::PRIMARY_STATE_INACTIVE, LFC_STATE::PRIMARY_STATE_INACTIVE,
LFC_STATE::PRIMARY_STATE_UNKNOWN}),
std::vector<std::string>({INACTIVE, INACTIVE, UNKNOWN}),
std::vector<std::vector<std::vector<bool>>>({
// is available
{{true, true}, {true, true, true}}, // actuator
{{}, {true}}, // sensor
{{false, false, false, false}, {false, false, false, false, false, false, false}}, // system
}),
std::vector<std::vector<std::vector<bool>>>({
// is claimed
{{false, false}, {false, false, false}}, // actuator
{{}, {false}}, // sensor
{{false, false, false, false}, {false, false, false, false, false, false, false}}, // system
}));
}
class TestControllerManagerHWManagementSrvsWithoutParams
: public TestControllerManagerHWManagementSrvs
{
public:
void SetUp() override
{
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
cm_ = std::make_shared<controller_manager::ControllerManager>(executor_, TEST_CM_NAME);
run_updater_ = false;
auto msg = std_msgs::msg::String();
msg.data = ros2_control_test_assets::minimal_robot_urdf;
cm_->robot_description_callback(msg);
SetUpSrvsCMExecutor();
}
};
TEST_F(TestControllerManagerHWManagementSrvsWithoutParams, test_default_activation_of_all_hardware)
{
// "hardware_components_initial_state.unconfigured" and
// "hardware_components_initial_state.inactive" are not set (empty). Therefore, all hardware
// components are active.
list_hardware_components_and_check(
// actuator, sensor, system
std::vector<uint8_t>(
{LFC_STATE::PRIMARY_STATE_ACTIVE, LFC_STATE::PRIMARY_STATE_ACTIVE,
LFC_STATE::PRIMARY_STATE_ACTIVE}),
std::vector<std::string>({ACTIVE, ACTIVE, ACTIVE}),
std::vector<std::vector<std::vector<bool>>>({
// is available
{{true, true}, {true, true, true}}, // actuator
{{}, {true}}, // sensor
{{true, true, true, true}, {true, true, true, true, true, true, true}}, // system
}),
std::vector<std::vector<std::vector<bool>>>({
// is claimed
{{false, false}, {false, false, false}}, // actuator
{{}, {false}}, // sensor
{{false, false, false, false}, {false, false, false, false, false, false, false}}, // system
}));
}