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