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