position_velocity.cpp
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 // Class for purely geometric (position + velocity) state.
40 //
42 
44 
45 #include <ros/ros.h>
46 
47 namespace fastrack {
48 namespace state {
49 
50 // Initialize static configuration space bounds.
51 PositionVelocity PositionVelocity::lower_ =
52  PositionVelocity(Vector3d::Zero(), Vector3d::Zero());
53 PositionVelocity PositionVelocity::upper_ =
54  PositionVelocity(Vector3d::Zero(), Vector3d::Zero());
55 
56 // Constructors.
57 PositionVelocity::PositionVelocity(double x, double y, double z, double vx,
58  double vy, double vz)
59  : State(), position_(Vector3d(x, y, z)), velocity_(Vector3d(vx, vy, vz)) {}
60 PositionVelocity::PositionVelocity(const Vector3d &position,
61  const Vector3d &velocity)
62  : State(), position_(position), velocity_(velocity) {}
63 PositionVelocity::PositionVelocity(const fastrack_msgs::State &msg)
64  : State(), position_(Vector3d::Zero()), velocity_(Vector3d::Zero()) {
65  // Check dimensions.
66  if (msg.x.size() == StateDimension()) {
67  position_(0) = msg.x[0];
68  position_(1) = msg.x[1];
69  position_(2) = msg.x[2];
70  velocity_(0) = msg.x[3];
71  velocity_(1) = msg.x[4];
72  velocity_(2) = msg.x[5];
73  } else if (msg.x.size() == ConfigurationDimension()) {
74  position_(0) = msg.x[0];
75  position_(1) = msg.x[1];
76  position_(2) = msg.x[2];
77  } else {
78  ROS_ERROR_THROTTLE(1.0,
79  "PositionVelocity: msg dimension is incorrect: %zu.",
80  msg.x.size());
81  }
82 }
84  : State(), position_(Vector3d::Zero()), velocity_(Vector3d::Zero()) {
85  // Check dimensions.
86  if (x.size() == ConfigurationDimension()) {
87  position_(0) = x(0);
88  position_(1) = x(1);
89  position_(2) = x(2);
90  } else if (x.size() == StateDimension()) {
91  position_(0) = x(0);
92  position_(1) = x(1);
93  position_(2) = x(2);
94  velocity_(0) = x(3);
95  velocity_(1) = x(4);
96  velocity_(2) = x(5);
97  } else {
98  throw std::runtime_error("PositionVelocity: incorrect input dimension.");
99  }
100 }
101 
102 // Set non-configuration dimensions to match the given config derivative.
103 void PositionVelocity::SetConfigurationDot(const VectorXd &configuration_dot) {
104  if (configuration_dot.size() != ConfigurationDimension())
105  throw std::runtime_error(
106  "PositionVelocity: invalid configuration_dot dim.");
107 
108  velocity_(0) = configuration_dot(0);
109  velocity_(1) = configuration_dot(1);
110  velocity_(2) = configuration_dot(2);
111 }
112 
113 // Static function to sample from the configuration space associated
114 // with this state space. Pass in the lower and upper bounds from
115 // which to sample.
117  const size_t kConfigurationSpaceDimension = 3;
118 
119  // Initialize a uniform random distribution in (0, 1).
120  std::uniform_real_distribution<double> unif(0.0, 1.0);
121 
122  // Extract lower/upper configuration bounds.
123  const VectorXd lower_config = PositionVelocity::GetConfigurationLower();
124  const VectorXd upper_config = PositionVelocity::GetConfigurationUpper();
125 
126  // Generate random sample.
127  VectorXd sample(kConfigurationSpaceDimension);
128  for (size_t ii = 0; ii < kConfigurationSpaceDimension; ii++)
129  sample(ii) =
130  lower_config(ii) + (upper_config(ii) - lower_config(ii)) * unif(rng_);
131 
132  return sample;
133 }
134 
135 // Sample from the state space itself.
137  // Initialize a uniform random distribution in (0, 1).
138  std::uniform_real_distribution<double> unif(0.0, 1.0);
139 
140  // Generate random sample.
141  const double x = lower_.position_(0) +
142  (upper_.position_(0) - lower_.position_(0)) * unif(rng_);
143  const double y = lower_.position_(1) +
144  (upper_.position_(1) - lower_.position_(1)) * unif(rng_);
145  const double z = lower_.position_(2) +
146  (upper_.position_(2) - lower_.position_(2)) * unif(rng_);
147  const double vx = lower_.velocity_(0) +
148  (upper_.velocity_(0) - lower_.velocity_(0)) * unif(rng_);
149  const double vy = lower_.velocity_(1) +
150  (upper_.velocity_(1) - lower_.velocity_(1)) * unif(rng_);
151  const double vz = lower_.velocity_(2) +
152  (upper_.velocity_(2) - lower_.velocity_(2)) * unif(rng_);
153 
154  return PositionVelocity(x, y, z, vx, vy, vz);
155 }
156 
157 // Samples within a box of the given position, intersected with the state space
158 // bounds.
160  double d) {
161  std::uniform_real_distribution<double> x_dist(
162  std::max(lower_.position_.x(), pos.x() - d),
163  std::min(upper_.position_.x(), pos.x() + d));
164  std::uniform_real_distribution<double> y_dist(
165  std::max(lower_.position_.y(), pos.y() - d),
166  std::min(upper_.position_.y(), pos.y() + d));
167  std::uniform_real_distribution<double> z_dist(
168  std::max(lower_.position_.z(), pos.z() - d),
169  std::min(upper_.position_.z(), pos.z() + d));
170 
171  // HACK! Set velocity to zero since that's what we want most of the time.
172  return PositionVelocity(x_dist(rng_), y_dist(rng_), z_dist(rng_), 0.0, 0.0,
173  0.0);
174 }
175 
176 // For a given configuration, what are the corresponding positions in
177 // position space that the system occupies.
178 // NOTE! For simplicity, this is a finite set. In future, this could
179 // be generalized to a collection of generic obstacles.
180 std::vector<Vector3d> PositionVelocity::OccupiedPositions() const {
181  return std::vector<Vector3d>({position_});
182 }
183 
184 // Set bounds of the state space.
186  const PositionVelocity &upper) {
187  lower_ = lower;
188  upper_ = upper;
189 }
190 
191 // Set state space bounds from std vectors. Layout is assumed to be
192 // [x, y, z, vx, vy, vz].
193 void PositionVelocity::SetBounds(const std::vector<double> &lower,
194  const std::vector<double> &upper) {
195  // Check dimensions.
196  if (lower.size() != 6 || upper.size() != 6)
197  throw std::runtime_error("PositionVelocity: bad bound dimension.");
198 
199  lower_.position_(0) = lower[0];
200  lower_.position_(1) = lower[1];
201  lower_.position_(2) = lower[2];
202  lower_.velocity_(0) = lower[3];
203  lower_.velocity_(1) = lower[4];
204  lower_.velocity_(2) = lower[5];
205 
206  upper_.position_(0) = upper[0];
207  upper_.position_(1) = upper[1];
208  upper_.position_(2) = upper[2];
209  upper_.velocity_(0) = upper[3];
210  upper_.velocity_(1) = upper[4];
211  upper_.velocity_(2) = upper[5];
212 }
213 
214 // Convert from VectorXd. Assume State is [x, y, z, vx, vy, vz].
215 void PositionVelocity::FromVector(const VectorXd &x) {
216  if (x.size() == 6) {
217  position_(0) = x(0);
218  position_(1) = x(1);
219  position_(2) = x(2);
220  velocity_(0) = x(3);
221  velocity_(1) = x(4);
222  velocity_(2) = x(5);
223  return;
224  }
225 
226  ROS_ERROR("PositionVelocity: vector is of the wrong size.");
227 }
228 
229 // Convert to VectorXd. Assume State is [x, y, z, vx, vy, vz].
230 VectorXd PositionVelocity::ToVector() const {
231  VectorXd x(6);
232  x(0) = position_(0);
233  x(1) = position_(1);
234  x(2) = position_(2);
235  x(3) = velocity_(0);
236  x(4) = velocity_(1);
237  x(5) = velocity_(2);
238 
239  return x;
240 }
241 
242 // Convert from ROS message. Assume State is [x, y, z, vx, vy, vz] or
243 // configuration only.
244 void PositionVelocity::FromRos(const fastrack_msgs::State &msg) {
245  if (msg.x.size() == 6) {
246  // Message contains full state.
247  position_(0) = msg.x[0];
248  position_(1) = msg.x[1];
249  position_(2) = msg.x[2];
250  velocity_(0) = msg.x[3];
251  velocity_(1) = msg.x[4];
252  velocity_(2) = msg.x[5];
253  } else if (msg.x.size() == 3) {
254  // Message contains configuration only.
255  position_(0) = msg.x[0];
256  position_(1) = msg.x[1];
257  position_(2) = msg.x[2];
258  } else
259  ROS_ERROR("PositionVelocity: msg is neither state nor configuration.");
260 }
261 
262 // Convert to ROS message.
263 fastrack_msgs::State PositionVelocity::ToRos() const {
264  fastrack_msgs::State msg;
265  msg.x.push_back(position_(0));
266  msg.x.push_back(position_(1));
267  msg.x.push_back(position_(2));
268  msg.x.push_back(velocity_(0));
269  msg.x.push_back(velocity_(1));
270  msg.x.push_back(velocity_(2));
271 
272  return msg;
273 }
274 
275 // Get bounds of state space.
278 
279 // Get bounds of configuration space.
281  return lower_.Configuration();
282 }
284  return upper_.Configuration();
285 }
286 
287 // Compound assignment operators.
289  position_ += rhs.position_;
290  velocity_ += rhs.velocity_;
291  return *this;
292 }
293 
295  position_ -= rhs.position_;
296  velocity_ -= rhs.velocity_;
297  return *this;
298 }
299 
301  position_ *= s;
302  velocity_ *= s;
303  return *this;
304 }
305 
307  position_ /= s;
308  velocity_ /= s;
309  return *this;
310 }
311 
312 // Binary operators.
314  lhs += rhs;
315  return lhs;
316 }
317 
319  lhs -= rhs;
320  return lhs;
321 }
322 
324  lhs *= s;
325  return lhs;
326 }
327 
329  rhs *= s;
330  return rhs;
331 }
332 
334  lhs /= s;
335  return lhs;
336 }
337 
339  rhs /= s;
340  return rhs;
341 }
342 
343 } // namespace state
344 } // namespace fastrack
static void SetBounds(const PositionVelocity &lower, const PositionVelocity &upper)
static PositionVelocity Sample()
void SetConfigurationDot(const VectorXd &configuration_dot)
PositionVelocity & operator+=(const PositionVelocity &rhs)
friend PositionVelocity operator/(PositionVelocity lhs, double s)
static const PositionVelocity & GetUpper()
PositionVelocity & operator-=(const PositionVelocity &rhs)
friend PositionVelocity operator+(PositionVelocity lhs, const PositionVelocity &rhs)
static const PositionVelocity & GetLower()
PositionVelocity & operator*=(double s)
std::vector< Vector3d > OccupiedPositions() const
Definition: box.h:53
void FromRos(const fastrack_msgs::State &msg)
fastrack_msgs::State ToRos() const
static constexpr size_t StateDimension()
static std::default_random_engine rng_
Definition: state.h:89
PositionVelocity & operator/=(double s)
friend PositionVelocity operator*(PositionVelocity lhs, double s)
static constexpr size_t ConfigurationDimension()
static PositionVelocity SampleCloseTo(const Vector3d &pos, double d)
friend PositionVelocity operator-(PositionVelocity lhs, const PositionVelocity &rhs)


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