Nathan Leong | dd72800 | 2024-02-03 15:26:53 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_CATAPULT_CATAPULT_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_CATAPULT_CATAPULT_H_ |
| 3 | |
| 4 | #include "frc971/control_loops/catapult/catapult_controller.h" |
| 5 | #include "frc971/control_loops/catapult/catapult_goal_generated.h" |
| 6 | #include "frc971/control_loops/static_zeroing_single_dof_profiled_subsystem.h" |
| 7 | #include "frc971/zeroing/pot_and_absolute_encoder.h" |
| 8 | |
| 9 | namespace frc971 { |
| 10 | namespace control_loops { |
| 11 | namespace catapult { |
| 12 | |
| 13 | // Class to handle transitioning between both the profiled subsystem and the MPC |
| 14 | // for shooting. |
| 15 | class Catapult { |
| 16 | public: |
| 17 | Catapult(frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 18 | zeroing::PotAndAbsoluteEncoderZeroingEstimator> |
| 19 | catapult_params, |
| 20 | StateFeedbackPlant<2, 1, 1> plant) |
| 21 | : catapult_(catapult_params), catapult_mpc_(std::move(plant), 30) {} |
| 22 | |
| 23 | using PotAndAbsoluteEncoderSubsystem = |
| 24 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystem< |
| 25 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator, |
| 26 | ::frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus>; |
| 27 | |
| 28 | // Resets all state for when WPILib restarts. |
| 29 | void Reset() { catapult_.Reset(); } |
| 30 | |
| 31 | void Estop() { catapult_.Estop(); } |
| 32 | |
| 33 | bool zeroed() const { return catapult_.zeroed(); } |
| 34 | bool estopped() const { return catapult_.estopped(); } |
| 35 | double solve_time() const { return catapult_mpc_.solve_time(); } |
| 36 | |
| 37 | uint8_t mpc_horizon() const { return current_horizon_; } |
| 38 | |
| 39 | bool mpc_active() const { return !use_profile_; } |
| 40 | |
| 41 | // Returns the number of shots taken. |
| 42 | int shot_count() const { return shot_count_; } |
| 43 | |
| 44 | // Returns the estimated position |
| 45 | double estimated_position() const { return catapult_.estimated_position(); } |
| 46 | |
| 47 | // Runs either the MPC or the profiled subsystem depending on if we are |
| 48 | // shooting or not. Returns the status. |
| 49 | const flatbuffers::Offset< |
| 50 | frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus> |
| 51 | Iterate(const CatapultGoal *catapult_goal, |
| 52 | const PotAndAbsolutePosition *position, double battery_voltage, |
| 53 | double *catapult_voltage, bool fire, |
| 54 | flatbuffers::FlatBufferBuilder *fbb); |
| 55 | |
| 56 | private: |
| 57 | PotAndAbsoluteEncoderSubsystem catapult_; |
| 58 | |
| 59 | frc971::control_loops::catapult::CatapultController catapult_mpc_; |
| 60 | |
| 61 | enum CatapultState { PROFILE, FIRING, RESETTING }; |
| 62 | |
| 63 | CatapultState catapult_state_ = CatapultState::PROFILE; |
| 64 | |
| 65 | double latched_shot_position = 0.0; |
| 66 | double latched_shot_velocity = 0.0; |
| 67 | |
| 68 | bool last_firing_ = false; |
| 69 | bool use_profile_ = true; |
| 70 | |
| 71 | int shot_count_ = 0; |
| 72 | uint8_t current_horizon_ = 0u; |
| 73 | }; |
| 74 | |
| 75 | } // namespace catapult |
| 76 | } // namespace control_loops |
| 77 | } // namespace frc971 |
| 78 | |
| 79 | #endif // Y2022_CONTROL_LOOPS_SUPERSTRUCTURE_CATAPULT_CATAPULT_H_ |