blob: b766db8852514cceab12a40effad51506175bde8 [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"
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#include "frc971/control_loops/control_loops_generated.h"
Austin Schuh093535c2016-03-05 23:21:00 -080010#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
12#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
13#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
14#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
Austin Schuh093535c2016-03-05 23:21:00 -080015#include "frc971/control_loops/drivetrain/gear.h"
James Kuszmaul3e1bb272020-01-17 18:38:19 -080016#include "frc971/control_loops/drivetrain/improved_down_estimator.h"
James Kuszmaule39cbcf2019-02-27 20:48:34 -080017#include "frc971/control_loops/drivetrain/line_follow_drivetrain.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070018#include "frc971/control_loops/drivetrain/localizer.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070019#include "frc971/control_loops/drivetrain/localizer_generated.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070020#include "frc971/control_loops/drivetrain/polydrivetrain.h"
Alex Perry731b4602019-02-02 22:13:01 -080021#include "frc971/control_loops/drivetrain/splinedrivetrain.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070022#include "frc971/control_loops/drivetrain/ssdrivetrain.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070023#include "frc971/queues/gyro_generated.h"
24#include "frc971/wpilib/imu_generated.h"
James Kuszmaul3e1bb272020-01-17 18:38:19 -080025#include "frc971/zeroing/imu_zeroer.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000026
27namespace frc971 {
28namespace control_loops {
29namespace drivetrain {
30
Alex Perrycb7da4b2019-08-28 19:35:56 -070031class DrivetrainLoop
32 : public aos::controls::ControlLoop<Goal, Position, Status, Output> {
Comran Morshed5323ecb2015-12-26 20:50:55 +000033 public:
34 // Constructs a control loop which can take a Drivetrain or defaults to the
35 // drivetrain at frc971::control_loops::drivetrain
Alex Perrycb7da4b2019-08-28 19:35:56 -070036 explicit DrivetrainLoop(const DrivetrainConfig<double> &dt_config,
37 ::aos::EventLoop *event_loop,
38 LocalizerInterface *localizer,
39 const ::std::string &name = "/drivetrain");
Comran Morshed5323ecb2015-12-26 20:50:55 +000040
James Kuszmaul531609d2020-02-18 17:12:23 -080041 virtual ~DrivetrainLoop() {}
42
Austin Schuh093535c2016-03-05 23:21:00 -080043 int ControllerIndexFromGears();
44
Comran Morshed5323ecb2015-12-26 20:50:55 +000045 protected:
46 // Executes one cycle of the control loop.
Adam Snaiderbc918b62016-02-27 21:03:39 -080047 void RunIteration(
Alex Perrycb7da4b2019-08-28 19:35:56 -070048 const ::frc971::control_loops::drivetrain::Goal *goal,
49 const ::frc971::control_loops::drivetrain::Position *position,
50 aos::Sender<::frc971::control_loops::drivetrain::Output>::Builder *output,
51 aos::Sender<::frc971::control_loops::drivetrain::Status>::Builder *status)
52 override;
Adam Snaiderbc918b62016-02-27 21:03:39 -080053
Alex Perrycb7da4b2019-08-28 19:35:56 -070054 flatbuffers::Offset<drivetrain::Output> Zero(
55 aos::Sender<drivetrain::Output>::Builder *builder) override;
Comran Morshed5323ecb2015-12-26 20:50:55 +000056
Comran Morshed5323ecb2015-12-26 20:50:55 +000057 double last_gyro_rate_ = 0.0;
58
Austin Schuhbcce26a2018-03-26 23:41:24 -070059 const DrivetrainConfig<double> dt_config_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000060
James Kuszmaulef428a02019-03-02 22:19:41 -080061 ::aos::Fetcher<LocalizerControl> localizer_control_fetcher_;
Austin Schuh73b6e3b2019-05-27 16:37:15 -070062 ::aos::Fetcher<::frc971::IMUValues> imu_values_fetcher_;
Austin Schuh1ea89bb2019-05-27 16:59:59 -070063 ::aos::Fetcher<::frc971::sensors::GyroReading> gyro_reading_fetcher_;
James Kuszmaul3e1bb272020-01-17 18:38:19 -080064
65 zeroing::ImuZeroer imu_zeroer_;
66 DrivetrainUkf down_estimator_;
67 aos::monotonic_clock::time_point last_imu_update_ =
68 aos::monotonic_clock::min_time;
James Kuszmaul3431d622019-02-17 17:07:44 -080069 LocalizerInterface *localizer_;
70
Diana Burgessd0180f12018-03-21 21:24:17 -070071 StateFeedbackLoop<7, 2, 4> kf_;
Austin Schuhbcce26a2018-03-26 23:41:24 -070072 PolyDrivetrain<double> dt_openloop_;
Austin Schuh41565602016-02-28 20:10:49 -080073 DrivetrainMotorsSS dt_closedloop_;
Alex Perry731b4602019-02-02 22:13:01 -080074 SplineDrivetrain dt_spline_;
James Kuszmaule39cbcf2019-02-27 20:48:34 -080075 LineFollowDrivetrain dt_line_follow_;
Austin Schuh9993fb32017-03-15 20:17:46 -070076 ::aos::monotonic_clock::time_point last_gyro_time_ =
77 ::aos::monotonic_clock::min_time;
Comran Morshed5323ecb2015-12-26 20:50:55 +000078
Austin Schuh093535c2016-03-05 23:21:00 -080079 // Current gears for each drive side.
80 Gear left_gear_;
81 Gear right_gear_;
82
Comran Morshed5323ecb2015-12-26 20:50:55 +000083 double last_left_voltage_ = 0;
84 double last_right_voltage_ = 0;
James Kuszmaulf3add3c2019-03-02 14:55:00 -080085 // The left/right voltages previous to last_*_voltage_.
86 double last_last_left_voltage_ = 0;
87 double last_last_right_voltage_ = 0;
Comran Morshed5323ecb2015-12-26 20:50:55 +000088
Austin Schuh093535c2016-03-05 23:21:00 -080089 bool left_high_requested_;
90 bool right_high_requested_;
Austin Schuh5900d142016-04-03 21:35:12 -070091
92 bool has_been_enabled_ = false;
Diana Burgessd0180f12018-03-21 21:24:17 -070093
94 double last_accel_ = 0.0;
Austin Schuh3a378462019-01-04 21:48:04 -080095
Austin Schuh3a378462019-01-04 21:48:04 -080096 // Last kalman filter state.
97 ::Eigen::Matrix<double, 7, 1> last_state_ =
98 ::Eigen::Matrix<double, 7, 1>::Zero();
Comran Morshed5323ecb2015-12-26 20:50:55 +000099};
100
101} // namespace drivetrain
102} // namespace control_loops
103} // namespace frc971
104
105#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_H_