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