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