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 {
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_.reset(new boost::thread(boost::bind(&TrajectoryMonitor::recordStates, this)));
81  ROS_DEBUG_NAMED(LOGNAME, "Started trajectory monitor");
82  }
83 }
84 
86 {
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)
101  trajectory_.clear();
102  if (restart)
104 }
105 
107 {
109  return;
110 
112 
113  while (record_states_thread_)
114  {
115  rate.sleep();
116  std::pair<robot_state::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  }
129  state_add_callback_(state.first, state.second);
130  }
131 }
#define ROS_INFO_NAMED(name,...)
static const std::string LOGNAME
#define ROS_DEBUG_NAMED(name,...)
std::unique_ptr< boost::thread > record_states_thread_
robot_trajectory::RobotTrajectory trajectory_
TrajectoryStateAddedCallback state_add_callback_
TrajectoryMonitor(const CurrentStateMonitorConstPtr &state_monitor, double sampling_frequency=0.0)
Constructor.
CurrentStateMonitorConstPtr current_state_monitor_
bool sleep()
void setSamplingFrequency(double sampling_frequency)
void addSuffixWayPoint(const robot_state::RobotState &state, double dt)
static const std::string LOGNAME


planning
Author(s): Ioan Sucan , Sachin Chitta
autogenerated on Sat Jul 11 2020 03:51:52