blob: 3f6c32f689d989bbc1651dd8d38d613c4095cf2f [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"
Comran Morshed5323ecb2015-12-26 20:50:55 +00009#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh093535c2016-03-05 23:21:00 -080010#include "frc971/control_loops/drivetrain/drivetrain_config.h"
11#include "frc971/control_loops/drivetrain/gear.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000012#include "frc971/control_loops/drivetrain/polydrivetrain.h"
13#include "frc971/control_loops/drivetrain/ssdrivetrain.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000014
15namespace frc971 {
16namespace control_loops {
17namespace drivetrain {
18
19class DrivetrainLoop : public aos::controls::ControlLoop<
20 ::frc971::control_loops::DrivetrainQueue> {
21 public:
22 // Constructs a control loop which can take a Drivetrain or defaults to the
23 // drivetrain at frc971::control_loops::drivetrain
Adam Snaiderbc918b62016-02-27 21:03:39 -080024 explicit DrivetrainLoop(
Austin Schuhbcce26a2018-03-26 23:41:24 -070025 const DrivetrainConfig<double> &dt_config,
Comran Morshed5323ecb2015-12-26 20:50:55 +000026 ::frc971::control_loops::DrivetrainQueue *my_drivetrain =
27 &::frc971::control_loops::drivetrain_queue);
28
Austin Schuh093535c2016-03-05 23:21:00 -080029 int ControllerIndexFromGears();
30
Comran Morshed5323ecb2015-12-26 20:50:55 +000031 protected:
32 // Executes one cycle of the control loop.
Adam Snaiderbc918b62016-02-27 21:03:39 -080033 void RunIteration(
Comran Morshed5323ecb2015-12-26 20:50:55 +000034 const ::frc971::control_loops::DrivetrainQueue::Goal *goal,
35 const ::frc971::control_loops::DrivetrainQueue::Position *position,
36 ::frc971::control_loops::DrivetrainQueue::Output *output,
Adam Snaiderbc918b62016-02-27 21:03:39 -080037 ::frc971::control_loops::DrivetrainQueue::Status *status) override;
38
39 void Zero(::frc971::control_loops::DrivetrainQueue::Output *output) override;
Comran Morshed5323ecb2015-12-26 20:50:55 +000040
Comran Morshed5323ecb2015-12-26 20:50:55 +000041 double last_gyro_rate_ = 0.0;
42
Austin Schuhbcce26a2018-03-26 23:41:24 -070043 const DrivetrainConfig<double> dt_config_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000044
Diana Burgessd0180f12018-03-21 21:24:17 -070045 StateFeedbackLoop<7, 2, 4> kf_;
Austin Schuhbcce26a2018-03-26 23:41:24 -070046 PolyDrivetrain<double> dt_openloop_;
Austin Schuh41565602016-02-28 20:10:49 -080047 DrivetrainMotorsSS dt_closedloop_;
Austin Schuh9993fb32017-03-15 20:17:46 -070048 ::aos::monotonic_clock::time_point last_gyro_time_ =
49 ::aos::monotonic_clock::min_time;
Comran Morshed5323ecb2015-12-26 20:50:55 +000050
Austin Schuh05c5a612016-04-02 15:10:25 -070051 StateFeedbackLoop<2, 1, 1> down_estimator_;
52 Eigen::Matrix<double, 1, 1> down_U_;
53
Austin Schuh093535c2016-03-05 23:21:00 -080054 // Current gears for each drive side.
55 Gear left_gear_;
56 Gear right_gear_;
57
Comran Morshed5323ecb2015-12-26 20:50:55 +000058 double last_left_voltage_ = 0;
59 double last_right_voltage_ = 0;
60
61 double integrated_kf_heading_ = 0;
Austin Schuh093535c2016-03-05 23:21:00 -080062
63 bool left_high_requested_;
64 bool right_high_requested_;
Austin Schuh5900d142016-04-03 21:35:12 -070065
66 bool has_been_enabled_ = false;
Diana Burgessd0180f12018-03-21 21:24:17 -070067
68 double last_accel_ = 0.0;
Comran Morshed5323ecb2015-12-26 20:50:55 +000069};
70
71} // namespace drivetrain
72} // namespace control_loops
73} // namespace frc971
74
75#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_H_