tracker.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, 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 Tracker base class, which is templated on the following types:
40 // -- Value function (V)
41 // -- [Planner] state (PS)
42 // -- [Tracker] state (TS), control (TC)
43 // -- Tracking error bound service (SB)
44 // -- Planner dynamics service (SP)
45 //
46 // All trackers essentially work by subscribing to state and reference topics
47 // and periodically (on a timer) querying the value function for the optimal
48 // control, then publishing that optimal control. Trackers also must provide
49 // services that other nodes can use to access planner dynamics and bound
50 // parameters.
51 //
53 
54 #ifndef FASTRACK_TRACKING_TRACKER_H
55 #define FASTRACK_TRACKING_TRACKER_H
56 
57 #include <fastrack/utils/types.h>
59 
60 #include <fastrack_msgs/Control.h>
61 #include <fastrack_msgs/State.h>
62 
63 #include <ros/ros.h>
64 #include <visualization_msgs/Marker.h>
65 #include <std_msgs/Empty.h>
66 
67 namespace fastrack {
68 namespace tracking {
69 
70 template<typename V, typename TS, typename TC, typename PS,
71  typename SB, typename SP>
72 class Tracker : private Uncopyable {
73 public:
74  ~Tracker() {}
75  explicit Tracker()
76  : ready_(false),
77  received_planner_x_(false),
78  received_tracker_x_(false),
79  initialized_(false) {}
80 
81  // Initialize from a ROS NodeHandle.
82  bool Initialize(const ros::NodeHandle& n);
83 
84 private:
85  // Load parameters and register callbacks.
86  bool LoadParameters(const ros::NodeHandle& n);
87  bool RegisterCallbacks(const ros::NodeHandle& n);
88 
89  // Is the system ready?
90  inline void ReadyCallback(const std_msgs::Empty::ConstPtr& msg) {
91  ready_ = true;
92  }
93 
94  // Callback to update tracker/planner state.
95  inline void TrackerStateCallback(const fastrack_msgs::State::ConstPtr& msg) {
96  tracker_x_.FromRosPtr(msg);
97  received_tracker_x_ = true;
98  }
99  inline void PlannerStateCallback(const fastrack_msgs::State::ConstPtr& msg) {
100  planner_x_.FromRosPtr(msg);
101  received_planner_x_ = true;
102  }
103 
104  // Service callbacks for tracking bound and planner parameters.
105  inline bool TrackingBoundServer(
106  typename SB::Request& req, typename SB::Response& res) {
107  res = value_.TrackingBound().ToRos();
108  return true;
109  }
111  typename SP::Request& req, typename SP::Response& res) {
112  res = value_.PlannerDynamics().ToRos();
113  return true;
114  }
115 
116  // Timer callback. Compute the optimal control and publish.
117  inline void TimerCallback(const ros::TimerEvent& e) const {
118  if (!ready_)
119  return;
120 
122  ROS_WARN_THROTTLE(1.0, "%s: Have not received planner/tracker state yet.",
123  name_.c_str());
124  return;
125  }
126 
127  // Publish bound.
128  value_.TrackingBound().Visualize(bound_pub_, planner_frame_);
129 
130  // Publish control.
131  control_pub_.publish(value_.OptimalControl(tracker_x_, planner_x_).ToRos(
132  value_.Priority(tracker_x_, planner_x_)));
133  }
134 
135  // Most recent tracker/planner states.
138 
141 
142  // Value function.
144 
145  // Planner frame of reference.
146  std::string planner_frame_;
147 
148  // Publishers and subscribers.
149  std::string ready_topic_;
150  std::string tracker_state_topic_;
151  std::string planner_state_topic_;
152  std::string control_topic_;
153  std::string bound_topic_;
154 
155  ros::Subscriber ready_sub_;
156  ros::Subscriber tracker_state_sub_;
157  ros::Subscriber planner_state_sub_;
158  ros::Publisher control_pub_;
159  ros::Publisher bound_pub_;
160 
161  // Services.
162  std::string bound_name_;
164 
165  ros::ServiceServer bound_srv_;
166  ros::ServiceServer planner_dynamics_srv_;
167 
168  // Timer.
169  ros::Timer timer_;
170  double time_step_;
171 
172  // Is the system ready for our control input?
173  bool ready_;
174 
175  // Flag for whether this class has been initialized yet.
177 
178  // Name of this class, for use in debug messages.
179  std::string name_;
180 }; //\class Tracker
181 
182 // ----------------------------- IMPLEMEMTATION ----------------------------- //
183 
184 // Initialize from a ROS NodeHandle.
185 template<typename V, typename TS, typename TC,
186  typename PS, typename SB, typename SP>
187 bool Tracker<V, TS, TC, PS, SB, SP>::Initialize(const ros::NodeHandle& n) {
188  name_ = ros::names::append(n.getNamespace(), "Tracker");
189 
190  // Initialize value function.
191  if (!value_.Initialize(n)) {
192  ROS_ERROR("%s: Failed to initialize value function.", name_.c_str());
193  return false;
194  }
195 
196  // Load parameters.
197  if (!LoadParameters(n)) {
198  ROS_ERROR("%s: Failed to load parameters.", name_.c_str());
199  return false;
200  }
201 
202  // Register callbacks.
203  if (!RegisterCallbacks(n)) {
204  ROS_ERROR("%s: Failed to register callbacks.", name_.c_str());
205  return false;
206  }
207 
208  initialized_ = true;
209  return true;
210 }
211 
212 // Load parameters.
213 template<typename V, typename TS, typename TC,
214  typename PS, typename SB, typename SP>
215 bool Tracker<V, TS, TC, PS, SB, SP>::LoadParameters(const ros::NodeHandle& n) {
216  ros::NodeHandle nl(n);
217 
218  // Topics.
219  if (!nl.getParam("topic/ready", ready_topic_)) return false;
220  if (!nl.getParam("topic/tracker_state", tracker_state_topic_)) return false;
221  if (!nl.getParam("topic/planner_state", planner_state_topic_)) return false;
222  if (!nl.getParam("topic/control", control_topic_)) return false;
223  if (!nl.getParam("vis/bound", bound_topic_)) return false;
224 
225  // Service names.
226  if (!nl.getParam("srv/bound", bound_name_)) return false;
227  if (!nl.getParam("srv/planner_dynamics", planner_dynamics_name_))
228  return false;
229 
230  // Planner frame of reference.
231  if (!nl.getParam("frames/planner", planner_frame_)) return false;
232 
233  // Time step.
234  if (!nl.getParam("time_step", time_step_)) return false;
235 
236  return true;
237 }
238 
239 // Register callbacks.
240 template<typename V, typename TS, typename TC,
241  typename PS, typename SB, typename SP>
243  ros::NodeHandle nl(n);
244 
245  // Services.
246  bound_srv_ = nl.advertiseService(bound_name_.c_str(),
248  planner_dynamics_srv_ = nl.advertiseService(planner_dynamics_name_.c_str(),
250 
251  // Subscribers.
252  ready_sub_ = nl.subscribe(ready_topic_.c_str(), 1,
254  planner_state_sub_ = nl.subscribe(planner_state_topic_.c_str(), 1,
256  tracker_state_sub_ = nl.subscribe(tracker_state_topic_.c_str(), 1,
258 
259  // Publishers.
260  control_pub_ = nl.advertise<fastrack_msgs::Control>(
261  control_topic_.c_str(), 1, false);
262  bound_pub_ = nl.advertise<visualization_msgs::Marker>(
263  bound_topic_.c_str(), 1, false);
264 
265  // Timer.
266  timer_ = nl.createTimer(ros::Duration(time_step_),
268 
269  return true;
270 }
271 
272 } //\namespace tracking
273 } //\namespace fastrack
274 
275 #endif
std::string ready_topic_
Definition: tracker.h:149
ros::Subscriber planner_state_sub_
Definition: tracker.h:157
void PlannerStateCallback(const fastrack_msgs::State::ConstPtr &msg)
Definition: tracker.h:99
ros::Subscriber ready_sub_
Definition: tracker.h:155
ros::Publisher control_pub_
Definition: tracker.h:158
ros::ServiceServer planner_dynamics_srv_
Definition: tracker.h:166
bool Initialize(const ros::NodeHandle &n)
Definition: tracker.h:187
std::string planner_state_topic_
Definition: tracker.h:151
void TrackerStateCallback(const fastrack_msgs::State::ConstPtr &msg)
Definition: tracker.h:95
ros::Publisher bound_pub_
Definition: tracker.h:159
void TimerCallback(const ros::TimerEvent &e) const
Definition: tracker.h:117
Definition: box.h:53
std::string planner_frame_
Definition: tracker.h:146
bool LoadParameters(const ros::NodeHandle &n)
Definition: tracker.h:215
ros::Subscriber tracker_state_sub_
Definition: tracker.h:156
bool PlannerDynamicsServer(typename SP::Request &req, typename SP::Response &res)
Definition: tracker.h:110
void ReadyCallback(const std_msgs::Empty::ConstPtr &msg)
Definition: tracker.h:90
std::string bound_topic_
Definition: tracker.h:153
bool RegisterCallbacks(const ros::NodeHandle &n)
Definition: tracker.h:242
ros::ServiceServer bound_srv_
Definition: tracker.h:165
std::string planner_dynamics_name_
Definition: tracker.h:163
bool TrackingBoundServer(typename SB::Request &req, typename SB::Response &res)
Definition: tracker.h:105
std::string control_topic_
Definition: tracker.h:152
std::string tracker_state_topic_
Definition: tracker.h:150


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