ilqgames
A new real-time solver for large-scale differential games.
single_player_car_5d.h
1 /*
2  * Copyright (c) 2019, 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 
37 ///////////////////////////////////////////////////////////////////////////////
38 //
39 // Single player dynamics modeling a car. 5 states and 2 control inputs.
40 // State is [x, y, theta, phi, v], control is [omega, a], and dynamics are:
41 // \dot px = v cos theta
42 // \dot py = v sin theta
43 // \dot theta = (v / L) * tan phi
44 // \dot phi = omega
45 // \dot v = a
46 // Please refer to
47 // https://www.sciencedirect.com/science/article/pii/S2405896316301215
48 // for further details.
49 //
50 ///////////////////////////////////////////////////////////////////////////////
51 
52 #ifndef ILQGAMES_DYNAMICS_SINGLE_PLAYER_CAR_5D_H
53 #define ILQGAMES_DYNAMICS_SINGLE_PLAYER_CAR_5D_H
54 
55 #include <ilqgames/dynamics/single_player_dynamical_system.h>
56 #include <ilqgames/utils/types.h>
57 
58 namespace ilqgames {
59 
61  public:
62  ~SinglePlayerCar5D() {}
63  SinglePlayerCar5D(float inter_axle_distance)
64  : SinglePlayerDynamicalSystem(kNumXDims, kNumUDims),
65  inter_axle_distance_(inter_axle_distance) {}
66 
67  // Compute time derivative of state.
68  VectorXf Evaluate(Time t, const VectorXf& x, const VectorXf& u) const;
69 
70  // Compute a discrete-time Jacobian linearization.
71  void Linearize(Time t, const VectorXf& x, const VectorXf& u,
72  Eigen::Ref<MatrixXf> A, Eigen::Ref<MatrixXf> B) const;
73 
74  // Distance metric between two states.
75  float DistanceBetween(const VectorXf& x0, const VectorXf& x1) const;
76 
77  // Position dimensions.
78  std::vector<Dimension> PositionDimensions() const { return {kPxIdx, kPyIdx}; }
79 
80  // Constexprs for state indices.
81  static const Dimension kNumXDims;
82  static const Dimension kPxIdx;
83  static const Dimension kPyIdx;
84  static const Dimension kThetaIdx;
85  static const Dimension kPhiIdx;
86  static const Dimension kVIdx;
87 
88  // Constexprs for control indices.
89  static const Dimension kNumUDims;
90  static const Dimension kOmegaIdx;
91  static const Dimension kAIdx;
92 
93  private:
94  // Inter-axle distance. Determines turning radius.
95  const float inter_axle_distance_;
96 }; //\class SinglePlayerCar5D
97 
98 // ----------------------------- IMPLEMENTATION ----------------------------- //
99 
100 inline VectorXf SinglePlayerCar5D::Evaluate(Time t, const VectorXf& x,
101  const VectorXf& u) const {
102  VectorXf xdot(xdim_);
103  xdot(kPxIdx) = x(kVIdx) * std::cos(x(kThetaIdx));
104  xdot(kPyIdx) = x(kVIdx) * std::sin(x(kThetaIdx));
105  xdot(kThetaIdx) = (x(kVIdx) / inter_axle_distance_) * std::tan(x(kPhiIdx));
106  xdot(kPhiIdx) = u(kOmegaIdx);
107  xdot(kVIdx) = u(kAIdx);
108 
109  return xdot;
110 }
111 
112 inline void SinglePlayerCar5D::Linearize(Time t,
113  const VectorXf& x, const VectorXf& u,
114  Eigen::Ref<MatrixXf> A,
115  Eigen::Ref<MatrixXf> B) const {
116  const float ctheta = std::cos(x(kThetaIdx)) * time::kTimeStep;
117  const float stheta = std::sin(x(kThetaIdx)) * time::kTimeStep;
118  const float cphi = std::cos(x(kPhiIdx));
119  const float tphi = std::tan(x(kPhiIdx));
120 
121  A(kPxIdx, kThetaIdx) += -x(kVIdx) * stheta;
122  A(kPxIdx, kVIdx) += ctheta;
123 
124  A(kPyIdx, kThetaIdx) += x(kVIdx) * ctheta;
125  A(kPyIdx, kVIdx) += stheta;
126 
127  A(kThetaIdx, kPhiIdx) +=
128  x(kVIdx) * time::kTimeStep / (inter_axle_distance_ * cphi * cphi);
129  A(kThetaIdx, kVIdx) += tphi * time::kTimeStep / inter_axle_distance_;
130 
131  B(kPhiIdx, kOmegaIdx) = time::kTimeStep;
132  B(kVIdx, kAIdx) = time::kTimeStep;
133 }
134 
135 inline float SinglePlayerCar5D::DistanceBetween(const VectorXf& x0,
136  const VectorXf& x1) const {
137  // Squared distance in position space.
138  const float dx = x0(kPxIdx) - x1(kPxIdx);
139  const float dy = x0(kPyIdx) - x1(kPyIdx);
140  return dx * dx + dy * dy;
141 }
142 
143 } // namespace ilqgames
144 
145 #endif