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 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 6 | #include "aos/common/controls/control_loop.h" |
| 7 | #include "aos/common/controls/polytope.h" |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 8 | #include "aos/common/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 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 41 | double last_gyro_rate_ = 0.0; |
| 42 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 43 | const DrivetrainConfig<double> dt_config_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 44 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 45 | StateFeedbackLoop<7, 2, 4> kf_; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 46 | PolyDrivetrain<double> dt_openloop_; |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 47 | DrivetrainMotorsSS dt_closedloop_; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 48 | ::aos::monotonic_clock::time_point last_gyro_time_ = |
| 49 | ::aos::monotonic_clock::min_time; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 50 | |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 51 | StateFeedbackLoop<2, 1, 1> down_estimator_; |
| 52 | Eigen::Matrix<double, 1, 1> down_U_; |
| 53 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 54 | // Current gears for each drive side. |
| 55 | Gear left_gear_; |
| 56 | Gear right_gear_; |
| 57 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 58 | double last_left_voltage_ = 0; |
| 59 | double last_right_voltage_ = 0; |
| 60 | |
| 61 | double integrated_kf_heading_ = 0; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 62 | |
| 63 | bool left_high_requested_; |
| 64 | bool right_high_requested_; |
Austin Schuh | 5900d14 | 2016-04-03 21:35:12 -0700 | [diff] [blame] | 65 | |
| 66 | bool has_been_enabled_ = false; |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 67 | |
| 68 | double last_accel_ = 0.0; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | } // namespace drivetrain |
| 72 | } // namespace control_loops |
| 73 | } // namespace frc971 |
| 74 | |
| 75 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_H_ |