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 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 3 | #include <sched.h> |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 4 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 5 | #include <cmath> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame^] | 6 | #include <cstdio> |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 7 | #include <memory> |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 8 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 9 | #include "Eigen/Dense" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 10 | #include "aos/logging/logging.h" |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/drivetrain/down_estimator.h" |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 12 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | #include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h" |
| 14 | #include "frc971/control_loops/drivetrain/drivetrain_output_generated.h" |
| 15 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
| 16 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 17 | #include "frc971/control_loops/drivetrain/polydrivetrain.h" |
| 18 | #include "frc971/control_loops/drivetrain/ssdrivetrain.h" |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 19 | #include "frc971/control_loops/runge_kutta.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | #include "frc971/queues/gyro_generated.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 21 | #include "frc971/shifter_hall_effect.h" |
Austin Schuh | ac17fba | 2020-03-28 15:55:33 -0700 | [diff] [blame] | 22 | #include "frc971/wpilib/imu_batch_generated.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 23 | |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 24 | using ::aos::monotonic_clock; |
| 25 | namespace chrono = ::std::chrono; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 26 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 27 | namespace frc971 { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 28 | namespace control_loops { |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 29 | namespace drivetrain { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 30 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 31 | DrivetrainFilters::DrivetrainFilters(const DrivetrainConfig<double> &dt_config, |
| 32 | ::aos::EventLoop *event_loop, |
| 33 | LocalizerInterface *localizer) |
| 34 | : dt_config_(dt_config), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 35 | localizer_control_fetcher_( |
| 36 | event_loop->MakeFetcher<LocalizerControl>("/drivetrain")), |
Austin Schuh | 73b6e3b | 2019-05-27 16:37:15 -0700 | [diff] [blame] | 37 | imu_values_fetcher_( |
Austin Schuh | ac17fba | 2020-03-28 15:55:33 -0700 | [diff] [blame] | 38 | event_loop->MakeFetcher<::frc971::IMUValuesBatch>("/drivetrain")), |
Austin Schuh | 1ea89bb | 2019-05-27 16:59:59 -0700 | [diff] [blame] | 39 | gyro_reading_fetcher_( |
| 40 | event_loop->MakeFetcher<::frc971::sensors::GyroReading>( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 41 | "/drivetrain")), |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 42 | down_estimator_(dt_config_), |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 43 | localizer_(localizer), |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 44 | kf_(dt_config_.make_kf_drivetrain_loop()), |
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 | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 49 | last_voltage_.setZero(); |
| 50 | last_last_voltage_.setZero(); |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 51 | frc971::controls::HPolytope<0>::Init(); |
James Kuszmaul | 30aca50 | 2020-01-19 15:05:33 -0800 | [diff] [blame] | 52 | event_loop->OnRun([this]() { |
| 53 | // On the first fetch, make sure that we are caught all the way up to the |
| 54 | // present. |
| 55 | imu_values_fetcher_.Fetch(); |
| 56 | }); |
James Kuszmaul | 2215d92 | 2020-02-11 17:17:26 -0800 | [diff] [blame] | 57 | if (dt_config.is_simulated) { |
| 58 | down_estimator_.assume_perfect_gravity(); |
| 59 | } |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 62 | flatbuffers::Offset<LocalizerState> DrivetrainFilters::PopulateLocalizerState( |
| 63 | flatbuffers::FlatBufferBuilder *fbb) { |
| 64 | return localizer_->PopulateStatus(fbb); |
| 65 | } |
| 66 | flatbuffers::Offset<ImuZeroerState> DrivetrainFilters::PopulateImuZeroerState( |
| 67 | flatbuffers::FlatBufferBuilder *fbb) { |
| 68 | return imu_zeroer_.PopulateStatus(fbb); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 71 | flatbuffers::Offset<DownEstimatorState> |
| 72 | DrivetrainFilters::PopulateDownEstimatorState( |
| 73 | flatbuffers::FlatBufferBuilder *fbb, |
| 74 | aos::monotonic_clock::time_point monotonic_now) { |
| 75 | return down_estimator_.PopulateStatus(fbb, monotonic_now); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 78 | void DrivetrainFilters::Reset(aos::monotonic_clock::time_point monotonic_now, |
| 79 | const drivetrain::Position *position) { |
| 80 | // If all the sensors got reset (e.g., due to wpilib_interface restarting), |
| 81 | // reset the localizer and down estimator to avoid weird jumps in the |
| 82 | // filters. |
| 83 | down_estimator_.Reset(); |
| 84 | // Just reset the localizer to the current state, except for the encoders. |
| 85 | LocalizerInterface::Ekf::State X_hat = localizer_->Xhat(); |
| 86 | X_hat(LocalizerInterface::StateIdx::kLeftEncoder) = position->left_encoder(); |
| 87 | X_hat(LocalizerInterface::StateIdx::kRightEncoder) = |
| 88 | position->right_encoder(); |
| 89 | localizer_->Reset(monotonic_now, X_hat); |
| 90 | } |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 91 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 92 | void DrivetrainFilters::Correct(aos::monotonic_clock::time_point monotonic_now, |
| 93 | const drivetrain::Position *position) { |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 94 | // TODO(austin): Put gear detection logic here. |
| 95 | switch (dt_config_.shifter_type) { |
| 96 | case ShifterType::SIMPLE_SHIFTER: |
| 97 | // Force the right controller for simple shifters since we assume that |
| 98 | // gear switching is instantaneous. |
| 99 | if (left_high_requested_) { |
| 100 | left_gear_ = Gear::HIGH; |
| 101 | } else { |
| 102 | left_gear_ = Gear::LOW; |
| 103 | } |
| 104 | if (right_high_requested_) { |
| 105 | right_gear_ = Gear::HIGH; |
| 106 | } else { |
| 107 | right_gear_ = Gear::LOW; |
| 108 | } |
| 109 | break; |
| 110 | case ShifterType::HALL_EFFECT_SHIFTER: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 111 | left_gear_ = ComputeGear(position->left_shifter_position(), |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 112 | dt_config_.left_drive, left_high_requested_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 113 | right_gear_ = ComputeGear(position->right_shifter_position(), |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 114 | dt_config_.right_drive, right_high_requested_); |
| 115 | break; |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 116 | case ShifterType::NO_SHIFTER: |
| 117 | break; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 118 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 119 | |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 120 | while (imu_values_fetcher_.FetchNext()) { |
Austin Schuh | ac17fba | 2020-03-28 15:55:33 -0700 | [diff] [blame] | 121 | CHECK(imu_values_fetcher_->has_readings()); |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 122 | last_gyro_time_ = monotonic_now; |
Austin Schuh | ac17fba | 2020-03-28 15:55:33 -0700 | [diff] [blame] | 123 | for (const IMUValues *value : *imu_values_fetcher_->readings()) { |
| 124 | imu_zeroer_.InsertMeasurement(*value); |
| 125 | if (!imu_zeroer_.Zeroed()) { |
| 126 | continue; |
| 127 | } |
| 128 | const aos::monotonic_clock::time_point reading_time( |
| 129 | std::chrono::nanoseconds(value->monotonic_timestamp_ns())); |
| 130 | if (last_imu_update_ == aos::monotonic_clock::min_time) { |
| 131 | last_imu_update_ = reading_time; |
| 132 | } |
| 133 | down_estimator_.Predict(imu_zeroer_.ZeroedGyro(), |
| 134 | imu_zeroer_.ZeroedAccel(), |
| 135 | reading_time - last_imu_update_); |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 136 | last_imu_update_ = reading_time; |
| 137 | } |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 138 | } |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 139 | |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 140 | bool got_imu_reading = false; |
| 141 | if (imu_values_fetcher_.get() != nullptr) { |
James Kuszmaul | b7f45bb | 2020-02-26 20:27:48 -0800 | [diff] [blame] | 142 | imu_zeroer_.ProcessMeasurements(); |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 143 | got_imu_reading = true; |
Austin Schuh | ac17fba | 2020-03-28 15:55:33 -0700 | [diff] [blame] | 144 | CHECK(imu_values_fetcher_->has_readings()); |
| 145 | const IMUValues *value = imu_values_fetcher_->readings()->Get( |
| 146 | imu_values_fetcher_->readings()->size() - 1); |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 147 | switch (dt_config_.imu_type) { |
| 148 | case IMUType::IMU_X: |
Austin Schuh | ac17fba | 2020-03-28 15:55:33 -0700 | [diff] [blame] | 149 | last_accel_ = -value->accelerometer_x(); |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 150 | break; |
Austin Schuh | e7f6ddf | 2019-03-22 20:29:49 -0700 | [diff] [blame] | 151 | case IMUType::IMU_FLIPPED_X: |
Austin Schuh | ac17fba | 2020-03-28 15:55:33 -0700 | [diff] [blame] | 152 | last_accel_ = value->accelerometer_x(); |
Austin Schuh | e7f6ddf | 2019-03-22 20:29:49 -0700 | [diff] [blame] | 153 | break; |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 154 | case IMUType::IMU_Y: |
Austin Schuh | ac17fba | 2020-03-28 15:55:33 -0700 | [diff] [blame] | 155 | last_accel_ = -value->accelerometer_y(); |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 156 | break; |
James Kuszmaul | 7f55f07 | 2020-03-01 10:21:26 -0800 | [diff] [blame] | 157 | case IMUType::IMU_Z: |
Austin Schuh | ac17fba | 2020-03-28 15:55:33 -0700 | [diff] [blame] | 158 | last_accel_ = value->accelerometer_z(); |
James Kuszmaul | 7f55f07 | 2020-03-01 10:21:26 -0800 | [diff] [blame] | 159 | break; |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 160 | } |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 161 | } |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 162 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 163 | // TODO(austin): Signal the current gear to both loops. |
| 164 | |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 165 | switch (dt_config_.gyro_type) { |
| 166 | case GyroType::IMU_X_GYRO: |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 167 | if (got_imu_reading) { |
| 168 | last_gyro_rate_ = imu_zeroer_.ZeroedGyro().x(); |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 169 | } |
| 170 | break; |
| 171 | case GyroType::IMU_Y_GYRO: |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 172 | if (got_imu_reading) { |
| 173 | last_gyro_rate_ = imu_zeroer_.ZeroedGyro().y(); |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 174 | } |
| 175 | break; |
| 176 | case GyroType::IMU_Z_GYRO: |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 177 | if (got_imu_reading) { |
| 178 | last_gyro_rate_ = imu_zeroer_.ZeroedGyro().z(); |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 179 | } |
| 180 | break; |
Austin Schuh | 90b43b4 | 2019-01-04 07:45:05 +1100 | [diff] [blame] | 181 | case GyroType::FLIPPED_IMU_Z_GYRO: |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 182 | if (got_imu_reading) { |
| 183 | last_gyro_rate_ = -imu_zeroer_.ZeroedGyro().z(); |
Austin Schuh | 90b43b4 | 2019-01-04 07:45:05 +1100 | [diff] [blame] | 184 | } |
| 185 | break; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 186 | case GyroType::SPARTAN_GYRO: |
Austin Schuh | 1ea89bb | 2019-05-27 16:59:59 -0700 | [diff] [blame] | 187 | if (gyro_reading_fetcher_.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 188 | last_gyro_rate_ = gyro_reading_fetcher_->velocity(); |
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::FLIPPED_SPARTAN_GYRO: |
Austin Schuh | 1ea89bb | 2019-05-27 16:59:59 -0700 | [diff] [blame] | 193 | if (gyro_reading_fetcher_.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 194 | last_gyro_rate_ = -gyro_reading_fetcher_->velocity(); |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 195 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 196 | } |
| 197 | break; |
| 198 | default: |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 199 | AOS_LOG(FATAL, "invalid gyro configured"); |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 200 | break; |
Austin Schuh | 9ab4d9d | 2016-02-16 23:55:44 -0800 | [diff] [blame] | 201 | } |
| 202 | |
James Kuszmaul | ddc6970 | 2021-03-24 21:55:03 -0700 | [diff] [blame] | 203 | ready_ = imu_zeroer_.Zeroed(); |
| 204 | |
| 205 | // TODO(james): How aggressively can we fault here? If we fault to |
| 206 | // aggressively, we might have issues during startup if wpilib_interface takes |
| 207 | // too long to start publishing IMU measurements. |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 208 | if (monotonic_now > last_gyro_time_ + chrono::milliseconds(20)) { |
| 209 | last_gyro_rate_ = 0.0; |
| 210 | } |
| 211 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 212 | localizer_->Update( |
| 213 | {last_last_voltage_(kLeftVoltage), last_last_voltage_(kRightVoltage)}, |
| 214 | monotonic_now, position->left_encoder(), position->right_encoder(), |
| 215 | down_estimator_.avg_recent_yaw_rates(), |
| 216 | down_estimator_.avg_recent_accel()); |
| 217 | |
| 218 | // If we get a new message setting the absolute position, then reset the |
| 219 | // localizer. |
| 220 | if (localizer_control_fetcher_.Fetch()) { |
| 221 | VLOG(1) << "localizer_control " |
| 222 | << aos::FlatbufferToJson(localizer_control_fetcher_.get()); |
| 223 | localizer_->ResetPosition( |
| 224 | monotonic_now, localizer_control_fetcher_->x(), |
| 225 | localizer_control_fetcher_->y(), localizer_control_fetcher_->theta(), |
| 226 | localizer_control_fetcher_->theta_uncertainty(), |
| 227 | !localizer_control_fetcher_->keep_current_theta()); |
| 228 | } |
| 229 | |
| 230 | kf_.set_index(ControllerIndexFromGears()); |
| 231 | |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 232 | { |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 233 | Eigen::Matrix<double, 4, 1> Y; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 234 | Y << position->left_encoder(), position->right_encoder(), last_gyro_rate_, |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 235 | last_accel_; |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 236 | kf_.Correct(Y); |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | Eigen::Matrix<double, 2, 1> DrivetrainFilters::VoltageError() const { |
| 241 | static_assert(kLeftError + 1 == kRightError); |
| 242 | Eigen::Matrix<double, 2, 2> error_K; |
| 243 | error_K << kf_.controller().K(kLeftVoltage, kLeftError), 0.0, 0.0, |
| 244 | kf_.controller().K(kRightVoltage, kRightError); |
| 245 | const Eigen::Matrix<double, 2, 1> voltage_error = |
| 246 | error_K * kf_.X_hat().block<2, 1>(kLeftError, 0); |
| 247 | return voltage_error; |
| 248 | } |
| 249 | |
| 250 | void DrivetrainFilters::UpdateObserver(Eigen::Matrix<double, 2, 1> U) { |
| 251 | last_last_voltage_ = last_voltage_; |
| 252 | |
| 253 | kf_.UpdateObserver(last_voltage_, dt_config_.dt); |
| 254 | |
| 255 | last_voltage_ = U; |
| 256 | } |
| 257 | |
| 258 | int DrivetrainFilters::ControllerIndexFromGears() const { |
| 259 | if (MaybeHigh(left_gear_)) { |
| 260 | if (MaybeHigh(right_gear_)) { |
| 261 | return 3; |
| 262 | } else { |
| 263 | return 2; |
| 264 | } |
| 265 | } else { |
| 266 | if (MaybeHigh(right_gear_)) { |
| 267 | return 1; |
| 268 | } else { |
| 269 | return 0; |
James Kuszmaul | 891f4f1 | 2020-10-31 17:13:23 -0700 | [diff] [blame] | 270 | } |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 271 | } |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 272 | } |
| 273 | flatbuffers::Offset<GearLogging> DrivetrainFilters::CreateGearLogging( |
| 274 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 275 | GearLogging::Builder gear_logging_builder(*fbb); |
| 276 | gear_logging_builder.add_left_state(static_cast<uint32_t>(left_gear_)); |
| 277 | gear_logging_builder.add_right_state(static_cast<uint32_t>(right_gear_)); |
| 278 | gear_logging_builder.add_left_loop_high(MaybeHigh(left_gear_)); |
| 279 | gear_logging_builder.add_right_loop_high(MaybeHigh(right_gear_)); |
| 280 | gear_logging_builder.add_controller_index(ControllerIndexFromGears()); |
| 281 | return gear_logging_builder.Finish(); |
| 282 | } |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 283 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 284 | Gear DrivetrainFilters::ComputeGear( |
| 285 | double shifter_position, const constants::ShifterHallEffect &shifter_config, |
| 286 | bool high_requested) const { |
| 287 | if (shifter_position < shifter_config.clear_low) { |
| 288 | return Gear::LOW; |
| 289 | } else if (shifter_position > shifter_config.clear_high) { |
| 290 | return Gear::HIGH; |
| 291 | } else { |
| 292 | if (high_requested) { |
| 293 | return Gear::SHIFTING_UP; |
| 294 | } else { |
| 295 | return Gear::SHIFTING_DOWN; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | DrivetrainLoop::DrivetrainLoop(const DrivetrainConfig<double> &dt_config, |
| 301 | ::aos::EventLoop *event_loop, |
| 302 | LocalizerInterface *localizer, |
| 303 | const ::std::string &name) |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 304 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
| 305 | name), |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 306 | dt_config_(dt_config), |
| 307 | filters_(dt_config, event_loop, localizer), |
| 308 | dt_openloop_(dt_config_, filters_.kf()), |
| 309 | dt_closedloop_(dt_config_, filters_.kf(), localizer), |
| 310 | dt_spline_(dt_config_), |
| 311 | dt_line_follow_(dt_config_, localizer->target_selector()) { |
| 312 | event_loop->SetRuntimeRealtimePriority(30); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 313 | for (size_t ii = 0; ii < trajectory_fetchers_.size(); ++ii) { |
| 314 | trajectory_fetchers_[ii].fetcher = |
| 315 | event_loop->MakeFetcher<fb::Trajectory>("/drivetrain"); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void DrivetrainLoop::UpdateTrajectoryFetchers() { |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 320 | if (dt_spline_.trajectory_count() >= trajectory_fetchers_.size()) { |
| 321 | aos::monotonic_clock::time_point min_time = aos::monotonic_clock::max_time; |
| 322 | size_t min_fetcher_index = 0; |
| 323 | size_t fetcher_index = 0; |
| 324 | // Find the oldest spline to forget. |
| 325 | for (auto &fetcher : trajectory_fetchers_) { |
| 326 | CHECK_NE(fetcher.fetcher.context().monotonic_event_time, |
| 327 | monotonic_clock::min_time); |
| 328 | if (fetcher.fetcher.context().monotonic_event_time < min_time && |
| 329 | !dt_spline_.IsCurrentTrajectory(fetcher.fetcher.get())) { |
| 330 | min_time = fetcher.fetcher.context().monotonic_event_time; |
| 331 | min_fetcher_index = fetcher_index; |
| 332 | } |
| 333 | ++fetcher_index; |
| 334 | } |
| 335 | |
| 336 | dt_spline_.DeleteTrajectory( |
| 337 | trajectory_fetchers_[min_fetcher_index].fetcher.get()); |
| 338 | trajectory_fetchers_[min_fetcher_index].in_use = false; |
| 339 | } |
| 340 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 341 | for (auto &fetcher : trajectory_fetchers_) { |
| 342 | const fb::Trajectory *trajectory = fetcher.fetcher.get(); |
| 343 | // If the current fetcher is already being used by the SplineDrivetrain, |
| 344 | // don't touch it. |
| 345 | // We have to check both in_use and HasTrajectory because if |
| 346 | // in_use is true and HasTrajectory() is false, that implies that the |
| 347 | // SplineDrivetrain has finished executing the trajectory and disposed of |
| 348 | // it; if in_use is false and HasTrajectory() is true, that implies that |
| 349 | // this fetcher is at the same point in the queue as another fetcher, and |
| 350 | // that the other fetcher is the one that we are using to keep the message |
| 351 | // pinned. |
| 352 | // TODO(james): Consider garbage-collecting splines once we run out of |
| 353 | // fetchers. |
| 354 | if (fetcher.in_use && dt_spline_.HasTrajectory(trajectory)) { |
| 355 | continue; |
| 356 | } |
| 357 | fetcher.in_use = false; |
| 358 | // Go through and find the next Trajectory that isn't already held by the |
| 359 | // SplineDrivetrain, and add it. |
| 360 | while (fetcher.fetcher.FetchNext()) { |
| 361 | trajectory = fetcher.fetcher.get(); |
| 362 | if (!dt_spline_.HasTrajectory(trajectory)) { |
| 363 | fetcher.in_use = true; |
| 364 | dt_spline_.AddTrajectory(trajectory); |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | } |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void DrivetrainLoop::RunIteration( |
| 372 | const drivetrain::Goal *goal, const drivetrain::Position *position, |
| 373 | aos::Sender<drivetrain::Output>::Builder *output, |
| 374 | aos::Sender<drivetrain::Status>::Builder *status) { |
| 375 | const monotonic_clock::time_point monotonic_now = |
| 376 | event_loop()->monotonic_now(); |
| 377 | |
| 378 | if (!has_been_enabled_ && output) { |
| 379 | has_been_enabled_ = true; |
| 380 | } |
| 381 | |
| 382 | if (WasReset()) { |
| 383 | filters_.Reset(monotonic_now, position); |
| 384 | } |
| 385 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 386 | UpdateTrajectoryFetchers(); |
| 387 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 388 | filters_.Correct(monotonic_now, position); |
| 389 | |
| 390 | // Set the gear-logging parts of the status |
| 391 | CHECK(status); |
| 392 | flatbuffers::Offset<GearLogging> gear_logging_offset = |
| 393 | filters_.CreateGearLogging(status->fbb()); |
| 394 | |
| 395 | dt_openloop_.SetPosition(position, filters_.left_gear(), |
| 396 | filters_.right_gear()); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 397 | |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 398 | ControllerType controller_type = ControllerType::POLYDRIVE; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 399 | if (goal) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 400 | controller_type = goal->controller_type(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 401 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 402 | dt_closedloop_.SetGoal(goal); |
| 403 | dt_openloop_.SetGoal(goal->wheel(), goal->throttle(), goal->quickturn(), |
| 404 | goal->highgear()); |
| 405 | dt_spline_.SetGoal(goal); |
| 406 | dt_line_follow_.SetGoal(monotonic_now, goal); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 409 | dt_openloop_.Update(robot_state().voltage_battery()); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 410 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 411 | dt_closedloop_.Update(output != nullptr && |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 412 | controller_type == ControllerType::MOTION_PROFILE); |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 413 | |
James Kuszmaul | e39cbcf | 2019-02-27 20:48:34 -0800 | [diff] [blame] | 414 | const Eigen::Matrix<double, 5, 1> trajectory_state = |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 415 | filters_.trajectory_state(); |
James Kuszmaul | e39cbcf | 2019-02-27 20:48:34 -0800 | [diff] [blame] | 416 | |
James Kuszmaul | 897ef1a | 2020-10-25 17:51:10 -0700 | [diff] [blame] | 417 | { |
| 418 | // TODO(james): The regular Kalman Filter's voltage error terms are |
| 419 | // currently unusable--either don't use voltage error at all for the spline |
| 420 | // following code, or use the EKF's voltage error estimates. |
| 421 | const Eigen::Matrix<double, 2, 1> voltage_error = |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 422 | 0 * filters_.VoltageError(); |
James Kuszmaul | 897ef1a | 2020-10-25 17:51:10 -0700 | [diff] [blame] | 423 | dt_spline_.Update( |
| 424 | output != nullptr && controller_type == ControllerType::SPLINE_FOLLOWER, |
| 425 | trajectory_state, voltage_error); |
| 426 | } |
James Kuszmaul | e39cbcf | 2019-02-27 20:48:34 -0800 | [diff] [blame] | 427 | |
James Kuszmaul | 38e7964 | 2019-03-09 15:48:27 -0800 | [diff] [blame] | 428 | dt_line_follow_.Update(monotonic_now, trajectory_state); |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 429 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 430 | OutputT output_struct; |
| 431 | |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 432 | switch (controller_type) { |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 433 | case ControllerType::POLYDRIVE: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 434 | dt_openloop_.SetOutput(output != nullptr ? &output_struct : nullptr); |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 435 | break; |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 436 | case ControllerType::MOTION_PROFILE: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 437 | dt_closedloop_.SetOutput(output != nullptr ? &output_struct : nullptr); |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 438 | break; |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 439 | case ControllerType::SPLINE_FOLLOWER: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 440 | dt_spline_.SetOutput(output != nullptr ? &output_struct : nullptr); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 441 | break; |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 442 | case ControllerType::LINE_FOLLOWER: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 443 | if (!dt_line_follow_.SetOutput(output != nullptr ? &output_struct |
| 444 | : nullptr)) { |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 445 | // If the line follow drivetrain was unable to execute (generally due to |
| 446 | // not having a target), execute the regular teleop drivetrain. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 447 | dt_openloop_.SetOutput(output != nullptr ? &output_struct : nullptr); |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 448 | } |
James Kuszmaul | e39cbcf | 2019-02-27 20:48:34 -0800 | [diff] [blame] | 449 | break; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 452 | // The output should now contain the shift request. |
| 453 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 454 | // set the output status of the control loop state |
| 455 | if (status) { |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 456 | Eigen::Matrix<double, 2, 1> linear = |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 457 | dt_config_.LeftRightToLinear(filters_.DrivetrainXHat()); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 458 | Eigen::Matrix<double, 2, 1> angular = |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 459 | dt_config_.LeftRightToAngular(filters_.DrivetrainXHat()); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 460 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 461 | angular(0, 0) = filters_.localizer_theta(); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 462 | |
| 463 | Eigen::Matrix<double, 4, 1> gyro_left_right = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 464 | dt_config_.AngularLinearToLeftRight(linear, angular); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 465 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 466 | const flatbuffers::Offset<CIMLogging> cim_logging_offset = |
James Kuszmaul | af5dfad | 2020-01-03 20:02:54 -0800 | [diff] [blame] | 467 | dt_openloop_.PopulateShiftingStatus(status->fbb()); |
| 468 | |
| 469 | const flatbuffers::Offset<PolyDriveLogging> poly_drive_logging_offset = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 470 | dt_openloop_.PopulateStatus(status->fbb()); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 471 | |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 472 | const flatbuffers::Offset<DownEstimatorState> down_estimator_state_offset = |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 473 | filters_.PopulateDownEstimatorState(status->fbb(), monotonic_now); |
James Kuszmaul | 2215d92 | 2020-02-11 17:17:26 -0800 | [diff] [blame] | 474 | |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 475 | const flatbuffers::Offset<LocalizerState> localizer_offset = |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 476 | filters_.PopulateLocalizerState(status->fbb()); |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 477 | |
James Kuszmaul | 2215d92 | 2020-02-11 17:17:26 -0800 | [diff] [blame] | 478 | const flatbuffers::Offset<ImuZeroerState> zeroer_offset = |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 479 | filters_.PopulateImuZeroerState(status->fbb()); |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 480 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 481 | flatbuffers::Offset<LineFollowLogging> line_follow_logging_offset = |
| 482 | dt_line_follow_.PopulateStatus(status); |
| 483 | flatbuffers::Offset<TrajectoryLogging> trajectory_logging_offset = |
| 484 | dt_spline_.MakeTrajectoryLogging(status); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 485 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 486 | Status::Builder builder = status->MakeBuilder<Status>(); |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 487 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 488 | dt_closedloop_.PopulateStatus(&builder); |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 489 | |
Austin Schuh | 95771d9 | 2021-01-23 14:42:25 -0800 | [diff] [blame] | 490 | builder.add_estimated_left_position(gyro_left_right(kLeftPosition)); |
| 491 | builder.add_estimated_right_position(gyro_left_right(kRightPosition)); |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 492 | |
Austin Schuh | 95771d9 | 2021-01-23 14:42:25 -0800 | [diff] [blame] | 493 | builder.add_estimated_left_velocity(gyro_left_right(kLeftVelocity)); |
| 494 | builder.add_estimated_right_velocity(gyro_left_right(kRightVelocity)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 495 | |
| 496 | if (dt_spline_.enable()) { |
| 497 | dt_spline_.PopulateStatus(&builder); |
| 498 | } else { |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 499 | builder.add_robot_speed((filters_.DrivetrainXHat(kLeftVelocity) + |
| 500 | filters_.DrivetrainXHat(kRightVelocity)) / |
| 501 | 2.0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 502 | builder.add_output_was_capped(dt_closedloop_.output_was_capped()); |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 503 | builder.add_uncapped_left_voltage( |
| 504 | filters_.DrivetrainUUncapped(kLeftVoltage)); |
| 505 | builder.add_uncapped_right_voltage( |
| 506 | filters_.DrivetrainUUncapped(kRightVoltage)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 509 | builder.add_left_voltage_error(filters_.DrivetrainXHat(kLeftError)); |
| 510 | builder.add_right_voltage_error(filters_.DrivetrainXHat(kRightError)); |
| 511 | builder.add_estimated_angular_velocity_error( |
| 512 | filters_.DrivetrainXHat(kAngularError)); |
| 513 | builder.add_estimated_heading(filters_.localizer_theta()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 514 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 515 | builder.add_x(filters_.x()); |
| 516 | builder.add_y(filters_.y()); |
| 517 | builder.add_theta(::aos::math::NormalizeAngle(filters_.localizer_theta())); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 518 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 519 | builder.add_cim_logging(cim_logging_offset); |
James Kuszmaul | af5dfad | 2020-01-03 20:02:54 -0800 | [diff] [blame] | 520 | builder.add_poly_drive_logging(poly_drive_logging_offset); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 521 | builder.add_gear_logging(gear_logging_offset); |
| 522 | builder.add_line_follow_logging(line_follow_logging_offset); |
| 523 | builder.add_trajectory_logging(trajectory_logging_offset); |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 524 | builder.add_down_estimator(down_estimator_state_offset); |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 525 | builder.add_localizer(localizer_offset); |
James Kuszmaul | 2215d92 | 2020-02-11 17:17:26 -0800 | [diff] [blame] | 526 | builder.add_zeroing(zeroer_offset); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 527 | status->Send(builder.Finish()); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 528 | } |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 529 | |
James Kuszmaul | ddc6970 | 2021-03-24 21:55:03 -0700 | [diff] [blame] | 530 | // If the filters aren't ready/valid, then disable all outputs (currently, |
| 531 | // this only happens if the IMU is faulted or has not zeroed). |
| 532 | // TODO(james): Add exceptions so that during competitive play the driver |
| 533 | // can retain minimal control of the robot. |
| 534 | if (!filters_.Ready()) { |
| 535 | output_struct.left_voltage = 0.0; |
| 536 | output_struct.right_voltage = 0.0; |
| 537 | } |
| 538 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 539 | double left_voltage = 0.0; |
| 540 | double right_voltage = 0.0; |
| 541 | if (output) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 542 | left_voltage = output_struct.left_voltage; |
| 543 | right_voltage = output_struct.right_voltage; |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 544 | filters_.set_left_high_requested(output_struct.left_high); |
| 545 | filters_.set_right_high_requested(output_struct.right_high); |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 548 | const double scalar = robot_state().voltage_battery() / 12.0; |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 549 | |
| 550 | left_voltage *= scalar; |
| 551 | right_voltage *= scalar; |
| 552 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 553 | // To validate, look at the following: |
| 554 | |
| 555 | // Observed - dx/dt velocity for left, right. |
| 556 | |
| 557 | // Angular velocity error compared to the gyro |
| 558 | // Gyro heading vs left-right |
| 559 | // Voltage error. |
| 560 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 561 | { |
| 562 | Eigen::Matrix<double, 2, 1> U; |
| 563 | U(kLeftVoltage) = left_voltage; |
| 564 | U(kRightVoltage) = right_voltage; |
| 565 | filters_.UpdateObserver(U); |
| 566 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 567 | |
| 568 | if (output) { |
| 569 | output->Send(Output::Pack(*output->fbb(), &output_struct)); |
| 570 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 573 | flatbuffers::Offset<Output> DrivetrainLoop::Zero( |
| 574 | aos::Sender<Output>::Builder *output) { |
| 575 | Output::Builder builder = output->MakeBuilder<Output>(); |
| 576 | builder.add_left_voltage(0); |
| 577 | builder.add_right_voltage(0); |
| 578 | builder.add_left_high(dt_config_.default_high_gear); |
| 579 | builder.add_right_high(dt_config_.default_high_gear); |
| 580 | return builder.Finish(); |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 583 | } // namespace drivetrain |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 584 | } // namespace control_loops |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 585 | } // namespace frc971 |