Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 1 | #ifndef Y2024_CONTROL_LOOPS_SUPERSTRUCTURE_SHOOTER_H_ |
| 2 | #define Y2024_CONTROL_LOOPS_SUPERSTRUCTURE_SHOOTER_H_ |
| 3 | |
| 4 | #include "frc971/control_loops/catapult/catapult.h" |
| 5 | #include "frc971/control_loops/catapult/catapult_goal_static.h" |
| 6 | #include "frc971/control_loops/static_zeroing_single_dof_profiled_subsystem.h" |
| 7 | #include "frc971/shooter_interpolation/interpolation.h" |
| 8 | #include "frc971/zeroing/pot_and_absolute_encoder.h" |
| 9 | #include "y2024/constants.h" |
| 10 | #include "y2024/constants/constants_generated.h" |
| 11 | #include "y2024/control_loops/superstructure/aiming.h" |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 12 | #include "y2024/control_loops/superstructure/collision_avoidance.h" |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 13 | #include "y2024/control_loops/superstructure/superstructure_can_position_generated.h" |
| 14 | #include "y2024/control_loops/superstructure/superstructure_goal_generated.h" |
| 15 | #include "y2024/control_loops/superstructure/superstructure_position_generated.h" |
| 16 | #include "y2024/control_loops/superstructure/superstructure_status_generated.h" |
| 17 | |
| 18 | namespace y2024::control_loops::superstructure { |
| 19 | |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 20 | class Debouncer { |
| 21 | public: |
| 22 | Debouncer(std::chrono::nanoseconds rising_delay, |
| 23 | std::chrono::nanoseconds falling_delay) |
| 24 | : rising_delay_(rising_delay), falling_delay_(falling_delay) {} |
| 25 | |
| 26 | void Update(bool state, aos::monotonic_clock::time_point now) { |
| 27 | if (state_transition_ != state) { |
| 28 | transition_time_ = now; |
| 29 | state_transition_ = state; |
| 30 | } |
| 31 | |
| 32 | if (state != output_state_) { |
| 33 | if (state) { |
| 34 | output_state_ = now > transition_time_ + rising_delay_; |
| 35 | } else { |
| 36 | output_state_ = !(now > transition_time_ + falling_delay_); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | bool state() const { return output_state_; } |
| 42 | |
| 43 | private: |
| 44 | const std::chrono::nanoseconds rising_delay_; |
| 45 | const std::chrono::nanoseconds falling_delay_; |
| 46 | |
| 47 | bool state_transition_ = false; |
| 48 | bool output_state_ = false; |
| 49 | aos::monotonic_clock::time_point transition_time_ = |
| 50 | aos::monotonic_clock::min_time; |
| 51 | }; |
| 52 | |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 53 | // The shooter class will control the various subsystems involved in the |
| 54 | // shooter- the turret, altitude, and catapult. |
| 55 | class Shooter { |
| 56 | public: |
| 57 | using PotAndAbsoluteEncoderSubsystem = |
| 58 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystem< |
| 59 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator, |
| 60 | ::frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus>; |
| 61 | |
| 62 | using CatapultSubsystem = |
| 63 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystem< |
| 64 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator, |
| 65 | ::frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus, |
| 66 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator, |
| 67 | aos::util::AsymmetricTrapezoidProfile>; |
| 68 | |
| 69 | Shooter(aos::EventLoop *event_loop, const Constants *robot_constants); |
| 70 | |
| 71 | void Reset() { |
| 72 | catapult_.Reset(); |
| 73 | turret_.Reset(); |
| 74 | altitude_.Reset(); |
| 75 | } |
| 76 | |
| 77 | void Estop() { |
| 78 | catapult_.Estop(); |
| 79 | turret_.Estop(); |
| 80 | altitude_.Estop(); |
| 81 | } |
| 82 | |
| 83 | bool zeroed() { |
| 84 | return catapult_.zeroed() && turret_.zeroed() && altitude_.zeroed(); |
| 85 | } |
| 86 | |
| 87 | bool estopped() { |
| 88 | return catapult_.estopped() && turret_.estopped() && altitude_.estopped(); |
| 89 | } |
| 90 | |
| 91 | inline const PotAndAbsoluteEncoderSubsystem &turret() const { |
| 92 | return turret_; |
| 93 | } |
| 94 | |
| 95 | inline const PotAndAbsoluteEncoderSubsystem &altitude() const { |
| 96 | return altitude_; |
| 97 | } |
| 98 | |
| 99 | flatbuffers::Offset<ShooterStatus> Iterate( |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 100 | const Position *position, const ShooterGoal *shooter_goal, bool fire, |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 101 | double *catapult_output, double *altitude_output, double *turret_output, |
Maxwell Henderson | d5bf47a | 2024-02-23 17:16:48 -0800 | [diff] [blame] | 102 | double *retention_roller_output, |
| 103 | double *retention_roller_stator_current_limit, double battery_voltage, |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 104 | /* Hacky way to use collision avoidance in this class */ |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame^] | 105 | CollisionAvoidance *collision_avoidance, const double extend_position, |
| 106 | const double extend_goal, double *max_extend_position, |
| 107 | double *min_extend_position, const double intake_pivot_position, |
| 108 | double *max_turret_intake_position, double *min_intake_pivot_position, |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 109 | flatbuffers::FlatBufferBuilder *fbb, |
| 110 | aos::monotonic_clock::time_point monotonic_now); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 111 | |
| 112 | private: |
| 113 | CatapultState state_ = CatapultState::RETRACTING; |
| 114 | |
| 115 | bool CatapultClose() const { |
| 116 | return (std::abs(catapult_.estimated_position() - |
| 117 | catapult_.unprofiled_goal(0, 0)) < 0.05 && |
| 118 | std::abs(catapult_.estimated_velocity()) < 0.5); |
| 119 | } |
| 120 | |
| 121 | aos::Fetcher<frc971::control_loops::drivetrain::Status> |
| 122 | drivetrain_status_fetcher_; |
| 123 | |
| 124 | aos::Fetcher<y2024::control_loops::superstructure::CANPosition> |
| 125 | superstructure_can_position_fetcher_; |
| 126 | |
| 127 | const Constants *robot_constants_; |
| 128 | |
| 129 | CatapultSubsystem catapult_; |
| 130 | |
| 131 | PotAndAbsoluteEncoderSubsystem turret_; |
| 132 | PotAndAbsoluteEncoderSubsystem altitude_; |
| 133 | |
| 134 | Aimer aimer_; |
| 135 | |
| 136 | frc971::shooter_interpolation::InterpolationTable< |
| 137 | y2024::constants::Values::ShotParams> |
| 138 | interpolation_table_; |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 139 | |
| 140 | Debouncer debouncer_; |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | } // namespace y2024::control_loops::superstructure |
| 144 | |
| 145 | #endif |