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