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" |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 14 | #include "frc971/control_loops/drivetrain/splinedrivetrain.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 15 | |
| 16 | namespace frc971 { |
| 17 | namespace control_loops { |
| 18 | namespace drivetrain { |
| 19 | |
| 20 | class DrivetrainLoop : public aos::controls::ControlLoop< |
| 21 | ::frc971::control_loops::DrivetrainQueue> { |
| 22 | public: |
| 23 | // Constructs a control loop which can take a Drivetrain or defaults to the |
| 24 | // drivetrain at frc971::control_loops::drivetrain |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 25 | explicit DrivetrainLoop( |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame] | 26 | const DrivetrainConfig<double> &dt_config, ::aos::EventLoop *event_loop, |
| 27 | const ::std::string &name = ".frc971.control_loops.drivetrain_queue"); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 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_; |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 54 | SplineDrivetrain dt_spline_; |
Austin Schuh | 9993fb3 | 2017-03-15 20:17:46 -0700 | [diff] [blame] | 55 | ::aos::monotonic_clock::time_point last_gyro_time_ = |
| 56 | ::aos::monotonic_clock::min_time; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 57 | |
Austin Schuh | 05c5a61 | 2016-04-02 15:10:25 -0700 | [diff] [blame] | 58 | StateFeedbackLoop<2, 1, 1> down_estimator_; |
| 59 | Eigen::Matrix<double, 1, 1> down_U_; |
| 60 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 61 | // Current gears for each drive side. |
| 62 | Gear left_gear_; |
| 63 | Gear right_gear_; |
| 64 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 65 | double last_left_voltage_ = 0; |
| 66 | double last_right_voltage_ = 0; |
| 67 | |
| 68 | double integrated_kf_heading_ = 0; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 69 | |
| 70 | bool left_high_requested_; |
| 71 | bool right_high_requested_; |
Austin Schuh | 5900d14 | 2016-04-03 21:35:12 -0700 | [diff] [blame] | 72 | |
| 73 | bool has_been_enabled_ = false; |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 74 | |
| 75 | double last_accel_ = 0.0; |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 76 | |
| 77 | // Current xytheta state of the robot. This is essentially the kalman filter |
| 78 | // integrated up in a direction. |
| 79 | // [x, y, theta, vl, vr, left_error, right_error] |
| 80 | ::Eigen::Matrix<double, 7, 1> xytheta_state_ = |
| 81 | ::Eigen::Matrix<double, 7, 1>::Zero(); |
| 82 | |
| 83 | // Last kalman filter state. |
| 84 | ::Eigen::Matrix<double, 7, 1> last_state_ = |
| 85 | ::Eigen::Matrix<double, 7, 1>::Zero(); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // namespace drivetrain |
| 89 | } // namespace control_loops |
| 90 | } // namespace frc971 |
| 91 | |
| 92 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_H_ |