blob: 6abefa46cd7ef70173cb3507e6f70d53cba17b81 [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
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#include "aos/events/event_loop.h"
7#include "aos/time/time.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07008#include "frc971/control_loops/control_loop.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080020namespace y2018::control_loops::superstructure {
Sabina Davis8d20ca82018-02-19 13:17:45 -080021
22class Superstructure
James Kuszmaul61750662021-06-21 21:32:33 -070023 : public ::frc971::controls::ControlLoop<Goal, Position, Status, Output> {
Sabina Davis8d20ca82018-02-19 13:17:45 -080024 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070025 explicit Superstructure(::aos::EventLoop *event_loop,
26 const ::std::string &name = "/superstructure");
Sabina Davis8d20ca82018-02-19 13:17:45 -080027
28 const intake::IntakeSide &intake_left() const { return intake_left_; }
29 const intake::IntakeSide &intake_right() const { return intake_right_; }
Austin Schuhcb091712018-02-21 20:01:55 -080030 const arm::Arm &arm() const { return arm_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -080031
32 protected:
Alex Perrycb7da4b2019-08-28 19:35:56 -070033 virtual void RunIteration(const Goal *unsafe_goal, const Position *position,
34 aos::Sender<Output>::Builder *output,
35 aos::Sender<Status>::Builder *status) override;
Sabina Davis8d20ca82018-02-19 13:17:45 -080036
37 private:
Austin Schuh01a9f2a2019-05-27 13:36:30 -070038 // 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 Schuh300f2f62019-05-27 13:49:23 -070042 ::aos::Fetcher<::y2018::vision::VisionStatus> vision_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070043 ::aos::Fetcher<::frc971::control_loops::drivetrain::Output>
Austin Schuhbd0a40f2019-06-30 14:56:31 -070044 drivetrain_output_fetcher_;
Austin Schuh01a9f2a2019-05-27 13:36:30 -070045
Sabina Davis8d20ca82018-02-19 13:17:45 -080046 intake::IntakeSide intake_left_;
47 intake::IntakeSide intake_right_;
Austin Schuhcb091712018-02-21 20:01:55 -080048 arm::Arm arm_;
Sabina Davis8d20ca82018-02-19 13:17:45 -080049
Neil Balchba9cbba2018-04-06 22:26:38 -070050 // 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 Schuh17dd0892018-03-02 20:06:31 -080058 enum class RotationState {
59 NOT_ROTATING = 0,
60 ROTATING_LEFT = 1,
Neil Balchba9cbba2018-04-06 22:26:38 -070061 ROTATING_RIGHT = 2,
62 STUCK = 3
Austin Schuh17dd0892018-03-02 20:06:31 -080063 };
64
65 RotationState rotation_state_ = RotationState::NOT_ROTATING;
66 int rotation_count_ = 0;
Neil Balchba9cbba2018-04-06 22:26:38 -070067 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 Schuh17dd0892018-03-02 20:06:31 -080072
Sabina Davis8d20ca82018-02-19 13:17:45 -080073 DISALLOW_COPY_AND_ASSIGN(Superstructure);
74};
75
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080076} // namespace y2018::control_loops::superstructure
Sabina Davis8d20ca82018-02-19 13:17:45 -080077
78#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_