blob: a4796a86668d2bc5fcb5ba1b7ab8b9b2461ad586 [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"
Austin Schuhff973552019-05-19 16:49:28 -07004#include "aos/events/event-loop.h"
Tyler Chatowe51334a2019-01-20 16:58:16 -08005#include "frc971/control_loops/control_loops.q.h"
Austin Schuh194c43c2019-03-22 20:40:53 -07006#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Theo Bafrali00e42272019-02-12 01:07:46 -08007#include "frc971/control_loops/static_zeroing_single_dof_profiled_subsystem.h"
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
Austin Schuh55a13dc2019-01-27 22:39:03 -080014Superstructure::Superstructure(::aos::EventLoop *event_loop,
15 const ::std::string &name)
Theo Bafrali00e42272019-02-12 01:07:46 -080016 : aos::controls::ControlLoop<SuperstructureQueue>(event_loop, name),
Austin Schuhff973552019-05-19 16:49:28 -070017 status_light_sender_(
18 event_loop->MakeSender<::y2019::StatusLight>(".y2019.status_light")),
Theo Bafrali00e42272019-02-12 01:07:46 -080019 elevator_(constants::GetValues().elevator.subsystem_params),
20 wrist_(constants::GetValues().wrist.subsystem_params),
21 intake_(constants::GetValues().intake),
22 stilts_(constants::GetValues().stilts.subsystem_params) {}
Tyler Chatowe51334a2019-01-20 16:58:16 -080023
Theo Bafrali00e42272019-02-12 01:07:46 -080024void Superstructure::RunIteration(const SuperstructureQueue::Goal *unsafe_goal,
25 const SuperstructureQueue::Position *position,
26 SuperstructureQueue::Output *output,
27 SuperstructureQueue::Status *status) {
Tyler Chatowe51334a2019-01-20 16:58:16 -080028 if (WasReset()) {
29 LOG(ERROR, "WPILib reset, restarting\n");
Theo Bafrali00e42272019-02-12 01:07:46 -080030 elevator_.Reset();
31 wrist_.Reset();
32 intake_.Reset();
33 stilts_.Reset();
Tyler Chatowe51334a2019-01-20 16:58:16 -080034 }
Theo Bafrali00e42272019-02-12 01:07:46 -080035
36 elevator_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->elevator) : nullptr,
37 &(position->elevator),
38 output != nullptr ? &(output->elevator_voltage) : nullptr,
39 &(status->elevator));
40
41 wrist_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->wrist) : nullptr,
42 &(position->wrist),
43 output != nullptr ? &(output->wrist_voltage) : nullptr,
44 &(status->wrist));
45
Theo Bafrali09517b92019-02-16 15:59:17 -080046 intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr,
47 &(position->intake_joint),
48 output != nullptr ? &(output->intake_joint_voltage) : nullptr,
49 &(status->intake));
Theo Bafrali00e42272019-02-12 01:07:46 -080050
51 stilts_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->stilts) : nullptr,
52 &(position->stilts),
53 output != nullptr ? &(output->stilts_voltage) : nullptr,
54 &(status->stilts));
55
Theo Bafrali3274a182019-02-17 20:01:38 -080056 vacuum_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->suction) : nullptr,
57 position->suction_pressure, output, &(status->has_piece),
58 event_loop());
Austin Schuhe8354752019-02-17 14:59:05 -080059
Theo Bafrali00e42272019-02-12 01:07:46 -080060 status->zeroed = status->elevator.zeroed && status->wrist.zeroed &&
61 status->intake.zeroed && status->stilts.zeroed;
62
63 status->estopped = status->elevator.estopped || status->wrist.estopped ||
64 status->intake.estopped || status->stilts.estopped;
65
Theo Bafrali09517b92019-02-16 15:59:17 -080066 if (output) {
Austin Schuh85e2e912019-02-17 15:04:29 -080067 if (unsafe_goal && status->intake.position > kMinIntakeAngleForRollers) {
68 output->intake_roller_voltage = unsafe_goal->roller_voltage;
Theo Bafrali09517b92019-02-16 15:59:17 -080069 } else {
70 output->intake_roller_voltage = 0.0;
71 }
72 }
73
Tyler Chatow993fe282019-04-06 22:24:36 -070074 if (unsafe_goal) {
75 if (!unsafe_goal->suction.grab_piece) {
76 wrist_.set_controller_index(0);
77 elevator_.set_controller_index(0);
78 } else if (unsafe_goal->suction.gamepiece_mode == 0) {
79 wrist_.set_controller_index(1);
80 elevator_.set_controller_index(1);
81 } else {
82 wrist_.set_controller_index(2);
83 elevator_.set_controller_index(2);
84 }
85 }
86
Theo Bafrali00e42272019-02-12 01:07:46 -080087 // TODO(theo) move these up when Iterate() is split
88 // update the goals
89 collision_avoidance_.UpdateGoal(status, unsafe_goal);
90
91 elevator_.set_min_position(collision_avoidance_.min_elevator_goal());
92 wrist_.set_min_position(collision_avoidance_.min_wrist_goal());
93 wrist_.set_max_position(collision_avoidance_.max_wrist_goal());
94 intake_.set_min_position(collision_avoidance_.min_intake_goal());
95 intake_.set_max_position(collision_avoidance_.max_intake_goal());
Sabina Davisc6329342019-03-01 20:44:42 -080096
Austin Schuh194c43c2019-03-22 20:40:53 -070097 ::frc971::control_loops::drivetrain_queue.status.FetchLatest();
98
Sabina Davisc6329342019-03-01 20:44:42 -080099 if (status && unsafe_goal) {
100 // Light Logic
101 if (status->estopped) {
102 // Estop is red
Sabina Davis77a11cf2019-03-09 18:20:26 -0800103 SendColors(1.0, 0.0, 0.0);
Austin Schuh194c43c2019-03-22 20:40:53 -0700104 } else if (::frc971::control_loops::drivetrain_queue.status.get() &&
105 ::frc971::control_loops::drivetrain_queue.status
106 ->line_follow_logging.frozen) {
James Kuszmaul4c1d4f42019-03-24 13:10:00 -0700107 // Vision align is flashing white for button pressed, purple for target
108 // acquired.
Austin Schuh194c43c2019-03-22 20:40:53 -0700109 ++line_blink_count_;
110 if (line_blink_count_ < 20) {
James Kuszmaul4c1d4f42019-03-24 13:10:00 -0700111 if (::frc971::control_loops::drivetrain_queue.status
112 ->line_follow_logging.have_target) {
113 SendColors(1.0, 0.0, 1.0);
114 } else {
115 SendColors(1.0, 1.0, 1.0);
116 }
Austin Schuh194c43c2019-03-22 20:40:53 -0700117 } else {
118 // And then flash with green if we have a game piece.
119 if (status->has_piece) {
120 SendColors(0.0, 1.0, 0.0);
121 } else {
122 SendColors(0.0, 0.0, 0.0);
123 }
124 }
125
126 if (line_blink_count_ > 40) {
127 line_blink_count_ = 0;
128 }
Sabina Davisc6329342019-03-01 20:44:42 -0800129 } else {
Austin Schuh194c43c2019-03-22 20:40:53 -0700130 line_blink_count_ = 0;
131 if (status->has_piece) {
132 // Green if we have a game piece.
133 SendColors(0.0, 1.0, 0.0);
134 } else if (unsafe_goal->suction.gamepiece_mode == 0 &&
135 !status->has_piece) {
136 // Ball mode is orange
137 SendColors(1.0, 0.1, 0.0);
138 } else if (unsafe_goal->suction.gamepiece_mode == 1 &&
139 !status->has_piece) {
140 // Disk mode is deep blue
141 SendColors(0.05, 0.1, 0.5);
142 } else {
143 SendColors(0.0, 0.0, 0.0);
144 }
Sabina Davisc6329342019-03-01 20:44:42 -0800145 }
146 }
Tyler Chatowe51334a2019-01-20 16:58:16 -0800147}
148
Austin Schuhff973552019-05-19 16:49:28 -0700149void Superstructure::SendColors(float red, float green, float blue) {
150 auto new_status_light = status_light_sender_.MakeMessage();
151 new_status_light->red = red;
152 new_status_light->green = green;
153 new_status_light->blue = blue;
154
155 if (!new_status_light.Send()) {
156 LOG(ERROR, "Failed to send lights.\n");
157 }
158}
159
Tyler Chatowe51334a2019-01-20 16:58:16 -0800160} // namespace superstructure
161} // namespace control_loops
Austin Schuh55a13dc2019-01-27 22:39:03 -0800162} // namespace y2019