42 #include <moveit_msgs/msg/robot_trajectory.hpp>
43 #include <sensor_msgs/msg/joint_state.hpp>
44 #include <std_msgs/msg/string.hpp>
45 #include <rclcpp/rclcpp.hpp>
47 #include <pluginlib/class_loader.hpp>
53 #include <moveit_trajectory_execution_manager_export.h>
88 const planning_scene_monitor::CurrentStateMonitorPtr& csm);
92 const planning_scene_monitor::CurrentStateMonitorPtr& csm,
bool manage_controllers);
98 bool isManagingControllers()
const;
101 const moveit_controller_manager::MoveItControllerManagerPtr& getControllerManager()
const;
104 void processEvent(
const std::string& event);
110 bool ensureActiveControllersForGroup(
const std::string&
group);
116 bool ensureActiveControllersForJoints(
const std::vector<std::string>& joints);
121 bool ensureActiveController(
const std::string& controller);
126 bool ensureActiveControllers(
const std::vector<std::string>& controllers);
129 bool isControllerActive(
const std::string& controller);
132 bool areControllersActive(
const std::vector<std::string>& controllers);
136 bool push(
const moveit_msgs::msg::RobotTrajectory& trajectory,
const std::string& controller =
"");
140 bool push(
const trajectory_msgs::msg::JointTrajectory& trajectory,
const std::string& controller =
"");
147 bool push(
const trajectory_msgs::msg::JointTrajectory& trajectory,
const std::vector<std::string>& controllers);
154 bool push(
const moveit_msgs::msg::RobotTrajectory& trajectory,
const std::vector<std::string>& controllers);
157 const std::vector<TrajectoryExecutionContext*>& getTrajectories()
const;
165 bool auto_clear =
true);
173 bool pushAndExecute(
const moveit_msgs::msg::RobotTrajectory& trajectory,
const std::string& controller =
"");
177 bool pushAndExecute(
const trajectory_msgs::msg::JointTrajectory& trajectory,
const std::string& controller =
"");
182 bool pushAndExecute(
const sensor_msgs::msg::JointState& state,
const std::string& controller =
"");
189 bool pushAndExecute(
const trajectory_msgs::msg::JointTrajectory& trajectory,
190 const std::vector<std::string>& controllers);
197 bool pushAndExecute(
const moveit_msgs::msg::RobotTrajectory& trajectory,
const std::vector<std::string>& controllers);
204 bool pushAndExecute(
const sensor_msgs::msg::JointState& state,
const std::vector<std::string>& controllers);
216 std::pair<int, int> getCurrentExpectedTrajectoryIndex()
const;
222 void stopExecution(
bool auto_clear =
true);
229 void enableExecutionDurationMonitoring(
bool flag);
233 void setAllowedExecutionDurationScaling(
double scaling);
237 void setAllowedGoalDurationMargin(
double margin);
241 void setExecutionVelocityScaling(
double scaling);
244 void setAllowedStartTolerance(
double tolerance);
247 void setWaitForTrajectoryCompletion(
bool flag);
251 return controller_mgr_node_;
255 struct ControllerInformation
258 std::set<std::string> joints_;
259 std::set<std::string> overlapping_controllers_;
261 rclcpp::Time last_update_{ 0, 0, RCL_ROS_TIME };
263 bool operator<(ControllerInformation& other)
const
265 if (joints_.size() != other.joints_.size())
266 return joints_.size() < other.joints_.size();
267 return name_ < other.name_;
273 void reloadControllerInformation();
276 bool validate(
const TrajectoryExecutionContext& context)
const;
277 bool configure(TrajectoryExecutionContext& context,
const moveit_msgs::msg::RobotTrajectory& trajectory,
278 const std::vector<std::string>& controllers);
280 void updateControllersState(
const rclcpp::Duration& age);
281 void updateControllerState(
const std::string& controller,
const rclcpp::Duration& age);
282 void updateControllerState(ControllerInformation& ci,
const rclcpp::Duration& age);
284 bool distributeTrajectory(
const moveit_msgs::msg::RobotTrajectory& trajectory,
285 const std::vector<std::string>& controllers,
286 std::vector<moveit_msgs::msg::RobotTrajectory>& parts);
288 bool findControllers(
const std::set<std::string>& actuated_joints, std::size_t controller_count,
289 const std::vector<std::string>& available_controllers,
290 std::vector<std::string>& selected_controllers);
291 bool checkControllerCombination(std::vector<std::string>& controllers,
const std::set<std::string>& actuated_joints);
292 void generateControllerCombination(std::size_t start_index, std::size_t controller_count,
293 const std::vector<std::string>& available_controllers,
294 std::vector<std::string>& selected_controllers,
296 const std::set<std::string>& actuated_joints);
297 bool selectControllers(
const std::set<std::string>& actuated_joints,
298 const std::vector<std::string>& available_controllers,
299 std::vector<std::string>& selected_controllers);
301 void executeThread(
const ExecutionCompleteCallback& callback,
const PathSegmentCompleteCallback& part_callback,
303 bool executePart(std::size_t part_index);
304 bool waitForRobotToStop(
const TrajectoryExecutionContext& context,
double wait_time = 1.0);
305 void continuousExecutionThread();
307 void stopExecutionInternal();
309 void receiveEvent(
const std_msgs::msg::String::ConstSharedPtr& event);
311 void loadControllerParams();
314 const std::string name_ =
"trajectory_execution_manager";
316 rclcpp::Node::SharedPtr node_;
317 rclcpp::Node::SharedPtr controller_mgr_node_;
318 std::shared_ptr<rclcpp::executors::SingleThreadedExecutor> private_executor_;
319 std::thread private_executor_thread_;
320 moveit::core::RobotModelConstPtr robot_model_;
321 planning_scene_monitor::CurrentStateMonitorPtr csm_;
322 rclcpp::Subscription<std_msgs::msg::String>::SharedPtr event_topic_subscriber_;
323 std::map<std::string, ControllerInformation> known_controllers_;
324 bool manage_controllers_;
327 std::unique_ptr<std::thread> execution_thread_;
330 std::unique_ptr<std::thread> continuous_execution_thread_;
332 std::mutex execution_state_mutex_;
333 std::mutex continuous_execution_mutex_;
334 std::mutex execution_thread_mutex_;
336 std::condition_variable continuous_execution_condition_;
339 std::condition_variable execution_complete_condition_;
342 std::vector<moveit_controller_manager::MoveItControllerHandlePtr> active_handles_;
343 int current_context_;
344 std::vector<rclcpp::Time> time_index_;
345 mutable std::mutex time_index_mutex_;
346 bool execution_complete_;
348 bool stop_continuous_execution_;
349 bool run_continuous_execution_thread_;
350 std::vector<TrajectoryExecutionContext*> trajectories_;
351 std::deque<TrajectoryExecutionContext*> continuous_execution_queue_;
353 std::unique_ptr<pluginlib::ClassLoader<moveit_controller_manager::MoveItControllerManager> > controller_manager_loader_;
354 moveit_controller_manager::MoveItControllerManagerPtr controller_manager_;
358 bool execution_duration_monitoring_;
360 double allowed_execution_duration_scaling_;
361 double allowed_goal_duration_margin_;
364 std::map<std::string, double> controller_allowed_execution_duration_scaling_;
365 std::map<std::string, double> controller_allowed_goal_duration_margin_;
367 double allowed_start_tolerance_;
368 double execution_velocity_scaling_;
369 bool wait_for_trajectory_completion_;
371 rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr callback_handler_;
std::function< void(const moveit_controller_manager::ExecutionStatus &)> ExecutionCompleteCallback
rclcpp::Node::SharedPtr getControllerManagerNode()
static const std::string EXECUTION_EVENT_TOPIC
std::function< void(std::size_t)> PathSegmentCompleteCallback
MOVEIT_CLASS_FORWARD(TrajectoryExecutionManager)
The reported execution status.
Each controller known to MoveIt has a state. This structure describes that controller's state.
Data structure that represents information necessary to execute a trajectory.
std::vector< moveit_msgs::msg::RobotTrajectory > trajectory_parts_
std::vector< std::string > controllers_
The controllers to use for executing the different trajectory parts;.
std::vector< std::vector< std::string > > selected_options