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_), |
| 34 | dt_closedloop_(dt_config_) { |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 35 | ::aos::controls::HPolytope<0>::Init(); |
| 36 | } |
| 37 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 38 | void DrivetrainLoop::RunIteration( |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 39 | const ::frc971::control_loops::DrivetrainQueue::Goal *goal, |
| 40 | const ::frc971::control_loops::DrivetrainQueue::Position *position, |
| 41 | ::frc971::control_loops::DrivetrainQueue::Output *output, |
| 42 | ::frc971::control_loops::DrivetrainQueue::Status *status) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 43 | bool bad_pos = false; |
| 44 | if (position == nullptr) { |
| 45 | LOG_INTERVAL(no_position_); |
| 46 | bad_pos = true; |
| 47 | } |
| 48 | no_position_.Print(); |
| 49 | |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 50 | kf_.set_controller_index(dt_openloop_.controller_index()); |
| 51 | |
Austin Schuh | 9ab4d9d | 2016-02-16 23:55:44 -0800 | [diff] [blame] | 52 | bool gyro_valid = false; |
| 53 | if (gyro_reading.FetchLatest()) { |
| 54 | LOG_STRUCT(DEBUG, "using", *gyro_reading.get()); |
| 55 | last_gyro_heading_ = gyro_reading->angle; |
| 56 | last_gyro_rate_ = gyro_reading->velocity; |
| 57 | gyro_valid = true; |
| 58 | } |
| 59 | |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 60 | { |
| 61 | Eigen::Matrix<double, 3, 1> Y; |
| 62 | Y << position->left_encoder, position->right_encoder, last_gyro_rate_; |
| 63 | kf_.Correct(Y); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 64 | integrated_kf_heading_ += dt_config_.dt * |
| 65 | (kf_.X_hat(3, 0) - kf_.X_hat(1, 0)) / |
| 66 | (dt_config_.robot_radius * 2.0); |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 69 | bool control_loop_driving = false; |
| 70 | if (goal) { |
| 71 | double wheel = goal->steering; |
| 72 | double throttle = goal->throttle; |
| 73 | bool quickturn = goal->quickturn; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 74 | bool highgear = goal->highgear; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 75 | |
| 76 | control_loop_driving = goal->control_loop_driving; |
| 77 | double left_goal = goal->left_goal; |
| 78 | double right_goal = goal->right_goal; |
| 79 | |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 80 | dt_closedloop_.SetGoal(left_goal, goal->left_velocity_goal, right_goal, |
| 81 | goal->right_velocity_goal); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 82 | dt_openloop_.SetGoal(wheel, throttle, quickturn, highgear); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | if (!bad_pos) { |
| 86 | const double left_encoder = position->left_encoder; |
| 87 | const double right_encoder = position->right_encoder; |
Austin Schuh | 9ab4d9d | 2016-02-16 23:55:44 -0800 | [diff] [blame] | 88 | if (gyro_valid) { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 89 | dt_closedloop_.SetPosition(left_encoder, right_encoder, |
| 90 | gyro_reading->angle); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 91 | } else { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 92 | dt_closedloop_.SetRawPosition(left_encoder, right_encoder); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 95 | dt_openloop_.SetPosition(position); |
| 96 | dt_openloop_.Update(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 97 | |
| 98 | if (control_loop_driving) { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 99 | dt_closedloop_.Update(output == NULL, true); |
| 100 | dt_closedloop_.SendMotors(output); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 101 | } else { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 102 | dt_openloop_.SendMotors(output); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 103 | if (output) { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 104 | dt_closedloop_.SetExternalMotors(output->left_voltage, |
| 105 | output->right_voltage); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 106 | } |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 107 | dt_closedloop_.Update(output == NULL, false); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // set the output status of the control loop state |
| 111 | if (status) { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 112 | status->robot_speed = dt_closedloop_.GetEstimatedRobotSpeed(); |
| 113 | status->filtered_left_position = dt_closedloop_.GetEstimatedLeftEncoder(); |
| 114 | status->filtered_right_position = dt_closedloop_.GetEstimatedRightEncoder(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 115 | |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 116 | status->filtered_left_velocity = dt_closedloop_.loop().X_hat(1, 0); |
| 117 | status->filtered_right_velocity = dt_closedloop_.loop().X_hat(3, 0); |
| 118 | status->output_was_capped = dt_closedloop_.OutputWasCapped(); |
| 119 | status->uncapped_left_voltage = dt_closedloop_.loop().U_uncapped(0, 0); |
| 120 | status->uncapped_right_voltage = dt_closedloop_.loop().U_uncapped(1, 0); |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 121 | |
| 122 | dt_openloop_.PopulateStatus(status); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 123 | } |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 124 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 125 | double left_voltage = 0.0; |
| 126 | double right_voltage = 0.0; |
| 127 | if (output) { |
| 128 | left_voltage = output->left_voltage; |
| 129 | right_voltage = output->right_voltage; |
| 130 | } |
| 131 | |
| 132 | const double scalar = ::aos::robot_state->voltage_battery / 12.0; |
| 133 | |
| 134 | left_voltage *= scalar; |
| 135 | right_voltage *= scalar; |
| 136 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 137 | // To validate, look at the following: |
| 138 | |
| 139 | // Observed - dx/dt velocity for left, right. |
| 140 | |
| 141 | // Angular velocity error compared to the gyro |
| 142 | // Gyro heading vs left-right |
| 143 | // Voltage error. |
| 144 | |
| 145 | Eigen::Matrix<double, 2, 1> U; |
| 146 | U << last_left_voltage_, last_right_voltage_; |
| 147 | last_left_voltage_ = left_voltage; |
| 148 | last_right_voltage_ = right_voltage; |
| 149 | |
| 150 | kf_.UpdateObserver(U); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 153 | void DrivetrainLoop::Zero( |
| 154 | ::frc971::control_loops::DrivetrainQueue::Output *output) { |
| 155 | output->left_voltage = 0; |
| 156 | output->right_voltage = 0; |
| 157 | output->left_high = dt_config_.default_high_gear; |
| 158 | output->right_high = dt_config_.default_high_gear; |
| 159 | } |
| 160 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 161 | } // namespace drivetrain |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 162 | } // namespace control_loops |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 163 | } // namespace frc971 |