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