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