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