blob: 5945f96c889296e553228772c6189d026a7d17ac [file] [log] [blame]
Austin Schuh64ebab22015-11-26 13:28:30 -08001#ifndef Y2014_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_
2#define Y2014_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_
3
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"
10#include "y2014/constants.h"
11#include "y2014/control_loops/drivetrain/drivetrain.q.h"
12
Austin Schuh6197a182015-11-28 16:04:40 -080013namespace y2014 {
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
30 const ::aos::controls::HPolytope<2> U_Poly_;
31 Eigen::Matrix<double, 2, 2> T, T_inverse;
32 bool output_was_capped_ = false;;
33 };
34
35 DrivetrainMotorsSS();
36
37 void SetGoal(double left, double left_velocity, double right,
38 double right_velocity);
39
40 void SetRawPosition(double left, double right);
41
42 void SetPosition(double left, double right, double gyro);
43
44 void SetExternalMotors(double left_voltage, double right_voltage);
45
46 void Update(bool stop_motors, bool enable_control_loop);
47
48 double GetEstimatedRobotSpeed() const;
49
50 double GetEstimatedLeftEncoder() const {
51 return loop_->X_hat(0, 0);
52 }
53
Austin Schuh209f1702015-11-29 17:03:00 -080054 double left_velocity() const { return loop_->X_hat(1, 0); }
55 double right_velocity() const { return loop_->X_hat(3, 0); }
56
Austin Schuh64ebab22015-11-26 13:28:30 -080057 double GetEstimatedRightEncoder() const {
58 return loop_->X_hat(2, 0);
59 }
60
61 bool OutputWasCapped() const {
62 return loop_->output_was_capped();
63 }
64
Austin Schuh6197a182015-11-28 16:04:40 -080065 void SendMotors(
66 ::frc971::control_loops::DrivetrainQueue::Output *output) const;
Austin Schuh64ebab22015-11-26 13:28:30 -080067
68 const LimitedDrivetrainLoop &loop() const { return *loop_; }
69
70 private:
71 ::std::unique_ptr<LimitedDrivetrainLoop> loop_;
72
73 double filtered_offset_;
74 double gyro_;
75 double left_goal_;
76 double right_goal_;
77 double raw_left_;
78 double raw_right_;
79};
80
Austin Schuh6197a182015-11-28 16:04:40 -080081} // namespace drivetrain
Austin Schuh64ebab22015-11-26 13:28:30 -080082} // namespace control_loops
Austin Schuh6197a182015-11-28 16:04:40 -080083} // namespace y2014
Austin Schuh64ebab22015-11-26 13:28:30 -080084
85#endif // Y2014_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_