blob: 63adbb23e775565bd6a2e912b89c842a2bd3d27a [file] [log] [blame]
Sabina Davis8d20ca82018-02-19 13:17:45 -08001#ifndef Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_
2#define Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_
3
4#include <memory>
5
James Kuszmaul61750662021-06-21 21:32:33 -07006#include "frc971/control_loops/control_loop.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "aos/events/event_loop.h"
8#include "aos/time/time.h"
9#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
Sabina Davis8d20ca82018-02-19 13:17:45 -080010#include "frc971/control_loops/state_feedback_loop.h"
Austin Schuhcb091712018-02-21 20:01:55 -080011#include "y2018/control_loops/superstructure/arm/arm.h"
Sabina Davis8d20ca82018-02-19 13:17:45 -080012#include "y2018/control_loops/superstructure/intake/intake.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070013#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 Davis8d20ca82018-02-19 13:17:45 -080019
20namespace y2018 {
21namespace control_loops {
22namespace superstructure {
23
24class Superstructure
James Kuszmaul61750662021-06-21 21:32:33 -070025 : public ::frc971::controls::ControlLoop<Goal, Position, Status, Output> {
Sabina Davis8d20ca82018-02-19 13:17:45 -080026 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070027 explicit Superstructure(::aos::EventLoop *event_loop,
28 const ::std::string &name = "/superstructure");
Sabina Davis8d20ca82018-02-19 13:17:45 -080029
30 const intake::IntakeSide &intake_left() const { return intake_left_; }
31 const intake::IntakeSide &intake_right() const { return intake_right_; }
Austin Schuhcb091712018-02-21 20:01:55 -080032 const arm::Arm &arm() const { return arm_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -080033
34 protected:
Alex Perrycb7da4b2019-08-28 19:35:56 -070035 virtual void RunIteration(const Goal *unsafe_goal, const Position *position,
36 aos::Sender<Output>::Builder *output,
37 aos::Sender<Status>::Builder *status) override;
Sabina Davis8d20ca82018-02-19 13:17:45 -080038
39 private:
Austin Schuh01a9f2a2019-05-27 13:36:30 -070040 // 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 Schuh300f2f62019-05-27 13:49:23 -070044 ::aos::Fetcher<::y2018::vision::VisionStatus> vision_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070045 ::aos::Fetcher<::frc971::control_loops::drivetrain::Output>
Austin Schuhbd0a40f2019-06-30 14:56:31 -070046 drivetrain_output_fetcher_;
Austin Schuh01a9f2a2019-05-27 13:36:30 -070047
Sabina Davis8d20ca82018-02-19 13:17:45 -080048 intake::IntakeSide intake_left_;
49 intake::IntakeSide intake_right_;
Austin Schuhcb091712018-02-21 20:01:55 -080050 arm::Arm arm_;
Sabina Davis8d20ca82018-02-19 13:17:45 -080051
Neil Balchba9cbba2018-04-06 22:26:38 -070052 // 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 Schuh17dd0892018-03-02 20:06:31 -080060 enum class RotationState {
61 NOT_ROTATING = 0,
62 ROTATING_LEFT = 1,
Neil Balchba9cbba2018-04-06 22:26:38 -070063 ROTATING_RIGHT = 2,
64 STUCK = 3
Austin Schuh17dd0892018-03-02 20:06:31 -080065 };
66
67 RotationState rotation_state_ = RotationState::NOT_ROTATING;
68 int rotation_count_ = 0;
Neil Balchba9cbba2018-04-06 22:26:38 -070069 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 Schuh17dd0892018-03-02 20:06:31 -080074
Sabina Davis8d20ca82018-02-19 13:17:45 -080075 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_