moveit2
The MoveIt Motion Planning Framework for ROS 2.
trajectory_monitor.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Willow Garage, Inc.
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 Willow Garage 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: Ioan Sucan */
36 
40 #include <limits>
41 #include <memory>
42 
43 static const rclcpp::Logger LOGGER = rclcpp::get_logger("moveit_ros.planning_scene_monitor.trajectory_monitor");
44 
45 planning_scene_monitor::TrajectoryMonitor::TrajectoryMonitor(const CurrentStateMonitorConstPtr& state_monitor,
46  double sampling_frequency)
47  : TrajectoryMonitor(state_monitor, std::make_unique<TrajectoryMonitorMiddlewareHandle>(sampling_frequency),
48  sampling_frequency)
49 {
50 }
51 
53  const CurrentStateMonitorConstPtr& state_monitor,
54  std::unique_ptr<TrajectoryMonitor::MiddlewareHandle> middleware_handle, double sampling_frequency)
55  : current_state_monitor_(state_monitor)
56  , middleware_handle_(std::move(middleware_handle))
57  , sampling_frequency_(sampling_frequency)
58  , trajectory_(current_state_monitor_->getRobotModel(), "")
59 {
60  setSamplingFrequency(sampling_frequency);
61 }
62 
64 {
65  stopTrajectoryMonitor();
66 }
67 
69 {
70  if (sampling_frequency == sampling_frequency_)
71  return; // silently return if nothing changes
72 
73  if (sampling_frequency <= std::numeric_limits<double>::epsilon())
74  RCLCPP_ERROR(LOGGER, "The sampling frequency for trajectory states should be positive");
75  else
76  RCLCPP_DEBUG(LOGGER, "Setting trajectory sampling frequency to %.1f", sampling_frequency);
77  sampling_frequency_ = sampling_frequency;
78 }
79 
81 {
82  return static_cast<bool>(record_states_thread_);
83 }
84 
86 {
87  if (sampling_frequency_ > std::numeric_limits<double>::epsilon() && !record_states_thread_)
88  {
89  record_states_thread_ = std::make_unique<std::thread>([this] { recordStates(); });
90  RCLCPP_DEBUG(LOGGER, "Started trajectory monitor");
91  }
92 }
93 
95 {
96  if (record_states_thread_)
97  {
98  std::unique_ptr<std::thread> copy;
99  copy.swap(record_states_thread_);
100  copy->join();
101  RCLCPP_DEBUG(LOGGER, "Stopped trajectory monitor");
102  }
103 }
104 
106 {
107  bool restart = isActive();
108  if (restart)
109  stopTrajectoryMonitor();
110  trajectory_.clear();
111  if (restart)
112  startTrajectoryMonitor();
113 }
114 
115 void planning_scene_monitor::TrajectoryMonitor::recordStates()
116 {
117  if (!current_state_monitor_)
118  return;
119 
120  middleware_handle_->setRate(sampling_frequency_);
121 
122  while (record_states_thread_)
123  {
124  middleware_handle_->sleep();
125  std::pair<moveit::core::RobotStatePtr, rclcpp::Time> state = current_state_monitor_->getCurrentStateAndTime();
126  if (trajectory_.empty())
127  {
128  trajectory_.addSuffixWayPoint(state.first, 0.0);
129  trajectory_start_time_ = state.second;
130  last_recorded_state_time_ = state.second;
131  }
132  else
133  {
134  trajectory_.addSuffixWayPoint(state.first, (state.second - last_recorded_state_time_).seconds());
135  last_recorded_state_time_ = state.second;
136  }
137  if (state_add_callback_)
138  state_add_callback_(state.first, state.second);
139  }
140 }
This class contains the ROS2 interfaces for TrajectoryMonitor. This class is useful for testing by mo...
Monitors the joint_states topic and tf to record the trajectory of the robot.
TrajectoryMonitor(const CurrentStateMonitorConstPtr &state_monitor, double sampling_frequency=0.0)
Constructor.
void setSamplingFrequency(double sampling_frequency)
const rclcpp::Logger LOGGER
Definition: async_test.h:31