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 bool deepcopy =
false;
210 robot_trajectory::RobotTrajectoryPtr trajectory;
211 robot_trajectory::RobotTrajectoryPtr trajectory_copy;
213 initTestTrajectory(trajectory);
214 copyTrajectory(trajectory, trajectory_copy, deepcopy);
215 modifyFirstWaypointPtrAndCheckTrajectory(trajectory);
219 std::vector<double> trajectory_first_state_after_update;
220 trajectory_first_waypoint_after_update.
copyJointGroupPositions(arm_jmg_name_, trajectory_first_state_after_update);
224 std::vector<double> trajectory_copy_first_state_after_update;
226 trajectory_copy_first_state_after_update);
229 EXPECT_EQ(trajectory_first_state_after_update[0], trajectory_copy_first_state_after_update[0]);
234 bool deepcopy =
true;
236 robot_trajectory::RobotTrajectoryPtr trajectory;
237 robot_trajectory::RobotTrajectoryPtr trajectory_copy;
239 initTestTrajectory(trajectory);
240 copyTrajectory(trajectory, trajectory_copy, deepcopy);
241 modifyFirstWaypointPtrAndCheckTrajectory(trajectory);
245 std::vector<double> trajectory_first_state_after_update;
246 trajectory_first_waypoint_after_update.
copyJointGroupPositions(arm_jmg_name_, trajectory_first_state_after_update);
250 std::vector<double> trajectory_copy_first_state_after_update;
252 trajectory_copy_first_state_after_update);
255 EXPECT_NE(trajectory_first_state_after_update[0], trajectory_copy_first_state_after_update[0]);
257 EXPECT_NE(trajectory->getWayPointDurationFromPrevious(0), trajectory_copy->getWayPointDurationFromPrevious(0));
262 robot_trajectory::RobotTrajectoryPtr trajectory;
263 initTestTrajectory(trajectory);
265 ASSERT_EQ(5u, trajectory->size());
266 std::vector<double> positions;
268 double start_pos = 0.0;
270 for (
size_t i = 0; i < trajectory->size(); ++i)
272 auto waypoint = trajectory->getWayPointPtr(i);
274 waypoint->copyJointGroupPositions(arm_jmg_name_, positions);
275 start_pos = positions[0];
276 positions[0] += 0.01 * i;
277 waypoint->setJointGroupPositions(arm_jmg_name_, positions);
280 unsigned int count = 0;
281 for (
const auto& waypoint_and_duration : *trajectory)
283 const auto& waypoint = waypoint_and_duration.first;
284 waypoint->copyJointGroupPositions(arm_jmg_name_, positions);
285 EXPECT_EQ(start_pos + count * 0.01, positions[0]);
289 EXPECT_EQ(count, trajectory->size());
292 EXPECT_EQ(trajectory->begin(), trajectory->begin());
293 EXPECT_EQ(trajectory->end(), trajectory->end());
296 EXPECT_NE(trajectory->begin(), trajectory->end());
297 EXPECT_NE(++trajectory->begin(), trajectory->end());
298 EXPECT_NE(++(++trajectory->begin()), trajectory->end());
299 EXPECT_NE(++(++(++trajectory->begin())), trajectory->end());
300 EXPECT_NE(++(++(++(++trajectory->begin()))), trajectory->end());
301 EXPECT_EQ(++(++(++(++(++trajectory->begin())))), trajectory->end());
306 robot_trajectory::RobotTrajectoryPtr trajectory;
307 initTestTrajectory(trajectory);
311 std::vector<double> positions;
312 for (
size_t i = 0; i < trajectory->size(); ++i)
314 auto waypoint = trajectory->getWayPointPtr(i);
315 waypoint->copyJointGroupPositions(arm_jmg_name_, positions);
316 positions[0] += 0.01 * i;
317 waypoint->setJointGroupPositions(arm_jmg_name_, positions);
324 robot_trajectory::RobotTrajectoryPtr trajectory;
325 initTestTrajectory(trajectory);
328 std::vector<double> positions;
329 for (
size_t i = 0; i < trajectory->size(); ++i)
331 auto waypoint = trajectory->getWayPointPtr(i);
332 waypoint->copyJointGroupPositions(arm_jmg_name_, positions);
333 positions[0] += 0.01 * i;
334 waypoint->setJointGroupPositions(arm_jmg_name_, positions);
348 robot_trajectory::RobotTrajectoryPtr trajectory;
349 initTestTrajectory(trajectory);
353 ASSERT_FALSE(density.has_value());
356 std::vector<double> positions;
357 for (
size_t i = 0; i < trajectory->size(); ++i)
359 auto waypoint = trajectory->getWayPointPtr(i);
360 waypoint->copyJointGroupPositions(arm_jmg_name_, positions);
361 positions[0] += 0.01 * i;
362 waypoint->setJointGroupPositions(arm_jmg_name_, positions);
366 ASSERT_TRUE(density.has_value());
367 EXPECT_GT(density.value(), 0.0);
372 EXPECT_FALSE(density.has_value());
375 int main(
int argc,
char** argv)
377 testing::InitGoogleTest(&argc, argv);
378 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)