45 double sampling_frequency)
52 const CurrentStateMonitorConstPtr& state_monitor,
53 std::unique_ptr<TrajectoryMonitor::MiddlewareHandle> middleware_handle,
double sampling_frequency)
54 : current_state_monitor_(state_monitor)
55 , middleware_handle_(std::move(middleware_handle))
56 , sampling_frequency_(sampling_frequency)
57 , trajectory_(current_state_monitor_->getRobotModel(),
"")
58 , logger_(
moveit::getLogger(
"moveit.ros.trajectory_monitor"))
65 stopTrajectoryMonitor();
70 if (sampling_frequency == sampling_frequency_)
73 if (sampling_frequency <= std::numeric_limits<double>::epsilon())
75 RCLCPP_ERROR(logger_,
"The sampling frequency for trajectory states should be positive");
79 RCLCPP_DEBUG(logger_,
"Setting trajectory sampling frequency to %.1f", sampling_frequency);
81 sampling_frequency_ = sampling_frequency;
86 return static_cast<bool>(record_states_thread_);
91 if (sampling_frequency_ > std::numeric_limits<double>::epsilon() && !record_states_thread_)
93 record_states_thread_ = std::make_unique<std::thread>([
this] { recordStates(); });
94 RCLCPP_DEBUG(logger_,
"Started trajectory monitor");
100 if (record_states_thread_)
102 std::unique_ptr<std::thread> copy;
103 copy.swap(record_states_thread_);
105 RCLCPP_DEBUG(logger_,
"Stopped trajectory monitor");
111 bool restart = isActive();
113 stopTrajectoryMonitor();
116 startTrajectoryMonitor();
119void planning_scene_monitor::TrajectoryMonitor::recordStates()
121 if (!current_state_monitor_)
124 middleware_handle_->setRate(sampling_frequency_);
126 while (record_states_thread_)
128 middleware_handle_->sleep();
129 std::pair<moveit::core::RobotStatePtr, rclcpp::Time> state = current_state_monitor_->getCurrentStateAndTime();
130 if (trajectory_.empty())
132 trajectory_.addSuffixWayPoint(state.first, 0.0);
133 trajectory_start_time_ = state.second;
134 last_recorded_state_time_ = state.second;
138 trajectory_.addSuffixWayPoint(state.first, (state.second - last_recorded_state_time_).seconds());
139 last_recorded_state_time_ = state.second;
141 if (state_add_callback_)
142 state_add_callback_(state.first, state.second);
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.
void stopTrajectoryMonitor()
TrajectoryMonitor(const CurrentStateMonitorConstPtr &state_monitor, double sampling_frequency=0.0)
Constructor.
void startTrajectoryMonitor()
void setSamplingFrequency(double sampling_frequency)
Main namespace for MoveIt.