Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 1 | #include "frc971/control_loops/drivetrain/trajectory.h" |
| 2 | |
| 3 | #include <chrono> |
| 4 | |
James Kuszmaul | 5e8ce31 | 2021-03-27 14:59:17 -0700 | [diff] [blame] | 5 | #include "aos/util/math.h" |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 6 | #include "Eigen/Dense" |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 7 | #include "frc971/control_loops/c2d.h" |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 8 | #include "frc971/control_loops/dlqr.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" |
| 11 | #include "frc971/control_loops/hybrid_state_feedback_loop.h" |
| 12 | #include "frc971/control_loops/state_feedback_loop.h" |
| 13 | |
| 14 | namespace frc971 { |
| 15 | namespace control_loops { |
| 16 | namespace drivetrain { |
| 17 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 18 | namespace { |
| 19 | float DefaultConstraint(ConstraintType type) { |
| 20 | switch (type) { |
| 21 | case ConstraintType::LONGITUDINAL_ACCELERATION: |
| 22 | return 2.0; |
| 23 | case ConstraintType::LATERAL_ACCELERATION: |
| 24 | return 3.0; |
| 25 | case ConstraintType::VOLTAGE: |
| 26 | return 12.0; |
| 27 | case ConstraintType::VELOCITY: |
| 28 | case ConstraintType::CONSTRAINT_TYPE_UNDEFINED: |
| 29 | LOG(FATAL) << "No default constraint value for " |
| 30 | << EnumNameConstraintType(type); |
| 31 | } |
| 32 | LOG(FATAL) << "Invalid ConstraintType " << static_cast<int>(type); |
| 33 | } |
| 34 | } // namespace |
| 35 | |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 36 | FinishedTrajectory::FinishedTrajectory( |
| 37 | const DrivetrainConfig<double> &config, const fb::Trajectory *buffer, |
| 38 | std::shared_ptr< |
| 39 | StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>, |
| 40 | HybridKalman<2, 2, 2>>> |
| 41 | velocity_drivetrain) |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 42 | : BaseTrajectory(CHECK_NOTNULL(CHECK_NOTNULL(buffer->spline())->spline()) |
| 43 | ->constraints(), |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 44 | config, std::move(velocity_drivetrain)), |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 45 | buffer_(buffer), |
| 46 | spline_(*buffer_->spline()) {} |
| 47 | |
| 48 | const Eigen::Matrix<double, 2, 1> BaseTrajectory::K1( |
| 49 | double current_ddtheta) const { |
| 50 | return (Eigen::Matrix<double, 2, 1>() << -robot_radius_l_ * current_ddtheta, |
| 51 | robot_radius_r_ * current_ddtheta) |
| 52 | .finished(); |
| 53 | } |
| 54 | |
| 55 | const Eigen::Matrix<double, 2, 1> BaseTrajectory::K2( |
| 56 | double current_dtheta) const { |
| 57 | return (Eigen::Matrix<double, 2, 1>() |
| 58 | << 1.0 - robot_radius_l_ * current_dtheta, |
| 59 | 1.0 + robot_radius_r_ * current_dtheta) |
| 60 | .finished(); |
| 61 | } |
| 62 | |
| 63 | void BaseTrajectory::K345(const double x, Eigen::Matrix<double, 2, 1> *K3, |
| 64 | Eigen::Matrix<double, 2, 1> *K4, |
| 65 | Eigen::Matrix<double, 2, 1> *K5) const { |
| 66 | const double current_ddtheta = spline().DDTheta(x); |
| 67 | const double current_dtheta = spline().DTheta(x); |
| 68 | // We've now got the equation: |
| 69 | // K2 * d^x/dt^2 + K1 (dx/dt)^2 = A * K2 * dx/dt + B * U |
| 70 | const Eigen::Matrix<double, 2, 1> my_K2 = K2(current_dtheta); |
| 71 | |
| 72 | const Eigen::Matrix<double, 2, 2> B_inverse = |
| 73 | velocity_drivetrain_->plant().coefficients().B_continuous.inverse(); |
| 74 | |
| 75 | // Now, rephrase it as K5 a + K3 v^2 + K4 v = U |
| 76 | *K3 = B_inverse * K1(current_ddtheta); |
| 77 | *K4 = -B_inverse * velocity_drivetrain_->plant().coefficients().A_continuous * |
| 78 | my_K2; |
| 79 | *K5 = B_inverse * my_K2; |
| 80 | } |
| 81 | |
| 82 | BaseTrajectory::BaseTrajectory( |
| 83 | const flatbuffers::Vector<flatbuffers::Offset<Constraint>> *constraints, |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 84 | const DrivetrainConfig<double> &config, |
| 85 | std::shared_ptr< |
| 86 | StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>, |
| 87 | HybridKalman<2, 2, 2>>> |
| 88 | velocity_drivetrain) |
| 89 | : velocity_drivetrain_(std::move(velocity_drivetrain)), |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 90 | config_(config), |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 91 | robot_radius_l_(config.robot_radius), |
| 92 | robot_radius_r_(config.robot_radius), |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 93 | lateral_acceleration_( |
| 94 | ConstraintValue(constraints, ConstraintType::LATERAL_ACCELERATION)), |
| 95 | longitudinal_acceleration_(ConstraintValue( |
| 96 | constraints, ConstraintType::LONGITUDINAL_ACCELERATION)), |
| 97 | voltage_limit_(ConstraintValue(constraints, ConstraintType::VOLTAGE)) {} |
| 98 | |
| 99 | Trajectory::Trajectory(const SplineGoal &spline_goal, |
| 100 | const DrivetrainConfig<double> &config) |
| 101 | : Trajectory(DistanceSpline{spline_goal.spline()}, config, |
| 102 | spline_goal.spline()->constraints(), |
| 103 | spline_goal.spline_idx()) { |
| 104 | drive_spline_backwards_ = spline_goal.drive_spline_backwards(); |
| 105 | } |
| 106 | |
| 107 | Trajectory::Trajectory( |
| 108 | DistanceSpline &&input_spline, const DrivetrainConfig<double> &config, |
| 109 | const flatbuffers::Vector<flatbuffers::Offset<Constraint>> *constraints, |
| 110 | int spline_idx, double vmax, int num_distance) |
| 111 | : BaseTrajectory(constraints, config), |
| 112 | spline_idx_(spline_idx), |
| 113 | spline_(std::move(input_spline)), |
| 114 | config_(config), |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 115 | plan_(num_distance == 0 |
Austin Schuh | 890196c | 2021-03-31 20:18:45 -0700 | [diff] [blame] | 116 | ? std::max(10000, static_cast<int>(spline_.length() / 0.0025)) |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 117 | : num_distance, |
| 118 | vmax), |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 119 | plan_segment_type_(plan_.size(), |
| 120 | fb::SegmentConstraint::VELOCITY_LIMITED) { |
| 121 | if (constraints != nullptr) { |
| 122 | for (const Constraint *constraint : *constraints) { |
| 123 | if (constraint->constraint_type() == ConstraintType::VELOCITY) { |
| 124 | LimitVelocity(constraint->start_distance(), constraint->end_distance(), |
| 125 | constraint->value()); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 130 | |
| 131 | void Trajectory::LateralAccelPass() { |
| 132 | for (size_t i = 0; i < plan_.size(); ++i) { |
| 133 | const double distance = Distance(i); |
Austin Schuh | d749d93 | 2020-12-30 21:38:40 -0800 | [diff] [blame] | 134 | const double velocity_limit = LateralVelocityCurvature(distance); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 135 | if (velocity_limit < plan_[i]) { |
| 136 | plan_[i] = velocity_limit; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 137 | plan_segment_type_[i] = fb::SegmentConstraint::CURVATURE_LIMITED; |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 138 | } |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 142 | void Trajectory::VoltageFeasibilityPass(VoltageLimit limit_type) { |
| 143 | for (size_t i = 0; i < plan_.size(); ++i) { |
| 144 | const double distance = Distance(i); |
| 145 | const double velocity_limit = VoltageVelocityLimit(distance, limit_type); |
| 146 | if (velocity_limit < plan_[i]) { |
| 147 | plan_[i] = velocity_limit; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 148 | plan_segment_type_[i] = fb::SegmentConstraint::VOLTAGE_LIMITED; |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 153 | double BaseTrajectory::BestAcceleration(double x, double v, |
| 154 | bool backwards) const { |
| 155 | Eigen::Matrix<double, 2, 1> K3; |
| 156 | Eigen::Matrix<double, 2, 1> K4; |
| 157 | Eigen::Matrix<double, 2, 1> K5; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 158 | K345(x, &K3, &K4, &K5); |
| 159 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 160 | // Now, solve for all a's and find the best one which meets our criteria. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 161 | const Eigen::Matrix<double, 2, 1> C = K3 * v * v + K4 * v; |
| 162 | double min_voltage_accel = std::numeric_limits<double>::infinity(); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 163 | double max_voltage_accel = -min_voltage_accel; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 164 | for (const double a : {(max_voltage() - C(0, 0)) / K5(0, 0), |
| 165 | (max_voltage() - C(1, 0)) / K5(1, 0), |
| 166 | (-max_voltage() - C(0, 0)) / K5(0, 0), |
| 167 | (-max_voltage() - C(1, 0)) / K5(1, 0)}) { |
| 168 | const Eigen::Matrix<double, 2, 1> U = K5 * a + K3 * v * v + K4 * v; |
| 169 | if ((U.array().abs() < max_voltage() + 1e-6).all()) { |
| 170 | min_voltage_accel = std::min(a, min_voltage_accel); |
| 171 | max_voltage_accel = std::max(a, max_voltage_accel); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 172 | } |
| 173 | } |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 174 | double best_accel = backwards ? min_voltage_accel : max_voltage_accel; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 175 | |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 176 | double min_friction_accel, max_friction_accel; |
| 177 | FrictionLngAccelLimits(x, v, &min_friction_accel, &max_friction_accel); |
| 178 | if (backwards) { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 179 | best_accel = std::max(best_accel, min_friction_accel); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 180 | } else { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 181 | best_accel = std::min(best_accel, max_friction_accel); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 182 | } |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 183 | |
James Kuszmaul | 66b7804 | 2020-02-23 15:30:51 -0800 | [diff] [blame] | 184 | // Ideally, the max would never be less than the min, but due to the way that |
| 185 | // the runge kutta solver works, it sometimes ticks over the edge. |
| 186 | if (max_friction_accel < min_friction_accel) { |
| 187 | VLOG(1) << "At x " << x << " v " << v << " min fric acc " |
| 188 | << min_friction_accel << " max fric accel " << max_friction_accel; |
| 189 | } |
| 190 | if (best_accel < min_voltage_accel || best_accel > max_voltage_accel) { |
| 191 | LOG(WARNING) << "Viable friction limits and viable voltage limits do not " |
Austin Schuh | d749d93 | 2020-12-30 21:38:40 -0800 | [diff] [blame] | 192 | "overlap (x: " |
| 193 | << x << ", v: " << v << ", backwards: " << backwards |
James Kuszmaul | 66b7804 | 2020-02-23 15:30:51 -0800 | [diff] [blame] | 194 | << ") best_accel = " << best_accel << ", min voltage " |
| 195 | << min_voltage_accel << ", max voltage " << max_voltage_accel |
| 196 | << " min friction " << min_friction_accel << " max friction " |
| 197 | << max_friction_accel << "."; |
| 198 | |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 199 | // Don't actually do anything--this will just result in attempting to drive |
| 200 | // higher voltages thatn we have available. In practice, that'll probably |
| 201 | // work out fine. |
| 202 | } |
| 203 | |
| 204 | return best_accel; |
| 205 | } |
| 206 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 207 | double BaseTrajectory::LateralVelocityCurvature(double distance) const { |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 208 | // To calculate these constraints, we first note that: |
| 209 | // wheel accels = K2 * v_robot' + K1 * v_robot^2 |
| 210 | // All that this logic does is solve for v_robot, leaving v_robot' free, |
| 211 | // assuming that the wheels are at their limits. |
| 212 | // To do this, we: |
| 213 | // |
| 214 | // 1) Determine what the wheel accels will be at the limit--since we have |
| 215 | // two free variables (v_robot, v_robot'), both wheels will be at their |
| 216 | // limits--if in a sufficiently tight turn (such that the signs of the |
| 217 | // coefficients of K2 are different), then the wheels will be accelerating |
| 218 | // in opposite directions; otherwise, they accelerate in the same direction. |
| 219 | // The magnitude of these per-wheel accelerations is a function of velocity, |
| 220 | // so it must also be solved for. |
| 221 | // |
| 222 | // 2) Eliminate that v_robot' term (since we don't care |
| 223 | // about it) by multiplying be a "K2prime" term (where K2prime * K2 = 0) on |
| 224 | // both sides of the equation. |
| 225 | // |
| 226 | // 3) Solving the relatively tractable remaining equation, which is |
| 227 | // basically just grouping all the terms together in one spot and taking the |
| 228 | // 4th root of everything. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 229 | const double dtheta = spline().DTheta(distance); |
| 230 | const Eigen::Matrix<double, 1, 2> K2prime = |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 231 | K2(dtheta).transpose() * |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 232 | (Eigen::Matrix<double, 2, 2>() << 0, 1, -1, 0).finished(); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 233 | // Calculate whether the wheels are spinning in opposite directions. |
| 234 | const bool opposites = K2prime(0) * K2prime(1) < 0; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 235 | const Eigen::Matrix<double, 2, 1> K1calc = K1(spline().DDTheta(distance)); |
| 236 | const double lat_accel_squared = std::pow(dtheta / max_lateral_accel(), 2); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 237 | const double curvature_change_term = |
| 238 | (K2prime * K1calc).value() / |
| 239 | (K2prime * |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 240 | (Eigen::Matrix<double, 2, 1>() << 1.0, (opposites ? -1.0 : 1.0)) |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 241 | .finished() * |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 242 | max_longitudinal_accel()) |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 243 | .value(); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 244 | const double vel_inv = std::sqrt( |
| 245 | std::sqrt(std::pow(curvature_change_term, 2) + lat_accel_squared)); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 246 | if (vel_inv == 0.0) { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 247 | return std::numeric_limits<double>::infinity(); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 248 | } |
| 249 | return 1.0 / vel_inv; |
| 250 | } |
| 251 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 252 | void BaseTrajectory::FrictionLngAccelLimits(double x, double v, |
| 253 | double *min_accel, |
| 254 | double *max_accel) const { |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 255 | // First, calculate the max longitudinal acceleration that can be achieved |
| 256 | // by either wheel given the friction elliipse that we have. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 257 | const double lateral_acceleration = v * v * spline().DDXY(x).norm(); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 258 | const double max_wheel_lng_accel_squared = |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 259 | 1.0 - std::pow(lateral_acceleration / max_lateral_accel(), 2.0); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 260 | if (max_wheel_lng_accel_squared < 0.0) { |
James Kuszmaul | 66b7804 | 2020-02-23 15:30:51 -0800 | [diff] [blame] | 261 | VLOG(1) << "Something (probably Runge-Kutta) queried invalid velocity " << v |
| 262 | << " at distance " << x; |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 263 | // If we encounter this, it means that the Runge-Kutta has attempted to |
| 264 | // sample points a bit past the edge of the friction boundary. If so, we |
| 265 | // gradually ramp the min/max accels to be more and more incorrect (note |
| 266 | // how min_accel > max_accel if we reach this case) to avoid causing any |
| 267 | // numerical issues. |
| 268 | *min_accel = |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 269 | std::sqrt(-max_wheel_lng_accel_squared) * max_longitudinal_accel(); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 270 | *max_accel = -*min_accel; |
| 271 | return; |
| 272 | } |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 273 | *min_accel = -std::numeric_limits<double>::infinity(); |
| 274 | *max_accel = std::numeric_limits<double>::infinity(); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 275 | |
| 276 | // Calculate max/min accelerations by calculating what the robots overall |
| 277 | // longitudinal acceleration would be if each wheel were running at the max |
| 278 | // forwards/backwards longitudinal acceleration. |
| 279 | const double max_wheel_lng_accel = |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 280 | max_longitudinal_accel() * std::sqrt(max_wheel_lng_accel_squared); |
| 281 | const Eigen::Matrix<double, 2, 1> K1v2 = K1(spline().DDTheta(x)) * v * v; |
| 282 | const Eigen::Matrix<double, 2, 1> K2inv = |
| 283 | K2(spline().DTheta(x)).cwiseInverse(); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 284 | // Store the accelerations of the robot corresponding to each wheel being at |
| 285 | // the max/min acceleration. The first coefficient in each vector |
| 286 | // corresponds to the left wheel, the second to the right wheel. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 287 | const Eigen::Matrix<double, 2, 1> accels1 = |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 288 | K2inv.array() * (-K1v2.array() + max_wheel_lng_accel); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 289 | const Eigen::Matrix<double, 2, 1> accels2 = |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 290 | K2inv.array() * (-K1v2.array() - max_wheel_lng_accel); |
| 291 | |
| 292 | // If either term is non-finite, that suggests that a term of K2 is zero |
| 293 | // (which is physically possible when turning such that one wheel is |
| 294 | // stationary), so just ignore that side of the drivetrain. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 295 | if (std::isfinite(accels1(0))) { |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 296 | // The inner max/min in this case determines which of the two cases (+ or |
| 297 | // - acceleration on the left wheel) we care about--in a sufficiently |
| 298 | // tight turning radius, the left hweel may be accelerating backwards when |
| 299 | // the robot as a whole accelerates forwards. We then use that |
| 300 | // acceleration to bound the min/max accel. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 301 | *min_accel = std::max(*min_accel, std::min(accels1(0), accels2(0))); |
| 302 | *max_accel = std::min(*max_accel, std::max(accels1(0), accels2(0))); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 303 | } |
| 304 | // Same logic as previous if-statement, but for the right wheel. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 305 | if (std::isfinite(accels1(1))) { |
| 306 | *min_accel = std::max(*min_accel, std::min(accels1(1), accels2(1))); |
| 307 | *max_accel = std::min(*max_accel, std::max(accels1(1), accels2(1))); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | double Trajectory::VoltageVelocityLimit( |
| 312 | double distance, VoltageLimit limit_type, |
| 313 | Eigen::Matrix<double, 2, 1> *constraint_voltages) const { |
| 314 | // To sketch an outline of the math going on here, we start with the basic |
| 315 | // dynamics of the robot along the spline: |
| 316 | // K2 * v_robot' + K1 * v_robot^2 = A * K2 * v_robot + B * U |
| 317 | // We need to determine the maximum v_robot given constrained U and free |
| 318 | // v_robot'. |
| 319 | // Similarly to the friction constraints, we accomplish this by first |
| 320 | // multiplying by a K2prime term to eliminate the v_robot' term. |
| 321 | // As with the friction constraints, we also know that the limits will occur |
| 322 | // when both sides of the drivetrain are driven at their max magnitude |
| 323 | // voltages, although they may be driven at different signs. |
| 324 | // Once we determine whether the voltages match signs, we still have to |
| 325 | // consider both possible pairings (technically we could probably |
| 326 | // predetermine which pairing, e.g. +/- or -/+, we acre about, but we don't |
| 327 | // need to). |
| 328 | // |
| 329 | // For each pairing, we then get to solve a quadratic formula for the robot |
| 330 | // velocity at those voltages. This gives us up to 4 solutions, of which |
| 331 | // up to 3 will give us positive velocities; each solution velocity |
| 332 | // corresponds to a transition from feasibility to infeasibility, where a |
| 333 | // velocity of zero is always feasible, and there will always be 0, 1, or 3 |
| 334 | // positive solutions. Among the positive solutions, we take both the min |
| 335 | // and the max--the min will be the highest velocity such that all |
| 336 | // velocities between zero and that velocity are valid; the max will be |
| 337 | // the highest feasible velocity. Which we return depends on what the |
| 338 | // limit_type is. |
| 339 | // |
| 340 | // Sketching the actual math: |
| 341 | // K2 * v_robot' + K1 * v_robot^2 = A * K2 * v_robot +/- B * U_max |
| 342 | // K2prime * K1 * v_robot^2 = K2prime * (A * K2 * v_robot +/- B * U_max) |
| 343 | // a v_robot^2 + b v_robot +/- c = 0 |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 344 | const Eigen::Matrix<double, 2, 2> B = |
| 345 | velocity_drivetrain().plant().coefficients().B_continuous; |
| 346 | const double dtheta = spline().DTheta(distance); |
| 347 | const Eigen::Matrix<double, 2, 1> BinvK2 = B.inverse() * K2(dtheta); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 348 | // Because voltages can actually impact *both* wheels, in order to determine |
| 349 | // whether the voltages will have opposite signs, we need to use B^-1 * K2. |
| 350 | const bool opposite_voltages = BinvK2(0) * BinvK2(1) > 0.0; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 351 | const Eigen::Matrix<double, 1, 2> K2prime = |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 352 | K2(dtheta).transpose() * |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 353 | (Eigen::Matrix<double, 2, 2>() << 0, 1, -1, 0).finished(); |
| 354 | const double a = K2prime * K1(spline().DDTheta(distance)); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 355 | const double b = -K2prime * |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 356 | velocity_drivetrain().plant().coefficients().A_continuous * |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 357 | K2(dtheta); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 358 | const Eigen::Matrix<double, 1, 2> c_coeff = -K2prime * B; |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 359 | // Calculate the "positive" version of the voltage limits we will use. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 360 | const Eigen::Matrix<double, 2, 1> abs_volts = |
| 361 | max_voltage() * |
| 362 | (Eigen::Matrix<double, 2, 1>() << 1.0, (opposite_voltages ? -1.0 : 1.0)) |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 363 | .finished(); |
| 364 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 365 | double min_valid_vel = std::numeric_limits<double>::infinity(); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 366 | if (limit_type == VoltageLimit::kAggressive) { |
| 367 | min_valid_vel = 0.0; |
| 368 | } |
| 369 | // Iterate over both possibilites for +/- voltage, and solve the quadratic |
| 370 | // formula. For every positive solution, adjust the velocity limit |
| 371 | // appropriately. |
| 372 | for (const double sign : {1.0, -1.0}) { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 373 | const Eigen::Matrix<double, 2, 1> U = sign * abs_volts; |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 374 | const double prev_vel = min_valid_vel; |
| 375 | const double c = c_coeff * U; |
| 376 | const double determinant = b * b - 4 * a * c; |
| 377 | if (a == 0) { |
| 378 | // If a == 0, that implies we are on a constant curvature path, in which |
| 379 | // case we just have b * v + c = 0. |
| 380 | // Note that if -b * c > 0.0, then vel will be greater than zero and b |
| 381 | // will be non-zero. |
| 382 | if (-b * c > 0.0) { |
| 383 | const double vel = -c / b; |
| 384 | if (limit_type == VoltageLimit::kConservative) { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 385 | min_valid_vel = std::min(min_valid_vel, vel); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 386 | } else { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 387 | min_valid_vel = std::max(min_valid_vel, vel); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 388 | } |
| 389 | } else if (b == 0) { |
| 390 | // If a and b are zero, then we are travelling in a straight line and |
| 391 | // have no voltage-based velocity constraints. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 392 | min_valid_vel = std::numeric_limits<double>::infinity(); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 393 | } |
| 394 | } else if (determinant > 0) { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 395 | const double sqrt_determinant = std::sqrt(determinant); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 396 | const double high_vel = (-b + sqrt_determinant) / (2.0 * a); |
| 397 | const double low_vel = (-b - sqrt_determinant) / (2.0 * a); |
| 398 | if (low_vel > 0) { |
| 399 | if (limit_type == VoltageLimit::kConservative) { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 400 | min_valid_vel = std::min(min_valid_vel, low_vel); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 401 | } else { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 402 | min_valid_vel = std::max(min_valid_vel, low_vel); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | if (high_vel > 0) { |
| 406 | if (limit_type == VoltageLimit::kConservative) { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 407 | min_valid_vel = std::min(min_valid_vel, high_vel); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 408 | } else { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 409 | min_valid_vel = std::max(min_valid_vel, high_vel); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | } else if (determinant == 0 && -b * a > 0) { |
| 413 | const double vel = -b / (2.0 * a); |
| 414 | if (vel > 0.0) { |
| 415 | if (limit_type == VoltageLimit::kConservative) { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 416 | min_valid_vel = std::min(min_valid_vel, vel); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 417 | } else { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 418 | min_valid_vel = std::max(min_valid_vel, vel); |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | } |
| 422 | if (constraint_voltages != nullptr && prev_vel != min_valid_vel) { |
| 423 | *constraint_voltages = U; |
| 424 | } |
| 425 | } |
| 426 | return min_valid_vel; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | void Trajectory::ForwardPass() { |
| 430 | plan_[0] = 0.0; |
| 431 | const double delta_distance = Distance(1) - Distance(0); |
| 432 | for (size_t i = 0; i < plan_.size() - 1; ++i) { |
| 433 | const double distance = Distance(i); |
| 434 | |
| 435 | // Integrate our acceleration forward one step. |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 436 | const double new_plan_velocity = IntegrateAccelForDistance( |
| 437 | [this](double x, double v) { return ForwardAcceleration(x, v); }, |
| 438 | plan_[i], distance, delta_distance); |
| 439 | |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 440 | if (new_plan_velocity <= plan_[i + 1]) { |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 441 | plan_[i + 1] = new_plan_velocity; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 442 | plan_segment_type_[i] = fb::SegmentConstraint::ACCELERATION_LIMITED; |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 443 | } |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 447 | void Trajectory::BackwardPass() { |
| 448 | const double delta_distance = Distance(0) - Distance(1); |
| 449 | plan_.back() = 0.0; |
| 450 | for (size_t i = plan_.size() - 1; i > 0; --i) { |
| 451 | const double distance = Distance(i); |
| 452 | |
| 453 | // Integrate our deceleration back one step. |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 454 | const double new_plan_velocity = IntegrateAccelForDistance( |
| 455 | [this](double x, double v) { return BackwardAcceleration(x, v); }, |
| 456 | plan_[i], distance, delta_distance); |
| 457 | |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 458 | if (new_plan_velocity <= plan_[i - 1]) { |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 459 | plan_[i - 1] = new_plan_velocity; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 460 | plan_segment_type_[i - 1] = fb::SegmentConstraint::DECELERATION_LIMITED; |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 461 | } |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 465 | Eigen::Matrix<double, 3, 1> BaseTrajectory::FFAcceleration( |
| 466 | double distance) const { |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 467 | if (distance < 0.0) { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 468 | // Make sure we don't end up off the beginning of the curve. |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 469 | distance = 0.0; |
| 470 | } else if (distance > length()) { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 471 | // Make sure we don't end up off the end of the curve. |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 472 | distance = length(); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 473 | } |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 474 | const size_t before_index = DistanceToSegment(distance); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 475 | const size_t after_index = |
| 476 | std::min(before_index + 1, distance_plan_size() - 1); |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 477 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 478 | const double before_distance = Distance(before_index); |
| 479 | const double after_distance = Distance(after_index); |
| 480 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 481 | // And then also make sure we aren't curvature limited. |
| 482 | const double vcurvature = LateralVelocityCurvature(distance); |
| 483 | |
| 484 | double acceleration; |
| 485 | double velocity; |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 486 | // TODO(james): While technically correct for sufficiently small segment |
| 487 | // steps, this method of switching between limits has a tendency to produce |
| 488 | // sudden jumps in acceelrations, which is undesirable. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 489 | switch (plan_constraint(DistanceToSegment(distance))) { |
| 490 | case fb::SegmentConstraint::VELOCITY_LIMITED: |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 491 | acceleration = 0.0; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 492 | velocity = |
| 493 | (plan_velocity(before_index) + plan_velocity(after_index)) / 2.0; |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 494 | // TODO(austin): Accelerate or decelerate until we hit the limit in the |
| 495 | // time slice. Otherwise our acceleration will be lying for this slice. |
| 496 | // Do note, we've got small slices so the effect will be small. |
| 497 | break; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 498 | case fb::SegmentConstraint::CURVATURE_LIMITED: |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 499 | velocity = vcurvature; |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 500 | FrictionLngAccelLimits(distance, velocity, &acceleration, &acceleration); |
| 501 | break; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 502 | case fb::SegmentConstraint::VOLTAGE_LIMITED: |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 503 | // Normally, we expect that voltage limited plans will all get dominated |
| 504 | // by the acceleration/deceleration limits. This may not always be true; |
| 505 | // if we ever encounter this error, we just need to back out what the |
| 506 | // accelerations would be in this case. |
Austin Schuh | d749d93 | 2020-12-30 21:38:40 -0800 | [diff] [blame] | 507 | LOG(FATAL) << "Unexpectedly got VOLTAGE_LIMITED plan."; |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 508 | break; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 509 | case fb::SegmentConstraint::ACCELERATION_LIMITED: |
James Kuszmaul | ea314d9 | 2019-02-18 19:45:06 -0800 | [diff] [blame] | 510 | // TODO(james): The integration done here and in the DECELERATION_LIMITED |
| 511 | // can technically cause us to violate friction constraints. We currently |
| 512 | // don't do anything about it to avoid causing sudden jumps in voltage, |
| 513 | // but we probably *should* at some point. |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 514 | velocity = IntegrateAccelForDistance( |
| 515 | [this](double x, double v) { return ForwardAcceleration(x, v); }, |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 516 | plan_velocity(before_index), before_distance, |
| 517 | distance - before_distance); |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 518 | acceleration = ForwardAcceleration(distance, velocity); |
| 519 | break; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 520 | case fb::SegmentConstraint::DECELERATION_LIMITED: |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 521 | velocity = IntegrateAccelForDistance( |
| 522 | [this](double x, double v) { return BackwardAcceleration(x, v); }, |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 523 | plan_velocity(after_index), after_distance, |
| 524 | distance - after_distance); |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 525 | acceleration = BackwardAcceleration(distance, velocity); |
| 526 | break; |
| 527 | default: |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 528 | AOS_LOG(FATAL, "Unknown segment type %d\n", |
| 529 | static_cast<int>(plan_constraint(DistanceToSegment(distance)))); |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 530 | break; |
| 531 | } |
| 532 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 533 | return (Eigen::Matrix<double, 3, 1>() << distance, velocity, acceleration) |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 534 | .finished(); |
| 535 | } |
| 536 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 537 | size_t FinishedTrajectory::distance_plan_size() const { |
| 538 | return trajectory().has_distance_based_plan() |
| 539 | ? trajectory().distance_based_plan()->size() |
| 540 | : 0u; |
| 541 | } |
| 542 | |
| 543 | fb::SegmentConstraint FinishedTrajectory::plan_constraint(size_t index) const { |
| 544 | CHECK_LT(index, distance_plan_size()); |
| 545 | return trajectory().distance_based_plan()->Get(index)->segment_constraint(); |
| 546 | } |
| 547 | |
| 548 | float FinishedTrajectory::plan_velocity(size_t index) const { |
| 549 | CHECK_LT(index, distance_plan_size()); |
| 550 | return trajectory().distance_based_plan()->Get(index)->velocity(); |
| 551 | } |
| 552 | |
| 553 | Eigen::Matrix<double, 2, 1> BaseTrajectory::FFVoltage(double distance) const { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 554 | const Eigen::Matrix<double, 3, 1> xva = FFAcceleration(distance); |
| 555 | const double velocity = xva(1); |
| 556 | const double acceleration = xva(2); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 557 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 558 | Eigen::Matrix<double, 2, 1> K3; |
| 559 | Eigen::Matrix<double, 2, 1> K4; |
| 560 | Eigen::Matrix<double, 2, 1> K5; |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 561 | K345(distance, &K3, &K4, &K5); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 562 | |
| 563 | return K5 * acceleration + K3 * velocity * velocity + K4 * velocity; |
| 564 | } |
| 565 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 566 | const std::vector<double> Trajectory::Distances() const { |
| 567 | std::vector<double> d; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 568 | d.reserve(plan_.size()); |
| 569 | for (size_t i = 0; i < plan_.size(); ++i) { |
| 570 | d.push_back(Distance(i)); |
| 571 | } |
| 572 | return d; |
| 573 | } |
| 574 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 575 | Eigen::Matrix<double, 3, 1> BaseTrajectory::GetNextXVA( |
| 576 | std::chrono::nanoseconds dt, Eigen::Matrix<double, 2, 1> *state) const { |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 577 | double dt_float = ::aos::time::DurationInSeconds(dt); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 578 | |
James Kuszmaul | 4d3c264 | 2020-03-05 07:32:39 -0800 | [diff] [blame] | 579 | const double last_distance = (*state)(0); |
Alex Perry | 4ae2fd7 | 2019-02-03 15:55:57 -0800 | [diff] [blame] | 580 | // TODO(austin): This feels like something that should be pulled out into |
| 581 | // a library for re-use. |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 582 | *state = RungeKutta( |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 583 | [this](const Eigen::Matrix<double, 2, 1> x) { |
| 584 | Eigen::Matrix<double, 3, 1> xva = FFAcceleration(x(0)); |
| 585 | return (Eigen::Matrix<double, 2, 1>() << x(1), xva(2)).finished(); |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 586 | }, |
| 587 | *state, dt_float); |
James Kuszmaul | 4d3c264 | 2020-03-05 07:32:39 -0800 | [diff] [blame] | 588 | // Force the distance to move forwards, to guarantee that we actually finish |
| 589 | // the planning. |
| 590 | constexpr double kMinDistanceIncrease = 1e-7; |
| 591 | if ((*state)(0) < last_distance + kMinDistanceIncrease) { |
| 592 | (*state)(0) = last_distance + kMinDistanceIncrease; |
| 593 | } |
Alex Perry | 4ae2fd7 | 2019-02-03 15:55:57 -0800 | [diff] [blame] | 594 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 595 | Eigen::Matrix<double, 3, 1> result = FFAcceleration((*state)(0)); |
Alex Perry | 4ae2fd7 | 2019-02-03 15:55:57 -0800 | [diff] [blame] | 596 | (*state)(1) = result(1); |
| 597 | return result; |
| 598 | } |
| 599 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 600 | std::vector<Eigen::Matrix<double, 3, 1>> Trajectory::PlanXVA( |
| 601 | std::chrono::nanoseconds dt) { |
| 602 | Eigen::Matrix<double, 2, 1> state = Eigen::Matrix<double, 2, 1>::Zero(); |
| 603 | std::vector<Eigen::Matrix<double, 3, 1>> result; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 604 | result.emplace_back(FFAcceleration(0)); |
| 605 | result.back()(1) = 0.0; |
| 606 | |
Alex Perry | 4ae2fd7 | 2019-02-03 15:55:57 -0800 | [diff] [blame] | 607 | while (!is_at_end(state)) { |
James Kuszmaul | 4d3c264 | 2020-03-05 07:32:39 -0800 | [diff] [blame] | 608 | if (state_is_faulted(state)) { |
| 609 | LOG(WARNING) |
| 610 | << "Found invalid state in generating spline and aborting. This is " |
| 611 | "likely due to a spline with extremely high jerk/changes in " |
| 612 | "curvature with an insufficiently small step size."; |
| 613 | return {}; |
| 614 | } |
Alex Perry | 4ae2fd7 | 2019-02-03 15:55:57 -0800 | [diff] [blame] | 615 | result.emplace_back(GetNextXVA(dt, &state)); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 616 | } |
| 617 | return result; |
| 618 | } |
| 619 | |
Austin Schuh | 5b9e9c2 | 2019-01-07 15:44:06 -0800 | [diff] [blame] | 620 | void Trajectory::LimitVelocity(double starting_distance, double ending_distance, |
| 621 | const double max_velocity) { |
| 622 | const double segment_length = ending_distance - starting_distance; |
| 623 | |
| 624 | const double min_length = length() / static_cast<double>(plan_.size() - 1); |
| 625 | if (starting_distance > ending_distance) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 626 | AOS_LOG(FATAL, "End before start: %f > %f\n", starting_distance, |
| 627 | ending_distance); |
Austin Schuh | 5b9e9c2 | 2019-01-07 15:44:06 -0800 | [diff] [blame] | 628 | } |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 629 | starting_distance = std::min(length(), std::max(0.0, starting_distance)); |
| 630 | ending_distance = std::min(length(), std::max(0.0, ending_distance)); |
Austin Schuh | 5b9e9c2 | 2019-01-07 15:44:06 -0800 | [diff] [blame] | 631 | if (segment_length < min_length) { |
| 632 | const size_t plan_index = static_cast<size_t>( |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 633 | std::round((starting_distance + ending_distance) / 2.0 / min_length)); |
Austin Schuh | 5b9e9c2 | 2019-01-07 15:44:06 -0800 | [diff] [blame] | 634 | if (max_velocity < plan_[plan_index]) { |
| 635 | plan_[plan_index] = max_velocity; |
| 636 | } |
| 637 | } else { |
| 638 | for (size_t i = DistanceToSegment(starting_distance) + 1; |
| 639 | i < DistanceToSegment(ending_distance) + 1; ++i) { |
| 640 | if (max_velocity < plan_[i]) { |
| 641 | plan_[i] = max_velocity; |
| 642 | if (i < DistanceToSegment(ending_distance)) { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 643 | plan_segment_type_[i] = fb::SegmentConstraint::VELOCITY_LIMITED; |
Austin Schuh | 5b9e9c2 | 2019-01-07 15:44:06 -0800 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 650 | void Trajectory::PathRelativeContinuousSystem(double distance, |
| 651 | Eigen::Matrix<double, 5, 5> *A, |
| 652 | Eigen::Matrix<double, 5, 2> *B) { |
| 653 | const double nominal_velocity = FFAcceleration(distance)(1); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 654 | const double dtheta_dt = spline().DThetaDt(distance, nominal_velocity); |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 655 | // Calculate the "path-relative" coordinates, which are: |
| 656 | // [[distance along the path], |
| 657 | // [lateral position along path], |
| 658 | // [theta], |
| 659 | // [left wheel velocity], |
| 660 | // [right wheel velocity]] |
| 661 | Eigen::Matrix<double, 5, 1> nominal_X; |
| 662 | nominal_X << distance, 0.0, 0.0, |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 663 | nominal_velocity - dtheta_dt * robot_radius_l(), |
| 664 | nominal_velocity + dtheta_dt * robot_radius_r(); |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 665 | PathRelativeContinuousSystem(nominal_X, A, B); |
| 666 | } |
| 667 | |
| 668 | void Trajectory::PathRelativeContinuousSystem( |
| 669 | const Eigen::Matrix<double, 5, 1> &X, Eigen::Matrix<double, 5, 5> *A, |
| 670 | Eigen::Matrix<double, 5, 2> *B) { |
| 671 | A->setZero(); |
| 672 | B->setZero(); |
| 673 | const double theta = X(2); |
| 674 | const double ctheta = std::cos(theta); |
| 675 | const double stheta = std::sin(theta); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 676 | const double curvature = spline().DTheta(X(0)); |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 677 | const double longitudinal_velocity = (X(3) + X(4)) / 2.0; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 678 | const double diameter = robot_radius_l() + robot_radius_r(); |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 679 | // d_dpath / dt = (v_left + v_right) / 2.0 * cos(theta) |
| 680 | // (d_dpath / dt) / dv_left = cos(theta) / 2.0 |
| 681 | (*A)(0, 3) = ctheta / 2.0; |
| 682 | // (d_dpath / dt) / dv_right = cos(theta) / 2.0 |
| 683 | (*A)(0, 4) = ctheta / 2.0; |
| 684 | // (d_dpath / dt) / dtheta = -(v_left + v_right) / 2.0 * sin(theta) |
| 685 | (*A)(0, 2) = -longitudinal_velocity * stheta; |
| 686 | // d_dlat / dt = (v_left + v_right) / 2.0 * sin(theta) |
| 687 | // (d_dlat / dt) / dv_left = sin(theta) / 2.0 |
| 688 | (*A)(1, 3) = stheta / 2.0; |
| 689 | // (d_dlat / dt) / dv_right = sin(theta) / 2.0 |
| 690 | (*A)(1, 4) = stheta / 2.0; |
| 691 | // (d_dlat / dt) / dtheta = (v_left + v_right) / 2.0 * cos(theta) |
| 692 | (*A)(1, 2) = longitudinal_velocity * ctheta; |
| 693 | // dtheta / dt = (v_right - v_left) / diameter - curvature * (v_left + |
| 694 | // v_right) / 2.0 |
| 695 | // (dtheta / dt) / dv_left = -1.0 / diameter - curvature / 2.0 |
| 696 | (*A)(2, 3) = -1.0 / diameter - curvature / 2.0; |
| 697 | // (dtheta / dt) / dv_right = 1.0 / diameter - curvature / 2.0 |
| 698 | (*A)(2, 4) = 1.0 / diameter - curvature / 2.0; |
| 699 | // v_{left,right} / dt = the normal LTI system. |
| 700 | A->block<2, 2>(3, 3) = |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 701 | velocity_drivetrain().plant().coefficients().A_continuous; |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 702 | B->block<2, 2>(3, 0) = |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 703 | velocity_drivetrain().plant().coefficients().B_continuous; |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | double Trajectory::EstimateDistanceAlongPath( |
| 707 | double nominal_distance, const Eigen::Matrix<double, 5, 1> &state) { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 708 | const double nominal_theta = spline().Theta(nominal_distance); |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 709 | const Eigen::Matrix<double, 2, 1> xy_err = |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 710 | state.block<2, 1>(0, 0) - spline().XY(nominal_distance); |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 711 | return nominal_distance + xy_err.x() * std::cos(nominal_theta) + |
| 712 | xy_err.y() * std::sin(nominal_theta); |
| 713 | } |
| 714 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 715 | Eigen::Matrix<double, 5, 1> FinishedTrajectory::StateToPathRelativeState( |
James Kuszmaul | 5e8ce31 | 2021-03-27 14:59:17 -0700 | [diff] [blame] | 716 | double distance, const Eigen::Matrix<double, 5, 1> &state, |
| 717 | bool drive_backwards) const { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 718 | const double nominal_theta = spline().Theta(distance); |
| 719 | const Eigen::Matrix<double, 2, 1> nominal_xy = spline().XY(distance); |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 720 | const Eigen::Matrix<double, 2, 1> xy_err = |
| 721 | state.block<2, 1>(0, 0) - nominal_xy; |
| 722 | const double ctheta = std::cos(nominal_theta); |
| 723 | const double stheta = std::sin(nominal_theta); |
| 724 | Eigen::Matrix<double, 5, 1> path_state; |
| 725 | path_state(0) = distance + xy_err.x() * ctheta + xy_err.y() * stheta; |
| 726 | path_state(1) = -xy_err.x() * stheta + xy_err.y() * ctheta; |
James Kuszmaul | 5e8ce31 | 2021-03-27 14:59:17 -0700 | [diff] [blame] | 727 | path_state(2) = aos::math::NormalizeAngle(state(2) - nominal_theta + |
| 728 | (drive_backwards ? M_PI : 0.0)); |
| 729 | path_state(2) = aos::math::NormalizeAngle(state(2) - nominal_theta); |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 730 | path_state(3) = state(3); |
| 731 | path_state(4) = state(4); |
James Kuszmaul | 5e8ce31 | 2021-03-27 14:59:17 -0700 | [diff] [blame] | 732 | if (drive_backwards) { |
| 733 | std::swap(path_state(3), path_state(4)); |
| 734 | path_state(3) *= -1.0; |
| 735 | path_state(4) *= -1.0; |
| 736 | } |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 737 | return path_state; |
| 738 | } |
| 739 | |
| 740 | // Path-relative controller method: |
| 741 | // For the path relative controller, we use a non-standard version of LQR to |
| 742 | // perform the control. Essentially, we first transform the system into |
| 743 | // a set of path-relative coordinates (where the reference that we use is the |
| 744 | // desired path reference). This gives us a system that is linear and |
| 745 | // time-varying, i.e. the system is a set of A_k, B_k matrices for each |
| 746 | // timestep k. |
| 747 | // In order to control this, we use a discrete-time finite-horizon LQR, using |
| 748 | // the appropraite [AB]_k for the given timestep. Note that the finite-horizon |
| 749 | // LQR requires choosing a terminal cost (i.e., what the cost should be |
| 750 | // for if we have not precisely reached the goal at the end of the time-period). |
| 751 | // For this, I approximate the infinite-horizon LQR solution by extending the |
| 752 | // finite-horizon much longer (albeit with the extension just using the |
| 753 | // linearization for the infal point). |
| 754 | void Trajectory::CalculatePathGains() { |
| 755 | const std::vector<Eigen::Matrix<double, 3, 1>> xva_plan = PlanXVA(config_.dt); |
James Kuszmaul | c3eaa47 | 2021-03-03 19:43:45 -0800 | [diff] [blame] | 756 | if (xva_plan.empty()) { |
| 757 | LOG(ERROR) << "Plan is empty--unable to plan trajectory."; |
| 758 | return; |
| 759 | } |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 760 | plan_gains_.resize(xva_plan.size()); |
| 761 | |
| 762 | // Set up reasonable gain matrices. Current choices of gains are arbitrary |
| 763 | // and just setup to work well enough for the simulation tests. |
| 764 | // TODO(james): Tune this on a real robot. |
| 765 | // TODO(james): Pull these out into a config. |
| 766 | Eigen::Matrix<double, 5, 5> Q; |
| 767 | Q.setIdentity(); |
James Kuszmaul | 49c9320 | 2023-03-23 20:44:03 -0700 | [diff] [blame] | 768 | Q.diagonal() << 30.0, 30.0, 20.0, 15.0, 15.0; |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 769 | Q *= 2.0; |
| 770 | Q = (Q * Q).eval(); |
| 771 | |
| 772 | Eigen::Matrix<double, 2, 2> R; |
| 773 | R.setIdentity(); |
| 774 | R *= 5.0; |
| 775 | |
| 776 | Eigen::Matrix<double, 5, 5> P = Q; |
| 777 | |
| 778 | CHECK_LT(0u, xva_plan.size()); |
| 779 | const int max_index = static_cast<int>(xva_plan.size()) - 1; |
| 780 | for (int i = max_index; i >= 0; --i) { |
| 781 | const double distance = xva_plan[i](0); |
| 782 | Eigen::Matrix<double, 5, 5> A_continuous; |
| 783 | Eigen::Matrix<double, 5, 2> B_continuous; |
| 784 | PathRelativeContinuousSystem(distance, &A_continuous, &B_continuous); |
| 785 | Eigen::Matrix<double, 5, 5> A_discrete; |
| 786 | Eigen::Matrix<double, 5, 2> B_discrete; |
| 787 | controls::C2D(A_continuous, B_continuous, config_.dt, &A_discrete, |
| 788 | &B_discrete); |
| 789 | |
| 790 | if (i == max_index) { |
| 791 | // At the final timestep, approximate P by iterating a bunch of times. |
| 792 | // This is terminal cost mentioned in function-level comments. |
| 793 | // This does a very loose job of solving the DARE. Ideally, we would |
| 794 | // actually use a DARE solver directly, but based on some initial testing, |
| 795 | // this method is a bit more robust (or, at least, it is a bit more robust |
| 796 | // if we don't want to spend more time handling the potential error |
| 797 | // cases the DARE solver can encounter). |
| 798 | constexpr int kExtraIters = 100; |
| 799 | for (int jj = 0; jj < kExtraIters; ++jj) { |
| 800 | const Eigen::Matrix<double, 5, 5> AP = A_discrete.transpose() * P; |
| 801 | const Eigen::Matrix<double, 5, 2> APB = AP * B_discrete; |
| 802 | const Eigen::Matrix<double, 2, 2> RBPBinv = |
| 803 | (R + B_discrete.transpose() * P * B_discrete).inverse(); |
| 804 | P = AP * A_discrete - APB * RBPBinv * APB.transpose() + Q; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | const Eigen::Matrix<double, 5, 5> AP = A_discrete.transpose() * P; |
| 809 | const Eigen::Matrix<double, 5, 2> APB = AP * B_discrete; |
| 810 | const Eigen::Matrix<double, 2, 2> RBPBinv = |
| 811 | (R + B_discrete.transpose() * P * B_discrete).inverse(); |
| 812 | plan_gains_[i].first = distance; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 813 | const Eigen::Matrix<double, 2, 5> K = RBPBinv * APB.transpose(); |
| 814 | plan_gains_[i].second = K.cast<float>(); |
| 815 | P = AP * A_discrete - APB * K + Q; |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 819 | Eigen::Matrix<double, 2, 5> FinishedTrajectory::GainForDistance( |
| 820 | double distance) const { |
| 821 | const flatbuffers::Vector<flatbuffers::Offset<fb::GainPoint>> &gains = |
| 822 | *CHECK_NOTNULL(trajectory().gains()); |
| 823 | CHECK_LT(0u, gains.size()); |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 824 | size_t index = 0; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 825 | for (index = 0; index < gains.size() - 1; ++index) { |
| 826 | if (gains[index + 1]->distance() > distance) { |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 827 | break; |
| 828 | } |
| 829 | } |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 830 | // ColMajor is the default storage order, but call it out explicitly here. |
| 831 | return Eigen::Matrix<float, 2, 5, Eigen::ColMajor>{ |
| 832 | gains[index]->gains()->data()} |
| 833 | .cast<double>(); |
| 834 | } |
| 835 | |
| 836 | namespace { |
| 837 | flatbuffers::Offset<Constraint> MakeWholeLengthConstraint( |
| 838 | flatbuffers::FlatBufferBuilder *fbb, ConstraintType constraint_type, |
| 839 | float value) { |
| 840 | Constraint::Builder builder(*fbb); |
| 841 | builder.add_constraint_type(constraint_type); |
| 842 | builder.add_value(value); |
| 843 | return builder.Finish(); |
| 844 | } |
| 845 | } // namespace |
| 846 | |
| 847 | flatbuffers::Offset<fb::Trajectory> Trajectory::Serialize( |
| 848 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 849 | std::array<flatbuffers::Offset<Constraint>, 3> constraints_offsets = { |
| 850 | MakeWholeLengthConstraint(fbb, ConstraintType::LONGITUDINAL_ACCELERATION, |
| 851 | max_longitudinal_accel()), |
| 852 | MakeWholeLengthConstraint(fbb, ConstraintType::LATERAL_ACCELERATION, |
| 853 | max_lateral_accel()), |
| 854 | MakeWholeLengthConstraint(fbb, ConstraintType::VOLTAGE, max_voltage())}; |
| 855 | const auto constraints = fbb->CreateVector<Constraint>( |
| 856 | constraints_offsets.data(), constraints_offsets.size()); |
| 857 | const flatbuffers::Offset<fb::DistanceSpline> spline_offset = |
| 858 | spline().Serialize(fbb, constraints); |
| 859 | |
| 860 | std::vector<flatbuffers::Offset<fb::PlanPoint>> plan_points; |
| 861 | for (size_t ii = 0; ii < distance_plan_size(); ++ii) { |
| 862 | plan_points.push_back(fb::CreatePlanPoint( |
| 863 | *fbb, Distance(ii), plan_velocity(ii), plan_constraint(ii))); |
| 864 | } |
| 865 | |
| 866 | // TODO(james): What is an appropriate cap? |
| 867 | CHECK_LT(plan_gains_.size(), 5000u); |
| 868 | CHECK_LT(0u, plan_gains_.size()); |
| 869 | std::vector<flatbuffers::Offset<fb::GainPoint>> gain_points; |
| 870 | const size_t matrix_size = plan_gains_[0].second.size(); |
| 871 | for (size_t ii = 0; ii < plan_gains_.size(); ++ii) { |
| 872 | gain_points.push_back(fb::CreateGainPoint( |
| 873 | *fbb, plan_gains_[ii].first, |
| 874 | fbb->CreateVector(plan_gains_[ii].second.data(), matrix_size))); |
| 875 | } |
| 876 | |
| 877 | return fb::CreateTrajectory(*fbb, spline_idx_, fbb->CreateVector(plan_points), |
| 878 | fbb->CreateVector(gain_points), spline_offset, |
| 879 | drive_spline_backwards_); |
| 880 | } |
| 881 | |
| 882 | float BaseTrajectory::ConstraintValue( |
| 883 | const flatbuffers::Vector<flatbuffers::Offset<Constraint>> *constraints, |
| 884 | ConstraintType type) { |
| 885 | if (constraints != nullptr) { |
| 886 | for (const Constraint *constraint : *constraints) { |
| 887 | if (constraint->constraint_type() == type) { |
| 888 | return constraint->value(); |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | return DefaultConstraint(type); |
| 893 | } |
| 894 | |
| 895 | const Eigen::Matrix<double, 5, 1> BaseTrajectory::GoalState( |
| 896 | double distance, double velocity) const { |
| 897 | Eigen::Matrix<double, 5, 1> result; |
| 898 | result.block<2, 1>(0, 0) = spline().XY(distance); |
| 899 | result(2, 0) = spline().Theta(distance); |
| 900 | |
| 901 | result.block<2, 1>(3, 0) = |
| 902 | config_.Tla_to_lr() * (Eigen::Matrix<double, 2, 1>() << velocity, |
| 903 | spline().DThetaDt(distance, velocity)) |
| 904 | .finished(); |
| 905 | return result; |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 908 | } // namespace drivetrain |
| 909 | } // namespace control_loops |
| 910 | } // namespace frc971 |