blob: 762cbc24c8fce621bf3eca0eafaaab2b87afed6a [file] [log] [blame]
Comran Morshed5323ecb2015-12-26 20:50:55 +00001#ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_
2#define FRC971_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_
Austin Schuh64ebab22015-11-26 13:28:30 -08003
John Park33858a32018-09-28 23:05:48 -07004#include "aos/commonmath.h"
5#include "aos/controls/control_loop.h"
6#include "aos/controls/polytope.h"
7#include "aos/logging/matrix_logging.h"
8#include "aos/util/trapezoid_profile.h"
Austin Schuh64ebab22015-11-26 13:28:30 -08009
10#include "frc971/control_loops/state_feedback_loop.h"
11#include "frc971/control_loops/coerce_goal.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000012#include "frc971/control_loops/drivetrain/drivetrain.q.h"
13#include "frc971/control_loops/drivetrain/drivetrain_config.h"
James Kuszmaul3431d622019-02-17 17:07:44 -080014#include "frc971/control_loops/drivetrain/localizer.h"
Austin Schuh64ebab22015-11-26 13:28:30 -080015
Comran Morshed5323ecb2015-12-26 20:50:55 +000016namespace frc971 {
Austin Schuh64ebab22015-11-26 13:28:30 -080017namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080018namespace drivetrain {
Austin Schuh64ebab22015-11-26 13:28:30 -080019
20class DrivetrainMotorsSS {
21 public:
Austin Schuhbcce26a2018-03-26 23:41:24 -070022 DrivetrainMotorsSS(const DrivetrainConfig<double> &dt_config,
Diana Burgessd0180f12018-03-21 21:24:17 -070023 StateFeedbackLoop<7, 2, 4> *kf,
James Kuszmaul3431d622019-02-17 17:07:44 -080024 LocalizerInterface *localizer);
Austin Schuh64ebab22015-11-26 13:28:30 -080025
Austin Schuh093535c2016-03-05 23:21:00 -080026 void SetGoal(const ::frc971::control_loops::DrivetrainQueue::Goal &goal);
Austin Schuh64ebab22015-11-26 13:28:30 -080027
Austin Schuh093535c2016-03-05 23:21:00 -080028 // Computes the power to send out as part of the controller. Should be called
29 // when disabled (with enable_control_loop false) so the profiles get computed
30 // correctly.
31 // enable_control_loop includes the actual enable bit and if the loop will go
32 // out to hw.
33 void Update(bool enable_control_loop);
Austin Schuh64ebab22015-11-26 13:28:30 -080034
Austin Schuh093535c2016-03-05 23:21:00 -080035 bool output_was_capped() const { return output_was_capped_; }
Brian Silverman4e1b0662016-01-31 18:03:19 -050036
Austin Schuh093535c2016-03-05 23:21:00 -080037 void SetOutput(
Comran Morshed5323ecb2015-12-26 20:50:55 +000038 ::frc971::control_loops::DrivetrainQueue::Output *output) const;
Austin Schuh093535c2016-03-05 23:21:00 -080039 void PopulateStatus(
40 ::frc971::control_loops::DrivetrainQueue::Status *status) const;
Austin Schuh64ebab22015-11-26 13:28:30 -080041
Austin Schuh64ebab22015-11-26 13:28:30 -080042 private:
Austin Schuh093535c2016-03-05 23:21:00 -080043 void PolyCapU(Eigen::Matrix<double, 2, 1> *U);
44 void ScaleCapU(Eigen::Matrix<double, 2, 1> *U);
Comran Morshed5323ecb2015-12-26 20:50:55 +000045
Austin Schuhbcce26a2018-03-26 23:41:24 -070046 const DrivetrainConfig<double> dt_config_;
Diana Burgessd0180f12018-03-21 21:24:17 -070047 StateFeedbackLoop<7, 2, 4> *kf_;
Austin Schuh093535c2016-03-05 23:21:00 -080048 Eigen::Matrix<double, 7, 1> unprofiled_goal_;
49
Austin Schuhba93d9e2016-03-18 22:38:57 -070050 double last_gyro_to_wheel_offset_ = 0;
51
Austin Schuh0aae9242018-03-14 19:49:44 -070052 constexpr static double kMaxVoltage = 12.0;
53 double max_voltage_ = kMaxVoltage;
54
Austin Schuh093535c2016-03-05 23:21:00 -080055 // Reprsents +/- full power on each motor in U-space, aka the square from
56 // (-12, -12) to (12, 12).
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070057 const ::aos::controls::HVPolytope<2, 4, 4> U_poly_;
Austin Schuh093535c2016-03-05 23:21:00 -080058
59 // multiplying by T converts [left_error, right_error] to
60 // [left_right_error_difference, total_distance_error].
61 Eigen::Matrix<double, 2, 2> T_, T_inverse_;
62
63 aos::util::TrapezoidProfile linear_profile_, angular_profile_;
64
65 bool output_was_capped_ = false;
66
67 bool use_profile_ = false;
68
James Kuszmaul3431d622019-02-17 17:07:44 -080069 LocalizerInterface *localizer_;
Austin Schuh64ebab22015-11-26 13:28:30 -080070};
71
Austin Schuh6197a182015-11-28 16:04:40 -080072} // namespace drivetrain
Austin Schuh64ebab22015-11-26 13:28:30 -080073} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +000074} // namespace frc971
Austin Schuh64ebab22015-11-26 13:28:30 -080075
Comran Morshed5323ecb2015-12-26 20:50:55 +000076#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_