forked from ros-controls/ros2_controllers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_trajectory_actions.cpp
More file actions
783 lines (658 loc) · 26.1 KB
/
test_trajectory_actions.cpp
File metadata and controls
783 lines (658 loc) · 26.1 KB
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
// Copyright 2020 Open Source Robotics Foundation, Inc.
//
// 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.
#ifndef _MSC_VER
#include <cxxabi.h>
#endif
#include <algorithm>
#include <chrono>
#include <functional>
#include <future>
#include <memory>
#include <ratio>
#include <stdexcept>
#include <string>
#include <system_error>
#include <thread>
#include <vector>
#include "action_msgs/msg/goal_status_array.hpp"
#include "control_msgs/action/detail/follow_joint_trajectory__struct.hpp"
#include "controller_interface/controller_interface.hpp"
#include "gtest/gtest.h"
#include "hardware_interface/resource_manager.hpp"
#include "joint_trajectory_controller/joint_trajectory_controller.hpp"
#include "rclcpp/clock.hpp"
#include "rclcpp/duration.hpp"
#include "rclcpp/executors/multi_threaded_executor.hpp"
#include "rclcpp/logging.hpp"
#include "rclcpp/node.hpp"
#include "rclcpp/parameter.hpp"
#include "rclcpp/time.hpp"
#include "rclcpp/utilities.hpp"
#include "rclcpp_action/client.hpp"
#include "rclcpp_action/client_goal_handle.hpp"
#include "rclcpp_action/create_client.hpp"
#include "rclcpp_lifecycle/lifecycle_node.hpp"
#include "test_trajectory_controller_utils.hpp"
#include "trajectory_msgs/msg/joint_trajectory.hpp"
#include "trajectory_msgs/msg/joint_trajectory_point.hpp"
using std::placeholders::_1;
using std::placeholders::_2;
using test_trajectory_controllers::TestableJointTrajectoryController;
using test_trajectory_controllers::TrajectoryControllerTest;
using trajectory_msgs::msg::JointTrajectoryPoint;
class TestTrajectoryActions : public TrajectoryControllerTest
{
protected:
void SetUp()
{
TrajectoryControllerTest::SetUp();
goal_options_.result_callback =
std::bind(&TestTrajectoryActions::common_result_response, this, _1);
goal_options_.feedback_callback = nullptr;
}
void SetUpExecutor(
const std::vector<rclcpp::Parameter> & parameters = {},
bool separate_cmd_and_state_values = false, double kp = 0.0)
{
setup_executor_ = true;
SetUpAndActivateTrajectoryController(executor_, parameters, separate_cmd_and_state_values, kp);
SetUpActionClient();
executor_.add_node(node_->get_node_base_interface());
executor_future_handle_ = std::async(std::launch::async, [&]() -> void { executor_.spin(); });
}
void SetUpControllerHardware()
{
setup_controller_hw_ = true;
controller_hw_thread_ = std::thread(
[&]()
{
// controller hardware cycle update loop
auto clock = rclcpp::Clock(RCL_STEADY_TIME);
auto start_time = clock.now();
rclcpp::Duration wait = rclcpp::Duration::from_seconds(2.0);
auto end_time = start_time + wait;
while (clock.now() < end_time)
{
traj_controller_->update(clock.now(), clock.now() - start_time);
}
});
// sometimes doesn't receive calls when we don't sleep
std::this_thread::sleep_for(std::chrono::milliseconds(300));
}
void SetUpActionClient()
{
action_client_ = rclcpp_action::create_client<control_msgs::action::FollowJointTrajectory>(
node_->get_node_base_interface(), node_->get_node_graph_interface(),
node_->get_node_logging_interface(), node_->get_node_waitables_interface(),
controller_name_ + "/follow_joint_trajectory");
bool response = action_client_->wait_for_action_server(std::chrono::seconds(1));
if (!response)
{
throw std::runtime_error("could not get action server");
}
}
static void TearDownTestCase() { rclcpp::shutdown(); }
void TearDown()
{
TearDownControllerHardware();
TearDownExecutor();
}
void TearDownExecutor()
{
if (setup_executor_)
{
setup_executor_ = false;
executor_.cancel();
executor_future_handle_.wait();
}
}
void TearDownControllerHardware()
{
if (setup_controller_hw_)
{
setup_controller_hw_ = false;
if (controller_hw_thread_.joinable())
{
controller_hw_thread_.join();
}
}
}
using FollowJointTrajectoryMsg = control_msgs::action::FollowJointTrajectory;
using GoalHandle = rclcpp_action::ClientGoalHandle<FollowJointTrajectoryMsg>;
using GoalOptions = rclcpp_action::Client<FollowJointTrajectoryMsg>::SendGoalOptions;
std::shared_future<typename GoalHandle::SharedPtr> sendActionGoal(
const std::vector<JointTrajectoryPoint> & points, double timeout, const GoalOptions & opt)
{
control_msgs::action::FollowJointTrajectory_Goal goal_msg;
goal_msg.goal_time_tolerance = rclcpp::Duration::from_seconds(timeout);
goal_msg.trajectory.joint_names = joint_names_;
goal_msg.trajectory.points = points;
return action_client_->async_send_goal(goal_msg, opt);
}
rclcpp_action::Client<FollowJointTrajectoryMsg>::SharedPtr action_client_;
rclcpp_action::ResultCode common_resultcode_ = rclcpp_action::ResultCode::UNKNOWN;
int common_action_result_code_ = control_msgs::action::FollowJointTrajectory_Result::SUCCESSFUL;
bool setup_executor_ = false;
rclcpp::executors::MultiThreadedExecutor executor_;
std::future<void> executor_future_handle_;
bool setup_controller_hw_ = false;
std::thread controller_hw_thread_;
GoalOptions goal_options_;
public:
void common_result_response(const GoalHandle::WrappedResult & result)
{
RCLCPP_DEBUG(
node_->get_logger(), "common_result_response time: %f", rclcpp::Clock().now().seconds());
common_resultcode_ = result.code;
common_action_result_code_ = result.result->error_code;
switch (result.code)
{
case rclcpp_action::ResultCode::SUCCEEDED:
break;
case rclcpp_action::ResultCode::ABORTED:
RCLCPP_DEBUG(node_->get_logger(), "Goal was aborted");
return;
case rclcpp_action::ResultCode::CANCELED:
RCLCPP_DEBUG(node_->get_logger(), "Goal was canceled");
return;
default:
RCLCPP_DEBUG(node_->get_logger(), "Unknown result code");
return;
}
}
};
// From the tutorial: https://www.sandordargo.com/blog/2019/04/24/parameterized-testing-with-gtest
class TestTrajectoryActionsTestParameterized
: public TestTrajectoryActions,
public ::testing::WithParamInterface<
std::tuple<std::vector<std::string>, std::vector<std::string>>>
{
public:
virtual void SetUp()
{
TestTrajectoryActions::SetUp();
command_interface_types_ = std::get<0>(GetParam());
state_interface_types_ = std::get<1>(GetParam());
}
static void TearDownTestCase() { TrajectoryControllerTest::TearDownTestCase(); }
};
TEST_P(TestTrajectoryActionsTestParameterized, test_success_single_point_sendgoal)
{
SetUpExecutor();
SetUpControllerHardware();
std::shared_future<typename GoalHandle::SharedPtr> gh_future;
// send goal
std::vector<double> point_positions{1.0, 2.0, 3.0};
{
std::vector<JointTrajectoryPoint> points;
JointTrajectoryPoint point;
point.time_from_start = rclcpp::Duration::from_seconds(0.5);
point.positions.resize(joint_names_.size());
point.positions = point_positions;
points.push_back(point);
gh_future = sendActionGoal(points, 1.0, goal_options_);
}
controller_hw_thread_.join();
EXPECT_TRUE(gh_future.get());
EXPECT_EQ(rclcpp_action::ResultCode::SUCCEEDED, common_resultcode_);
// run an update
updateControllerAsync(rclcpp::Duration::from_seconds(0.01));
// it should be holding the last position goal
// i.e., active but trivial trajectory (one point only)
// note: the action goal also is a trivial trajectory
if (traj_controller_->has_position_command_interface())
{
expectHoldingPoint(point_positions);
}
else
{
// no integration to position state interface from velocity/acceleration
expectHoldingPoint(INITIAL_POS_JOINTS);
}
}
TEST_P(TestTrajectoryActionsTestParameterized, test_success_multi_point_sendgoal)
{
SetUpExecutor();
SetUpControllerHardware();
// add feedback
bool feedback_recv = false;
goal_options_.feedback_callback =
[&](
rclcpp_action::ClientGoalHandle<FollowJointTrajectoryMsg>::SharedPtr,
const std::shared_ptr<const FollowJointTrajectoryMsg::Feedback>) { feedback_recv = true; };
std::shared_future<typename GoalHandle::SharedPtr> gh_future;
// send goal with multiple points
std::vector<std::vector<double>> points_positions{{{4.0, 5.0, 6.0}}, {{7.0, 8.0, 9.0}}};
{
std::vector<JointTrajectoryPoint> points;
JointTrajectoryPoint point1;
point1.time_from_start = rclcpp::Duration::from_seconds(0.2);
point1.positions.resize(joint_names_.size());
point1.positions = points_positions.at(0);
points.push_back(point1);
JointTrajectoryPoint point2;
point2.time_from_start = rclcpp::Duration::from_seconds(0.3);
point2.positions.resize(joint_names_.size());
point2.positions = points_positions.at(1);
points.push_back(point2);
gh_future = sendActionGoal(points, 1.0, goal_options_);
}
controller_hw_thread_.join();
EXPECT_TRUE(feedback_recv);
EXPECT_TRUE(gh_future.get());
EXPECT_EQ(rclcpp_action::ResultCode::SUCCEEDED, common_resultcode_);
// run an update
updateControllerAsync(rclcpp::Duration::from_seconds(0.01));
// it should be holding the last position goal
// i.e., active but trivial trajectory (one point only)
if (traj_controller_->has_position_command_interface())
{
expectHoldingPoint(points_positions.at(1));
}
else
{
// no integration to position state interface from velocity/acceleration
expectHoldingPoint(INITIAL_POS_JOINTS);
}
}
/**
* Makes sense with position command interface only,
* because no integration to position state interface is implemented
*/
TEST_F(TestTrajectoryActions, test_goal_tolerances_single_point_success)
{
// set tolerance parameters
std::vector<rclcpp::Parameter> params = {
rclcpp::Parameter("constraints.joint1.goal", 0.1),
rclcpp::Parameter("constraints.joint2.goal", 0.1),
rclcpp::Parameter("constraints.joint3.goal", 0.1)};
SetUpExecutor(params);
SetUpControllerHardware();
std::shared_future<typename GoalHandle::SharedPtr> gh_future;
// send goal
std::vector<std::vector<double>> points_positions{{{1.0, 2.0, 3.0}}};
{
std::vector<JointTrajectoryPoint> points;
JointTrajectoryPoint point;
point.time_from_start = rclcpp::Duration::from_seconds(0.5);
point.positions.resize(joint_names_.size());
point.positions = points_positions.at(0);
points.push_back(point);
gh_future = sendActionGoal(points, 1.0, goal_options_);
}
controller_hw_thread_.join();
EXPECT_TRUE(gh_future.get());
EXPECT_EQ(rclcpp_action::ResultCode::SUCCEEDED, common_resultcode_);
EXPECT_EQ(
control_msgs::action::FollowJointTrajectory_Result::SUCCESSFUL, common_action_result_code_);
// run an update
updateControllerAsync(rclcpp::Duration::from_seconds(0.01));
// it should be holding the last position goal
// i.e., active but trivial trajectory (one point only)
expectHoldingPoint(points_positions.at(0));
}
/**
* Makes sense with position command interface only,
* because no integration to position state interface is implemented
*/
TEST_F(TestTrajectoryActions, test_goal_tolerances_multi_point_success)
{
// set tolerance parameters
std::vector<rclcpp::Parameter> params = {
rclcpp::Parameter("constraints.joint1.goal", 0.1),
rclcpp::Parameter("constraints.joint2.goal", 0.1),
rclcpp::Parameter("constraints.joint3.goal", 0.1)};
SetUpExecutor(params);
SetUpControllerHardware();
// add feedback
bool feedback_recv = false;
goal_options_.feedback_callback =
[&](
rclcpp_action::ClientGoalHandle<FollowJointTrajectoryMsg>::SharedPtr,
const std::shared_ptr<const FollowJointTrajectoryMsg::Feedback>) { feedback_recv = true; };
std::shared_future<typename GoalHandle::SharedPtr> gh_future;
// send goal with multiple points
std::vector<std::vector<double>> points_positions{{{4.0, 5.0, 6.0}}, {{7.0, 8.0, 9.0}}};
{
std::vector<JointTrajectoryPoint> points;
JointTrajectoryPoint point1;
point1.time_from_start = rclcpp::Duration::from_seconds(0.2);
point1.positions.resize(joint_names_.size());
point1.positions = points_positions.at(0);
points.push_back(point1);
JointTrajectoryPoint point2;
point2.time_from_start = rclcpp::Duration::from_seconds(0.3);
point2.positions.resize(joint_names_.size());
point2.positions = points_positions.at(1);
points.push_back(point2);
gh_future = sendActionGoal(points, 1.0, goal_options_);
}
controller_hw_thread_.join();
EXPECT_TRUE(feedback_recv);
EXPECT_TRUE(gh_future.get());
EXPECT_EQ(rclcpp_action::ResultCode::SUCCEEDED, common_resultcode_);
EXPECT_EQ(
control_msgs::action::FollowJointTrajectory_Result::SUCCESSFUL, common_action_result_code_);
// run an update
updateControllerAsync(rclcpp::Duration::from_seconds(0.01));
// it should be holding the last position goal
// i.e., active but trivial trajectory (one point only)
expectHoldingPoint(points_positions.at(1));
}
TEST_P(TestTrajectoryActionsTestParameterized, test_state_tolerances_fail)
{
// set joint tolerance parameters
const double state_tol = 0.0001;
std::vector<rclcpp::Parameter> params = {
rclcpp::Parameter("constraints.joint1.trajectory", state_tol),
rclcpp::Parameter("constraints.joint2.trajectory", state_tol),
rclcpp::Parameter("constraints.joint3.trajectory", state_tol)};
// separate command from states -> immediate state tolerance fail
bool separate_cmd_and_state_values = true;
SetUpExecutor(params, separate_cmd_and_state_values);
SetUpControllerHardware();
std::shared_future<typename GoalHandle::SharedPtr> gh_future;
// send goal
std::vector<std::vector<double>> points_positions{{{4.0, 5.0, 6.0}}, {{7.0, 8.0, 9.0}}};
{
std::vector<JointTrajectoryPoint> points;
JointTrajectoryPoint point1;
point1.time_from_start = rclcpp::Duration::from_seconds(0.0);
point1.positions.resize(joint_names_.size());
point1.positions = points_positions.at(0);
points.push_back(point1);
JointTrajectoryPoint point2;
point2.time_from_start = rclcpp::Duration::from_seconds(0.1);
point2.positions.resize(joint_names_.size());
point2.positions = points_positions.at(1);
points.push_back(point2);
gh_future = sendActionGoal(points, 1.0, goal_options_);
}
controller_hw_thread_.join();
EXPECT_TRUE(gh_future.get());
EXPECT_EQ(rclcpp_action::ResultCode::ABORTED, common_resultcode_);
EXPECT_EQ(
control_msgs::action::FollowJointTrajectory_Result::PATH_TOLERANCE_VIOLATED,
common_action_result_code_);
// run an update
updateControllerAsync(rclcpp::Duration::from_seconds(0.01));
// it should be holding the position (being the initial one)
// i.e., active but trivial trajectory (one point only)
std::vector<double> initial_positions{INITIAL_POS_JOINT1, INITIAL_POS_JOINT2, INITIAL_POS_JOINT3};
expectHoldingPoint(initial_positions);
}
TEST_P(TestTrajectoryActionsTestParameterized, test_goal_tolerances_fail)
{
// set joint tolerance parameters
const double goal_tol = 0.1;
// set very small goal_time so that goal_time is violated
const double goal_time = 0.000001;
std::vector<rclcpp::Parameter> params = {
rclcpp::Parameter("constraints.joint1.goal", goal_tol),
rclcpp::Parameter("constraints.joint2.goal", goal_tol),
rclcpp::Parameter("constraints.joint3.goal", goal_tol),
rclcpp::Parameter("constraints.goal_time", goal_time)};
// separate command from states -> goal won't never be reached
bool separate_cmd_and_state_values = true;
SetUpExecutor(params, separate_cmd_and_state_values);
SetUpControllerHardware();
std::shared_future<typename GoalHandle::SharedPtr> gh_future;
// send goal; one point only -> command is directly set to reach this goal (no interpolation)
{
std::vector<JointTrajectoryPoint> points;
JointTrajectoryPoint point;
point.time_from_start = rclcpp::Duration::from_seconds(0.0);
point.positions.resize(joint_names_.size());
point.positions[0] = 4.0;
point.positions[1] = 5.0;
point.positions[2] = 6.0;
points.push_back(point);
gh_future = sendActionGoal(points, 1.0, goal_options_);
}
controller_hw_thread_.join();
EXPECT_TRUE(gh_future.get());
EXPECT_EQ(rclcpp_action::ResultCode::ABORTED, common_resultcode_);
EXPECT_EQ(
control_msgs::action::FollowJointTrajectory_Result::GOAL_TOLERANCE_VIOLATED,
common_action_result_code_);
// run an update
updateControllerAsync(rclcpp::Duration::from_seconds(0.01));
// it should be holding the position (being the initial one)
// i.e., active but trivial trajectory (one point only)
std::vector<double> initial_positions{INITIAL_POS_JOINT1, INITIAL_POS_JOINT2, INITIAL_POS_JOINT3};
expectHoldingPoint(initial_positions);
}
TEST_P(TestTrajectoryActionsTestParameterized, test_no_time_from_start_state_tolerance_fail)
{
// set joint tolerance parameters
const double state_tol = 0.0001;
std::vector<rclcpp::Parameter> params = {
rclcpp::Parameter("constraints.joint1.trajectory", state_tol),
rclcpp::Parameter("constraints.joint2.trajectory", state_tol),
rclcpp::Parameter("constraints.joint3.trajectory", state_tol)};
// separate command from states -> goal won't never be reached
bool separate_cmd_and_state_values = true;
SetUpExecutor(params, separate_cmd_and_state_values);
SetUpControllerHardware();
std::shared_future<typename GoalHandle::SharedPtr> gh_future;
// send goal
{
std::vector<JointTrajectoryPoint> points;
JointTrajectoryPoint point;
point.time_from_start = rclcpp::Duration::from_seconds(0.0);
point.positions.resize(joint_names_.size());
point.positions[0] = 4.0;
point.positions[1] = 5.0;
point.positions[2] = 6.0;
points.push_back(point);
gh_future = sendActionGoal(points, 1.0, goal_options_);
}
controller_hw_thread_.join();
EXPECT_TRUE(gh_future.get());
EXPECT_EQ(rclcpp_action::ResultCode::ABORTED, common_resultcode_);
EXPECT_EQ(
control_msgs::action::FollowJointTrajectory_Result::PATH_TOLERANCE_VIOLATED,
common_action_result_code_);
// run an update
updateControllerAsync(rclcpp::Duration::from_seconds(0.01));
// it should be holding the position (being the initial one)
// i.e., active but trivial trajectory (one point only)
std::vector<double> initial_positions{INITIAL_POS_JOINT1, INITIAL_POS_JOINT2, INITIAL_POS_JOINT3};
expectHoldingPoint(initial_positions);
}
TEST_P(TestTrajectoryActionsTestParameterized, test_cancel_hold_position)
{
SetUpExecutor();
SetUpControllerHardware();
std::shared_future<typename GoalHandle::SharedPtr> gh_future;
// send goal
{
std::vector<JointTrajectoryPoint> points;
JointTrajectoryPoint point;
point.time_from_start = rclcpp::Duration::from_seconds(1.0);
point.positions.resize(joint_names_.size());
point.positions[0] = 4.0;
point.positions[1] = 5.0;
point.positions[2] = 6.0;
points.push_back(point);
control_msgs::action::FollowJointTrajectory_Goal goal_msg;
goal_msg.goal_time_tolerance = rclcpp::Duration::from_seconds(2.0);
goal_msg.trajectory.joint_names = joint_names_;
goal_msg.trajectory.points = points;
// send and wait for half a second before cancel
gh_future = action_client_->async_send_goal(goal_msg, goal_options_);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
const auto goal_handle = gh_future.get();
action_client_->async_cancel_goal(goal_handle);
}
controller_hw_thread_.join();
EXPECT_TRUE(gh_future.get());
EXPECT_EQ(rclcpp_action::ResultCode::CANCELED, common_resultcode_);
EXPECT_EQ(
control_msgs::action::FollowJointTrajectory_Result::SUCCESSFUL, common_action_result_code_);
std::vector<double> cancelled_position{joint_pos_[0], joint_pos_[1], joint_pos_[2]};
// run an update
updateControllerAsync(rclcpp::Duration::from_seconds(0.01));
// it should be holding the last position,
// i.e., active but trivial trajectory (one point only)
expectHoldingPoint(cancelled_position);
}
TEST_P(TestTrajectoryActionsTestParameterized, test_allow_nonzero_velocity_at_trajectory_end_true)
{
std::vector<rclcpp::Parameter> params = {
rclcpp::Parameter("allow_nonzero_velocity_at_trajectory_end", true)};
SetUpExecutor(params);
SetUpControllerHardware();
std::shared_future<typename GoalHandle::SharedPtr> gh_future;
// send goal with nonzero last velocities
{
std::vector<JointTrajectoryPoint> points;
JointTrajectoryPoint point1;
point1.time_from_start = rclcpp::Duration::from_seconds(0.0);
point1.positions.resize(joint_names_.size());
point1.velocities.resize(joint_names_.size());
point1.positions[0] = 4.0;
point1.positions[1] = 5.0;
point1.positions[2] = 6.0;
point1.velocities[0] = 4.0;
point1.velocities[1] = 5.0;
point1.velocities[2] = 6.0;
points.push_back(point1);
JointTrajectoryPoint point2;
point2.time_from_start = rclcpp::Duration::from_seconds(0.1);
point2.positions.resize(joint_names_.size());
point2.velocities.resize(joint_names_.size());
point2.positions[0] = 7.0;
point2.positions[1] = 8.0;
point2.positions[2] = 9.0;
point2.velocities[0] = 4.0;
point2.velocities[1] = 5.0;
point2.velocities[2] = 6.0;
points.push_back(point2);
gh_future = sendActionGoal(points, 1.0, goal_options_);
}
controller_hw_thread_.join();
// will be accepted despite nonzero last point
EXPECT_TRUE(gh_future.get());
if ((traj_controller_->has_effort_command_interface()) == false)
{
// can't succeed with effort cmd if
EXPECT_EQ(rclcpp_action::ResultCode::SUCCEEDED, common_resultcode_);
}
}
TEST_P(TestTrajectoryActionsTestParameterized, test_allow_nonzero_velocity_at_trajectory_end_false)
{
std::vector<rclcpp::Parameter> params = {
rclcpp::Parameter("allow_nonzero_velocity_at_trajectory_end", false)};
SetUpExecutor(params);
SetUpControllerHardware();
std::shared_future<typename GoalHandle::SharedPtr> gh_future;
// send goal with nonzero last velocities
{
std::vector<JointTrajectoryPoint> points;
JointTrajectoryPoint point1;
point1.time_from_start = rclcpp::Duration::from_seconds(0.0);
point1.positions.resize(joint_names_.size());
point1.velocities.resize(joint_names_.size());
point1.positions[0] = 4.0;
point1.positions[1] = 5.0;
point1.positions[2] = 6.0;
point1.velocities[0] = 4.0;
point1.velocities[1] = 5.0;
point1.velocities[2] = 6.0;
points.push_back(point1);
JointTrajectoryPoint point2;
point2.time_from_start = rclcpp::Duration::from_seconds(0.1);
point2.positions.resize(joint_names_.size());
point2.velocities.resize(joint_names_.size());
point2.positions[0] = 7.0;
point2.positions[1] = 8.0;
point2.positions[2] = 9.0;
point2.velocities[0] = 4.0;
point2.velocities[1] = 5.0;
point2.velocities[2] = 6.0;
points.push_back(point2);
gh_future = sendActionGoal(points, 1.0, goal_options_);
}
controller_hw_thread_.join();
EXPECT_FALSE(gh_future.get());
// send goal with last velocity being zero
{
std::vector<JointTrajectoryPoint> points;
JointTrajectoryPoint point1;
point1.time_from_start = rclcpp::Duration::from_seconds(0.0);
point1.positions.resize(joint_names_.size());
point1.velocities.resize(joint_names_.size());
point1.positions[0] = 4.0;
point1.positions[1] = 5.0;
point1.positions[2] = 6.0;
point1.velocities[0] = 4.0;
point1.velocities[1] = 5.0;
point1.velocities[2] = 6.0;
points.push_back(point1);
JointTrajectoryPoint point2;
point2.time_from_start = rclcpp::Duration::from_seconds(0.1);
point2.positions.resize(joint_names_.size());
point2.velocities.resize(joint_names_.size());
point2.positions[0] = 7.0;
point2.positions[1] = 8.0;
point2.positions[2] = 9.0;
point2.velocities[0] = 0.0;
point2.velocities[1] = 0.0;
point2.velocities[2] = 0.0;
points.push_back(point2);
gh_future = sendActionGoal(points, 1.0, goal_options_);
}
EXPECT_TRUE(gh_future.get());
}
// position controllers
INSTANTIATE_TEST_SUITE_P(
PositionTrajectoryControllersActions, TestTrajectoryActionsTestParameterized,
::testing::Values(
std::make_tuple(std::vector<std::string>({"position"}), std::vector<std::string>({"position"})),
std::make_tuple(
std::vector<std::string>({"position"}), std::vector<std::string>({"position", "velocity"})),
std::make_tuple(
std::vector<std::string>({"position"}),
std::vector<std::string>({"position", "velocity", "acceleration"}))));
// position_velocity controllers
INSTANTIATE_TEST_SUITE_P(
PositionVelocityTrajectoryControllersActions, TestTrajectoryActionsTestParameterized,
::testing::Values(
std::make_tuple(
std::vector<std::string>({"position", "velocity"}), std::vector<std::string>({"position"})),
std::make_tuple(
std::vector<std::string>({"position", "velocity"}),
std::vector<std::string>({"position", "velocity"})),
std::make_tuple(
std::vector<std::string>({"position", "velocity"}),
std::vector<std::string>({"position", "velocity", "acceleration"}))));
// only velocity controller
INSTANTIATE_TEST_SUITE_P(
OnlyVelocityTrajectoryControllersAction, TestTrajectoryActionsTestParameterized,
::testing::Values(
std::make_tuple(
std::vector<std::string>({"velocity"}), std::vector<std::string>({"position", "velocity"})),
std::make_tuple(
std::vector<std::string>({"velocity"}),
std::vector<std::string>({"position", "velocity", "acceleration"}))));
// only effort controller
INSTANTIATE_TEST_SUITE_P(
OnlyEffortTrajectoryControllers, TestTrajectoryActionsTestParameterized,
::testing::Values(
std::make_tuple(
std::vector<std::string>({"effort"}), std::vector<std::string>({"position", "velocity"})),
std::make_tuple(
std::vector<std::string>({"effort"}),
std::vector<std::string>({"position", "velocity", "acceleration"}))));