blob: b6ad9e86e44d13dd43bbeb29b168bbd7612eca85 [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
Tyler Chatow993fe282019-04-06 22:24:36 -070086 if (unsafe_goal) {
87 if (!unsafe_goal->suction.grab_piece) {
88 wrist_.set_controller_index(0);
89 elevator_.set_controller_index(0);
90 } else if (unsafe_goal->suction.gamepiece_mode == 0) {
91 wrist_.set_controller_index(1);
92 elevator_.set_controller_index(1);
93 } else {
94 wrist_.set_controller_index(2);
95 elevator_.set_controller_index(2);
96 }
97 }
98
Theo Bafrali00e42272019-02-12 01:07:46 -080099 // TODO(theo) move these up when Iterate() is split
100 // update the goals
101 collision_avoidance_.UpdateGoal(status, unsafe_goal);
102
103 elevator_.set_min_position(collision_avoidance_.min_elevator_goal());
104 wrist_.set_min_position(collision_avoidance_.min_wrist_goal());
105 wrist_.set_max_position(collision_avoidance_.max_wrist_goal());
106 intake_.set_min_position(collision_avoidance_.min_intake_goal());
107 intake_.set_max_position(collision_avoidance_.max_intake_goal());
Sabina Davisc6329342019-03-01 20:44:42 -0800108
Austin Schuh194c43c2019-03-22 20:40:53 -0700109 ::frc971::control_loops::drivetrain_queue.status.FetchLatest();
110
Sabina Davisc6329342019-03-01 20:44:42 -0800111 if (status && unsafe_goal) {
112 // Light Logic
113 if (status->estopped) {
114 // Estop is red
Sabina Davis77a11cf2019-03-09 18:20:26 -0800115 SendColors(1.0, 0.0, 0.0);
Austin Schuh194c43c2019-03-22 20:40:53 -0700116 } else if (::frc971::control_loops::drivetrain_queue.status.get() &&
117 ::frc971::control_loops::drivetrain_queue.status
118 ->line_follow_logging.frozen) {
James Kuszmaul4c1d4f42019-03-24 13:10:00 -0700119 // Vision align is flashing white for button pressed, purple for target
120 // acquired.
Austin Schuh194c43c2019-03-22 20:40:53 -0700121 ++line_blink_count_;
122 if (line_blink_count_ < 20) {
James Kuszmaul4c1d4f42019-03-24 13:10:00 -0700123 if (::frc971::control_loops::drivetrain_queue.status
124 ->line_follow_logging.have_target) {
125 SendColors(1.0, 0.0, 1.0);
126 } else {
127 SendColors(1.0, 1.0, 1.0);
128 }
Austin Schuh194c43c2019-03-22 20:40:53 -0700129 } else {
130 // And then flash with green if we have a game piece.
131 if (status->has_piece) {
132 SendColors(0.0, 1.0, 0.0);
133 } else {
134 SendColors(0.0, 0.0, 0.0);
135 }
136 }
137
138 if (line_blink_count_ > 40) {
139 line_blink_count_ = 0;
140 }
Sabina Davisc6329342019-03-01 20:44:42 -0800141 } else {
Austin Schuh194c43c2019-03-22 20:40:53 -0700142 line_blink_count_ = 0;
143 if (status->has_piece) {
144 // Green if we have a game piece.
145 SendColors(0.0, 1.0, 0.0);
146 } else if (unsafe_goal->suction.gamepiece_mode == 0 &&
147 !status->has_piece) {
148 // Ball mode is orange
149 SendColors(1.0, 0.1, 0.0);
150 } else if (unsafe_goal->suction.gamepiece_mode == 1 &&
151 !status->has_piece) {
152 // Disk mode is deep blue
153 SendColors(0.05, 0.1, 0.5);
154 } else {
155 SendColors(0.0, 0.0, 0.0);
156 }
Sabina Davisc6329342019-03-01 20:44:42 -0800157 }
158 }
Tyler Chatowe51334a2019-01-20 16:58:16 -0800159}
160
161} // namespace superstructure
162} // namespace control_loops
Austin Schuh55a13dc2019-01-27 22:39:03 -0800163} // namespace y2019