-
Notifications
You must be signed in to change notification settings - Fork 525
/
joint_trajectory_controller_test.cpp
1312 lines (1077 loc) · 51.8 KB
/
joint_trajectory_controller_test.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
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2013, PAL Robotics S.L.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of PAL Robotics S.L. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//////////////////////////////////////////////////////////////////////////////
/// \author Adolfo Rodriguez Tsouroukdissian
#include <algorithm>
#include <cmath>
#include <limits>
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <gtest/gtest.h>
#include <ros/ros.h>
#include <actionlib/client/simple_action_client.h>
#include <std_msgs/Float64.h>
#include <std_msgs/Bool.h>
#include <control_msgs/FollowJointTrajectoryAction.h>
#include <control_msgs/JointTrajectoryControllerState.h>
#include <control_msgs/QueryTrajectoryState.h>
#include <controller_manager_msgs/LoadController.h>
#include <controller_manager_msgs/UnloadController.h>
#include <controller_manager_msgs/SwitchController.h>
// Floating-point value comparison threshold
const double EPS = 0.01;
using actionlib::SimpleClientGoalState;
class JointTrajectoryControllerTest : public ::testing::Test
{
public:
JointTrajectoryControllerTest()
: nh("rrbot_controller"),
short_timeout(1.0),
long_timeout(10.0),
controller_state(),
stop_trajectory_duration(0.0)
{
n_joints = (2);
joint_names.resize(n_joints);
joint_names[0] = "joint1";
joint_names[1] = "joint2";
controller_min_actual_velocity.resize(n_joints);
controller_max_actual_velocity.resize(n_joints);
trajectory_msgs::JointTrajectoryPoint point;
point.positions.resize(n_joints, 0.0);
point.velocities.resize(n_joints, 0.0);
point.accelerations.resize(n_joints, 0.0);
// Go home trajectory
traj_home.joint_names = joint_names;
traj_home.points.resize(1, point);
traj_home.points[0].time_from_start = ros::Duration(1.0);
// Three-point trajectory
points.resize(3, point);
points[0].positions[0] = M_PI / 4.0;
points[0].positions[1] = 0.0;
points[0].time_from_start = ros::Duration(1.0);
points[1].positions[0] = 0.0;
points[1].positions[1] = -M_PI / 4.0;
points[1].time_from_start = ros::Duration(2.0);
points[2].positions[0] = -M_PI / 4.0;
points[2].positions[1] = M_PI / 4.0;
points[2].time_from_start = ros::Duration(4.0);
traj.joint_names = joint_names;
traj.points = points;
// Action goals
traj_home_goal.trajectory = traj_home;
traj_goal.trajectory = traj;
// Smoothing publisher (determines how well the robot follows a trajectory)
smoothing_pub = ros::NodeHandle().advertise<std_msgs::Float64>("smoothing", 1);
// Delay publisher (allows to simulate a delay of one cycle in the hardware interface)
delay_pub = ros::NodeHandle().advertise<std_msgs::Bool>("delay", 1);
// Upper bound publisher (allows to simulate a wall)
upper_bound_pub = ros::NodeHandle().advertise<std_msgs::Float64>("upper_bound", 1);
// Trajectory publisher
traj_pub = nh.advertise<trajectory_msgs::JointTrajectory>("command", 1);
// State subscriber
state_sub = nh.subscribe<control_msgs::JointTrajectoryControllerState>("state",
1,
&JointTrajectoryControllerTest::stateCB,
this);
// Query state service client
query_state_service = nh.serviceClient<control_msgs::QueryTrajectoryState>("query_state");
// Controller management services
load_controller_service = nh.serviceClient<controller_manager_msgs::LoadController>("/controller_manager/load_controller");
unload_controller_service = nh.serviceClient<controller_manager_msgs::UnloadController>("/controller_manager/unload_controller");
switch_controller_service = nh.serviceClient<controller_manager_msgs::SwitchController>("/controller_manager/switch_controller");
// Action client
const std::string action_server_name = nh.getNamespace() + "/follow_joint_trajectory";
action_client.reset(new ActionClient(action_server_name));
action_client2.reset(new ActionClient(action_server_name));
nh.getParam("stop_trajectory_duration", stop_trajectory_duration);
}
~JointTrajectoryControllerTest()
{
state_sub.shutdown(); // This is important, to make sure that the callback is not woken up later in the destructor
}
protected:
typedef actionlib::SimpleActionClient<control_msgs::FollowJointTrajectoryAction> ActionClient;
typedef boost::shared_ptr<ActionClient> ActionClientPtr;
typedef control_msgs::FollowJointTrajectoryGoal ActionGoal;
typedef control_msgs::JointTrajectoryControllerStateConstPtr StateConstPtr;
boost::mutex mutex;
ros::NodeHandle nh;
unsigned int n_joints;
std::vector<std::string> joint_names;
std::vector<trajectory_msgs::JointTrajectoryPoint> points;
trajectory_msgs::JointTrajectory traj_home;
trajectory_msgs::JointTrajectory traj;
ActionGoal traj_home_goal;
ActionGoal traj_goal;
ros::Duration short_timeout;
ros::Duration long_timeout;
ros::Publisher smoothing_pub;
ros::Publisher delay_pub;
ros::Publisher upper_bound_pub;
ros::Publisher traj_pub;
ros::Subscriber state_sub;
ros::ServiceClient query_state_service;
ros::ServiceClient load_controller_service;
ros::ServiceClient unload_controller_service;
ros::ServiceClient switch_controller_service;
ActionClientPtr action_client;
ActionClientPtr action_client2;
StateConstPtr controller_state;
std::vector<double> controller_min_actual_velocity;
std::vector<double> controller_max_actual_velocity;
double stop_trajectory_duration;
void stateCB(const StateConstPtr& state)
{
boost::mutex::scoped_lock lock(mutex);
controller_state = state;
std::transform(controller_min_actual_velocity.begin(), controller_min_actual_velocity.end(),
state->actual.velocities.begin(), controller_min_actual_velocity.begin(),
std::min<double>);
std::transform(controller_max_actual_velocity.begin(), controller_max_actual_velocity.end(),
state->actual.velocities.begin(), controller_max_actual_velocity.begin(),
std::max<double>);
}
StateConstPtr getState()
{
boost::mutex::scoped_lock lock(mutex);
return controller_state;
}
bool waitForNextState(const ros::Duration& timeout)
{
ros::Time start_time = ros::Time::now();
ros::Time state_time = getState()->header.stamp;
while ( getState()->header.stamp <= state_time && ros::ok() )
{
if (timeout >= ros::Duration(0.0) && (ros::Time::now() - start_time) > timeout) {return false;} // Timed-out
ros::Duration(0.001).sleep();
}
}
bool initState(const ros::Duration& timeout = ros::Duration(5.0))
{
bool init_ok = false;
ros::Time start_time = ros::Time::now();
while (!init_ok && (ros::Time::now() - start_time) < timeout)
{
{
boost::mutex::scoped_lock lock(mutex);
init_ok = controller_state && !controller_state->joint_names.empty();
}
ros::Duration(0.1).sleep();
}
return init_ok;
}
std::vector<double> getMinActualVelocity()
{
boost::mutex::scoped_lock lock(mutex);
return controller_min_actual_velocity;
}
std::vector<double> getMaxActualVelocity()
{
boost::mutex::scoped_lock lock(mutex);
return controller_max_actual_velocity;
}
void resetActualVelocityObserver()
{
boost::mutex::scoped_lock lock(mutex);
for(size_t i = 0; i < controller_min_actual_velocity.size(); ++i)
{
controller_min_actual_velocity[i] = std::numeric_limits<double>::infinity();
controller_max_actual_velocity[i] = -std::numeric_limits<double>::infinity();
}
}
static bool waitForState(const ActionClientPtr& action_client,
const actionlib::SimpleClientGoalState& state,
const ros::Duration& timeout)
{
using ros::Time;
using ros::Duration;
Time start_time = Time::now();
while (action_client->getState() != state && ros::ok())
{
if (timeout >= Duration(0.0) && (Time::now() - start_time) > timeout) {return false;} // Timed-out
ros::Duration(0.01).sleep();
}
return true;
}
bool reloadController(const std::string& name)
{
controller_manager_msgs::SwitchController stop_controller;
stop_controller.request.stop_controllers.push_back(name);
stop_controller.request.strictness = stop_controller.request.STRICT;
if(!switch_controller_service.call(stop_controller)) return false;
if(!stop_controller.response.ok) return false;
controller_manager_msgs::UnloadController unload_controller;
unload_controller.request.name = name;
if(!unload_controller_service.call(unload_controller)) return false;
if(!unload_controller.response.ok) return false;
controller_manager_msgs::LoadController load_controller;
load_controller.request.name = name;
if(!load_controller_service.call(load_controller)) return false;
if(!load_controller.response.ok) return false;
controller_manager_msgs::SwitchController start_controller;
start_controller.request.start_controllers.push_back(name);
start_controller.request.strictness = start_controller.request.STRICT;
if(!switch_controller_service.call(start_controller)) return false;
if(!start_controller.response.ok) return false;
return true;
}
};
// Controller state ROS API ////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(JointTrajectoryControllerTest, stateTopicConsistency)
{
// Get current controller state
ASSERT_TRUE(initState());
StateConstPtr state = getState();
// Checks that are valid for all state messages
for (unsigned int i = 0; i < n_joints; ++i)
{
EXPECT_EQ(joint_names[i], state->joint_names[i]);
}
EXPECT_EQ(n_joints, state->desired.positions.size());
EXPECT_EQ(n_joints, state->desired.velocities.size());
EXPECT_EQ(n_joints, state->desired.accelerations.size());
EXPECT_EQ(n_joints, state->actual.positions.size());
EXPECT_EQ(n_joints, state->actual.velocities.size());
EXPECT_TRUE(state->actual.accelerations.empty());
EXPECT_EQ(n_joints, state->error.positions.size());
EXPECT_EQ(n_joints, state->error.velocities.size());
EXPECT_TRUE(state->error.accelerations.empty());
}
TEST_F(JointTrajectoryControllerTest, queryStateServiceConsistency)
{
query_state_service.waitForExistence(ros::Duration(2.0));
ASSERT_TRUE(query_state_service.isValid());
control_msgs::QueryTrajectoryState srv;
srv.request.time = ros::Time::now();
ASSERT_TRUE(query_state_service.call(srv));
// Checks that are valid for all queries
for (unsigned int i = 0; i < n_joints; ++i)
{
EXPECT_EQ(joint_names[i], srv.response.name[i]);
}
EXPECT_EQ(n_joints, srv.response.position.size());
EXPECT_EQ(n_joints, srv.response.velocity.size());
EXPECT_EQ(n_joints, srv.response.acceleration.size());
}
// Invalid messages ////////////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(JointTrajectoryControllerTest, invalidMessages)
{
ASSERT_TRUE(initState());
ASSERT_TRUE(action_client->waitForServer(long_timeout));
// Invalid size (No partial joints goals allowed)
{
trajectory_msgs::JointTrajectoryPoint point;
point.positions.resize(1, 0.0);
point.velocities.resize(1, 0.0);
point.accelerations.resize(1, 0.0);
trajectory_msgs::JointTrajectory bad_traj;
bad_traj.joint_names.resize(1, "joint1");
bad_traj.points.resize(1, point);
bad_traj.points[0].time_from_start = ros::Duration(1.0);
ActionGoal bad_goal;
bad_goal.trajectory = bad_traj;
bad_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(bad_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::REJECTED, long_timeout));
EXPECT_EQ(action_client->getResult()->error_code, control_msgs::FollowJointTrajectoryResult::INVALID_JOINTS);
}
// Incompatible joint names
{
ActionGoal bad_goal = traj_home_goal;
bad_goal.trajectory.joint_names[0] = "bad_name";
bad_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(bad_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::REJECTED, long_timeout));
EXPECT_EQ(action_client->getResult()->error_code, control_msgs::FollowJointTrajectoryResult::INVALID_JOINTS);
}
// No position data
{
ActionGoal bad_goal = traj_home_goal;
bad_goal.trajectory.points[0].positions.clear();
bad_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(bad_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::REJECTED, long_timeout));
EXPECT_EQ(action_client->getResult()->error_code, control_msgs::FollowJointTrajectoryResult::INVALID_GOAL);
}
// Incompatible data sizes
{
ActionGoal bad_goal = traj_home_goal;
bad_goal.trajectory.points[0].positions.pop_back();
bad_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(bad_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::REJECTED, long_timeout));
EXPECT_EQ(action_client->getResult()->error_code, control_msgs::FollowJointTrajectoryResult::INVALID_GOAL);
}
// Non-strictly increasing waypoint times
{
ActionGoal bad_goal = traj_goal;
bad_goal.trajectory.points[2].time_from_start = bad_goal.trajectory.points[1].time_from_start;
action_client->sendGoal(bad_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::REJECTED, long_timeout));
EXPECT_EQ(action_client->getResult()->error_code, control_msgs::FollowJointTrajectoryResult::INVALID_GOAL);
}
// Empty trajectory through action interface
// NOTE: Sending an empty trajectory through the topic interface cancels execution of all queued segments, but
// an empty trajectory is interpreted by the action interface as not having the correct joint names.
{
ActionGoal empty_goal;
action_client->sendGoal(empty_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::REJECTED, long_timeout));
EXPECT_EQ(action_client->getResult()->error_code, control_msgs::FollowJointTrajectoryResult::INVALID_JOINTS);
}
}
// Uninterrupted trajectory execution //////////////////////////////////////////////////////////////////////////////////
TEST_F(JointTrajectoryControllerTest, topicSingleTraj)
{
ASSERT_TRUE(initState());
// Send trajectory
traj.header.stamp = ros::Time(0); // Start immediately
traj_pub.publish(traj);
ros::Duration wait_duration = traj.points.back().time_from_start + ros::Duration(0.5);
wait_duration.sleep(); // Wait until done
// Validate state topic values
StateConstPtr state = getState();
for (unsigned int i = 0; i < n_joints; ++i)
{
EXPECT_NEAR(traj.points.back().positions[i], state->desired.positions[i], EPS);
EXPECT_NEAR(traj.points.back().velocities[i], state->desired.velocities[i], EPS);
EXPECT_NEAR(traj.points.back().accelerations[i], state->desired.accelerations[i], EPS);
EXPECT_NEAR(traj.points.back().positions[i], state->actual.positions[i], EPS);
EXPECT_NEAR(traj.points.back().velocities[i], state->actual.velocities[i], EPS);
EXPECT_NEAR(0.0, state->error.positions[i], EPS);
EXPECT_NEAR(0.0, state->error.velocities[i], EPS);
}
// Validate query state service
control_msgs::QueryTrajectoryState srv;
srv.request.time = ros::Time::now();
ASSERT_TRUE(query_state_service.call(srv));
for (unsigned int i = 0; i < n_joints; ++i)
{
EXPECT_NEAR(state->desired.positions[i], srv.response.position[i], EPS);
EXPECT_NEAR(state->desired.velocities[i], srv.response.velocity[i], EPS);
EXPECT_NEAR(state->desired.accelerations[i], srv.response.acceleration[i], EPS);
}
}
TEST_F(JointTrajectoryControllerTest, actionSingleTraj)
{
ASSERT_TRUE(initState());
ASSERT_TRUE(action_client->waitForServer(long_timeout));
// Send trajectory
traj_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(traj_goal);
// Wait until done
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, long_timeout));
EXPECT_EQ(action_client->getResult()->error_code, control_msgs::FollowJointTrajectoryResult::SUCCESSFUL);
ros::Duration(0.5).sleep(); // Allows values to settle to within EPS, especially accelerations
// Validate state topic values
StateConstPtr state = getState();
for (unsigned int i = 0; i < n_joints; ++i)
{
EXPECT_NEAR(traj.points.back().positions[i], state->desired.positions[i], EPS);
EXPECT_NEAR(traj.points.back().velocities[i], state->desired.velocities[i], EPS);
EXPECT_NEAR(traj.points.back().accelerations[i], state->desired.accelerations[i], EPS);
EXPECT_NEAR(traj.points.back().positions[i], state->actual.positions[i], EPS);
EXPECT_NEAR(traj.points.back().velocities[i], state->actual.velocities[i], EPS);
EXPECT_NEAR(0.0, state->error.positions[i], EPS);
EXPECT_NEAR(0.0, state->error.velocities[i], EPS);
}
}
// Joint reordering ////////////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(JointTrajectoryControllerTest, jointReordering)
{
ASSERT_TRUE(initState());
ASSERT_TRUE(action_client->waitForServer(long_timeout));
// Message joints are ordered differently than in controller
ActionGoal reorder_goal = traj_home_goal;
std::swap(reorder_goal.trajectory.joint_names[0], reorder_goal.trajectory.joint_names[1]);
reorder_goal.trajectory.points.front().positions[0] = M_PI / 4.0;
reorder_goal.trajectory.points.front().positions[1] = 0.0;
reorder_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(reorder_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, long_timeout));
ros::Duration(0.5).sleep(); // Allows values to settle to within EPS, especially accelerations
// Validate state topic values
StateConstPtr state = getState();
EXPECT_NEAR(reorder_goal.trajectory.points.back().positions[0], state->desired.positions[1], EPS);
EXPECT_NEAR(reorder_goal.trajectory.points.back().positions[1], state->desired.positions[0], EPS);
}
// Joint wraparound ////////////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(JointTrajectoryControllerTest, jointWraparound)
{
ASSERT_TRUE(initState());
ASSERT_TRUE(action_client->waitForServer(long_timeout));
// Go to home configuration, we need known initial conditions
traj_home_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(traj_home_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, long_timeout));
// Trajectory goals that trigger wrapping. Between the first and second goals:
// - First joint command has a wraparound of -2 loops
// - Second joint command has a wraparound of +1 loop
// - Both joints should end up doing an angular displacement of |pi/4| for the second goal
trajectory_msgs::JointTrajectoryPoint point;
point.positions.resize(n_joints, 0.0);
point.velocities.resize(n_joints, 0.0);
point.accelerations.resize(n_joints, 0.0);
ActionGoal goal1;
goal1.trajectory.joint_names = joint_names;
goal1.trajectory.points.resize(1, point);
goal1.trajectory.points[0].positions[0] = 0.25 * M_PI; //-M_PI / 2.0;
goal1.trajectory.points[0].positions[1] = 0.50 * M_PI; // M_PI / 2.0;
goal1.trajectory.points[0].time_from_start = ros::Duration(1.0);
ActionGoal goal2;
goal2.trajectory.joint_names = joint_names;
goal2.trajectory.points.resize(1, point);
goal2.trajectory.points[0].positions[0] = 4.50 * M_PI;//M_PI / 2.0;
goal2.trajectory.points[0].positions[1] = -1.75 * M_PI; //0.0;
goal2.trajectory.points[0].time_from_start = ros::Duration(1.0);
// Send trajectory
goal1.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(goal1);
// Wait until done
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, long_timeout));
ros::Duration(0.5).sleep(); // Allows values to settle to within EPS, especially accelerations
// Send trajectory
goal2.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(goal2);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, long_timeout));
// Validate state topic values
StateConstPtr state = getState();
EXPECT_NEAR(goal1.trajectory.points[0].positions[0] + 0.25 * M_PI, state->desired.positions[0], EPS);
EXPECT_NEAR(goal1.trajectory.points[0].positions[1] - 0.25 * M_PI, state->desired.positions[1], EPS);
}
TEST_F(JointTrajectoryControllerTest, jointWraparoundPiSingularity)
{
ASSERT_TRUE(initState());
ASSERT_TRUE(action_client->waitForServer(long_timeout));
// Go to home configuration, we need known initial conditions
traj_home_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(traj_home_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, long_timeout));
// Trajectory goals that trigger wrapping.
// When moving a wrapping joint pi radians (eg. from -pi/2 to pi/2), numeric errors can make the controller think
// that it has to wrap when in fact it doesn't. This comes from a call to angles::shortest_angular_distance, which
// sometimes wields pi, and sometimes -pi.
trajectory_msgs::JointTrajectoryPoint point;
point.positions.resize(n_joints, 0.0);
point.velocities.resize(n_joints, 0.0);
point.accelerations.resize(n_joints, 0.0);
ActionGoal goal1;
goal1.trajectory.joint_names = joint_names;
goal1.trajectory.points.resize(1, point);
goal1.trajectory.points[0].positions[0] = -M_PI / 2.0;
goal1.trajectory.points[0].positions[1] = 0.0;
goal1.trajectory.points[0].time_from_start = ros::Duration(1.0);
ActionGoal goal2;
goal2.trajectory.joint_names = joint_names;
goal2.trajectory.points.resize(1, point);
goal2.trajectory.points[0].positions[0] = M_PI / 2.0;
goal2.trajectory.points[0].positions[1] = 0.0;
goal2.trajectory.points[0].time_from_start = ros::Duration(2.0);
// Send trajectory
goal1.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(goal1);
// Wait until done
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, long_timeout));
ros::Duration(0.5).sleep(); // Allows values to settle to within EPS, especially accelerations
// Send trajectory
goal2.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(goal2);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, long_timeout));
// Validate state topic values
StateConstPtr state = getState();
for (unsigned int i = 0; i < n_joints; ++i)
{
EXPECT_NEAR(goal2.trajectory.points[0].positions[i], state->desired.positions[i], EPS);
}
}
// Trajectory replacement //////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(JointTrajectoryControllerTest, topicReplacesTopicTraj)
{
ASSERT_TRUE(initState());
// Send trajectory
traj.header.stamp = ros::Time(0); // Start immediately
traj_pub.publish(traj);
ros::Duration wait_duration = traj.points.front().time_from_start;
wait_duration.sleep(); // Wait until ~first waypoint
// Send another trajectory that preempts the previous one
traj_home.header.stamp = ros::Time(0); // Start immediately
traj_pub.publish(traj_home);
wait_duration = traj_home.points.back().time_from_start + ros::Duration(0.5);
wait_duration.sleep(); // Wait until done
// Check that we're back home
StateConstPtr state = getState();
for (unsigned int i = 0; i < n_joints; ++i)
{
EXPECT_NEAR(traj_home.points.back().positions[i], state->desired.positions[i], EPS);
EXPECT_NEAR(traj_home.points.back().velocities[i], state->desired.velocities[i], EPS);
EXPECT_NEAR(traj_home.points.back().accelerations[i], state->desired.accelerations[i], EPS);
}
}
TEST_F(JointTrajectoryControllerTest, actionReplacesActionTraj)
{
ASSERT_TRUE(initState());
ASSERT_TRUE(action_client->waitForServer(long_timeout));
// Send trajectory
traj_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(traj_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::ACTIVE, short_timeout));
ros::Duration wait_duration = traj.points.front().time_from_start;
wait_duration.sleep(); // Wait until ~first waypoint
// Send another trajectory that preempts the previous one
traj_home_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client2->sendGoal(traj_home_goal);
ASSERT_TRUE(waitForState(action_client2, SimpleClientGoalState::ACTIVE, short_timeout));
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::PREEMPTED, short_timeout));
// Wait until done
ASSERT_TRUE(waitForState(action_client2, SimpleClientGoalState::SUCCEEDED, long_timeout));
ros::Duration(0.5).sleep(); // Allows values to settle to within EPS, especially accelerations
// Check that we're back home
StateConstPtr state = getState();
for (unsigned int i = 0; i < n_joints; ++i)
{
EXPECT_NEAR(traj_home.points.back().positions[i], state->desired.positions[i], EPS);
EXPECT_NEAR(traj_home.points.back().velocities[i], state->desired.velocities[i], EPS);
EXPECT_NEAR(traj_home.points.back().accelerations[i], state->desired.accelerations[i], EPS);
}
}
TEST_F(JointTrajectoryControllerTest, actionReplacesTopicTraj)
{
ASSERT_TRUE(initState());
ASSERT_TRUE(action_client->waitForServer(long_timeout));
// Send trajectory
traj.header.stamp = ros::Time(0); // Start immediately
traj_pub.publish(traj);
ros::Duration wait_duration = traj.points.front().time_from_start;
wait_duration.sleep(); // Wait until ~first waypoint
// Send another trajectory that preempts the previous one
traj_home_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(traj_home_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::ACTIVE, short_timeout));
// Wait until done
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, long_timeout));
ros::Duration(0.5).sleep(); // Allows values to settle to within EPS, especially accelerations
// Check that we're back home
StateConstPtr state = getState();
for (unsigned int i = 0; i < n_joints; ++i)
{
EXPECT_NEAR(traj_home.points.back().positions[i], state->desired.positions[i], EPS);
EXPECT_NEAR(traj_home.points.back().velocities[i], state->desired.velocities[i], EPS);
EXPECT_NEAR(traj_home.points.back().accelerations[i], state->desired.accelerations[i], EPS);
}
}
TEST_F(JointTrajectoryControllerTest, topicReplacesActionTraj)
{
ASSERT_TRUE(initState());
ASSERT_TRUE(action_client->waitForServer(long_timeout));
// Send trajectory
traj_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(traj_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::ACTIVE, short_timeout));
ros::Duration wait_duration = traj.points.front().time_from_start;
wait_duration.sleep(); // Wait until ~first waypoint
// Send another trajectory that preempts the previous one
traj_home.header.stamp = ros::Time(0); // Start immediately
traj_pub.publish(traj_home);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::PREEMPTED, short_timeout));
wait_duration = traj_home.points.back().time_from_start + ros::Duration(0.5);
wait_duration.sleep(); // Wait until done
// Check that we're back home
StateConstPtr state = getState();
for (unsigned int i = 0; i < n_joints; ++i)
{
EXPECT_NEAR(traj_home.points.back().positions[i], state->desired.positions[i], EPS);
EXPECT_NEAR(traj_home.points.back().velocities[i], state->desired.velocities[i], EPS);
EXPECT_NEAR(traj_home.points.back().accelerations[i], state->desired.accelerations[i], EPS);
}
}
// Cancel execution ////////////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(JointTrajectoryControllerTest, emptyTopicCancelsTopicTraj)
{
ASSERT_TRUE(initState());
ASSERT_TRUE(action_client->waitForServer(long_timeout));
// Go to home configuration, we need known initial conditions
traj_home_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(traj_home_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, long_timeout));
// Start state
StateConstPtr start_state = getState();
// Send trajectory
traj.header.stamp = ros::Time(0); // Start immediately
traj_pub.publish(traj);
ros::Duration wait_duration = traj.points.front().time_from_start;
wait_duration.sleep(); // Wait until ~first waypoint
// Send an empty trajectory that preempts the previous one and stops the robot where it is
trajectory_msgs::JointTrajectory traj_empty;
traj_pub.publish(traj_empty);
ros::Duration(2.0).sleep(); // Stopping takes some time
// Check that we're not on the start state
StateConstPtr state1 = getState();
EXPECT_LT(traj.points.front().positions[0] * 0.9,
std::abs(start_state->desired.positions[0] - state1->desired.positions[0]));
// Check that we're not moving
ros::Duration(0.5).sleep(); // Wait
StateConstPtr state2 = getState();
for (unsigned int i = 0; i < n_joints; ++i)
{
EXPECT_NEAR(state1->desired.positions[i], state2->desired.positions[i], EPS);
EXPECT_NEAR(state1->desired.velocities[i], state2->desired.velocities[i], EPS);
EXPECT_NEAR(state1->desired.accelerations[i], state2->desired.accelerations[i], EPS);
}
}
TEST_F(JointTrajectoryControllerTest, emptyTopicCancelsActionTraj)
{
ASSERT_TRUE(initState());
ASSERT_TRUE(action_client->waitForServer(long_timeout));
// Go to home configuration, we need known initial conditions
traj_home_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(traj_home_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, long_timeout));
// Start state
StateConstPtr start_state = getState();
// Send trajectory
traj_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(traj_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::ACTIVE, short_timeout));
ros::Duration wait_duration = traj.points.front().time_from_start;
wait_duration.sleep(); // Wait until ~first waypoint
// Send an empty trajectory that preempts the previous one and stops the robot where it is
trajectory_msgs::JointTrajectory traj_empty;
traj_pub.publish(traj_empty);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::PREEMPTED, short_timeout));
// make sure that stateCB received the newer topics than when we confirmed with waitForState function
waitForNextState(short_timeout);
// Check that we're not on the start state
StateConstPtr state1 = getState();
EXPECT_LT(traj.points.front().positions[0] * 0.9,
std::abs(start_state->desired.positions[0] - state1->desired.positions[0]));
// Check that we're not moving
ros::Duration(0.5).sleep(); // Wait
StateConstPtr state2 = getState();
for (unsigned int i = 0; i < n_joints; ++i)
{
EXPECT_NEAR(state1->desired.positions[i], state2->desired.positions[i], EPS);
EXPECT_NEAR(state1->desired.velocities[i], state2->desired.velocities[i], EPS);
EXPECT_NEAR(state1->desired.accelerations[i], state2->desired.accelerations[i], EPS);
}
}
TEST_F(JointTrajectoryControllerTest, emptyTopicCancelsActionTrajWithDelay)
{
// check stop ramp for trajectory duration > 0
if(stop_trajectory_duration > 0)
{
ASSERT_TRUE(action_client->waitForServer(long_timeout));
// Go to home configuration, we need known initial conditions
traj_home_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(traj_home_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, long_timeout));
// Make robot respond with a delay
{
std_msgs::Bool delay;
delay.data = true;
delay_pub.publish(delay);
ros::Duration(0.5).sleep();
}
resetActualVelocityObserver();
// Send trajectory
traj_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(traj_goal);
EXPECT_TRUE(waitForState(action_client, SimpleClientGoalState::ACTIVE, short_timeout));
ros::Duration wait_duration = traj.points.front().time_from_start * 0.5;
wait_duration.sleep(); // Wait until half of first segment
ActionGoal empty_goal;
empty_goal.trajectory.joint_names = joint_names;
action_client->sendGoal(empty_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, short_timeout));
// Velocity should be continuous so all axes >= 0
std::vector<double> minVelocity = getMinActualVelocity();
for(size_t i = 0; i < minVelocity.size(); ++i)
{
EXPECT_GE(minVelocity[i], 0.);
}
// Restore perfect control
{
std_msgs::Bool delay;
delay.data = false;
delay_pub.publish(delay);
ros::Duration(0.5).sleep();
}
}
else
{
SUCCEED();
}
}
TEST_F(JointTrajectoryControllerTest, emptyTopicCancelsActionTrajWithDelayStopZero)
{
// check set position = actual position for stop_duration == 0
ASSERT_TRUE(action_client->waitForServer(long_timeout));
// Go to home configuration, we need known initial conditions
traj_home_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(traj_home_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, long_timeout));
// Make robot respond with a delay and clip position at a wall
std_msgs::Float64 upper_bound;
{
std_msgs::Bool delay;
delay.data = true;
delay_pub.publish(delay);
upper_bound.data = traj_goal.trajectory.points.front().positions.front() / 3.;
upper_bound_pub.publish(upper_bound);
ros::Duration(0.5).sleep();
}
resetActualVelocityObserver();
// Send trajectory
traj_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(traj_goal);
EXPECT_TRUE(waitForState(action_client, SimpleClientGoalState::ACTIVE, short_timeout));
ros::Duration wait_duration = traj.points.front().time_from_start * 0.5;
wait_duration.sleep(); // Wait until half of first segment
ActionGoal empty_goal;
empty_goal.trajectory.joint_names = joint_names;
action_client->sendGoal(empty_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, short_timeout));
StateConstPtr state1 = getState();
if(stop_trajectory_duration == 0.0)
{
// Here we expect that the desired position is equal to the actual position of the robot,
// which is given through upper_bound by construction.
EXPECT_NEAR(state1->desired.positions[0], upper_bound.data, EPS); // first joint
}
else
{
// stop ramp should be calculated using the desired position
// so it is greater than the upper bound
EXPECT_GT(state1->desired.positions[0], upper_bound.data); // first joint
}
// Restore perfect control
{
std_msgs::Bool delay;
delay.data = false;
delay_pub.publish(delay);
std_msgs::Float64 upper_bound;
upper_bound.data = std::numeric_limits<double>::infinity();
upper_bound_pub.publish(upper_bound);
ros::Duration(0.5).sleep();
}
}
TEST_F(JointTrajectoryControllerTest, emptyActionCancelsTopicTraj)
{
ASSERT_TRUE(initState());
ASSERT_TRUE(action_client->waitForServer(long_timeout));
// Go to home configuration, we need known initial conditions
traj_home_goal.trajectory.header.stamp = ros::Time(0); // Start immediately
action_client->sendGoal(traj_home_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, long_timeout));
// Start state
StateConstPtr start_state = getState();
// Send trajectory
traj.header.stamp = ros::Time(0); // Start immediately
traj_pub.publish(traj);
ros::Duration wait_duration = traj.points.front().time_from_start;
wait_duration.sleep(); // Wait until ~first waypoint
// Send an empty trajectory goal that preempts the previous one and stops the robot where it is
ActionGoal empty_goal;
empty_goal.trajectory.joint_names = traj.joint_names;
action_client->sendGoal(empty_goal);
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::ACTIVE, short_timeout));
ASSERT_TRUE(waitForState(action_client, SimpleClientGoalState::SUCCEEDED, short_timeout));
// make sure that stateCB received the newer topics than when we confirmed with waitForState function
waitForNextState(short_timeout);
// Check that we're not on the start state
StateConstPtr state1 = getState();
EXPECT_LT(traj.points.front().positions[0] * 0.9,
std::abs(start_state->desired.positions[0] - state1->desired.positions[0]));
// Check that we're not moving
ros::Duration(0.5).sleep(); // Wait
StateConstPtr state2 = getState();
for (unsigned int i = 0; i < n_joints; ++i)
{
EXPECT_NEAR(state1->desired.positions[i], state2->desired.positions[i], EPS);
EXPECT_NEAR(state1->desired.velocities[i], state2->desired.velocities[i], EPS);
EXPECT_NEAR(state1->desired.accelerations[i], state2->desired.accelerations[i], EPS);
}
}
TEST_F(JointTrajectoryControllerTest, emptyActionCancelsActionTraj)
{
ASSERT_TRUE(initState());
ASSERT_TRUE(action_client->waitForServer(long_timeout));
// Go to home configuration, we need known initial conditions
traj_home_goal.trajectory.header.stamp = ros::Time(0); // Start immediately