@@ -373,29 +373,36 @@ void RobotTrajectory::getRobotTrajectoryMsg(moveit_msgs::msg::RobotTrajectory& t
373
373
{
374
374
const std::vector<std::string> names = mdof[j]->getVariableNames ();
375
375
const double * velocities = waypoints_[i]->getJointVelocities (mdof[j]);
376
+ const double * accelerations = waypoints_[i]->getJointAccelerations (mdof[j]);
376
377
377
378
geometry_msgs::msg::Twist point_velocity;
379
+ geometry_msgs::msg::Twist point_acceleration;
378
380
379
381
for (std::size_t k = 0 ; k < names.size (); ++k)
380
382
{
381
383
if (names[k].find (" /x" ) != std::string::npos)
382
384
{
383
385
point_velocity.linear .x = velocities[k];
386
+ point_acceleration.linear .x = accelerations[k];
384
387
}
385
388
else if (names[k].find (" /y" ) != std::string::npos)
386
389
{
387
390
point_velocity.linear .y = velocities[k];
391
+ point_acceleration.linear .y = accelerations[k];
388
392
}
389
393
else if (names[k].find (" /z" ) != std::string::npos)
390
394
{
391
395
point_velocity.linear .z = velocities[k];
396
+ point_acceleration.linear .z = accelerations[k];
392
397
}
393
398
else if (names[k].find (" /theta" ) != std::string::npos)
394
399
{
395
400
point_velocity.angular .z = velocities[k];
401
+ point_acceleration.angular .z = accelerations[k];
396
402
}
397
403
}
398
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);
399
406
}
400
407
}
401
408
if (duration_from_previous_.size () > i)
@@ -707,4 +714,129 @@ std::optional<double> waypointDensity(const RobotTrajectory& trajectory)
707
714
return std::nullopt;
708
715
}
709
716
717
+ std::optional<trajectory_msgs::msg::JointTrajectory> toJointTrajectory (const RobotTrajectory& trajectory,
718
+ bool include_mdof_joints,
719
+ const std::vector<std::string>& joint_filter)
720
+ {
721
+ const auto group = trajectory.getGroup ();
722
+ const auto robot_model = trajectory.getRobotModel ();
723
+ const std::vector<const moveit::core::JointModel*>& jnts =
724
+ group ? group->getActiveJointModels () : robot_model->getActiveJointModels ();
725
+
726
+ if (trajectory.empty () || jnts.empty ())
727
+ return std::nullopt;
728
+
729
+ trajectory_msgs::msg::JointTrajectory joint_trajectory;
730
+ std::vector<const moveit::core::JointModel*> onedof;
731
+ std::vector<const moveit::core::JointModel*> mdof;
732
+
733
+ for (const moveit::core::JointModel* active_joint : jnts)
734
+ {
735
+ // only consider joints listed in joint_filter
736
+ if (!joint_filter.empty () &&
737
+ std::find (joint_filter.begin (), joint_filter.end (), active_joint->getName ()) == joint_filter.end ())
738
+ continue ;
739
+
740
+ if (active_joint->getVariableCount () == 1 )
741
+ {
742
+ onedof.push_back (active_joint);
743
+ }
744
+ else if (include_mdof_joints)
745
+ {
746
+ mdof.push_back (active_joint);
747
+ }
748
+ }
749
+
750
+ for (const auto & joint : onedof)
751
+ {
752
+ joint_trajectory.joint_names .push_back (joint->getName ());
753
+ }
754
+ for (const auto & joint : mdof)
755
+ {
756
+ for (const auto & name : joint->getVariableNames ())
757
+ {
758
+ joint_trajectory.joint_names .push_back (name);
759
+ }
760
+ }
761
+
762
+ if (!onedof.empty () || !mdof.empty ())
763
+ {
764
+ joint_trajectory.header .frame_id = robot_model->getModelFrame ();
765
+ joint_trajectory.header .stamp = rclcpp::Time (0 , 0 , RCL_ROS_TIME);
766
+ joint_trajectory.points .resize (trajectory.getWayPointCount ());
767
+ }
768
+
769
+ static const auto ZERO_DURATION = rclcpp::Duration::from_seconds (0 );
770
+ double total_time = 0.0 ;
771
+ for (std::size_t i = 0 ; i < trajectory.getWayPointCount (); ++i)
772
+ {
773
+ total_time += trajectory.getWayPointDurationFromPrevious (i);
774
+ joint_trajectory.points [i].time_from_start = rclcpp::Duration::from_seconds (total_time);
775
+ const auto & waypoint = trajectory.getWayPoint (i);
776
+
777
+ if (!onedof.empty ())
778
+ {
779
+ joint_trajectory.points [i].positions .resize (onedof.size ());
780
+ joint_trajectory.points [i].velocities .reserve (onedof.size ());
781
+
782
+ for (std::size_t j = 0 ; j < onedof.size (); ++j)
783
+ {
784
+ joint_trajectory.points [i].positions [j] = waypoint.getVariablePosition (onedof[j]->getFirstVariableIndex ());
785
+ // if we have velocities/accelerations/effort, copy those too
786
+ if (waypoint.hasVelocities ())
787
+ {
788
+ joint_trajectory.points [i].velocities .push_back (
789
+ waypoint.getVariableVelocity (onedof[j]->getFirstVariableIndex ()));
790
+ }
791
+ if (waypoint.hasAccelerations ())
792
+ {
793
+ joint_trajectory.points [i].accelerations .push_back (
794
+ waypoint.getVariableAcceleration (onedof[j]->getFirstVariableIndex ()));
795
+ }
796
+ if (waypoint.hasEffort ())
797
+ {
798
+ joint_trajectory.points [i].effort .push_back (waypoint.getVariableEffort (onedof[j]->getFirstVariableIndex ()));
799
+ }
800
+ }
801
+ // clear velocities if we have an incomplete specification
802
+ if (joint_trajectory.points [i].velocities .size () != onedof.size ())
803
+ joint_trajectory.points [i].velocities .clear ();
804
+ // clear accelerations if we have an incomplete specification
805
+ if (joint_trajectory.points [i].accelerations .size () != onedof.size ())
806
+ joint_trajectory.points [i].accelerations .clear ();
807
+ // clear effort if we have an incomplete specification
808
+ if (joint_trajectory.points [i].effort .size () != onedof.size ())
809
+ joint_trajectory.points [i].effort .clear ();
810
+ }
811
+
812
+ if (!mdof.empty ())
813
+ {
814
+ for (const auto joint : mdof)
815
+ {
816
+ // Add variable placeholders
817
+ const std::vector<std::string> names = joint->getVariableNames ();
818
+ joint_trajectory.points [i].positions .reserve (joint_trajectory.points [i].positions .size () + names.size ());
819
+
820
+ joint_trajectory.points [i].velocities .reserve (joint_trajectory.points [i].velocities .size () + names.size ());
821
+
822
+ for (const auto & name : names)
823
+ {
824
+ joint_trajectory.points [i].positions .push_back (waypoint.getVariablePosition (name));
825
+
826
+ if (waypoint.hasVelocities ())
827
+ {
828
+ joint_trajectory.points [i].velocities .push_back (waypoint.getVariableVelocity (name));
829
+ }
830
+ if (waypoint.hasAccelerations ())
831
+ {
832
+ joint_trajectory.points [i].accelerations .push_back (waypoint.getVariableAcceleration (name));
833
+ }
834
+ }
835
+ }
836
+ }
837
+ }
838
+
839
+ return joint_trajectory;
840
+ }
841
+
710
842
} // end of namespace robot_trajectory
0 commit comments