kinematics.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 // Kinematics are templated on state alone and inherit from dynamics.
40 // Kinematics are treated just like dynamics except that they operate directly
41 // in the configuration space of the templated state type and assume that
42 // the control bounds are maximum speeds in each dimension of that
43 // configuration space.
44 //
46 
47 #ifndef FASTRACK_DYNAMICS_KINEMATICS_H
48 #define FASTRACK_DYNAMICS_KINEMATICS_H
49 
52 #include <fastrack_srvs/KinematicPlannerDynamics.h>
53 
54 #include <exception>
55 
56 namespace fastrack {
57 namespace dynamics {
58 
59 using control::VectorBoundBox;
60 
61 template <typename S>
63  : public Dynamics<S, VectorXd, VectorBoundBox,
64  fastrack_srvs::KinematicPlannerDynamics::Response> {
65  public:
66  explicit Kinematics()
67  : Dynamics<S, VectorXd, VectorBoundBox,
68  fastrack_srvs::KinematicPlannerDynamics::Response>() {}
69  explicit Kinematics(const VectorBoundBox& bound)
70  : Dynamics<S, VectorXd, VectorBoundBox,
71  fastrack_srvs::KinematicPlannerDynamics::Response>(bound) {}
72  explicit Kinematics(const std::vector<double>& params)
73  : Dynamics<S, VectorXd, VectorBoundBox,
74  fastrack_srvs::KinematicPlannerDynamics::Response>(params) {}
75  explicit Kinematics(const VectorXd& u_lower, const VectorXd& u_upper)
76  : Dynamics<S, VectorXd, VectorBoundBox,
77  fastrack_srvs::KinematicPlannerDynamics::Response>(
78  VectorBoundBox(u_lower, u_upper)) {}
79 
80  // Derived classes must be able to give the time derivative of state
81  // as a function of current state and control.
82  inline S Evaluate(const S& x, const VectorXd& u) const;
83 
84  // Since this function does not really make sense for kinematics,
85  // we will throw an error here.
86  inline VectorXd OptimalControl(const S& x, const S& value_gradient) const {
87  throw std::runtime_error("Kinematics: OptimalControl is not implemented.");
88  }
89 
90  // Convert to the appropriate service response type.
91  inline fastrack_srvs::KinematicPlannerDynamics::Response ToRos() const;
92 
93  // Convert from the appropriate service response type.
94  inline void FromRos(
95  const fastrack_srvs::KinematicPlannerDynamics::Response& res);
96 
97  // How much time will it take us to go between two configurations if we move
98  // at max velocity between them in each dimension.
99  double BestPossibleTime(const S& x1, const S& x2) const;
100 }; //\class Kinematics
101 
102 // ----------------------------- IMPLEMENTATION ----------------------------- //
103 
104 // Derived classes must be able to give the time derivative of state
105 // as a function of current state and control.
106 // NOTE! To access the member variables from Dynamics we will need to
107 // use the 'this' keyword (this is a consequence of our template structure).
108 template <typename S>
109 S Kinematics<S>::Evaluate(const S& x, const VectorXd& u) const {
110  // Make sure dimensions agree.
111  const VectorXd c = x.Configuration();
112  if (c.size() != u.size()) {
113  throw std::runtime_error("Kinematics: config/control spaces not equal.");
114  }
115 
116  return S(u);
117 }
118 
119 // Convert to the appropriate service response type.
120 template <typename S>
121 fastrack_srvs::KinematicPlannerDynamics::Response Kinematics<S>::ToRos() const {
122  if (!this->control_bound_)
123  throw std::runtime_error("Kinematics was not initialized.");
124 
125  const auto& lower_bound = this->control_bound_->Min();
126  const auto& upper_bound = this->control_bound_->Max();
127 
128  fastrack_srvs::KinematicPlannerDynamics::Response res;
129  for (size_t ii = 0; ii < lower_bound.size(); ii++) {
130  res.min_speed.push_back(lower_bound(ii));
131  res.max_speed.push_back(upper_bound(ii));
132  }
133 
134  return res;
135 }
136 
137 // Convert from the appropriate service response type.
138 template <typename S>
140  const fastrack_srvs::KinematicPlannerDynamics::Response& res) {
141  if (res.max_speed.size() != res.min_speed.size())
142  throw std::runtime_error("Kinematics: invalid service response.");
143 
144  // Populate control bounds.
145  VectorXd lower_bound(res.min_speed.size());
146  VectorXd upper_bound(res.min_speed.size());
147  for (size_t ii = 0; ii < res.min_speed.size(); ii++) {
148  lower_bound(ii) = res.min_speed[ii];
149  upper_bound(ii) = res.max_speed[ii];
150  }
151 
152  this->control_bound_.reset(new VectorBoundBox(lower_bound, upper_bound));
153 }
154 
155 // How much time will it take us to go between two configurations if we move
156 // at max velocity between them in each dimension.
157 template <typename S>
158 double Kinematics<S>::BestPossibleTime(const S& x1, const S& x2) const {
159  if (!this->control_bound_)
160  throw std::runtime_error("Kinematics was not initialized.");
161 
162  // Unpack into configurations.
163  const VectorXd c1 = x1.Configuration();
164  const VectorXd c2 = x2.Configuration();
165 
166  // Take the maximum of the times in each dimension.
167  double time = -std::numeric_limits<double>::infinity();
168  for (size_t ii = 0; ii < S::ConfigurationDimension(); ii++) {
169  const double time_this_dim =
170  (c2(ii) >= c1(ii)) ? (c2(ii) - c1(ii)) / this->control_bound_->Max()(ii)
171  : (c2(ii) - c1(ii)) / this->control_bound_->Min()(ii);
172  time = std::max(time, time_this_dim);
173  }
174 
175  return time;
176 }
177 
178 } // namespace dynamics
179 } // namespace fastrack
180 
181 #endif
S Evaluate(const S &x, const VectorXd &u) const
Definition: kinematics.h:109
Kinematics(const std::vector< double > &params)
Definition: kinematics.h:72
Definition: box.h:53
Kinematics(const VectorBoundBox &bound)
Definition: kinematics.h:69
double BestPossibleTime(const S &x1, const S &x2) const
Definition: kinematics.h:158
fastrack_srvs::KinematicPlannerDynamics::Response ToRos() const
Definition: kinematics.h:121
void FromRos(const fastrack_srvs::KinematicPlannerDynamics::Response &res)
Definition: kinematics.h:139
Kinematics(const VectorXd &u_lower, const VectorXd &u_upper)
Definition: kinematics.h:75
VectorXd OptimalControl(const S &x, const S &value_gradient) const
Definition: kinematics.h:86


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