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" |
| 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 | |
| 13 | namespace frc971 { |
| 14 | namespace control_loops { |
| 15 | namespace drivetrain { |
| 16 | |
| 17 | template <typename F> |
| 18 | double 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. |
| 41 | class Trajectory { |
| 42 | public: |
| 43 | Trajectory(const DistanceSpline *spline, |
Austin Schuh | 45cfd8b | 2019-01-07 16:14:31 -0800 | [diff] [blame] | 44 | const DrivetrainConfig<double> &config, double vmax = 10.0, |
| 45 | int num_distance = 0); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 46 | // Sets the plan longitudinal acceleration limit |
| 47 | void set_longitudinal_acceleration(double longitudinal_acceleration) { |
| 48 | longitudinal_acceleration_ = longitudinal_acceleration; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 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 | |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 59 | // Returns the friction-constrained velocity limit at a given distance along |
| 60 | // the path. At the returned velocity, one or both wheels will be on the edge |
| 61 | // of slipping. |
| 62 | // There are some very disorganized thoughts on the math here and in some of |
| 63 | // the other functions in spline_math.tex. |
| 64 | double LateralVelocityCurvature(double distance) const; |
| 65 | |
| 66 | // Returns the range of allowable longitudinal accelerations for the center of |
| 67 | // the robot at a particular distance (x) along the path and velocity (v). |
| 68 | // min_accel and max_accel correspodn to the min/max accelerations that can be |
| 69 | // achieved without breaking friction limits on one or both wheels. |
| 70 | // If max_accel < min_accel, that implies that v is too high for there to be |
| 71 | // any valid acceleration. FrictionLngAccelLimits(x, |
| 72 | // LateralVelocityCurvature(x), &min_accel, &max_accel) should result in |
| 73 | // min_accel == max_accel. |
| 74 | void FrictionLngAccelLimits(double x, double v, double *min_accel, |
| 75 | double *max_accel) const; |
| 76 | |
| 77 | enum class VoltageLimit { |
| 78 | kConservative, |
| 79 | kAggressive, |
| 80 | }; |
| 81 | |
| 82 | // Calculates the maximum voltage at which we *can* track the path. In some |
| 83 | // cases there will be two ranges of feasible velocities for traversing the |
| 84 | // path--in such a situation, from zero to velocity A we will be able to track |
| 85 | // the path, from velocity A to B we can't, from B to C we can and above C we |
| 86 | // can't. If limit_type = kConservative, we return A; if limit_type = |
| 87 | // kAggressive, we return C. We currently just use the kConservative limit |
| 88 | // because that way we can guarantee that all velocities between zero and A |
| 89 | // are allowable and don't have to handle a more complicated planning problem. |
| 90 | // constraint_voltages will be populated by the only wheel voltages that are |
| 91 | // valid at the returned limit. |
| 92 | double VoltageVelocityLimit( |
| 93 | double distance, VoltageLimit limit_type, |
| 94 | Eigen::Matrix<double, 2, 1> *constraint_voltages = nullptr) const; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 95 | |
Austin Schuh | 5b9e9c2 | 2019-01-07 15:44:06 -0800 | [diff] [blame] | 96 | // Limits the velocity in the specified segment to the max velocity. |
| 97 | void LimitVelocity(double starting_distance, double ending_distance, |
| 98 | double max_velocity); |
| 99 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 100 | // Runs the lateral acceleration (curvature) pass on the plan. |
| 101 | void LateralAccelPass(); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 102 | void VoltageFeasibilityPass(VoltageLimit limit_type); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 103 | |
| 104 | // Runs the forwards pass, setting the starting velocity to 0 m/s |
| 105 | void ForwardPass(); |
| 106 | |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 107 | // Returns the forwards/backwards acceleration for a distance along the spline |
| 108 | // taking into account the lateral acceleration, longitudinal acceleration, |
| 109 | // and voltage limits. |
| 110 | double BestAcceleration(double x, double v, bool backwards); |
| 111 | double BackwardAcceleration(double x, double v) { |
| 112 | return BestAcceleration(x, v, true); |
| 113 | } |
| 114 | double ForwardAcceleration(double x, double v) { |
| 115 | return BestAcceleration(x, v, false); |
| 116 | } |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 117 | |
| 118 | // Runs the forwards pass, setting the ending velocity to 0 m/s |
| 119 | void BackwardPass(); |
| 120 | |
| 121 | // Runs all the planning passes. |
| 122 | void Plan() { |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 123 | VoltageFeasibilityPass(VoltageLimit::kConservative); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 124 | LateralAccelPass(); |
| 125 | ForwardPass(); |
| 126 | BackwardPass(); |
| 127 | } |
| 128 | |
| 129 | // Returns the feed forwards position, velocity, acceleration for an explicit |
| 130 | // distance. |
| 131 | ::Eigen::Matrix<double, 3, 1> FFAcceleration(double distance); |
| 132 | |
| 133 | // Returns the feed forwards voltage for an explicit distance. |
| 134 | ::Eigen::Matrix<double, 2, 1> FFVoltage(double distance); |
| 135 | |
Alex Perry | 4ae2fd7 | 2019-02-03 15:55:57 -0800 | [diff] [blame] | 136 | // Returns whether a state represents a state at the end of the spline. |
| 137 | bool is_at_end(::Eigen::Matrix<double, 2, 1> state) const { |
| 138 | return state(0) > length() - 1e-4; |
| 139 | } |
| 140 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 141 | // Returns the length of the path in meters. |
| 142 | double length() const { return spline_->length(); } |
| 143 | |
| 144 | // Returns a list of the distances. Mostly useful for plotting. |
| 145 | const ::std::vector<double> Distances() const; |
| 146 | // Returns the distance for an index in the plan. |
| 147 | double Distance(int index) const { |
| 148 | return static_cast<double>(index) * length() / |
| 149 | static_cast<double>(plan_.size() - 1); |
| 150 | } |
| 151 | |
| 152 | // Returns the plan. This is the pathwise velocity as a function of distance. |
| 153 | // To get the distance for an index, use the Distance(index) function provided |
| 154 | // with the index. |
| 155 | const ::std::vector<double> plan() const { return plan_; } |
| 156 | |
| 157 | // Returns the left, right to linear, angular transformation matrix. |
| 158 | const ::Eigen::Matrix<double, 2, 2> &Tlr_to_la() const { return Tlr_to_la_; } |
| 159 | // Returns the linear, angular to left, right transformation matrix. |
| 160 | const ::Eigen::Matrix<double, 2, 2> &Tla_to_lr() const { return Tla_to_lr_; } |
| 161 | |
| 162 | // Returns the goal state as a function of path distance, velocity. |
| 163 | const ::Eigen::Matrix<double, 5, 1> GoalState(double distance, |
| 164 | double velocity); |
| 165 | |
| 166 | // Returns the velocity drivetrain in use. |
| 167 | const StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>, |
| 168 | HybridKalman<2, 2, 2>> |
| 169 | &velocity_drivetrain() const { |
| 170 | return *velocity_drivetrain_; |
| 171 | } |
| 172 | |
| 173 | // Returns the continuous statespace A and B matricies for [x, y, theta, vl, |
| 174 | // vr] for the linearized system (around the provided state). |
| 175 | ::Eigen::Matrix<double, 5, 5> ALinearizedContinuous( |
| 176 | const ::Eigen::Matrix<double, 5, 1> &state) const; |
| 177 | ::Eigen::Matrix<double, 5, 2> BLinearizedContinuous() const; |
| 178 | |
| 179 | // Returns the discrete time A and B matricies for the provided state, |
| 180 | // assuming the provided timestep. |
| 181 | void AB(const ::Eigen::Matrix<double, 5, 1> &state, |
| 182 | ::std::chrono::nanoseconds dt, ::Eigen::Matrix<double, 5, 5> *A, |
| 183 | ::Eigen::Matrix<double, 5, 2> *B) const; |
| 184 | |
| 185 | // Returns the lqr controller for the current state, timestep, and Q and R |
| 186 | // gains. |
| 187 | // TODO(austin): This feels like it should live somewhere else, but I'm not |
| 188 | // sure where. So, throw it here... |
| 189 | ::Eigen::Matrix<double, 2, 5> KForState( |
| 190 | const ::Eigen::Matrix<double, 5, 1> &state, ::std::chrono::nanoseconds dt, |
| 191 | const ::Eigen::DiagonalMatrix<double, 5> &Q, |
| 192 | const ::Eigen::DiagonalMatrix<double, 2> &R) const; |
| 193 | |
Alex Perry | 4ae2fd7 | 2019-02-03 15:55:57 -0800 | [diff] [blame] | 194 | // Return the next position, velocity, acceleration based on the current |
| 195 | // state. Updates the passed in state for the next iteration. |
| 196 | ::Eigen::Matrix<double, 3, 1> GetNextXVA( |
| 197 | ::std::chrono::nanoseconds dt, ::Eigen::Matrix<double, 2, 1> *state); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 198 | ::std::vector<::Eigen::Matrix<double, 3, 1>> PlanXVA( |
| 199 | ::std::chrono::nanoseconds dt); |
| 200 | |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 201 | enum SegmentType : uint8_t { |
| 202 | VELOCITY_LIMITED, |
| 203 | CURVATURE_LIMITED, |
| 204 | ACCELERATION_LIMITED, |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 205 | DECELERATION_LIMITED, |
| 206 | VOLTAGE_LIMITED, |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | const ::std::vector<SegmentType> &plan_segment_type() const { |
| 210 | return plan_segment_type_; |
| 211 | } |
| 212 | |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 213 | // Returns K1 and K2. |
| 214 | // K2 * d^x/dt^2 + K1 (dx/dt)^2 = A * K2 * dx/dt + B * U |
| 215 | const ::Eigen::Matrix<double, 2, 1> K1(double current_ddtheta) const { |
| 216 | return (::Eigen::Matrix<double, 2, 1>() |
| 217 | << -robot_radius_l_ * current_ddtheta, |
| 218 | robot_radius_r_ * current_ddtheta).finished(); |
| 219 | } |
| 220 | |
| 221 | const ::Eigen::Matrix<double, 2, 1> K2(double current_dtheta) const { |
| 222 | return (::Eigen::Matrix<double, 2, 1>() |
| 223 | << 1.0 - robot_radius_l_ * current_dtheta, |
| 224 | 1.0 + robot_radius_r_ * current_dtheta) |
| 225 | .finished(); |
| 226 | } |
| 227 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 228 | private: |
| 229 | // Computes alpha for a distance. |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 230 | size_t DistanceToSegment(double distance) const { |
| 231 | return ::std::max( |
| 232 | static_cast<size_t>(0), |
| 233 | ::std::min(plan_segment_type_.size() - 1, |
| 234 | static_cast<size_t>(::std::floor(distance / length() * |
| 235 | (plan_.size() - 1))))); |
| 236 | } |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 237 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 238 | // Computes K3, K4, and K5 for the provided distance. |
| 239 | // K5 a + K3 v^2 + K4 v = U |
| 240 | void K345(const double x, ::Eigen::Matrix<double, 2, 1> *K3, |
| 241 | ::Eigen::Matrix<double, 2, 1> *K4, |
| 242 | ::Eigen::Matrix<double, 2, 1> *K5) { |
| 243 | const double current_ddtheta = spline_->DDTheta(x); |
| 244 | const double current_dtheta = spline_->DTheta(x); |
| 245 | // We've now got the equation: |
| 246 | // K2 * d^x/dt^2 + K1 (dx/dt)^2 = A * K2 * dx/dt + B * U |
| 247 | const ::Eigen::Matrix<double, 2, 1> my_K2 = K2(current_dtheta); |
| 248 | |
| 249 | const ::Eigen::Matrix<double, 2, 2> B_inverse = |
| 250 | velocity_drivetrain_->plant().coefficients().B_continuous.inverse(); |
| 251 | |
| 252 | // Now, rephrase it as K5 a + K3 v^2 + K4 v = U |
| 253 | *K3 = B_inverse * K1(current_ddtheta); |
| 254 | *K4 = -B_inverse * |
| 255 | velocity_drivetrain_->plant().coefficients().A_continuous * my_K2; |
| 256 | *K5 = B_inverse * my_K2; |
| 257 | } |
| 258 | |
| 259 | // The spline we are planning for. |
| 260 | const DistanceSpline *spline_; |
| 261 | // The drivetrain we are controlling. |
| 262 | ::std::unique_ptr< |
| 263 | StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>, |
| 264 | HybridKalman<2, 2, 2>>> |
| 265 | velocity_drivetrain_; |
| 266 | |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 267 | // Robot radiuses. |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 268 | const double robot_radius_l_; |
| 269 | const double robot_radius_r_; |
| 270 | // Acceleration limits. |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 271 | double longitudinal_acceleration_; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 272 | double lateral_acceleration_; |
| 273 | // Transformation matrix from left, right to linear, angular |
| 274 | const ::Eigen::Matrix<double, 2, 2> Tlr_to_la_; |
| 275 | // Transformation matrix from linear, angular to left, right |
| 276 | const ::Eigen::Matrix<double, 2, 2> Tla_to_lr_; |
| 277 | // Velocities in the plan (distance for each index is defined by distance()) |
| 278 | ::std::vector<double> plan_; |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 279 | ::std::vector<SegmentType> plan_segment_type_; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 280 | // Plan voltage limit. |
| 281 | double voltage_limit_ = 12.0; |
| 282 | }; |
| 283 | |
| 284 | // Returns the continuous time dynamics given the [x, y, theta, vl, vr] state |
| 285 | // and the [vl, vr] input voltage. |
| 286 | inline ::Eigen::Matrix<double, 5, 1> ContinuousDynamics( |
| 287 | const StateFeedbackHybridPlant<2, 2, 2> &velocity_drivetrain, |
| 288 | const ::Eigen::Matrix<double, 2, 2> &Tlr_to_la, |
| 289 | const ::Eigen::Matrix<double, 5, 1> X, |
| 290 | const ::Eigen::Matrix<double, 2, 1> U) { |
| 291 | const auto &velocity = X.block<2, 1>(3, 0); |
| 292 | const double theta = X(2); |
| 293 | ::Eigen::Matrix<double, 2, 1> la = Tlr_to_la * velocity; |
| 294 | return (::Eigen::Matrix<double, 5, 1>() << ::std::cos(theta) * la(0), |
| 295 | ::std::sin(theta) * la(0), la(1), |
| 296 | (velocity_drivetrain.coefficients().A_continuous * velocity + |
| 297 | velocity_drivetrain.coefficients().B_continuous * U)) |
| 298 | .finished(); |
| 299 | } |
| 300 | |
| 301 | } // namespace drivetrain |
| 302 | } // namespace control_loops |
| 303 | } // namespace frc971 |
| 304 | |
| 305 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_TRAJECTORY_H_ |