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