Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 1 | #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 Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 7 | |
James Kuszmaul | dc53443 | 2023-02-05 14:51:11 -0800 | [diff] [blame] | 8 | #include "aos/flatbuffers.h" |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 9 | #include "frc971/control_loops/drivetrain/distance_spline.h" |
| 10 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/drivetrain/spline_goal_generated.h" |
James Kuszmaul | dc53443 | 2023-02-05 14:51:11 -0800 | [diff] [blame] | 12 | #include "frc971/control_loops/drivetrain/trajectory_generated.h" |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 13 | #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 Kuszmaul | 5c4ccf6 | 2024-03-03 17:29:49 -0800 | [diff] [blame] | 17 | // 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 Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 22 | namespace frc971::control_loops::drivetrain { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 23 | |
| 24 | template <typename F> |
| 25 | double 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 Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 37 | if (std::abs(y) < 1e-6) { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 38 | return 0.0; |
| 39 | } |
| 40 | return (fn(t, y) - a0) / y; |
| 41 | }, |
| 42 | v, x, dx) - |
| 43 | v) + |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 44 | std::sqrt(2.0 * a0 * dx + v * v); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 45 | } |
| 46 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 47 | class BaseTrajectory { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 48 | public: |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 49 | BaseTrajectory( |
| 50 | const flatbuffers::Vector<flatbuffers::Offset<Constraint>> *constraints, |
James Kuszmaul | 5c4ccf6 | 2024-03-03 17:29:49 -0800 | [diff] [blame] | 51 | const DrivetrainConfig<double> *config) |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 52 | : BaseTrajectory(constraints, config, |
| 53 | std::make_shared<StateFeedbackLoop< |
| 54 | 2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>, |
| 55 | HybridKalman<2, 2, 2>>>( |
James Kuszmaul | 5c4ccf6 | 2024-03-03 17:29:49 -0800 | [diff] [blame] | 56 | config->make_hybrid_drivetrain_velocity_loop())) {} |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 57 | |
| 58 | BaseTrajectory( |
| 59 | const flatbuffers::Vector<flatbuffers::Offset<Constraint>> *constraints, |
James Kuszmaul | 5c4ccf6 | 2024-03-03 17:29:49 -0800 | [diff] [blame] | 60 | const DrivetrainConfig<double> *config, |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 61 | std::shared_ptr< |
| 62 | StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>, |
| 63 | HybridKalman<2, 2, 2>>> |
| 64 | velocity_drivetrain); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 65 | |
| 66 | virtual ~BaseTrajectory() = default; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 67 | |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 68 | // 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 Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 86 | // 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 Schuh | 50e3dca | 2023-07-23 14:34:27 -0700 | [diff] [blame] | 98 | HybridKalman<2, 2, 2>> & |
| 99 | velocity_drivetrain() const { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 100 | 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 Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 114 | virtual const DistanceSplineBase &spline() const = 0; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 115 | |
| 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 Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 195 | std::shared_ptr< |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 196 | StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>, |
| 197 | HybridKalman<2, 2, 2>>> |
| 198 | velocity_drivetrain_; |
| 199 | |
James Kuszmaul | 5c4ccf6 | 2024-03-03 17:29:49 -0800 | [diff] [blame] | 200 | const DrivetrainConfig<double> *config_; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 201 | |
| 202 | // Robot radiuses. |
James Kuszmaul | dc53443 | 2023-02-05 14:51:11 -0800 | [diff] [blame] | 203 | double robot_radius_l_; |
| 204 | double robot_radius_r_; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 205 | 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 Kuszmaul | dc53443 | 2023-02-05 14:51:11 -0800 | [diff] [blame] | 212 | class FinishedTrajectory : public BaseTrajectory { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 213 | public: |
| 214 | // Note: The lifetime of the supplied buffer is assumed to be greater than |
| 215 | // that of this object. |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 216 | explicit FinishedTrajectory( |
James Kuszmaul | 5c4ccf6 | 2024-03-03 17:29:49 -0800 | [diff] [blame] | 217 | const DrivetrainConfig<double> *config, const fb::Trajectory *buffer, |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 218 | std::shared_ptr< |
| 219 | StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>, |
| 220 | HybridKalman<2, 2, 2>>> |
| 221 | velocity_drivetrain); |
| 222 | |
James Kuszmaul | 5c4ccf6 | 2024-03-03 17:29:49 -0800 | [diff] [blame] | 223 | explicit FinishedTrajectory(const DrivetrainConfig<double> *config, |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 224 | 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 Kuszmaul | 5c4ccf6 | 2024-03-03 17:29:49 -0800 | [diff] [blame] | 230 | config->make_hybrid_drivetrain_velocity_loop())) {} |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 231 | |
James Kuszmaul | dc53443 | 2023-02-05 14:51:11 -0800 | [diff] [blame] | 232 | FinishedTrajectory(const FinishedTrajectory &) = delete; |
| 233 | FinishedTrajectory &operator=(const FinishedTrajectory &) = delete; |
| 234 | FinishedTrajectory(FinishedTrajectory &&) = default; |
| 235 | FinishedTrajectory &operator=(FinishedTrajectory &&) = default; |
| 236 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 237 | 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 Kuszmaul | 5e8ce31 | 2021-03-27 14:59:17 -0700 | [diff] [blame] | 244 | double distance, const Eigen::Matrix<double, 5, 1> &state, |
| 245 | bool drive_backwards) const; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 246 | |
| 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 Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 262 | const DistanceSplineBase &spline() const override { return spline_; } |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 263 | const fb::Trajectory *buffer_; |
James Kuszmaul | dc53443 | 2023-02-05 14:51:11 -0800 | [diff] [blame] | 264 | FinishedDistanceSpline spline_; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | // Class to handle plannign a trajectory and producing a flatbuffer containing |
| 268 | // all the information required to create a FinishedTrajectory; |
| 269 | class Trajectory : public BaseTrajectory { |
| 270 | public: |
| 271 | Trajectory(const SplineGoal &spline_goal, |
James Kuszmaul | 5c4ccf6 | 2024-03-03 17:29:49 -0800 | [diff] [blame] | 272 | const DrivetrainConfig<double> *config); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 273 | Trajectory( |
James Kuszmaul | 5c4ccf6 | 2024-03-03 17:29:49 -0800 | [diff] [blame] | 274 | DistanceSpline &&spline, const DrivetrainConfig<double> *config, |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 275 | const flatbuffers::Vector<flatbuffers::Offset<Constraint>> *constraints, |
| 276 | int spline_idx = 0, double vmax = 10.0, int num_distance = 0); |
| 277 | |
James Kuszmaul | 5c4ccf6 | 2024-03-03 17:29:49 -0800 | [diff] [blame] | 278 | // 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 Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 284 | virtual ~Trajectory() = default; |
| 285 | |
James Kuszmaul | dc53443 | 2023-02-05 14:51:11 -0800 | [diff] [blame] | 286 | std::vector<Eigen::Matrix<double, 3, 1>> PlanXVA(std::chrono::nanoseconds dt); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 287 | |
James Kuszmaul | 5c4ccf6 | 2024-03-03 17:29:49 -0800 | [diff] [blame] | 288 | const DrivetrainConfig<double> *drivetrain_config() { return config_; } |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 289 | 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 Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 307 | |
Austin Schuh | 5b9e9c2 | 2019-01-07 15:44:06 -0800 | [diff] [blame] | 308 | // 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 Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 312 | // Runs the lateral acceleration (curvature) pass on the plan. |
| 313 | void LateralAccelPass(); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 314 | void VoltageFeasibilityPass(VoltageLimit limit_type); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 315 | |
| 316 | // Runs the forwards pass, setting the starting velocity to 0 m/s |
| 317 | void ForwardPass(); |
| 318 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 319 | // 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 Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 324 | VoltageFeasibilityPass(VoltageLimit::kConservative); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 325 | LateralAccelPass(); |
| 326 | ForwardPass(); |
| 327 | BackwardPass(); |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 328 | CalculatePathGains(); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 329 | } |
| 330 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 331 | // Returns a list of the distances. Mostly useful for plotting. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 332 | const std::vector<double> Distances() const; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 333 | // 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 Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 339 | const std::vector<fb::SegmentConstraint> &plan_segment_type() const { |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 340 | return plan_segment_type_; |
| 341 | } |
| 342 | |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 343 | // 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 Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 385 | 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 Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 391 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 392 | private: |
James Kuszmaul | dc53443 | 2023-02-05 14:51:11 -0800 | [diff] [blame] | 393 | float plan_velocity(size_t index) const override { return plan_[index]; } |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 394 | 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 Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 398 | } |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 399 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 400 | const int spline_idx_; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 401 | |
| 402 | // The spline we are planning for. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 403 | const DistanceSpline spline_; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 404 | |
James Kuszmaul | 5c4ccf6 | 2024-03-03 17:29:49 -0800 | [diff] [blame] | 405 | std::unique_ptr<DrivetrainConfig<double>> owned_config_; |
| 406 | const DrivetrainConfig<double> *config_; |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 407 | |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 408 | // 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 Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 415 | 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 Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | // Returns the continuous time dynamics given the [x, y, theta, vl, vr] state |
| 422 | // and the [vl, vr] input voltage. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 423 | inline Eigen::Matrix<double, 5, 1> ContinuousDynamics( |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 424 | const StateFeedbackHybridPlant<2, 2, 2> &velocity_drivetrain, |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 425 | const Eigen::Matrix<double, 2, 2> &Tlr_to_la, |
James Kuszmaul | dc53443 | 2023-02-05 14:51:11 -0800 | [diff] [blame] | 426 | const Eigen::Matrix<double, 5, 1> X, const Eigen::Matrix<double, 2, 1> U) { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 427 | const auto &velocity = X.block<2, 1>(3, 0); |
| 428 | const double theta = X(2); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 429 | 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 Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 432 | (velocity_drivetrain.coefficients().A_continuous * velocity + |
| 433 | velocity_drivetrain.coefficients().B_continuous * U)) |
| 434 | .finished(); |
| 435 | } |
| 436 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 437 | } // namespace frc971::control_loops::drivetrain |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 438 | |
| 439 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_TRAJECTORY_H_ |