-
Notifications
You must be signed in to change notification settings - Fork 282
/
test_forward_command_controller.cpp
442 lines (354 loc) · 15.8 KB
/
test_forward_command_controller.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
// Copyright 2020 PAL Robotics SL.
//
// 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 <functional>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "test_forward_command_controller.hpp"
#include "forward_command_controller/forward_command_controller.hpp"
#include "hardware_interface/loaned_command_interface.hpp"
#include "hardware_interface/types/hardware_interface_return_values.hpp"
#include "lifecycle_msgs/msg/state.hpp"
#include "rclcpp/node.hpp"
#include "rclcpp/qos.hpp"
#include "rclcpp/subscription.hpp"
#include "rclcpp/utilities.hpp"
#include "rclcpp/wait_set.hpp"
#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
using hardware_interface::LoanedCommandInterface;
using testing::IsEmpty;
using testing::SizeIs;
namespace
{
rclcpp::WaitResultKind wait_for(rclcpp::SubscriptionBase::SharedPtr subscription)
{
rclcpp::WaitSet wait_set;
wait_set.add_subscription(subscription);
const auto timeout = std::chrono::seconds(10);
return wait_set.wait(timeout).kind();
}
} // namespace
void ForwardCommandControllerTest::SetUpTestCase() { rclcpp::init(0, nullptr); }
void ForwardCommandControllerTest::TearDownTestCase() { rclcpp::shutdown(); }
void ForwardCommandControllerTest::SetUp()
{
// initialize controller
controller_ = std::make_unique<FriendForwardCommandController>();
}
void ForwardCommandControllerTest::TearDown() { controller_.reset(nullptr); }
void ForwardCommandControllerTest::SetUpController()
{
const auto result = controller_->init("forward_command_controller");
ASSERT_EQ(result, controller_interface::return_type::OK);
std::vector<LoanedCommandInterface> command_ifs;
command_ifs.emplace_back(joint_1_pos_cmd_);
command_ifs.emplace_back(joint_2_pos_cmd_);
command_ifs.emplace_back(joint_3_pos_cmd_);
controller_->assign_interfaces(std::move(command_ifs), {});
}
TEST_F(ForwardCommandControllerTest, JointsParameterNotSet)
{
SetUpController();
controller_->get_node()->set_parameter({"interface_name", ""});
// configure failed, 'joints' parameter not set
ASSERT_EQ(
controller_->on_configure(rclcpp_lifecycle::State()),
controller_interface::CallbackReturn::ERROR);
}
TEST_F(ForwardCommandControllerTest, InterfaceParameterNotSet)
{
SetUpController();
controller_->get_node()->set_parameter({"joints", std::vector<std::string>()});
// configure failed, 'interface_name' parameter not set
ASSERT_EQ(
controller_->on_configure(rclcpp_lifecycle::State()),
controller_interface::CallbackReturn::ERROR);
}
TEST_F(ForwardCommandControllerTest, JointsParameterIsEmpty)
{
SetUpController();
controller_->get_node()->set_parameter({"joints", std::vector<std::string>()});
controller_->get_node()->set_parameter({"interface_name", ""});
// configure failed, 'joints' is empty
ASSERT_EQ(
controller_->on_configure(rclcpp_lifecycle::State()),
controller_interface::CallbackReturn::ERROR);
}
TEST_F(ForwardCommandControllerTest, InterfaceParameterEmpty)
{
SetUpController();
controller_->get_node()->set_parameter({"joints", std::vector<std::string>{"joint1", "joint2"}});
controller_->get_node()->set_parameter({"interface_name", ""});
// configure failed, 'interface_name' is empty
ASSERT_EQ(
controller_->on_configure(rclcpp_lifecycle::State()),
controller_interface::CallbackReturn::ERROR);
}
TEST_F(ForwardCommandControllerTest, ConfigureParamsSuccess)
{
SetUpController();
controller_->get_node()->set_parameter({"joints", std::vector<std::string>{"joint1", "joint2"}});
controller_->get_node()->set_parameter({"interface_name", "position"});
// configure successful
ASSERT_EQ(
controller_->on_configure(rclcpp_lifecycle::State()),
controller_interface::CallbackReturn::SUCCESS);
// check interface configuration
auto cmd_if_conf = controller_->command_interface_configuration();
EXPECT_EQ(cmd_if_conf.type, controller_interface::interface_configuration_type::INDIVIDUAL);
ASSERT_THAT(cmd_if_conf.names, SizeIs(2lu));
auto state_if_conf = controller_->state_interface_configuration();
ASSERT_THAT(state_if_conf.names, IsEmpty());
}
TEST_F(ForwardCommandControllerTest, ActivateWithWrongJointsNamesFails)
{
SetUpController();
controller_->get_node()->set_parameter({"joints", std::vector<std::string>{"joint1", "joint4"}});
controller_->get_node()->set_parameter({"interface_name", "position"});
// activate failed, 'joint4' is not a valid joint name for the hardware
ASSERT_EQ(
controller_->on_configure(rclcpp_lifecycle::State()),
controller_interface::CallbackReturn::SUCCESS);
ASSERT_EQ(
controller_->on_activate(rclcpp_lifecycle::State()),
controller_interface::CallbackReturn::ERROR);
}
TEST_F(ForwardCommandControllerTest, ActivateWithWrongInterfaceNameFails)
{
SetUpController();
controller_->get_node()->set_parameter({"joints", joint_names_});
controller_->get_node()->set_parameter({"interface_name", "acceleration"});
// activate failed, 'acceleration' is not a registered interface for `joint1`
ASSERT_EQ(
controller_->on_configure(rclcpp_lifecycle::State()),
controller_interface::CallbackReturn::SUCCESS);
ASSERT_EQ(
controller_->on_activate(rclcpp_lifecycle::State()),
controller_interface::CallbackReturn::ERROR);
}
TEST_F(ForwardCommandControllerTest, ActivateSuccess)
{
SetUpController();
controller_->get_node()->set_parameter({"joints", joint_names_});
controller_->get_node()->set_parameter({"interface_name", "position"});
// activate successful
ASSERT_EQ(
controller_->on_configure(rclcpp_lifecycle::State()),
controller_interface::CallbackReturn::SUCCESS);
// check interface configuration
auto cmd_if_conf = controller_->command_interface_configuration();
EXPECT_EQ(cmd_if_conf.type, controller_interface::interface_configuration_type::INDIVIDUAL);
ASSERT_THAT(cmd_if_conf.names, SizeIs(joint_names_.size()));
auto state_if_conf = controller_->state_interface_configuration();
ASSERT_THAT(state_if_conf.names, IsEmpty());
ASSERT_EQ(
controller_->on_activate(rclcpp_lifecycle::State()),
controller_interface::CallbackReturn::SUCCESS);
// check interface configuration
cmd_if_conf = controller_->command_interface_configuration();
ASSERT_THAT(cmd_if_conf.names, SizeIs(joint_names_.size()));
state_if_conf = controller_->state_interface_configuration();
ASSERT_THAT(state_if_conf.names, IsEmpty());
}
TEST_F(ForwardCommandControllerTest, CommandSuccessTest)
{
SetUpController();
// configure controller
controller_->get_node()->set_parameter({"joints", joint_names_});
controller_->get_node()->set_parameter({"interface_name", "position"});
ASSERT_EQ(
controller_->on_configure(rclcpp_lifecycle::State()),
controller_interface::CallbackReturn::SUCCESS);
// update successful though no command has been send yet
ASSERT_EQ(
controller_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)),
controller_interface::return_type::OK);
// check joint commands are still the default ones
ASSERT_EQ(joint_1_pos_cmd_.get_value(), 1.1);
ASSERT_EQ(joint_2_pos_cmd_.get_value(), 2.1);
ASSERT_EQ(joint_3_pos_cmd_.get_value(), 3.1);
// send command
auto command_ptr = std::make_shared<forward_command_controller::CmdType>();
command_ptr->data = {10.0, 20.0, 30.0};
controller_->rt_command_ptr_.writeFromNonRT(command_ptr);
// update successful, command received
ASSERT_EQ(
controller_->update(rclcpp::Time(100000000), rclcpp::Duration::from_seconds(0.01)),
controller_interface::return_type::OK);
// check joint commands have been modified
ASSERT_EQ(joint_1_pos_cmd_.get_value(), 10.0);
ASSERT_EQ(joint_2_pos_cmd_.get_value(), 20.0);
ASSERT_EQ(joint_3_pos_cmd_.get_value(), 30.0);
}
TEST_F(ForwardCommandControllerTest, WrongCommandCheckTest)
{
SetUpController();
// configure controller
controller_->get_node()->set_parameter({"joints", joint_names_});
controller_->get_node()->set_parameter({"interface_name", "position"});
ASSERT_EQ(
controller_->on_configure(rclcpp_lifecycle::State()),
controller_interface::CallbackReturn::SUCCESS);
// send command with wrong number of joints
auto command_ptr = std::make_shared<forward_command_controller::CmdType>();
command_ptr->data = {10.0, 20.0};
controller_->rt_command_ptr_.writeFromNonRT(command_ptr);
// update failed, command size does not match number of joints
ASSERT_EQ(
controller_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)),
controller_interface::return_type::ERROR);
// check joint commands are still the default ones
ASSERT_EQ(joint_1_pos_cmd_.get_value(), 1.1);
ASSERT_EQ(joint_2_pos_cmd_.get_value(), 2.1);
ASSERT_EQ(joint_3_pos_cmd_.get_value(), 3.1);
}
TEST_F(ForwardCommandControllerTest, NoCommandCheckTest)
{
SetUpController();
// configure controller
controller_->get_node()->set_parameter({"joints", joint_names_});
controller_->get_node()->set_parameter({"interface_name", "position"});
ASSERT_EQ(
controller_->on_configure(rclcpp_lifecycle::State()),
controller_interface::CallbackReturn::SUCCESS);
// update successful, no command received yet
ASSERT_EQ(
controller_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)),
controller_interface::return_type::OK);
// check joint commands are still the default ones
ASSERT_EQ(joint_1_pos_cmd_.get_value(), 1.1);
ASSERT_EQ(joint_2_pos_cmd_.get_value(), 2.1);
ASSERT_EQ(joint_3_pos_cmd_.get_value(), 3.1);
}
TEST_F(ForwardCommandControllerTest, CommandCallbackTest)
{
SetUpController();
controller_->get_node()->set_parameter({"joints", joint_names_});
controller_->get_node()->set_parameter({"interface_name", "position"});
// default values
ASSERT_EQ(joint_1_pos_cmd_.get_value(), 1.1);
ASSERT_EQ(joint_2_pos_cmd_.get_value(), 2.1);
ASSERT_EQ(joint_3_pos_cmd_.get_value(), 3.1);
auto node_state = controller_->get_node()->configure();
ASSERT_EQ(node_state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE);
node_state = controller_->get_node()->activate();
ASSERT_EQ(node_state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
// send a new command
rclcpp::Node test_node("test_node");
auto command_pub = test_node.create_publisher<std_msgs::msg::Float64MultiArray>(
std::string(controller_->get_node()->get_name()) + "/commands", rclcpp::SystemDefaultsQoS());
std_msgs::msg::Float64MultiArray command_msg;
command_msg.data = {10.0, 20.0, 30.0};
command_pub->publish(command_msg);
// wait for command message to be passed
ASSERT_EQ(wait_for(controller_->joints_command_subscriber_), rclcpp::WaitResultKind::Ready);
// process callbacks
rclcpp::spin_some(controller_->get_node()->get_node_base_interface());
// update successful
ASSERT_EQ(
controller_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)),
controller_interface::return_type::OK);
// check command in handle was set
ASSERT_EQ(joint_1_pos_cmd_.get_value(), 10.0);
ASSERT_EQ(joint_2_pos_cmd_.get_value(), 20.0);
ASSERT_EQ(joint_3_pos_cmd_.get_value(), 30.0);
}
TEST_F(ForwardCommandControllerTest, ActivateDeactivateCommandsResetSuccess)
{
SetUpController();
controller_->get_node()->set_parameter({"joints", joint_names_});
controller_->get_node()->set_parameter({"interface_name", "position"});
// default values
ASSERT_EQ(joint_1_pos_cmd_.get_value(), 1.1);
ASSERT_EQ(joint_2_pos_cmd_.get_value(), 2.1);
ASSERT_EQ(joint_3_pos_cmd_.get_value(), 3.1);
auto node_state = controller_->configure();
ASSERT_EQ(node_state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE);
// check interface configuration
auto cmd_if_conf = controller_->command_interface_configuration();
ASSERT_THAT(cmd_if_conf.names, SizeIs(joint_names_.size()));
EXPECT_EQ(cmd_if_conf.type, controller_interface::interface_configuration_type::INDIVIDUAL);
auto state_if_conf = controller_->state_interface_configuration();
ASSERT_THAT(state_if_conf.names, IsEmpty());
node_state = controller_->get_node()->activate();
ASSERT_EQ(node_state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
// check interface configuration
cmd_if_conf = controller_->command_interface_configuration();
ASSERT_THAT(cmd_if_conf.names, SizeIs(joint_names_.size()));
state_if_conf = controller_->state_interface_configuration();
ASSERT_THAT(state_if_conf.names, IsEmpty());
auto command_msg = std::make_shared<std_msgs::msg::Float64MultiArray>();
command_msg->data = {10.0, 20.0, 30.0};
controller_->rt_command_ptr_.writeFromNonRT(command_msg);
// update successful
ASSERT_EQ(
controller_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)),
controller_interface::return_type::OK);
// check command in handle was set
ASSERT_EQ(joint_1_pos_cmd_.get_value(), 10);
ASSERT_EQ(joint_2_pos_cmd_.get_value(), 20);
ASSERT_EQ(joint_3_pos_cmd_.get_value(), 30);
node_state = controller_->get_node()->deactivate();
ASSERT_EQ(node_state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE);
// check interface configuration
cmd_if_conf = controller_->command_interface_configuration();
ASSERT_THAT(cmd_if_conf.names, SizeIs(joint_names_.size())); // did not change
state_if_conf = controller_->state_interface_configuration();
ASSERT_THAT(state_if_conf.names, IsEmpty());
// command ptr should be reset (nullptr) after deactivation - same check as in `update`
ASSERT_FALSE(
controller_->rt_command_ptr_.readFromNonRT() &&
*(controller_->rt_command_ptr_.readFromNonRT()));
ASSERT_FALSE(
controller_->rt_command_ptr_.readFromRT() && *(controller_->rt_command_ptr_.readFromRT()));
// Controller is inactive but let's put some data into buffer (simulate callback when inactive)
command_msg = std::make_shared<std_msgs::msg::Float64MultiArray>();
command_msg->data = {5.5, 6.6, 7.7};
controller_->rt_command_ptr_.writeFromNonRT(command_msg);
// command ptr should be available and message should be there - same check as in `update`
ASSERT_TRUE(
controller_->rt_command_ptr_.readFromNonRT() &&
*(controller_->rt_command_ptr_.readFromNonRT()));
ASSERT_TRUE(
controller_->rt_command_ptr_.readFromRT() && *(controller_->rt_command_ptr_.readFromRT()));
// Now activate again
node_state = controller_->get_node()->activate();
ASSERT_EQ(node_state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
// command ptr should be reset (nullptr) after activation - same check as in `update`
ASSERT_FALSE(
controller_->rt_command_ptr_.readFromNonRT() &&
*(controller_->rt_command_ptr_.readFromNonRT()));
ASSERT_FALSE(
controller_->rt_command_ptr_.readFromRT() && *(controller_->rt_command_ptr_.readFromRT()));
// update successful
ASSERT_EQ(
controller_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)),
controller_interface::return_type::OK);
// values should not change
ASSERT_EQ(joint_1_pos_cmd_.get_value(), 10);
ASSERT_EQ(joint_2_pos_cmd_.get_value(), 20);
ASSERT_EQ(joint_3_pos_cmd_.get_value(), 30);
// set commands again
controller_->rt_command_ptr_.writeFromNonRT(command_msg);
// update successful
ASSERT_EQ(
controller_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)),
controller_interface::return_type::OK);
// check command in handle was set
ASSERT_EQ(joint_1_pos_cmd_.get_value(), 5.5);
ASSERT_EQ(joint_2_pos_cmd_.get_value(), 6.6);
ASSERT_EQ(joint_3_pos_cmd_.get_value(), 7.7);
}