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" |
James Kuszmaul | e39cbcf | 2019-02-27 20:48:34 -0800 | [diff] [blame] | 12 | #include "frc971/control_loops/drivetrain/line_follow_drivetrain.h" |
Austin Schuh | 73b6e3b | 2019-05-27 16:37:15 -0700 | [diff] [blame^] | 13 | #include "frc971/control_loops/drivetrain/localizer.h" |
James Kuszmaul | ef428a0 | 2019-03-02 22:19:41 -0800 | [diff] [blame] | 14 | #include "frc971/control_loops/drivetrain/localizer.q.h" |
Austin Schuh | 73b6e3b | 2019-05-27 16:37:15 -0700 | [diff] [blame^] | 15 | #include "frc971/control_loops/drivetrain/polydrivetrain.h" |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 16 | #include "frc971/control_loops/drivetrain/splinedrivetrain.h" |
Austin Schuh | 73b6e3b | 2019-05-27 16:37:15 -0700 | [diff] [blame^] | 17 | #include "frc971/control_loops/drivetrain/ssdrivetrain.h" |
| 18 | #include "frc971/wpilib/imu.q.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 19 | |
| 20 | namespace frc971 { |
| 21 | namespace control_loops { |
| 22 | namespace drivetrain { |
| 23 | |
| 24 | class DrivetrainLoop : public aos::controls::ControlLoop< |
| 25 | ::frc971::control_loops::DrivetrainQueue> { |
| 26 | public: |
| 27 | // Constructs a control loop which can take a Drivetrain or defaults to the |
| 28 | // drivetrain at frc971::control_loops::drivetrain |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 29 | explicit DrivetrainLoop( |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame] | 30 | const DrivetrainConfig<double> &dt_config, ::aos::EventLoop *event_loop, |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 31 | LocalizerInterface *localizer, |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame] | 32 | const ::std::string &name = ".frc971.control_loops.drivetrain_queue"); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 33 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 34 | int ControllerIndexFromGears(); |
| 35 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 36 | protected: |
| 37 | // Executes one cycle of the control loop. |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 38 | void RunIteration( |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 39 | const ::frc971::control_loops::DrivetrainQueue::Goal *goal, |
| 40 | const ::frc971::control_loops::DrivetrainQueue::Position *position, |
| 41 | ::frc971::control_loops::DrivetrainQueue::Output *output, |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 42 | ::frc971::control_loops::DrivetrainQueue::Status *status) override; |
| 43 | |
| 44 | void Zero(::frc971::control_loops::DrivetrainQueue::Output *output) override; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 45 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 46 | double last_gyro_rate_ = 0.0; |
| 47 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 48 | const DrivetrainConfig<double> dt_config_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 49 | |
James Kuszmaul | ef428a0 | 2019-03-02 22:19:41 -0800 | [diff] [blame] | 50 | ::aos::Fetcher<LocalizerControl> localizer_control_fetcher_; |
Austin Schuh | 73b6e3b | 2019-05-27 16:37:15 -0700 | [diff] [blame^] | 51 | ::aos::Fetcher<::frc971::IMUValues> imu_values_fetcher_; |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 52 | LocalizerInterface *localizer_; |
| 53 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 54 | StateFeedbackLoop<7, 2, 4> kf_; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 55 | PolyDrivetrain<double> dt_openloop_; |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 56 | DrivetrainMotorsSS dt_closedloop_; |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 57 | SplineDrivetrain dt_spline_; |
James Kuszmaul | e39cbcf | 2019-02-27 20:48:34 -0800 | [diff] [blame] | 58 | LineFollowDrivetrain dt_line_follow_; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 59 | ::aos::monotonic_clock::time_point last_gyro_time_ = |
| 60 | ::aos::monotonic_clock::min_time; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 61 | |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 62 | StateFeedbackLoop<2, 1, 1> down_estimator_; |
| 63 | Eigen::Matrix<double, 1, 1> down_U_; |
| 64 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 65 | // Current gears for each drive side. |
| 66 | Gear left_gear_; |
| 67 | Gear right_gear_; |
| 68 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 69 | double last_left_voltage_ = 0; |
| 70 | double last_right_voltage_ = 0; |
James Kuszmaul | f3add3c | 2019-03-02 14:55:00 -0800 | [diff] [blame] | 71 | // The left/right voltages previous to last_*_voltage_. |
| 72 | double last_last_left_voltage_ = 0; |
| 73 | double last_last_right_voltage_ = 0; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 74 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 75 | bool left_high_requested_; |
| 76 | bool right_high_requested_; |
Austin Schuh | 5900d14 | 2016-04-03 21:35:12 -0700 | [diff] [blame] | 77 | |
| 78 | bool has_been_enabled_ = false; |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 79 | |
| 80 | double last_accel_ = 0.0; |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 81 | |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 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_ |