blob: 1f9a27b116cc23580329b52f9f7f466a8a133bb7 [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
Austin Schuh6197a182015-11-28 16:04:40 -080011namespace y2014 {
Austin Schuh96ce8ae2015-11-26 12:46:02 -080012namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080013namespace drivetrain {
Austin Schuh96ce8ae2015-11-26 12:46:02 -080014
15class PolyDrivetrain {
16 public:
17 enum Gear { HIGH, LOW, SHIFTING_UP, SHIFTING_DOWN };
18 // Stall Torque in N m
Austin Schuh6197a182015-11-28 16:04:40 -080019 static constexpr double kStallTorque = kStallTorque;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080020 // Stall Current in Amps
Austin Schuh6197a182015-11-28 16:04:40 -080021 static constexpr double kStallCurrent = kStallCurrent;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080022 // Free Speed in RPM. Used number from last year.
Austin Schuh6197a182015-11-28 16:04:40 -080023 static constexpr double kFreeSpeed = kFreeSpeedRPM;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080024 // Free Current in Amps
Austin Schuh6197a182015-11-28 16:04:40 -080025 static constexpr double kFreeCurrent = kFreeCurrent;
26 static constexpr double kWheelRadius = kWheelRadius;
27 // Resistance of the motor, divided by the number of motors per side.
28 static constexpr double kR = kR;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080029 // Motor velocity constant
Austin Schuh6197a182015-11-28 16:04:40 -080030 static constexpr double Kv = kV;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080031
32 // Torque constant
Austin Schuh6197a182015-11-28 16:04:40 -080033 static constexpr double Kt = kT;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080034
35 PolyDrivetrain();
36
Austin Schuh6197a182015-11-28 16:04:40 -080037 int controller_index() const { return loop_->controller_index(); }
38
Austin Schuh96ce8ae2015-11-26 12:46:02 -080039 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
40
41 static double MotorSpeed(const constants::ShifterHallEffect &hall_effect,
42 double shifter_position, double velocity);
43
44 Gear ComputeGear(const constants::ShifterHallEffect &hall_effect,
45 double velocity, Gear current);
46
47 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear);
48
Austin Schuh6197a182015-11-28 16:04:40 -080049 void SetPosition(
50 const ::frc971::control_loops::DrivetrainQueue::Position *position);
Austin Schuh96ce8ae2015-11-26 12:46:02 -080051
52 double FilterVelocity(double throttle);
53
54 double MaxVelocity();
55
56 void Update();
57
Austin Schuh6197a182015-11-28 16:04:40 -080058 void SendMotors(::frc971::control_loops::DrivetrainQueue::Output *output);
Austin Schuh96ce8ae2015-11-26 12:46:02 -080059
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_;
Austin Schuh6197a182015-11-28 16:04:40 -080073 ::frc971::control_loops::DrivetrainQueue::Position last_position_;
74 ::frc971::control_loops::DrivetrainQueue::Position position_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080075 int counter_;
76};
77
Austin Schuh6197a182015-11-28 16:04:40 -080078} // namespace drivetrain
Austin Schuh96ce8ae2015-11-26 12:46:02 -080079} // namespace control_loops
Austin Schuh6197a182015-11-28 16:04:40 -080080} // namespace y2014
Austin Schuh96ce8ae2015-11-26 12:46:02 -080081
82#endif // Y2014_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_