moveit2
The MoveIt Motion Planning Framework for ROS 2.
robot_trajectory.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2022, Peter David Fagan
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of PickNik Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Peter David Fagan */
36 
37 #include "robot_trajectory.h"
40 
41 namespace moveit_py
42 {
43 namespace bind_robot_trajectory
44 {
45 moveit_msgs::msg::RobotTrajectory
46 getRobotTrajectoryMsg(const robot_trajectory::RobotTrajectoryConstPtr& robot_trajectory,
47  const std::vector<std::string>& joint_filter)
48 {
49  moveit_msgs::msg::RobotTrajectory msg;
50  robot_trajectory->getRobotTrajectoryMsg(msg, joint_filter);
51  return msg;
52 }
53 
55 setRobotTrajectoryMsg(const std::shared_ptr<robot_trajectory::RobotTrajectory>& robot_trajectory,
56  const moveit::core::RobotState& robot_state, const moveit_msgs::msg::RobotTrajectory& msg)
57 {
58  return robot_trajectory->setRobotTrajectoryMsg(robot_state, msg);
59 }
60 
61 void initRobotTrajectory(py::module& m)
62 {
63  py::module robot_trajectory = m.def_submodule("robot_trajectory");
64 
65  py::class_<robot_trajectory::RobotTrajectory, std::shared_ptr<robot_trajectory::RobotTrajectory>>(robot_trajectory,
66  "RobotTrajectory",
67  R"(
68  Maintains a sequence of waypoints and the durations between these waypoints.
69  )")
70 
71  .def(py::init<const std::shared_ptr<const moveit::core::RobotModel>&>(),
72  R"(
73  Initializes an empty robot trajectory from a robot model.
74 
75  Args:
76  :py:class:`moveit_py.core.RobotModel`: The robot model associated to the instantiated robot state.
77 
78  )")
79  .def("__getitem__", &robot_trajectory::RobotTrajectory::getWayPoint, py::arg("idx"),
80  R"(
81  Get the waypoint at the specified index in the trajectory.
82 
83  Returns:
84  :py:class:`moveit_py.core.RobotState`: The robot state corresponding to a waypoint at the specified index in the trajectory.
85  )")
86  .def(
87  "__iter__",
88  [](robot_trajectory::RobotTrajectory& self) { return py::make_iterator(self.begin(), self.end()); },
89  py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */,
90  R"(
91  Iterate over the waypoints in the trajectory.
92  )")
93 
95  R"(
96  Returns:
97  int: The number of waypoints in the trajectory.
98  )")
99 
100  .def("__reverse__", &robot_trajectory::RobotTrajectory::reverse,
101  R"(
102  Reverse the trajectory.
103  )")
104 
105  .def_property("joint_model_group_name", &robot_trajectory::RobotTrajectory::getGroupName,
107  R"(
108  str: The name of the joint model group that this trajectory is for.
109  )")
110 
111  .def_property("robot_model", &robot_trajectory::RobotTrajectory::getRobotModel, nullptr,
112  R"(
113  :py:class:`moveit_py.core.RobotModel`: The robot model that this trajectory is for.
114  )")
115 
116  .def_property("duration", &robot_trajectory::RobotTrajectory::getDuration, nullptr,
117  R"(
118  float: The duration of the trajectory.
119  )")
120 
121  .def_property("average_segment_duration", &robot_trajectory::RobotTrajectory::getAverageSegmentDuration, nullptr,
122  R"(
123  float: The average duration of the segments in the trajectory.
124  )")
125 
126  .def("unwind", py::overload_cast<>(&robot_trajectory::RobotTrajectory::unwind),
127  R"(
128  Unwind the trajectory.
129  )")
130 
131  .def("get_waypoint_durations", &robot_trajectory::RobotTrajectory::getWayPointDurations,
132  R"(
133  Get the durations from the previous waypoint in the trajectory.
134 
135  Returns:
136  list of float: The duration from previous of each waypoint in the trajectory.
137  )")
138  .def("apply_totg_time_parameterization", &trajectory_processing::applyTOTGTimeParameterization,
139  py::arg("velocity_scaling_factor"), py::arg("acceleration_scaling_factor"), py::kw_only(),
140  py::arg("path_tolerance") = 0.1, py::arg("resample_dt") = 0.1, py::arg("min_angle_change") = 0.001,
141  R"(
142  Adds time parameterization to the trajectory using the Time-Optimal Trajectory Generation (TOTG) algorithm.
143 
144  Args:
145  velocity_scaling_factor (float): The velocity scaling factor.
146  acceleration_scaling_factor (float): The acceleration scaling factor.
147  path_tolerance (float): The path tolerance to use for time parameterization (default: 0.1).
148  resample_dt (float): The time step to use for time parameterization (default: 0.1).
149  min_angle_change (float): The minimum angle change to use for time parameterization (default: 0.001).
150  Returns:
151  bool: True if the trajectory was successfully retimed, false otherwise.
152  )")
153  .def("apply_ruckig_smoothing", &trajectory_processing::applyRuckigSmoothing, py::arg("velocity_scaling_factor"),
154  py::arg("acceleration_scaling_factor"), py::kw_only(), py::arg("mitigate_overshoot") = false,
155  py::arg("overshoot_threshold") = 0.01,
156  R"(
157  Applies Ruckig smoothing to the trajectory.
158 
159  Args:
160  velocity_scaling_factor (float): The velocity scaling factor.
161  acceleration_scaling_factor (float): The acceleration scaling factor.
162  mitigate_overshoot (bool): Whether to mitigate overshoot during smoothing (default: false).
163  overshoot_threshold (float): The maximum allowed overshoot during smoothing (default: 0.01).
164  Returns:
165  bool: True if the trajectory was successfully retimed, false otherwise.
166  )")
167  .def("get_robot_trajectory_msg", &moveit_py::bind_robot_trajectory::getRobotTrajectoryMsg,
168  py::arg("joint_filter") = std::vector<std::string>(),
169  R"(
170  Get the trajectory as a moveit_msgs.msg.RobotTrajectory message.
171 
172  Args:
173  joint_filter (list[string]): List of joints to consider in creating the message. If empty, uses all joints.
174  Returns:
175  moveit_msgs.msg.RobotTrajectory: A ROS robot trajectory message.
176  )")
177  .def("set_robot_trajectory_msg", &moveit_py::bind_robot_trajectory::setRobotTrajectoryMsg, py::arg("robot_state"),
178  py::arg("msg"),
179  R"(
180  Set the trajectory from a moveit_msgs.msg.RobotTrajectory message.
181 
182  Args:
183  robot_state (:py:class:`moveit_py.core.RobotState`): The reference robot starting state.
184  msg (moveit_msgs.msg.RobotTrajectory): A ROS robot trajectory message.
185  )");
186  // TODO (peterdavidfagan): support other methods such as appending trajectories
187 }
188 } // namespace bind_robot_trajectory
189 } // namespace moveit_py
Representation of a robot's state. This includes position, velocity, acceleration and effort.
Definition: robot_state.h:90
Maintain a sequence of waypoints and the time durations between these waypoints.
const std::string & getGroupName() const
const moveit::core::RobotModelConstPtr & getRobotModel() const
RobotTrajectory & setGroupName(const std::string &group_name)
const std::deque< double > & getWayPointDurations() const
const moveit::core::RobotState & getWayPoint(std::size_t index) const
robot_trajectory::RobotTrajectory setRobotTrajectoryMsg(const std::shared_ptr< robot_trajectory::RobotTrajectory > &robot_trajectory, const moveit::core::RobotState &robot_state, const moveit_msgs::msg::RobotTrajectory &msg)
moveit_msgs::msg::RobotTrajectory getRobotTrajectoryMsg(const robot_trajectory::RobotTrajectoryConstPtr &robot_trajectory, const std::vector< std::string > &joint_filter)
void initRobotTrajectory(py::module &m)
bool applyTOTGTimeParameterization(robot_trajectory::RobotTrajectory &trajectory, double velocity_scaling_factor, double acceleration_scaling_factor, double path_tolerance=0.1, double resample_dt=0.1, double min_angle_change=0.001)
Applies time parameterization to a robot trajectory using the Time-Optimal Trajectory Generation (TOT...
bool applyRuckigSmoothing(robot_trajectory::RobotTrajectory &trajectory, double velocity_scaling_factor, double acceleration_scaling_factor, bool mitigate_overshoot=false, double overshoot_threshold=0.01)
Applies Ruckig smoothing to a robot trajectory.