blob: 9f6a34c3899a62158e613a5bfb5c0774a21859bb [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"
14#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
Austin Schuh093535c2016-03-05 23:21:00 -080015#include "frc971/control_loops/drivetrain/gear.h"
James Kuszmaule39cbcf2019-02-27 20:48:34 -080016#include "frc971/control_loops/drivetrain/line_follow_drivetrain.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070017#include "frc971/control_loops/drivetrain/localizer.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070018#include "frc971/control_loops/drivetrain/localizer_generated.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070019#include "frc971/control_loops/drivetrain/polydrivetrain.h"
Alex Perry731b4602019-02-02 22:13:01 -080020#include "frc971/control_loops/drivetrain/splinedrivetrain.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070021#include "frc971/control_loops/drivetrain/ssdrivetrain.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070022#include "frc971/queues/gyro_generated.h"
23#include "frc971/wpilib/imu_generated.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000024
25namespace frc971 {
26namespace control_loops {
27namespace drivetrain {
28
Alex Perrycb7da4b2019-08-28 19:35:56 -070029class DrivetrainLoop
30 : public aos::controls::ControlLoop<Goal, Position, Status, Output> {
Comran Morshed5323ecb2015-12-26 20:50:55 +000031 public:
32 // Constructs a control loop which can take a Drivetrain or defaults to the
33 // drivetrain at frc971::control_loops::drivetrain
Alex Perrycb7da4b2019-08-28 19:35:56 -070034 explicit DrivetrainLoop(const DrivetrainConfig<double> &dt_config,
35 ::aos::EventLoop *event_loop,
36 LocalizerInterface *localizer,
37 const ::std::string &name = "/drivetrain");
Comran Morshed5323ecb2015-12-26 20:50:55 +000038
Austin Schuh093535c2016-03-05 23:21:00 -080039 int ControllerIndexFromGears();
40
Comran Morshed5323ecb2015-12-26 20:50:55 +000041 protected:
42 // Executes one cycle of the control loop.
Adam Snaiderbc918b62016-02-27 21:03:39 -080043 void RunIteration(
Alex Perrycb7da4b2019-08-28 19:35:56 -070044 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 Snaiderbc918b62016-02-27 21:03:39 -080049
Alex Perrycb7da4b2019-08-28 19:35:56 -070050 flatbuffers::Offset<drivetrain::Output> Zero(
51 aos::Sender<drivetrain::Output>::Builder *builder) override;
Comran Morshed5323ecb2015-12-26 20:50:55 +000052
Comran Morshed5323ecb2015-12-26 20:50:55 +000053 double last_gyro_rate_ = 0.0;
54
Austin Schuhbcce26a2018-03-26 23:41:24 -070055 const DrivetrainConfig<double> dt_config_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000056
James Kuszmaulef428a02019-03-02 22:19:41 -080057 ::aos::Fetcher<LocalizerControl> localizer_control_fetcher_;
Austin Schuh73b6e3b2019-05-27 16:37:15 -070058 ::aos::Fetcher<::frc971::IMUValues> imu_values_fetcher_;
Austin Schuh1ea89bb2019-05-27 16:59:59 -070059 ::aos::Fetcher<::frc971::sensors::GyroReading> gyro_reading_fetcher_;
James Kuszmaul3431d622019-02-17 17:07:44 -080060 LocalizerInterface *localizer_;
61
Diana Burgessd0180f12018-03-21 21:24:17 -070062 StateFeedbackLoop<7, 2, 4> kf_;
Austin Schuhbcce26a2018-03-26 23:41:24 -070063 PolyDrivetrain<double> dt_openloop_;
Austin Schuh41565602016-02-28 20:10:49 -080064 DrivetrainMotorsSS dt_closedloop_;
Alex Perry731b4602019-02-02 22:13:01 -080065 SplineDrivetrain dt_spline_;
James Kuszmaule39cbcf2019-02-27 20:48:34 -080066 LineFollowDrivetrain dt_line_follow_;
Austin Schuh9993fb32017-03-15 20:17:46 -070067 ::aos::monotonic_clock::time_point last_gyro_time_ =
68 ::aos::monotonic_clock::min_time;
Comran Morshed5323ecb2015-12-26 20:50:55 +000069
Austin Schuh05c5a612016-04-02 15:10:25 -070070 StateFeedbackLoop<2, 1, 1> down_estimator_;
71 Eigen::Matrix<double, 1, 1> down_U_;
72
Austin Schuh093535c2016-03-05 23:21:00 -080073 // Current gears for each drive side.
74 Gear left_gear_;
75 Gear right_gear_;
76
Comran Morshed5323ecb2015-12-26 20:50:55 +000077 double last_left_voltage_ = 0;
78 double last_right_voltage_ = 0;
James Kuszmaulf3add3c2019-03-02 14:55:00 -080079 // The left/right voltages previous to last_*_voltage_.
80 double last_last_left_voltage_ = 0;
81 double last_last_right_voltage_ = 0;
Comran Morshed5323ecb2015-12-26 20:50:55 +000082
Austin Schuh093535c2016-03-05 23:21:00 -080083 bool left_high_requested_;
84 bool right_high_requested_;
Austin Schuh5900d142016-04-03 21:35:12 -070085
86 bool has_been_enabled_ = false;
Diana Burgessd0180f12018-03-21 21:24:17 -070087
88 double last_accel_ = 0.0;
Austin Schuh3a378462019-01-04 21:48:04 -080089
Austin Schuh3a378462019-01-04 21:48:04 -080090 // Last kalman filter state.
91 ::Eigen::Matrix<double, 7, 1> last_state_ =
92 ::Eigen::Matrix<double, 7, 1>::Zero();
Comran Morshed5323ecb2015-12-26 20:50:55 +000093};
94
95} // namespace drivetrain
96} // namespace control_loops
97} // namespace frc971
98
99#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_H_