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