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 | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 16 | #include "y2014/control_loops/drivetrain/polydrivetrain.h" |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 17 | #include "y2014/control_loops/drivetrain/ssdrivetrain.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" |
| 20 | |
| 21 | // A consistent way to mark code that goes away without shifters. It's still |
| 22 | // here because we will have shifters again in the future. |
| 23 | #define HAVE_SHIFTERS 1 |
| 24 | |
| 25 | using frc971::sensors::gyro_reading; |
| 26 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 27 | namespace y2014 { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 28 | namespace control_loops { |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 29 | namespace drivetrain { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 30 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 31 | void DrivetrainLoop::RunIteration( |
| 32 | const ::frc971::control_loops::DrivetrainQueue::Goal *goal, |
| 33 | const ::frc971::control_loops::DrivetrainQueue::Position *position, |
| 34 | ::frc971::control_loops::DrivetrainQueue::Output *output, |
| 35 | ::frc971::control_loops::DrivetrainQueue::Status *status) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 36 | bool bad_pos = false; |
| 37 | if (position == nullptr) { |
| 38 | LOG_INTERVAL(no_position_); |
| 39 | bad_pos = true; |
| 40 | } |
| 41 | no_position_.Print(); |
| 42 | |
| 43 | bool control_loop_driving = false; |
| 44 | if (goal) { |
| 45 | double wheel = goal->steering; |
| 46 | double throttle = goal->throttle; |
| 47 | bool quickturn = goal->quickturn; |
| 48 | #if HAVE_SHIFTERS |
| 49 | bool highgear = goal->highgear; |
| 50 | #endif |
| 51 | |
| 52 | control_loop_driving = goal->control_loop_driving; |
| 53 | double left_goal = goal->left_goal; |
| 54 | double right_goal = goal->right_goal; |
| 55 | |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 56 | dt_closedloop_.SetGoal(left_goal, goal->left_velocity_goal, right_goal, |
| 57 | goal->right_velocity_goal); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 58 | #if HAVE_SHIFTERS |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 59 | dt_openloop_.SetGoal(wheel, throttle, quickturn, highgear); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 60 | #else |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 61 | dt_openloop_.SetGoal(wheel, throttle, quickturn, false); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 62 | #endif |
| 63 | } |
| 64 | |
| 65 | if (!bad_pos) { |
| 66 | const double left_encoder = position->left_encoder; |
| 67 | const double right_encoder = position->right_encoder; |
| 68 | if (gyro_reading.FetchLatest()) { |
| 69 | LOG_STRUCT(DEBUG, "using", *gyro_reading.get()); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 70 | dt_closedloop_.SetPosition(left_encoder, right_encoder, |
| 71 | gyro_reading->angle); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 72 | } else { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 73 | dt_closedloop_.SetRawPosition(left_encoder, right_encoder); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 76 | dt_openloop_.SetPosition(position); |
| 77 | dt_openloop_.Update(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 78 | |
| 79 | if (control_loop_driving) { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 80 | dt_closedloop_.Update(output == NULL, true); |
| 81 | dt_closedloop_.SendMotors(output); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 82 | } else { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 83 | dt_openloop_.SendMotors(output); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 84 | if (output) { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 85 | dt_closedloop_.SetExternalMotors(output->left_voltage, |
| 86 | output->right_voltage); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 87 | } |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 88 | dt_closedloop_.Update(output == NULL, false); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // set the output status of the control loop state |
| 92 | if (status) { |
| 93 | bool done = false; |
| 94 | if (goal) { |
| 95 | done = ((::std::abs(goal->left_goal - |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 96 | dt_closedloop_.GetEstimatedLeftEncoder()) < |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 97 | constants::GetValues().drivetrain_done_distance) && |
| 98 | (::std::abs(goal->right_goal - |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 99 | dt_closedloop_.GetEstimatedRightEncoder()) < |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 100 | constants::GetValues().drivetrain_done_distance)); |
| 101 | } |
| 102 | status->is_done = done; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 103 | status->robot_speed = dt_closedloop_.GetEstimatedRobotSpeed(); |
| 104 | status->filtered_left_position = dt_closedloop_.GetEstimatedLeftEncoder(); |
| 105 | status->filtered_right_position = dt_closedloop_.GetEstimatedRightEncoder(); |
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 | status->filtered_left_velocity = dt_closedloop_.loop().X_hat(1, 0); |
| 108 | status->filtered_right_velocity = dt_closedloop_.loop().X_hat(3, 0); |
| 109 | status->output_was_capped = dt_closedloop_.OutputWasCapped(); |
| 110 | status->uncapped_left_voltage = dt_closedloop_.loop().U_uncapped(0, 0); |
| 111 | status->uncapped_right_voltage = dt_closedloop_.loop().U_uncapped(1, 0); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 115 | } // namespace drivetrain |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 116 | } // namespace control_loops |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 117 | } // namespace y2014 |