blob: d83473d9d1549c59d40773089c60f8cb94195b1e [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"
John Park33858a32018-09-28 23:05:48 -07007#include "aos/util/trapezoid_profile.h"
Austin Schuh64ebab22015-11-26 13:28:30 -08008
Austin Schuh64ebab22015-11-26 13:28:30 -08009#include "frc971/control_loops/coerce_goal.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "frc971/control_loops/control_loops_generated.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000011#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
13#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
Austin Schuh95771d92021-01-23 14:42:25 -080014#include "frc971/control_loops/drivetrain/drivetrain_states.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070015#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
James Kuszmaul3431d622019-02-17 17:07:44 -080016#include "frc971/control_loops/drivetrain/localizer.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070017#include "frc971/control_loops/state_feedback_loop.h"
Austin Schuh64ebab22015-11-26 13:28:30 -080018
Comran Morshed5323ecb2015-12-26 20:50:55 +000019namespace frc971 {
Austin Schuh64ebab22015-11-26 13:28:30 -080020namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080021namespace drivetrain {
Austin Schuh64ebab22015-11-26 13:28:30 -080022
23class DrivetrainMotorsSS {
24 public:
Austin Schuhbcce26a2018-03-26 23:41:24 -070025 DrivetrainMotorsSS(const DrivetrainConfig<double> &dt_config,
Diana Burgessd0180f12018-03-21 21:24:17 -070026 StateFeedbackLoop<7, 2, 4> *kf,
James Kuszmaul3431d622019-02-17 17:07:44 -080027 LocalizerInterface *localizer);
Austin Schuh64ebab22015-11-26 13:28:30 -080028
Alex Perrycb7da4b2019-08-28 19:35:56 -070029 void SetGoal(const ::frc971::control_loops::drivetrain::Goal *goal);
Austin Schuh64ebab22015-11-26 13:28:30 -080030
Austin Schuh093535c2016-03-05 23:21:00 -080031 // Computes the power to send out as part of the controller. Should be called
32 // when disabled (with enable_control_loop false) so the profiles get computed
33 // correctly.
34 // enable_control_loop includes the actual enable bit and if the loop will go
35 // out to hw.
36 void Update(bool enable_control_loop);
Austin Schuh64ebab22015-11-26 13:28:30 -080037
Austin Schuh093535c2016-03-05 23:21:00 -080038 bool output_was_capped() const { return output_was_capped_; }
Brian Silverman4e1b0662016-01-31 18:03:19 -050039
Alex Perrycb7da4b2019-08-28 19:35:56 -070040 void SetOutput(::frc971::control_loops::drivetrain::OutputT *output) const;
Austin Schuh093535c2016-03-05 23:21:00 -080041 void PopulateStatus(
Alex Perrycb7da4b2019-08-28 19:35:56 -070042 ::frc971::control_loops::drivetrain::StatusBuilder *builder) const;
Austin Schuh64ebab22015-11-26 13:28:30 -080043
Austin Schuh64ebab22015-11-26 13:28:30 -080044 private:
Austin Schuh093535c2016-03-05 23:21:00 -080045 void PolyCapU(Eigen::Matrix<double, 2, 1> *U);
46 void ScaleCapU(Eigen::Matrix<double, 2, 1> *U);
Comran Morshed5323ecb2015-12-26 20:50:55 +000047
Austin Schuhbcce26a2018-03-26 23:41:24 -070048 const DrivetrainConfig<double> dt_config_;
Diana Burgessd0180f12018-03-21 21:24:17 -070049 StateFeedbackLoop<7, 2, 4> *kf_;
Austin Schuh093535c2016-03-05 23:21:00 -080050 Eigen::Matrix<double, 7, 1> unprofiled_goal_;
51
Austin Schuhba93d9e2016-03-18 22:38:57 -070052 double last_gyro_to_wheel_offset_ = 0;
53
Austin Schuh0aae9242018-03-14 19:49:44 -070054 constexpr static double kMaxVoltage = 12.0;
55 double max_voltage_ = kMaxVoltage;
56
Austin Schuh093535c2016-03-05 23:21:00 -080057 // Reprsents +/- full power on each motor in U-space, aka the square from
58 // (-12, -12) to (12, 12).
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070059 const ::aos::controls::HVPolytope<2, 4, 4> U_poly_;
Austin Schuh093535c2016-03-05 23:21:00 -080060
61 // multiplying by T converts [left_error, right_error] to
62 // [left_right_error_difference, total_distance_error].
63 Eigen::Matrix<double, 2, 2> T_, T_inverse_;
64
65 aos::util::TrapezoidProfile linear_profile_, angular_profile_;
66
67 bool output_was_capped_ = false;
68
69 bool use_profile_ = false;
70
James Kuszmaul3431d622019-02-17 17:07:44 -080071 LocalizerInterface *localizer_;
Austin Schuh64ebab22015-11-26 13:28:30 -080072};
73
Austin Schuh6197a182015-11-28 16:04:40 -080074} // namespace drivetrain
Austin Schuh64ebab22015-11-26 13:28:30 -080075} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +000076} // namespace frc971
Austin Schuh64ebab22015-11-26 13:28:30 -080077
Comran Morshed5323ecb2015-12-26 20:50:55 +000078#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_