blob: 81b6d9d965111838113134a1c5079b29cebb2cb0 [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
John Park33858a32018-09-28 23:05:48 -07006#include "aos/controls/control_loop.h"
Austin Schuh01a9f2a2019-05-27 13:36:30 -07007#include "aos/events/event-loop.h"
Austin Schuhbd0a40f2019-06-30 14:56:31 -07008#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Sabina Davis8d20ca82018-02-19 13:17:45 -08009#include "frc971/control_loops/state_feedback_loop.h"
Austin Schuhcb091712018-02-21 20:01:55 -080010#include "y2018/control_loops/superstructure/arm/arm.h"
Sabina Davis8d20ca82018-02-19 13:17:45 -080011#include "y2018/control_loops/superstructure/intake/intake.h"
12#include "y2018/control_loops/superstructure/superstructure.q.h"
Austin Schuh01a9f2a2019-05-27 13:36:30 -070013#include "y2018/status_light.q.h"
Austin Schuh300f2f62019-05-27 13:49:23 -070014#include "y2018/vision/vision.q.h"
Sabina Davis8d20ca82018-02-19 13:17:45 -080015
16namespace y2018 {
17namespace control_loops {
18namespace superstructure {
19
20class Superstructure
21 : public ::aos::controls::ControlLoop<control_loops::SuperstructureQueue> {
22 public:
23 explicit Superstructure(
Austin Schuh55a13dc2019-01-27 22:39:03 -080024 ::aos::EventLoop *event_loop,
25 const ::std::string &name = ".y2018.control_loops.superstructure_queue");
Sabina Davis8d20ca82018-02-19 13:17:45 -080026
27 const intake::IntakeSide &intake_left() const { return intake_left_; }
28 const intake::IntakeSide &intake_right() const { return intake_right_; }
Austin Schuhcb091712018-02-21 20:01:55 -080029 const arm::Arm &arm() const { return arm_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -080030
31 protected:
32 virtual void RunIteration(
33 const control_loops::SuperstructureQueue::Goal *unsafe_goal,
34 const control_loops::SuperstructureQueue::Position *position,
35 control_loops::SuperstructureQueue::Output *output,
36 control_loops::SuperstructureQueue::Status *status) override;
37
38 private:
Austin Schuh01a9f2a2019-05-27 13:36:30 -070039 // Sends the status light message for the 3 colors provided.
40 void SendColors(float red, float green, float blue);
41
42 ::aos::Sender<::y2018::StatusLight> status_light_sender_;
Austin Schuh300f2f62019-05-27 13:49:23 -070043 ::aos::Fetcher<::y2018::vision::VisionStatus> vision_status_fetcher_;
Austin Schuhbd0a40f2019-06-30 14:56:31 -070044 ::aos::Fetcher<::frc971::control_loops::DrivetrainQueue::Output>
45 drivetrain_output_fetcher_;
Austin Schuh01a9f2a2019-05-27 13:36:30 -070046
Sabina Davis8d20ca82018-02-19 13:17:45 -080047 intake::IntakeSide intake_left_;
48 intake::IntakeSide intake_right_;
Austin Schuhcb091712018-02-21 20:01:55 -080049 arm::Arm arm_;
Sabina Davis8d20ca82018-02-19 13:17:45 -080050
Neil Balchba9cbba2018-04-06 22:26:38 -070051 // The last centering error. This is the distance that the center of the two
52 // intakes is away from 0.
53 double last_intake_center_error_ = 0.0;
54 // The last distance that the box distance lidar measured.
55 double last_box_distance_ = 0.0;
56 // State variable for the box velocity low pass filter.
57 double filtered_box_velocity_ = 0.0;
58
Austin Schuh17dd0892018-03-02 20:06:31 -080059 enum class RotationState {
60 NOT_ROTATING = 0,
61 ROTATING_LEFT = 1,
Neil Balchba9cbba2018-04-06 22:26:38 -070062 ROTATING_RIGHT = 2,
63 STUCK = 3
Austin Schuh17dd0892018-03-02 20:06:31 -080064 };
65
66 RotationState rotation_state_ = RotationState::NOT_ROTATING;
67 int rotation_count_ = 0;
Neil Balchba9cbba2018-04-06 22:26:38 -070068 int stuck_count_ = 0;
69 ::aos::monotonic_clock::time_point last_stuck_time_ =
70 ::aos::monotonic_clock::min_time;
71 ::aos::monotonic_clock::time_point last_unstuck_time_ =
72 ::aos::monotonic_clock::min_time;
Austin Schuh17dd0892018-03-02 20:06:31 -080073
Sabina Davis8d20ca82018-02-19 13:17:45 -080074 DISALLOW_COPY_AND_ASSIGN(Superstructure);
75};
76
77} // namespace superstructure
78} // namespace control_loops
79} // namespace y2018
80
81#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_