Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_ |
| 3 | |
| 4 | #include "aos/common/controls/polytope.h" |
| 5 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 6 | #include "frc971/control_loops/drivetrain/gear.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 7 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 8 | #include "frc971/control_loops/state_feedback_loop.h" |
| 9 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
| 10 | |
| 11 | namespace frc971 { |
| 12 | namespace control_loops { |
| 13 | namespace drivetrain { |
| 14 | |
| 15 | class PolyDrivetrain { |
| 16 | public: |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 17 | PolyDrivetrain(const DrivetrainConfig &dt_config, |
| 18 | StateFeedbackLoop<7, 2, 3> *kf); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 19 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 20 | int controller_index() const { return loop_->index(); } |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 21 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 22 | // Computes the speed of the motor given the hall effect position and the |
| 23 | // speed of the robot. |
| 24 | double MotorSpeed(const constants::ShifterHallEffect &hall_effect, |
Austin Schuh | 746fc6d | 2016-02-21 02:53:33 -0800 | [diff] [blame] | 25 | double shifter_position, double velocity, Gear gear); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 26 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 27 | void SetGoal(const ::frc971::control_loops::DrivetrainQueue::Goal &goal); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 28 | |
| 29 | void SetPosition( |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 30 | const ::frc971::control_loops::DrivetrainQueue::Position *position, |
| 31 | Gear left_gear, Gear right_gear); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 32 | |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 33 | double FilterVelocity(double throttle) const; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 34 | |
| 35 | double MaxVelocity(); |
| 36 | |
| 37 | void Update(); |
| 38 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 39 | void SetOutput(::frc971::control_loops::DrivetrainQueue::Output *output); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 40 | |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 41 | void PopulateStatus(::frc971::control_loops::DrivetrainQueue::Status *status); |
| 42 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 43 | // Computes the next state of a shifter given the current state and the |
| 44 | // requested state. |
| 45 | Gear UpdateSingleGear(Gear requested_gear, Gear current_gear); |
| 46 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 47 | private: |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 48 | StateFeedbackLoop<7, 2, 3> *kf_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 49 | |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 50 | const ::aos::controls::HVPolytope<2, 4, 4> U_Poly_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 51 | |
| 52 | ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_; |
| 53 | |
| 54 | const double ttrust_; |
| 55 | double wheel_; |
| 56 | double throttle_; |
| 57 | bool quickturn_; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 58 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 59 | Gear left_gear_; |
| 60 | Gear right_gear_; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 61 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 62 | ::frc971::control_loops::DrivetrainQueue::Position last_position_; |
| 63 | ::frc971::control_loops::DrivetrainQueue::Position position_; |
| 64 | int counter_; |
| 65 | DrivetrainConfig dt_config_; |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 66 | |
| 67 | double goal_left_velocity_ = 0.0; |
| 68 | double goal_right_velocity_ = 0.0; |
Kyle Stachowicz | 2f3c20f | 2017-07-13 16:04:05 -0700 | [diff] [blame^] | 69 | |
| 70 | // Stored from the last iteration, for logging shifting logic. |
| 71 | double left_motor_speed_ = 0.0; |
| 72 | double right_motor_speed_ = 0.0; |
| 73 | double current_left_velocity_ = 0.0; |
| 74 | double current_right_velocity_ = 0.0; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | } // namespace drivetrain |
| 78 | } // namespace control_loops |
| 79 | } // namespace frc971 |
| 80 | |
| 81 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_ |