blob: 1aa3e1349c5638715b150d36ef6fa344b434baf8 [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"
James Kuszmaul3431d622019-02-17 17:07:44 -080012#include "frc971/control_loops/drivetrain/localizer.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000013#include "frc971/control_loops/drivetrain/polydrivetrain.h"
14#include "frc971/control_loops/drivetrain/ssdrivetrain.h"
Alex Perry731b4602019-02-02 22:13:01 -080015#include "frc971/control_loops/drivetrain/splinedrivetrain.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000016
17namespace frc971 {
18namespace control_loops {
19namespace drivetrain {
20
21class DrivetrainLoop : public aos::controls::ControlLoop<
22 ::frc971::control_loops::DrivetrainQueue> {
23 public:
24 // Constructs a control loop which can take a Drivetrain or defaults to the
25 // drivetrain at frc971::control_loops::drivetrain
Adam Snaiderbc918b62016-02-27 21:03:39 -080026 explicit DrivetrainLoop(
Austin Schuh55a13dc2019-01-27 22:39:03 -080027 const DrivetrainConfig<double> &dt_config, ::aos::EventLoop *event_loop,
James Kuszmaul3431d622019-02-17 17:07:44 -080028 LocalizerInterface *localizer,
Austin Schuh55a13dc2019-01-27 22:39:03 -080029 const ::std::string &name = ".frc971.control_loops.drivetrain_queue");
Comran Morshed5323ecb2015-12-26 20:50:55 +000030
Austin Schuh093535c2016-03-05 23:21:00 -080031 int ControllerIndexFromGears();
32
Comran Morshed5323ecb2015-12-26 20:50:55 +000033 protected:
34 // Executes one cycle of the control loop.
Adam Snaiderbc918b62016-02-27 21:03:39 -080035 void RunIteration(
Comran Morshed5323ecb2015-12-26 20:50:55 +000036 const ::frc971::control_loops::DrivetrainQueue::Goal *goal,
37 const ::frc971::control_loops::DrivetrainQueue::Position *position,
38 ::frc971::control_loops::DrivetrainQueue::Output *output,
Adam Snaiderbc918b62016-02-27 21:03:39 -080039 ::frc971::control_loops::DrivetrainQueue::Status *status) override;
40
41 void Zero(::frc971::control_loops::DrivetrainQueue::Output *output) override;
Comran Morshed5323ecb2015-12-26 20:50:55 +000042
Comran Morshed5323ecb2015-12-26 20:50:55 +000043 double last_gyro_rate_ = 0.0;
44
Austin Schuhbcce26a2018-03-26 23:41:24 -070045 const DrivetrainConfig<double> dt_config_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000046
James Kuszmaul3431d622019-02-17 17:07:44 -080047 LocalizerInterface *localizer_;
48
Diana Burgessd0180f12018-03-21 21:24:17 -070049 StateFeedbackLoop<7, 2, 4> kf_;
Austin Schuhbcce26a2018-03-26 23:41:24 -070050 PolyDrivetrain<double> dt_openloop_;
Austin Schuh41565602016-02-28 20:10:49 -080051 DrivetrainMotorsSS dt_closedloop_;
Alex Perry731b4602019-02-02 22:13:01 -080052 SplineDrivetrain dt_spline_;
Austin Schuh9993fb32017-03-15 20:17:46 -070053 ::aos::monotonic_clock::time_point last_gyro_time_ =
54 ::aos::monotonic_clock::min_time;
Comran Morshed5323ecb2015-12-26 20:50:55 +000055
Austin Schuh05c5a612016-04-02 15:10:25 -070056 StateFeedbackLoop<2, 1, 1> down_estimator_;
57 Eigen::Matrix<double, 1, 1> down_U_;
58
Austin Schuh093535c2016-03-05 23:21:00 -080059 // Current gears for each drive side.
60 Gear left_gear_;
61 Gear right_gear_;
62
Comran Morshed5323ecb2015-12-26 20:50:55 +000063 double last_left_voltage_ = 0;
64 double last_right_voltage_ = 0;
65
Austin Schuh093535c2016-03-05 23:21:00 -080066 bool left_high_requested_;
67 bool right_high_requested_;
Austin Schuh5900d142016-04-03 21:35:12 -070068
69 bool has_been_enabled_ = false;
Diana Burgessd0180f12018-03-21 21:24:17 -070070
71 double last_accel_ = 0.0;
Austin Schuh3a378462019-01-04 21:48:04 -080072
Austin Schuh3a378462019-01-04 21:48:04 -080073 // Last kalman filter state.
74 ::Eigen::Matrix<double, 7, 1> last_state_ =
75 ::Eigen::Matrix<double, 7, 1>::Zero();
Comran Morshed5323ecb2015-12-26 20:50:55 +000076};
77
78} // namespace drivetrain
79} // namespace control_loops
80} // namespace frc971
81
82#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_H_