blob: 864eec8923452ee279d0d8aa14f0a0098194a2a6 [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 Schuh250eb4c2015-11-28 16:35:39 -080019 static constexpr double kStallTorque = drivetrain::kStallTorque;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080020 // Stall Current in Amps
Austin Schuh250eb4c2015-11-28 16:35:39 -080021 static constexpr double kStallCurrent = drivetrain::kStallCurrent;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080022 // Free Speed in RPM. Used number from last year.
Austin Schuh250eb4c2015-11-28 16:35:39 -080023 static constexpr double kFreeSpeed = drivetrain::kFreeSpeedRPM;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080024 // Free Current in Amps
Austin Schuh250eb4c2015-11-28 16:35:39 -080025 static constexpr double kFreeCurrent = drivetrain::kFreeCurrent;
26 static constexpr double kWheelRadius = drivetrain::kWheelRadius;
Austin Schuh6197a182015-11-28 16:04:40 -080027 // Resistance of the motor, divided by the number of motors per side.
Austin Schuh250eb4c2015-11-28 16:35:39 -080028 static constexpr double kR = drivetrain::kR;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080029 // Motor velocity constant
Austin Schuh250eb4c2015-11-28 16:35:39 -080030 static constexpr double Kv = drivetrain::kV;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080031
32 // Torque constant
Austin Schuh250eb4c2015-11-28 16:35:39 -080033 static constexpr double Kt = drivetrain::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(
Brian Silvermanb601d892015-12-20 18:24:38 -050050 const ::y2014::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
Brian Silvermanb601d892015-12-20 18:24:38 -050058 void SendMotors(::y2014::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_;
Brian Silvermanb601d892015-12-20 18:24:38 -050073 ::y2014::control_loops::DrivetrainQueue::Position last_position_;
74 ::y2014::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_