blob: 66dcba1fcc638c0370500f8abda57841a6be6c52 [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
Austin Schuh5b9e9c22019-01-07 15:44:06 -080064 // Limits the velocity in the specified segment to the max velocity.
65 void LimitVelocity(double starting_distance, double ending_distance,
66 double max_velocity);
67
Austin Schuhec7f06d2019-01-04 07:47:15 +110068 // Runs the lateral acceleration (curvature) pass on the plan.
69 void LateralAccelPass();
70
71 // Returns the forward acceleration for a distance along the spline taking
72 // into account the lateral acceleration, longitudinal acceleration, and
73 // voltage limits.
74 double ForwardAcceleration(const double x, const double v);
75
76 // Runs the forwards pass, setting the starting velocity to 0 m/s
77 void ForwardPass();
78
79 // Returns the backwards acceleration for a distance along the spline taking
80 // into account the lateral acceleration, longitudinal acceleration, and
81 // voltage limits.
82 double BackwardAcceleration(double x, double v);
83
84 // Runs the forwards pass, setting the ending velocity to 0 m/s
85 void BackwardPass();
86
87 // Runs all the planning passes.
88 void Plan() {
89 LateralAccelPass();
90 ForwardPass();
91 BackwardPass();
92 }
93
94 // Returns the feed forwards position, velocity, acceleration for an explicit
95 // distance.
96 ::Eigen::Matrix<double, 3, 1> FFAcceleration(double distance);
97
98 // Returns the feed forwards voltage for an explicit distance.
99 ::Eigen::Matrix<double, 2, 1> FFVoltage(double distance);
100
Alex Perry4ae2fd72019-02-03 15:55:57 -0800101 // Returns whether a state represents a state at the end of the spline.
102 bool is_at_end(::Eigen::Matrix<double, 2, 1> state) const {
103 return state(0) > length() - 1e-4;
104 }
105
Austin Schuhec7f06d2019-01-04 07:47:15 +1100106 // Returns the length of the path in meters.
107 double length() const { return spline_->length(); }
108
109 // Returns a list of the distances. Mostly useful for plotting.
110 const ::std::vector<double> Distances() const;
111 // Returns the distance for an index in the plan.
112 double Distance(int index) const {
113 return static_cast<double>(index) * length() /
114 static_cast<double>(plan_.size() - 1);
115 }
116
117 // Returns the plan. This is the pathwise velocity as a function of distance.
118 // To get the distance for an index, use the Distance(index) function provided
119 // with the index.
120 const ::std::vector<double> plan() const { return plan_; }
121
122 // Returns the left, right to linear, angular transformation matrix.
123 const ::Eigen::Matrix<double, 2, 2> &Tlr_to_la() const { return Tlr_to_la_; }
124 // Returns the linear, angular to left, right transformation matrix.
125 const ::Eigen::Matrix<double, 2, 2> &Tla_to_lr() const { return Tla_to_lr_; }
126
127 // Returns the goal state as a function of path distance, velocity.
128 const ::Eigen::Matrix<double, 5, 1> GoalState(double distance,
129 double velocity);
130
131 // Returns the velocity drivetrain in use.
132 const StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>,
133 HybridKalman<2, 2, 2>>
134 &velocity_drivetrain() const {
135 return *velocity_drivetrain_;
136 }
137
138 // Returns the continuous statespace A and B matricies for [x, y, theta, vl,
139 // vr] for the linearized system (around the provided state).
140 ::Eigen::Matrix<double, 5, 5> ALinearizedContinuous(
141 const ::Eigen::Matrix<double, 5, 1> &state) const;
142 ::Eigen::Matrix<double, 5, 2> BLinearizedContinuous() const;
143
144 // Returns the discrete time A and B matricies for the provided state,
145 // assuming the provided timestep.
146 void AB(const ::Eigen::Matrix<double, 5, 1> &state,
147 ::std::chrono::nanoseconds dt, ::Eigen::Matrix<double, 5, 5> *A,
148 ::Eigen::Matrix<double, 5, 2> *B) const;
149
150 // Returns the lqr controller for the current state, timestep, and Q and R
151 // gains.
152 // TODO(austin): This feels like it should live somewhere else, but I'm not
153 // sure where. So, throw it here...
154 ::Eigen::Matrix<double, 2, 5> KForState(
155 const ::Eigen::Matrix<double, 5, 1> &state, ::std::chrono::nanoseconds dt,
156 const ::Eigen::DiagonalMatrix<double, 5> &Q,
157 const ::Eigen::DiagonalMatrix<double, 2> &R) const;
158
Alex Perry4ae2fd72019-02-03 15:55:57 -0800159 // Return the next position, velocity, acceleration based on the current
160 // state. Updates the passed in state for the next iteration.
161 ::Eigen::Matrix<double, 3, 1> GetNextXVA(
162 ::std::chrono::nanoseconds dt, ::Eigen::Matrix<double, 2, 1> *state);
Austin Schuhec7f06d2019-01-04 07:47:15 +1100163 ::std::vector<::Eigen::Matrix<double, 3, 1>> PlanXVA(
164 ::std::chrono::nanoseconds dt);
165
Austin Schuhe73a9052019-01-07 12:16:17 -0800166 enum SegmentType : uint8_t {
167 VELOCITY_LIMITED,
168 CURVATURE_LIMITED,
169 ACCELERATION_LIMITED,
170 DECELERATION_LIMITED
171 };
172
173 const ::std::vector<SegmentType> &plan_segment_type() const {
174 return plan_segment_type_;
175 }
176
Austin Schuhec7f06d2019-01-04 07:47:15 +1100177 private:
178 // Computes alpha for a distance.
Austin Schuhe73a9052019-01-07 12:16:17 -0800179 size_t DistanceToSegment(double distance) const {
180 return ::std::max(
181 static_cast<size_t>(0),
182 ::std::min(plan_segment_type_.size() - 1,
183 static_cast<size_t>(::std::floor(distance / length() *
184 (plan_.size() - 1)))));
185 }
Austin Schuhec7f06d2019-01-04 07:47:15 +1100186
187 // Returns K1 and K2.
188 // K2 * d^x/dt^2 + K1 (dx/dt)^2 = A * K2 * dx/dt + B * U
189 const ::Eigen::Matrix<double, 2, 1> K1(double current_ddtheta) const {
190 return (::Eigen::Matrix<double, 2, 1>()
191 << -robot_radius_l_ * current_ddtheta,
192 robot_radius_r_ * current_ddtheta)
193 .finished();
194 }
195
196 const ::Eigen::Matrix<double, 2, 1> K2(double current_dtheta) const {
197 return (::Eigen::Matrix<double, 2, 1>()
198 << 1.0 - robot_radius_l_ * current_dtheta,
199 1.0 + robot_radius_r_ * current_dtheta)
200 .finished();
201 }
202
203 // Computes K3, K4, and K5 for the provided distance.
204 // K5 a + K3 v^2 + K4 v = U
205 void K345(const double x, ::Eigen::Matrix<double, 2, 1> *K3,
206 ::Eigen::Matrix<double, 2, 1> *K4,
207 ::Eigen::Matrix<double, 2, 1> *K5) {
208 const double current_ddtheta = spline_->DDTheta(x);
209 const double current_dtheta = spline_->DTheta(x);
210 // We've now got the equation:
211 // K2 * d^x/dt^2 + K1 (dx/dt)^2 = A * K2 * dx/dt + B * U
212 const ::Eigen::Matrix<double, 2, 1> my_K2 = K2(current_dtheta);
213
214 const ::Eigen::Matrix<double, 2, 2> B_inverse =
215 velocity_drivetrain_->plant().coefficients().B_continuous.inverse();
216
217 // Now, rephrase it as K5 a + K3 v^2 + K4 v = U
218 *K3 = B_inverse * K1(current_ddtheta);
219 *K4 = -B_inverse *
220 velocity_drivetrain_->plant().coefficients().A_continuous * my_K2;
221 *K5 = B_inverse * my_K2;
222 }
223
224 // The spline we are planning for.
225 const DistanceSpline *spline_;
226 // The drivetrain we are controlling.
227 ::std::unique_ptr<
228 StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>,
229 HybridKalman<2, 2, 2>>>
230 velocity_drivetrain_;
231
232 // Left and right robot radiuses.
233 const double robot_radius_l_;
234 const double robot_radius_r_;
235 // Acceleration limits.
236 double longitudal_acceleration_;
237 double lateral_acceleration_;
238 // Transformation matrix from left, right to linear, angular
239 const ::Eigen::Matrix<double, 2, 2> Tlr_to_la_;
240 // Transformation matrix from linear, angular to left, right
241 const ::Eigen::Matrix<double, 2, 2> Tla_to_lr_;
242 // Velocities in the plan (distance for each index is defined by distance())
243 ::std::vector<double> plan_;
Austin Schuhe73a9052019-01-07 12:16:17 -0800244 ::std::vector<SegmentType> plan_segment_type_;
Austin Schuhec7f06d2019-01-04 07:47:15 +1100245 // Plan voltage limit.
246 double voltage_limit_ = 12.0;
247};
248
249// Returns the continuous time dynamics given the [x, y, theta, vl, vr] state
250// and the [vl, vr] input voltage.
251inline ::Eigen::Matrix<double, 5, 1> ContinuousDynamics(
252 const StateFeedbackHybridPlant<2, 2, 2> &velocity_drivetrain,
253 const ::Eigen::Matrix<double, 2, 2> &Tlr_to_la,
254 const ::Eigen::Matrix<double, 5, 1> X,
255 const ::Eigen::Matrix<double, 2, 1> U) {
256 const auto &velocity = X.block<2, 1>(3, 0);
257 const double theta = X(2);
258 ::Eigen::Matrix<double, 2, 1> la = Tlr_to_la * velocity;
259 return (::Eigen::Matrix<double, 5, 1>() << ::std::cos(theta) * la(0),
260 ::std::sin(theta) * la(0), la(1),
261 (velocity_drivetrain.coefficients().A_continuous * velocity +
262 velocity_drivetrain.coefficients().B_continuous * U))
263 .finished();
264}
265
266} // namespace drivetrain
267} // namespace control_loops
268} // namespace frc971
269
270#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_TRAJECTORY_H_