39 #include <gtest/gtest.h>
42 #include <urdf_parser/urdf_parser.h>
48 TEST(time_optimal_trajectory_generation, test1)
50 Eigen::VectorXd waypoint(4);
51 std::list<Eigen::VectorXd> waypoints;
53 waypoint << 1424.0, 984.999694824219, 2126.0, 0.0;
54 waypoints.push_back(waypoint);
55 waypoint << 1423.0, 985.000244140625, 2126.0, 0.0;
56 waypoints.push_back(waypoint);
58 Eigen::VectorXd max_velocities(4);
59 max_velocities << 1.3, 0.67, 0.67, 0.5;
60 Eigen::VectorXd max_accelerations(4);
61 max_accelerations << 0.00249, 0.00249, 0.00249, 0.00249;
63 Trajectory trajectory(
Path(waypoints, 100.0), max_velocities, max_accelerations, 10.0);
65 EXPECT_DOUBLE_EQ(40.080256821829849, trajectory.
getDuration());
68 EXPECT_DOUBLE_EQ(1424.0, trajectory.
getPosition(0.0)[0]);
69 EXPECT_DOUBLE_EQ(984.999694824219, trajectory.
getPosition(0.0)[1]);
70 EXPECT_DOUBLE_EQ(2126.0, trajectory.
getPosition(0.0)[2]);
71 EXPECT_DOUBLE_EQ(0.0, trajectory.
getPosition(0.0)[3]);
80 const double traj_duration = trajectory.
getDuration();
85 TEST(time_optimal_trajectory_generation, test2)
87 Eigen::VectorXd waypoint(4);
88 std::list<Eigen::VectorXd> waypoints;
90 waypoint << 1427.0, 368.0, 690.0, 90.0;
91 waypoints.push_back(waypoint);
92 waypoint << 1427.0, 368.0, 790.0, 90.0;
93 waypoints.push_back(waypoint);
94 waypoint << 952.499938964844, 433.0, 1051.0, 90.0;
95 waypoints.push_back(waypoint);
96 waypoint << 452.5, 533.0, 1051.0, 90.0;
97 waypoints.push_back(waypoint);
98 waypoint << 452.5, 533.0, 951.0, 90.0;
99 waypoints.push_back(waypoint);
101 Eigen::VectorXd max_velocities(4);
102 max_velocities << 1.3, 0.67, 0.67, 0.5;
103 Eigen::VectorXd max_accelerations(4);
104 max_accelerations << 0.002, 0.002, 0.002, 0.002;
106 Trajectory trajectory(
Path(waypoints, 100.0), max_velocities, max_accelerations, 10.0);
108 EXPECT_DOUBLE_EQ(1922.1418427445944, trajectory.
getDuration());
111 EXPECT_DOUBLE_EQ(1427.0, trajectory.
getPosition(0.0)[0]);
112 EXPECT_DOUBLE_EQ(368.0, trajectory.
getPosition(0.0)[1]);
113 EXPECT_DOUBLE_EQ(690.0, trajectory.
getPosition(0.0)[2]);
114 EXPECT_DOUBLE_EQ(90.0, trajectory.
getPosition(0.0)[3]);
123 const double traj_duration = trajectory.
getDuration();
128 TEST(time_optimal_trajectory_generation, test3)
130 Eigen::VectorXd waypoint(4);
131 std::list<Eigen::VectorXd> waypoints;
133 waypoint << 1427.0, 368.0, 690.0, 90.0;
134 waypoints.push_back(waypoint);
135 waypoint << 1427.0, 368.0, 790.0, 90.0;
136 waypoints.push_back(waypoint);
137 waypoint << 952.499938964844, 433.0, 1051.0, 90.0;
138 waypoints.push_back(waypoint);
139 waypoint << 452.5, 533.0, 1051.0, 90.0;
140 waypoints.push_back(waypoint);
141 waypoint << 452.5, 533.0, 951.0, 90.0;
142 waypoints.push_back(waypoint);
144 Eigen::VectorXd max_velocities(4);
145 max_velocities << 1.3, 0.67, 0.67, 0.5;
146 Eigen::VectorXd max_accelerations(4);
147 max_accelerations << 0.002, 0.002, 0.002, 0.002;
149 Trajectory trajectory(
Path(waypoints, 100.0), max_velocities, max_accelerations);
151 EXPECT_DOUBLE_EQ(1919.5597888812974, trajectory.
getDuration());
154 EXPECT_DOUBLE_EQ(1427.0, trajectory.
getPosition(0.0)[0]);
155 EXPECT_DOUBLE_EQ(368.0, trajectory.
getPosition(0.0)[1]);
156 EXPECT_DOUBLE_EQ(690.0, trajectory.
getPosition(0.0)[2]);
157 EXPECT_DOUBLE_EQ(90.0, trajectory.
getPosition(0.0)[3]);
166 const double traj_duration = trajectory.
getDuration();
172 TEST(time_optimal_trajectory_generation, testLargeAccel)
174 double path_tolerance = 0.1;
175 double resample_dt = 0.1;
176 Eigen::VectorXd waypoint(6);
177 std::list<Eigen::VectorXd> waypoints;
178 Eigen::VectorXd max_velocities(6);
179 Eigen::VectorXd max_accelerations(6);
183 waypoint << 1.6113056281076339,
184 -0.21400163389235427,
186 9.9653618690354051e-12,
189 waypoints.push_back(waypoint);
191 waypoint << 1.6088016187976597,
192 -0.21792862470933924,
194 0.00010424017303217738,
197 waypoints.push_back(waypoint);
199 waypoint << 1.5887695443178671,
200 -0.24934455124521923,
202 0.00093816147756670078,
205 waypoints.push_back(waypoint);
207 waypoint << 1.1647412393815282,
208 -0.91434018564402375,
210 0.018590164397622583,
213 waypoints.push_back(waypoint);
216 max_velocities << 0.89535390627300004,
223 max_accelerations << 0.82673490883799994,
231 Trajectory parameterized(
Path(waypoints, path_tolerance), max_velocities, max_accelerations, 0.001);
233 ASSERT_TRUE(parameterized.
isValid());
235 size_t sample_count = std::ceil(parameterized.
getDuration() / resample_dt);
236 for (
size_t sample = 0; sample <= sample_count; ++sample)
239 double t = std::min(parameterized.
getDuration(), sample * resample_dt);
242 ASSERT_EQ(acceleration.size(), 6);
243 for (std::size_t i = 0; i < 6; ++i)
244 EXPECT_NEAR(acceleration(i), 0.0, 100.0) <<
"Invalid acceleration at position " << sample_count <<
"\n";
248 TEST(time_optimal_trajectory_generation, testPluginAPI)
250 constexpr
auto robot_name{
"panda" };
251 constexpr
auto group_name{
"panda_arm" };
254 ASSERT_TRUE((
bool)
robot_model) <<
"Failed to load robot model" << robot_name;
255 auto group =
robot_model->getJointModelGroup(group_name);
256 ASSERT_TRUE((
bool)group) <<
"Failed to load joint model group " << group_name;
261 waypoint_state.
setJointGroupPositions(group, std::vector<double>{ -0.5, -3.52, 1.35, -2.51, -0.88, 0.63, 0.0 });
263 waypoint_state.
setJointGroupPositions(group, std::vector<double>{ -0.5, -3.52, 1.35, -2.51, -0.88, 0.63, 0.0 });
265 waypoint_state.
setJointGroupPositions(group, std::vector<double>{ 0.0, -3.5, 1.4, -1.2, -1.0, -0.2, 0.0 });
267 waypoint_state.
setJointGroupPositions(group, std::vector<double>{ -0.5, -3.52, 1.35, -2.51, -0.88, 0.63, 0.0 });
269 waypoint_state.
setJointGroupPositions(group, std::vector<double>{ 0.0, -3.5, 1.4, -1.2, -1.0, -0.2, 0.0 });
271 waypoint_state.
setJointGroupPositions(group, std::vector<double>{ -0.5, -3.52, 1.35, -2.51, -0.88, 0.63, 0.0 });
275 constexpr
size_t totg_iterations = 10;
278 moveit_msgs::RobotTrajectory first_trajectory_msg_start, first_trajectory_msg_end;
286 ASSERT_EQ(test_bounds.size(), original_bounds.size());
287 for (
size_t bound_idx = 0; bound_idx < test_bounds.at(0)->size(); ++bound_idx)
289 ASSERT_EQ(test_bounds.at(0)->at(bound_idx).max_velocity_, original_bounds.at(0)->at(bound_idx).max_velocity_);
290 ASSERT_EQ(test_bounds.at(0)->at(bound_idx).min_velocity_, original_bounds.at(0)->at(bound_idx).min_velocity_);
291 ASSERT_EQ(test_bounds.at(0)->at(bound_idx).velocity_bounded_,
292 original_bounds.at(0)->at(bound_idx).velocity_bounded_);
294 ASSERT_EQ(test_bounds.at(0)->at(bound_idx).max_acceleration_,
295 original_bounds.at(0)->at(bound_idx).max_acceleration_);
296 ASSERT_EQ(test_bounds.at(0)->at(bound_idx).min_acceleration_,
297 original_bounds.at(0)->at(bound_idx).min_acceleration_);
298 ASSERT_EQ(test_bounds.at(0)->at(bound_idx).acceleration_bounded_,
299 original_bounds.at(0)->at(bound_idx).acceleration_bounded_);
304 ASSERT_TRUE(totg.
computeTimeStamps(test_trajectory)) <<
"Failed to compute time stamps";
309 for (
size_t i = 0; i < totg_iterations; ++i)
312 EXPECT_TRUE(totg_success) <<
"Failed to compute time stamps with a same TOTG instance in iteration " << i;
319 moveit_msgs::RobotTrajectory second_trajectory_msg_start, second_trajectory_msg_end;
325 ASSERT_TRUE(totg.
computeTimeStamps(test_trajectory)) <<
"Failed to compute time stamps";
331 for (
size_t i = 0; i < totg_iterations; ++i)
335 EXPECT_TRUE(totg_success) <<
"Failed to compute time stamps with a new TOTG instance in iteration " << i;
342 ASSERT_EQ(first_trajectory_msg_start, second_trajectory_msg_start);
343 ASSERT_EQ(first_trajectory_msg_end, second_trajectory_msg_end);
346 moveit_msgs::RobotTrajectory third_trajectory_msg_end;
350 ASSERT_TRUE(totg.
computeTimeStamps(trajectory)) <<
"Failed to compute time stamps";
353 for (
size_t i = 0; i < totg_iterations; ++i)
357 ASSERT_TRUE(totg_success) <<
"Failed to compute timestamps on a new TOTG instance in iteration " << i;
364 ASSERT_EQ(first_trajectory_msg_end, third_trajectory_msg_end);
367 TEST(time_optimal_trajectory_generation, testSingleDofDiscontinuity)
370 Eigen::VectorXd waypoint(1);
371 std::list<Eigen::VectorXd> waypoints;
373 const double start_position = 1.881943;
374 waypoint << start_position;
375 waypoints.push_back(waypoint);
376 waypoint << 2.600542;
377 waypoints.push_back(waypoint);
379 Eigen::VectorXd max_velocities(1);
380 max_velocities << 4.54;
381 Eigen::VectorXd max_accelerations(1);
382 max_accelerations << 28.0;
384 Trajectory trajectory(
Path(waypoints, 0.1 ), max_velocities, max_accelerations,
389 const double traj_duration = trajectory.
getDuration();
393 EXPECT_DOUBLE_EQ(start_position, trajectory.
getPosition(0.0)[0]);
399 for (
double time = 0; time < traj_duration; time += 0.01)
402 double t_switch = 0.1603407;
407 else if (time > t_switch)
414 TEST(time_optimal_trajectory_generation, testMimicJoint)
416 const std::string
urdf = R
"(<?xml version="1.0" ?>
417 <robot name="one_robot">
418 <link name="base_link"/>
419 <link name="link_a"/>
420 <link name="link_b"/>
421 <joint name="joint_a" type="continuous">
423 <parent link="base_link"/>
424 <child link="link_a"/>
425 <limit effort="3" velocity="3"/>
427 <joint name="joint_b" type="continuous">
429 <parent link="link_a"/>
430 <child link="link_b"/>
431 <mimic joint="joint_a" multiplier="2" />
432 <limit effort="3" velocity="3"/>
436 const std::string
srdf = R
"(<?xml version="1.0" ?>
437 <robot name="one_robot">
438 <virtual_joint name="base_joint" child_link="base_link" parent_frame="odom_combined" type="planar"/>
440 <joint name="joint_a"/>
441 <joint name="joint_b"/>
445 urdf::ModelInterfaceSharedPtr urdf_model = urdf::parseURDF(urdf);
447 srdf_model->initString(*urdf_model, srdf);
448 auto robot_model = std::make_shared<moveit::core::RobotModel>(urdf_model, srdf_model);
449 ASSERT_TRUE((
bool)
robot_model) <<
"Failed to load robot model";
451 auto group =
robot_model->getJointModelGroup(
"group");
452 ASSERT_TRUE((
bool)group) <<
"Failed to load joint model group ";
463 ASSERT_TRUE(totg.
computeTimeStamps(trajectory)) <<
"Failed to compute time stamps";
466 int main(
int argc,
char** argv)
468 testing::InitGoogleTest(&argc, argv);
469 return RUN_ALL_TESTS();