blob: 1fb9062ec5244f28b0d71500a3e7677a82ee7a56 [file] [log] [blame]
Austin Schuhec7f06d2019-01-04 07:47:15 +11001#ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_TRAJECTORY_H_
2#define FRC971_CONTROL_LOOPS_DRIVETRAIN_TRAJECTORY_H_
3
4#include <chrono>
5
6#include "Eigen/Dense"
7#include "frc971/control_loops/drivetrain/distance_spline.h"
8#include "frc971/control_loops/drivetrain/drivetrain_config.h"
9#include "frc971/control_loops/hybrid_state_feedback_loop.h"
10#include "frc971/control_loops/runge_kutta.h"
11#include "frc971/control_loops/state_feedback_loop.h"
12
13namespace frc971 {
14namespace control_loops {
15namespace drivetrain {
16
17template <typename F>
18double IntegrateAccelForDistance(const F &fn, double v, double x, double dx) {
19 // Use a trick from
20 // https://www.johndcook.com/blog/2012/02/21/care-and-treatment-of-singularities/
21 const double a0 = fn(x, v);
22
23 return (RungeKutta(
24 [&fn, &a0](double t, double y) {
25 // Since we know that a0 == a(0) and that they are asymtotically
26 // the same at 0, we know that the limit is 0 at 0. This is
27 // true because when starting from a stop, under sane
28 // accelerations, we can assume that we will start with a
29 // constant acceleration. So, hard-code it.
30 if (::std::abs(y) < 1e-6) {
31 return 0.0;
32 }
33 return (fn(t, y) - a0) / y;
34 },
35 v, x, dx) -
36 v) +
37 ::std::sqrt(2.0 * a0 * dx + v * v);
38}
39
40// Class to plan and hold the velocity plan for a spline.
41class Trajectory {
42 public:
43 Trajectory(const DistanceSpline *spline,
Austin Schuh45cfd8b2019-01-07 16:14:31 -080044 const DrivetrainConfig<double> &config, double vmax = 10.0,
45 int num_distance = 0);
Austin Schuhec7f06d2019-01-04 07:47:15 +110046 // Sets the plan longitudal acceleration limit
47 void set_longitudal_acceleration(double longitudal_acceleration) {
48 longitudal_acceleration_ = longitudal_acceleration;
49 }
50 // Sets the plan lateral acceleration limit
51 void set_lateral_acceleration(double lateral_acceleration) {
52 lateral_acceleration_ = lateral_acceleration;
53 }
54 // Sets the voltage limit
55 void set_voltage_limit(double voltage_limit) {
56 voltage_limit_ = voltage_limit;
57 }
58
59 // Returns the velocity limit for a defined distance.
60 double LateralVelocityCurvature(double distance) const {
61 return ::std::sqrt(lateral_acceleration_ / spline_->DDXY(distance).norm());
62 }
63
64 // Runs the lateral acceleration (curvature) pass on the plan.
65 void LateralAccelPass();
66
67 // Returns the forward acceleration for a distance along the spline taking
68 // into account the lateral acceleration, longitudinal acceleration, and
69 // voltage limits.
70 double ForwardAcceleration(const double x, const double v);
71
72 // Runs the forwards pass, setting the starting velocity to 0 m/s
73 void ForwardPass();
74
75 // Returns the backwards acceleration for a distance along the spline taking
76 // into account the lateral acceleration, longitudinal acceleration, and
77 // voltage limits.
78 double BackwardAcceleration(double x, double v);
79
80 // Runs the forwards pass, setting the ending velocity to 0 m/s
81 void BackwardPass();
82
83 // Runs all the planning passes.
84 void Plan() {
85 LateralAccelPass();
86 ForwardPass();
87 BackwardPass();
88 }
89
90 // Returns the feed forwards position, velocity, acceleration for an explicit
91 // distance.
92 ::Eigen::Matrix<double, 3, 1> FFAcceleration(double distance);
93
94 // Returns the feed forwards voltage for an explicit distance.
95 ::Eigen::Matrix<double, 2, 1> FFVoltage(double distance);
96
97 // Returns the length of the path in meters.
98 double length() const { return spline_->length(); }
99
100 // Returns a list of the distances. Mostly useful for plotting.
101 const ::std::vector<double> Distances() const;
102 // Returns the distance for an index in the plan.
103 double Distance(int index) const {
104 return static_cast<double>(index) * length() /
105 static_cast<double>(plan_.size() - 1);
106 }
107
108 // Returns the plan. This is the pathwise velocity as a function of distance.
109 // To get the distance for an index, use the Distance(index) function provided
110 // with the index.
111 const ::std::vector<double> plan() const { return plan_; }
112
113 // Returns the left, right to linear, angular transformation matrix.
114 const ::Eigen::Matrix<double, 2, 2> &Tlr_to_la() const { return Tlr_to_la_; }
115 // Returns the linear, angular to left, right transformation matrix.
116 const ::Eigen::Matrix<double, 2, 2> &Tla_to_lr() const { return Tla_to_lr_; }
117
118 // Returns the goal state as a function of path distance, velocity.
119 const ::Eigen::Matrix<double, 5, 1> GoalState(double distance,
120 double velocity);
121
122 // Returns the velocity drivetrain in use.
123 const StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>,
124 HybridKalman<2, 2, 2>>
125 &velocity_drivetrain() const {
126 return *velocity_drivetrain_;
127 }
128
129 // Returns the continuous statespace A and B matricies for [x, y, theta, vl,
130 // vr] for the linearized system (around the provided state).
131 ::Eigen::Matrix<double, 5, 5> ALinearizedContinuous(
132 const ::Eigen::Matrix<double, 5, 1> &state) const;
133 ::Eigen::Matrix<double, 5, 2> BLinearizedContinuous() const;
134
135 // Returns the discrete time A and B matricies for the provided state,
136 // assuming the provided timestep.
137 void AB(const ::Eigen::Matrix<double, 5, 1> &state,
138 ::std::chrono::nanoseconds dt, ::Eigen::Matrix<double, 5, 5> *A,
139 ::Eigen::Matrix<double, 5, 2> *B) const;
140
141 // Returns the lqr controller for the current state, timestep, and Q and R
142 // gains.
143 // TODO(austin): This feels like it should live somewhere else, but I'm not
144 // sure where. So, throw it here...
145 ::Eigen::Matrix<double, 2, 5> KForState(
146 const ::Eigen::Matrix<double, 5, 1> &state, ::std::chrono::nanoseconds dt,
147 const ::Eigen::DiagonalMatrix<double, 5> &Q,
148 const ::Eigen::DiagonalMatrix<double, 2> &R) const;
149
150 ::std::vector<::Eigen::Matrix<double, 3, 1>> PlanXVA(
151 ::std::chrono::nanoseconds dt);
152
Austin Schuhe73a9052019-01-07 12:16:17 -0800153 enum SegmentType : uint8_t {
154 VELOCITY_LIMITED,
155 CURVATURE_LIMITED,
156 ACCELERATION_LIMITED,
157 DECELERATION_LIMITED
158 };
159
160 const ::std::vector<SegmentType> &plan_segment_type() const {
161 return plan_segment_type_;
162 }
163
Austin Schuhec7f06d2019-01-04 07:47:15 +1100164 private:
165 // Computes alpha for a distance.
Austin Schuhe73a9052019-01-07 12:16:17 -0800166 size_t DistanceToSegment(double distance) const {
167 return ::std::max(
168 static_cast<size_t>(0),
169 ::std::min(plan_segment_type_.size() - 1,
170 static_cast<size_t>(::std::floor(distance / length() *
171 (plan_.size() - 1)))));
172 }
Austin Schuhec7f06d2019-01-04 07:47:15 +1100173
174 // Returns K1 and K2.
175 // K2 * d^x/dt^2 + K1 (dx/dt)^2 = A * K2 * dx/dt + B * U
176 const ::Eigen::Matrix<double, 2, 1> K1(double current_ddtheta) const {
177 return (::Eigen::Matrix<double, 2, 1>()
178 << -robot_radius_l_ * current_ddtheta,
179 robot_radius_r_ * current_ddtheta)
180 .finished();
181 }
182
183 const ::Eigen::Matrix<double, 2, 1> K2(double current_dtheta) const {
184 return (::Eigen::Matrix<double, 2, 1>()
185 << 1.0 - robot_radius_l_ * current_dtheta,
186 1.0 + robot_radius_r_ * current_dtheta)
187 .finished();
188 }
189
190 // Computes K3, K4, and K5 for the provided distance.
191 // K5 a + K3 v^2 + K4 v = U
192 void K345(const double x, ::Eigen::Matrix<double, 2, 1> *K3,
193 ::Eigen::Matrix<double, 2, 1> *K4,
194 ::Eigen::Matrix<double, 2, 1> *K5) {
195 const double current_ddtheta = spline_->DDTheta(x);
196 const double current_dtheta = spline_->DTheta(x);
197 // We've now got the equation:
198 // K2 * d^x/dt^2 + K1 (dx/dt)^2 = A * K2 * dx/dt + B * U
199 const ::Eigen::Matrix<double, 2, 1> my_K2 = K2(current_dtheta);
200
201 const ::Eigen::Matrix<double, 2, 2> B_inverse =
202 velocity_drivetrain_->plant().coefficients().B_continuous.inverse();
203
204 // Now, rephrase it as K5 a + K3 v^2 + K4 v = U
205 *K3 = B_inverse * K1(current_ddtheta);
206 *K4 = -B_inverse *
207 velocity_drivetrain_->plant().coefficients().A_continuous * my_K2;
208 *K5 = B_inverse * my_K2;
209 }
210
211 // The spline we are planning for.
212 const DistanceSpline *spline_;
213 // The drivetrain we are controlling.
214 ::std::unique_ptr<
215 StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>,
216 HybridKalman<2, 2, 2>>>
217 velocity_drivetrain_;
218
219 // Left and right robot radiuses.
220 const double robot_radius_l_;
221 const double robot_radius_r_;
222 // Acceleration limits.
223 double longitudal_acceleration_;
224 double lateral_acceleration_;
225 // Transformation matrix from left, right to linear, angular
226 const ::Eigen::Matrix<double, 2, 2> Tlr_to_la_;
227 // Transformation matrix from linear, angular to left, right
228 const ::Eigen::Matrix<double, 2, 2> Tla_to_lr_;
229 // Velocities in the plan (distance for each index is defined by distance())
230 ::std::vector<double> plan_;
Austin Schuhe73a9052019-01-07 12:16:17 -0800231 ::std::vector<SegmentType> plan_segment_type_;
Austin Schuhec7f06d2019-01-04 07:47:15 +1100232 // Plan voltage limit.
233 double voltage_limit_ = 12.0;
234};
235
236// Returns the continuous time dynamics given the [x, y, theta, vl, vr] state
237// and the [vl, vr] input voltage.
238inline ::Eigen::Matrix<double, 5, 1> ContinuousDynamics(
239 const StateFeedbackHybridPlant<2, 2, 2> &velocity_drivetrain,
240 const ::Eigen::Matrix<double, 2, 2> &Tlr_to_la,
241 const ::Eigen::Matrix<double, 5, 1> X,
242 const ::Eigen::Matrix<double, 2, 1> U) {
243 const auto &velocity = X.block<2, 1>(3, 0);
244 const double theta = X(2);
245 ::Eigen::Matrix<double, 2, 1> la = Tlr_to_la * velocity;
246 return (::Eigen::Matrix<double, 5, 1>() << ::std::cos(theta) * la(0),
247 ::std::sin(theta) * la(0), la(1),
248 (velocity_drivetrain.coefficients().A_continuous * velocity +
249 velocity_drivetrain.coefficients().B_continuous * U))
250 .finished();
251}
252
253} // namespace drivetrain
254} // namespace control_loops
255} // namespace frc971
256
257#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_TRAJECTORY_H_