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" |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/down_estimator.h" |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 12 | #include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h" |
| 13 | #include "frc971/control_loops/drivetrain/drivetrain_output_generated.h" |
| 14 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
| 15 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.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" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | #include "frc971/queues/gyro_generated.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 20 | #include "frc971/shifter_hall_effect.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | #include "frc971/wpilib/imu_generated.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) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 34 | : aos::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
| 35 | name), |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 36 | dt_config_(dt_config), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 37 | localizer_control_fetcher_( |
| 38 | event_loop->MakeFetcher<LocalizerControl>("/drivetrain")), |
Austin Schuh | 73b6e3b | 2019-05-27 16:37:15 -0700 | [diff] [blame] | 39 | imu_values_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 40 | event_loop->MakeFetcher<::frc971::IMUValues>("/drivetrain")), |
Austin Schuh | 1ea89bb | 2019-05-27 16:59:59 -0700 | [diff] [blame] | 41 | gyro_reading_fetcher_( |
| 42 | event_loop->MakeFetcher<::frc971::sensors::GyroReading>( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 43 | "/drivetrain")), |
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 | 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 | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 54 | ::aos::controls::HPolytope<0>::Init(); |
Austin Schuh | 59dddac | 2019-12-22 16:44:49 -0800 | [diff] [blame] | 55 | event_loop->SetRuntimeRealtimePriority(30); |
James Kuszmaul | 30aca50 | 2020-01-19 15:05:33 -0800 | [diff] [blame] | 56 | event_loop->OnRun([this]() { |
| 57 | // On the first fetch, make sure that we are caught all the way up to the |
| 58 | // present. |
| 59 | imu_values_fetcher_.Fetch(); |
| 60 | }); |
James Kuszmaul | 2215d92 | 2020-02-11 17:17:26 -0800 | [diff] [blame] | 61 | if (dt_config.is_simulated) { |
| 62 | down_estimator_.assume_perfect_gravity(); |
| 63 | } |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 66 | int DrivetrainLoop::ControllerIndexFromGears() { |
| 67 | if (MaybeHigh(left_gear_)) { |
| 68 | if (MaybeHigh(right_gear_)) { |
| 69 | return 3; |
| 70 | } else { |
| 71 | return 2; |
| 72 | } |
| 73 | } else { |
| 74 | if (MaybeHigh(right_gear_)) { |
| 75 | return 1; |
| 76 | } else { |
| 77 | return 0; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | Gear ComputeGear(double shifter_position, |
| 83 | const constants::ShifterHallEffect &shifter_config, |
| 84 | bool high_requested) { |
| 85 | if (shifter_position < shifter_config.clear_low) { |
| 86 | return Gear::LOW; |
| 87 | } else if (shifter_position > shifter_config.clear_high) { |
| 88 | return Gear::HIGH; |
| 89 | } else { |
| 90 | if (high_requested) { |
| 91 | return Gear::SHIFTING_UP; |
| 92 | } else { |
| 93 | return Gear::SHIFTING_DOWN; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 98 | void DrivetrainLoop::RunIteration( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 99 | const drivetrain::Goal *goal, const drivetrain::Position *position, |
| 100 | aos::Sender<drivetrain::Output>::Builder *output, |
| 101 | aos::Sender<drivetrain::Status>::Builder *status) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 102 | const monotonic_clock::time_point monotonic_now = |
| 103 | event_loop()->monotonic_now(); |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 104 | |
Austin Schuh | 5900d14 | 2016-04-03 21:35:12 -0700 | [diff] [blame] | 105 | if (!has_been_enabled_ && output) { |
| 106 | has_been_enabled_ = true; |
Austin Schuh | 5900d14 | 2016-04-03 21:35:12 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 109 | // TODO(austin): Put gear detection logic here. |
| 110 | switch (dt_config_.shifter_type) { |
| 111 | case ShifterType::SIMPLE_SHIFTER: |
| 112 | // Force the right controller for simple shifters since we assume that |
| 113 | // gear switching is instantaneous. |
| 114 | if (left_high_requested_) { |
| 115 | left_gear_ = Gear::HIGH; |
| 116 | } else { |
| 117 | left_gear_ = Gear::LOW; |
| 118 | } |
| 119 | if (right_high_requested_) { |
| 120 | right_gear_ = Gear::HIGH; |
| 121 | } else { |
| 122 | right_gear_ = Gear::LOW; |
| 123 | } |
| 124 | break; |
| 125 | case ShifterType::HALL_EFFECT_SHIFTER: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 126 | left_gear_ = ComputeGear(position->left_shifter_position(), |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 127 | dt_config_.left_drive, left_high_requested_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 128 | right_gear_ = ComputeGear(position->right_shifter_position(), |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 129 | dt_config_.right_drive, right_high_requested_); |
| 130 | break; |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 131 | case ShifterType::NO_SHIFTER: |
| 132 | break; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 133 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 134 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 135 | kf_.set_index(ControllerIndexFromGears()); |
Kyle Stachowicz | 2f3c20f | 2017-07-13 16:04:05 -0700 | [diff] [blame] | 136 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 137 | flatbuffers::Offset<GearLogging> gear_logging_offset; |
Kyle Stachowicz | 2f3c20f | 2017-07-13 16:04:05 -0700 | [diff] [blame] | 138 | // Set the gear-logging parts of the status |
| 139 | if (status) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 140 | GearLogging::Builder gear_logging_builder = |
| 141 | status->MakeBuilder<GearLogging>(); |
| 142 | gear_logging_builder.add_left_state(static_cast<uint32_t>(left_gear_)); |
| 143 | gear_logging_builder.add_right_state(static_cast<uint32_t>(right_gear_)); |
| 144 | gear_logging_builder.add_left_loop_high(MaybeHigh(left_gear_)); |
| 145 | gear_logging_builder.add_right_loop_high(MaybeHigh(right_gear_)); |
| 146 | gear_logging_builder.add_controller_index(kf_.index()); |
| 147 | gear_logging_offset = gear_logging_builder.Finish(); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 148 | } |
Kyle Stachowicz | 2f3c20f | 2017-07-13 16:04:05 -0700 | [diff] [blame] | 149 | |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 150 | while (imu_values_fetcher_.FetchNext()) { |
| 151 | imu_zeroer_.ProcessMeasurement(*imu_values_fetcher_); |
| 152 | last_gyro_time_ = monotonic_now; |
James Kuszmaul | b1e2937 | 2020-02-11 16:55:36 -0800 | [diff] [blame] | 153 | if (!imu_zeroer_.Zeroed()) { |
| 154 | continue; |
| 155 | } |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 156 | aos::monotonic_clock::time_point reading_time(std::chrono::nanoseconds( |
| 157 | imu_values_fetcher_->monotonic_timestamp_ns())); |
| 158 | if (last_imu_update_ == aos::monotonic_clock::min_time) { |
| 159 | last_imu_update_ = reading_time; |
| 160 | } |
| 161 | down_estimator_.Predict(imu_zeroer_.ZeroedGyro(), imu_zeroer_.ZeroedAccel(), |
| 162 | reading_time - last_imu_update_); |
| 163 | last_imu_update_ = reading_time; |
| 164 | } |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 165 | |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 166 | bool got_imu_reading = false; |
| 167 | if (imu_values_fetcher_.get() != nullptr) { |
| 168 | got_imu_reading = true; |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 169 | switch (dt_config_.imu_type) { |
| 170 | case IMUType::IMU_X: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 171 | last_accel_ = -imu_values_fetcher_->accelerometer_x(); |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 172 | break; |
Austin Schuh | e7f6ddf | 2019-03-22 20:29:49 -0700 | [diff] [blame] | 173 | case IMUType::IMU_FLIPPED_X: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 174 | last_accel_ = imu_values_fetcher_->accelerometer_x(); |
Austin Schuh | e7f6ddf | 2019-03-22 20:29:49 -0700 | [diff] [blame] | 175 | break; |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 176 | case IMUType::IMU_Y: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 177 | last_accel_ = -imu_values_fetcher_->accelerometer_y(); |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 178 | break; |
| 179 | } |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 180 | } |
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: |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 186 | if (got_imu_reading) { |
| 187 | last_gyro_rate_ = imu_zeroer_.ZeroedGyro().x(); |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 188 | } |
| 189 | break; |
| 190 | case GyroType::IMU_Y_GYRO: |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 191 | if (got_imu_reading) { |
| 192 | last_gyro_rate_ = imu_zeroer_.ZeroedGyro().y(); |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 193 | } |
| 194 | break; |
| 195 | case GyroType::IMU_Z_GYRO: |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 196 | if (got_imu_reading) { |
| 197 | last_gyro_rate_ = imu_zeroer_.ZeroedGyro().z(); |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 198 | } |
| 199 | break; |
Austin Schuh | 90b43b4 | 2019-01-04 07:45:05 +1100 | [diff] [blame] | 200 | case GyroType::FLIPPED_IMU_Z_GYRO: |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 201 | if (got_imu_reading) { |
| 202 | last_gyro_rate_ = -imu_zeroer_.ZeroedGyro().z(); |
Austin Schuh | 90b43b4 | 2019-01-04 07:45:05 +1100 | [diff] [blame] | 203 | } |
| 204 | break; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 205 | case GyroType::SPARTAN_GYRO: |
Austin Schuh | 1ea89bb | 2019-05-27 16:59:59 -0700 | [diff] [blame] | 206 | if (gyro_reading_fetcher_.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 207 | last_gyro_rate_ = gyro_reading_fetcher_->velocity(); |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 208 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 209 | } |
| 210 | break; |
| 211 | case GyroType::FLIPPED_SPARTAN_GYRO: |
Austin Schuh | 1ea89bb | 2019-05-27 16:59:59 -0700 | [diff] [blame] | 212 | if (gyro_reading_fetcher_.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 213 | last_gyro_rate_ = -gyro_reading_fetcher_->velocity(); |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 214 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 215 | } |
| 216 | break; |
| 217 | default: |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 218 | AOS_LOG(FATAL, "invalid gyro configured"); |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 219 | break; |
Austin Schuh | 9ab4d9d | 2016-02-16 23:55:44 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 222 | if (monotonic_now > last_gyro_time_ + chrono::milliseconds(20)) { |
| 223 | last_gyro_rate_ = 0.0; |
| 224 | } |
| 225 | |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 226 | { |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 227 | Eigen::Matrix<double, 4, 1> Y; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 228 | Y << position->left_encoder(), position->right_encoder(), last_gyro_rate_, |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 229 | last_accel_; |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 230 | kf_.Correct(Y); |
James Kuszmaul | ef428a0 | 2019-03-02 22:19:41 -0800 | [diff] [blame] | 231 | // If we get a new message setting the absolute position, then reset the |
| 232 | // localizer. |
| 233 | // TODO(james): Use a watcher (instead of a fetcher) once we support it in |
| 234 | // simulation. |
| 235 | if (localizer_control_fetcher_.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 236 | VLOG(1) << "localizer_control " |
| 237 | << aos::FlatbufferToJson(localizer_control_fetcher_.get()); |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 238 | localizer_->ResetPosition( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 239 | monotonic_now, localizer_control_fetcher_->x(), |
| 240 | localizer_control_fetcher_->y(), localizer_control_fetcher_->theta(), |
| 241 | localizer_control_fetcher_->theta_uncertainty(), |
| 242 | !localizer_control_fetcher_->keep_current_theta()); |
James Kuszmaul | ef428a0 | 2019-03-02 22:19:41 -0800 | [diff] [blame] | 243 | } |
James Kuszmaul | f3add3c | 2019-03-02 14:55:00 -0800 | [diff] [blame] | 244 | localizer_->Update({last_last_left_voltage_, last_last_right_voltage_}, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 245 | monotonic_now, position->left_encoder(), |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 246 | position->right_encoder(), |
| 247 | down_estimator_.avg_recent_yaw_rates(), |
| 248 | down_estimator_.avg_recent_accel()); |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 251 | dt_openloop_.SetPosition(position, left_gear_, right_gear_); |
| 252 | |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 253 | ControllerType controller_type = ControllerType::POLYDRIVE; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 254 | if (goal) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 255 | controller_type = goal->controller_type(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 256 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 257 | dt_closedloop_.SetGoal(goal); |
| 258 | dt_openloop_.SetGoal(goal->wheel(), goal->throttle(), goal->quickturn(), |
| 259 | goal->highgear()); |
| 260 | dt_spline_.SetGoal(goal); |
| 261 | dt_line_follow_.SetGoal(monotonic_now, goal); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 264 | dt_openloop_.Update(robot_state().voltage_battery()); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 265 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 266 | dt_closedloop_.Update(output != nullptr && |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 267 | controller_type == ControllerType::MOTION_PROFILE); |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 268 | |
James Kuszmaul | e39cbcf | 2019-02-27 20:48:34 -0800 | [diff] [blame] | 269 | const Eigen::Matrix<double, 5, 1> trajectory_state = |
| 270 | (Eigen::Matrix<double, 5, 1>() << localizer_->x(), localizer_->y(), |
| 271 | localizer_->theta(), localizer_->left_velocity(), |
| 272 | localizer_->right_velocity()) |
| 273 | .finished(); |
| 274 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 275 | dt_spline_.Update( |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 276 | output != nullptr && controller_type == ControllerType::SPLINE_FOLLOWER, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 277 | trajectory_state); |
James Kuszmaul | e39cbcf | 2019-02-27 20:48:34 -0800 | [diff] [blame] | 278 | |
James Kuszmaul | 38e7964 | 2019-03-09 15:48:27 -0800 | [diff] [blame] | 279 | dt_line_follow_.Update(monotonic_now, trajectory_state); |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 280 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 281 | OutputT output_struct; |
| 282 | |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 283 | switch (controller_type) { |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 284 | case ControllerType::POLYDRIVE: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 285 | dt_openloop_.SetOutput(output != nullptr ? &output_struct : nullptr); |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 286 | break; |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 287 | case ControllerType::MOTION_PROFILE: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 288 | dt_closedloop_.SetOutput(output != nullptr ? &output_struct : nullptr); |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 289 | break; |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 290 | case ControllerType::SPLINE_FOLLOWER: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 291 | dt_spline_.SetOutput(output != nullptr ? &output_struct : nullptr); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 292 | break; |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 293 | case ControllerType::LINE_FOLLOWER: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 294 | if (!dt_line_follow_.SetOutput(output != nullptr ? &output_struct |
| 295 | : nullptr)) { |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 296 | // If the line follow drivetrain was unable to execute (generally due to |
| 297 | // not having a target), execute the regular teleop drivetrain. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 298 | dt_openloop_.SetOutput(output != nullptr ? &output_struct : nullptr); |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 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 | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 307 | Eigen::Matrix<double, 2, 1> linear = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 308 | dt_config_.LeftRightToLinear(kf_.X_hat()); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 309 | Eigen::Matrix<double, 2, 1> angular = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 310 | dt_config_.LeftRightToAngular(kf_.X_hat()); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 311 | |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 312 | angular(0, 0) = localizer_->theta(); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 313 | |
| 314 | Eigen::Matrix<double, 4, 1> gyro_left_right = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 315 | dt_config_.AngularLinearToLeftRight(linear, angular); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 316 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 317 | const flatbuffers::Offset<CIMLogging> cim_logging_offset = |
James Kuszmaul | af5dfad | 2020-01-03 20:02:54 -0800 | [diff] [blame] | 318 | dt_openloop_.PopulateShiftingStatus(status->fbb()); |
| 319 | |
| 320 | const flatbuffers::Offset<PolyDriveLogging> poly_drive_logging_offset = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 321 | dt_openloop_.PopulateStatus(status->fbb()); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 322 | |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 323 | const flatbuffers::Offset<DownEstimatorState> down_estimator_state_offset = |
James Kuszmaul | 2215d92 | 2020-02-11 17:17:26 -0800 | [diff] [blame] | 324 | down_estimator_.PopulateStatus(status->fbb(), monotonic_now); |
| 325 | |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 326 | const flatbuffers::Offset<LocalizerState> localizer_offset = |
| 327 | localizer_->PopulateStatus(status->fbb()); |
| 328 | |
James Kuszmaul | 2215d92 | 2020-02-11 17:17:26 -0800 | [diff] [blame] | 329 | const flatbuffers::Offset<ImuZeroerState> zeroer_offset = |
| 330 | imu_zeroer_.PopulateStatus(status->fbb()); |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 331 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 332 | flatbuffers::Offset<LineFollowLogging> line_follow_logging_offset = |
| 333 | dt_line_follow_.PopulateStatus(status); |
| 334 | flatbuffers::Offset<TrajectoryLogging> trajectory_logging_offset = |
| 335 | dt_spline_.MakeTrajectoryLogging(status); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 336 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 337 | StatusBuilder builder = status->MakeBuilder<Status>(); |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 338 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 339 | dt_closedloop_.PopulateStatus(&builder); |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 340 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 341 | builder.add_estimated_left_position(gyro_left_right(0, 0)); |
| 342 | builder.add_estimated_right_position(gyro_left_right(2, 0)); |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 343 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 344 | builder.add_estimated_left_velocity(gyro_left_right(1, 0)); |
| 345 | builder.add_estimated_right_velocity(gyro_left_right(3, 0)); |
| 346 | |
| 347 | if (dt_spline_.enable()) { |
| 348 | dt_spline_.PopulateStatus(&builder); |
| 349 | } else { |
| 350 | builder.add_robot_speed((kf_.X_hat(1) + kf_.X_hat(3)) / 2.0); |
| 351 | builder.add_output_was_capped(dt_closedloop_.output_was_capped()); |
| 352 | builder.add_uncapped_left_voltage(kf_.U_uncapped(0, 0)); |
| 353 | builder.add_uncapped_right_voltage(kf_.U_uncapped(1, 0)); |
| 354 | } |
| 355 | |
| 356 | builder.add_left_voltage_error(kf_.X_hat(4)); |
| 357 | builder.add_right_voltage_error(kf_.X_hat(5)); |
| 358 | builder.add_estimated_angular_velocity_error(kf_.X_hat(6)); |
| 359 | builder.add_estimated_heading(localizer_->theta()); |
| 360 | |
| 361 | builder.add_x(localizer_->x()); |
| 362 | builder.add_y(localizer_->y()); |
| 363 | builder.add_theta(::aos::math::NormalizeAngle(localizer_->theta())); |
| 364 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 365 | builder.add_cim_logging(cim_logging_offset); |
James Kuszmaul | af5dfad | 2020-01-03 20:02:54 -0800 | [diff] [blame] | 366 | builder.add_poly_drive_logging(poly_drive_logging_offset); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 367 | builder.add_gear_logging(gear_logging_offset); |
| 368 | builder.add_line_follow_logging(line_follow_logging_offset); |
| 369 | builder.add_trajectory_logging(trajectory_logging_offset); |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 370 | builder.add_down_estimator(down_estimator_state_offset); |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 371 | builder.add_localizer(localizer_offset); |
James Kuszmaul | 2215d92 | 2020-02-11 17:17:26 -0800 | [diff] [blame] | 372 | builder.add_zeroing(zeroer_offset); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 373 | status->Send(builder.Finish()); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 374 | } |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 375 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 376 | double left_voltage = 0.0; |
| 377 | double right_voltage = 0.0; |
| 378 | if (output) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 379 | left_voltage = output_struct.left_voltage; |
| 380 | right_voltage = output_struct.right_voltage; |
| 381 | left_high_requested_ = output_struct.left_high; |
| 382 | right_high_requested_ = output_struct.right_high; |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 385 | const double scalar = robot_state().voltage_battery() / 12.0; |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 386 | |
| 387 | left_voltage *= scalar; |
| 388 | right_voltage *= scalar; |
| 389 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 390 | // To validate, look at the following: |
| 391 | |
| 392 | // Observed - dx/dt velocity for left, right. |
| 393 | |
| 394 | // Angular velocity error compared to the gyro |
| 395 | // Gyro heading vs left-right |
| 396 | // Voltage error. |
| 397 | |
James Kuszmaul | f3add3c | 2019-03-02 14:55:00 -0800 | [diff] [blame] | 398 | last_last_left_voltage_ = last_left_voltage_; |
| 399 | last_last_right_voltage_ = last_right_voltage_; |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 400 | Eigen::Matrix<double, 2, 1> U; |
Austin Schuh | df79d11 | 2016-10-15 21:25:32 -0700 | [diff] [blame] | 401 | U(0, 0) = last_left_voltage_; |
| 402 | U(1, 0) = last_right_voltage_; |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 403 | last_left_voltage_ = left_voltage; |
| 404 | last_right_voltage_ = right_voltage; |
| 405 | |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 406 | last_state_ = kf_.X_hat(); |
| 407 | kf_.UpdateObserver(U, dt_config_.dt); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 408 | |
| 409 | if (output) { |
| 410 | output->Send(Output::Pack(*output->fbb(), &output_struct)); |
| 411 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 414 | flatbuffers::Offset<Output> DrivetrainLoop::Zero( |
| 415 | aos::Sender<Output>::Builder *output) { |
| 416 | Output::Builder builder = output->MakeBuilder<Output>(); |
| 417 | builder.add_left_voltage(0); |
| 418 | builder.add_right_voltage(0); |
| 419 | builder.add_left_high(dt_config_.default_high_gear); |
| 420 | builder.add_right_high(dt_config_.default_high_gear); |
| 421 | return builder.Finish(); |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 424 | } // namespace drivetrain |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 425 | } // namespace control_loops |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 426 | } // namespace frc971 |