blob: 05bf711bda95ad09604659c40fa77c247f46c18c [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"
Alex Perry731b4602019-02-02 22:13:01 -080014#include "frc971/control_loops/drivetrain/splinedrivetrain.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000015
16namespace frc971 {
17namespace control_loops {
18namespace drivetrain {
19
20class DrivetrainLoop : public aos::controls::ControlLoop<
21 ::frc971::control_loops::DrivetrainQueue> {
22 public:
23 // Constructs a control loop which can take a Drivetrain or defaults to the
24 // drivetrain at frc971::control_loops::drivetrain
Adam Snaiderbc918b62016-02-27 21:03:39 -080025 explicit DrivetrainLoop(
Austin Schuh55a13dc2019-01-27 22:39:03 -080026 const DrivetrainConfig<double> &dt_config, ::aos::EventLoop *event_loop,
27 const ::std::string &name = ".frc971.control_loops.drivetrain_queue");
Comran Morshed5323ecb2015-12-26 20:50:55 +000028
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_;
Alex Perry731b4602019-02-02 22:13:01 -080054 SplineDrivetrain dt_spline_;
Austin Schuh9993fb32017-03-15 20:17:46 -070055 ::aos::monotonic_clock::time_point last_gyro_time_ =
56 ::aos::monotonic_clock::min_time;
Comran Morshed5323ecb2015-12-26 20:50:55 +000057
Austin Schuh05c5a612016-04-02 15:10:25 -070058 StateFeedbackLoop<2, 1, 1> down_estimator_;
59 Eigen::Matrix<double, 1, 1> down_U_;
60
Austin Schuh093535c2016-03-05 23:21:00 -080061 // Current gears for each drive side.
62 Gear left_gear_;
63 Gear right_gear_;
64
Comran Morshed5323ecb2015-12-26 20:50:55 +000065 double last_left_voltage_ = 0;
66 double last_right_voltage_ = 0;
67
68 double integrated_kf_heading_ = 0;
Austin Schuh093535c2016-03-05 23:21:00 -080069
70 bool left_high_requested_;
71 bool right_high_requested_;
Austin Schuh5900d142016-04-03 21:35:12 -070072
73 bool has_been_enabled_ = false;
Diana Burgessd0180f12018-03-21 21:24:17 -070074
75 double last_accel_ = 0.0;
Austin Schuh3a378462019-01-04 21:48:04 -080076
77 // Current xytheta state of the robot. This is essentially the kalman filter
78 // integrated up in a direction.
79 // [x, y, theta, vl, vr, left_error, right_error]
80 ::Eigen::Matrix<double, 7, 1> xytheta_state_ =
81 ::Eigen::Matrix<double, 7, 1>::Zero();
82
83 // Last kalman filter state.
84 ::Eigen::Matrix<double, 7, 1> last_state_ =
85 ::Eigen::Matrix<double, 7, 1>::Zero();
Comran Morshed5323ecb2015-12-26 20:50:55 +000086};
87
88} // namespace drivetrain
89} // namespace control_loops
90} // namespace frc971
91
92#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_H_