blob: 73dcc8b66504fc639b5029421751819832d15ec0 [file] [log] [blame]
Brian Silvermanc71537c2016-01-01 13:43:14 -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 "y2012/control_loops/drivetrain/drivetrain.q.h"
7#include "frc971/control_loops/state_feedback_loop.h"
8#include "y2012/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
9
10namespace y2012 {
11namespace control_loops {
12namespace drivetrain {
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 = drivetrain::kStallTorque;
19 // Stall Current in Amps
20 static constexpr double kStallCurrent = drivetrain::kStallCurrent;
21 // Free Speed in RPM. Used number from last year.
22 static constexpr double kFreeSpeed = drivetrain::kFreeSpeedRPM;
23 // Free Current in Amps
24 static constexpr double kFreeCurrent = drivetrain::kFreeCurrent;
25 static constexpr double kWheelRadius = drivetrain::kWheelRadius;
26 // Resistance of the motor, divided by the number of motors per side.
27 static constexpr double kR = drivetrain::kR;
28 // Motor velocity constant
29 static constexpr double Kv = drivetrain::kV;
30
31 // Torque constant
32 static constexpr double Kt = drivetrain::kT;
33
34 static constexpr double kLowGearRatio = 15.0 / 60.0 * 15.0 / 50.0;
35 static constexpr double kHighGearRatio = 30.0 / 45.0 * 15.0 / 50.0;
36
37 PolyDrivetrain();
38
39 int controller_index() const { return loop_->controller_index(); }
40
41 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
42
43 static double MotorSpeed(bool high_gear, double velocity);
44
45 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear);
46
47 void SetPosition(
48 const ::y2012::control_loops::DrivetrainQueue::Position *position);
49
50 double FilterVelocity(double throttle);
51
52 double MaxVelocity();
53
54 void Update();
55
56 void SendMotors(::y2012::control_loops::DrivetrainQueue::Output *output);
57
58 private:
59 const ::aos::controls::HPolytope<2> U_Poly_;
60
61 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
62
63 const double ttrust_;
64 double wheel_;
65 double throttle_;
66 bool quickturn_;
67 int stale_count_;
68 double position_time_delta_;
69 Gear left_gear_;
70 Gear right_gear_;
71 ::y2012::control_loops::DrivetrainQueue::Position last_position_;
72 ::y2012::control_loops::DrivetrainQueue::Position position_;
73 int counter_;
74};
75
76} // namespace drivetrain
77} // namespace control_loops
78} // namespace y2012
79
80#endif // Y2014_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_