Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 1 | #ifndef Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_ |
| 2 | #define Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_ |
| 3 | |
| 4 | #include <memory> |
| 5 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 6 | #include "aos/events/event_loop.h" |
| 7 | #include "aos/time/time.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 8 | #include "frc971/control_loops/control_loop.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | #include "frc971/control_loops/drivetrain/drivetrain_output_generated.h" |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 10 | #include "frc971/control_loops/state_feedback_loop.h" |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 11 | #include "y2018/control_loops/superstructure/arm/arm.h" |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 12 | #include "y2018/control_loops/superstructure/intake/intake.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | #include "y2018/control_loops/superstructure/superstructure_goal_generated.h" |
| 14 | #include "y2018/control_loops/superstructure/superstructure_output_generated.h" |
| 15 | #include "y2018/control_loops/superstructure/superstructure_position_generated.h" |
| 16 | #include "y2018/control_loops/superstructure/superstructure_status_generated.h" |
| 17 | #include "y2018/status_light_generated.h" |
| 18 | #include "y2018/vision/vision_generated.h" |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 19 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 20 | namespace y2018::control_loops::superstructure { |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 21 | |
| 22 | class Superstructure |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 23 | : public ::frc971::controls::ControlLoop<Goal, Position, Status, Output> { |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 24 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | explicit Superstructure(::aos::EventLoop *event_loop, |
| 26 | const ::std::string &name = "/superstructure"); |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 27 | |
| 28 | const intake::IntakeSide &intake_left() const { return intake_left_; } |
| 29 | const intake::IntakeSide &intake_right() const { return intake_right_; } |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 30 | const arm::Arm &arm() const { return arm_; } |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 31 | |
| 32 | protected: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 33 | virtual void RunIteration(const Goal *unsafe_goal, const Position *position, |
| 34 | aos::Sender<Output>::Builder *output, |
| 35 | aos::Sender<Status>::Builder *status) override; |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 36 | |
| 37 | private: |
Austin Schuh | 01a9f2a | 2019-05-27 13:36:30 -0700 | [diff] [blame] | 38 | // Sends the status light message for the 3 colors provided. |
| 39 | void SendColors(float red, float green, float blue); |
| 40 | |
| 41 | ::aos::Sender<::y2018::StatusLight> status_light_sender_; |
Austin Schuh | 300f2f6 | 2019-05-27 13:49:23 -0700 | [diff] [blame] | 42 | ::aos::Fetcher<::y2018::vision::VisionStatus> vision_status_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 43 | ::aos::Fetcher<::frc971::control_loops::drivetrain::Output> |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 44 | drivetrain_output_fetcher_; |
Austin Schuh | 01a9f2a | 2019-05-27 13:36:30 -0700 | [diff] [blame] | 45 | |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 46 | intake::IntakeSide intake_left_; |
| 47 | intake::IntakeSide intake_right_; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 48 | arm::Arm arm_; |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 49 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 50 | // The last centering error. This is the distance that the center of the two |
| 51 | // intakes is away from 0. |
| 52 | double last_intake_center_error_ = 0.0; |
| 53 | // The last distance that the box distance lidar measured. |
| 54 | double last_box_distance_ = 0.0; |
| 55 | // State variable for the box velocity low pass filter. |
| 56 | double filtered_box_velocity_ = 0.0; |
| 57 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 58 | enum class RotationState { |
| 59 | NOT_ROTATING = 0, |
| 60 | ROTATING_LEFT = 1, |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 61 | ROTATING_RIGHT = 2, |
| 62 | STUCK = 3 |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | RotationState rotation_state_ = RotationState::NOT_ROTATING; |
| 66 | int rotation_count_ = 0; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 67 | int stuck_count_ = 0; |
| 68 | ::aos::monotonic_clock::time_point last_stuck_time_ = |
| 69 | ::aos::monotonic_clock::min_time; |
| 70 | ::aos::monotonic_clock::time_point last_unstuck_time_ = |
| 71 | ::aos::monotonic_clock::min_time; |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 72 | |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 73 | DISALLOW_COPY_AND_ASSIGN(Superstructure); |
| 74 | }; |
| 75 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 76 | } // namespace y2018::control_loops::superstructure |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 77 | |
| 78 | #endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_ |