blob: 21088463615905f83c3e3e74ff9327bba2f58645 [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 Kuszmaul7d1ef442019-03-23 20:20:50 -070031using ::y2019::control_loops::drivetrain::target_selector_hint;
James Kuszmauld8deb682019-03-10 10:38:42 -070032using ::frc971::control_loops::drivetrain::localizer_control;
Sabina Davis91b23602019-01-21 00:06:01 -080033using ::aos::input::driver_station::ButtonLocation;
34using ::aos::input::driver_station::ControlBit;
35using ::aos::input::driver_station::JoystickAxis;
36using ::aos::input::driver_station::POVLocation;
Tyler Chatowe0241452019-03-08 21:07:50 -080037using ::aos::events::ProtoTXUdpSocket;
Sabina Davis91b23602019-01-21 00:06:01 -080038
Austin Schuhaab7e162019-03-13 22:44:58 -070039namespace chrono = ::std::chrono;
40
Sabina Davis91b23602019-01-21 00:06:01 -080041namespace y2019 {
42namespace input {
43namespace joysticks {
44
Tyler Chatowe0241452019-03-08 21:07:50 -080045using google::protobuf::StringPrintf;
46
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080047struct ElevatorWristPosition {
48 double elevator;
49 double wrist;
50};
Sabina Davis91b23602019-01-21 00:06:01 -080051
Tyler Chatow5eeee902019-04-12 20:47:48 -070052const ButtonLocation kSuctionBall(4, 2);
53const ButtonLocation kSuctionHatch(3, 15);
54const ButtonLocation kDeployStilt(4, 1);
55const ButtonLocation kHalfStilt(4, 3);
56const ButtonLocation kFallOver(3, 16);
57
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080058const ButtonLocation kRocketForwardLower(5, 1);
59const ButtonLocation kRocketForwardMiddle(5, 2);
60const ButtonLocation kRocketForwardUpper(5, 4);
61const ButtonLocation kCargoForward(5, 3);
62
63const POVLocation kRocketBackwardUnpressed(5, -1);
64const POVLocation kRocketBackwardLower(5, 180);
65const POVLocation kRocketBackwardMiddle(5, 90);
66const POVLocation kRocketBackwardUpper(5, 0);
67const POVLocation kCargoBackward(5, 270);
68
69const ButtonLocation kPanelSwitch(5, 7);
70const ButtonLocation kCargoSwitch(5, 8);
71
72const ButtonLocation kBallHPIntakeForward(5, 6);
73const ButtonLocation kBallHPIntakeBackward(5, 5);
74const JoystickAxis kBallOutake(5, 3);
75const JoystickAxis kBallIntake(5, 4);
76
77const ButtonLocation kPanelHPIntakeForward(5, 6);
78const ButtonLocation kPanelHPIntakeBackward(5, 5);
79
80const ButtonLocation kRelease(2, 4);
James Kuszmaulccc368b2019-04-11 20:00:07 -070081// Reuse quickturn for the cancel button.
82const ButtonLocation kCancelAutoMode(2, 3);
Tyler Chatow5eeee902019-04-12 20:47:48 -070083const ButtonLocation kReleaseButtonBoard(3, 4);
84const ButtonLocation kResetLocalizerLeftForwards(3, 10);
85const ButtonLocation kResetLocalizerLeftBackwards(3, 9);
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080086
Tyler Chatow5eeee902019-04-12 20:47:48 -070087const ButtonLocation kResetLocalizerRightForwards(3, 8);
88const ButtonLocation kResetLocalizerRightBackwards(3, 7);
Tyler Chatow07c906b2019-03-09 21:29:06 -080089
James Kuszmaul518640d2019-04-13 15:50:50 -070090const ButtonLocation kResetLocalizerLeft(3, 11);
91const ButtonLocation kResetLocalizerRight(3, 13);
92
Tyler Chatow5eeee902019-04-12 20:47:48 -070093const ButtonLocation kNearCargoHint(3, 3);
94const ButtonLocation kMidCargoHint(3, 5);
95const ButtonLocation kFarCargoHint(3, 6);
James Kuszmaul7d1ef442019-03-23 20:20:50 -070096
James Kuszmaul518640d2019-04-13 15:50:50 -070097const JoystickAxis kCargoSelectorY(5, 6);
98const JoystickAxis kCargoSelectorX(5, 5);
99
Tyler Chatow5eeee902019-04-12 20:47:48 -0700100const ButtonLocation kCameraLog(3, 14);
Austin Schuh4e2629d2019-03-28 14:44:37 -0700101
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800102const ElevatorWristPosition kStowPos{0.36, 0.0};
Austin Schuh02be8b92019-03-24 16:04:14 -0700103const ElevatorWristPosition kClimbPos{0.0, M_PI / 4.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800104
Austin Schuh139f59d2019-03-17 18:16:13 -0700105const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.01, M_PI / 2.0};
106const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.015, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800107
108const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
109const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
110
Sabina Davise48004f2019-03-02 23:15:24 -0800111const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0};
112const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800113
Sabina Davise48004f2019-03-02 23:15:24 -0800114const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0};
115const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800116
Sabina Davise6fe6c52019-03-03 15:48:51 -0800117const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0};
118const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0};
119
Austin Schuh6c5deb12019-03-24 16:48:32 -0700120const ElevatorWristPosition kBallForwardLowerPos{0.21, 1.27};
Austin Schuh20dc0502019-03-30 07:24:07 -0700121const ElevatorWristPosition kBallBackwardLowerPos{0.43, -1.99};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800122
Austin Schuh20dc0502019-03-30 07:24:07 -0700123const ElevatorWristPosition kBallForwardMiddlePos{0.925, 1.21};
124const ElevatorWristPosition kBallBackwardMiddlePos{1.19, -1.98};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800125
Austin Schuh139f59d2019-03-17 18:16:13 -0700126const ElevatorWristPosition kBallForwardUpperPos{1.51, 0.961};
127const ElevatorWristPosition kBallBackwardUpperPos{1.44, -1.217};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800128
Austin Schuh20dc0502019-03-30 07:24:07 -0700129const ElevatorWristPosition kBallCargoForwardPos{0.59, 1.2};
130const ElevatorWristPosition kBallCargoBackwardPos{0.868265, -2.1};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800131
Sabina Davise6fe6c52019-03-03 15:48:51 -0800132const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097};
133const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800134
Austin Schuh20dc0502019-03-30 07:24:07 -0700135const ElevatorWristPosition kBallIntakePos{0.309, 2.13};
Austin Schuh2cf16b82019-02-15 23:23:22 -0800136
Sabina Davis91b23602019-01-21 00:06:01 -0800137class Reader : public ::aos::input::ActionJoystickInput {
138 public:
139 Reader(::aos::EventLoop *event_loop)
140 : ::aos::input::ActionJoystickInput(
141 event_loop,
James Kuszmaulccc368b2019-04-11 20:00:07 -0700142 ::y2019::control_loops::drivetrain::GetDrivetrainConfig(),
143 {.run_teleop_in_auto = true,
144 .cancel_auto_button = kCancelAutoMode}) {
Tyler Chatowe0241452019-03-08 21:07:50 -0800145 const uint16_t team = ::aos::network::GetTeamNumber();
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800146 superstructure_queue.goal.FetchLatest();
147 if (superstructure_queue.goal.get()) {
Sabina Davisc6329342019-03-01 20:44:42 -0800148 grab_piece_ = superstructure_queue.goal->suction.grab_piece;
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700149 switch_ball_ = superstructure_queue.goal->suction.gamepiece_mode == 0;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800150 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800151 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
152 StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000));
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800153 }
Sabina Davis91b23602019-01-21 00:06:01 -0800154
Austin Schuha78857f2019-03-13 22:43:41 -0700155 void AutoEnded() override {
156 LOG(INFO, "Auto ended, assuming disc and have piece\n");
157 grab_piece_ = true;
158 switch_ball_ = false;
159 }
160
161 void HandleTeleop(const ::aos::input::driver_station::Data &data) override {
Austin Schuhaab7e162019-03-13 22:44:58 -0700162 ::aos::monotonic_clock::time_point monotonic_now =
163 ::aos::monotonic_clock::now();
Sabina Davis91b23602019-01-21 00:06:01 -0800164 superstructure_queue.position.FetchLatest();
165 superstructure_queue.status.FetchLatest();
166 if (!superstructure_queue.status.get() ||
167 !superstructure_queue.position.get()) {
Austin Schuhe2f22482019-04-13 23:05:43 -0700168 LOG(ERROR, "Got no superstructure status or position packet.\n");
Sabina Davis91b23602019-01-21 00:06:01 -0800169 return;
170 }
171
Austin Schuhaab7e162019-03-13 22:44:58 -0700172 if (!superstructure_queue.status->has_piece) {
173 last_not_has_piece_ = monotonic_now;
174 }
175
Sabina Davis91b23602019-01-21 00:06:01 -0800176 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
177
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700178 {
179 auto target_hint = target_selector_hint.MakeMessage();
180 if (data.IsPressed(kNearCargoHint)) {
181 target_hint->suggested_target = 1;
182 } else if (data.IsPressed(kMidCargoHint)) {
183 target_hint->suggested_target = 2;
184 } else if (data.IsPressed(kFarCargoHint)) {
185 target_hint->suggested_target = 3;
186 } else {
James Kuszmaul518640d2019-04-13 15:50:50 -0700187 const double cargo_joy_y = data.GetAxis(kCargoSelectorY);
188 const double cargo_joy_x = data.GetAxis(kCargoSelectorX);
189 if (cargo_joy_y > 0.5) {
James Kuszmaul518640d2019-04-13 15:50:50 -0700190 target_hint->suggested_target = 1;
James Kuszmaul1b822b42019-04-14 14:27:35 -0700191 } else if (cargo_joy_y < -0.5) {
192 target_hint->suggested_target = 3;
James Kuszmaul518640d2019-04-13 15:50:50 -0700193 } else if (::std::abs(cargo_joy_x) > 0.5) {
194 target_hint->suggested_target = 2;
195 } else {
196 target_hint->suggested_target = 0;
197 }
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700198 }
199 if (!target_hint.Send()) {
200 LOG(ERROR, "Failed to send target selector hint.\n");
201 }
202 }
203
James Kuszmaul518640d2019-04-13 15:50:50 -0700204 if (data.PosEdge(kResetLocalizerLeft)) {
205 auto localizer_resetter = localizer_control.MakeMessage();
206 // Start at the left feeder station.
207 localizer_resetter->x = 0.6;
208 localizer_resetter->y = 3.4;
209 localizer_resetter->keep_current_theta = true;
210
211 if (!localizer_resetter.Send()) {
212 LOG(ERROR, "Failed to reset localizer.\n");
213 }
214 }
215
216 if (data.PosEdge(kResetLocalizerRight)) {
217 auto localizer_resetter = localizer_control.MakeMessage();
218 // Start at the left feeder station.
219 localizer_resetter->x = 0.6;
220 localizer_resetter->y = -3.4;
221 localizer_resetter->keep_current_theta = true;
222
223 if (!localizer_resetter.Send()) {
224 LOG(ERROR, "Failed to reset localizer.\n");
225 }
226 }
227
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700228 if (data.PosEdge(kResetLocalizerLeftForwards)) {
James Kuszmauld8deb682019-03-10 10:38:42 -0700229 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700230 // Start at the left feeder station.
James Kuszmauld8deb682019-03-10 10:38:42 -0700231 localizer_resetter->x = 0.4;
232 localizer_resetter->y = 3.4;
233 localizer_resetter->theta = 0.0;
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700234
235 if (!localizer_resetter.Send()) {
236 LOG(ERROR, "Failed to reset localizer.\n");
237 }
238 }
239
240 if (data.PosEdge(kResetLocalizerLeftBackwards)) {
241 auto localizer_resetter = localizer_control.MakeMessage();
242 // Start at the left feeder station.
243 localizer_resetter->x = 0.4;
244 localizer_resetter->y = 3.4;
245 localizer_resetter->theta = M_PI;
246
247 if (!localizer_resetter.Send()) {
248 LOG(ERROR, "Failed to reset localizer.\n");
249 }
250 }
251
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700252 if (data.PosEdge(kResetLocalizerRightForwards)) {
253 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700254 // Start at the right feeder station.
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700255 localizer_resetter->x = 0.4;
256 localizer_resetter->y = -3.4;
257 localizer_resetter->theta = 0.0;
258
259 if (!localizer_resetter.Send()) {
260 LOG(ERROR, "Failed to reset localizer.\n");
261 }
262 }
263
264 if (data.PosEdge(kResetLocalizerRightBackwards)) {
265 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700266 // Start at the right feeder station.
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700267 localizer_resetter->x = 0.4;
268 localizer_resetter->y = -3.4;
269 localizer_resetter->theta = M_PI;
270
James Kuszmauld8deb682019-03-10 10:38:42 -0700271 if (!localizer_resetter.Send()) {
272 LOG(ERROR, "Failed to reset localizer.\n");
273 }
274 }
275
James Kuszmaul13738862019-04-14 10:48:00 -0700276 if (data.PosEdge(kRelease) &&
277 monotonic_now >
278 last_release_button_press_ + ::std::chrono::milliseconds(500)) {
279 if (superstructure_queue.status->has_piece) {
280 release_mode_ = ReleaseButtonMode::kRelease;
281 } else {
282 release_mode_ = ReleaseButtonMode::kBallIntake;
283 }
284 }
285
286 if (data.IsPressed(kRelease)) {
287 last_release_button_press_ = monotonic_now;
288 }
289
Austin Schuh1a8fb572019-05-08 20:07:58 -0700290 LOG(INFO, "has_piece: %d\n", superstructure_queue.status->has_piece);
Austin Schuh1a17e132019-02-17 15:05:06 -0800291 if (data.IsPressed(kSuctionBall)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800292 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800293 } else if (data.IsPressed(kSuctionHatch)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800294 grab_piece_ = true;
James Kuszmaul13738862019-04-14 10:48:00 -0700295 } else if ((release_mode_ == ReleaseButtonMode::kRelease &&
296 data.IsPressed(kRelease)) ||
Tyler Chatow5eeee902019-04-12 20:47:48 -0700297 data.IsPressed(kReleaseButtonBoard) ||
Austin Schuh1a17e132019-02-17 15:05:06 -0800298 !superstructure_queue.status->has_piece) {
Sabina Davisc6329342019-03-01 20:44:42 -0800299 grab_piece_ = false;
Austin Schuh1a8fb572019-05-08 20:07:58 -0700300 LOG(INFO, "releasing due to other thing\n");
Austin Schuh1a17e132019-02-17 15:05:06 -0800301 }
Sabina Davis91b23602019-01-21 00:06:01 -0800302
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800303 if (data.IsPressed(kRocketBackwardUnpressed)) {
304 elevator_wrist_pos_ = kStowPos;
305 }
306 new_superstructure_goal->intake.unsafe_goal = -1.2;
307 new_superstructure_goal->roller_voltage = 0.0;
308
James Kuszmaul13738862019-04-14 10:48:00 -0700309 const bool kDoBallIntake =
310 (!climbed_ && release_mode_ == ReleaseButtonMode::kBallIntake &&
311 data.IsPressed(kRelease)) ||
312 data.GetAxis(kBallIntake) > 0.9;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800313 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
314
315 if (data.IsPressed(kPanelSwitch)) {
316 switch_ball_ = false;
317 } else if (data.IsPressed(kCargoSwitch)) {
318 switch_ball_ = true;
319 }
320
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800321 if (switch_ball_) {
322 if (superstructure_queue.status->has_piece) {
323 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800324 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800325
326 // Go to intake position and apply vacuum
327 if (data.IsPressed(kBallHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800328 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800329 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
330 } else if (data.IsPressed(kBallHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800331 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800332 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
333 }
334
335 // Go to elevator/wrist position. Overrides intake position if pressed so
336 // we can re-grab the ball.
337 if (data.IsPressed(kRocketForwardLower)) {
338 elevator_wrist_pos_ = kBallForwardLowerPos;
339 } else if (data.IsPressed(kRocketBackwardLower)) {
340 elevator_wrist_pos_ = kBallBackwardLowerPos;
341 } else if (data.IsPressed(kRocketForwardMiddle)) {
342 elevator_wrist_pos_ = kBallForwardMiddlePos;
343 } else if (data.IsPressed(kRocketBackwardMiddle)) {
344 elevator_wrist_pos_ = kBallBackwardMiddlePos;
345 } else if (data.IsPressed(kRocketForwardUpper)) {
346 elevator_wrist_pos_ = kBallForwardUpperPos;
347 } else if (data.IsPressed(kRocketBackwardUpper)) {
348 elevator_wrist_pos_ = kBallBackwardUpperPos;
349 } else if (data.IsPressed(kCargoForward)) {
350 elevator_wrist_pos_ = kBallCargoForwardPos;
351 } else if (data.IsPressed(kCargoBackward)) {
352 elevator_wrist_pos_ = kBallCargoBackwardPos;
353 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800354 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800355 if (data.IsPressed(kPanelHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800356 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800357 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
358 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800359 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800360 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
361 }
362
363 // Go to elevator/wrist position. Overrides intake position if pressed so
364 // we can re-grab the panel.
365 if (data.IsPressed(kRocketForwardLower)) {
366 elevator_wrist_pos_ = kPanelForwardLowerPos;
367 } else if (data.IsPressed(kRocketBackwardLower)) {
368 elevator_wrist_pos_ = kPanelBackwardLowerPos;
369 } else if (data.IsPressed(kRocketForwardMiddle)) {
370 elevator_wrist_pos_ = kPanelForwardMiddlePos;
371 } else if (data.IsPressed(kRocketBackwardMiddle)) {
372 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
373 } else if (data.IsPressed(kRocketForwardUpper)) {
374 elevator_wrist_pos_ = kPanelForwardUpperPos;
375 } else if (data.IsPressed(kRocketBackwardUpper)) {
376 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800377 } else if (data.IsPressed(kCargoForward)) {
378 elevator_wrist_pos_ = kPanelCargoForwardPos;
379 } else if (data.IsPressed(kCargoBackward)) {
380 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800381 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800382 }
383
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800384 if (switch_ball_) {
385 if (kDoBallOutake ||
Austin Schuhaab7e162019-03-13 22:44:58 -0700386 (kDoBallIntake &&
Austin Schuh20dc0502019-03-30 07:24:07 -0700387 monotonic_now < last_not_has_piece_ + chrono::milliseconds(200))) {
Austin Schuhd8098b42019-05-08 20:48:17 -0700388 new_superstructure_goal->intake.unsafe_goal = 0.83;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800389 }
Austin Schuh23a51632019-02-19 16:50:36 -0800390
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800391 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
392 elevator_wrist_pos_ = kBallIntakePos;
393 new_superstructure_goal->roller_voltage = 9.0;
Sabina Davisc6329342019-03-01 20:44:42 -0800394 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800395 } else {
396 if (kDoBallOutake) {
397 new_superstructure_goal->roller_voltage = -6.0;
398 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800399 new_superstructure_goal->roller_voltage = 0.0;
400 }
401 }
Austin Schuh23a51632019-02-19 16:50:36 -0800402 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800403
Austin Schuh457bde32019-03-17 18:16:41 -0700404 constexpr double kDeployStiltPosition = 0.5;
405
Austin Schuh457bde32019-03-17 18:16:41 -0700406 if (data.IsPressed(kFallOver)) {
407 new_superstructure_goal->stilts.unsafe_goal = 0.71;
408 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
409 new_superstructure_goal->stilts.profile_params.max_acceleration = 1.25;
Austin Schuhe2f22482019-04-13 23:05:43 -0700410 } else if (data.IsPressed(kHalfStilt)) {
411 was_above_ = false;
412 new_superstructure_goal->stilts.unsafe_goal = 0.345;
413 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
414 } else if (data.IsPressed(kDeployStilt) || was_above_) {
Austin Schuh457bde32019-03-17 18:16:41 -0700415 new_superstructure_goal->stilts.unsafe_goal = kDeployStiltPosition;
416 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
417 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Austin Schuh457bde32019-03-17 18:16:41 -0700418 } else {
419 new_superstructure_goal->stilts.unsafe_goal = 0.005;
420 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
421 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
422 }
423
James Kuszmaul0448f142019-03-24 16:22:34 -0700424 if (superstructure_queue.status->stilts.position > 0.1) {
Austin Schuh02be8b92019-03-24 16:04:14 -0700425 elevator_wrist_pos_ = kClimbPos;
James Kuszmaul13738862019-04-14 10:48:00 -0700426 climbed_ = true;
Austin Schuhd8a18bb2019-03-24 19:21:38 -0700427 new_superstructure_goal->wrist.profile_params.max_acceleration = 10;
428 new_superstructure_goal->elevator.profile_params.max_acceleration = 6;
Austin Schuh02be8b92019-03-24 16:04:14 -0700429 }
430
Austin Schuhe2f22482019-04-13 23:05:43 -0700431 // If we've been asked to go above deploy and made it up that high, latch
432 // was_above.
433 if (new_superstructure_goal->stilts.unsafe_goal > kDeployStiltPosition &&
434 superstructure_queue.status->stilts.position >= kDeployStiltPosition) {
435 was_above_ = true;
Austin Schuh255ff342019-04-14 19:56:12 -0700436 } else if ((superstructure_queue.position->platform_left_detect &&
Austin Schuhe2f22482019-04-13 23:05:43 -0700437 superstructure_queue.position->platform_right_detect) &&
438 !data.IsPressed(kDeployStilt) && !data.IsPressed(kFallOver)) {
Austin Schuhe2f22482019-04-13 23:05:43 -0700439 was_above_ = false;
440 }
441
Austin Schuh457bde32019-03-17 18:16:41 -0700442 if (superstructure_queue.status->stilts.position > kDeployStiltPosition &&
443 new_superstructure_goal->stilts.unsafe_goal == kDeployStiltPosition) {
444 new_superstructure_goal->stilts.profile_params.max_velocity = 0.30;
445 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
446 }
447
James Kuszmaul13738862019-04-14 10:48:00 -0700448 if ((release_mode_ == ReleaseButtonMode::kRelease &&
449 data.IsPressed(kRelease)) ||
450 data.IsPressed(kReleaseButtonBoard)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800451 grab_piece_ = false;
Austin Schuh1a8fb572019-05-08 20:07:58 -0700452 LOG(INFO, "Releasing due to button\n");
Austin Schuh1a17e132019-02-17 15:05:06 -0800453 }
454
Sabina Davisc6329342019-03-01 20:44:42 -0800455 if (switch_ball_) {
456 new_superstructure_goal->suction.gamepiece_mode = 0;
457 } else {
458 new_superstructure_goal->suction.gamepiece_mode = 1;
459 }
460
Tyler Chatowe0241452019-03-08 21:07:50 -0800461 vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0);
462
Sabina Davisc6329342019-03-01 20:44:42 -0800463 new_superstructure_goal->suction.grab_piece = grab_piece_;
Sabina Davis91b23602019-01-21 00:06:01 -0800464
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800465 new_superstructure_goal->elevator.unsafe_goal =
466 elevator_wrist_pos_.elevator;
467 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800468
469 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
470 if (!new_superstructure_goal.Send()) {
471 LOG(ERROR, "Sending superstructure goal failed.\n");
472 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800473
Austin Schuhaab7e162019-03-13 22:44:58 -0700474 if (monotonic_now >
475 last_vision_control_ + ::std::chrono::milliseconds(50)) {
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700476 video_tx_->Send(vision_control_);
Austin Schuhaab7e162019-03-13 22:44:58 -0700477 last_vision_control_ = monotonic_now;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700478 }
Austin Schuh4e2629d2019-03-28 14:44:37 -0700479
480 {
481 auto camera_log_message = camera_log.MakeMessage();
482 camera_log_message->log = data.IsPressed(kCameraLog);
483 LOG_STRUCT(DEBUG, "camera_log", *camera_log_message);
484 camera_log_message.Send();
485 }
Sabina Davis91b23602019-01-21 00:06:01 -0800486 }
487
488 private:
Austin Schuha9644062019-03-28 14:31:52 -0700489 uint32_t GetAutonomousMode() override {
490 ::frc971::autonomous::auto_mode.FetchLatest();
491 if (::frc971::autonomous::auto_mode.get() == nullptr) {
492 LOG(WARNING, "no auto mode values\n");
493 return 0;
494 }
495 return ::frc971::autonomous::auto_mode->mode;
496 }
497
Austin Schuhe2f22482019-04-13 23:05:43 -0700498 // Bool to track if we've been above the deploy position. Once this bool is
499 // set, don't let the stilts retract until we see the platform.
500 bool was_above_ = false;
501
Sabina Davis91b23602019-01-21 00:06:01 -0800502 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800503 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Sabina Davisc6329342019-03-01 20:44:42 -0800504 bool grab_piece_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800505
506 bool switch_ball_ = false;
Tyler Chatowe0241452019-03-08 21:07:50 -0800507
James Kuszmaul13738862019-04-14 10:48:00 -0700508 bool climbed_ = false;
509
510 enum class ReleaseButtonMode {
511 kBallIntake,
512 kRelease,
513 };
514
515 ReleaseButtonMode release_mode_ = ReleaseButtonMode::kRelease;
516 aos::monotonic_clock::time_point last_release_button_press_ =
517 aos::monotonic_clock::min_time;
518
Tyler Chatowe0241452019-03-08 21:07:50 -0800519 VisionControl vision_control_;
520 ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700521 ::aos::monotonic_clock::time_point last_vision_control_ =
522 ::aos::monotonic_clock::time_point::min();
Austin Schuhaab7e162019-03-13 22:44:58 -0700523
524 // Time at which we last did not have a game piece.
525 ::aos::monotonic_clock::time_point last_not_has_piece_ =
526 ::aos::monotonic_clock::time_point::min();
Sabina Davis91b23602019-01-21 00:06:01 -0800527};
528
529} // namespace joysticks
530} // namespace input
531} // namespace y2019
532
533int main() {
534 ::aos::Init(-1);
535 ::aos::ShmEventLoop event_loop;
536 ::y2019::input::joysticks::Reader reader(&event_loop);
537 reader.Run();
538 ::aos::Cleanup();
539}