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, |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 34 | LocalizerInterface *localizer, |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame] | 35 | const ::std::string &name) |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 36 | : aos::controls::ControlLoop<::frc971::control_loops::DrivetrainQueue>( |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame] | 37 | event_loop, name), |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 38 | dt_config_(dt_config), |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 39 | localizer_(localizer), |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 40 | kf_(dt_config_.make_kf_drivetrain_loop()), |
| 41 | dt_openloop_(dt_config_, &kf_), |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 42 | dt_closedloop_(dt_config_, &kf_, localizer_), |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 43 | dt_spline_(dt_config_), |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 44 | down_estimator_(MakeDownEstimatorLoop()), |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 45 | left_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW), |
| 46 | right_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW), |
| 47 | left_high_requested_(dt_config_.default_high_gear), |
| 48 | right_high_requested_(dt_config_.default_high_gear) { |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 49 | ::aos::controls::HPolytope<0>::Init(); |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 50 | down_U_.setZero(); |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 53 | int DrivetrainLoop::ControllerIndexFromGears() { |
| 54 | if (MaybeHigh(left_gear_)) { |
| 55 | if (MaybeHigh(right_gear_)) { |
| 56 | return 3; |
| 57 | } else { |
| 58 | return 2; |
| 59 | } |
| 60 | } else { |
| 61 | if (MaybeHigh(right_gear_)) { |
| 62 | return 1; |
| 63 | } else { |
| 64 | return 0; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | Gear ComputeGear(double shifter_position, |
| 70 | const constants::ShifterHallEffect &shifter_config, |
| 71 | bool high_requested) { |
| 72 | if (shifter_position < shifter_config.clear_low) { |
| 73 | return Gear::LOW; |
| 74 | } else if (shifter_position > shifter_config.clear_high) { |
| 75 | return Gear::HIGH; |
| 76 | } else { |
| 77 | if (high_requested) { |
| 78 | return Gear::SHIFTING_UP; |
| 79 | } else { |
| 80 | return Gear::SHIFTING_DOWN; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 85 | void DrivetrainLoop::RunIteration( |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 86 | const ::frc971::control_loops::DrivetrainQueue::Goal *goal, |
| 87 | const ::frc971::control_loops::DrivetrainQueue::Position *position, |
| 88 | ::frc971::control_loops::DrivetrainQueue::Output *output, |
| 89 | ::frc971::control_loops::DrivetrainQueue::Status *status) { |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 90 | monotonic_clock::time_point monotonic_now = monotonic_clock::now(); |
| 91 | |
Austin Schuh | 5900d14 | 2016-04-03 21:35:12 -0700 | [diff] [blame] | 92 | if (!has_been_enabled_ && output) { |
| 93 | has_been_enabled_ = true; |
| 94 | down_estimator_.mutable_X_hat(1, 0) = 0.0; |
| 95 | } |
| 96 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 97 | // TODO(austin): Put gear detection logic here. |
| 98 | switch (dt_config_.shifter_type) { |
| 99 | case ShifterType::SIMPLE_SHIFTER: |
| 100 | // Force the right controller for simple shifters since we assume that |
| 101 | // gear switching is instantaneous. |
| 102 | if (left_high_requested_) { |
| 103 | left_gear_ = Gear::HIGH; |
| 104 | } else { |
| 105 | left_gear_ = Gear::LOW; |
| 106 | } |
| 107 | if (right_high_requested_) { |
| 108 | right_gear_ = Gear::HIGH; |
| 109 | } else { |
| 110 | right_gear_ = Gear::LOW; |
| 111 | } |
| 112 | break; |
| 113 | case ShifterType::HALL_EFFECT_SHIFTER: |
| 114 | left_gear_ = ComputeGear(position->left_shifter_position, |
| 115 | dt_config_.left_drive, left_high_requested_); |
| 116 | right_gear_ = ComputeGear(position->right_shifter_position, |
| 117 | dt_config_.right_drive, right_high_requested_); |
| 118 | break; |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 119 | case ShifterType::NO_SHIFTER: |
| 120 | break; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 121 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 122 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 123 | kf_.set_index(ControllerIndexFromGears()); |
Kyle Stachowicz | 2f3c20f | 2017-07-13 16:04:05 -0700 | [diff] [blame] | 124 | |
| 125 | // Set the gear-logging parts of the status |
| 126 | if (status) { |
| 127 | status->gear_logging.left_state = static_cast<uint32_t>(left_gear_); |
| 128 | status->gear_logging.right_state = static_cast<uint32_t>(right_gear_); |
| 129 | status->gear_logging.left_loop_high = MaybeHigh(left_gear_); |
| 130 | status->gear_logging.right_loop_high = MaybeHigh(right_gear_); |
| 131 | status->gear_logging.controller_index = kf_.index(); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 132 | } |
Kyle Stachowicz | 2f3c20f | 2017-07-13 16:04:05 -0700 | [diff] [blame] | 133 | |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 134 | const bool is_latest_imu_values = ::frc971::imu_values.FetchLatest(); |
| 135 | if (is_latest_imu_values) { |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 136 | const double rate = -::frc971::imu_values->gyro_y; |
| 137 | const double accel_squared = ::frc971::imu_values->accelerometer_x * |
| 138 | ::frc971::imu_values->accelerometer_x + |
| 139 | ::frc971::imu_values->accelerometer_y * |
| 140 | ::frc971::imu_values->accelerometer_y + |
| 141 | ::frc971::imu_values->accelerometer_z * |
| 142 | ::frc971::imu_values->accelerometer_z; |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame] | 143 | const double angle = ::std::atan2(::frc971::imu_values->accelerometer_x, |
| 144 | ::frc971::imu_values->accelerometer_z) + |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 145 | 0.008; |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 146 | |
| 147 | switch (dt_config_.imu_type) { |
| 148 | case IMUType::IMU_X: |
| 149 | last_accel_ = -::frc971::imu_values->accelerometer_x; |
| 150 | break; |
| 151 | case IMUType::IMU_Y: |
| 152 | last_accel_ = -::frc971::imu_values->accelerometer_y; |
| 153 | break; |
| 154 | } |
| 155 | |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 156 | if (accel_squared > 1.03 || accel_squared < 0.97) { |
| 157 | LOG(DEBUG, "New IMU value, rejecting reading\n"); |
| 158 | } else { |
| 159 | // -y is our gyro. |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame] | 160 | // z accel is down |
| 161 | // x accel is the front of the robot pointed down. |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 162 | Eigen::Matrix<double, 1, 1> Y; |
Austin Schuh | df79d11 | 2016-10-15 21:25:32 -0700 | [diff] [blame] | 163 | Y(0, 0) = angle; |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 164 | down_estimator_.Correct(Y); |
| 165 | } |
| 166 | |
| 167 | LOG(DEBUG, |
Lee Mracek | 28313b0 | 2019-02-07 19:23:36 -0500 | [diff] [blame] | 168 | "New IMU value, rate is %f, angle %f, fused %f, bias " |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 169 | "%f\n", |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 170 | rate, angle, down_estimator_.X_hat(0), down_estimator_.X_hat(1)); |
Austin Schuh | df79d11 | 2016-10-15 21:25:32 -0700 | [diff] [blame] | 171 | down_U_(0, 0) = rate; |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 172 | } |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 173 | down_estimator_.UpdateObserver(down_U_, ::aos::controls::kLoopFrequency); |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 174 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 175 | // TODO(austin): Signal the current gear to both loops. |
| 176 | |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 177 | switch (dt_config_.gyro_type) { |
| 178 | case GyroType::IMU_X_GYRO: |
| 179 | if (is_latest_imu_values) { |
| 180 | LOG_STRUCT(DEBUG, "using", *imu_values.get()); |
| 181 | last_gyro_rate_ = imu_values->gyro_x; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 182 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 183 | } |
| 184 | break; |
| 185 | case GyroType::IMU_Y_GYRO: |
| 186 | if (is_latest_imu_values) { |
| 187 | LOG_STRUCT(DEBUG, "using", *imu_values.get()); |
| 188 | last_gyro_rate_ = imu_values->gyro_y; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 189 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 190 | } |
| 191 | break; |
| 192 | case GyroType::IMU_Z_GYRO: |
| 193 | if (is_latest_imu_values) { |
| 194 | LOG_STRUCT(DEBUG, "using", *imu_values.get()); |
| 195 | last_gyro_rate_ = imu_values->gyro_z; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 196 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 197 | } |
| 198 | break; |
Austin Schuh | 90b43b4 | 2019-01-04 07:45:05 +1100 | [diff] [blame] | 199 | case GyroType::FLIPPED_IMU_Z_GYRO: |
| 200 | if (is_latest_imu_values) { |
| 201 | LOG_STRUCT(DEBUG, "using", *imu_values.get()); |
| 202 | last_gyro_rate_ = -imu_values->gyro_z; |
| 203 | last_gyro_time_ = monotonic_now; |
| 204 | } |
| 205 | break; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 206 | case GyroType::SPARTAN_GYRO: |
| 207 | if (gyro_reading.FetchLatest()) { |
| 208 | LOG_STRUCT(DEBUG, "using", *gyro_reading.get()); |
| 209 | last_gyro_rate_ = gyro_reading->velocity; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 210 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 211 | } |
| 212 | break; |
| 213 | case GyroType::FLIPPED_SPARTAN_GYRO: |
| 214 | if (gyro_reading.FetchLatest()) { |
| 215 | LOG_STRUCT(DEBUG, "using", *gyro_reading.get()); |
| 216 | last_gyro_rate_ = -gyro_reading->velocity; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 217 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 218 | } |
| 219 | break; |
| 220 | default: |
| 221 | LOG(FATAL, "invalid gyro configured"); |
| 222 | break; |
Austin Schuh | 9ab4d9d | 2016-02-16 23:55:44 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 225 | if (monotonic_now > last_gyro_time_ + chrono::milliseconds(20)) { |
| 226 | last_gyro_rate_ = 0.0; |
| 227 | } |
| 228 | |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 229 | { |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 230 | Eigen::Matrix<double, 4, 1> Y; |
| 231 | Y << position->left_encoder, position->right_encoder, last_gyro_rate_, |
| 232 | last_accel_; |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 233 | kf_.Correct(Y); |
James Kuszmaul | 09f564a | 2019-02-18 17:37:09 -0800 | [diff] [blame] | 234 | // TODO(james): Account for delayed_U as appropriate (should be |
| 235 | // last_last_*_voltage). |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 236 | localizer_->Update({last_left_voltage_, last_right_voltage_}, monotonic_now, |
| 237 | position->left_encoder, position->right_encoder, |
| 238 | last_gyro_rate_, last_accel_); |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 241 | dt_openloop_.SetPosition(position, left_gear_, right_gear_); |
| 242 | |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 243 | int controller_type = 0; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 244 | if (goal) { |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 245 | controller_type = goal->controller_type; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 246 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 247 | dt_closedloop_.SetGoal(*goal); |
| 248 | dt_openloop_.SetGoal(*goal); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 249 | dt_spline_.SetGoal(*goal); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 252 | dt_openloop_.Update(robot_state().voltage_battery); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 253 | |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 254 | dt_closedloop_.Update(output != NULL && controller_type == 1); |
| 255 | |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 256 | dt_spline_.Update(output != NULL && controller_type == 2, |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 257 | (Eigen::Matrix<double, 5, 1>() << localizer_->x(), |
| 258 | localizer_->y(), localizer_->theta(), |
| 259 | localizer_->left_velocity(), localizer_->right_velocity()) |
| 260 | .finished()); |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 261 | |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 262 | switch (controller_type) { |
| 263 | case 0: |
| 264 | dt_openloop_.SetOutput(output); |
| 265 | break; |
| 266 | case 1: |
| 267 | dt_closedloop_.SetOutput(output); |
| 268 | break; |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 269 | case 2: |
| 270 | dt_spline_.SetOutput(output); |
| 271 | break; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 274 | // The output should now contain the shift request. |
| 275 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 276 | // set the output status of the control loop state |
| 277 | if (status) { |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 278 | 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] | 279 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 280 | Eigen::Matrix<double, 2, 1> linear = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 281 | dt_config_.LeftRightToLinear(kf_.X_hat()); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 282 | Eigen::Matrix<double, 2, 1> angular = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 283 | dt_config_.LeftRightToAngular(kf_.X_hat()); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 284 | |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 285 | angular(0, 0) = localizer_->theta(); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 286 | |
| 287 | Eigen::Matrix<double, 4, 1> gyro_left_right = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 288 | dt_config_.AngularLinearToLeftRight(linear, angular); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 289 | |
| 290 | status->estimated_left_position = gyro_left_right(0, 0); |
| 291 | status->estimated_right_position = gyro_left_right(2, 0); |
| 292 | |
| 293 | status->estimated_left_velocity = gyro_left_right(1, 0); |
| 294 | status->estimated_right_velocity = gyro_left_right(3, 0); |
| 295 | status->output_was_capped = dt_closedloop_.output_was_capped(); |
| 296 | status->uncapped_left_voltage = kf_.U_uncapped(0, 0); |
| 297 | status->uncapped_right_voltage = kf_.U_uncapped(1, 0); |
| 298 | |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 299 | status->left_voltage_error = kf_.X_hat(4); |
| 300 | status->right_voltage_error = kf_.X_hat(5); |
| 301 | status->estimated_angular_velocity_error = kf_.X_hat(6); |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 302 | status->estimated_heading = localizer_->theta(); |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 303 | |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 304 | status->x = localizer_->x(); |
| 305 | status->y = localizer_->y(); |
| 306 | status->theta = localizer_->theta(); |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 307 | |
| 308 | status->ground_angle = down_estimator_.X_hat(0) + dt_config_.down_offset; |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 309 | |
| 310 | dt_openloop_.PopulateStatus(status); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 311 | dt_closedloop_.PopulateStatus(status); |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 312 | dt_spline_.PopulateStatus(status); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 313 | } |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 314 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 315 | double left_voltage = 0.0; |
| 316 | double right_voltage = 0.0; |
| 317 | if (output) { |
| 318 | left_voltage = output->left_voltage; |
| 319 | right_voltage = output->right_voltage; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 320 | left_high_requested_ = output->left_high; |
| 321 | right_high_requested_ = output->right_high; |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 324 | const double scalar = robot_state().voltage_battery / 12.0; |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 325 | |
| 326 | left_voltage *= scalar; |
| 327 | right_voltage *= scalar; |
| 328 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 329 | // To validate, look at the following: |
| 330 | |
| 331 | // Observed - dx/dt velocity for left, right. |
| 332 | |
| 333 | // Angular velocity error compared to the gyro |
| 334 | // Gyro heading vs left-right |
| 335 | // Voltage error. |
| 336 | |
| 337 | Eigen::Matrix<double, 2, 1> U; |
Austin Schuh | df79d11 | 2016-10-15 21:25:32 -0700 | [diff] [blame] | 338 | U(0, 0) = last_left_voltage_; |
| 339 | U(1, 0) = last_right_voltage_; |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 340 | last_left_voltage_ = left_voltage; |
| 341 | last_right_voltage_ = right_voltage; |
| 342 | |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 343 | last_state_ = kf_.X_hat(); |
| 344 | kf_.UpdateObserver(U, dt_config_.dt); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 347 | void DrivetrainLoop::Zero( |
| 348 | ::frc971::control_loops::DrivetrainQueue::Output *output) { |
| 349 | output->left_voltage = 0; |
| 350 | output->right_voltage = 0; |
| 351 | output->left_high = dt_config_.default_high_gear; |
| 352 | output->right_high = dt_config_.default_high_gear; |
| 353 | } |
| 354 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 355 | } // namespace drivetrain |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 356 | } // namespace control_loops |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 357 | } // namespace frc971 |