Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 1 | #include "frc971/control_loops/drivetrain/drivetrain.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <sched.h> |
| 5 | #include <cmath> |
| 6 | #include <memory> |
| 7 | #include "Eigen/Dense" |
| 8 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/logging/logging.h" |
| 10 | #include "aos/logging/queue_logging.h" |
| 11 | #include "aos/logging/matrix_logging.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 12 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 13 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 14 | #include "frc971/control_loops/drivetrain/polydrivetrain.h" |
| 15 | #include "frc971/control_loops/drivetrain/ssdrivetrain.h" |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 16 | #include "frc971/control_loops/drivetrain/down_estimator.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 17 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 18 | #include "frc971/queues/gyro.q.h" |
| 19 | #include "frc971/shifter_hall_effect.h" |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 20 | #include "frc971/wpilib/imu.q.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 21 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 22 | using frc971::sensors::gyro_reading; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 23 | using frc971::imu_values; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 24 | using ::aos::monotonic_clock; |
| 25 | namespace chrono = ::std::chrono; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 26 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 27 | namespace frc971 { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 28 | namespace control_loops { |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 29 | namespace drivetrain { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 30 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 31 | DrivetrainLoop::DrivetrainLoop( |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 32 | const DrivetrainConfig<double> &dt_config, |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 33 | ::frc971::control_loops::DrivetrainQueue *my_drivetrain) |
| 34 | : aos::controls::ControlLoop<::frc971::control_loops::DrivetrainQueue>( |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 35 | my_drivetrain), |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 36 | dt_config_(dt_config), |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 37 | kf_(dt_config_.make_kf_drivetrain_loop()), |
| 38 | dt_openloop_(dt_config_, &kf_), |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 39 | dt_closedloop_(dt_config_, &kf_, &integrated_kf_heading_), |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 40 | down_estimator_(MakeDownEstimatorLoop()), |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 41 | left_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW), |
| 42 | right_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW), |
| 43 | left_high_requested_(dt_config_.default_high_gear), |
| 44 | right_high_requested_(dt_config_.default_high_gear) { |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 45 | ::aos::controls::HPolytope<0>::Init(); |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 46 | down_U_.setZero(); |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 49 | int DrivetrainLoop::ControllerIndexFromGears() { |
| 50 | if (MaybeHigh(left_gear_)) { |
| 51 | if (MaybeHigh(right_gear_)) { |
| 52 | return 3; |
| 53 | } else { |
| 54 | return 2; |
| 55 | } |
| 56 | } else { |
| 57 | if (MaybeHigh(right_gear_)) { |
| 58 | return 1; |
| 59 | } else { |
| 60 | return 0; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | Gear ComputeGear(double shifter_position, |
| 66 | const constants::ShifterHallEffect &shifter_config, |
| 67 | bool high_requested) { |
| 68 | if (shifter_position < shifter_config.clear_low) { |
| 69 | return Gear::LOW; |
| 70 | } else if (shifter_position > shifter_config.clear_high) { |
| 71 | return Gear::HIGH; |
| 72 | } else { |
| 73 | if (high_requested) { |
| 74 | return Gear::SHIFTING_UP; |
| 75 | } else { |
| 76 | return Gear::SHIFTING_DOWN; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 81 | void DrivetrainLoop::RunIteration( |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 82 | const ::frc971::control_loops::DrivetrainQueue::Goal *goal, |
| 83 | const ::frc971::control_loops::DrivetrainQueue::Position *position, |
| 84 | ::frc971::control_loops::DrivetrainQueue::Output *output, |
| 85 | ::frc971::control_loops::DrivetrainQueue::Status *status) { |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 86 | monotonic_clock::time_point monotonic_now = monotonic_clock::now(); |
| 87 | |
Austin Schuh | 5900d14 | 2016-04-03 21:35:12 -0700 | [diff] [blame] | 88 | if (!has_been_enabled_ && output) { |
| 89 | has_been_enabled_ = true; |
| 90 | down_estimator_.mutable_X_hat(1, 0) = 0.0; |
| 91 | } |
| 92 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 93 | // TODO(austin): Put gear detection logic here. |
| 94 | switch (dt_config_.shifter_type) { |
| 95 | case ShifterType::SIMPLE_SHIFTER: |
| 96 | // Force the right controller for simple shifters since we assume that |
| 97 | // gear switching is instantaneous. |
| 98 | if (left_high_requested_) { |
| 99 | left_gear_ = Gear::HIGH; |
| 100 | } else { |
| 101 | left_gear_ = Gear::LOW; |
| 102 | } |
| 103 | if (right_high_requested_) { |
| 104 | right_gear_ = Gear::HIGH; |
| 105 | } else { |
| 106 | right_gear_ = Gear::LOW; |
| 107 | } |
| 108 | break; |
| 109 | case ShifterType::HALL_EFFECT_SHIFTER: |
| 110 | left_gear_ = ComputeGear(position->left_shifter_position, |
| 111 | dt_config_.left_drive, left_high_requested_); |
| 112 | right_gear_ = ComputeGear(position->right_shifter_position, |
| 113 | dt_config_.right_drive, right_high_requested_); |
| 114 | break; |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 115 | case ShifterType::NO_SHIFTER: |
| 116 | break; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 117 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 118 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 119 | kf_.set_index(ControllerIndexFromGears()); |
Kyle Stachowicz | 2f3c20f | 2017-07-13 16:04:05 -0700 | [diff] [blame] | 120 | |
| 121 | // Set the gear-logging parts of the status |
| 122 | if (status) { |
| 123 | status->gear_logging.left_state = static_cast<uint32_t>(left_gear_); |
| 124 | status->gear_logging.right_state = static_cast<uint32_t>(right_gear_); |
| 125 | status->gear_logging.left_loop_high = MaybeHigh(left_gear_); |
| 126 | status->gear_logging.right_loop_high = MaybeHigh(right_gear_); |
| 127 | status->gear_logging.controller_index = kf_.index(); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 128 | } |
Kyle Stachowicz | 2f3c20f | 2017-07-13 16:04:05 -0700 | [diff] [blame] | 129 | |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 130 | const bool is_latest_imu_values = ::frc971::imu_values.FetchLatest(); |
| 131 | if (is_latest_imu_values) { |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 132 | const double rate = -::frc971::imu_values->gyro_y; |
| 133 | const double accel_squared = ::frc971::imu_values->accelerometer_x * |
| 134 | ::frc971::imu_values->accelerometer_x + |
| 135 | ::frc971::imu_values->accelerometer_y * |
| 136 | ::frc971::imu_values->accelerometer_y + |
| 137 | ::frc971::imu_values->accelerometer_z * |
| 138 | ::frc971::imu_values->accelerometer_z; |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame] | 139 | const double angle = ::std::atan2(::frc971::imu_values->accelerometer_x, |
| 140 | ::frc971::imu_values->accelerometer_z) + |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 141 | 0.008; |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 142 | |
| 143 | switch (dt_config_.imu_type) { |
| 144 | case IMUType::IMU_X: |
| 145 | last_accel_ = -::frc971::imu_values->accelerometer_x; |
| 146 | break; |
| 147 | case IMUType::IMU_Y: |
| 148 | last_accel_ = -::frc971::imu_values->accelerometer_y; |
| 149 | break; |
| 150 | } |
| 151 | |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 152 | if (accel_squared > 1.03 || accel_squared < 0.97) { |
| 153 | LOG(DEBUG, "New IMU value, rejecting reading\n"); |
| 154 | } else { |
| 155 | // -y is our gyro. |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame] | 156 | // z accel is down |
| 157 | // x accel is the front of the robot pointed down. |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 158 | Eigen::Matrix<double, 1, 1> Y; |
Austin Schuh | df79d11 | 2016-10-15 21:25:32 -0700 | [diff] [blame] | 159 | Y(0, 0) = angle; |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 160 | down_estimator_.Correct(Y); |
| 161 | } |
| 162 | |
| 163 | LOG(DEBUG, |
| 164 | "New IMU value from ADIS16448, rate is %f, angle %f, fused %f, bias " |
| 165 | "%f\n", |
| 166 | rate, angle, down_estimator_.X_hat(0, 0), down_estimator_.X_hat(1, 0)); |
Austin Schuh | df79d11 | 2016-10-15 21:25:32 -0700 | [diff] [blame] | 167 | down_U_(0, 0) = rate; |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 168 | } |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 169 | down_estimator_.UpdateObserver(down_U_, ::aos::controls::kLoopFrequency); |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 170 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 171 | // TODO(austin): Signal the current gear to both loops. |
| 172 | |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 173 | switch (dt_config_.gyro_type) { |
| 174 | case GyroType::IMU_X_GYRO: |
| 175 | if (is_latest_imu_values) { |
| 176 | LOG_STRUCT(DEBUG, "using", *imu_values.get()); |
| 177 | last_gyro_rate_ = imu_values->gyro_x; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 178 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 179 | } |
| 180 | break; |
| 181 | case GyroType::IMU_Y_GYRO: |
| 182 | if (is_latest_imu_values) { |
| 183 | LOG_STRUCT(DEBUG, "using", *imu_values.get()); |
| 184 | last_gyro_rate_ = imu_values->gyro_y; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 185 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 186 | } |
| 187 | break; |
| 188 | case GyroType::IMU_Z_GYRO: |
| 189 | if (is_latest_imu_values) { |
| 190 | LOG_STRUCT(DEBUG, "using", *imu_values.get()); |
| 191 | last_gyro_rate_ = imu_values->gyro_z; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 192 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 193 | } |
| 194 | break; |
Austin Schuh | 90b43b4 | 2019-01-04 07:45:05 +1100 | [diff] [blame] | 195 | case GyroType::FLIPPED_IMU_Z_GYRO: |
| 196 | if (is_latest_imu_values) { |
| 197 | LOG_STRUCT(DEBUG, "using", *imu_values.get()); |
| 198 | last_gyro_rate_ = -imu_values->gyro_z; |
| 199 | last_gyro_time_ = monotonic_now; |
| 200 | } |
| 201 | break; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 202 | case GyroType::SPARTAN_GYRO: |
| 203 | if (gyro_reading.FetchLatest()) { |
| 204 | LOG_STRUCT(DEBUG, "using", *gyro_reading.get()); |
| 205 | last_gyro_rate_ = gyro_reading->velocity; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 206 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 207 | } |
| 208 | break; |
| 209 | case GyroType::FLIPPED_SPARTAN_GYRO: |
| 210 | if (gyro_reading.FetchLatest()) { |
| 211 | LOG_STRUCT(DEBUG, "using", *gyro_reading.get()); |
| 212 | last_gyro_rate_ = -gyro_reading->velocity; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 213 | last_gyro_time_ = monotonic_now; |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 214 | } |
| 215 | break; |
| 216 | default: |
| 217 | LOG(FATAL, "invalid gyro configured"); |
| 218 | break; |
Austin Schuh | 9ab4d9d | 2016-02-16 23:55:44 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 221 | if (monotonic_now > last_gyro_time_ + chrono::milliseconds(20)) { |
| 222 | last_gyro_rate_ = 0.0; |
| 223 | } |
| 224 | |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 225 | { |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 226 | Eigen::Matrix<double, 4, 1> Y; |
| 227 | Y << position->left_encoder, position->right_encoder, last_gyro_rate_, |
| 228 | last_accel_; |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 229 | kf_.Correct(Y); |
Austin Schuh | bb735b7 | 2019-01-03 12:58:41 -0800 | [diff] [blame] | 230 | integrated_kf_heading_ += |
| 231 | chrono::duration_cast<chrono::duration<double>>(dt_config_.dt).count() * |
| 232 | (kf_.X_hat(3, 0) - kf_.X_hat(1, 0)) / (dt_config_.robot_radius * 2.0); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 233 | |
| 234 | // gyro_heading = (real_right - real_left) / width |
| 235 | // wheel_heading = (wheel_right - wheel_left) / width |
| 236 | // gyro_heading + offset = wheel_heading |
| 237 | // gyro_goal + offset = wheel_goal |
| 238 | // offset = wheel_heading - gyro_heading |
| 239 | |
| 240 | // gyro_goal + wheel_heading - gyro_heading = wheel_goal |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 243 | dt_openloop_.SetPosition(position, left_gear_, right_gear_); |
| 244 | |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame^] | 245 | int controller_type = 0; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 246 | if (goal) { |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame^] | 247 | controller_type = goal->controller_type; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 248 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 249 | dt_closedloop_.SetGoal(*goal); |
| 250 | dt_openloop_.SetGoal(*goal); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 253 | dt_openloop_.Update(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 254 | |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame^] | 255 | dt_closedloop_.Update(output != NULL && controller_type == 1); |
| 256 | |
| 257 | switch (controller_type) { |
| 258 | case 0: |
| 259 | dt_openloop_.SetOutput(output); |
| 260 | break; |
| 261 | case 1: |
| 262 | dt_closedloop_.SetOutput(output); |
| 263 | break; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 266 | // The output should now contain the shift request. |
| 267 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 268 | // set the output status of the control loop state |
| 269 | if (status) { |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 270 | status->robot_speed = (kf_.X_hat(1, 0) + kf_.X_hat(3, 0)) / 2.0; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 271 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 272 | Eigen::Matrix<double, 2, 1> linear = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 273 | dt_config_.LeftRightToLinear(kf_.X_hat()); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 274 | Eigen::Matrix<double, 2, 1> angular = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 275 | dt_config_.LeftRightToAngular(kf_.X_hat()); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 276 | |
| 277 | angular(0, 0) = integrated_kf_heading_; |
| 278 | |
| 279 | Eigen::Matrix<double, 4, 1> gyro_left_right = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 280 | dt_config_.AngularLinearToLeftRight(linear, angular); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 281 | |
| 282 | status->estimated_left_position = gyro_left_right(0, 0); |
| 283 | status->estimated_right_position = gyro_left_right(2, 0); |
| 284 | |
| 285 | status->estimated_left_velocity = gyro_left_right(1, 0); |
| 286 | status->estimated_right_velocity = gyro_left_right(3, 0); |
| 287 | status->output_was_capped = dt_closedloop_.output_was_capped(); |
| 288 | status->uncapped_left_voltage = kf_.U_uncapped(0, 0); |
| 289 | status->uncapped_right_voltage = kf_.U_uncapped(1, 0); |
| 290 | |
| 291 | status->left_voltage_error = kf_.X_hat(4, 0); |
| 292 | status->right_voltage_error = kf_.X_hat(5, 0); |
| 293 | status->estimated_angular_velocity_error = kf_.X_hat(6, 0); |
| 294 | status->estimated_heading = integrated_kf_heading_; |
Austin Schuh | 889fee8 | 2016-04-13 22:16:36 -0700 | [diff] [blame] | 295 | status->ground_angle = down_estimator_.X_hat(0, 0) + dt_config_.down_offset; |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 296 | |
| 297 | dt_openloop_.PopulateStatus(status); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 298 | dt_closedloop_.PopulateStatus(status); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 299 | } |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 300 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 301 | double left_voltage = 0.0; |
| 302 | double right_voltage = 0.0; |
| 303 | if (output) { |
| 304 | left_voltage = output->left_voltage; |
| 305 | right_voltage = output->right_voltage; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 306 | left_high_requested_ = output->left_high; |
| 307 | right_high_requested_ = output->right_high; |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | const double scalar = ::aos::robot_state->voltage_battery / 12.0; |
| 311 | |
| 312 | left_voltage *= scalar; |
| 313 | right_voltage *= scalar; |
| 314 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 315 | // To validate, look at the following: |
| 316 | |
| 317 | // Observed - dx/dt velocity for left, right. |
| 318 | |
| 319 | // Angular velocity error compared to the gyro |
| 320 | // Gyro heading vs left-right |
| 321 | // Voltage error. |
| 322 | |
| 323 | Eigen::Matrix<double, 2, 1> U; |
Austin Schuh | df79d11 | 2016-10-15 21:25:32 -0700 | [diff] [blame] | 324 | U(0, 0) = last_left_voltage_; |
| 325 | U(1, 0) = last_right_voltage_; |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 326 | last_left_voltage_ = left_voltage; |
| 327 | last_right_voltage_ = right_voltage; |
| 328 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 329 | kf_.UpdateObserver(U, ::aos::controls::kLoopFrequency); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 332 | void DrivetrainLoop::Zero( |
| 333 | ::frc971::control_loops::DrivetrainQueue::Output *output) { |
| 334 | output->left_voltage = 0; |
| 335 | output->right_voltage = 0; |
| 336 | output->left_high = dt_config_.default_high_gear; |
| 337 | output->right_high = dt_config_.default_high_gear; |
| 338 | } |
| 339 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 340 | } // namespace drivetrain |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 341 | } // namespace control_loops |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 342 | } // namespace frc971 |