Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 1 | #include "y2014/control_loops/drivetrain/drivetrain.h" |
| 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 | |
| 13 | #include "y2014/constants.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 14 | #include "y2014/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 0e99773 | 2015-11-08 15:14:53 -0800 | [diff] [blame] | 15 | #include "y2014/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 16 | #include "y2014/control_loops/drivetrain/kalman_drivetrain_motor_plant.h" |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 17 | #include "y2014/control_loops/drivetrain/polydrivetrain.h" |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 18 | #include "y2014/control_loops/drivetrain/ssdrivetrain.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 19 | #include "frc971/queues/gyro.q.h" |
| 20 | #include "frc971/shifter_hall_effect.h" |
| 21 | |
| 22 | // A consistent way to mark code that goes away without shifters. It's still |
| 23 | // here because we will have shifters again in the future. |
| 24 | #define HAVE_SHIFTERS 1 |
| 25 | |
| 26 | using frc971::sensors::gyro_reading; |
| 27 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 28 | namespace y2014 { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 29 | namespace control_loops { |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 30 | namespace drivetrain { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 31 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 32 | DrivetrainLoop::DrivetrainLoop( |
| 33 | ::frc971::control_loops::DrivetrainQueue *my_drivetrain) |
| 34 | : aos::controls::ControlLoop<::frc971::control_loops::DrivetrainQueue>( |
| 35 | my_drivetrain), |
| 36 | kf_(::y2014::control_loops::drivetrain::MakeKFDrivetrainLoop()) { |
| 37 | ::aos::controls::HPolytope<0>::Init(); |
| 38 | } |
| 39 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 40 | void DrivetrainLoop::RunIteration( |
| 41 | const ::frc971::control_loops::DrivetrainQueue::Goal *goal, |
| 42 | const ::frc971::control_loops::DrivetrainQueue::Position *position, |
| 43 | ::frc971::control_loops::DrivetrainQueue::Output *output, |
| 44 | ::frc971::control_loops::DrivetrainQueue::Status *status) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 45 | bool bad_pos = false; |
| 46 | if (position == nullptr) { |
| 47 | LOG_INTERVAL(no_position_); |
| 48 | bad_pos = true; |
| 49 | } |
| 50 | no_position_.Print(); |
| 51 | |
| 52 | bool control_loop_driving = false; |
| 53 | if (goal) { |
| 54 | double wheel = goal->steering; |
| 55 | double throttle = goal->throttle; |
| 56 | bool quickturn = goal->quickturn; |
| 57 | #if HAVE_SHIFTERS |
| 58 | bool highgear = goal->highgear; |
| 59 | #endif |
| 60 | |
| 61 | control_loop_driving = goal->control_loop_driving; |
| 62 | double left_goal = goal->left_goal; |
| 63 | double right_goal = goal->right_goal; |
| 64 | |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 65 | dt_closedloop_.SetGoal(left_goal, goal->left_velocity_goal, right_goal, |
| 66 | goal->right_velocity_goal); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 67 | #if HAVE_SHIFTERS |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 68 | dt_openloop_.SetGoal(wheel, throttle, quickturn, highgear); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 69 | #else |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 70 | dt_openloop_.SetGoal(wheel, throttle, quickturn, false); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 71 | #endif |
| 72 | } |
| 73 | |
| 74 | if (!bad_pos) { |
| 75 | const double left_encoder = position->left_encoder; |
| 76 | const double right_encoder = position->right_encoder; |
| 77 | if (gyro_reading.FetchLatest()) { |
| 78 | LOG_STRUCT(DEBUG, "using", *gyro_reading.get()); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 79 | dt_closedloop_.SetPosition(left_encoder, right_encoder, |
| 80 | gyro_reading->angle); |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 81 | last_gyro_heading_ = gyro_reading->angle; |
| 82 | last_gyro_rate_ = gyro_reading->velocity; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 83 | } else { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 84 | dt_closedloop_.SetRawPosition(left_encoder, right_encoder); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 87 | dt_openloop_.SetPosition(position); |
| 88 | dt_openloop_.Update(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 89 | |
| 90 | if (control_loop_driving) { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 91 | dt_closedloop_.Update(output == NULL, true); |
| 92 | dt_closedloop_.SendMotors(output); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 93 | } else { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 94 | dt_openloop_.SendMotors(output); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 95 | if (output) { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 96 | dt_closedloop_.SetExternalMotors(output->left_voltage, |
| 97 | output->right_voltage); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 98 | } |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 99 | dt_closedloop_.Update(output == NULL, false); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // set the output status of the control loop state |
| 103 | if (status) { |
| 104 | bool done = false; |
| 105 | if (goal) { |
| 106 | done = ((::std::abs(goal->left_goal - |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 107 | dt_closedloop_.GetEstimatedLeftEncoder()) < |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 108 | constants::GetValues().drivetrain_done_distance) && |
| 109 | (::std::abs(goal->right_goal - |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 110 | dt_closedloop_.GetEstimatedRightEncoder()) < |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 111 | constants::GetValues().drivetrain_done_distance)); |
| 112 | } |
| 113 | status->is_done = done; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 114 | status->robot_speed = dt_closedloop_.GetEstimatedRobotSpeed(); |
| 115 | status->filtered_left_position = dt_closedloop_.GetEstimatedLeftEncoder(); |
| 116 | status->filtered_right_position = dt_closedloop_.GetEstimatedRightEncoder(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 117 | |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 118 | status->filtered_left_velocity = dt_closedloop_.loop().X_hat(1, 0); |
| 119 | status->filtered_right_velocity = dt_closedloop_.loop().X_hat(3, 0); |
| 120 | status->output_was_capped = dt_closedloop_.OutputWasCapped(); |
| 121 | status->uncapped_left_voltage = dt_closedloop_.loop().U_uncapped(0, 0); |
| 122 | status->uncapped_right_voltage = dt_closedloop_.loop().U_uncapped(1, 0); |
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 | |
| 125 | |
| 126 | double left_voltage = 0.0; |
| 127 | double right_voltage = 0.0; |
| 128 | if (output) { |
| 129 | left_voltage = output->left_voltage; |
| 130 | right_voltage = output->right_voltage; |
| 131 | } |
| 132 | |
| 133 | const double scalar = ::aos::robot_state->voltage_battery / 12.0; |
| 134 | |
| 135 | left_voltage *= scalar; |
| 136 | right_voltage *= scalar; |
| 137 | |
| 138 | kf_.set_controller_index(dt_openloop_.controller_index()); |
| 139 | |
| 140 | Eigen::Matrix<double, 3, 1> Y; |
| 141 | Y << position->left_encoder, position->right_encoder, last_gyro_rate_; |
| 142 | kf_.Correct(Y); |
| 143 | integrated_kf_heading_ += |
| 144 | kDt * (kf_.X_hat(3, 0) - kf_.X_hat(1, 0)) / (kRobotRadius * 2.0); |
| 145 | |
| 146 | // To validate, look at the following: |
| 147 | |
| 148 | // Observed - dx/dt velocity for left, right. |
| 149 | |
| 150 | // Angular velocity error compared to the gyro |
| 151 | // Gyro heading vs left-right |
| 152 | // Voltage error. |
| 153 | |
| 154 | Eigen::Matrix<double, 2, 1> U; |
| 155 | U << last_left_voltage_, last_right_voltage_; |
| 156 | last_left_voltage_ = left_voltage; |
| 157 | last_right_voltage_ = right_voltage; |
| 158 | |
| 159 | kf_.UpdateObserver(U); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 162 | } // namespace drivetrain |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 163 | } // namespace control_loops |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 164 | } // namespace y2014 |