blob: 82ebd24a57375b95329a89d11026d0eb29473f22 [file] [log] [blame]
Austin Schuh96ce8ae2015-11-26 12:46:02 -08001#ifndef Y2014_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_
2#define Y2014_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_
3
4#include "aos/common/controls/polytope.h"
5
6#include "y2014/constants.h"
7#include "y2014/control_loops/drivetrain/drivetrain.q.h"
8#include "frc971/control_loops/state_feedback_loop.h"
9#include "y2014/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
10
11namespace frc971 {
12namespace control_loops {
13
14class PolyDrivetrain {
15 public:
16 enum Gear { HIGH, LOW, SHIFTING_UP, SHIFTING_DOWN };
17 // Stall Torque in N m
18 static constexpr double kStallTorque =
19 ::y2014::control_loops::drivetrain::kStallTorque;
20 // Stall Current in Amps
21 static constexpr double kStallCurrent = ::y2014::control_loops::drivetrain::kStallCurrent;
22 // Free Speed in RPM. Used number from last year.
23 static constexpr double kFreeSpeed =
24 ::y2014::control_loops::drivetrain::kFreeSpeedRPM;
25 // Free Current in Amps
26 static constexpr double kFreeCurrent =
27 ::y2014::control_loops::drivetrain::kFreeCurrent;
28 static constexpr double kWheelRadius =
29 ::y2014::control_loops::drivetrain::kWheelRadius;
30 // Resistance of the motor, divided by the number of motors.
31 static constexpr double kR = ::y2014::control_loops::drivetrain::kR;
32 // Motor velocity constant
33 static constexpr double Kv = ::y2014::control_loops::drivetrain::kV;
34
35 // Torque constant
36 static constexpr double Kt = ::y2014::control_loops::drivetrain::kT;
37
38 PolyDrivetrain();
39
40 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
41
42 static double MotorSpeed(const constants::ShifterHallEffect &hall_effect,
43 double shifter_position, double velocity);
44
45 Gear ComputeGear(const constants::ShifterHallEffect &hall_effect,
46 double velocity, Gear current);
47
48 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear);
49
50 void SetPosition(const DrivetrainQueue::Position *position);
51
52 double FilterVelocity(double throttle);
53
54 double MaxVelocity();
55
56 void Update();
57
58 void SendMotors(DrivetrainQueue::Output *output);
59
60 private:
61 const ::aos::controls::HPolytope<2> U_Poly_;
62
63 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
64
65 const double ttrust_;
66 double wheel_;
67 double throttle_;
68 bool quickturn_;
69 int stale_count_;
70 double position_time_delta_;
71 Gear left_gear_;
72 Gear right_gear_;
73 DrivetrainQueue::Position last_position_;
74 DrivetrainQueue::Position position_;
75 int counter_;
76};
77
78} // namespace control_loops
79} // namespace frc971
80
81#endif // Y2014_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_