Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 1 | #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 Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame^] | 11 | namespace y2014 { |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 12 | namespace control_loops { |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame^] | 13 | namespace drivetrain { |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 14 | |
| 15 | class PolyDrivetrain { |
| 16 | public: |
| 17 | enum Gear { HIGH, LOW, SHIFTING_UP, SHIFTING_DOWN }; |
| 18 | // Stall Torque in N m |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame^] | 19 | static constexpr double kStallTorque = kStallTorque; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 20 | // Stall Current in Amps |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame^] | 21 | static constexpr double kStallCurrent = kStallCurrent; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 22 | // Free Speed in RPM. Used number from last year. |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame^] | 23 | static constexpr double kFreeSpeed = kFreeSpeedRPM; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 24 | // Free Current in Amps |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame^] | 25 | 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 Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 29 | // Motor velocity constant |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame^] | 30 | static constexpr double Kv = kV; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 31 | |
| 32 | // Torque constant |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame^] | 33 | static constexpr double Kt = kT; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 34 | |
| 35 | PolyDrivetrain(); |
| 36 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame^] | 37 | int controller_index() const { return loop_->controller_index(); } |
| 38 | |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 39 | 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 Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame^] | 49 | void SetPosition( |
| 50 | const ::frc971::control_loops::DrivetrainQueue::Position *position); |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 51 | |
| 52 | double FilterVelocity(double throttle); |
| 53 | |
| 54 | double MaxVelocity(); |
| 55 | |
| 56 | void Update(); |
| 57 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame^] | 58 | void SendMotors(::frc971::control_loops::DrivetrainQueue::Output *output); |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 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_; |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame^] | 73 | ::frc971::control_loops::DrivetrainQueue::Position last_position_; |
| 74 | ::frc971::control_loops::DrivetrainQueue::Position position_; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 75 | int counter_; |
| 76 | }; |
| 77 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame^] | 78 | } // namespace drivetrain |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 79 | } // namespace control_loops |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame^] | 80 | } // namespace y2014 |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 81 | |
| 82 | #endif // Y2014_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_ |