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