blob: 9396aed1a06075056a4b62790f51629cbe0a4e66 [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
Austin Schuh3a378462019-01-04 21:48:04 -080041 // Computes the xy state change given the change in the lr state.
42 ::Eigen::Matrix<double, 3, 1> PredictState(
43 const ::Eigen::Matrix<double, 3, 1> &xytheta_state,
44 const ::Eigen::Matrix<double, 7, 1> &state,
45 const ::Eigen::Matrix<double, 7, 1> &previous_state) const;
46
Comran Morshed5323ecb2015-12-26 20:50:55 +000047 double last_gyro_rate_ = 0.0;
48
Austin Schuhbcce26a2018-03-26 23:41:24 -070049 const DrivetrainConfig<double> dt_config_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000050
Diana Burgessd0180f12018-03-21 21:24:17 -070051 StateFeedbackLoop<7, 2, 4> kf_;
Austin Schuhbcce26a2018-03-26 23:41:24 -070052 PolyDrivetrain<double> dt_openloop_;
Austin Schuh41565602016-02-28 20:10:49 -080053 DrivetrainMotorsSS dt_closedloop_;
Austin Schuh9993fb32017-03-15 20:17:46 -070054 ::aos::monotonic_clock::time_point last_gyro_time_ =
55 ::aos::monotonic_clock::min_time;
Comran Morshed5323ecb2015-12-26 20:50:55 +000056
Austin Schuh05c5a612016-04-02 15:10:25 -070057 StateFeedbackLoop<2, 1, 1> down_estimator_;
58 Eigen::Matrix<double, 1, 1> down_U_;
59
Austin Schuh093535c2016-03-05 23:21:00 -080060 // Current gears for each drive side.
61 Gear left_gear_;
62 Gear right_gear_;
63
Comran Morshed5323ecb2015-12-26 20:50:55 +000064 double last_left_voltage_ = 0;
65 double last_right_voltage_ = 0;
66
67 double integrated_kf_heading_ = 0;
Austin Schuh093535c2016-03-05 23:21:00 -080068
69 bool left_high_requested_;
70 bool right_high_requested_;
Austin Schuh5900d142016-04-03 21:35:12 -070071
72 bool has_been_enabled_ = false;
Diana Burgessd0180f12018-03-21 21:24:17 -070073
74 double last_accel_ = 0.0;
Austin Schuh3a378462019-01-04 21:48:04 -080075
76 // Current xytheta state of the robot. This is essentially the kalman filter
77 // integrated up in a direction.
78 // [x, y, theta, vl, vr, left_error, right_error]
79 ::Eigen::Matrix<double, 7, 1> xytheta_state_ =
80 ::Eigen::Matrix<double, 7, 1>::Zero();
81
82 // Last kalman filter state.
83 ::Eigen::Matrix<double, 7, 1> last_state_ =
84 ::Eigen::Matrix<double, 7, 1>::Zero();
Comran Morshed5323ecb2015-12-26 20:50:55 +000085};
86
87} // namespace drivetrain
88} // namespace control_loops
89} // namespace frc971
90
91#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_H_