replanner.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, The Regents of the University of California (Regents).
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
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder 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 AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: David Fridovich-Keil ( dfk@eecs.berkeley.edu )
35  */
36 
38 //
39 // Defines the Replanner class, which listens for replanning requests,
40 // makes the appropriate service calls, and re-sends the service response
41 // back on the appropriate topic.
42 //
43 // NOTE! This class exists because the Planner interface is service-based,
44 // rather than pub/sub-based, and the PlannerManager cannot afford to wait for
45 // the service calls to finish (i.e. for the planner to compute) before
46 // sending the next planner reference state.
47 //
49 
51 
52 namespace fastrack {
53 namespace planning {
54 
55 // Callback for processing replanning requests.
57  const fastrack_msgs::ReplanRequest::ConstPtr& msg) {
58  // Make sure server is connected.
59  if (!replan_srv_) {
60  ROS_ERROR("%s: Replan server was disconnected.", name_.c_str());
61  return;
62  }
63 
64  // Make a service call.
65  fastrack_srvs::Replan replan;
66  replan.request.req = *msg;
67  if (!replan_srv_.call(replan)) {
68  ROS_ERROR("%s: Replan server error.", name_.c_str());
69  replan.response.traj.states.clear();
70  replan.response.traj.times.clear();
71  }
72 
73  // Publish trajectory.
74  traj_pub_.publish(replan.response.traj);
75 }
76 
77 // Initialize this class with all parameters and callbacks.
78 bool Replanner::Initialize(const ros::NodeHandle& n) {
79  name_ = ros::names::append(n.getNamespace(), "Replanner");
80 
81  // Load parameters.
82  if (!LoadParameters(n)) {
83  ROS_ERROR("%s: Failed to load parameters.", name_.c_str());
84  return false;
85  }
86 
87  // Register callbacks.
88  if (!RegisterCallbacks(n)) {
89  ROS_ERROR("%s: Failed to register callbacks.", name_.c_str());
90  return false;
91  }
92 
93  initialized_ = true;
94  return true;
95 }
96 
97 // Load parameters.
98 bool Replanner::LoadParameters(const ros::NodeHandle& n) {
99  ros::NodeHandle nl(n);
100 
101  // Topics.
102  if (!nl.getParam("topic/traj", traj_topic_)) return false;
103  if (!nl.getParam("topic/replan_request", replan_request_topic_)) return false;
104 
105  // Services.
106  if (!nl.getParam("srv/replan", replan_srv_name_)) return false;
107 
108  return true;
109 }
110 
111 // Register callbacks.
112 bool Replanner::RegisterCallbacks(const ros::NodeHandle& n) {
113  ros::NodeHandle nl(n);
114 
115  // Publishers.
116  traj_pub_ = nl.advertise<fastrack_msgs::Trajectory>(
117  traj_topic_.c_str(), 1, false);
118 
119  // Subscribers.
120  replan_request_sub_ = nl.subscribe(replan_request_topic_.c_str(), 1,
122 
123  // Services.
124  ros::service::waitForService(replan_srv_name_);
125  replan_srv_ = nl.serviceClient<fastrack_srvs::Replan>(
126  replan_srv_name_.c_str(), true);
127 
128  return true;
129 }
130 
131 } //\namespace planning
132 } //\namespace fastrack
ros::ServiceClient replan_srv_
Definition: replanner.h:96
ros::Publisher traj_pub_
Definition: replanner.h:89
void ReplanRequestCallback(const fastrack_msgs::ReplanRequest::ConstPtr &msg)
Definition: replanner.cpp:56
bool LoadParameters(const ros::NodeHandle &n)
Definition: replanner.cpp:98
bool Initialize(const ros::NodeHandle &n)
Definition: replanner.cpp:78
Definition: box.h:53
std::string replan_request_topic_
Definition: replanner.h:92
bool RegisterCallbacks(const ros::NodeHandle &n)
Definition: replanner.cpp:112
ros::Subscriber replan_request_sub_
Definition: replanner.h:90


fastrack
Author(s): David Fridovich-Keil
autogenerated on Mon Aug 3 2020 21:28:37