trajectory.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 // Templated class to hold timestamped sequences of states and rapidly
40 // interpolate between states linearly.
41 //
43 
44 #ifndef FASTRACK_PLANNING_TRAJECTORY_H
45 #define FASTRACK_PLANNING_TRAJECTORY_H
46 
47 #include <fastrack/state/state.h>
48 #include <fastrack/utils/types.h>
49 #include <fastrack_msgs/Trajectory.h>
50 #include <fastrack_msgs/State.h>
51 
52 #include <ros/ros.h>
53 #include <visualization_msgs/Marker.h>
54 #include <geometry_msgs/Vector3.h>
55 #include <std_msgs/ColorRGBA.h>
56 
57 namespace fastrack {
58 namespace trajectory {
59 
60 template<typename S>
61 class Trajectory {
62 public:
64  explicit Trajectory() {}
65 
66  // Construct from a list of trajectories.
67  // NOTE! Not const because we will automatically time-shift trajectories
68  // to splice them together.
69  explicit Trajectory(const std::list< Trajectory<S> >& trajs);
70 
71  // Construct from lists of states and times.
72  explicit Trajectory(const std::vector<S> &states,
73  const std::vector<double> &times);
74 
75  // Construct from a ROS message.
76  explicit Trajectory(const fastrack_msgs::Trajectory::ConstPtr& msg);
77 
78  // Size (number of states in this Trajectory).
79  inline size_t Size() const { return states_.size(); }
80 
81  // Duration in seconds.
82  inline double Duration() const { return times_.back() - times_.front(); }
83 
84  // First and last states/times.
85  inline S FirstState() const { return states_.front(); }
86  inline S LastState() const { return states_.back(); }
87  inline double FirstTime() const { return times_.front(); }
88  inline double LastTime() const { return times_.back(); }
89 
90  // Const accessors.
91  const std::vector<S>& States() const { return states_; }
92  const std::vector<double>& Times() const { return times_; }
93 
94  // Interpolate at a particular time.
95  S Interpolate(double t) const;
96 
97  // Reset first time and update all other times to preserve the deltas.
98  void ResetFirstTime(double t);
99 
100  // Convert to a ROS message.
101  fastrack_msgs::Trajectory ToRos() const;
102 
103  // Visualize this trajectory.
104  void Visualize(const ros::Publisher& pub, const std::string& frame) const;
105 
106 private:
107  // Custom colormap for the given time.
108  std_msgs::ColorRGBA Colormap(double t) const;
109 
110  // Lists of states and times.
111  std::vector<S> states_;
112  std::vector<double> times_;
113 
114  // Is this a trajectory in configuration space?
116 }; //\class Trajectory
117 
118 // ------------------------------ IMPLEMENTATION ----------------------------- //
119 
120 // Construct from a list of trajectories.
121 // NOTE! Not const because we will automatically time-shift trajectories
122 // to splice them together.
123 template<typename S>
124 Trajectory<S>::Trajectory(const std::list< Trajectory<S> >& trajs)
125  : configuration_(false) {
126  for (const auto& traj : trajs) {
127  // Reset first time to match last time of previous trajectory.
128  const double time_offset =
129  (times_.empty()) ? 0.0 : times_.back() - traj.times_.front();
130 
131  // Concatenate states and times to existing lists.
132  states_.insert(states_.end(), traj.states_.begin(), traj.states_.end());
133  for (size_t ii = 0; ii < traj.times_.size(); ii++)
134  times_.push_back(traj.times_[ii] + time_offset);
135 
136  // Set configuration to true if any trajectory is in configuration space.
137  configuration_ |= traj.configuration_;
138  }
139 
140  // Check to make sure time is monotonic increasing.
141  for (size_t ii = 1; ii < times_.size(); ii++) {
142  if (times_[ii - 1] > times_[ii])
143  throw std::runtime_error("Time was not monotone.");
144  }
145 }
146 
147 // Construct from lists of states and times.
148 template<typename S>
149 Trajectory<S>::Trajectory(const std::vector<S>& states,
150  const std::vector<double>& times)
151  : states_(states),
152  times_(times),
153  configuration_(false) {
154  // Warn if state/time lists are not the same length and truncate
155  // the longer one to match the smaller.
156  if (states_.size() != times_.size()) {
157  ROS_ERROR("Trajectory: states/times are not the same length.");
158 
159  // Resize the shorter one.
160  if (states_.size() > times_.size())
161  states_.resize(times_.size());
162  else
163  times_.resize(states_.size());
164  }
165 
166  // Make sure times are sorted. Overwrite any inversions with the larger
167  // time as we move left to right in the list.
168  for (size_t ii = 1; ii < times_.size(); ii++) {
169  if (times_[ii - 1] > times_[ii]) {
170  ROS_ERROR("Trajectory: fixing an inversion in the list of times.");
171  times_[ii] = times_[ii + 1];
172  }
173  }
174 }
175 
176 // Construct from a ROS message.
177 template<typename S>
178 Trajectory<S>::Trajectory(const fastrack_msgs::Trajectory::ConstPtr& msg)
179  : configuration_(false) {
180  size_t num_elements = msg->states.size();
181 
182  // Get size carefully.
183  if (msg->states.size() != msg->times.size()) {
184  ROS_ERROR("Trajectory: states/times are not the same length.");
185  num_elements = std::min(msg->states.size(), msg->times.size());
186  }
187 
188  // Unpack message.
189  for (size_t ii = 0; ii < num_elements; ii++) {
190  states_.push_back(S(msg->states[ii]));
191  times_.push_back(msg->times[ii]);
192  }
193 
194  // Is this trajectory in state space or configuration space?
195  if (num_elements > 0)
196  configuration_ = msg->states.front().x.size() == S::ConfigurationDimension();
197  else
198  configuration_ = false;
199 }
200 
201 // Interpolate at a particular time.
202 template<typename S>
203 S Trajectory<S>::Interpolate(double t) const {
204  // Get an iterator pointing to the first element in times_ that does
205  // not compare less than t.
206  const auto iter = std::lower_bound(times_.begin(), times_.end(), t);
207 
208  // Catch case where iter points to the beginning of the list.
209  // This will happen if t occurs before the first time in the list.
210  if (iter == times_.begin()) {
211  ROS_WARN_THROTTLE(1.0, "Trajectory: interpolating before first time.");
212  return states_.front();
213  }
214 
215  // Catch case where iter points to the end of the list.
216  // This will happen if t occurs after the last time in the list.
217  if (iter == times_.end()) {
218  ROS_WARN_THROTTLE(1.0, "Trajectory: interpolating after the last time.");
219  return states_.back();
220  }
221 
222  // Iterator definitely points to somewhere in the middle of the list.
223  // Get indices sandwiching t.
224  const size_t hi = (iter - times_.begin());
225  const size_t lo = hi - 1;
226 
227  // Linearly interpolate states.
228  const double frac = (t - times_[lo]) / (times_[hi] - times_[lo]);
229  S interpolated = (1.0 - frac) * states_[lo] + frac * states_[hi];
230 
231  // If this is a configuration trajectory, set non-configuration states
232  // by providing a numerical derivative of configuration.
233  if (configuration_) {
234  if (times_[hi] - times_[lo] > 1e-8)
235  interpolated.SetConfigurationDot(
236  (states_[hi] - states_[lo]).Configuration() / (times_[hi] - times_[lo]));
237  else
238  interpolated.SetConfigurationDot(
239  VectorXd::Zero(S::ConfigurationDimension()));
240  }
241 
242  return interpolated;
243 }
244 
245 // Reset first time and update all other times to preserve the deltas.
246 template<typename S>
248  if (times_.size() == 0) {
249  ROS_ERROR("Trajectory: Tried to reset first time of empty trajectory.");
250  return;
251  }
252 
253  const double constant_shift = t - times_.front();
254  for (size_t ii = 0; ii < times_.size(); ii++)
255  times_[ii] += constant_shift;
256 }
257 
258 // Convert to a ROS message.
259 template<typename S>
260 fastrack_msgs::Trajectory Trajectory<S>::ToRos() const {
261  fastrack_msgs::Trajectory msg;
262 
263  for (size_t ii = 0; ii < states_.size(); ii++) {
264  msg.states.push_back(states_[ii].ToRos());
265  msg.times.push_back(times_[ii]);
266  }
267 
268  return msg;
269 }
270 
271 
272 // Visualize this trajectory.
273 template<typename S>
274 void Trajectory<S>::Visualize(const ros::Publisher& pub,
275  const std::string& frame) const {
276  if (pub.getNumSubscribers() == 0) {
277  ROS_WARN_THROTTLE(1.0, "Trajectory: I'm lonely. Please subscribe.");
278  return;
279  }
280 
281  // Set up spheres marker.
282  visualization_msgs::Marker spheres;
283  spheres.ns = "spheres";
284  spheres.header.frame_id = frame;
285  spheres.header.stamp = ros::Time::now();
286  spheres.id = 0;
287  spheres.type = visualization_msgs::Marker::SPHERE_LIST;
288  spheres.action = visualization_msgs::Marker::ADD;
289  spheres.scale.x = 0.1;
290  spheres.scale.y = 0.1;
291  spheres.scale.z = 0.1;
292 
293  // Set up line strip marker.
294  visualization_msgs::Marker lines;
295  lines.ns = "lines";
296  lines.header.frame_id = frame;
297  lines.header.stamp = ros::Time::now();
298  lines.id = 0;
299  lines.type = visualization_msgs::Marker::LINE_STRIP;
300  lines.action = visualization_msgs::Marker::ADD;
301  lines.scale.x = 0.05;
302 
303  // Populate markers.
304  for (size_t ii = 0; ii < states_.size(); ii++) {
305  geometry_msgs::Point p;
306  p.x = states_[ii].X();
307  p.y = states_[ii].Y();
308  p.z = states_[ii].Z();
309 
310  const std_msgs::ColorRGBA c = Colormap(times_[ii]);
311 
312  spheres.points.push_back(p);
313  spheres.colors.push_back(c);
314  lines.points.push_back(p);
315  lines.colors.push_back(c);
316  }
317 
318  // Publish.
319  pub.publish(spheres);
320  if (states_.size() > 1)
321  pub.publish(lines);
322 }
323 
324 // Custom colormap for the given time.
325 template<typename S>
326 std_msgs::ColorRGBA Trajectory<S>::Colormap(double t) const {
327  std_msgs::ColorRGBA c;
328 
329  c.r = (Size() == 0 || Duration() < 1e-8) ? 0.0 :
330  std::max(0.0, std::min(1.0, (t - times_.front()) / Duration()));
331  c.g = 0.0;
332  c.b = 1.0 - c.r;
333  c.a = 0.9;
334 
335  return c;
336 }
337 
338 } //\namespace planning
339 } //\namespace fastrack
340 
341 #endif
std_msgs::ColorRGBA Colormap(double t) const
Definition: trajectory.h:326
const std::vector< S > & States() const
Definition: trajectory.h:91
const std::vector< double > & Times() const
Definition: trajectory.h:92
void Visualize(const ros::Publisher &pub, const std::string &frame) const
Definition: trajectory.h:274
S Interpolate(double t) const
Definition: trajectory.h:203
Definition: box.h:53
fastrack_msgs::Trajectory ToRos() const
Definition: trajectory.h:260
std::vector< double > times_
Definition: trajectory.h:112


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