blob: 5964bf5d9bfb438a06b5fb267248065dcabb6ab3 [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"
Philipp Schrader790cb542023-07-05 21:06:52 -07007
James Kuszmauldc534432023-02-05 14:51:11 -08008#include "aos/flatbuffers.h"
Austin Schuhec7f06d2019-01-04 07:47:15 +11009#include "frc971/control_loops/drivetrain/distance_spline.h"
10#include "frc971/control_loops/drivetrain/drivetrain_config.h"
James Kuszmaul75a18c52021-03-10 22:02:07 -080011#include "frc971/control_loops/drivetrain/spline_goal_generated.h"
James Kuszmauldc534432023-02-05 14:51:11 -080012#include "frc971/control_loops/drivetrain/trajectory_generated.h"
Austin Schuhec7f06d2019-01-04 07:47:15 +110013#include "frc971/control_loops/hybrid_state_feedback_loop.h"
14#include "frc971/control_loops/runge_kutta.h"
15#include "frc971/control_loops/state_feedback_loop.h"
16
James Kuszmaul5c4ccf62024-03-03 17:29:49 -080017// Note for all of these classes:
18// Whenever a pointer to a DrivetrainConfig is taken in the constructor, it must
19// live for the entire lifetime of the object. The classes here do not take
20// ownership of the pointer.
21
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080022namespace frc971::control_loops::drivetrain {
Austin Schuhec7f06d2019-01-04 07:47:15 +110023
24template <typename F>
25double IntegrateAccelForDistance(const F &fn, double v, double x, double dx) {
26 // Use a trick from
27 // https://www.johndcook.com/blog/2012/02/21/care-and-treatment-of-singularities/
28 const double a0 = fn(x, v);
29
30 return (RungeKutta(
31 [&fn, &a0](double t, double y) {
32 // Since we know that a0 == a(0) and that they are asymtotically
33 // the same at 0, we know that the limit is 0 at 0. This is
34 // true because when starting from a stop, under sane
35 // accelerations, we can assume that we will start with a
36 // constant acceleration. So, hard-code it.
James Kuszmaul75a18c52021-03-10 22:02:07 -080037 if (std::abs(y) < 1e-6) {
Austin Schuhec7f06d2019-01-04 07:47:15 +110038 return 0.0;
39 }
40 return (fn(t, y) - a0) / y;
41 },
42 v, x, dx) -
43 v) +
James Kuszmaul75a18c52021-03-10 22:02:07 -080044 std::sqrt(2.0 * a0 * dx + v * v);
Austin Schuhec7f06d2019-01-04 07:47:15 +110045}
46
James Kuszmaul75a18c52021-03-10 22:02:07 -080047class BaseTrajectory {
Austin Schuhec7f06d2019-01-04 07:47:15 +110048 public:
James Kuszmaul75a18c52021-03-10 22:02:07 -080049 BaseTrajectory(
50 const flatbuffers::Vector<flatbuffers::Offset<Constraint>> *constraints,
James Kuszmaul5c4ccf62024-03-03 17:29:49 -080051 const DrivetrainConfig<double> *config)
Austin Schuhf7c65202022-11-04 21:28:20 -070052 : BaseTrajectory(constraints, config,
53 std::make_shared<StateFeedbackLoop<
54 2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>,
55 HybridKalman<2, 2, 2>>>(
James Kuszmaul5c4ccf62024-03-03 17:29:49 -080056 config->make_hybrid_drivetrain_velocity_loop())) {}
Austin Schuhf7c65202022-11-04 21:28:20 -070057
58 BaseTrajectory(
59 const flatbuffers::Vector<flatbuffers::Offset<Constraint>> *constraints,
James Kuszmaul5c4ccf62024-03-03 17:29:49 -080060 const DrivetrainConfig<double> *config,
Austin Schuhf7c65202022-11-04 21:28:20 -070061 std::shared_ptr<
62 StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>,
63 HybridKalman<2, 2, 2>>>
64 velocity_drivetrain);
James Kuszmaul75a18c52021-03-10 22:02:07 -080065
66 virtual ~BaseTrajectory() = default;
Austin Schuhec7f06d2019-01-04 07:47:15 +110067
James Kuszmaulea314d92019-02-18 19:45:06 -080068 // Returns the friction-constrained velocity limit at a given distance along
69 // the path. At the returned velocity, one or both wheels will be on the edge
70 // of slipping.
71 // There are some very disorganized thoughts on the math here and in some of
72 // the other functions in spline_math.tex.
73 double LateralVelocityCurvature(double distance) const;
74
75 // Returns the range of allowable longitudinal accelerations for the center of
76 // the robot at a particular distance (x) along the path and velocity (v).
77 // min_accel and max_accel correspodn to the min/max accelerations that can be
78 // achieved without breaking friction limits on one or both wheels.
79 // If max_accel < min_accel, that implies that v is too high for there to be
80 // any valid acceleration. FrictionLngAccelLimits(x,
81 // LateralVelocityCurvature(x), &min_accel, &max_accel) should result in
82 // min_accel == max_accel.
83 void FrictionLngAccelLimits(double x, double v, double *min_accel,
84 double *max_accel) const;
85
James Kuszmaul75a18c52021-03-10 22:02:07 -080086 // Returns the forwards/backwards acceleration for a distance along the spline
87 // taking into account the lateral acceleration, longitudinal acceleration,
88 // and voltage limits.
89 double BestAcceleration(double x, double v, bool backwards) const;
90 double BackwardAcceleration(double x, double v) const {
91 return BestAcceleration(x, v, true);
92 }
93 double ForwardAcceleration(double x, double v) const {
94 return BestAcceleration(x, v, false);
95 }
96
97 const StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>,
Austin Schuh50e3dca2023-07-23 14:34:27 -070098 HybridKalman<2, 2, 2>> &
99 velocity_drivetrain() const {
James Kuszmaul75a18c52021-03-10 22:02:07 -0800100 return *velocity_drivetrain_;
101 }
102
103 // Returns K1 and K2.
104 // K2 * d^x/dt^2 + K1 (dx/dt)^2 = A * K2 * dx/dt + B * U
105 const Eigen::Matrix<double, 2, 1> K1(double current_ddtheta) const;
106 const Eigen::Matrix<double, 2, 1> K2(double current_dtheta) const;
107
108 // Computes K3, K4, and K5 for the provided distance.
109 // K5 a + K3 v^2 + K4 v = U
110 void K345(const double x, Eigen::Matrix<double, 2, 1> *K3,
111 Eigen::Matrix<double, 2, 1> *K4,
112 Eigen::Matrix<double, 2, 1> *K5) const;
113
Austin Schuhf7c65202022-11-04 21:28:20 -0700114 virtual const DistanceSplineBase &spline() const = 0;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800115
116 // Returns the length of the path in meters.
117 double length() const { return spline().length(); }
118
119 // Returns whether a state represents a state at the end of the spline.
120 bool is_at_end(Eigen::Matrix<double, 2, 1> state) const {
121 return state(0) > length() - 1e-4;
122 }
123
124 // Returns true if the state is invalid or unreasonable in some way.
125 bool state_is_faulted(Eigen::Matrix<double, 2, 1> state) const {
126 // Consider things faulted if the current velocity implies we are going
127 // backwards or if any infinities/NaNs have crept in.
128 return state(1) < 0 || !state.allFinite();
129 }
130
131 virtual float plan_velocity(size_t index) const = 0;
132 virtual size_t distance_plan_size() const = 0;
133
134 // Sets the plan longitudinal acceleration limit
135 void set_longitudinal_acceleration(double longitudinal_acceleration) {
136 longitudinal_acceleration_ = longitudinal_acceleration;
137 }
138 // Sets the plan lateral acceleration limit
139 void set_lateral_acceleration(double lateral_acceleration) {
140 lateral_acceleration_ = lateral_acceleration;
141 }
142 // Sets the voltage limit
143 void set_voltage_limit(double voltage_limit) {
144 voltage_limit_ = voltage_limit;
145 }
146
147 float max_lateral_accel() const { return lateral_acceleration_; }
148
149 float max_longitudinal_accel() const { return longitudinal_acceleration_; }
150
151 float max_voltage() const { return voltage_limit_; }
152
153 // Return the next position, velocity, acceleration based on the current
154 // state. Updates the passed in state for the next iteration.
155 Eigen::Matrix<double, 3, 1> GetNextXVA(
156 std::chrono::nanoseconds dt, Eigen::Matrix<double, 2, 1> *state) const;
157
158 // Returns the distance for an index in the plan.
159 double Distance(int index) const {
160 return static_cast<double>(index) * length() /
161 static_cast<double>(distance_plan_size() - 1);
162 }
163
164 virtual fb::SegmentConstraint plan_constraint(size_t index) const = 0;
165
166 // Returns the feed forwards position, velocity, acceleration for an explicit
167 // distance.
168 Eigen::Matrix<double, 3, 1> FFAcceleration(double distance) const;
169
170 // Returns the feed forwards voltage for an explicit distance.
171 Eigen::Matrix<double, 2, 1> FFVoltage(double distance) const;
172
173 // Computes alpha for a distance.
174 size_t DistanceToSegment(double distance) const {
175 return std::max(
176 static_cast<size_t>(0),
177 std::min(distance_plan_size() - 1,
178 static_cast<size_t>(std::floor(distance / length() *
179 (distance_plan_size() - 1)))));
180 }
181
182 // Returns the goal state as a function of path distance, velocity.
183 const ::Eigen::Matrix<double, 5, 1> GoalState(double distance,
184 double velocity) const;
185
186 protected:
187 double robot_radius_l() const { return robot_radius_l_; }
188 double robot_radius_r() const { return robot_radius_r_; }
189
190 private:
191 static float ConstraintValue(
192 const flatbuffers::Vector<flatbuffers::Offset<Constraint>> *constraints,
193 ConstraintType type);
194
Austin Schuhf7c65202022-11-04 21:28:20 -0700195 std::shared_ptr<
James Kuszmaul75a18c52021-03-10 22:02:07 -0800196 StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>,
197 HybridKalman<2, 2, 2>>>
198 velocity_drivetrain_;
199
James Kuszmaul5c4ccf62024-03-03 17:29:49 -0800200 const DrivetrainConfig<double> *config_;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800201
202 // Robot radiuses.
James Kuszmauldc534432023-02-05 14:51:11 -0800203 double robot_radius_l_;
204 double robot_radius_r_;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800205 float lateral_acceleration_ = 3.0;
206 float longitudinal_acceleration_ = 2.0;
207 float voltage_limit_ = 12.0;
208};
209
210// A wrapper around the Trajectory flatbuffer to allow for controlling to a
211// spline using a pre-generated trajectory.
James Kuszmauldc534432023-02-05 14:51:11 -0800212class FinishedTrajectory : public BaseTrajectory {
James Kuszmaul75a18c52021-03-10 22:02:07 -0800213 public:
214 // Note: The lifetime of the supplied buffer is assumed to be greater than
215 // that of this object.
Austin Schuhf7c65202022-11-04 21:28:20 -0700216 explicit FinishedTrajectory(
James Kuszmaul5c4ccf62024-03-03 17:29:49 -0800217 const DrivetrainConfig<double> *config, const fb::Trajectory *buffer,
Austin Schuhf7c65202022-11-04 21:28:20 -0700218 std::shared_ptr<
219 StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>,
220 HybridKalman<2, 2, 2>>>
221 velocity_drivetrain);
222
James Kuszmaul5c4ccf62024-03-03 17:29:49 -0800223 explicit FinishedTrajectory(const DrivetrainConfig<double> *config,
Austin Schuhf7c65202022-11-04 21:28:20 -0700224 const fb::Trajectory *buffer)
225 : FinishedTrajectory(
226 config, buffer,
227 std::make_shared<StateFeedbackLoop<
228 2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>,
229 HybridKalman<2, 2, 2>>>(
James Kuszmaul5c4ccf62024-03-03 17:29:49 -0800230 config->make_hybrid_drivetrain_velocity_loop())) {}
James Kuszmaul75a18c52021-03-10 22:02:07 -0800231
James Kuszmauldc534432023-02-05 14:51:11 -0800232 FinishedTrajectory(const FinishedTrajectory &) = delete;
233 FinishedTrajectory &operator=(const FinishedTrajectory &) = delete;
234 FinishedTrajectory(FinishedTrajectory &&) = default;
235 FinishedTrajectory &operator=(FinishedTrajectory &&) = default;
236
James Kuszmaul75a18c52021-03-10 22:02:07 -0800237 virtual ~FinishedTrajectory() = default;
238
239 // Takes the 5-element state that is [x, y, theta, v_left, v_right] and
240 // converts it to a path-relative state, using distance as a linearization
241 // point (i.e., distance should be roughly equal to the actual distance along
242 // the path).
243 Eigen::Matrix<double, 5, 1> StateToPathRelativeState(
James Kuszmaul5e8ce312021-03-27 14:59:17 -0700244 double distance, const Eigen::Matrix<double, 5, 1> &state,
245 bool drive_backwards) const;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800246
247 // Retrieves the gain matrix K for a given distance along the path.
248 Eigen::Matrix<double, 2, 5> GainForDistance(double distance) const;
249
250 size_t distance_plan_size() const override;
251 float plan_velocity(size_t index) const override;
252 fb::SegmentConstraint plan_constraint(size_t index) const override;
253
254 bool drive_spline_backwards() const {
255 return trajectory().drive_spline_backwards();
256 }
257
258 int spline_handle() const { return trajectory().handle(); }
259 const fb::Trajectory &trajectory() const { return *buffer_; }
260
261 private:
Austin Schuhf7c65202022-11-04 21:28:20 -0700262 const DistanceSplineBase &spline() const override { return spline_; }
James Kuszmaul75a18c52021-03-10 22:02:07 -0800263 const fb::Trajectory *buffer_;
James Kuszmauldc534432023-02-05 14:51:11 -0800264 FinishedDistanceSpline spline_;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800265};
266
267// Class to handle plannign a trajectory and producing a flatbuffer containing
268// all the information required to create a FinishedTrajectory;
269class Trajectory : public BaseTrajectory {
270 public:
271 Trajectory(const SplineGoal &spline_goal,
James Kuszmaul5c4ccf62024-03-03 17:29:49 -0800272 const DrivetrainConfig<double> *config);
James Kuszmaul75a18c52021-03-10 22:02:07 -0800273 Trajectory(
James Kuszmaul5c4ccf62024-03-03 17:29:49 -0800274 DistanceSpline &&spline, const DrivetrainConfig<double> *config,
James Kuszmaul75a18c52021-03-10 22:02:07 -0800275 const flatbuffers::Vector<flatbuffers::Offset<Constraint>> *constraints,
276 int spline_idx = 0, double vmax = 10.0, int num_distance = 0);
277
James Kuszmaul5c4ccf62024-03-03 17:29:49 -0800278 // Version that owns its own DrivetrainConfig.
279 Trajectory(
280 DistanceSpline &&spline, std::unique_ptr<DrivetrainConfig<double>> config,
281 const flatbuffers::Vector<flatbuffers::Offset<Constraint>> *constraints,
282 int spline_idx, double vmax, int num_distance);
283
James Kuszmaul75a18c52021-03-10 22:02:07 -0800284 virtual ~Trajectory() = default;
285
James Kuszmauldc534432023-02-05 14:51:11 -0800286 std::vector<Eigen::Matrix<double, 3, 1>> PlanXVA(std::chrono::nanoseconds dt);
James Kuszmaul75a18c52021-03-10 22:02:07 -0800287
James Kuszmaul5c4ccf62024-03-03 17:29:49 -0800288 const DrivetrainConfig<double> *drivetrain_config() { return config_; }
James Kuszmaulea314d92019-02-18 19:45:06 -0800289 enum class VoltageLimit {
290 kConservative,
291 kAggressive,
292 };
293
294 // Calculates the maximum voltage at which we *can* track the path. In some
295 // cases there will be two ranges of feasible velocities for traversing the
296 // path--in such a situation, from zero to velocity A we will be able to track
297 // the path, from velocity A to B we can't, from B to C we can and above C we
298 // can't. If limit_type = kConservative, we return A; if limit_type =
299 // kAggressive, we return C. We currently just use the kConservative limit
300 // because that way we can guarantee that all velocities between zero and A
301 // are allowable and don't have to handle a more complicated planning problem.
302 // constraint_voltages will be populated by the only wheel voltages that are
303 // valid at the returned limit.
304 double VoltageVelocityLimit(
305 double distance, VoltageLimit limit_type,
306 Eigen::Matrix<double, 2, 1> *constraint_voltages = nullptr) const;
Austin Schuhec7f06d2019-01-04 07:47:15 +1100307
Austin Schuh5b9e9c22019-01-07 15:44:06 -0800308 // Limits the velocity in the specified segment to the max velocity.
309 void LimitVelocity(double starting_distance, double ending_distance,
310 double max_velocity);
311
Austin Schuhec7f06d2019-01-04 07:47:15 +1100312 // Runs the lateral acceleration (curvature) pass on the plan.
313 void LateralAccelPass();
James Kuszmaulea314d92019-02-18 19:45:06 -0800314 void VoltageFeasibilityPass(VoltageLimit limit_type);
Austin Schuhec7f06d2019-01-04 07:47:15 +1100315
316 // Runs the forwards pass, setting the starting velocity to 0 m/s
317 void ForwardPass();
318
Austin Schuhec7f06d2019-01-04 07:47:15 +1100319 // Runs the forwards pass, setting the ending velocity to 0 m/s
320 void BackwardPass();
321
322 // Runs all the planning passes.
323 void Plan() {
James Kuszmaulea314d92019-02-18 19:45:06 -0800324 VoltageFeasibilityPass(VoltageLimit::kConservative);
Austin Schuhec7f06d2019-01-04 07:47:15 +1100325 LateralAccelPass();
326 ForwardPass();
327 BackwardPass();
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700328 CalculatePathGains();
Austin Schuhec7f06d2019-01-04 07:47:15 +1100329 }
330
Austin Schuhec7f06d2019-01-04 07:47:15 +1100331 // Returns a list of the distances. Mostly useful for plotting.
James Kuszmaul75a18c52021-03-10 22:02:07 -0800332 const std::vector<double> Distances() const;
Austin Schuhec7f06d2019-01-04 07:47:15 +1100333 // Returns the distance for an index in the plan.
334 double Distance(int index) const {
335 return static_cast<double>(index) * length() /
336 static_cast<double>(plan_.size() - 1);
337 }
338
James Kuszmaul75a18c52021-03-10 22:02:07 -0800339 const std::vector<fb::SegmentConstraint> &plan_segment_type() const {
Austin Schuhe73a9052019-01-07 12:16:17 -0800340 return plan_segment_type_;
341 }
342
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700343 // The controller represented by these functions uses a discrete-time,
344 // finite-horizon LQR with states that are relative to the predicted path
345 // of the robot to produce a gain to be used on the error.
346 // The controller does not currently account for saturation, but is defined
347 // in a way that would make accounting for saturation feasible.
348 // This controller uses a state of:
349 // distance along path
350 // distance lateral to path (positive when robot is to the left of the path).
351 // heading relative to path (positive if robot pointed to left).
352 // v_left (speed of left side of robot)
353 // v_right (speed of right side of robot).
354
355 // Retrieve the continuous-time A/B matrices for the path-relative system
356 // at the given distance along the path. Performs all linearizations about
357 // the nominal velocity that the robot should be following at that point
358 // along the path.
359 void PathRelativeContinuousSystem(double distance,
360 Eigen::Matrix<double, 5, 5> *A,
361 Eigen::Matrix<double, 5, 2> *B);
362 // Retrieve the continuous-time A/B matrices for the path-relative system
363 // given the current path-relative state, as defined above.
364 void PathRelativeContinuousSystem(const Eigen::Matrix<double, 5, 1> &X,
365 Eigen::Matrix<double, 5, 5> *A,
366 Eigen::Matrix<double, 5, 2> *B);
367
368 // Takes the 5-element state that is [x, y, theta, v_left, v_right] and
369 // converts it to a path-relative state, using distance as a linearization
370 // point (i.e., distance should be roughly equal to the actual distance along
371 // the path).
372 Eigen::Matrix<double, 5, 1> StateToPathRelativeState(
373 double distance, const Eigen::Matrix<double, 5, 1> &state);
374
375 // Estimates the current distance along the path, given the current expected
376 // distance and the [x, y, theta, v_left, v_right] state.
377 double EstimateDistanceAlongPath(double nominal_distance,
378 const Eigen::Matrix<double, 5, 1> &state);
379
380 // Calculates all the gains for each point along the planned trajectory.
381 // Only called directly in tests; this is normally a part of the planning
382 // phase, and is a relatively expensive operation.
383 void CalculatePathGains();
384
James Kuszmaul75a18c52021-03-10 22:02:07 -0800385 flatbuffers::Offset<fb::Trajectory> Serialize(
386 flatbuffers::FlatBufferBuilder *fbb) const;
387
388 const std::vector<double> plan() const { return plan_; }
389
390 const DistanceSpline &spline() const override { return spline_; }
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700391
Austin Schuhec7f06d2019-01-04 07:47:15 +1100392 private:
James Kuszmauldc534432023-02-05 14:51:11 -0800393 float plan_velocity(size_t index) const override { return plan_[index]; }
James Kuszmaul75a18c52021-03-10 22:02:07 -0800394 size_t distance_plan_size() const override { return plan_.size(); }
395
396 fb::SegmentConstraint plan_constraint(size_t index) const override {
397 return plan_segment_type_[index];
Austin Schuhe73a9052019-01-07 12:16:17 -0800398 }
Austin Schuhec7f06d2019-01-04 07:47:15 +1100399
James Kuszmaul75a18c52021-03-10 22:02:07 -0800400 const int spline_idx_;
Austin Schuhec7f06d2019-01-04 07:47:15 +1100401
402 // The spline we are planning for.
James Kuszmaul75a18c52021-03-10 22:02:07 -0800403 const DistanceSpline spline_;
Austin Schuhec7f06d2019-01-04 07:47:15 +1100404
James Kuszmaul5c4ccf62024-03-03 17:29:49 -0800405 std::unique_ptr<DrivetrainConfig<double>> owned_config_;
406 const DrivetrainConfig<double> *config_;
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700407
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700408 // Velocities in the plan (distance for each index is defined by Distance())
409 std::vector<double> plan_;
410 // Gain matrices to use for the path-relative state error at each stage in the
411 // plan Individual elements of the plan_gains_ vector are separated by
412 // config_.dt in time.
413 // The first value in the pair is the distance along the path corresponding to
414 // the gain matrix; the second value is the gain itself.
James Kuszmaul75a18c52021-03-10 22:02:07 -0800415 std::vector<std::pair<double, Eigen::Matrix<float, 2, 5>>> plan_gains_;
416 std::vector<fb::SegmentConstraint> plan_segment_type_;
417
418 bool drive_spline_backwards_ = false;
Austin Schuhec7f06d2019-01-04 07:47:15 +1100419};
420
421// Returns the continuous time dynamics given the [x, y, theta, vl, vr] state
422// and the [vl, vr] input voltage.
James Kuszmaul75a18c52021-03-10 22:02:07 -0800423inline Eigen::Matrix<double, 5, 1> ContinuousDynamics(
Austin Schuhec7f06d2019-01-04 07:47:15 +1100424 const StateFeedbackHybridPlant<2, 2, 2> &velocity_drivetrain,
James Kuszmaul75a18c52021-03-10 22:02:07 -0800425 const Eigen::Matrix<double, 2, 2> &Tlr_to_la,
James Kuszmauldc534432023-02-05 14:51:11 -0800426 const Eigen::Matrix<double, 5, 1> X, const Eigen::Matrix<double, 2, 1> U) {
Austin Schuhec7f06d2019-01-04 07:47:15 +1100427 const auto &velocity = X.block<2, 1>(3, 0);
428 const double theta = X(2);
James Kuszmaul75a18c52021-03-10 22:02:07 -0800429 Eigen::Matrix<double, 2, 1> la = Tlr_to_la * velocity;
430 return (Eigen::Matrix<double, 5, 1>() << std::cos(theta) * la(0),
431 std::sin(theta) * la(0), la(1),
Austin Schuhec7f06d2019-01-04 07:47:15 +1100432 (velocity_drivetrain.coefficients().A_continuous * velocity +
433 velocity_drivetrain.coefficients().B_continuous * U))
434 .finished();
435}
436
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800437} // namespace frc971::control_loops::drivetrain
Austin Schuhec7f06d2019-01-04 07:47:15 +1100438
439#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_TRAJECTORY_H_