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 | 250eb4c | 2015-11-28 16:35:39 -0800 | [diff] [blame] | 19 | static constexpr double kStallTorque = drivetrain::kStallTorque; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 20 | // Stall Current in Amps |
Austin Schuh | 250eb4c | 2015-11-28 16:35:39 -0800 | [diff] [blame] | 21 | static constexpr double kStallCurrent = drivetrain::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 | 250eb4c | 2015-11-28 16:35:39 -0800 | [diff] [blame] | 23 | static constexpr double kFreeSpeed = drivetrain::kFreeSpeedRPM; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 24 | // Free Current in Amps |
Austin Schuh | 250eb4c | 2015-11-28 16:35:39 -0800 | [diff] [blame] | 25 | static constexpr double kFreeCurrent = drivetrain::kFreeCurrent; |
| 26 | static constexpr double kWheelRadius = drivetrain::kWheelRadius; |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 27 | // Resistance of the motor, divided by the number of motors per side. |
Austin Schuh | 250eb4c | 2015-11-28 16:35:39 -0800 | [diff] [blame] | 28 | static constexpr double kR = drivetrain::kR; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 29 | // Motor velocity constant |
Austin Schuh | 250eb4c | 2015-11-28 16:35:39 -0800 | [diff] [blame] | 30 | static constexpr double Kv = drivetrain::kV; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 31 | |
| 32 | // Torque constant |
Austin Schuh | 250eb4c | 2015-11-28 16:35:39 -0800 | [diff] [blame] | 33 | static constexpr double Kt = drivetrain::kT; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 34 | |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 35 | PolyDrivetrain(StateFeedbackLoop<7, 2, 3> *kf); |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 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 | |
Austin Schuh | b9d5e8e | 2015-12-27 14:08:47 -0800 | [diff] [blame] | 41 | // Computes the speed of the motor given the hall effect position and the |
| 42 | // speed of the robot. |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 43 | static double MotorSpeed(const constants::ShifterHallEffect &hall_effect, |
| 44 | double shifter_position, double velocity); |
| 45 | |
Austin Schuh | b9d5e8e | 2015-12-27 14:08:47 -0800 | [diff] [blame] | 46 | // Computes the states of the shifters for the left and right drivetrain sides |
| 47 | // given a requested state. |
| 48 | void UpdateGears(Gear requested_gear); |
| 49 | |
| 50 | // Computes the next state of a shifter given the current state and the |
| 51 | // requested state. |
| 52 | static Gear UpdateSingleGear(Gear requested_gear, Gear current_gear); |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 53 | |
| 54 | void SetGoal(double wheel, double throttle, bool quickturn, bool highgear); |
| 55 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 56 | void SetPosition( |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 57 | const ::y2014::control_loops::DrivetrainQueue::Position *position); |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 58 | |
| 59 | double FilterVelocity(double throttle); |
| 60 | |
| 61 | double MaxVelocity(); |
| 62 | |
| 63 | void Update(); |
| 64 | |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 65 | void SendMotors(::y2014::control_loops::DrivetrainQueue::Output *output); |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 66 | |
| 67 | private: |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 68 | StateFeedbackLoop<7, 2, 3> *kf_; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 69 | const ::aos::controls::HPolytope<2> U_Poly_; |
| 70 | |
| 71 | ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_; |
| 72 | |
| 73 | const double ttrust_; |
| 74 | double wheel_; |
| 75 | double throttle_; |
| 76 | bool quickturn_; |
| 77 | int stale_count_; |
| 78 | double position_time_delta_; |
| 79 | Gear left_gear_; |
| 80 | Gear right_gear_; |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 81 | ::y2014::control_loops::DrivetrainQueue::Position last_position_; |
| 82 | ::y2014::control_loops::DrivetrainQueue::Position position_; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 83 | int counter_; |
| 84 | }; |
| 85 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 86 | } // namespace drivetrain |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 87 | } // namespace control_loops |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 88 | } // namespace y2014 |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 89 | |
| 90 | #endif // Y2014_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_ |