blob: a9c7ce16a359afc90a6c9f2cc522ba02a1cbf866 [file] [log] [blame]
Tyler Chatowe51334a2019-01-20 16:58:16 -08001#include "y2019/control_loops/superstructure/superstructure.h"
2
3#include "aos/controls/control_loops.q.h"
4#include "frc971/control_loops/control_loops.q.h"
Austin Schuh194c43c2019-03-22 20:40:53 -07005#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Theo Bafrali00e42272019-02-12 01:07:46 -08006#include "frc971/control_loops/static_zeroing_single_dof_profiled_subsystem.h"
Tyler Chatowe51334a2019-01-20 16:58:16 -08007
Sabina Davisc6329342019-03-01 20:44:42 -08008#include "y2019/status_light.q.h"
9
Tyler Chatowe51334a2019-01-20 16:58:16 -080010namespace y2019 {
11namespace control_loops {
12namespace superstructure {
13
Sabina Davisc6329342019-03-01 20:44:42 -080014namespace {
15
16void SendColors(float red, float green, float blue) {
17 auto new_status_light = status_light.MakeMessage();
18 new_status_light->red = red;
19 new_status_light->green = green;
20 new_status_light->blue = blue;
21
22 if (!new_status_light.Send()) {
23 LOG(ERROR, "Failed to send lights.\n");
24 }
25}
26} // namespace
27
Austin Schuh55a13dc2019-01-27 22:39:03 -080028Superstructure::Superstructure(::aos::EventLoop *event_loop,
29 const ::std::string &name)
Theo Bafrali00e42272019-02-12 01:07:46 -080030 : aos::controls::ControlLoop<SuperstructureQueue>(event_loop, name),
31 elevator_(constants::GetValues().elevator.subsystem_params),
32 wrist_(constants::GetValues().wrist.subsystem_params),
33 intake_(constants::GetValues().intake),
34 stilts_(constants::GetValues().stilts.subsystem_params) {}
Tyler Chatowe51334a2019-01-20 16:58:16 -080035
Theo Bafrali00e42272019-02-12 01:07:46 -080036void Superstructure::RunIteration(const SuperstructureQueue::Goal *unsafe_goal,
37 const SuperstructureQueue::Position *position,
38 SuperstructureQueue::Output *output,
39 SuperstructureQueue::Status *status) {
Tyler Chatowe51334a2019-01-20 16:58:16 -080040 if (WasReset()) {
41 LOG(ERROR, "WPILib reset, restarting\n");
Theo Bafrali00e42272019-02-12 01:07:46 -080042 elevator_.Reset();
43 wrist_.Reset();
44 intake_.Reset();
45 stilts_.Reset();
Tyler Chatowe51334a2019-01-20 16:58:16 -080046 }
Theo Bafrali00e42272019-02-12 01:07:46 -080047
48 elevator_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->elevator) : nullptr,
49 &(position->elevator),
50 output != nullptr ? &(output->elevator_voltage) : nullptr,
51 &(status->elevator));
52
53 wrist_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->wrist) : nullptr,
54 &(position->wrist),
55 output != nullptr ? &(output->wrist_voltage) : nullptr,
56 &(status->wrist));
57
Theo Bafrali09517b92019-02-16 15:59:17 -080058 intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr,
59 &(position->intake_joint),
60 output != nullptr ? &(output->intake_joint_voltage) : nullptr,
61 &(status->intake));
Theo Bafrali00e42272019-02-12 01:07:46 -080062
63 stilts_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->stilts) : nullptr,
64 &(position->stilts),
65 output != nullptr ? &(output->stilts_voltage) : nullptr,
66 &(status->stilts));
67
Theo Bafrali3274a182019-02-17 20:01:38 -080068 vacuum_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->suction) : nullptr,
69 position->suction_pressure, output, &(status->has_piece),
70 event_loop());
Austin Schuhe8354752019-02-17 14:59:05 -080071
Theo Bafrali00e42272019-02-12 01:07:46 -080072 status->zeroed = status->elevator.zeroed && status->wrist.zeroed &&
73 status->intake.zeroed && status->stilts.zeroed;
74
75 status->estopped = status->elevator.estopped || status->wrist.estopped ||
76 status->intake.estopped || status->stilts.estopped;
77
Theo Bafrali09517b92019-02-16 15:59:17 -080078 if (output) {
Austin Schuh85e2e912019-02-17 15:04:29 -080079 if (unsafe_goal && status->intake.position > kMinIntakeAngleForRollers) {
80 output->intake_roller_voltage = unsafe_goal->roller_voltage;
Theo Bafrali09517b92019-02-16 15:59:17 -080081 } else {
82 output->intake_roller_voltage = 0.0;
83 }
84 }
85
Theo Bafrali00e42272019-02-12 01:07:46 -080086 // TODO(theo) move these up when Iterate() is split
87 // update the goals
88 collision_avoidance_.UpdateGoal(status, unsafe_goal);
89
90 elevator_.set_min_position(collision_avoidance_.min_elevator_goal());
91 wrist_.set_min_position(collision_avoidance_.min_wrist_goal());
92 wrist_.set_max_position(collision_avoidance_.max_wrist_goal());
93 intake_.set_min_position(collision_avoidance_.min_intake_goal());
94 intake_.set_max_position(collision_avoidance_.max_intake_goal());
Sabina Davisc6329342019-03-01 20:44:42 -080095
Austin Schuh194c43c2019-03-22 20:40:53 -070096 ::frc971::control_loops::drivetrain_queue.status.FetchLatest();
97
Sabina Davisc6329342019-03-01 20:44:42 -080098 if (status && unsafe_goal) {
99 // Light Logic
100 if (status->estopped) {
101 // Estop is red
Sabina Davis77a11cf2019-03-09 18:20:26 -0800102 SendColors(1.0, 0.0, 0.0);
Austin Schuh194c43c2019-03-22 20:40:53 -0700103 } else if (::frc971::control_loops::drivetrain_queue.status.get() &&
104 ::frc971::control_loops::drivetrain_queue.status
105 ->line_follow_logging.frozen) {
106 // Vision align is flashing white.
107 ++line_blink_count_;
108 if (line_blink_count_ < 20) {
109 SendColors(1.0, 1.0, 1.0);
110 } else {
111 // And then flash with green if we have a game piece.
112 if (status->has_piece) {
113 SendColors(0.0, 1.0, 0.0);
114 } else {
115 SendColors(0.0, 0.0, 0.0);
116 }
117 }
118
119 if (line_blink_count_ > 40) {
120 line_blink_count_ = 0;
121 }
Sabina Davisc6329342019-03-01 20:44:42 -0800122 } else {
Austin Schuh194c43c2019-03-22 20:40:53 -0700123 line_blink_count_ = 0;
124 if (status->has_piece) {
125 // Green if we have a game piece.
126 SendColors(0.0, 1.0, 0.0);
127 } else if (unsafe_goal->suction.gamepiece_mode == 0 &&
128 !status->has_piece) {
129 // Ball mode is orange
130 SendColors(1.0, 0.1, 0.0);
131 } else if (unsafe_goal->suction.gamepiece_mode == 1 &&
132 !status->has_piece) {
133 // Disk mode is deep blue
134 SendColors(0.05, 0.1, 0.5);
135 } else {
136 SendColors(0.0, 0.0, 0.0);
137 }
Sabina Davisc6329342019-03-01 20:44:42 -0800138 }
139 }
Tyler Chatowe51334a2019-01-20 16:58:16 -0800140}
141
142} // namespace superstructure
143} // namespace control_loops
Austin Schuh55a13dc2019-01-27 22:39:03 -0800144} // namespace y2019