trajectory_server.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 <ros/ros.h>
39 #include <math.h>
40 #include <fstream>
41 #include <string>
42 
43 #include <geometry_msgs/Twist.h>
45 #include <nav_msgs/Path.h>
47 #include <trajectory_tracker_msgs/ChangePath.h>
48 #include <trajectory_tracker_msgs/TrajectoryServerStatus.h>
49 #include <visualization_msgs/InteractiveMarkerUpdate.h>
50 
52 
54 
56 {
57 public:
58  ServerNode();
59  ~ServerNode();
60  void spin();
61 
62 private:
69 
70  nav_msgs::Path path_;
71  std::string topic_path_;
72  trajectory_tracker_msgs::ChangePath::Request req_path_;
73  double hz_;
76  double filter_step_;
78 
79  bool loadFile();
80  void loadPath();
81  bool change(trajectory_tracker_msgs::ChangePath::Request& req,
82  trajectory_tracker_msgs::ChangePath::Response& res);
83  void processFeedback(
84  const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback);
85  void updateIM();
86  enum
87  {
90  };
93 };
94 
96  : nh_()
97  , pnh_("~")
98  , srv_im_fb_("trajectory_server")
99  , buffer_(new uint8_t[1024])
100 {
102  neonavigation_common::compat::deprecatedParam(pnh_, "path", topic_path_, std::string("path"));
103  pnh_.param("file", req_path_.filename, std::string("a.path"));
104  pnh_.param("hz", hz_, 5.0);
105  pnh_.param("filter_step", filter_step_, 0.0);
106 
107  pub_path_ = neonavigation_common::compat::advertise<nav_msgs::Path>(
108  nh_, "path",
109  pnh_, topic_path_, 2, true);
110  pub_status_ = pnh_.advertise<trajectory_tracker_msgs::TrajectoryServerStatus>("status", 2);
112  nh_, "change_path",
113  pnh_, "ChangePath", &ServerNode::change, this);
114  update_num_ = 0;
115  max_markers_ = 0;
116 }
118 {
119 }
120 
122 {
123  std::ifstream ifs(req_path_.filename.c_str());
124  if (ifs.good())
125  {
126  ifs.seekg(0, ifs.end);
127  serial_size_ = ifs.tellg();
128  ifs.seekg(0, ifs.beg);
129  buffer_.reset(new uint8_t[serial_size_]);
130  ifs.read(reinterpret_cast<char*>(buffer_.get()), serial_size_);
131 
132  return true;
133  }
134  return false;
135 }
136 
138  const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback)
139 {
140  int id = std::atoi(feedback->marker_name.c_str());
141  switch (feedback->event_type)
142  {
143  case visualization_msgs::InteractiveMarkerFeedback::POSE_UPDATE:
144  path_.poses[id].pose = feedback->pose;
145  break;
146  case visualization_msgs::InteractiveMarkerFeedback::MOUSE_UP:
147  path_.header.stamp = ros::Time::now();
149  break;
150  case visualization_msgs::InteractiveMarkerFeedback::MENU_SELECT:
151  switch (feedback->menu_entry_id)
152  {
153  case MENU_DELETE:
154  path_.poses.erase(path_.poses.begin() + id);
155  break;
156  case MENU_ADD:
157  path_.poses.insert(path_.poses.begin() + id, path_.poses[id]);
158  break;
159  }
161  updateIM();
162  break;
163  }
164 }
165 
167 {
168  visualization_msgs::InteractiveMarkerUpdate viz;
169  viz.type = viz.KEEP_ALIVE;
170  viz.seq_num = update_num_++;
171  viz.server_id = "Path";
172  srv_im_fb_.clear();
173  int i = 0;
174  for (auto& p : path_.poses)
175  {
176  visualization_msgs::InteractiveMarker mark;
177  visualization_msgs::Marker marker;
178  visualization_msgs::InteractiveMarkerControl ctl;
179  visualization_msgs::MenuEntry menu;
180  mark.header = path_.header;
181  mark.pose = p.pose;
182  mark.scale = 1.0;
183  std::stringstream ss;
184  ss << i++;
185  mark.name = ss.str();
186  marker.ns = "Path";
187  marker.id = i;
188  marker.scale.x = 0.2;
189  marker.scale.y = 0.05;
190  marker.scale.z = 0.05;
191  marker.color.r = 1;
192  marker.color.g = 1;
193  marker.color.b = 1;
194  marker.color.a = 1;
195  marker.type = marker.ARROW;
196 
197  ss << " ctl";
198  ctl.orientation = tf2::toMsg(tf2::Quaternion(tf2::Vector3(0.0, 1.0, 0.0), M_PI / 2.0));
199  ctl.interaction_mode = ctl.MOVE_ROTATE;
200  ctl.orientation_mode = ctl.INHERIT;
201  ctl.always_visible = true;
202  ctl.markers.push_back(marker);
203  ctl.name = ss.str();
204  mark.controls.push_back(ctl);
205 
206  ss << " menu";
207  ctl.interaction_mode = ctl.MENU;
208  ctl.name = ss.str();
209  marker.type = marker.SPHERE;
210  marker.color.g = 0;
211  marker.color.b = 0;
212  marker.scale.x = 0.08;
213  marker.scale.y = 0.08;
214  marker.scale.z = 0.08;
215  ctl.markers[0] = marker;
216  mark.controls.push_back(ctl);
217 
218  menu.id = MENU_DELETE;
219  menu.parent_id = 0;
220  menu.title = "Delete";
221  menu.command_type = menu.FEEDBACK;
222  mark.menu_entries.push_back(menu);
223  menu.id = MENU_ADD;
224  menu.parent_id = 0;
225  menu.title = "Add";
226 
227  mark.menu_entries.push_back(menu);
228  srv_im_fb_.insert(mark, boost::bind(&ServerNode::processFeedback, this, _1));
229  viz.markers.push_back(mark);
230  }
232 }
233 
234 bool ServerNode::change(trajectory_tracker_msgs::ChangePath::Request& req,
235  trajectory_tracker_msgs::ChangePath::Response& res)
236 {
237  req_path_ = req;
238  res.success = false;
239 
240  if (loadFile())
241  {
242  res.success = true;
245  path_.header.stamp = ros::Time::now();
246  if (filter_step_ > 0)
247  {
248  std::cout << filter_step_ << std::endl;
250  trajectory_tracker::Filter::FILTER_LPF, filter_step_, path_.poses[0].pose.position.x);
252  trajectory_tracker::Filter::FILTER_LPF, filter_step_, path_.poses[0].pose.position.y);
253 
254  for (size_t i = 0; i < path_.poses.size(); i++)
255  {
256  path_.poses[i].pose.position.x = lpf_[0]->in(path_.poses[i].pose.position.x);
257  path_.poses[i].pose.position.y = lpf_[1]->in(path_.poses[i].pose.position.y);
258  }
259 
260  delete lpf_[0];
261  delete lpf_[1];
262  }
263 
265  updateIM();
266  }
267  else
268  {
269  serial_size_ = 0;
270  req_path_.filename = "";
271  path_.poses.clear();
272  path_.header.frame_id = "map";
273  }
274  return true;
275 }
276 
278 {
279  ros::Rate loop_rate(hz_);
280  trajectory_tracker_msgs::TrajectoryServerStatus status;
281 
282  while (ros::ok())
283  {
284  status.header = path_.header;
285  status.filename = req_path_.filename;
286  status.id = req_path_.id;
287  pub_status_.publish(status);
288  ros::spinOnce();
289  loop_rate.sleep();
290  }
291 }
292 
293 int main(int argc, char** argv)
294 {
295  ros::init(argc, argv, "trajectory_server");
296 
297  ServerNode serv;
298  serv.spin();
299 
300  return 0;
301 }
ros::Publisher pub_path_
void processFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
bool change(trajectory_tracker_msgs::ChangePath::Request &req, trajectory_tracker_msgs::ChangePath::Response &res)
void publish(const boost::shared_ptr< M > &message) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
ros::ServiceServer srv_change_path_
void loadPath()
ros::ServiceServer advertiseService(ros::NodeHandle &nh_new, const std::string &service_new, ros::NodeHandle &nh_old, const std::string &service_old, bool(T::*srv_func)(MReq &, MRes &), T *obj)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ROSCPP_DECL bool ok()
boost::shared_array< uint8_t > buffer_
ros::NodeHandle pnh_
nav_msgs::Path path_
bool sleep()
std::string topic_path_
B toMsg(const A &a)
void insert(const visualization_msgs::InteractiveMarker &int_marker)
ros::NodeHandle nh_
interactive_markers::InteractiveMarkerServer srv_im_fb_
trajectory_tracker::Filter * lpf_[2]
static Time now()
ros::Publisher pub_status_
trajectory_tracker_msgs::ChangePath::Request req_path_
ROSCPP_DECL void spinOnce()
void deprecatedParam(const ros::NodeHandle &nh, const std::string &key, T &param, const T &default_value)
void deserialize(Stream &stream, T &t)
float in(const float &i)
Definition: filter.h:84


trajectory_tracker
Author(s): Atsushi Watanabe
autogenerated on Tue Jul 9 2019 05:00:09