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 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 6 | #include "aos/flatbuffers.h" |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 7 | #include "Eigen/Dense" |
| 8 | #include "frc971/control_loops/drivetrain/distance_spline.h" |
| 9 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/trajectory_generated.h" |
| 11 | #include "frc971/control_loops/drivetrain/spline_goal_generated.h" |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 12 | #include "frc971/control_loops/hybrid_state_feedback_loop.h" |
| 13 | #include "frc971/control_loops/runge_kutta.h" |
| 14 | #include "frc971/control_loops/state_feedback_loop.h" |
| 15 | |
| 16 | namespace frc971 { |
| 17 | namespace control_loops { |
| 18 | namespace drivetrain { |
| 19 | |
| 20 | template <typename F> |
| 21 | double IntegrateAccelForDistance(const F &fn, double v, double x, double dx) { |
| 22 | // Use a trick from |
| 23 | // https://www.johndcook.com/blog/2012/02/21/care-and-treatment-of-singularities/ |
| 24 | const double a0 = fn(x, v); |
| 25 | |
| 26 | return (RungeKutta( |
| 27 | [&fn, &a0](double t, double y) { |
| 28 | // Since we know that a0 == a(0) and that they are asymtotically |
| 29 | // the same at 0, we know that the limit is 0 at 0. This is |
| 30 | // true because when starting from a stop, under sane |
| 31 | // accelerations, we can assume that we will start with a |
| 32 | // constant acceleration. So, hard-code it. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 33 | if (std::abs(y) < 1e-6) { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 34 | return 0.0; |
| 35 | } |
| 36 | return (fn(t, y) - a0) / y; |
| 37 | }, |
| 38 | v, x, dx) - |
| 39 | v) + |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 40 | std::sqrt(2.0 * a0 * dx + v * v); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 41 | } |
| 42 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 43 | class BaseTrajectory { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 44 | public: |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 45 | BaseTrajectory( |
| 46 | const flatbuffers::Vector<flatbuffers::Offset<Constraint>> *constraints, |
| 47 | const DrivetrainConfig<double> &config); |
| 48 | |
| 49 | virtual ~BaseTrajectory() = default; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 50 | |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 51 | // Returns the friction-constrained velocity limit at a given distance along |
| 52 | // the path. At the returned velocity, one or both wheels will be on the edge |
| 53 | // of slipping. |
| 54 | // There are some very disorganized thoughts on the math here and in some of |
| 55 | // the other functions in spline_math.tex. |
| 56 | double LateralVelocityCurvature(double distance) const; |
| 57 | |
| 58 | // Returns the range of allowable longitudinal accelerations for the center of |
| 59 | // the robot at a particular distance (x) along the path and velocity (v). |
| 60 | // min_accel and max_accel correspodn to the min/max accelerations that can be |
| 61 | // achieved without breaking friction limits on one or both wheels. |
| 62 | // If max_accel < min_accel, that implies that v is too high for there to be |
| 63 | // any valid acceleration. FrictionLngAccelLimits(x, |
| 64 | // LateralVelocityCurvature(x), &min_accel, &max_accel) should result in |
| 65 | // min_accel == max_accel. |
| 66 | void FrictionLngAccelLimits(double x, double v, double *min_accel, |
| 67 | double *max_accel) const; |
| 68 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 69 | // Returns the forwards/backwards acceleration for a distance along the spline |
| 70 | // taking into account the lateral acceleration, longitudinal acceleration, |
| 71 | // and voltage limits. |
| 72 | double BestAcceleration(double x, double v, bool backwards) const; |
| 73 | double BackwardAcceleration(double x, double v) const { |
| 74 | return BestAcceleration(x, v, true); |
| 75 | } |
| 76 | double ForwardAcceleration(double x, double v) const { |
| 77 | return BestAcceleration(x, v, false); |
| 78 | } |
| 79 | |
| 80 | const StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>, |
| 81 | HybridKalman<2, 2, 2>> |
| 82 | &velocity_drivetrain() const { |
| 83 | return *velocity_drivetrain_; |
| 84 | } |
| 85 | |
| 86 | // Returns K1 and K2. |
| 87 | // K2 * d^x/dt^2 + K1 (dx/dt)^2 = A * K2 * dx/dt + B * U |
| 88 | const Eigen::Matrix<double, 2, 1> K1(double current_ddtheta) const; |
| 89 | const Eigen::Matrix<double, 2, 1> K2(double current_dtheta) const; |
| 90 | |
| 91 | // Computes K3, K4, and K5 for the provided distance. |
| 92 | // K5 a + K3 v^2 + K4 v = U |
| 93 | void K345(const double x, Eigen::Matrix<double, 2, 1> *K3, |
| 94 | Eigen::Matrix<double, 2, 1> *K4, |
| 95 | Eigen::Matrix<double, 2, 1> *K5) const; |
| 96 | |
| 97 | virtual const DistanceSpline &spline() const = 0; |
| 98 | |
| 99 | // Returns the length of the path in meters. |
| 100 | double length() const { return spline().length(); } |
| 101 | |
| 102 | // Returns whether a state represents a state at the end of the spline. |
| 103 | bool is_at_end(Eigen::Matrix<double, 2, 1> state) const { |
| 104 | return state(0) > length() - 1e-4; |
| 105 | } |
| 106 | |
| 107 | // Returns true if the state is invalid or unreasonable in some way. |
| 108 | bool state_is_faulted(Eigen::Matrix<double, 2, 1> state) const { |
| 109 | // Consider things faulted if the current velocity implies we are going |
| 110 | // backwards or if any infinities/NaNs have crept in. |
| 111 | return state(1) < 0 || !state.allFinite(); |
| 112 | } |
| 113 | |
| 114 | virtual float plan_velocity(size_t index) const = 0; |
| 115 | virtual size_t distance_plan_size() const = 0; |
| 116 | |
| 117 | // Sets the plan longitudinal acceleration limit |
| 118 | void set_longitudinal_acceleration(double longitudinal_acceleration) { |
| 119 | longitudinal_acceleration_ = longitudinal_acceleration; |
| 120 | } |
| 121 | // Sets the plan lateral acceleration limit |
| 122 | void set_lateral_acceleration(double lateral_acceleration) { |
| 123 | lateral_acceleration_ = lateral_acceleration; |
| 124 | } |
| 125 | // Sets the voltage limit |
| 126 | void set_voltage_limit(double voltage_limit) { |
| 127 | voltage_limit_ = voltage_limit; |
| 128 | } |
| 129 | |
| 130 | float max_lateral_accel() const { return lateral_acceleration_; } |
| 131 | |
| 132 | float max_longitudinal_accel() const { return longitudinal_acceleration_; } |
| 133 | |
| 134 | float max_voltage() const { return voltage_limit_; } |
| 135 | |
| 136 | // Return the next position, velocity, acceleration based on the current |
| 137 | // state. Updates the passed in state for the next iteration. |
| 138 | Eigen::Matrix<double, 3, 1> GetNextXVA( |
| 139 | std::chrono::nanoseconds dt, Eigen::Matrix<double, 2, 1> *state) const; |
| 140 | |
| 141 | // Returns the distance for an index in the plan. |
| 142 | double Distance(int index) const { |
| 143 | return static_cast<double>(index) * length() / |
| 144 | static_cast<double>(distance_plan_size() - 1); |
| 145 | } |
| 146 | |
| 147 | virtual fb::SegmentConstraint plan_constraint(size_t index) const = 0; |
| 148 | |
| 149 | // Returns the feed forwards position, velocity, acceleration for an explicit |
| 150 | // distance. |
| 151 | Eigen::Matrix<double, 3, 1> FFAcceleration(double distance) const; |
| 152 | |
| 153 | // Returns the feed forwards voltage for an explicit distance. |
| 154 | Eigen::Matrix<double, 2, 1> FFVoltage(double distance) const; |
| 155 | |
| 156 | // Computes alpha for a distance. |
| 157 | size_t DistanceToSegment(double distance) const { |
| 158 | return std::max( |
| 159 | static_cast<size_t>(0), |
| 160 | std::min(distance_plan_size() - 1, |
| 161 | static_cast<size_t>(std::floor(distance / length() * |
| 162 | (distance_plan_size() - 1))))); |
| 163 | } |
| 164 | |
| 165 | // Returns the goal state as a function of path distance, velocity. |
| 166 | const ::Eigen::Matrix<double, 5, 1> GoalState(double distance, |
| 167 | double velocity) const; |
| 168 | |
| 169 | protected: |
| 170 | double robot_radius_l() const { return robot_radius_l_; } |
| 171 | double robot_radius_r() const { return robot_radius_r_; } |
| 172 | |
| 173 | private: |
| 174 | static float ConstraintValue( |
| 175 | const flatbuffers::Vector<flatbuffers::Offset<Constraint>> *constraints, |
| 176 | ConstraintType type); |
| 177 | |
| 178 | std::unique_ptr< |
| 179 | StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>, |
| 180 | HybridKalman<2, 2, 2>>> |
| 181 | velocity_drivetrain_; |
| 182 | |
| 183 | const DrivetrainConfig<double> config_; |
| 184 | |
| 185 | // Robot radiuses. |
| 186 | const double robot_radius_l_; |
| 187 | const double robot_radius_r_; |
| 188 | float lateral_acceleration_ = 3.0; |
| 189 | float longitudinal_acceleration_ = 2.0; |
| 190 | float voltage_limit_ = 12.0; |
| 191 | }; |
| 192 | |
| 193 | // A wrapper around the Trajectory flatbuffer to allow for controlling to a |
| 194 | // spline using a pre-generated trajectory. |
| 195 | class FinishedTrajectory : public BaseTrajectory { |
| 196 | public: |
| 197 | // Note: The lifetime of the supplied buffer is assumed to be greater than |
| 198 | // that of this object. |
| 199 | explicit FinishedTrajectory(const DrivetrainConfig<double> &config, |
| 200 | const fb::Trajectory *buffer); |
| 201 | |
| 202 | virtual ~FinishedTrajectory() = default; |
| 203 | |
| 204 | // Takes the 5-element state that is [x, y, theta, v_left, v_right] and |
| 205 | // converts it to a path-relative state, using distance as a linearization |
| 206 | // point (i.e., distance should be roughly equal to the actual distance along |
| 207 | // the path). |
| 208 | Eigen::Matrix<double, 5, 1> StateToPathRelativeState( |
James Kuszmaul | 5e8ce31 | 2021-03-27 14:59:17 -0700 | [diff] [blame^] | 209 | double distance, const Eigen::Matrix<double, 5, 1> &state, |
| 210 | bool drive_backwards) const; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 211 | |
| 212 | // Retrieves the gain matrix K for a given distance along the path. |
| 213 | Eigen::Matrix<double, 2, 5> GainForDistance(double distance) const; |
| 214 | |
| 215 | size_t distance_plan_size() const override; |
| 216 | float plan_velocity(size_t index) const override; |
| 217 | fb::SegmentConstraint plan_constraint(size_t index) const override; |
| 218 | |
| 219 | bool drive_spline_backwards() const { |
| 220 | return trajectory().drive_spline_backwards(); |
| 221 | } |
| 222 | |
| 223 | int spline_handle() const { return trajectory().handle(); } |
| 224 | const fb::Trajectory &trajectory() const { return *buffer_; } |
| 225 | |
| 226 | private: |
| 227 | const DistanceSpline &spline() const override { return spline_; } |
| 228 | const fb::Trajectory *buffer_; |
| 229 | const DistanceSpline spline_; |
| 230 | }; |
| 231 | |
| 232 | // Class to handle plannign a trajectory and producing a flatbuffer containing |
| 233 | // all the information required to create a FinishedTrajectory; |
| 234 | class Trajectory : public BaseTrajectory { |
| 235 | public: |
| 236 | Trajectory(const SplineGoal &spline_goal, |
| 237 | const DrivetrainConfig<double> &config); |
| 238 | Trajectory( |
| 239 | DistanceSpline &&spline, const DrivetrainConfig<double> &config, |
| 240 | const flatbuffers::Vector<flatbuffers::Offset<Constraint>> *constraints, |
| 241 | int spline_idx = 0, double vmax = 10.0, int num_distance = 0); |
| 242 | |
| 243 | virtual ~Trajectory() = default; |
| 244 | |
| 245 | std::vector<Eigen::Matrix<double, 3, 1>> PlanXVA( |
| 246 | std::chrono::nanoseconds dt); |
| 247 | |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 248 | enum class VoltageLimit { |
| 249 | kConservative, |
| 250 | kAggressive, |
| 251 | }; |
| 252 | |
| 253 | // Calculates the maximum voltage at which we *can* track the path. In some |
| 254 | // cases there will be two ranges of feasible velocities for traversing the |
| 255 | // path--in such a situation, from zero to velocity A we will be able to track |
| 256 | // the path, from velocity A to B we can't, from B to C we can and above C we |
| 257 | // can't. If limit_type = kConservative, we return A; if limit_type = |
| 258 | // kAggressive, we return C. We currently just use the kConservative limit |
| 259 | // because that way we can guarantee that all velocities between zero and A |
| 260 | // are allowable and don't have to handle a more complicated planning problem. |
| 261 | // constraint_voltages will be populated by the only wheel voltages that are |
| 262 | // valid at the returned limit. |
| 263 | double VoltageVelocityLimit( |
| 264 | double distance, VoltageLimit limit_type, |
| 265 | Eigen::Matrix<double, 2, 1> *constraint_voltages = nullptr) const; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 266 | |
Austin Schuh | 5b9e9c2 | 2019-01-07 15:44:06 -0800 | [diff] [blame] | 267 | // Limits the velocity in the specified segment to the max velocity. |
| 268 | void LimitVelocity(double starting_distance, double ending_distance, |
| 269 | double max_velocity); |
| 270 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 271 | // Runs the lateral acceleration (curvature) pass on the plan. |
| 272 | void LateralAccelPass(); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 273 | void VoltageFeasibilityPass(VoltageLimit limit_type); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 274 | |
| 275 | // Runs the forwards pass, setting the starting velocity to 0 m/s |
| 276 | void ForwardPass(); |
| 277 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 278 | // Runs the forwards pass, setting the ending velocity to 0 m/s |
| 279 | void BackwardPass(); |
| 280 | |
| 281 | // Runs all the planning passes. |
| 282 | void Plan() { |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 283 | VoltageFeasibilityPass(VoltageLimit::kConservative); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 284 | LateralAccelPass(); |
| 285 | ForwardPass(); |
| 286 | BackwardPass(); |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 287 | CalculatePathGains(); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 288 | } |
| 289 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 290 | // Returns a list of the distances. Mostly useful for plotting. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 291 | const std::vector<double> Distances() const; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 292 | // Returns the distance for an index in the plan. |
| 293 | double Distance(int index) const { |
| 294 | return static_cast<double>(index) * length() / |
| 295 | static_cast<double>(plan_.size() - 1); |
| 296 | } |
| 297 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 298 | const std::vector<fb::SegmentConstraint> &plan_segment_type() const { |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 299 | return plan_segment_type_; |
| 300 | } |
| 301 | |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 302 | // The controller represented by these functions uses a discrete-time, |
| 303 | // finite-horizon LQR with states that are relative to the predicted path |
| 304 | // of the robot to produce a gain to be used on the error. |
| 305 | // The controller does not currently account for saturation, but is defined |
| 306 | // in a way that would make accounting for saturation feasible. |
| 307 | // This controller uses a state of: |
| 308 | // distance along path |
| 309 | // distance lateral to path (positive when robot is to the left of the path). |
| 310 | // heading relative to path (positive if robot pointed to left). |
| 311 | // v_left (speed of left side of robot) |
| 312 | // v_right (speed of right side of robot). |
| 313 | |
| 314 | // Retrieve the continuous-time A/B matrices for the path-relative system |
| 315 | // at the given distance along the path. Performs all linearizations about |
| 316 | // the nominal velocity that the robot should be following at that point |
| 317 | // along the path. |
| 318 | void PathRelativeContinuousSystem(double distance, |
| 319 | Eigen::Matrix<double, 5, 5> *A, |
| 320 | Eigen::Matrix<double, 5, 2> *B); |
| 321 | // Retrieve the continuous-time A/B matrices for the path-relative system |
| 322 | // given the current path-relative state, as defined above. |
| 323 | void PathRelativeContinuousSystem(const Eigen::Matrix<double, 5, 1> &X, |
| 324 | Eigen::Matrix<double, 5, 5> *A, |
| 325 | Eigen::Matrix<double, 5, 2> *B); |
| 326 | |
| 327 | // Takes the 5-element state that is [x, y, theta, v_left, v_right] and |
| 328 | // converts it to a path-relative state, using distance as a linearization |
| 329 | // point (i.e., distance should be roughly equal to the actual distance along |
| 330 | // the path). |
| 331 | Eigen::Matrix<double, 5, 1> StateToPathRelativeState( |
| 332 | double distance, const Eigen::Matrix<double, 5, 1> &state); |
| 333 | |
| 334 | // Estimates the current distance along the path, given the current expected |
| 335 | // distance and the [x, y, theta, v_left, v_right] state. |
| 336 | double EstimateDistanceAlongPath(double nominal_distance, |
| 337 | const Eigen::Matrix<double, 5, 1> &state); |
| 338 | |
| 339 | // Calculates all the gains for each point along the planned trajectory. |
| 340 | // Only called directly in tests; this is normally a part of the planning |
| 341 | // phase, and is a relatively expensive operation. |
| 342 | void CalculatePathGains(); |
| 343 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 344 | flatbuffers::Offset<fb::Trajectory> Serialize( |
| 345 | flatbuffers::FlatBufferBuilder *fbb) const; |
| 346 | |
| 347 | const std::vector<double> plan() const { return plan_; } |
| 348 | |
| 349 | const DistanceSpline &spline() const override { return spline_; } |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 350 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 351 | private: |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 352 | |
| 353 | float plan_velocity(size_t index) const override { |
| 354 | return plan_[index]; |
| 355 | } |
| 356 | size_t distance_plan_size() const override { return plan_.size(); } |
| 357 | |
| 358 | fb::SegmentConstraint plan_constraint(size_t index) const override { |
| 359 | return plan_segment_type_[index]; |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 360 | } |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 361 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 362 | const int spline_idx_; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 363 | |
| 364 | // The spline we are planning for. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 365 | const DistanceSpline spline_; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 366 | |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 367 | const DrivetrainConfig<double> config_; |
| 368 | |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 369 | // Velocities in the plan (distance for each index is defined by Distance()) |
| 370 | std::vector<double> plan_; |
| 371 | // Gain matrices to use for the path-relative state error at each stage in the |
| 372 | // plan Individual elements of the plan_gains_ vector are separated by |
| 373 | // config_.dt in time. |
| 374 | // The first value in the pair is the distance along the path corresponding to |
| 375 | // the gain matrix; the second value is the gain itself. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 376 | std::vector<std::pair<double, Eigen::Matrix<float, 2, 5>>> plan_gains_; |
| 377 | std::vector<fb::SegmentConstraint> plan_segment_type_; |
| 378 | |
| 379 | bool drive_spline_backwards_ = false; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | // Returns the continuous time dynamics given the [x, y, theta, vl, vr] state |
| 383 | // and the [vl, vr] input voltage. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 384 | inline Eigen::Matrix<double, 5, 1> ContinuousDynamics( |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 385 | const StateFeedbackHybridPlant<2, 2, 2> &velocity_drivetrain, |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 386 | const Eigen::Matrix<double, 2, 2> &Tlr_to_la, |
| 387 | const Eigen::Matrix<double, 5, 1> X, |
| 388 | const Eigen::Matrix<double, 2, 1> U) { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 389 | const auto &velocity = X.block<2, 1>(3, 0); |
| 390 | const double theta = X(2); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 391 | Eigen::Matrix<double, 2, 1> la = Tlr_to_la * velocity; |
| 392 | return (Eigen::Matrix<double, 5, 1>() << std::cos(theta) * la(0), |
| 393 | std::sin(theta) * la(0), la(1), |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 394 | (velocity_drivetrain.coefficients().A_continuous * velocity + |
| 395 | velocity_drivetrain.coefficients().B_continuous * U)) |
| 396 | .finished(); |
| 397 | } |
| 398 | |
| 399 | } // namespace drivetrain |
| 400 | } // namespace control_loops |
| 401 | } // namespace frc971 |
| 402 | |
| 403 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_TRAJECTORY_H_ |