134 end_index = std::min(end_index, source.waypoints_.size());
135 if (start_index >= end_index)
137 waypoints_.insert(waypoints_.end(), std::next(source.waypoints_.begin(), start_index),
138 std::next(source.waypoints_.begin(), end_index));
139 std::size_t index = duration_from_previous_.size();
140 duration_from_previous_.insert(duration_from_previous_.end(),
141 std::next(source.duration_from_previous_.begin(), start_index),
142 std::next(source.duration_from_previous_.begin(), end_index));
143 if (duration_from_previous_.size() > index)
144 duration_from_previous_[index] = dt;
169 if (waypoints_.empty())
172 const std::vector<const moveit::core::JointModel*>& cont_joints =
178 double running_offset = 0.0;
179 double last_value = waypoints_[0]->getJointPositions(cont_joint)[0];
180 cont_joint->enforcePositionBounds(&last_value);
181 waypoints_[0]->setJointPositions(cont_joint, &last_value);
183 for (std::size_t j = 1; j < waypoints_.size(); ++j)
185 double current_value = waypoints_[j]->getJointPositions(cont_joint)[0];
186 cont_joint->enforcePositionBounds(¤t_value);
187 if (last_value > current_value + M_PI)
189 running_offset += 2.0 * M_PI;
191 else if (current_value > last_value + M_PI)
193 running_offset -= 2.0 * M_PI;
196 last_value = current_value;
197 current_value += running_offset;
198 waypoints_[j]->setJointPositions(cont_joint, ¤t_value);
201 for (moveit::core::RobotStatePtr& waypoint : waypoints_)
211 if (waypoints_.empty())
214 const std::vector<const moveit::core::JointModel*>& cont_joints =
220 double reference_value = reference_value0;
221 cont_joint->enforcePositionBounds(&reference_value);
224 double running_offset = reference_value0 - reference_value;
226 double last_value = waypoints_[0]->getJointPositions(cont_joint)[0];
227 cont_joint->enforcePositionBounds(&last_value);
228 if (last_value > reference_value + M_PI)
230 running_offset -= 2.0 * M_PI;
232 else if (last_value < reference_value - M_PI)
234 running_offset += 2.0 * M_PI;
236 double current_start_value = last_value + running_offset;
237 waypoints_[0]->setJointPositions(cont_joint, ¤t_start_value);
239 for (std::size_t j = 1; j < waypoints_.size(); ++j)
241 double current_value = waypoints_[j]->getJointPositions(cont_joint)[0];
242 cont_joint->enforcePositionBounds(¤t_value);
243 if (last_value > current_value + M_PI)
245 running_offset += 2.0 * M_PI;
247 else if (current_value > last_value + M_PI)
249 running_offset -= 2.0 * M_PI;
252 last_value = current_value;
253 current_value += running_offset;
254 waypoints_[j]->setJointPositions(cont_joint, ¤t_value);
257 for (moveit::core::RobotStatePtr& waypoint : waypoints_)
266 const std::vector<std::string>& joint_filter)
const
268 trajectory = moveit_msgs::msg::RobotTrajectory();
269 if (waypoints_.empty())
271 const std::vector<const moveit::core::JointModel*>& jnts =
274 std::vector<const moveit::core::JointModel*> onedof;
275 std::vector<const moveit::core::JointModel*> mdof;
276 trajectory.joint_trajectory.joint_names.clear();
277 trajectory.multi_dof_joint_trajectory.joint_names.clear();
282 if (!joint_filter.empty() &&
283 std::find(joint_filter.begin(), joint_filter.end(), active_joint->getName()) == joint_filter.end())
286 if (active_joint->getVariableCount() == 1)
288 trajectory.joint_trajectory.joint_names.push_back(active_joint->getName());
289 onedof.push_back(active_joint);
293 trajectory.multi_dof_joint_trajectory.joint_names.push_back(active_joint->getName());
294 mdof.push_back(active_joint);
300 trajectory.joint_trajectory.header.frame_id = robot_model_->getModelFrame();
301 trajectory.joint_trajectory.header.stamp = rclcpp::Time(0, 0, RCL_ROS_TIME);
302 trajectory.joint_trajectory.points.resize(waypoints_.size());
307 trajectory.multi_dof_joint_trajectory.header.frame_id = robot_model_->getModelFrame();
308 trajectory.multi_dof_joint_trajectory.header.stamp = rclcpp::Time(0, 0, RCL_ROS_TIME);
309 trajectory.multi_dof_joint_trajectory.points.resize(waypoints_.size());
312 static const auto ZERO_DURATION = rclcpp::Duration::from_seconds(0);
313 double total_time = 0.0;
314 for (std::size_t i = 0; i < waypoints_.size(); ++i)
316 if (duration_from_previous_.size() > i)
317 total_time += duration_from_previous_[i];
321 trajectory.joint_trajectory.points[i].positions.resize(onedof.size());
322 trajectory.joint_trajectory.points[i].velocities.reserve(onedof.size());
324 for (std::size_t j = 0; j < onedof.size(); ++j)
326 trajectory.joint_trajectory.points[i].positions[j] =
327 waypoints_[i]->getVariablePosition(onedof[j]->getFirstVariableIndex());
329 if (waypoints_[i]->hasVelocities())
331 trajectory.joint_trajectory.points[i].velocities.push_back(
332 waypoints_[i]->getVariableVelocity(onedof[j]->getFirstVariableIndex()));
334 if (waypoints_[i]->hasAccelerations())
336 trajectory.joint_trajectory.points[i].accelerations.push_back(
337 waypoints_[i]->getVariableAcceleration(onedof[j]->getFirstVariableIndex()));
339 if (waypoints_[i]->hasEffort())
341 trajectory.joint_trajectory.points[i].effort.push_back(
342 waypoints_[i]->getVariableEffort(onedof[j]->getFirstVariableIndex()));
346 if (trajectory.joint_trajectory.points[i].velocities.size() != onedof.size())
347 trajectory.joint_trajectory.points[i].velocities.clear();
349 if (trajectory.joint_trajectory.points[i].accelerations.size() != onedof.size())
350 trajectory.joint_trajectory.points[i].accelerations.clear();
352 if (trajectory.joint_trajectory.points[i].effort.size() != onedof.size())
353 trajectory.joint_trajectory.points[i].effort.clear();
355 if (duration_from_previous_.size() > i)
357 trajectory.joint_trajectory.points[i].time_from_start = rclcpp::Duration::from_seconds(total_time);
361 trajectory.joint_trajectory.points[i].time_from_start = ZERO_DURATION;
366 trajectory.multi_dof_joint_trajectory.points[i].transforms.resize(mdof.size());
367 for (std::size_t j = 0; j < mdof.size(); ++j)
369 geometry_msgs::msg::TransformStamped ts = tf2::eigenToTransform(waypoints_[i]->getJointTransform(mdof[j]));
370 trajectory.multi_dof_joint_trajectory.points[i].transforms[j] = ts.transform;
374 const std::vector<std::string> names = mdof[j]->getVariableNames();
375 const double* velocities = waypoints_[i]->getJointVelocities(mdof[j]);
376 const double* accelerations = waypoints_[i]->getJointAccelerations(mdof[j]);
378 geometry_msgs::msg::Twist point_velocity;
379 geometry_msgs::msg::Twist point_acceleration;
381 for (std::size_t k = 0; k < names.size(); ++k)
383 if (names[k].find(
"/x") != std::string::npos)
385 point_velocity.linear.x = velocities[k];
386 point_acceleration.linear.x = accelerations[k];
388 else if (names[k].find(
"/y") != std::string::npos)
390 point_velocity.linear.y = velocities[k];
391 point_acceleration.linear.y = accelerations[k];
393 else if (names[k].find(
"/z") != std::string::npos)
395 point_velocity.linear.z = velocities[k];
396 point_acceleration.linear.z = accelerations[k];
398 else if (names[k].find(
"/theta") != std::string::npos)
400 point_velocity.angular.z = velocities[k];
401 point_acceleration.angular.z = accelerations[k];
404 trajectory.multi_dof_joint_trajectory.points[i].velocities.push_back(point_velocity);
405 trajectory.multi_dof_joint_trajectory.points[i].accelerations.push_back(point_acceleration);
408 if (duration_from_previous_.size() > i)
410 trajectory.multi_dof_joint_trajectory.points[i].time_from_start = rclcpp::Duration::from_seconds(total_time);
414 trajectory.multi_dof_joint_trajectory.points[i].time_from_start = ZERO_DURATION;
421 const trajectory_msgs::msg::JointTrajectory& trajectory)
426 std::size_t state_count = trajectory.points.size();
427 rclcpp::Time last_time_stamp = trajectory.header.stamp;
428 rclcpp::Time this_time_stamp = last_time_stamp;
430 for (std::size_t i = 0; i < state_count; ++i)
432 this_time_stamp = rclcpp::Time(trajectory.header.stamp) + trajectory.points[i].time_from_start;
433 auto st = std::make_shared<moveit::core::RobotState>(copy);
434 st->setVariablePositions(trajectory.joint_names, trajectory.points[i].positions);
435 if (!trajectory.points[i].velocities.empty())
436 st->setVariableVelocities(trajectory.joint_names, trajectory.points[i].velocities);
437 if (!trajectory.points[i].accelerations.empty())
438 st->setVariableAccelerations(trajectory.joint_names, trajectory.points[i].accelerations);
439 if (!trajectory.points[i].effort.empty())
440 st->setVariableEffort(trajectory.joint_names, trajectory.points[i].effort);
442 last_time_stamp = this_time_stamp;
449 const moveit_msgs::msg::RobotTrajectory& trajectory)
455 std::size_t state_count =
456 std::max(trajectory.joint_trajectory.points.size(), trajectory.multi_dof_joint_trajectory.points.size());
457 rclcpp::Time last_time_stamp = trajectory.joint_trajectory.points.empty() ?
458 trajectory.multi_dof_joint_trajectory.header.stamp :
459 trajectory.joint_trajectory.header.stamp;
460 rclcpp::Time this_time_stamp = last_time_stamp;
462 for (std::size_t i = 0; i < state_count; ++i)
464 auto st = std::make_shared<moveit::core::RobotState>(copy);
465 if (trajectory.joint_trajectory.points.size() > i)
467 st->setVariablePositions(trajectory.joint_trajectory.joint_names, trajectory.joint_trajectory.points[i].positions);
468 if (!trajectory.joint_trajectory.points[i].velocities.empty())
470 st->setVariableVelocities(trajectory.joint_trajectory.joint_names,
471 trajectory.joint_trajectory.points[i].velocities);
473 if (!trajectory.joint_trajectory.points[i].accelerations.empty())
475 st->setVariableAccelerations(trajectory.joint_trajectory.joint_names,
476 trajectory.joint_trajectory.points[i].accelerations);
478 if (!trajectory.joint_trajectory.points[i].effort.empty())
479 st->setVariableEffort(trajectory.joint_trajectory.joint_names, trajectory.joint_trajectory.points[i].effort);
480 this_time_stamp = rclcpp::Time(trajectory.joint_trajectory.header.stamp) +
481 trajectory.joint_trajectory.points[i].time_from_start;
483 if (trajectory.multi_dof_joint_trajectory.points.size() > i)
485 for (std::size_t j = 0; j < trajectory.multi_dof_joint_trajectory.joint_names.size(); ++j)
487 Eigen::Isometry3d t = tf2::transformToEigen(trajectory.multi_dof_joint_trajectory.points[i].transforms[j]);
488 st->setJointPositions(trajectory.multi_dof_joint_trajectory.joint_names[j], t);
490 this_time_stamp = rclcpp::Time(trajectory.multi_dof_joint_trajectory.header.stamp) +
491 trajectory.multi_dof_joint_trajectory.points[i].time_from_start;
495 last_time_stamp = this_time_stamp;
578 out <<
"Empty trajectory.";
582 std::ios::fmtflags old_settings = out.flags();
583 int old_precision = out.precision();
584 out << std::fixed << std::setprecision(3);
586 out <<
"Trajectory has " << num_points <<
" points over " <<
getDuration() <<
" seconds\n";
588 if (variable_indexes.empty())
597 variable_indexes.resize(robot_model_->getVariableCount());
598 std::iota(variable_indexes.begin(), variable_indexes.end(), 0);
602 for (
size_t p_i = 0; p_i < num_points; ++p_i)
605 out <<
" waypoint " << std::setw(3) << p_i;
608 for (
int index : variable_indexes)
615 for (
int index : variable_indexes)
623 for (
int index : variable_indexes)
631 for (
int index : variable_indexes)
639 out.flags(old_settings);
640 out.precision(old_precision);
718 bool include_mdof_joints,
719 const std::vector<std::string>& joint_filter)
721 const auto group = trajectory.
getGroup();
723 const std::vector<const moveit::core::JointModel*>& jnts =
724 group ? group->getActiveJointModels() : robot_model->getActiveJointModels();
726 if (trajectory.
empty() || jnts.empty())
729 trajectory_msgs::msg::JointTrajectory joint_trajectory;
730 std::vector<const moveit::core::JointModel*> onedof;
731 std::vector<const moveit::core::JointModel*> mdof;
736 if (!joint_filter.empty() &&
737 std::find(joint_filter.begin(), joint_filter.end(), active_joint->getName()) == joint_filter.end())
740 if (active_joint->getVariableCount() == 1)
742 onedof.push_back(active_joint);
744 else if (include_mdof_joints)
746 mdof.push_back(active_joint);
750 for (
const auto& joint : onedof)
752 joint_trajectory.joint_names.push_back(joint->getName());
754 for (
const auto& joint : mdof)
756 for (
const auto& name : joint->getVariableNames())
758 joint_trajectory.joint_names.push_back(name);
762 if (!onedof.empty() || !mdof.empty())
764 joint_trajectory.header.frame_id = robot_model->getModelFrame();
765 joint_trajectory.header.stamp = rclcpp::Time(0, 0, RCL_ROS_TIME);
769 static const auto ZERO_DURATION = rclcpp::Duration::from_seconds(0);
770 double total_time = 0.0;
774 joint_trajectory.points[i].time_from_start = rclcpp::Duration::from_seconds(total_time);
779 joint_trajectory.points[i].positions.resize(onedof.size());
780 joint_trajectory.points[i].velocities.reserve(onedof.size());
782 for (std::size_t j = 0; j < onedof.size(); ++j)
784 joint_trajectory.points[i].positions[j] = waypoint.
getVariablePosition(onedof[j]->getFirstVariableIndex());
786 if (waypoint.hasVelocities())
788 joint_trajectory.points[i].velocities.push_back(
789 waypoint.getVariableVelocity(onedof[j]->getFirstVariableIndex()));
791 if (waypoint.hasAccelerations())
793 joint_trajectory.points[i].accelerations.push_back(
794 waypoint.getVariableAcceleration(onedof[j]->getFirstVariableIndex()));
796 if (waypoint.hasEffort())
798 joint_trajectory.points[i].effort.push_back(waypoint.getVariableEffort(onedof[j]->getFirstVariableIndex()));
802 if (joint_trajectory.points[i].velocities.size() != onedof.size())
803 joint_trajectory.points[i].velocities.clear();
805 if (joint_trajectory.points[i].accelerations.size() != onedof.size())
806 joint_trajectory.points[i].accelerations.clear();
808 if (joint_trajectory.points[i].effort.size() != onedof.size())
809 joint_trajectory.points[i].effort.clear();
814 for (
const auto joint : mdof)
817 const std::vector<std::string> names = joint->getVariableNames();
818 joint_trajectory.points[i].positions.reserve(joint_trajectory.points[i].positions.size() + names.size());
820 joint_trajectory.points[i].velocities.reserve(joint_trajectory.points[i].velocities.size() + names.size());
822 for (
const auto& name : names)
824 joint_trajectory.points[i].positions.push_back(waypoint.getVariablePosition(name));
826 if (waypoint.hasVelocities())
828 joint_trajectory.points[i].velocities.push_back(waypoint.getVariableVelocity(name));
830 if (waypoint.hasAccelerations())
832 joint_trajectory.points[i].accelerations.push_back(waypoint.getVariableAcceleration(name));
839 return joint_trajectory;