blob: 58d1c36a839d5d4dbea28797609fd71db03d3565 [file] [log] [blame]
Sabina Davis91b23602019-01-21 00:06:01 -08001#include <math.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Tyler Chatow4fedeea2019-03-10 15:33:36 -07005#include <chrono>
Sabina Davis91b23602019-01-21 00:06:01 -08006
7#include "aos/actions/actions.h"
8#include "aos/init.h"
9#include "aos/input/action_joystick_input.h"
10#include "aos/input/driver_station_data.h"
11#include "aos/input/drivetrain_input.h"
12#include "aos/input/joystick_input.h"
13#include "aos/logging/logging.h"
14#include "aos/logging/logging.h"
Tyler Chatowe0241452019-03-08 21:07:50 -080015#include "aos/network/team_number.h"
Sabina Davis91b23602019-01-21 00:06:01 -080016#include "aos/util/log_interval.h"
Tyler Chatowe0241452019-03-08 21:07:50 -080017#include "aos/vision/events/udp.h"
18#include "external/com_google_protobuf/src/google/protobuf/stubs/stringprintf.h"
Sabina Davis91b23602019-01-21 00:06:01 -080019#include "frc971/autonomous/auto.q.h"
20#include "frc971/autonomous/base_autonomous_actor.h"
21#include "frc971/control_loops/drivetrain/drivetrain.q.h"
James Kuszmauld8deb682019-03-10 10:38:42 -070022#include "frc971/control_loops/drivetrain/localizer.q.h"
Sabina Davis91b23602019-01-21 00:06:01 -080023
24#include "y2019/control_loops/drivetrain/drivetrain_base.h"
James Kuszmaul7d1ef442019-03-23 20:20:50 -070025#include "y2019/control_loops/drivetrain/target_selector.q.h"
Sabina Davis91b23602019-01-21 00:06:01 -080026#include "y2019/control_loops/superstructure/superstructure.q.h"
Sabina Davisc6329342019-03-01 20:44:42 -080027#include "y2019/status_light.q.h"
Tyler Chatowe0241452019-03-08 21:07:50 -080028#include "y2019/vision.pb.h"
Sabina Davis91b23602019-01-21 00:06:01 -080029
30using ::y2019::control_loops::superstructure::superstructure_queue;
James Kuszmauld8deb682019-03-10 10:38:42 -070031using ::frc971::control_loops::drivetrain::localizer_control;
Sabina Davis91b23602019-01-21 00:06:01 -080032using ::aos::input::driver_station::ButtonLocation;
33using ::aos::input::driver_station::ControlBit;
34using ::aos::input::driver_station::JoystickAxis;
35using ::aos::input::driver_station::POVLocation;
Tyler Chatowe0241452019-03-08 21:07:50 -080036using ::aos::events::ProtoTXUdpSocket;
Sabina Davis91b23602019-01-21 00:06:01 -080037
Austin Schuhaab7e162019-03-13 22:44:58 -070038namespace chrono = ::std::chrono;
39
Sabina Davis91b23602019-01-21 00:06:01 -080040namespace y2019 {
41namespace input {
42namespace joysticks {
43
Tyler Chatowe0241452019-03-08 21:07:50 -080044using google::protobuf::StringPrintf;
45
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080046struct ElevatorWristPosition {
47 double elevator;
48 double wrist;
49};
Sabina Davis91b23602019-01-21 00:06:01 -080050
Tyler Chatow5eeee902019-04-12 20:47:48 -070051const ButtonLocation kSuctionBall(4, 2);
52const ButtonLocation kSuctionHatch(3, 15);
53const ButtonLocation kDeployStilt(4, 1);
54const ButtonLocation kHalfStilt(4, 3);
55const ButtonLocation kFallOver(3, 16);
56
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080057const ButtonLocation kRocketForwardLower(5, 1);
58const ButtonLocation kRocketForwardMiddle(5, 2);
59const ButtonLocation kRocketForwardUpper(5, 4);
60const ButtonLocation kCargoForward(5, 3);
61
62const POVLocation kRocketBackwardUnpressed(5, -1);
63const POVLocation kRocketBackwardLower(5, 180);
64const POVLocation kRocketBackwardMiddle(5, 90);
65const POVLocation kRocketBackwardUpper(5, 0);
66const POVLocation kCargoBackward(5, 270);
67
68const ButtonLocation kPanelSwitch(5, 7);
69const ButtonLocation kCargoSwitch(5, 8);
70
71const ButtonLocation kBallHPIntakeForward(5, 6);
72const ButtonLocation kBallHPIntakeBackward(5, 5);
73const JoystickAxis kBallOutake(5, 3);
74const JoystickAxis kBallIntake(5, 4);
75
76const ButtonLocation kPanelHPIntakeForward(5, 6);
77const ButtonLocation kPanelHPIntakeBackward(5, 5);
78
79const ButtonLocation kRelease(2, 4);
James Kuszmaulccc368b2019-04-11 20:00:07 -070080// Reuse quickturn for the cancel button.
81const ButtonLocation kCancelAutoMode(2, 3);
Tyler Chatow5eeee902019-04-12 20:47:48 -070082const ButtonLocation kReleaseButtonBoard(3, 4);
83const ButtonLocation kResetLocalizerLeftForwards(3, 10);
84const ButtonLocation kResetLocalizerLeftBackwards(3, 9);
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080085
Tyler Chatow5eeee902019-04-12 20:47:48 -070086const ButtonLocation kResetLocalizerRightForwards(3, 8);
87const ButtonLocation kResetLocalizerRightBackwards(3, 7);
Tyler Chatow07c906b2019-03-09 21:29:06 -080088
James Kuszmaul518640d2019-04-13 15:50:50 -070089const ButtonLocation kResetLocalizerLeft(3, 11);
90const ButtonLocation kResetLocalizerRight(3, 13);
91
Tyler Chatow5eeee902019-04-12 20:47:48 -070092const ButtonLocation kNearCargoHint(3, 3);
93const ButtonLocation kMidCargoHint(3, 5);
94const ButtonLocation kFarCargoHint(3, 6);
James Kuszmaul7d1ef442019-03-23 20:20:50 -070095
James Kuszmaul518640d2019-04-13 15:50:50 -070096const JoystickAxis kCargoSelectorY(5, 6);
97const JoystickAxis kCargoSelectorX(5, 5);
98
Tyler Chatow5eeee902019-04-12 20:47:48 -070099const ButtonLocation kCameraLog(3, 14);
Austin Schuh4e2629d2019-03-28 14:44:37 -0700100
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800101const ElevatorWristPosition kStowPos{0.36, 0.0};
Austin Schuh02be8b92019-03-24 16:04:14 -0700102const ElevatorWristPosition kClimbPos{0.0, M_PI / 4.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800103
Austin Schuh139f59d2019-03-17 18:16:13 -0700104const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.01, M_PI / 2.0};
105const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.015, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800106
107const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
108const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
109
Sabina Davise48004f2019-03-02 23:15:24 -0800110const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0};
111const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800112
Sabina Davise48004f2019-03-02 23:15:24 -0800113const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0};
114const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800115
Sabina Davise6fe6c52019-03-03 15:48:51 -0800116const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0};
117const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0};
118
Austin Schuh6c5deb12019-03-24 16:48:32 -0700119const ElevatorWristPosition kBallForwardLowerPos{0.21, 1.27};
Austin Schuh20dc0502019-03-30 07:24:07 -0700120const ElevatorWristPosition kBallBackwardLowerPos{0.43, -1.99};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800121
Austin Schuh20dc0502019-03-30 07:24:07 -0700122const ElevatorWristPosition kBallForwardMiddlePos{0.925, 1.21};
123const ElevatorWristPosition kBallBackwardMiddlePos{1.19, -1.98};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800124
Austin Schuh139f59d2019-03-17 18:16:13 -0700125const ElevatorWristPosition kBallForwardUpperPos{1.51, 0.961};
126const ElevatorWristPosition kBallBackwardUpperPos{1.44, -1.217};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800127
Austin Schuh20dc0502019-03-30 07:24:07 -0700128const ElevatorWristPosition kBallCargoForwardPos{0.59, 1.2};
129const ElevatorWristPosition kBallCargoBackwardPos{0.868265, -2.1};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800130
Sabina Davise6fe6c52019-03-03 15:48:51 -0800131const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097};
132const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800133
Austin Schuh20dc0502019-03-30 07:24:07 -0700134const ElevatorWristPosition kBallIntakePos{0.309, 2.13};
Austin Schuh2cf16b82019-02-15 23:23:22 -0800135
Sabina Davis91b23602019-01-21 00:06:01 -0800136class Reader : public ::aos::input::ActionJoystickInput {
137 public:
138 Reader(::aos::EventLoop *event_loop)
139 : ::aos::input::ActionJoystickInput(
140 event_loop,
James Kuszmaulccc368b2019-04-11 20:00:07 -0700141 ::y2019::control_loops::drivetrain::GetDrivetrainConfig(),
142 {.run_teleop_in_auto = true,
Austin Schuhc087b102019-05-12 15:33:12 -0700143 .cancel_auto_button = kCancelAutoMode}),
144 target_selector_hint_sender_(
145 event_loop->MakeSender<
146 ::y2019::control_loops::drivetrain::TargetSelectorHint>(
147 ".y2019.control_loops.drivetrain.target_selector_hint")) {
Tyler Chatowe0241452019-03-08 21:07:50 -0800148 const uint16_t team = ::aos::network::GetTeamNumber();
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800149 superstructure_queue.goal.FetchLatest();
150 if (superstructure_queue.goal.get()) {
Sabina Davisc6329342019-03-01 20:44:42 -0800151 grab_piece_ = superstructure_queue.goal->suction.grab_piece;
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700152 switch_ball_ = superstructure_queue.goal->suction.gamepiece_mode == 0;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800153 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800154 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
155 StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000));
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800156 }
Sabina Davis91b23602019-01-21 00:06:01 -0800157
Austin Schuha78857f2019-03-13 22:43:41 -0700158 void AutoEnded() override {
159 LOG(INFO, "Auto ended, assuming disc and have piece\n");
160 grab_piece_ = true;
161 switch_ball_ = false;
162 }
163
164 void HandleTeleop(const ::aos::input::driver_station::Data &data) override {
Austin Schuhaab7e162019-03-13 22:44:58 -0700165 ::aos::monotonic_clock::time_point monotonic_now =
166 ::aos::monotonic_clock::now();
Sabina Davis91b23602019-01-21 00:06:01 -0800167 superstructure_queue.position.FetchLatest();
168 superstructure_queue.status.FetchLatest();
169 if (!superstructure_queue.status.get() ||
170 !superstructure_queue.position.get()) {
Austin Schuhe2f22482019-04-13 23:05:43 -0700171 LOG(ERROR, "Got no superstructure status or position packet.\n");
Sabina Davis91b23602019-01-21 00:06:01 -0800172 return;
173 }
174
Austin Schuhaab7e162019-03-13 22:44:58 -0700175 if (!superstructure_queue.status->has_piece) {
176 last_not_has_piece_ = monotonic_now;
177 }
178
Sabina Davis91b23602019-01-21 00:06:01 -0800179 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
180
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700181 {
Austin Schuhc087b102019-05-12 15:33:12 -0700182 auto target_hint = target_selector_hint_sender_.MakeMessage();
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700183 if (data.IsPressed(kNearCargoHint)) {
184 target_hint->suggested_target = 1;
185 } else if (data.IsPressed(kMidCargoHint)) {
186 target_hint->suggested_target = 2;
187 } else if (data.IsPressed(kFarCargoHint)) {
188 target_hint->suggested_target = 3;
189 } else {
James Kuszmaul518640d2019-04-13 15:50:50 -0700190 const double cargo_joy_y = data.GetAxis(kCargoSelectorY);
191 const double cargo_joy_x = data.GetAxis(kCargoSelectorX);
192 if (cargo_joy_y > 0.5) {
James Kuszmaul518640d2019-04-13 15:50:50 -0700193 target_hint->suggested_target = 1;
James Kuszmaul1b822b42019-04-14 14:27:35 -0700194 } else if (cargo_joy_y < -0.5) {
195 target_hint->suggested_target = 3;
James Kuszmaul518640d2019-04-13 15:50:50 -0700196 } else if (::std::abs(cargo_joy_x) > 0.5) {
197 target_hint->suggested_target = 2;
198 } else {
199 target_hint->suggested_target = 0;
200 }
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700201 }
202 if (!target_hint.Send()) {
203 LOG(ERROR, "Failed to send target selector hint.\n");
204 }
205 }
206
James Kuszmaul518640d2019-04-13 15:50:50 -0700207 if (data.PosEdge(kResetLocalizerLeft)) {
208 auto localizer_resetter = localizer_control.MakeMessage();
209 // Start at the left feeder station.
210 localizer_resetter->x = 0.6;
211 localizer_resetter->y = 3.4;
212 localizer_resetter->keep_current_theta = true;
213
214 if (!localizer_resetter.Send()) {
215 LOG(ERROR, "Failed to reset localizer.\n");
216 }
217 }
218
219 if (data.PosEdge(kResetLocalizerRight)) {
220 auto localizer_resetter = localizer_control.MakeMessage();
221 // Start at the left feeder station.
222 localizer_resetter->x = 0.6;
223 localizer_resetter->y = -3.4;
224 localizer_resetter->keep_current_theta = true;
225
226 if (!localizer_resetter.Send()) {
227 LOG(ERROR, "Failed to reset localizer.\n");
228 }
229 }
230
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700231 if (data.PosEdge(kResetLocalizerLeftForwards)) {
James Kuszmauld8deb682019-03-10 10:38:42 -0700232 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700233 // Start at the left feeder station.
James Kuszmauld8deb682019-03-10 10:38:42 -0700234 localizer_resetter->x = 0.4;
235 localizer_resetter->y = 3.4;
236 localizer_resetter->theta = 0.0;
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700237
238 if (!localizer_resetter.Send()) {
239 LOG(ERROR, "Failed to reset localizer.\n");
240 }
241 }
242
243 if (data.PosEdge(kResetLocalizerLeftBackwards)) {
244 auto localizer_resetter = localizer_control.MakeMessage();
245 // Start at the left feeder station.
246 localizer_resetter->x = 0.4;
247 localizer_resetter->y = 3.4;
248 localizer_resetter->theta = M_PI;
249
250 if (!localizer_resetter.Send()) {
251 LOG(ERROR, "Failed to reset localizer.\n");
252 }
253 }
254
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700255 if (data.PosEdge(kResetLocalizerRightForwards)) {
256 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700257 // Start at the right feeder station.
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700258 localizer_resetter->x = 0.4;
259 localizer_resetter->y = -3.4;
260 localizer_resetter->theta = 0.0;
261
262 if (!localizer_resetter.Send()) {
263 LOG(ERROR, "Failed to reset localizer.\n");
264 }
265 }
266
267 if (data.PosEdge(kResetLocalizerRightBackwards)) {
268 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700269 // Start at the right feeder station.
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700270 localizer_resetter->x = 0.4;
271 localizer_resetter->y = -3.4;
272 localizer_resetter->theta = M_PI;
273
James Kuszmauld8deb682019-03-10 10:38:42 -0700274 if (!localizer_resetter.Send()) {
275 LOG(ERROR, "Failed to reset localizer.\n");
276 }
277 }
278
James Kuszmaul13738862019-04-14 10:48:00 -0700279 if (data.PosEdge(kRelease) &&
280 monotonic_now >
281 last_release_button_press_ + ::std::chrono::milliseconds(500)) {
282 if (superstructure_queue.status->has_piece) {
283 release_mode_ = ReleaseButtonMode::kRelease;
284 } else {
285 release_mode_ = ReleaseButtonMode::kBallIntake;
286 }
287 }
288
289 if (data.IsPressed(kRelease)) {
290 last_release_button_press_ = monotonic_now;
291 }
292
Austin Schuh1a8fb572019-05-08 20:07:58 -0700293 LOG(INFO, "has_piece: %d\n", superstructure_queue.status->has_piece);
Austin Schuh1a17e132019-02-17 15:05:06 -0800294 if (data.IsPressed(kSuctionBall)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800295 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800296 } else if (data.IsPressed(kSuctionHatch)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800297 grab_piece_ = true;
James Kuszmaul13738862019-04-14 10:48:00 -0700298 } else if ((release_mode_ == ReleaseButtonMode::kRelease &&
299 data.IsPressed(kRelease)) ||
Tyler Chatow5eeee902019-04-12 20:47:48 -0700300 data.IsPressed(kReleaseButtonBoard) ||
Austin Schuh1a17e132019-02-17 15:05:06 -0800301 !superstructure_queue.status->has_piece) {
Sabina Davisc6329342019-03-01 20:44:42 -0800302 grab_piece_ = false;
Austin Schuh1a8fb572019-05-08 20:07:58 -0700303 LOG(INFO, "releasing due to other thing\n");
Austin Schuh1a17e132019-02-17 15:05:06 -0800304 }
Sabina Davis91b23602019-01-21 00:06:01 -0800305
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800306 if (data.IsPressed(kRocketBackwardUnpressed)) {
307 elevator_wrist_pos_ = kStowPos;
308 }
309 new_superstructure_goal->intake.unsafe_goal = -1.2;
310 new_superstructure_goal->roller_voltage = 0.0;
311
James Kuszmaul13738862019-04-14 10:48:00 -0700312 const bool kDoBallIntake =
313 (!climbed_ && release_mode_ == ReleaseButtonMode::kBallIntake &&
314 data.IsPressed(kRelease)) ||
315 data.GetAxis(kBallIntake) > 0.9;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800316 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
317
318 if (data.IsPressed(kPanelSwitch)) {
319 switch_ball_ = false;
320 } else if (data.IsPressed(kCargoSwitch)) {
321 switch_ball_ = true;
322 }
323
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800324 if (switch_ball_) {
325 if (superstructure_queue.status->has_piece) {
326 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800327 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800328
329 // Go to intake position and apply vacuum
330 if (data.IsPressed(kBallHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800331 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800332 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
333 } else if (data.IsPressed(kBallHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800334 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800335 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
336 }
337
338 // Go to elevator/wrist position. Overrides intake position if pressed so
339 // we can re-grab the ball.
340 if (data.IsPressed(kRocketForwardLower)) {
341 elevator_wrist_pos_ = kBallForwardLowerPos;
342 } else if (data.IsPressed(kRocketBackwardLower)) {
343 elevator_wrist_pos_ = kBallBackwardLowerPos;
344 } else if (data.IsPressed(kRocketForwardMiddle)) {
345 elevator_wrist_pos_ = kBallForwardMiddlePos;
346 } else if (data.IsPressed(kRocketBackwardMiddle)) {
347 elevator_wrist_pos_ = kBallBackwardMiddlePos;
348 } else if (data.IsPressed(kRocketForwardUpper)) {
349 elevator_wrist_pos_ = kBallForwardUpperPos;
350 } else if (data.IsPressed(kRocketBackwardUpper)) {
351 elevator_wrist_pos_ = kBallBackwardUpperPos;
352 } else if (data.IsPressed(kCargoForward)) {
353 elevator_wrist_pos_ = kBallCargoForwardPos;
354 } else if (data.IsPressed(kCargoBackward)) {
355 elevator_wrist_pos_ = kBallCargoBackwardPos;
356 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800357 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800358 if (data.IsPressed(kPanelHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800359 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800360 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
361 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800362 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800363 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
364 }
365
366 // Go to elevator/wrist position. Overrides intake position if pressed so
367 // we can re-grab the panel.
368 if (data.IsPressed(kRocketForwardLower)) {
369 elevator_wrist_pos_ = kPanelForwardLowerPos;
370 } else if (data.IsPressed(kRocketBackwardLower)) {
371 elevator_wrist_pos_ = kPanelBackwardLowerPos;
372 } else if (data.IsPressed(kRocketForwardMiddle)) {
373 elevator_wrist_pos_ = kPanelForwardMiddlePos;
374 } else if (data.IsPressed(kRocketBackwardMiddle)) {
375 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
376 } else if (data.IsPressed(kRocketForwardUpper)) {
377 elevator_wrist_pos_ = kPanelForwardUpperPos;
378 } else if (data.IsPressed(kRocketBackwardUpper)) {
379 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800380 } else if (data.IsPressed(kCargoForward)) {
381 elevator_wrist_pos_ = kPanelCargoForwardPos;
382 } else if (data.IsPressed(kCargoBackward)) {
383 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800384 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800385 }
386
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800387 if (switch_ball_) {
388 if (kDoBallOutake ||
Austin Schuhaab7e162019-03-13 22:44:58 -0700389 (kDoBallIntake &&
Austin Schuh20dc0502019-03-30 07:24:07 -0700390 monotonic_now < last_not_has_piece_ + chrono::milliseconds(200))) {
Austin Schuhd8098b42019-05-08 20:48:17 -0700391 new_superstructure_goal->intake.unsafe_goal = 0.83;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800392 }
Austin Schuh23a51632019-02-19 16:50:36 -0800393
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800394 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
395 elevator_wrist_pos_ = kBallIntakePos;
396 new_superstructure_goal->roller_voltage = 9.0;
Sabina Davisc6329342019-03-01 20:44:42 -0800397 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800398 } else {
399 if (kDoBallOutake) {
400 new_superstructure_goal->roller_voltage = -6.0;
401 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800402 new_superstructure_goal->roller_voltage = 0.0;
403 }
404 }
Austin Schuh23a51632019-02-19 16:50:36 -0800405 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800406
Austin Schuh457bde32019-03-17 18:16:41 -0700407 constexpr double kDeployStiltPosition = 0.5;
408
Austin Schuh457bde32019-03-17 18:16:41 -0700409 if (data.IsPressed(kFallOver)) {
410 new_superstructure_goal->stilts.unsafe_goal = 0.71;
411 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
412 new_superstructure_goal->stilts.profile_params.max_acceleration = 1.25;
Austin Schuhe2f22482019-04-13 23:05:43 -0700413 } else if (data.IsPressed(kHalfStilt)) {
414 was_above_ = false;
415 new_superstructure_goal->stilts.unsafe_goal = 0.345;
416 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
417 } else if (data.IsPressed(kDeployStilt) || was_above_) {
Austin Schuh457bde32019-03-17 18:16:41 -0700418 new_superstructure_goal->stilts.unsafe_goal = kDeployStiltPosition;
419 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
420 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Austin Schuh457bde32019-03-17 18:16:41 -0700421 } else {
422 new_superstructure_goal->stilts.unsafe_goal = 0.005;
423 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
424 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
425 }
426
James Kuszmaul0448f142019-03-24 16:22:34 -0700427 if (superstructure_queue.status->stilts.position > 0.1) {
Austin Schuh02be8b92019-03-24 16:04:14 -0700428 elevator_wrist_pos_ = kClimbPos;
James Kuszmaul13738862019-04-14 10:48:00 -0700429 climbed_ = true;
Austin Schuhd8a18bb2019-03-24 19:21:38 -0700430 new_superstructure_goal->wrist.profile_params.max_acceleration = 10;
431 new_superstructure_goal->elevator.profile_params.max_acceleration = 6;
Austin Schuh02be8b92019-03-24 16:04:14 -0700432 }
433
Austin Schuhe2f22482019-04-13 23:05:43 -0700434 // If we've been asked to go above deploy and made it up that high, latch
435 // was_above.
436 if (new_superstructure_goal->stilts.unsafe_goal > kDeployStiltPosition &&
437 superstructure_queue.status->stilts.position >= kDeployStiltPosition) {
438 was_above_ = true;
Austin Schuh255ff342019-04-14 19:56:12 -0700439 } else if ((superstructure_queue.position->platform_left_detect &&
Austin Schuhe2f22482019-04-13 23:05:43 -0700440 superstructure_queue.position->platform_right_detect) &&
441 !data.IsPressed(kDeployStilt) && !data.IsPressed(kFallOver)) {
Austin Schuhe2f22482019-04-13 23:05:43 -0700442 was_above_ = false;
443 }
444
Austin Schuh457bde32019-03-17 18:16:41 -0700445 if (superstructure_queue.status->stilts.position > kDeployStiltPosition &&
446 new_superstructure_goal->stilts.unsafe_goal == kDeployStiltPosition) {
447 new_superstructure_goal->stilts.profile_params.max_velocity = 0.30;
448 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
449 }
450
James Kuszmaul13738862019-04-14 10:48:00 -0700451 if ((release_mode_ == ReleaseButtonMode::kRelease &&
452 data.IsPressed(kRelease)) ||
453 data.IsPressed(kReleaseButtonBoard)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800454 grab_piece_ = false;
Austin Schuh1a8fb572019-05-08 20:07:58 -0700455 LOG(INFO, "Releasing due to button\n");
Austin Schuh1a17e132019-02-17 15:05:06 -0800456 }
457
Sabina Davisc6329342019-03-01 20:44:42 -0800458 if (switch_ball_) {
459 new_superstructure_goal->suction.gamepiece_mode = 0;
460 } else {
461 new_superstructure_goal->suction.gamepiece_mode = 1;
462 }
463
Tyler Chatowe0241452019-03-08 21:07:50 -0800464 vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0);
465
Sabina Davisc6329342019-03-01 20:44:42 -0800466 new_superstructure_goal->suction.grab_piece = grab_piece_;
Sabina Davis91b23602019-01-21 00:06:01 -0800467
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800468 new_superstructure_goal->elevator.unsafe_goal =
469 elevator_wrist_pos_.elevator;
470 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800471
472 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
473 if (!new_superstructure_goal.Send()) {
474 LOG(ERROR, "Sending superstructure goal failed.\n");
475 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800476
Austin Schuhaab7e162019-03-13 22:44:58 -0700477 if (monotonic_now >
478 last_vision_control_ + ::std::chrono::milliseconds(50)) {
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700479 video_tx_->Send(vision_control_);
Austin Schuhaab7e162019-03-13 22:44:58 -0700480 last_vision_control_ = monotonic_now;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700481 }
Austin Schuh4e2629d2019-03-28 14:44:37 -0700482
483 {
484 auto camera_log_message = camera_log.MakeMessage();
485 camera_log_message->log = data.IsPressed(kCameraLog);
486 LOG_STRUCT(DEBUG, "camera_log", *camera_log_message);
487 camera_log_message.Send();
488 }
Sabina Davis91b23602019-01-21 00:06:01 -0800489 }
490
491 private:
Austin Schuha9644062019-03-28 14:31:52 -0700492 uint32_t GetAutonomousMode() override {
493 ::frc971::autonomous::auto_mode.FetchLatest();
494 if (::frc971::autonomous::auto_mode.get() == nullptr) {
495 LOG(WARNING, "no auto mode values\n");
496 return 0;
497 }
498 return ::frc971::autonomous::auto_mode->mode;
499 }
500
Austin Schuhc087b102019-05-12 15:33:12 -0700501 ::aos::Sender<::y2019::control_loops::drivetrain::TargetSelectorHint>
502 target_selector_hint_sender_;
503
Austin Schuhe2f22482019-04-13 23:05:43 -0700504 // Bool to track if we've been above the deploy position. Once this bool is
505 // set, don't let the stilts retract until we see the platform.
506 bool was_above_ = false;
507
Sabina Davis91b23602019-01-21 00:06:01 -0800508 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800509 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Sabina Davisc6329342019-03-01 20:44:42 -0800510 bool grab_piece_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800511
512 bool switch_ball_ = false;
Tyler Chatowe0241452019-03-08 21:07:50 -0800513
James Kuszmaul13738862019-04-14 10:48:00 -0700514 bool climbed_ = false;
515
516 enum class ReleaseButtonMode {
517 kBallIntake,
518 kRelease,
519 };
520
521 ReleaseButtonMode release_mode_ = ReleaseButtonMode::kRelease;
522 aos::monotonic_clock::time_point last_release_button_press_ =
523 aos::monotonic_clock::min_time;
524
Tyler Chatowe0241452019-03-08 21:07:50 -0800525 VisionControl vision_control_;
526 ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700527 ::aos::monotonic_clock::time_point last_vision_control_ =
528 ::aos::monotonic_clock::time_point::min();
Austin Schuhaab7e162019-03-13 22:44:58 -0700529
530 // Time at which we last did not have a game piece.
531 ::aos::monotonic_clock::time_point last_not_has_piece_ =
532 ::aos::monotonic_clock::time_point::min();
Sabina Davis91b23602019-01-21 00:06:01 -0800533};
534
535} // namespace joysticks
536} // namespace input
537} // namespace y2019
538
539int main() {
540 ::aos::Init(-1);
541 ::aos::ShmEventLoop event_loop;
542 ::y2019::input::joysticks::Reader reader(&event_loop);
543 reader.Run();
544 ::aos::Cleanup();
545}