blob: 193dbb21a1a78452ab542b3f2d4d5091f15b8d9b [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
4#include "aos/common/controls/polytope.h"
5#include "aos/common/commonmath.h"
6#include "aos/common/logging/matrix_logging.h"
7
8#include "frc971/control_loops/state_feedback_loop.h"
9#include "frc971/control_loops/coerce_goal.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000010#include "frc971/control_loops/drivetrain/drivetrain.q.h"
11#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Austin Schuh64ebab22015-11-26 13:28:30 -080012
Comran Morshed5323ecb2015-12-26 20:50:55 +000013namespace frc971 {
Austin Schuh64ebab22015-11-26 13:28:30 -080014namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080015namespace drivetrain {
Austin Schuh64ebab22015-11-26 13:28:30 -080016
17class DrivetrainMotorsSS {
18 public:
19 class LimitedDrivetrainLoop : public StateFeedbackLoop<4, 2, 2> {
20 public:
21 LimitedDrivetrainLoop(StateFeedbackLoop<4, 2, 2> &&loop);
22
23 bool output_was_capped() const {
24 return output_was_capped_;
25 }
26
27 private:
28 void CapU() override;
29
Brian Silverman4e1b0662016-01-31 18:03:19 -050030 // Reprsents +/- full power on each motor in U-space, aka the square from
31 // (-12, -12) to (12, 12).
32 const ::aos::controls::HPolytope<2> U_poly_;
33
34 // multiplying by T converts [left_error, right_error] to
35 // [left_right_error_difference, total_distance_error].
36 Eigen::Matrix<double, 2, 2> T_, T_inverse_;
37
38 bool output_was_capped_ = false;
Austin Schuh64ebab22015-11-26 13:28:30 -080039 };
40
Comran Morshed5323ecb2015-12-26 20:50:55 +000041 DrivetrainMotorsSS(const DrivetrainConfig &dt_config);
Austin Schuh64ebab22015-11-26 13:28:30 -080042
43 void SetGoal(double left, double left_velocity, double right,
44 double right_velocity);
45
46 void SetRawPosition(double left, double right);
47
48 void SetPosition(double left, double right, double gyro);
49
50 void SetExternalMotors(double left_voltage, double right_voltage);
51
52 void Update(bool stop_motors, bool enable_control_loop);
53
54 double GetEstimatedRobotSpeed() const;
55
56 double GetEstimatedLeftEncoder() const {
57 return loop_->X_hat(0, 0);
58 }
59
Austin Schuh209f1702015-11-29 17:03:00 -080060 double left_velocity() const { return loop_->X_hat(1, 0); }
61 double right_velocity() const { return loop_->X_hat(3, 0); }
62
Austin Schuh64ebab22015-11-26 13:28:30 -080063 double GetEstimatedRightEncoder() const {
64 return loop_->X_hat(2, 0);
65 }
66
67 bool OutputWasCapped() const {
68 return loop_->output_was_capped();
69 }
70
Austin Schuh6197a182015-11-28 16:04:40 -080071 void SendMotors(
Comran Morshed5323ecb2015-12-26 20:50:55 +000072 ::frc971::control_loops::DrivetrainQueue::Output *output) const;
Austin Schuh64ebab22015-11-26 13:28:30 -080073
74 const LimitedDrivetrainLoop &loop() const { return *loop_; }
75
76 private:
77 ::std::unique_ptr<LimitedDrivetrainLoop> loop_;
78
79 double filtered_offset_;
80 double gyro_;
81 double left_goal_;
82 double right_goal_;
83 double raw_left_;
84 double raw_right_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000085
86 const DrivetrainConfig dt_config_;
Austin Schuh64ebab22015-11-26 13:28:30 -080087};
88
Austin Schuh6197a182015-11-28 16:04:40 -080089} // namespace drivetrain
Austin Schuh64ebab22015-11-26 13:28:30 -080090} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +000091} // namespace frc971
Austin Schuh64ebab22015-11-26 13:28:30 -080092
Comran Morshed5323ecb2015-12-26 20:50:55 +000093#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_