Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_DRIVETRAIN_H_ |
| 3 | |
| 4 | #include "Eigen/Dense" |
| 5 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/controls/control_loop.h" |
| 7 | #include "aos/controls/polytope.h" |
| 8 | #include "aos/util/log_interval.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 9 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
| 11 | #include "frc971/control_loops/drivetrain/gear.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 12 | #include "frc971/control_loops/drivetrain/polydrivetrain.h" |
| 13 | #include "frc971/control_loops/drivetrain/ssdrivetrain.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 14 | |
| 15 | namespace frc971 { |
| 16 | namespace control_loops { |
| 17 | namespace drivetrain { |
| 18 | |
| 19 | class DrivetrainLoop : public aos::controls::ControlLoop< |
| 20 | ::frc971::control_loops::DrivetrainQueue> { |
| 21 | public: |
| 22 | // Constructs a control loop which can take a Drivetrain or defaults to the |
| 23 | // drivetrain at frc971::control_loops::drivetrain |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 24 | explicit DrivetrainLoop( |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 25 | const DrivetrainConfig<double> &dt_config, |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 26 | ::frc971::control_loops::DrivetrainQueue *my_drivetrain = |
| 27 | &::frc971::control_loops::drivetrain_queue); |
| 28 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 29 | int ControllerIndexFromGears(); |
| 30 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 31 | protected: |
| 32 | // Executes one cycle of the control loop. |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 33 | void RunIteration( |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 34 | const ::frc971::control_loops::DrivetrainQueue::Goal *goal, |
| 35 | const ::frc971::control_loops::DrivetrainQueue::Position *position, |
| 36 | ::frc971::control_loops::DrivetrainQueue::Output *output, |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 37 | ::frc971::control_loops::DrivetrainQueue::Status *status) override; |
| 38 | |
| 39 | void Zero(::frc971::control_loops::DrivetrainQueue::Output *output) override; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 40 | |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame^] | 41 | // Computes the xy state change given the change in the lr state. |
| 42 | ::Eigen::Matrix<double, 3, 1> PredictState( |
| 43 | const ::Eigen::Matrix<double, 3, 1> &xytheta_state, |
| 44 | const ::Eigen::Matrix<double, 7, 1> &state, |
| 45 | const ::Eigen::Matrix<double, 7, 1> &previous_state) const; |
| 46 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 47 | double last_gyro_rate_ = 0.0; |
| 48 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 49 | const DrivetrainConfig<double> dt_config_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 50 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 51 | StateFeedbackLoop<7, 2, 4> kf_; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 52 | PolyDrivetrain<double> dt_openloop_; |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 53 | DrivetrainMotorsSS dt_closedloop_; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 54 | ::aos::monotonic_clock::time_point last_gyro_time_ = |
| 55 | ::aos::monotonic_clock::min_time; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 56 | |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 57 | StateFeedbackLoop<2, 1, 1> down_estimator_; |
| 58 | Eigen::Matrix<double, 1, 1> down_U_; |
| 59 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 60 | // Current gears for each drive side. |
| 61 | Gear left_gear_; |
| 62 | Gear right_gear_; |
| 63 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 64 | double last_left_voltage_ = 0; |
| 65 | double last_right_voltage_ = 0; |
| 66 | |
| 67 | double integrated_kf_heading_ = 0; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 68 | |
| 69 | bool left_high_requested_; |
| 70 | bool right_high_requested_; |
Austin Schuh | 5900d14 | 2016-04-03 21:35:12 -0700 | [diff] [blame] | 71 | |
| 72 | bool has_been_enabled_ = false; |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 73 | |
| 74 | double last_accel_ = 0.0; |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame^] | 75 | |
| 76 | // Current xytheta state of the robot. This is essentially the kalman filter |
| 77 | // integrated up in a direction. |
| 78 | // [x, y, theta, vl, vr, left_error, right_error] |
| 79 | ::Eigen::Matrix<double, 7, 1> xytheta_state_ = |
| 80 | ::Eigen::Matrix<double, 7, 1>::Zero(); |
| 81 | |
| 82 | // Last kalman filter state. |
| 83 | ::Eigen::Matrix<double, 7, 1> last_state_ = |
| 84 | ::Eigen::Matrix<double, 7, 1>::Zero(); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | } // namespace drivetrain |
| 88 | } // namespace control_loops |
| 89 | } // namespace frc971 |
| 90 | |
| 91 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_H_ |