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