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