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