blob: a357eab07a4f5f30346cb82b7cee746e0cd52b1f [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 Schuh55a13dc2019-01-27 22:39:03 -080025 const DrivetrainConfig<double> &dt_config, ::aos::EventLoop *event_loop,
26 const ::std::string &name = ".frc971.control_loops.drivetrain_queue");
Comran Morshed5323ecb2015-12-26 20:50:55 +000027
Austin Schuh093535c2016-03-05 23:21:00 -080028 int ControllerIndexFromGears();
29
Comran Morshed5323ecb2015-12-26 20:50:55 +000030 protected:
31 // Executes one cycle of the control loop.
Adam Snaiderbc918b62016-02-27 21:03:39 -080032 void RunIteration(
Comran Morshed5323ecb2015-12-26 20:50:55 +000033 const ::frc971::control_loops::DrivetrainQueue::Goal *goal,
34 const ::frc971::control_loops::DrivetrainQueue::Position *position,
35 ::frc971::control_loops::DrivetrainQueue::Output *output,
Adam Snaiderbc918b62016-02-27 21:03:39 -080036 ::frc971::control_loops::DrivetrainQueue::Status *status) override;
37
38 void Zero(::frc971::control_loops::DrivetrainQueue::Output *output) override;
Comran Morshed5323ecb2015-12-26 20:50:55 +000039
Austin Schuh3a378462019-01-04 21:48:04 -080040 // Computes the xy state change given the change in the lr state.
41 ::Eigen::Matrix<double, 3, 1> PredictState(
42 const ::Eigen::Matrix<double, 3, 1> &xytheta_state,
43 const ::Eigen::Matrix<double, 7, 1> &state,
44 const ::Eigen::Matrix<double, 7, 1> &previous_state) const;
45
Comran Morshed5323ecb2015-12-26 20:50:55 +000046 double last_gyro_rate_ = 0.0;
47
Austin Schuhbcce26a2018-03-26 23:41:24 -070048 const DrivetrainConfig<double> dt_config_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000049
Diana Burgessd0180f12018-03-21 21:24:17 -070050 StateFeedbackLoop<7, 2, 4> kf_;
Austin Schuhbcce26a2018-03-26 23:41:24 -070051 PolyDrivetrain<double> dt_openloop_;
Austin Schuh41565602016-02-28 20:10:49 -080052 DrivetrainMotorsSS dt_closedloop_;
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
66 double integrated_kf_heading_ = 0;
Austin Schuh093535c2016-03-05 23:21:00 -080067
68 bool left_high_requested_;
69 bool right_high_requested_;
Austin Schuh5900d142016-04-03 21:35:12 -070070
71 bool has_been_enabled_ = false;
Diana Burgessd0180f12018-03-21 21:24:17 -070072
73 double last_accel_ = 0.0;
Austin Schuh3a378462019-01-04 21:48:04 -080074
75 // Current xytheta state of the robot. This is essentially the kalman filter
76 // integrated up in a direction.
77 // [x, y, theta, vl, vr, left_error, right_error]
78 ::Eigen::Matrix<double, 7, 1> xytheta_state_ =
79 ::Eigen::Matrix<double, 7, 1>::Zero();
80
81 // Last kalman filter state.
82 ::Eigen::Matrix<double, 7, 1> last_state_ =
83 ::Eigen::Matrix<double, 7, 1>::Zero();
Comran Morshed5323ecb2015-12-26 20:50:55 +000084};
85
86} // namespace drivetrain
87} // namespace control_loops
88} // namespace frc971
89
90#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_H_