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 
39 #include <ros/rate.h>
40 #include <limits>
41 #include <memory>
42 
43 static const std::string LOGNAME = "TrajectoryMonitor";
44 
45 planning_scene_monitor::TrajectoryMonitor::TrajectoryMonitor(const CurrentStateMonitorConstPtr& state_monitor,
46  double sampling_frequency)
47  : current_state_monitor_(state_monitor)
48  , sampling_frequency_(sampling_frequency)
49  , trajectory_(current_state_monitor_->getRobotModel(), "")
50 {
51  setSamplingFrequency(sampling_frequency);
52 }
53 
55 {
56  stopTrajectoryMonitor();
57 }
58 
60 {
61  if (sampling_frequency == sampling_frequency_)
62  return; // silently return if nothing changes
63 
64  if (sampling_frequency <= std::numeric_limits<double>::epsilon())
65  ROS_INFO_NAMED(LOGNAME, "Disabling trajectory recording");
66  else
67  ROS_DEBUG_NAMED(LOGNAME, "Setting trajectory sampling frequency to %.1f", sampling_frequency);
68  sampling_frequency_ = sampling_frequency;
69 }
70 
72 {
73  return static_cast<bool>(record_states_thread_);
74 }
75 
77 {
78  if (sampling_frequency_ > std::numeric_limits<double>::epsilon() && !record_states_thread_)
79  {
80  record_states_thread_ = std::make_unique<boost::thread>([this] { recordStates(); });
81  ROS_DEBUG_NAMED(LOGNAME, "Started trajectory monitor");
82  }
83 }
84 
86 {
87  if (record_states_thread_)
88  {
89  std::unique_ptr<boost::thread> copy;
90  copy.swap(record_states_thread_);
91  copy->join();
92  ROS_DEBUG_NAMED(LOGNAME, "Stopped trajectory monitor");
93  }
94 }
95 
97 {
98  bool restart = isActive();
99  if (restart)
100  stopTrajectoryMonitor();
101  trajectory_.clear();
102  if (restart)
103  startTrajectoryMonitor();
104 }
105 
107 {
108  if (!current_state_monitor_)
109  return;
110 
111  ros::Rate rate(sampling_frequency_);
112 
113  while (record_states_thread_)
114  {
115  rate.sleep();
116  std::pair<moveit::core::RobotStatePtr, ros::Time> state = current_state_monitor_->getCurrentStateAndTime();
117  if (trajectory_.empty())
118  {
119  trajectory_.addSuffixWayPoint(state.first, 0.0);
120  trajectory_start_time_ = state.second;
121  last_recorded_state_time_ = state.second;
122  }
123  else
124  {
125  trajectory_.addSuffixWayPoint(state.first, (state.second - last_recorded_state_time_).toSec());
126  last_recorded_state_time_ = state.second;
127  }
128  if (state_add_callback_)
129  state_add_callback_(state.first, state.second);
130  }
131 }
planning_scene_monitor::TrajectoryMonitor::recordStates
void recordStates()
Definition: trajectory_monitor.cpp:106
planning_scene_monitor::TrajectoryMonitor::clearTrajectory
void clearTrajectory()
Definition: trajectory_monitor.cpp:96
planning_scene_monitor::LOGNAME
static const std::string LOGNAME
Definition: planning_scene_monitor.cpp:91
LOGNAME
static const std::string LOGNAME
Definition: trajectory_monitor.cpp:43
planning_scene_monitor::TrajectoryMonitor::stopTrajectoryMonitor
void stopTrajectoryMonitor()
Definition: trajectory_monitor.cpp:85
ROS_INFO_NAMED
#define ROS_INFO_NAMED(name,...)
ROS_DEBUG_NAMED
#define ROS_DEBUG_NAMED(name,...)
rate.h
planning_scene_monitor::TrajectoryMonitor::isActive
bool isActive() const
Definition: trajectory_monitor.cpp:71
ros::Rate::sleep
bool sleep()
planning_scene_monitor::TrajectoryMonitor::TrajectoryMonitor
TrajectoryMonitor(const CurrentStateMonitorConstPtr &state_monitor, double sampling_frequency=0.0)
Constructor.
Definition: trajectory_monitor.cpp:45
planning_scene_monitor::TrajectoryMonitor::~TrajectoryMonitor
~TrajectoryMonitor()
Definition: trajectory_monitor.cpp:54
trajectory_monitor.h
planning_scene_monitor::TrajectoryMonitor::startTrajectoryMonitor
void startTrajectoryMonitor()
Definition: trajectory_monitor.cpp:76
ros::Rate
planning_scene_monitor::TrajectoryMonitor::setSamplingFrequency
void setSamplingFrequency(double sampling_frequency)
Definition: trajectory_monitor.cpp:59
trajectory_tools.h


planning
Author(s): Ioan Sucan , Sachin Chitta
autogenerated on Thu Nov 21 2024 03:24:18