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