Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 1 | #include "frc971/control_loops/drivetrain/drivetrain.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <sched.h> |
| 5 | #include <cmath> |
| 6 | #include <memory> |
| 7 | #include "Eigen/Dense" |
| 8 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/logging/logging.h" |
| 10 | #include "aos/logging/queue_logging.h" |
| 11 | #include "aos/logging/matrix_logging.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 12 | |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 13 | #include "frc971/control_loops/drivetrain/down_estimator.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 14 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 15 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 16 | #include "frc971/control_loops/drivetrain/polydrivetrain.h" |
| 17 | #include "frc971/control_loops/drivetrain/ssdrivetrain.h" |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 18 | #include "frc971/control_loops/runge_kutta.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 19 | #include "frc971/queues/gyro.q.h" |
| 20 | #include "frc971/shifter_hall_effect.h" |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 21 | #include "frc971/wpilib/imu.q.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 22 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 23 | using frc971::sensors::gyro_reading; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 24 | using frc971::imu_values; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 25 | using ::aos::monotonic_clock; |
| 26 | namespace chrono = ::std::chrono; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 27 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 28 | namespace frc971 { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 29 | namespace control_loops { |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 30 | namespace drivetrain { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 31 | |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame^] | 32 | DrivetrainLoop::DrivetrainLoop(const DrivetrainConfig<double> &dt_config, |
| 33 | ::aos::EventLoop *event_loop, |
| 34 | const ::std::string &name) |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 35 | : aos::controls::ControlLoop<::frc971::control_loops::DrivetrainQueue>( |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame^] | 36 | event_loop, name), |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 37 | dt_config_(dt_config), |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 38 | kf_(dt_config_.make_kf_drivetrain_loop()), |
| 39 | dt_openloop_(dt_config_, &kf_), |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 40 | dt_closedloop_(dt_config_, &kf_, &integrated_kf_heading_), |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 41 | down_estimator_(MakeDownEstimatorLoop()), |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 42 | left_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW), |
| 43 | right_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW), |
| 44 | left_high_requested_(dt_config_.default_high_gear), |
| 45 | right_high_requested_(dt_config_.default_high_gear) { |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 46 | ::aos::controls::HPolytope<0>::Init(); |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 47 | down_U_.setZero(); |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 50 | int DrivetrainLoop::ControllerIndexFromGears() { |
| 51 | if (MaybeHigh(left_gear_)) { |
| 52 | if (MaybeHigh(right_gear_)) { |
| 53 | return 3; |
| 54 | } else { |
| 55 | return 2; |
| 56 | } |
| 57 | } else { |
| 58 | if (MaybeHigh(right_gear_)) { |
| 59 | return 1; |
| 60 | } else { |
| 61 | return 0; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | Gear ComputeGear(double shifter_position, |
| 67 | const constants::ShifterHallEffect &shifter_config, |
| 68 | bool high_requested) { |
| 69 | if (shifter_position < shifter_config.clear_low) { |
| 70 | return Gear::LOW; |
| 71 | } else if (shifter_position > shifter_config.clear_high) { |
| 72 | return Gear::HIGH; |
| 73 | } else { |
| 74 | if (high_requested) { |
| 75 | return Gear::SHIFTING_UP; |
| 76 | } else { |
| 77 | return Gear::SHIFTING_DOWN; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 82 | ::Eigen::Matrix<double, 3, 1> DrivetrainLoop::PredictState( |
| 83 | const ::Eigen::Matrix<double, 3, 1> &xytheta_state, |
| 84 | const ::Eigen::Matrix<double, 7, 1> &state, |
| 85 | const ::Eigen::Matrix<double, 7, 1> &previous_state) const { |
| 86 | const double dt = |
| 87 | ::std::chrono::duration_cast<::std::chrono::duration<double>>( |
| 88 | dt_config_.dt) |
| 89 | .count(); |
| 90 | |
| 91 | const double distance_traveled = |
| 92 | (state(0) + state(2)) / 2.0 - |
| 93 | (previous_state(0) + previous_state(2)) / 2.0; |
| 94 | |
| 95 | const double omega0 = |
| 96 | (previous_state(3) - previous_state(1)) / (dt_config_.robot_radius * 2.0); |
| 97 | const double omega1 = (state(3) - state(1)) / (dt_config_.robot_radius * 2.0); |
| 98 | const double alpha = (omega1 - omega0) / dt; |
| 99 | |
| 100 | const double velocity_start = (previous_state(3) + previous_state(1)) / 2.0; |
| 101 | const double velocity_end = (state(3) + state(1)) / 2.0; |
| 102 | |
| 103 | const double acceleration = (velocity_end - velocity_start) / dt; |
| 104 | const double velocity_offset = |
| 105 | distance_traveled / dt - 0.5 * acceleration * dt - velocity_start; |
| 106 | const double velocity0 = velocity_start + velocity_offset; |
| 107 | |
| 108 | // TODO(austin): Substep 10x here. This is super important! ? |
| 109 | return RungeKutta( |
| 110 | [&dt, &velocity0, &acceleration, &omega0, &alpha]( |
| 111 | double t, const ::Eigen::Matrix<double, 3, 1> &X) { |
| 112 | const double velocity1 = velocity0 + acceleration * t; |
| 113 | const double omega1 = omega0 + alpha * t; |
| 114 | const double theta = X(2); |
| 115 | |
| 116 | return (::Eigen::Matrix<double, 3, 1>() |
| 117 | << ::std::cos(theta) * velocity1, |
| 118 | ::std::sin(theta) * velocity1, omega1) |
| 119 | .finished(); |
| 120 | }, |
| 121 | xytheta_state, 0.0, |
| 122 | ::std::chrono::duration_cast<::std::chrono::duration<double>>( |
| 123 | dt_config_.dt) |
| 124 | .count()); |
| 125 | } |
| 126 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 127 | void DrivetrainLoop::RunIteration( |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 128 | const ::frc971::control_loops::DrivetrainQueue::Goal *goal, |
| 129 | const ::frc971::control_loops::DrivetrainQueue::Position *position, |
| 130 | ::frc971::control_loops::DrivetrainQueue::Output *output, |
| 131 | ::frc971::control_loops::DrivetrainQueue::Status *status) { |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 132 | monotonic_clock::time_point monotonic_now = monotonic_clock::now(); |
| 133 | |
Austin Schuh | 5900d14 | 2016-04-03 21:35:12 -0700 | [diff] [blame] | 134 | if (!has_been_enabled_ && output) { |
| 135 | has_been_enabled_ = true; |
| 136 | down_estimator_.mutable_X_hat(1, 0) = 0.0; |
| 137 | } |
| 138 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 139 | // TODO(austin): Put gear detection logic here. |
| 140 | switch (dt_config_.shifter_type) { |
| 141 | case ShifterType::SIMPLE_SHIFTER: |
| 142 | // Force the right controller for simple shifters since we assume that |
| 143 | // gear switching is instantaneous. |
| 144 | if (left_high_requested_) { |
| 145 | left_gear_ = Gear::HIGH; |
| 146 | } else { |
| 147 | left_gear_ = Gear::LOW; |
| 148 | } |
| 149 | if (right_high_requested_) { |
| 150 | right_gear_ = Gear::HIGH; |
| 151 | } else { |
| 152 | right_gear_ = Gear::LOW; |
| 153 | } |
| 154 | break; |
| 155 | case ShifterType::HALL_EFFECT_SHIFTER: |
| 156 | left_gear_ = ComputeGear(position->left_shifter_position, |
| 157 | dt_config_.left_drive, left_high_requested_); |
| 158 | right_gear_ = ComputeGear(position->right_shifter_position, |
| 159 | dt_config_.right_drive, right_high_requested_); |
| 160 | break; |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 161 | case ShifterType::NO_SHIFTER: |
| 162 | break; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 163 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 164 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 165 | kf_.set_index(ControllerIndexFromGears()); |
Kyle Stachowicz | 2f3c20f | 2017-07-13 16:04:05 -0700 | [diff] [blame] | 166 | |
| 167 | // Set the gear-logging parts of the status |
| 168 | if (status) { |
| 169 | status->gear_logging.left_state = static_cast<uint32_t>(left_gear_); |
| 170 | status->gear_logging.right_state = static_cast<uint32_t>(right_gear_); |
| 171 | status->gear_logging.left_loop_high = MaybeHigh(left_gear_); |
| 172 | status->gear_logging.right_loop_high = MaybeHigh(right_gear_); |
| 173 | status->gear_logging.controller_index = kf_.index(); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 174 | } |
Kyle Stachowicz | 2f3c20f | 2017-07-13 16:04:05 -0700 | [diff] [blame] | 175 | |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 176 | const bool is_latest_imu_values = ::frc971::imu_values.FetchLatest(); |
| 177 | if (is_latest_imu_values) { |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 178 | const double rate = -::frc971::imu_values->gyro_y; |
| 179 | const double accel_squared = ::frc971::imu_values->accelerometer_x * |
| 180 | ::frc971::imu_values->accelerometer_x + |
| 181 | ::frc971::imu_values->accelerometer_y * |
| 182 | ::frc971::imu_values->accelerometer_y + |
| 183 | ::frc971::imu_values->accelerometer_z * |
| 184 | ::frc971::imu_values->accelerometer_z; |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame] | 185 | const double angle = ::std::atan2(::frc971::imu_values->accelerometer_x, |
| 186 | ::frc971::imu_values->accelerometer_z) + |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 187 | 0.008; |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 188 | |
| 189 | switch (dt_config_.imu_type) { |
| 190 | case IMUType::IMU_X: |
| 191 | last_accel_ = -::frc971::imu_values->accelerometer_x; |
| 192 | break; |
| 193 | case IMUType::IMU_Y: |
| 194 | last_accel_ = -::frc971::imu_values->accelerometer_y; |
| 195 | break; |
| 196 | } |
| 197 | |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 198 | if (accel_squared > 1.03 || accel_squared < 0.97) { |
| 199 | LOG(DEBUG, "New IMU value, rejecting reading\n"); |
| 200 | } else { |
| 201 | // -y is our gyro. |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame] | 202 | // z accel is down |
| 203 | // x accel is the front of the robot pointed down. |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 204 | Eigen::Matrix<double, 1, 1> Y; |
Austin Schuh | df79d11 | 2016-10-15 21:25:32 -0700 | [diff] [blame] | 205 | Y(0, 0) = angle; |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 206 | down_estimator_.Correct(Y); |
| 207 | } |
| 208 | |
| 209 | LOG(DEBUG, |
| 210 | "New IMU value from ADIS16448, rate is %f, angle %f, fused %f, bias " |
| 211 | "%f\n", |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 212 | rate, angle, down_estimator_.X_hat(0), down_estimator_.X_hat(1)); |
Austin Schuh | df79d11 | 2016-10-15 21:25:32 -0700 | [diff] [blame] | 213 | down_U_(0, 0) = rate; |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 214 | } |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 215 | down_estimator_.UpdateObserver(down_U_, ::aos::controls::kLoopFrequency); |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 216 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 217 | // TODO(austin): Signal the current gear to both loops. |
| 218 | |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 219 | switch (dt_config_.gyro_type) { |
| 220 | case GyroType::IMU_X_GYRO: |
| 221 | if (is_latest_imu_values) { |
| 222 | LOG_STRUCT(DEBUG, "using", *imu_values.get()); |
| 223 | last_gyro_rate_ = imu_values->gyro_x; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 224 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 225 | } |
| 226 | break; |
| 227 | case GyroType::IMU_Y_GYRO: |
| 228 | if (is_latest_imu_values) { |
| 229 | LOG_STRUCT(DEBUG, "using", *imu_values.get()); |
| 230 | last_gyro_rate_ = imu_values->gyro_y; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 231 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 232 | } |
| 233 | break; |
| 234 | case GyroType::IMU_Z_GYRO: |
| 235 | if (is_latest_imu_values) { |
| 236 | LOG_STRUCT(DEBUG, "using", *imu_values.get()); |
| 237 | last_gyro_rate_ = imu_values->gyro_z; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 238 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 239 | } |
| 240 | break; |
Austin Schuh | 90b43b4 | 2019-01-04 07:45:05 +1100 | [diff] [blame] | 241 | case GyroType::FLIPPED_IMU_Z_GYRO: |
| 242 | if (is_latest_imu_values) { |
| 243 | LOG_STRUCT(DEBUG, "using", *imu_values.get()); |
| 244 | last_gyro_rate_ = -imu_values->gyro_z; |
| 245 | last_gyro_time_ = monotonic_now; |
| 246 | } |
| 247 | break; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 248 | case GyroType::SPARTAN_GYRO: |
| 249 | if (gyro_reading.FetchLatest()) { |
| 250 | LOG_STRUCT(DEBUG, "using", *gyro_reading.get()); |
| 251 | last_gyro_rate_ = gyro_reading->velocity; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 252 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 253 | } |
| 254 | break; |
| 255 | case GyroType::FLIPPED_SPARTAN_GYRO: |
| 256 | if (gyro_reading.FetchLatest()) { |
| 257 | LOG_STRUCT(DEBUG, "using", *gyro_reading.get()); |
| 258 | last_gyro_rate_ = -gyro_reading->velocity; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 259 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 260 | } |
| 261 | break; |
| 262 | default: |
| 263 | LOG(FATAL, "invalid gyro configured"); |
| 264 | break; |
Austin Schuh | 9ab4d9d | 2016-02-16 23:55:44 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 267 | if (monotonic_now > last_gyro_time_ + chrono::milliseconds(20)) { |
| 268 | last_gyro_rate_ = 0.0; |
| 269 | } |
| 270 | |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 271 | { |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 272 | Eigen::Matrix<double, 4, 1> Y; |
| 273 | Y << position->left_encoder, position->right_encoder, last_gyro_rate_, |
| 274 | last_accel_; |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 275 | kf_.Correct(Y); |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 276 | |
| 277 | // We are going to choose to integrate velocity to get position by assuming |
| 278 | // that velocity is a linear function of time. For drivetrains with large |
| 279 | // amounts of mass, we won't get large changes in acceleration over a 5 ms |
| 280 | // timestep. Do note, the only place that this matters is when we are |
| 281 | // talking about the curvature errors introduced by integration. The |
| 282 | // velocities are scaled such that the distance traveled is correct. |
| 283 | // |
| 284 | // We want to do this after the kalman filter runs so we take into account |
| 285 | // the encoder and gyro corrections. |
| 286 | // |
| 287 | // Start by computing the beginning and ending linear and angular |
| 288 | // velocities. |
| 289 | // To handle 0 velocity well, compute the offset required to be added to |
| 290 | // both velocities to make the robot travel the correct distance. |
| 291 | |
| 292 | xytheta_state_.block<3, 1>(0, 0) = PredictState( |
| 293 | xytheta_state_.block<3, 1>(0, 0), kf_.X_hat(), last_state_); |
| 294 | |
| 295 | // Use trapezoidal integration for the gyro heading since it's more |
| 296 | // accurate. |
| 297 | const double average_angular_velocity = |
| 298 | ((kf_.X_hat(3) - kf_.X_hat(1)) + (last_state_(3) - last_state_(1))) / |
| 299 | 2.0 / (dt_config_.robot_radius * 2.0); |
| 300 | |
Austin Schuh | bb735b7 | 2019-01-03 12:58:41 -0800 | [diff] [blame] | 301 | integrated_kf_heading_ += |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 302 | ::std::chrono::duration_cast<::std::chrono::duration<double>>( |
| 303 | dt_config_.dt) |
| 304 | .count() * |
| 305 | average_angular_velocity; |
| 306 | |
| 307 | // Copy over the gyro heading. |
| 308 | xytheta_state_(2) = integrated_kf_heading_; |
| 309 | // Copy over the velocities heading. |
| 310 | xytheta_state_(3) = kf_.X_hat(1); |
| 311 | xytheta_state_(4) = kf_.X_hat(3); |
| 312 | // Copy over the voltage errors. |
| 313 | xytheta_state_.block<2, 1>(5, 0) = kf_.X_hat().block<2, 1>(4, 0); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 314 | |
| 315 | // gyro_heading = (real_right - real_left) / width |
| 316 | // wheel_heading = (wheel_right - wheel_left) / width |
| 317 | // gyro_heading + offset = wheel_heading |
| 318 | // gyro_goal + offset = wheel_goal |
| 319 | // offset = wheel_heading - gyro_heading |
| 320 | |
| 321 | // gyro_goal + wheel_heading - gyro_heading = wheel_goal |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 324 | dt_openloop_.SetPosition(position, left_gear_, right_gear_); |
| 325 | |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 326 | int controller_type = 0; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 327 | if (goal) { |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 328 | controller_type = goal->controller_type; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 329 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 330 | dt_closedloop_.SetGoal(*goal); |
| 331 | dt_openloop_.SetGoal(*goal); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 334 | dt_openloop_.Update(robot_state().voltage_battery); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 335 | |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 336 | dt_closedloop_.Update(output != NULL && controller_type == 1); |
| 337 | |
| 338 | switch (controller_type) { |
| 339 | case 0: |
| 340 | dt_openloop_.SetOutput(output); |
| 341 | break; |
| 342 | case 1: |
| 343 | dt_closedloop_.SetOutput(output); |
| 344 | break; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 347 | // The output should now contain the shift request. |
| 348 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 349 | // set the output status of the control loop state |
| 350 | if (status) { |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 351 | status->robot_speed = (kf_.X_hat(1) + kf_.X_hat(3)) / 2.0; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 352 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 353 | Eigen::Matrix<double, 2, 1> linear = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 354 | dt_config_.LeftRightToLinear(kf_.X_hat()); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 355 | Eigen::Matrix<double, 2, 1> angular = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 356 | dt_config_.LeftRightToAngular(kf_.X_hat()); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 357 | |
| 358 | angular(0, 0) = integrated_kf_heading_; |
| 359 | |
| 360 | Eigen::Matrix<double, 4, 1> gyro_left_right = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 361 | dt_config_.AngularLinearToLeftRight(linear, angular); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 362 | |
| 363 | status->estimated_left_position = gyro_left_right(0, 0); |
| 364 | status->estimated_right_position = gyro_left_right(2, 0); |
| 365 | |
| 366 | status->estimated_left_velocity = gyro_left_right(1, 0); |
| 367 | status->estimated_right_velocity = gyro_left_right(3, 0); |
| 368 | status->output_was_capped = dt_closedloop_.output_was_capped(); |
| 369 | status->uncapped_left_voltage = kf_.U_uncapped(0, 0); |
| 370 | status->uncapped_right_voltage = kf_.U_uncapped(1, 0); |
| 371 | |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 372 | status->left_voltage_error = kf_.X_hat(4); |
| 373 | status->right_voltage_error = kf_.X_hat(5); |
| 374 | status->estimated_angular_velocity_error = kf_.X_hat(6); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 375 | status->estimated_heading = integrated_kf_heading_; |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 376 | |
| 377 | status->x = xytheta_state_(0); |
| 378 | status->y = xytheta_state_(1); |
| 379 | status->theta = xytheta_state_(2); |
| 380 | |
| 381 | status->ground_angle = down_estimator_.X_hat(0) + dt_config_.down_offset; |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 382 | |
| 383 | dt_openloop_.PopulateStatus(status); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 384 | dt_closedloop_.PopulateStatus(status); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 385 | } |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 386 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 387 | double left_voltage = 0.0; |
| 388 | double right_voltage = 0.0; |
| 389 | if (output) { |
| 390 | left_voltage = output->left_voltage; |
| 391 | right_voltage = output->right_voltage; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 392 | left_high_requested_ = output->left_high; |
| 393 | right_high_requested_ = output->right_high; |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 396 | const double scalar = robot_state().voltage_battery / 12.0; |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 397 | |
| 398 | left_voltage *= scalar; |
| 399 | right_voltage *= scalar; |
| 400 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 401 | // To validate, look at the following: |
| 402 | |
| 403 | // Observed - dx/dt velocity for left, right. |
| 404 | |
| 405 | // Angular velocity error compared to the gyro |
| 406 | // Gyro heading vs left-right |
| 407 | // Voltage error. |
| 408 | |
| 409 | Eigen::Matrix<double, 2, 1> U; |
Austin Schuh | df79d11 | 2016-10-15 21:25:32 -0700 | [diff] [blame] | 410 | U(0, 0) = last_left_voltage_; |
| 411 | U(1, 0) = last_right_voltage_; |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 412 | last_left_voltage_ = left_voltage; |
| 413 | last_right_voltage_ = right_voltage; |
| 414 | |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 415 | last_state_ = kf_.X_hat(); |
| 416 | kf_.UpdateObserver(U, dt_config_.dt); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 419 | void DrivetrainLoop::Zero( |
| 420 | ::frc971::control_loops::DrivetrainQueue::Output *output) { |
| 421 | output->left_voltage = 0; |
| 422 | output->right_voltage = 0; |
| 423 | output->left_high = dt_config_.default_high_gear; |
| 424 | output->right_high = dt_config_.default_high_gear; |
| 425 | } |
| 426 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 427 | } // namespace drivetrain |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 428 | } // namespace control_loops |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 429 | } // namespace frc971 |