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