blob: d3468b3ea9c41744d3b12ea4b3178c7dad04db80 [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
101 // Returns the length of the path in meters.
102 double length() const { return spline_->length(); }
103
104 // Returns a list of the distances. Mostly useful for plotting.
105 const ::std::vector<double> Distances() const;
106 // Returns the distance for an index in the plan.
107 double Distance(int index) const {
108 return static_cast<double>(index) * length() /
109 static_cast<double>(plan_.size() - 1);
110 }
111
112 // Returns the plan. This is the pathwise velocity as a function of distance.
113 // To get the distance for an index, use the Distance(index) function provided
114 // with the index.
115 const ::std::vector<double> plan() const { return plan_; }
116
117 // Returns the left, right to linear, angular transformation matrix.
118 const ::Eigen::Matrix<double, 2, 2> &Tlr_to_la() const { return Tlr_to_la_; }
119 // Returns the linear, angular to left, right transformation matrix.
120 const ::Eigen::Matrix<double, 2, 2> &Tla_to_lr() const { return Tla_to_lr_; }
121
122 // Returns the goal state as a function of path distance, velocity.
123 const ::Eigen::Matrix<double, 5, 1> GoalState(double distance,
124 double velocity);
125
126 // Returns the velocity drivetrain in use.
127 const StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>,
128 HybridKalman<2, 2, 2>>
129 &velocity_drivetrain() const {
130 return *velocity_drivetrain_;
131 }
132
133 // Returns the continuous statespace A and B matricies for [x, y, theta, vl,
134 // vr] for the linearized system (around the provided state).
135 ::Eigen::Matrix<double, 5, 5> ALinearizedContinuous(
136 const ::Eigen::Matrix<double, 5, 1> &state) const;
137 ::Eigen::Matrix<double, 5, 2> BLinearizedContinuous() const;
138
139 // Returns the discrete time A and B matricies for the provided state,
140 // assuming the provided timestep.
141 void AB(const ::Eigen::Matrix<double, 5, 1> &state,
142 ::std::chrono::nanoseconds dt, ::Eigen::Matrix<double, 5, 5> *A,
143 ::Eigen::Matrix<double, 5, 2> *B) const;
144
145 // Returns the lqr controller for the current state, timestep, and Q and R
146 // gains.
147 // TODO(austin): This feels like it should live somewhere else, but I'm not
148 // sure where. So, throw it here...
149 ::Eigen::Matrix<double, 2, 5> KForState(
150 const ::Eigen::Matrix<double, 5, 1> &state, ::std::chrono::nanoseconds dt,
151 const ::Eigen::DiagonalMatrix<double, 5> &Q,
152 const ::Eigen::DiagonalMatrix<double, 2> &R) const;
153
154 ::std::vector<::Eigen::Matrix<double, 3, 1>> PlanXVA(
155 ::std::chrono::nanoseconds dt);
156
Austin Schuhe73a9052019-01-07 12:16:17 -0800157 enum SegmentType : uint8_t {
158 VELOCITY_LIMITED,
159 CURVATURE_LIMITED,
160 ACCELERATION_LIMITED,
161 DECELERATION_LIMITED
162 };
163
164 const ::std::vector<SegmentType> &plan_segment_type() const {
165 return plan_segment_type_;
166 }
167
Austin Schuhec7f06d2019-01-04 07:47:15 +1100168 private:
169 // Computes alpha for a distance.
Austin Schuhe73a9052019-01-07 12:16:17 -0800170 size_t DistanceToSegment(double distance) const {
171 return ::std::max(
172 static_cast<size_t>(0),
173 ::std::min(plan_segment_type_.size() - 1,
174 static_cast<size_t>(::std::floor(distance / length() *
175 (plan_.size() - 1)))));
176 }
Austin Schuhec7f06d2019-01-04 07:47:15 +1100177
178 // Returns K1 and K2.
179 // K2 * d^x/dt^2 + K1 (dx/dt)^2 = A * K2 * dx/dt + B * U
180 const ::Eigen::Matrix<double, 2, 1> K1(double current_ddtheta) const {
181 return (::Eigen::Matrix<double, 2, 1>()
182 << -robot_radius_l_ * current_ddtheta,
183 robot_radius_r_ * current_ddtheta)
184 .finished();
185 }
186
187 const ::Eigen::Matrix<double, 2, 1> K2(double current_dtheta) const {
188 return (::Eigen::Matrix<double, 2, 1>()
189 << 1.0 - robot_radius_l_ * current_dtheta,
190 1.0 + robot_radius_r_ * current_dtheta)
191 .finished();
192 }
193
194 // Computes K3, K4, and K5 for the provided distance.
195 // K5 a + K3 v^2 + K4 v = U
196 void K345(const double x, ::Eigen::Matrix<double, 2, 1> *K3,
197 ::Eigen::Matrix<double, 2, 1> *K4,
198 ::Eigen::Matrix<double, 2, 1> *K5) {
199 const double current_ddtheta = spline_->DDTheta(x);
200 const double current_dtheta = spline_->DTheta(x);
201 // We've now got the equation:
202 // K2 * d^x/dt^2 + K1 (dx/dt)^2 = A * K2 * dx/dt + B * U
203 const ::Eigen::Matrix<double, 2, 1> my_K2 = K2(current_dtheta);
204
205 const ::Eigen::Matrix<double, 2, 2> B_inverse =
206 velocity_drivetrain_->plant().coefficients().B_continuous.inverse();
207
208 // Now, rephrase it as K5 a + K3 v^2 + K4 v = U
209 *K3 = B_inverse * K1(current_ddtheta);
210 *K4 = -B_inverse *
211 velocity_drivetrain_->plant().coefficients().A_continuous * my_K2;
212 *K5 = B_inverse * my_K2;
213 }
214
215 // The spline we are planning for.
216 const DistanceSpline *spline_;
217 // The drivetrain we are controlling.
218 ::std::unique_ptr<
219 StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>,
220 HybridKalman<2, 2, 2>>>
221 velocity_drivetrain_;
222
223 // Left and right robot radiuses.
224 const double robot_radius_l_;
225 const double robot_radius_r_;
226 // Acceleration limits.
227 double longitudal_acceleration_;
228 double lateral_acceleration_;
229 // Transformation matrix from left, right to linear, angular
230 const ::Eigen::Matrix<double, 2, 2> Tlr_to_la_;
231 // Transformation matrix from linear, angular to left, right
232 const ::Eigen::Matrix<double, 2, 2> Tla_to_lr_;
233 // Velocities in the plan (distance for each index is defined by distance())
234 ::std::vector<double> plan_;
Austin Schuhe73a9052019-01-07 12:16:17 -0800235 ::std::vector<SegmentType> plan_segment_type_;
Austin Schuhec7f06d2019-01-04 07:47:15 +1100236 // Plan voltage limit.
237 double voltage_limit_ = 12.0;
238};
239
240// Returns the continuous time dynamics given the [x, y, theta, vl, vr] state
241// and the [vl, vr] input voltage.
242inline ::Eigen::Matrix<double, 5, 1> ContinuousDynamics(
243 const StateFeedbackHybridPlant<2, 2, 2> &velocity_drivetrain,
244 const ::Eigen::Matrix<double, 2, 2> &Tlr_to_la,
245 const ::Eigen::Matrix<double, 5, 1> X,
246 const ::Eigen::Matrix<double, 2, 1> U) {
247 const auto &velocity = X.block<2, 1>(3, 0);
248 const double theta = X(2);
249 ::Eigen::Matrix<double, 2, 1> la = Tlr_to_la * velocity;
250 return (::Eigen::Matrix<double, 5, 1>() << ::std::cos(theta) * la(0),
251 ::std::sin(theta) * la(0), la(1),
252 (velocity_drivetrain.coefficients().A_continuous * velocity +
253 velocity_drivetrain.coefficients().B_continuous * U))
254 .finished();
255}
256
257} // namespace drivetrain
258} // namespace control_loops
259} // namespace frc971
260
261#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_TRAJECTORY_H_