41 #include <gtest/gtest.h>
75 EXPECT_EQ(trajectory->getGroupName(),
arm_jmg_name_) <<
"Generated trajectory group name does not match";
76 EXPECT_TRUE(trajectory->empty()) <<
"Generated trajectory not empty";
78 double duration_from_previous = 0.1;
79 std::size_t waypoint_count = 5;
80 for (std::size_t ix = 0; ix < waypoint_count; ++ix)
82 trajectory->addSuffixWayPoint(*
robot_state_, duration_from_previous);
86 EXPECT_EQ(trajectory->getDuration(), duration_from_previous * waypoint_count)
87 <<
"Generated trajectory duration incorrect";
88 EXPECT_EQ(waypoint_count, trajectory->getWayPointDurations().size())
89 <<
"Generated trajectory has the wrong number of waypoints";
90 EXPECT_EQ(waypoint_count, trajectory->size());
94 robot_trajectory::RobotTrajectoryPtr& trajectory_copy,
bool deepcopy)
97 trajectory_copy = std::make_shared<robot_trajectory::RobotTrajectory>(*trajectory, deepcopy);
99 EXPECT_EQ(trajectory_copy->getDuration(), trajectory->getDuration());
100 EXPECT_EQ(trajectory_copy->getWayPointDurations().size(), trajectory->getWayPointDurations().size());
109 moveit::core::RobotStatePtr trajectory_first_waypoint = trajectory->getWayPointPtr(0);
111 std::vector<double> trajectory_first_state;
112 trajectory_first_waypoint->copyJointGroupPositions(
arm_jmg_name_, trajectory_first_state);
115 trajectory_first_state[0] += 0.01;
116 trajectory_first_waypoint->setJointGroupPositions(
arm_jmg_name_, trajectory_first_state);
119 moveit::core::RobotStatePtr trajectory_first_waypoint_after_update = trajectory->getWayPointPtr(0);
120 std::vector<double> trajectory_first_state_after_update;
121 trajectory_first_waypoint_after_update->copyJointGroupPositions(
arm_jmg_name_, trajectory_first_state_after_update);
122 EXPECT_EQ(trajectory_first_state[0], trajectory_first_state_after_update[0]);
125 double trajectory_first_duration_before_update = trajectory->getWayPointDurationFromPrevious(0);
126 double new_duration = trajectory_first_duration_before_update + 0.1;
127 trajectory->setWayPointDurationFromPrevious(0, new_duration);
130 EXPECT_EQ(trajectory->getWayPointDurationFromPrevious(0), new_duration);
141 std::vector<double> trajectory_first_state;
145 trajectory_first_state[0] += 0.01;
150 std::vector<double> trajectory_first_state_after_update;
152 EXPECT_NE(trajectory_first_state[0], trajectory_first_state_after_update[0]);
158 robot_trajectory::RobotTrajectoryPtr trajectory;
159 initTestTrajectory(trajectory);
160 modifyFirstWaypointPtrAndCheckTrajectory(trajectory);
165 robot_trajectory::RobotTrajectoryPtr trajectory;
166 initTestTrajectory(trajectory);
167 modifyFirstWaypointAndCheckTrajectory(trajectory);
172 robot_trajectory::RobotTrajectoryPtr trajectory;
173 initTestTrajectory(trajectory);
174 moveit_msgs::msg::RobotTrajectory initial_trajectory_msg;
175 trajectory->getRobotTrajectoryMsg(initial_trajectory_msg);
177 trajectory->reverse().reverse();
179 moveit_msgs::msg::RobotTrajectory edited_trajectory_msg;
180 trajectory->getRobotTrajectoryMsg(edited_trajectory_msg);
182 EXPECT_EQ(initial_trajectory_msg, edited_trajectory_msg);
187 robot_trajectory::RobotTrajectoryPtr initial_trajectory;
188 initTestTrajectory(initial_trajectory);
189 moveit_msgs::msg::RobotTrajectory initial_trajectory_msg;
190 initial_trajectory->getRobotTrajectoryMsg(initial_trajectory_msg);
200 .
append(*initial_trajectory, 0.1);
203 EXPECT_EQ(trajectory.
getWayPointCount(), initial_trajectory->getWayPointCount() * 2 + 3);
208 robot_trajectory::RobotTrajectoryPtr initial_trajectory;
209 initTestTrajectory(initial_trajectory);
210 EXPECT_EQ(initial_trajectory->getWayPointCount(),
size_t(5));
213 robot_trajectory::RobotTrajectoryPtr traj2;
214 initTestTrajectory(traj2);
215 EXPECT_EQ(traj2->getWayPointCount(),
size_t(5));
218 const double expected_duration = 0.1;
219 initial_trajectory->append(*traj2, expected_duration, 0, 5);
220 EXPECT_EQ(initial_trajectory->getWayPointCount(),
size_t(10));
222 EXPECT_EQ(initial_trajectory->getWayPointDurationFromPrevious(4), expected_duration);
223 EXPECT_EQ(initial_trajectory->getWayPointDurationFromPrevious(5), expected_duration);
224 EXPECT_EQ(initial_trajectory->getWayPointDurationFromPrevious(6), expected_duration);
229 bool deepcopy =
false;
231 robot_trajectory::RobotTrajectoryPtr trajectory;
232 robot_trajectory::RobotTrajectoryPtr trajectory_copy;
234 initTestTrajectory(trajectory);
235 copyTrajectory(trajectory, trajectory_copy, deepcopy);
236 modifyFirstWaypointPtrAndCheckTrajectory(trajectory);
240 std::vector<double> trajectory_first_state_after_update;
241 trajectory_first_waypoint_after_update.
copyJointGroupPositions(arm_jmg_name_, trajectory_first_state_after_update);
245 std::vector<double> trajectory_copy_first_state_after_update;
247 trajectory_copy_first_state_after_update);
250 EXPECT_EQ(trajectory_first_state_after_update[0], trajectory_copy_first_state_after_update[0]);
255 bool deepcopy =
true;
257 robot_trajectory::RobotTrajectoryPtr trajectory;
258 robot_trajectory::RobotTrajectoryPtr trajectory_copy;
260 initTestTrajectory(trajectory);
261 copyTrajectory(trajectory, trajectory_copy, deepcopy);
262 modifyFirstWaypointPtrAndCheckTrajectory(trajectory);
266 std::vector<double> trajectory_first_state_after_update;
267 trajectory_first_waypoint_after_update.
copyJointGroupPositions(arm_jmg_name_, trajectory_first_state_after_update);
271 std::vector<double> trajectory_copy_first_state_after_update;
273 trajectory_copy_first_state_after_update);
276 EXPECT_NE(trajectory_first_state_after_update[0], trajectory_copy_first_state_after_update[0]);
278 EXPECT_NE(trajectory->getWayPointDurationFromPrevious(0), trajectory_copy->getWayPointDurationFromPrevious(0));
283 robot_trajectory::RobotTrajectoryPtr trajectory;
284 initTestTrajectory(trajectory);
286 ASSERT_EQ(5u, trajectory->size());
287 std::vector<double> positions;
289 double start_pos = 0.0;
291 for (
size_t i = 0; i < trajectory->size(); ++i)
293 auto waypoint = trajectory->getWayPointPtr(i);
295 waypoint->copyJointGroupPositions(arm_jmg_name_, positions);
296 start_pos = positions[0];
297 positions[0] += 0.01 * i;
298 waypoint->setJointGroupPositions(arm_jmg_name_, positions);
301 unsigned int count = 0;
302 for (
const auto& waypoint_and_duration : *trajectory)
304 const auto& waypoint = waypoint_and_duration.first;
305 waypoint->copyJointGroupPositions(arm_jmg_name_, positions);
306 EXPECT_EQ(start_pos + count * 0.01, positions[0]);
310 EXPECT_EQ(count, trajectory->size());
313 EXPECT_EQ(trajectory->begin(), trajectory->begin());
314 EXPECT_EQ(trajectory->end(), trajectory->end());
317 EXPECT_NE(trajectory->begin(), trajectory->end());
318 EXPECT_NE(++trajectory->begin(), trajectory->end());
319 EXPECT_NE(++(++trajectory->begin()), trajectory->end());
320 EXPECT_NE(++(++(++trajectory->begin())), trajectory->end());
321 EXPECT_NE(++(++(++(++trajectory->begin()))), trajectory->end());
322 EXPECT_EQ(++(++(++(++(++trajectory->begin())))), trajectory->end());
327 robot_trajectory::RobotTrajectoryPtr trajectory;
328 initTestTrajectory(trajectory);
332 std::vector<double> positions;
333 for (
size_t i = 0; i < trajectory->size(); ++i)
335 auto waypoint = trajectory->getWayPointPtr(i);
336 waypoint->copyJointGroupPositions(arm_jmg_name_, positions);
337 positions[0] += 0.01 * i;
338 waypoint->setJointGroupPositions(arm_jmg_name_, positions);
345 robot_trajectory::RobotTrajectoryPtr trajectory;
346 initTestTrajectory(trajectory);
349 std::vector<double> positions;
350 for (
size_t i = 0; i < trajectory->size(); ++i)
352 auto waypoint = trajectory->getWayPointPtr(i);
353 waypoint->copyJointGroupPositions(arm_jmg_name_, positions);
354 positions[0] += 0.01 * i;
355 waypoint->setJointGroupPositions(arm_jmg_name_, positions);
369 robot_trajectory::RobotTrajectoryPtr trajectory;
370 initTestTrajectory(trajectory);
374 ASSERT_FALSE(density.has_value());
377 std::vector<double> positions;
378 for (
size_t i = 0; i < trajectory->size(); ++i)
380 auto waypoint = trajectory->getWayPointPtr(i);
381 waypoint->copyJointGroupPositions(arm_jmg_name_, positions);
382 positions[0] += 0.01 * i;
383 waypoint->setJointGroupPositions(arm_jmg_name_, positions);
387 ASSERT_TRUE(density.has_value());
388 EXPECT_GT(density.value(), 0.0);
393 EXPECT_FALSE(density.has_value());
396 int main(
int argc,
char** argv)
398 testing::InitGoogleTest(&argc, argv);
399 return RUN_ALL_TESTS();
void modifyFirstWaypointPtrAndCheckTrajectory(robot_trajectory::RobotTrajectoryPtr &trajectory)
const std::string robot_model_name_
void copyTrajectory(const robot_trajectory::RobotTrajectoryPtr &trajectory, robot_trajectory::RobotTrajectoryPtr &trajectory_copy, bool deepcopy)
moveit::core::RobotModelConstPtr robot_model_
moveit::core::RobotStatePtr robot_state_
void modifyFirstWaypointAndCheckTrajectory(robot_trajectory::RobotTrajectoryPtr &trajectory)
const std::string arm_jmg_name_
const std::string arm_state_name_
void initTestTrajectory(robot_trajectory::RobotTrajectoryPtr &trajectory)
Representation of a robot's state. This includes position, velocity, acceleration and effort.
void setJointGroupPositions(const std::string &joint_group_name, const double *gstate)
Given positions for the variables that make up a group, in the order found in the group (including va...
void copyJointGroupPositions(const std::string &joint_group_name, std::vector< double > &gstate) const
For a given group, copy the position values of the variables that make up the group into another loca...
Maintain a sequence of waypoints and the time durations between these waypoints.
RobotTrajectory & reverse()
const std::string & getGroupName() const
RobotTrajectory & setGroupName(const std::string &group_name)
RobotTrajectory & addSuffixWayPoint(const moveit::core::RobotState &state, double dt)
Add a point to the trajectory.
RobotTrajectory & setRobotTrajectoryMsg(const moveit::core::RobotState &reference_state, const trajectory_msgs::msg::JointTrajectory &trajectory)
Copy the content of the trajectory message into this class. The trajectory message itself is not requ...
RobotTrajectory & append(const RobotTrajectory &source, double dt, size_t start_index=0, size_t end_index=std::numeric_limits< std::size_t >::max())
Add a specified part of a trajectory to the end of the current trajectory. The default (when start_in...
RobotTrajectory & insertWayPoint(std::size_t index, const moveit::core::RobotState &state, double dt)
std::size_t getWayPointCount() const
RobotTrajectory & addPrefixWayPoint(const moveit::core::RobotState &state, double dt)
RobotTrajectory & clear()
moveit::core::RobotModelPtr loadTestingRobotModel(const std::string &robot_name)
Loads a robot from moveit_resources.
double path_length(RobotTrajectory const &trajectory)
Calculate the path length of a given trajectory based on the accumulated robot state distances....
std::optional< double > waypoint_density(RobotTrajectory const &trajectory)
Calculate the waypoint density of a trajectory.
std::optional< double > smoothness(RobotTrajectory const &trajectory)
Calculate the smoothness of a given trajectory.
int main(int argc, char **argv)
TEST_F(RobotTrajectoryTestFixture, ModifyFirstWaypointByPtr)