blob: f282cc0a08e6b7a2305b2ff74c375633b67fdd9e [file] [log] [blame]
Comran Morshed5323ecb2015-12-26 20:50:55 +00001#ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_H_
2#define FRC971_CONTROL_LOOPS_DRIVETRAIN_H_
3
4#include "Eigen/Dense"
5
John Park33858a32018-09-28 23:05:48 -07006#include "aos/controls/control_loop.h"
7#include "aos/controls/polytope.h"
8#include "aos/util/log_interval.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#include "frc971/control_loops/control_loops_generated.h"
Austin Schuh093535c2016-03-05 23:21:00 -080010#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011#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"
Austin Schuh95771d92021-01-23 14:42:25 -080014#include "frc971/control_loops/drivetrain/drivetrain_states.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070015#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
Austin Schuh093535c2016-03-05 23:21:00 -080016#include "frc971/control_loops/drivetrain/gear.h"
James Kuszmaul3e1bb272020-01-17 18:38:19 -080017#include "frc971/control_loops/drivetrain/improved_down_estimator.h"
James Kuszmaule39cbcf2019-02-27 20:48:34 -080018#include "frc971/control_loops/drivetrain/line_follow_drivetrain.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070019#include "frc971/control_loops/drivetrain/localizer.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070020#include "frc971/control_loops/drivetrain/localizer_generated.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070021#include "frc971/control_loops/drivetrain/polydrivetrain.h"
Alex Perry731b4602019-02-02 22:13:01 -080022#include "frc971/control_loops/drivetrain/splinedrivetrain.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070023#include "frc971/control_loops/drivetrain/ssdrivetrain.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070024#include "frc971/queues/gyro_generated.h"
Austin Schuhac17fba2020-03-28 15:55:33 -070025#include "frc971/wpilib/imu_batch_generated.h"
James Kuszmaul3e1bb272020-01-17 18:38:19 -080026#include "frc971/zeroing/imu_zeroer.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000027
28namespace frc971 {
29namespace control_loops {
30namespace drivetrain {
31
Alex Perrycb7da4b2019-08-28 19:35:56 -070032class DrivetrainLoop
33 : public aos::controls::ControlLoop<Goal, Position, Status, Output> {
Comran Morshed5323ecb2015-12-26 20:50:55 +000034 public:
35 // Constructs a control loop which can take a Drivetrain or defaults to the
36 // drivetrain at frc971::control_loops::drivetrain
Alex Perrycb7da4b2019-08-28 19:35:56 -070037 explicit DrivetrainLoop(const DrivetrainConfig<double> &dt_config,
38 ::aos::EventLoop *event_loop,
39 LocalizerInterface *localizer,
40 const ::std::string &name = "/drivetrain");
Comran Morshed5323ecb2015-12-26 20:50:55 +000041
James Kuszmaul531609d2020-02-18 17:12:23 -080042 virtual ~DrivetrainLoop() {}
43
Austin Schuh093535c2016-03-05 23:21:00 -080044 int ControllerIndexFromGears();
45
Comran Morshed5323ecb2015-12-26 20:50:55 +000046 protected:
47 // Executes one cycle of the control loop.
Adam Snaiderbc918b62016-02-27 21:03:39 -080048 void RunIteration(
Alex Perrycb7da4b2019-08-28 19:35:56 -070049 const ::frc971::control_loops::drivetrain::Goal *goal,
50 const ::frc971::control_loops::drivetrain::Position *position,
51 aos::Sender<::frc971::control_loops::drivetrain::Output>::Builder *output,
52 aos::Sender<::frc971::control_loops::drivetrain::Status>::Builder *status)
53 override;
Adam Snaiderbc918b62016-02-27 21:03:39 -080054
Alex Perrycb7da4b2019-08-28 19:35:56 -070055 flatbuffers::Offset<drivetrain::Output> Zero(
56 aos::Sender<drivetrain::Output>::Builder *builder) override;
Comran Morshed5323ecb2015-12-26 20:50:55 +000057
Comran Morshed5323ecb2015-12-26 20:50:55 +000058 double last_gyro_rate_ = 0.0;
59
Austin Schuhbcce26a2018-03-26 23:41:24 -070060 const DrivetrainConfig<double> dt_config_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000061
James Kuszmaulef428a02019-03-02 22:19:41 -080062 ::aos::Fetcher<LocalizerControl> localizer_control_fetcher_;
Austin Schuhac17fba2020-03-28 15:55:33 -070063 ::aos::Fetcher<::frc971::IMUValuesBatch> imu_values_fetcher_;
Austin Schuh1ea89bb2019-05-27 16:59:59 -070064 ::aos::Fetcher<::frc971::sensors::GyroReading> gyro_reading_fetcher_;
James Kuszmaul3e1bb272020-01-17 18:38:19 -080065
66 zeroing::ImuZeroer imu_zeroer_;
67 DrivetrainUkf down_estimator_;
68 aos::monotonic_clock::time_point last_imu_update_ =
69 aos::monotonic_clock::min_time;
James Kuszmaul3431d622019-02-17 17:07:44 -080070 LocalizerInterface *localizer_;
71
Diana Burgessd0180f12018-03-21 21:24:17 -070072 StateFeedbackLoop<7, 2, 4> kf_;
Austin Schuhbcce26a2018-03-26 23:41:24 -070073 PolyDrivetrain<double> dt_openloop_;
Austin Schuh41565602016-02-28 20:10:49 -080074 DrivetrainMotorsSS dt_closedloop_;
Alex Perry731b4602019-02-02 22:13:01 -080075 SplineDrivetrain dt_spline_;
James Kuszmaule39cbcf2019-02-27 20:48:34 -080076 LineFollowDrivetrain dt_line_follow_;
Austin Schuh9993fb32017-03-15 20:17:46 -070077 ::aos::monotonic_clock::time_point last_gyro_time_ =
78 ::aos::monotonic_clock::min_time;
Comran Morshed5323ecb2015-12-26 20:50:55 +000079
Austin Schuh093535c2016-03-05 23:21:00 -080080 // Current gears for each drive side.
81 Gear left_gear_;
82 Gear right_gear_;
83
Comran Morshed5323ecb2015-12-26 20:50:55 +000084 double last_left_voltage_ = 0;
85 double last_right_voltage_ = 0;
James Kuszmaulf3add3c2019-03-02 14:55:00 -080086 // The left/right voltages previous to last_*_voltage_.
87 double last_last_left_voltage_ = 0;
88 double last_last_right_voltage_ = 0;
Comran Morshed5323ecb2015-12-26 20:50:55 +000089
Austin Schuh093535c2016-03-05 23:21:00 -080090 bool left_high_requested_;
91 bool right_high_requested_;
Austin Schuh5900d142016-04-03 21:35:12 -070092
93 bool has_been_enabled_ = false;
Diana Burgessd0180f12018-03-21 21:24:17 -070094
95 double last_accel_ = 0.0;
Austin Schuh3a378462019-01-04 21:48:04 -080096
Austin Schuh3a378462019-01-04 21:48:04 -080097 // Last kalman filter state.
98 ::Eigen::Matrix<double, 7, 1> last_state_ =
99 ::Eigen::Matrix<double, 7, 1>::Zero();
Comran Morshed5323ecb2015-12-26 20:50:55 +0000100};
101
102} // namespace drivetrain
103} // namespace control_loops
104} // namespace frc971
105
106#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_H_