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" |
| 6 | #include "aos/logging/matrix_logging.h" |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 7 | #include "frc971/control_loops/c2d.h" |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 8 | #include "frc971/control_loops/dlqr.h" |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 9 | #include "frc971/control_loops/drivetrain/distance_spline.h" |
| 10 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
| 11 | #include "frc971/control_loops/hybrid_state_feedback_loop.h" |
| 12 | #include "frc971/control_loops/state_feedback_loop.h" |
| 13 | |
| 14 | namespace frc971 { |
| 15 | namespace control_loops { |
| 16 | namespace drivetrain { |
| 17 | |
| 18 | Trajectory::Trajectory(const DistanceSpline *spline, |
| 19 | const DrivetrainConfig<double> &config, double vmax, |
| 20 | int num_distance) |
| 21 | : spline_(spline), |
| 22 | velocity_drivetrain_( |
| 23 | ::std::unique_ptr<StateFeedbackLoop<2, 2, 2, double, |
| 24 | StateFeedbackHybridPlant<2, 2, 2>, |
| 25 | HybridKalman<2, 2, 2>>>( |
| 26 | new StateFeedbackLoop<2, 2, 2, double, |
| 27 | StateFeedbackHybridPlant<2, 2, 2>, |
| 28 | HybridKalman<2, 2, 2>>( |
| 29 | config.make_hybrid_drivetrain_velocity_loop()))), |
| 30 | robot_radius_l_(config.robot_radius), |
| 31 | robot_radius_r_(config.robot_radius), |
| 32 | longitudal_acceleration_(3.0), |
| 33 | lateral_acceleration_(2.0), |
| 34 | Tlr_to_la_((::Eigen::Matrix<double, 2, 2>() << 0.5, 0.5, |
| 35 | -1.0 / (robot_radius_l_ + robot_radius_r_), |
| 36 | 1.0 / (robot_radius_l_ + robot_radius_r_)) |
| 37 | .finished()), |
| 38 | Tla_to_lr_(Tlr_to_la_.inverse()), |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 39 | plan_(num_distance == 0 |
| 40 | ? ::std::max(100, static_cast<int>(spline_->length() / 0.0025)) |
| 41 | : num_distance, |
| 42 | vmax), |
| 43 | plan_segment_type_(plan_.size() - 1, SegmentType::VELOCITY_LIMITED) {} |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 44 | |
| 45 | void Trajectory::LateralAccelPass() { |
| 46 | for (size_t i = 0; i < plan_.size(); ++i) { |
| 47 | const double distance = Distance(i); |
| 48 | plan_[i] = ::std::min(plan_[i], LateralVelocityCurvature(distance)); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // TODO(austin): Deduplicate this potentially with the backward accel function. |
| 53 | // Need to sort out how the max velocity limit is going to work since the |
| 54 | // velocity and acceleration need to match at all points. |
| 55 | // TODO(austin): Accel check the wheels instead of the center of mass. |
| 56 | double Trajectory::ForwardAcceleration(const double x, const double v) { |
| 57 | ::Eigen::Matrix<double, 2, 1> K3; |
| 58 | ::Eigen::Matrix<double, 2, 1> K4; |
| 59 | ::Eigen::Matrix<double, 2, 1> K5; |
| 60 | K345(x, &K3, &K4, &K5); |
| 61 | |
| 62 | const ::Eigen::Matrix<double, 2, 1> C = K3 * v * v + K4 * v; |
| 63 | // Now, solve for all a's and find the best one which meets our criteria. |
| 64 | double maxa = -::std::numeric_limits<double>::infinity(); |
| 65 | for (const double a : {(voltage_limit_ - C(0, 0)) / K5(0, 0), |
| 66 | (voltage_limit_ - C(1, 0)) / K5(1, 0), |
| 67 | (-voltage_limit_ - C(0, 0)) / K5(0, 0), |
| 68 | (-voltage_limit_ - C(1, 0)) / K5(1, 0)}) { |
| 69 | const ::Eigen::Matrix<double, 2, 1> U = K5 * a + K3 * v * v + K4 * v; |
| 70 | if ((U.array().abs() < voltage_limit_ + 1e-6).all()) { |
| 71 | maxa = ::std::max(maxa, a); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Then, assume an acceleration oval and stay inside it. |
| 76 | const double lateral_acceleration = v * v * spline_->DDXY(x).norm(); |
Austin Schuh | cc7e476 | 2019-01-07 17:03:14 -0800 | [diff] [blame] | 77 | const double ellipse_down_shift = longitudal_acceleration_ * 1.0; |
| 78 | const double ellipse_width_stretch = ::std::sqrt( |
| 79 | 1.0 / (1.0 - ::std::pow(ellipse_down_shift / (longitudal_acceleration_ + |
| 80 | ellipse_down_shift), |
| 81 | 2.0))); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 82 | const double squared = |
Austin Schuh | cc7e476 | 2019-01-07 17:03:14 -0800 | [diff] [blame] | 83 | 1.0 - ::std::pow(lateral_acceleration / lateral_acceleration_ / |
| 84 | ellipse_width_stretch, |
| 85 | 2.0); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 86 | // If we would end up with an imaginary number, cap us at 0 acceleration. |
| 87 | // TODO(austin): Investigate when this happens, why, and fix it. |
| 88 | if (squared < 0.0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame^] | 89 | AOS_LOG(ERROR, "Imaginary %f, maxa: %f, fa(%f, %f) -> 0.0\n", squared, maxa, |
| 90 | x, v); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 91 | return 0.0; |
| 92 | } |
| 93 | const double longitudal_acceleration = |
Austin Schuh | cc7e476 | 2019-01-07 17:03:14 -0800 | [diff] [blame] | 94 | ::std::sqrt(::std::abs(squared)) * |
| 95 | (longitudal_acceleration_ + ellipse_down_shift) - |
| 96 | ellipse_down_shift; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 97 | return ::std::min(longitudal_acceleration, maxa); |
| 98 | } |
| 99 | |
| 100 | void Trajectory::ForwardPass() { |
| 101 | plan_[0] = 0.0; |
| 102 | const double delta_distance = Distance(1) - Distance(0); |
| 103 | for (size_t i = 0; i < plan_.size() - 1; ++i) { |
| 104 | const double distance = Distance(i); |
| 105 | |
| 106 | // Integrate our acceleration forward one step. |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 107 | const double new_plan_velocity = IntegrateAccelForDistance( |
| 108 | [this](double x, double v) { return ForwardAcceleration(x, v); }, |
| 109 | plan_[i], distance, delta_distance); |
| 110 | |
| 111 | if (new_plan_velocity < plan_[i + 1]) { |
| 112 | plan_[i + 1] = new_plan_velocity; |
| 113 | plan_segment_type_[i] = SegmentType::ACCELERATION_LIMITED; |
| 114 | } |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | double Trajectory::BackwardAcceleration(double x, double v) { |
| 119 | ::Eigen::Matrix<double, 2, 1> K3; |
| 120 | ::Eigen::Matrix<double, 2, 1> K4; |
| 121 | ::Eigen::Matrix<double, 2, 1> K5; |
| 122 | K345(x, &K3, &K4, &K5); |
| 123 | |
| 124 | // Now, solve for all a's and find the best one which meets our criteria. |
| 125 | const ::Eigen::Matrix<double, 2, 1> C = K3 * v * v + K4 * v; |
| 126 | double mina = ::std::numeric_limits<double>::infinity(); |
| 127 | for (const double a : {(voltage_limit_ - C(0, 0)) / K5(0, 0), |
| 128 | (voltage_limit_ - C(1, 0)) / K5(1, 0), |
| 129 | (-voltage_limit_ - C(0, 0)) / K5(0, 0), |
| 130 | (-voltage_limit_ - C(1, 0)) / K5(1, 0)}) { |
| 131 | const ::Eigen::Matrix<double, 2, 1> U = K5 * a + K3 * v * v + K4 * v; |
| 132 | if ((U.array().abs() < voltage_limit_ + 1e-6).all()) { |
| 133 | mina = ::std::min(mina, a); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Then, assume an acceleration oval and stay inside it. |
| 138 | const double lateral_acceleration = v * v * spline_->DDXY(x).norm(); |
Austin Schuh | cc7e476 | 2019-01-07 17:03:14 -0800 | [diff] [blame] | 139 | const double ellipse_down_shift = longitudal_acceleration_ * 1.0; |
| 140 | const double ellipse_width_stretch = ::std::sqrt( |
| 141 | 1.0 / (1.0 - ::std::pow(ellipse_down_shift / (longitudal_acceleration_ + |
| 142 | ellipse_down_shift), |
| 143 | 2.0))); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 144 | const double squared = |
Austin Schuh | cc7e476 | 2019-01-07 17:03:14 -0800 | [diff] [blame] | 145 | 1.0 - ::std::pow(lateral_acceleration / lateral_acceleration_ / |
| 146 | ellipse_width_stretch, |
| 147 | 2.0); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 148 | // If we would end up with an imaginary number, cap us at 0 acceleration. |
| 149 | // TODO(austin): Investigate when this happens, why, and fix it. |
| 150 | if (squared < 0.0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame^] | 151 | AOS_LOG(ERROR, "Imaginary %f, mina: %f, fa(%f, %f) -> 0.0\n", squared, mina, |
| 152 | x, v); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 153 | return 0.0; |
| 154 | } |
| 155 | const double longitudal_acceleration = |
Austin Schuh | cc7e476 | 2019-01-07 17:03:14 -0800 | [diff] [blame] | 156 | -::std::sqrt(::std::abs(squared)) * |
| 157 | (longitudal_acceleration_ + ellipse_down_shift) + |
| 158 | ellipse_down_shift; |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 159 | return ::std::max(longitudal_acceleration, mina); |
| 160 | } |
| 161 | |
| 162 | void Trajectory::BackwardPass() { |
| 163 | const double delta_distance = Distance(0) - Distance(1); |
| 164 | plan_.back() = 0.0; |
| 165 | for (size_t i = plan_.size() - 1; i > 0; --i) { |
| 166 | const double distance = Distance(i); |
| 167 | |
| 168 | // Integrate our deceleration back one step. |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 169 | const double new_plan_velocity = IntegrateAccelForDistance( |
| 170 | [this](double x, double v) { return BackwardAcceleration(x, v); }, |
| 171 | plan_[i], distance, delta_distance); |
| 172 | |
| 173 | if (new_plan_velocity < plan_[i - 1]) { |
| 174 | plan_[i - 1] = new_plan_velocity; |
| 175 | plan_segment_type_[i - 1] = SegmentType::DECELERATION_LIMITED; |
| 176 | } |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | ::Eigen::Matrix<double, 3, 1> Trajectory::FFAcceleration(double distance) { |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 181 | if (distance < 0.0) { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 182 | // 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] | 183 | distance = 0.0; |
| 184 | } else if (distance > length()) { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 185 | // 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] | 186 | distance = length(); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 187 | } |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 188 | const size_t before_index = DistanceToSegment(distance); |
| 189 | const size_t after_index = before_index + 1; |
| 190 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 191 | const double before_distance = Distance(before_index); |
| 192 | const double after_distance = Distance(after_index); |
| 193 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 194 | // And then also make sure we aren't curvature limited. |
| 195 | const double vcurvature = LateralVelocityCurvature(distance); |
| 196 | |
| 197 | double acceleration; |
| 198 | double velocity; |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 199 | switch (plan_segment_type_[DistanceToSegment(distance)]) { |
| 200 | case SegmentType::VELOCITY_LIMITED: |
| 201 | acceleration = 0.0; |
| 202 | velocity = (plan_[before_index] + plan_[after_index]) / 2.0; |
| 203 | // TODO(austin): Accelerate or decelerate until we hit the limit in the |
| 204 | // time slice. Otherwise our acceleration will be lying for this slice. |
| 205 | // Do note, we've got small slices so the effect will be small. |
| 206 | break; |
| 207 | case SegmentType::CURVATURE_LIMITED: |
| 208 | velocity = vcurvature; |
| 209 | acceleration = 0.0; |
| 210 | break; |
| 211 | case SegmentType::ACCELERATION_LIMITED: |
| 212 | velocity = IntegrateAccelForDistance( |
| 213 | [this](double x, double v) { return ForwardAcceleration(x, v); }, |
| 214 | plan_[before_index], before_distance, distance - before_distance); |
| 215 | acceleration = ForwardAcceleration(distance, velocity); |
| 216 | break; |
| 217 | case SegmentType::DECELERATION_LIMITED: |
| 218 | velocity = IntegrateAccelForDistance( |
| 219 | [this](double x, double v) { return BackwardAcceleration(x, v); }, |
| 220 | plan_[after_index], after_distance, distance - after_distance); |
| 221 | acceleration = BackwardAcceleration(distance, velocity); |
| 222 | break; |
| 223 | default: |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame^] | 224 | AOS_LOG( |
| 225 | FATAL, "Unknown segment type %d\n", |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 226 | static_cast<int>(plan_segment_type_[DistanceToSegment(distance)])); |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | if (vcurvature < velocity) { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 231 | velocity = vcurvature; |
| 232 | acceleration = 0.0; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame^] | 233 | AOS_LOG(ERROR, "Curvature won\n"); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 234 | } |
| 235 | return (::Eigen::Matrix<double, 3, 1>() << distance, velocity, acceleration) |
| 236 | .finished(); |
| 237 | } |
| 238 | |
| 239 | ::Eigen::Matrix<double, 2, 1> Trajectory::FFVoltage(double distance) { |
| 240 | const Eigen::Matrix<double, 3, 1> xva = FFAcceleration(distance); |
| 241 | const double velocity = xva(1); |
| 242 | const double acceleration = xva(2); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 243 | |
Austin Schuh | e73a905 | 2019-01-07 12:16:17 -0800 | [diff] [blame] | 244 | ::Eigen::Matrix<double, 2, 1> K3; |
| 245 | ::Eigen::Matrix<double, 2, 1> K4; |
| 246 | ::Eigen::Matrix<double, 2, 1> K5; |
| 247 | K345(distance, &K3, &K4, &K5); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 248 | |
| 249 | return K5 * acceleration + K3 * velocity * velocity + K4 * velocity; |
| 250 | } |
| 251 | |
| 252 | const ::std::vector<double> Trajectory::Distances() const { |
| 253 | ::std::vector<double> d; |
| 254 | d.reserve(plan_.size()); |
| 255 | for (size_t i = 0; i < plan_.size(); ++i) { |
| 256 | d.push_back(Distance(i)); |
| 257 | } |
| 258 | return d; |
| 259 | } |
| 260 | |
| 261 | ::Eigen::Matrix<double, 5, 5> Trajectory::ALinearizedContinuous( |
| 262 | const ::Eigen::Matrix<double, 5, 1> &state) const { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 263 | const double sintheta = ::std::sin(state(2)); |
| 264 | const double costheta = ::std::cos(state(2)); |
| 265 | const ::Eigen::Matrix<double, 2, 1> linear_angular = |
| 266 | Tlr_to_la_ * state.block<2, 1>(3, 0); |
| 267 | |
| 268 | // When stopped, just roll with a min velocity. |
| 269 | double linear_velocity = 0.0; |
| 270 | constexpr double kMinVelocity = 0.1; |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 271 | if (::std::abs(linear_angular(0)) < kMinVelocity / 100.0) { |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 272 | linear_velocity = 0.1; |
| 273 | } else if (::std::abs(linear_angular(0)) > kMinVelocity) { |
| 274 | linear_velocity = linear_angular(0); |
| 275 | } else if (linear_angular(0) > 0) { |
| 276 | linear_velocity = kMinVelocity; |
| 277 | } else if (linear_angular(0) < 0) { |
| 278 | linear_velocity = -kMinVelocity; |
| 279 | } |
| 280 | |
| 281 | ::Eigen::Matrix<double, 5, 5> result = ::Eigen::Matrix<double, 5, 5>::Zero(); |
| 282 | result(0, 2) = -sintheta * linear_velocity; |
| 283 | result(0, 3) = 0.5 * costheta; |
| 284 | result(0, 4) = 0.5 * costheta; |
| 285 | |
| 286 | result(1, 2) = costheta * linear_velocity; |
| 287 | result(1, 3) = 0.5 * sintheta; |
| 288 | result(1, 4) = 0.5 * sintheta; |
| 289 | |
| 290 | result(2, 3) = Tlr_to_la_(1, 0); |
| 291 | result(2, 4) = Tlr_to_la_(1, 1); |
| 292 | |
| 293 | result.block<2, 2>(3, 3) = |
| 294 | velocity_drivetrain_->plant().coefficients().A_continuous; |
| 295 | return result; |
| 296 | } |
| 297 | |
| 298 | ::Eigen::Matrix<double, 5, 2> Trajectory::BLinearizedContinuous() const { |
| 299 | ::Eigen::Matrix<double, 5, 2> result = ::Eigen::Matrix<double, 5, 2>::Zero(); |
| 300 | result.block<2, 2>(3, 0) = |
| 301 | velocity_drivetrain_->plant().coefficients().B_continuous; |
| 302 | return result; |
| 303 | } |
| 304 | |
| 305 | void Trajectory::AB(const ::Eigen::Matrix<double, 5, 1> &state, |
| 306 | ::std::chrono::nanoseconds dt, |
| 307 | ::Eigen::Matrix<double, 5, 5> *A, |
| 308 | ::Eigen::Matrix<double, 5, 2> *B) const { |
| 309 | ::Eigen::Matrix<double, 5, 5> A_linearized_continuous = |
| 310 | ALinearizedContinuous(state); |
| 311 | ::Eigen::Matrix<double, 5, 2> B_linearized_continuous = |
| 312 | BLinearizedContinuous(); |
| 313 | |
| 314 | // Now, convert it to discrete. |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 315 | controls::C2D(A_linearized_continuous, B_linearized_continuous, dt, A, B); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | ::Eigen::Matrix<double, 2, 5> Trajectory::KForState( |
| 319 | const ::Eigen::Matrix<double, 5, 1> &state, ::std::chrono::nanoseconds dt, |
| 320 | const ::Eigen::DiagonalMatrix<double, 5> &Q, |
| 321 | const ::Eigen::DiagonalMatrix<double, 2> &R) const { |
| 322 | ::Eigen::Matrix<double, 5, 5> A; |
| 323 | ::Eigen::Matrix<double, 5, 2> B; |
| 324 | AB(state, dt, &A, &B); |
| 325 | |
| 326 | ::Eigen::Matrix<double, 5, 5> S = ::Eigen::Matrix<double, 5, 5>::Zero(); |
| 327 | ::Eigen::Matrix<double, 2, 5> K = ::Eigen::Matrix<double, 2, 5>::Zero(); |
| 328 | |
| 329 | int info = ::frc971::controls::dlqr<5, 2>(A, B, Q, R, &K, &S); |
| 330 | if (info == 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame^] | 331 | AOS_LOG_MATRIX(INFO, "K", K); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 332 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame^] | 333 | AOS_LOG(ERROR, "Failed to solve %d, controllability: %d\n", info, |
| 334 | controls::Controllability(A, B)); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 335 | // TODO(austin): Can we be more clever here? Use the last one? We should |
| 336 | // collect more info about when this breaks down from logs. |
| 337 | K = ::Eigen::Matrix<double, 2, 5>::Zero(); |
| 338 | } |
| 339 | ::Eigen::EigenSolver<::Eigen::Matrix<double, 5, 5>> eigensolver(A - B * K); |
| 340 | const auto eigenvalues = eigensolver.eigenvalues(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame^] | 341 | AOS_LOG(DEBUG, |
| 342 | "Eigenvalues: (%f + %fj), (%f + %fj), (%f + %fj), (%f + %fj), (%f + " |
| 343 | "%fj)\n", |
| 344 | eigenvalues(0).real(), eigenvalues(0).imag(), eigenvalues(1).real(), |
| 345 | eigenvalues(1).imag(), eigenvalues(2).real(), eigenvalues(2).imag(), |
| 346 | eigenvalues(3).real(), eigenvalues(3).imag(), eigenvalues(4).real(), |
| 347 | eigenvalues(4).imag()); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 348 | return K; |
| 349 | } |
| 350 | |
| 351 | const ::Eigen::Matrix<double, 5, 1> Trajectory::GoalState(double distance, |
| 352 | double velocity) { |
| 353 | ::Eigen::Matrix<double, 5, 1> result; |
| 354 | result.block<2, 1>(0, 0) = spline_->XY(distance); |
| 355 | result(2, 0) = spline_->Theta(distance); |
| 356 | |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 357 | result.block<2, 1>(3, 0) = |
| 358 | Tla_to_lr_ * (::Eigen::Matrix<double, 2, 1>() << velocity, |
| 359 | spline_->DThetaDt(distance, velocity)) |
| 360 | .finished(); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 361 | return result; |
| 362 | } |
| 363 | |
Alex Perry | 4ae2fd7 | 2019-02-03 15:55:57 -0800 | [diff] [blame] | 364 | ::Eigen::Matrix<double, 3, 1> Trajectory::GetNextXVA( |
| 365 | ::std::chrono::nanoseconds dt, ::Eigen::Matrix<double, 2, 1> *state) { |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 366 | double dt_float = ::aos::time::DurationInSeconds(dt); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 367 | |
Alex Perry | 4ae2fd7 | 2019-02-03 15:55:57 -0800 | [diff] [blame] | 368 | // TODO(austin): This feels like something that should be pulled out into |
| 369 | // a library for re-use. |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 370 | *state = RungeKutta( |
| 371 | [this](const ::Eigen::Matrix<double, 2, 1> x) { |
| 372 | ::Eigen::Matrix<double, 3, 1> xva = FFAcceleration(x(0)); |
| 373 | return (::Eigen::Matrix<double, 2, 1>() << x(1), xva(2)).finished(); |
| 374 | }, |
| 375 | *state, dt_float); |
Alex Perry | 4ae2fd7 | 2019-02-03 15:55:57 -0800 | [diff] [blame] | 376 | |
| 377 | ::Eigen::Matrix<double, 3, 1> result = FFAcceleration((*state)(0)); |
| 378 | (*state)(1) = result(1); |
| 379 | return result; |
| 380 | } |
| 381 | |
| 382 | ::std::vector<::Eigen::Matrix<double, 3, 1>> Trajectory::PlanXVA( |
| 383 | ::std::chrono::nanoseconds dt) { |
| 384 | ::Eigen::Matrix<double, 2, 1> state = ::Eigen::Matrix<double, 2, 1>::Zero(); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 385 | ::std::vector<::Eigen::Matrix<double, 3, 1>> result; |
| 386 | result.emplace_back(FFAcceleration(0)); |
| 387 | result.back()(1) = 0.0; |
| 388 | |
Alex Perry | 4ae2fd7 | 2019-02-03 15:55:57 -0800 | [diff] [blame] | 389 | while (!is_at_end(state)) { |
| 390 | result.emplace_back(GetNextXVA(dt, &state)); |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 391 | } |
| 392 | return result; |
| 393 | } |
| 394 | |
Austin Schuh | 5b9e9c2 | 2019-01-07 15:44:06 -0800 | [diff] [blame] | 395 | void Trajectory::LimitVelocity(double starting_distance, double ending_distance, |
| 396 | const double max_velocity) { |
| 397 | const double segment_length = ending_distance - starting_distance; |
| 398 | |
| 399 | const double min_length = length() / static_cast<double>(plan_.size() - 1); |
| 400 | if (starting_distance > ending_distance) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame^] | 401 | AOS_LOG(FATAL, "End before start: %f > %f\n", starting_distance, |
| 402 | ending_distance); |
Austin Schuh | 5b9e9c2 | 2019-01-07 15:44:06 -0800 | [diff] [blame] | 403 | } |
| 404 | starting_distance = ::std::min(length(), ::std::max(0.0, starting_distance)); |
| 405 | ending_distance = ::std::min(length(), ::std::max(0.0, ending_distance)); |
| 406 | if (segment_length < min_length) { |
| 407 | const size_t plan_index = static_cast<size_t>( |
| 408 | ::std::round((starting_distance + ending_distance) / 2.0 / min_length)); |
| 409 | if (max_velocity < plan_[plan_index]) { |
| 410 | plan_[plan_index] = max_velocity; |
| 411 | } |
| 412 | } else { |
| 413 | for (size_t i = DistanceToSegment(starting_distance) + 1; |
| 414 | i < DistanceToSegment(ending_distance) + 1; ++i) { |
| 415 | if (max_velocity < plan_[i]) { |
| 416 | plan_[i] = max_velocity; |
| 417 | if (i < DistanceToSegment(ending_distance)) { |
| 418 | plan_segment_type_[i] = SegmentType::VELOCITY_LIMITED; |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 425 | } // namespace drivetrain |
| 426 | } // namespace control_loops |
| 427 | } // namespace frc971 |