blob: ec9e2e53070a04e92454f61f4153ac8721e12986 [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")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070019 drivetrain_status_fetcher_(
20 event_loop
21 ->MakeFetcher<::frc971::control_loops::DrivetrainQueue::Status>(
22 ".frc971.control_loops.drivetrain_queue.status")),
Theo Bafrali00e42272019-02-12 01:07:46 -080023 elevator_(constants::GetValues().elevator.subsystem_params),
24 wrist_(constants::GetValues().wrist.subsystem_params),
25 intake_(constants::GetValues().intake),
26 stilts_(constants::GetValues().stilts.subsystem_params) {}
Tyler Chatowe51334a2019-01-20 16:58:16 -080027
Theo Bafrali00e42272019-02-12 01:07:46 -080028void Superstructure::RunIteration(const SuperstructureQueue::Goal *unsafe_goal,
29 const SuperstructureQueue::Position *position,
30 SuperstructureQueue::Output *output,
31 SuperstructureQueue::Status *status) {
Tyler Chatowe51334a2019-01-20 16:58:16 -080032 if (WasReset()) {
33 LOG(ERROR, "WPILib reset, restarting\n");
Theo Bafrali00e42272019-02-12 01:07:46 -080034 elevator_.Reset();
35 wrist_.Reset();
36 intake_.Reset();
37 stilts_.Reset();
Tyler Chatowe51334a2019-01-20 16:58:16 -080038 }
Theo Bafrali00e42272019-02-12 01:07:46 -080039
40 elevator_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->elevator) : nullptr,
41 &(position->elevator),
42 output != nullptr ? &(output->elevator_voltage) : nullptr,
43 &(status->elevator));
44
45 wrist_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->wrist) : nullptr,
46 &(position->wrist),
47 output != nullptr ? &(output->wrist_voltage) : nullptr,
48 &(status->wrist));
49
Theo Bafrali09517b92019-02-16 15:59:17 -080050 intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr,
51 &(position->intake_joint),
52 output != nullptr ? &(output->intake_joint_voltage) : nullptr,
53 &(status->intake));
Theo Bafrali00e42272019-02-12 01:07:46 -080054
55 stilts_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->stilts) : nullptr,
56 &(position->stilts),
57 output != nullptr ? &(output->stilts_voltage) : nullptr,
58 &(status->stilts));
59
Theo Bafrali3274a182019-02-17 20:01:38 -080060 vacuum_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->suction) : nullptr,
61 position->suction_pressure, output, &(status->has_piece),
62 event_loop());
Austin Schuhe8354752019-02-17 14:59:05 -080063
Theo Bafrali00e42272019-02-12 01:07:46 -080064 status->zeroed = status->elevator.zeroed && status->wrist.zeroed &&
65 status->intake.zeroed && status->stilts.zeroed;
66
67 status->estopped = status->elevator.estopped || status->wrist.estopped ||
68 status->intake.estopped || status->stilts.estopped;
69
Theo Bafrali09517b92019-02-16 15:59:17 -080070 if (output) {
Austin Schuh85e2e912019-02-17 15:04:29 -080071 if (unsafe_goal && status->intake.position > kMinIntakeAngleForRollers) {
72 output->intake_roller_voltage = unsafe_goal->roller_voltage;
Theo Bafrali09517b92019-02-16 15:59:17 -080073 } else {
74 output->intake_roller_voltage = 0.0;
75 }
76 }
77
Tyler Chatow993fe282019-04-06 22:24:36 -070078 if (unsafe_goal) {
79 if (!unsafe_goal->suction.grab_piece) {
80 wrist_.set_controller_index(0);
81 elevator_.set_controller_index(0);
82 } else if (unsafe_goal->suction.gamepiece_mode == 0) {
83 wrist_.set_controller_index(1);
84 elevator_.set_controller_index(1);
85 } else {
86 wrist_.set_controller_index(2);
87 elevator_.set_controller_index(2);
88 }
89 }
90
Theo Bafrali00e42272019-02-12 01:07:46 -080091 // TODO(theo) move these up when Iterate() is split
92 // update the goals
93 collision_avoidance_.UpdateGoal(status, unsafe_goal);
94
95 elevator_.set_min_position(collision_avoidance_.min_elevator_goal());
96 wrist_.set_min_position(collision_avoidance_.min_wrist_goal());
97 wrist_.set_max_position(collision_avoidance_.max_wrist_goal());
98 intake_.set_min_position(collision_avoidance_.min_intake_goal());
99 intake_.set_max_position(collision_avoidance_.max_intake_goal());
Sabina Davisc6329342019-03-01 20:44:42 -0800100
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700101 drivetrain_status_fetcher_.Fetch();
Austin Schuh194c43c2019-03-22 20:40:53 -0700102
Sabina Davisc6329342019-03-01 20:44:42 -0800103 if (status && unsafe_goal) {
104 // Light Logic
105 if (status->estopped) {
106 // Estop is red
Sabina Davis77a11cf2019-03-09 18:20:26 -0800107 SendColors(1.0, 0.0, 0.0);
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700108 } else if (drivetrain_status_fetcher_.get() &&
109 drivetrain_status_fetcher_->line_follow_logging.frozen) {
James Kuszmaul4c1d4f42019-03-24 13:10:00 -0700110 // Vision align is flashing white for button pressed, purple for target
111 // acquired.
Austin Schuh194c43c2019-03-22 20:40:53 -0700112 ++line_blink_count_;
113 if (line_blink_count_ < 20) {
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700114 if (drivetrain_status_fetcher_->line_follow_logging.have_target) {
James Kuszmaul4c1d4f42019-03-24 13:10:00 -0700115 SendColors(1.0, 0.0, 1.0);
116 } else {
117 SendColors(1.0, 1.0, 1.0);
118 }
Austin Schuh194c43c2019-03-22 20:40:53 -0700119 } else {
120 // And then flash with green if we have a game piece.
121 if (status->has_piece) {
122 SendColors(0.0, 1.0, 0.0);
123 } else {
124 SendColors(0.0, 0.0, 0.0);
125 }
126 }
127
128 if (line_blink_count_ > 40) {
129 line_blink_count_ = 0;
130 }
Sabina Davisc6329342019-03-01 20:44:42 -0800131 } else {
Austin Schuh194c43c2019-03-22 20:40:53 -0700132 line_blink_count_ = 0;
133 if (status->has_piece) {
134 // Green if we have a game piece.
135 SendColors(0.0, 1.0, 0.0);
136 } else if (unsafe_goal->suction.gamepiece_mode == 0 &&
137 !status->has_piece) {
138 // Ball mode is orange
139 SendColors(1.0, 0.1, 0.0);
140 } else if (unsafe_goal->suction.gamepiece_mode == 1 &&
141 !status->has_piece) {
142 // Disk mode is deep blue
143 SendColors(0.05, 0.1, 0.5);
144 } else {
145 SendColors(0.0, 0.0, 0.0);
146 }
Sabina Davisc6329342019-03-01 20:44:42 -0800147 }
148 }
Tyler Chatowe51334a2019-01-20 16:58:16 -0800149}
150
Austin Schuhff973552019-05-19 16:49:28 -0700151void Superstructure::SendColors(float red, float green, float blue) {
152 auto new_status_light = status_light_sender_.MakeMessage();
153 new_status_light->red = red;
154 new_status_light->green = green;
155 new_status_light->blue = blue;
156
157 if (!new_status_light.Send()) {
158 LOG(ERROR, "Failed to send lights.\n");
159 }
160}
161
Tyler Chatowe51334a2019-01-20 16:58:16 -0800162} // namespace superstructure
163} // namespace control_loops
Austin Schuh55a13dc2019-01-27 22:39:03 -0800164} // namespace y2019