trajectory_recorder.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, ATR, Atsushi Watanabe
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * This research was supported by a contract with the Ministry of Internal
32  Affairs and Communications entitled, 'Novel and innovative R&D making use
33  of brain structures'
34 
35  This software was implemented to accomplish the above research.
36  */
37 
38 #include <cmath>
39 #include <string>
40 
41 #include <ros/ros.h>
42 
43 #include <geometry_msgs/Twist.h>
44 #include <nav_msgs/Path.h>
47 #include <std_srvs/Empty.h>
48 
50 
52 {
53 public:
54  RecorderNode();
55  ~RecorderNode();
56  void spin();
57 
58 private:
59  bool clearPath(std_srvs::Empty::Request& req,
60  std_srvs::Empty::Response& res);
61 
62  std::string topic_path_;
63  std::string frame_robot_;
64  std::string frame_global_;
66  double ang_interval_;
68 
75 
76  nav_msgs::Path path_;
77 };
78 
80  : nh_()
81  , pnh_("~")
82  , tfl_(tfbuf_)
83 {
85  pnh_.param("frame_robot", frame_robot_, std::string("base_link"));
86  pnh_.param("frame_global", frame_global_, std::string("map"));
87  neonavigation_common::compat::deprecatedParam(pnh_, "path", topic_path_, std::string("recpath"));
88  pnh_.param("dist_interval", dist_interval_, 0.3);
89  pnh_.param("ang_interval", ang_interval_, 1.0);
90  pnh_.param("store_time", store_time_, false);
91 
92  pub_path_ = neonavigation_common::compat::advertise<nav_msgs::Path>(
93  nh_, "path",
94  pnh_, topic_path_, 10, true);
95  srs_clear_path_ = pnh_.advertiseService("clear_path", &RecorderNode::clearPath, this);
96 }
97 
99 {
100 }
101 
102 float dist2d(geometry_msgs::Point& a, geometry_msgs::Point& b)
103 {
104  return std::sqrt(std::pow(a.x - b.x, 2) + std::pow(a.y - b.y, 2));
105 }
106 
107 bool RecorderNode::clearPath(std_srvs::Empty::Request& /* req */,
108  std_srvs::Empty::Response& /* res */)
109 {
110  path_.poses.clear();
111  return true;
112 }
113 
115 {
116  ros::Rate loop_rate(50);
117  path_.header.frame_id = frame_global_;
118  path_.header.seq = 0;
119 
120  while (ros::ok())
121  {
122  ros::Time now = ros::Time(0);
123  if (store_time_)
124  now = ros::Time::now();
126  try
127  {
128  tf2::fromMsg(
130  }
131  catch (tf2::TransformException& e)
132  {
133  ROS_WARN("TF exception: %s", e.what());
134  continue;
135  }
136  geometry_msgs::PoseStamped pose;
137  tf2::Quaternion q;
138  transform.getBasis().getRotation(q);
139  pose.pose.orientation = tf2::toMsg(q);
140  tf2::Vector3 origin = transform.getOrigin();
141  pose.pose.position.x = origin.x();
142  pose.pose.position.y = origin.y();
143  pose.pose.position.z = origin.z();
144  pose.header.frame_id = frame_global_;
145  pose.header.stamp = now;
146  pose.header.seq = path_.poses.size();
147 
148  path_.header.seq++;
149  path_.header.stamp = now;
150 
151  if (path_.poses.size() == 0)
152  {
153  path_.poses.push_back(pose);
155  }
156  else if (dist2d(path_.poses.back().pose.position, pose.pose.position) > dist_interval_)
157  {
158  path_.poses.push_back(pose);
160  }
161 
162  ros::spinOnce();
163  loop_rate.sleep();
164  }
165 }
166 
167 int main(int argc, char** argv)
168 {
169  ros::init(argc, argv, "trajectory_recorder");
170 
171  RecorderNode rec;
172  rec.spin();
173 
174  return 0;
175 }
float dist2d(geometry_msgs::Point &a, geometry_msgs::Point &b)
void publish(const boost::shared_ptr< M > &message) const
std::string frame_robot_
std::string topic_path_
int main(int argc, char **argv)
ros::Publisher pub_path_
virtual geometry_msgs::TransformStamped lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, const ros::Duration timeout) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ros::ServiceServer srs_clear_path_
#define ROS_WARN(...)
tf2_ros::Buffer tfbuf_
std::string frame_global_
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void fromMsg(const A &, B &b)
ROSCPP_DECL bool ok()
ros::NodeHandle pnh_
bool sleep()
B toMsg(const A &a)
static Time now()
bool clearPath(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res)
ros::NodeHandle nh_
ROSCPP_DECL void spinOnce()
nav_msgs::Path path_
void deprecatedParam(const ros::NodeHandle &nh, const std::string &key, T &param, const T &default_value)
tf2_ros::TransformListener tfl_


trajectory_tracker
Author(s): Atsushi Watanabe
autogenerated on Wed May 12 2021 02:20:40