blob: e354647fab043d19146596c38b1a0a3315288cdf [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;
31using ::aos::input::driver_station::ButtonLocation;
32using ::aos::input::driver_station::ControlBit;
33using ::aos::input::driver_station::JoystickAxis;
34using ::aos::input::driver_station::POVLocation;
Tyler Chatowe0241452019-03-08 21:07:50 -080035using ::aos::events::ProtoTXUdpSocket;
Sabina Davis91b23602019-01-21 00:06:01 -080036
Austin Schuhaab7e162019-03-13 22:44:58 -070037namespace chrono = ::std::chrono;
38
Sabina Davis91b23602019-01-21 00:06:01 -080039namespace y2019 {
40namespace input {
41namespace joysticks {
42
Tyler Chatowe0241452019-03-08 21:07:50 -080043using google::protobuf::StringPrintf;
44
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080045struct ElevatorWristPosition {
46 double elevator;
47 double wrist;
48};
Sabina Davis91b23602019-01-21 00:06:01 -080049
Tyler Chatow5eeee902019-04-12 20:47:48 -070050const ButtonLocation kSuctionBall(4, 2);
51const ButtonLocation kSuctionHatch(3, 15);
52const ButtonLocation kDeployStilt(4, 1);
53const ButtonLocation kHalfStilt(4, 3);
54const ButtonLocation kFallOver(3, 16);
55
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080056const ButtonLocation kRocketForwardLower(5, 1);
57const ButtonLocation kRocketForwardMiddle(5, 2);
58const ButtonLocation kRocketForwardUpper(5, 4);
59const ButtonLocation kCargoForward(5, 3);
60
61const POVLocation kRocketBackwardUnpressed(5, -1);
62const POVLocation kRocketBackwardLower(5, 180);
63const POVLocation kRocketBackwardMiddle(5, 90);
64const POVLocation kRocketBackwardUpper(5, 0);
65const POVLocation kCargoBackward(5, 270);
66
67const ButtonLocation kPanelSwitch(5, 7);
68const ButtonLocation kCargoSwitch(5, 8);
69
70const ButtonLocation kBallHPIntakeForward(5, 6);
71const ButtonLocation kBallHPIntakeBackward(5, 5);
72const JoystickAxis kBallOutake(5, 3);
73const JoystickAxis kBallIntake(5, 4);
74
75const ButtonLocation kPanelHPIntakeForward(5, 6);
76const ButtonLocation kPanelHPIntakeBackward(5, 5);
77
78const ButtonLocation kRelease(2, 4);
James Kuszmaulccc368b2019-04-11 20:00:07 -070079// Reuse quickturn for the cancel button.
80const ButtonLocation kCancelAutoMode(2, 3);
Tyler Chatow5eeee902019-04-12 20:47:48 -070081const ButtonLocation kReleaseButtonBoard(3, 4);
82const ButtonLocation kResetLocalizerLeftForwards(3, 10);
83const ButtonLocation kResetLocalizerLeftBackwards(3, 9);
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080084
Tyler Chatow5eeee902019-04-12 20:47:48 -070085const ButtonLocation kResetLocalizerRightForwards(3, 8);
86const ButtonLocation kResetLocalizerRightBackwards(3, 7);
Tyler Chatow07c906b2019-03-09 21:29:06 -080087
James Kuszmaul518640d2019-04-13 15:50:50 -070088const ButtonLocation kResetLocalizerLeft(3, 11);
89const ButtonLocation kResetLocalizerRight(3, 13);
90
Tyler Chatow5eeee902019-04-12 20:47:48 -070091const ButtonLocation kNearCargoHint(3, 3);
92const ButtonLocation kMidCargoHint(3, 5);
93const ButtonLocation kFarCargoHint(3, 6);
James Kuszmaul7d1ef442019-03-23 20:20:50 -070094
James Kuszmaul518640d2019-04-13 15:50:50 -070095const JoystickAxis kCargoSelectorY(5, 6);
96const JoystickAxis kCargoSelectorX(5, 5);
97
Tyler Chatow5eeee902019-04-12 20:47:48 -070098const ButtonLocation kCameraLog(3, 14);
Austin Schuh4e2629d2019-03-28 14:44:37 -070099
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800100const ElevatorWristPosition kStowPos{0.36, 0.0};
Austin Schuh02be8b92019-03-24 16:04:14 -0700101const ElevatorWristPosition kClimbPos{0.0, M_PI / 4.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800102
Austin Schuh139f59d2019-03-17 18:16:13 -0700103const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.01, M_PI / 2.0};
104const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.015, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800105
106const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
107const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
108
Sabina Davise48004f2019-03-02 23:15:24 -0800109const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0};
110const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800111
Sabina Davise48004f2019-03-02 23:15:24 -0800112const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0};
113const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800114
Sabina Davise6fe6c52019-03-03 15:48:51 -0800115const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0};
116const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0};
117
Austin Schuh6c5deb12019-03-24 16:48:32 -0700118const ElevatorWristPosition kBallForwardLowerPos{0.21, 1.27};
Austin Schuh20dc0502019-03-30 07:24:07 -0700119const ElevatorWristPosition kBallBackwardLowerPos{0.43, -1.99};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800120
Austin Schuh20dc0502019-03-30 07:24:07 -0700121const ElevatorWristPosition kBallForwardMiddlePos{0.925, 1.21};
122const ElevatorWristPosition kBallBackwardMiddlePos{1.19, -1.98};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800123
Austin Schuh139f59d2019-03-17 18:16:13 -0700124const ElevatorWristPosition kBallForwardUpperPos{1.51, 0.961};
125const ElevatorWristPosition kBallBackwardUpperPos{1.44, -1.217};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800126
Austin Schuh20dc0502019-03-30 07:24:07 -0700127const ElevatorWristPosition kBallCargoForwardPos{0.59, 1.2};
128const ElevatorWristPosition kBallCargoBackwardPos{0.868265, -2.1};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800129
Sabina Davise6fe6c52019-03-03 15:48:51 -0800130const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097};
131const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800132
Austin Schuh20dc0502019-03-30 07:24:07 -0700133const ElevatorWristPosition kBallIntakePos{0.309, 2.13};
Austin Schuh2cf16b82019-02-15 23:23:22 -0800134
Sabina Davis91b23602019-01-21 00:06:01 -0800135class Reader : public ::aos::input::ActionJoystickInput {
136 public:
137 Reader(::aos::EventLoop *event_loop)
138 : ::aos::input::ActionJoystickInput(
139 event_loop,
James Kuszmaulccc368b2019-04-11 20:00:07 -0700140 ::y2019::control_loops::drivetrain::GetDrivetrainConfig(),
141 {.run_teleop_in_auto = true,
Austin Schuhc087b102019-05-12 15:33:12 -0700142 .cancel_auto_button = kCancelAutoMode}),
143 target_selector_hint_sender_(
144 event_loop->MakeSender<
145 ::y2019::control_loops::drivetrain::TargetSelectorHint>(
Austin Schuheb99d072019-05-12 21:03:38 -0700146 ".y2019.control_loops.drivetrain.target_selector_hint")),
147 localizer_control_sender_(
148 event_loop->MakeSender<
149 ::frc971::control_loops::drivetrain::LocalizerControl>(
Austin Schuh5671a8c2019-05-19 17:01:04 -0700150 ".frc971.control_loops.drivetrain.localizer_control")),
151 camera_log_sender_(
152 event_loop->MakeSender<::y2019::CameraLog>(".y2019.camera_log")) {
Tyler Chatowe0241452019-03-08 21:07:50 -0800153 const uint16_t team = ::aos::network::GetTeamNumber();
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800154 superstructure_queue.goal.FetchLatest();
155 if (superstructure_queue.goal.get()) {
Sabina Davisc6329342019-03-01 20:44:42 -0800156 grab_piece_ = superstructure_queue.goal->suction.grab_piece;
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700157 switch_ball_ = superstructure_queue.goal->suction.gamepiece_mode == 0;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800158 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800159 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
160 StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000));
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800161 }
Sabina Davis91b23602019-01-21 00:06:01 -0800162
Austin Schuha78857f2019-03-13 22:43:41 -0700163 void AutoEnded() override {
164 LOG(INFO, "Auto ended, assuming disc and have piece\n");
165 grab_piece_ = true;
166 switch_ball_ = false;
167 }
168
169 void HandleTeleop(const ::aos::input::driver_station::Data &data) override {
Austin Schuhaab7e162019-03-13 22:44:58 -0700170 ::aos::monotonic_clock::time_point monotonic_now =
171 ::aos::monotonic_clock::now();
Sabina Davis91b23602019-01-21 00:06:01 -0800172 superstructure_queue.position.FetchLatest();
173 superstructure_queue.status.FetchLatest();
174 if (!superstructure_queue.status.get() ||
175 !superstructure_queue.position.get()) {
Austin Schuhe2f22482019-04-13 23:05:43 -0700176 LOG(ERROR, "Got no superstructure status or position packet.\n");
Sabina Davis91b23602019-01-21 00:06:01 -0800177 return;
178 }
179
Austin Schuhaab7e162019-03-13 22:44:58 -0700180 if (!superstructure_queue.status->has_piece) {
181 last_not_has_piece_ = monotonic_now;
182 }
183
Sabina Davis91b23602019-01-21 00:06:01 -0800184 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
185
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700186 {
Austin Schuhc087b102019-05-12 15:33:12 -0700187 auto target_hint = target_selector_hint_sender_.MakeMessage();
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700188 if (data.IsPressed(kNearCargoHint)) {
189 target_hint->suggested_target = 1;
190 } else if (data.IsPressed(kMidCargoHint)) {
191 target_hint->suggested_target = 2;
192 } else if (data.IsPressed(kFarCargoHint)) {
193 target_hint->suggested_target = 3;
194 } else {
James Kuszmaul518640d2019-04-13 15:50:50 -0700195 const double cargo_joy_y = data.GetAxis(kCargoSelectorY);
196 const double cargo_joy_x = data.GetAxis(kCargoSelectorX);
197 if (cargo_joy_y > 0.5) {
James Kuszmaul518640d2019-04-13 15:50:50 -0700198 target_hint->suggested_target = 1;
James Kuszmaul1b822b42019-04-14 14:27:35 -0700199 } else if (cargo_joy_y < -0.5) {
200 target_hint->suggested_target = 3;
James Kuszmaul518640d2019-04-13 15:50:50 -0700201 } else if (::std::abs(cargo_joy_x) > 0.5) {
202 target_hint->suggested_target = 2;
203 } else {
204 target_hint->suggested_target = 0;
205 }
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700206 }
207 if (!target_hint.Send()) {
208 LOG(ERROR, "Failed to send target selector hint.\n");
209 }
210 }
211
James Kuszmaul518640d2019-04-13 15:50:50 -0700212 if (data.PosEdge(kResetLocalizerLeft)) {
Austin Schuheb99d072019-05-12 21:03:38 -0700213 auto localizer_resetter = localizer_control_sender_.MakeMessage();
James Kuszmaul518640d2019-04-13 15:50:50 -0700214 // Start at the left feeder station.
215 localizer_resetter->x = 0.6;
216 localizer_resetter->y = 3.4;
217 localizer_resetter->keep_current_theta = true;
218
219 if (!localizer_resetter.Send()) {
220 LOG(ERROR, "Failed to reset localizer.\n");
221 }
222 }
223
224 if (data.PosEdge(kResetLocalizerRight)) {
Austin Schuheb99d072019-05-12 21:03:38 -0700225 auto localizer_resetter = localizer_control_sender_.MakeMessage();
James Kuszmaul518640d2019-04-13 15:50:50 -0700226 // Start at the left feeder station.
227 localizer_resetter->x = 0.6;
228 localizer_resetter->y = -3.4;
229 localizer_resetter->keep_current_theta = true;
230
231 if (!localizer_resetter.Send()) {
232 LOG(ERROR, "Failed to reset localizer.\n");
233 }
234 }
235
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700236 if (data.PosEdge(kResetLocalizerLeftForwards)) {
Austin Schuheb99d072019-05-12 21:03:38 -0700237 auto localizer_resetter = localizer_control_sender_.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700238 // Start at the left feeder station.
James Kuszmauld8deb682019-03-10 10:38:42 -0700239 localizer_resetter->x = 0.4;
240 localizer_resetter->y = 3.4;
241 localizer_resetter->theta = 0.0;
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700242
243 if (!localizer_resetter.Send()) {
244 LOG(ERROR, "Failed to reset localizer.\n");
245 }
246 }
247
248 if (data.PosEdge(kResetLocalizerLeftBackwards)) {
Austin Schuheb99d072019-05-12 21:03:38 -0700249 auto localizer_resetter = localizer_control_sender_.MakeMessage();
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700250 // Start at the left feeder station.
251 localizer_resetter->x = 0.4;
252 localizer_resetter->y = 3.4;
253 localizer_resetter->theta = M_PI;
254
255 if (!localizer_resetter.Send()) {
256 LOG(ERROR, "Failed to reset localizer.\n");
257 }
258 }
259
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700260 if (data.PosEdge(kResetLocalizerRightForwards)) {
Austin Schuheb99d072019-05-12 21:03:38 -0700261 auto localizer_resetter = localizer_control_sender_.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700262 // Start at the right feeder station.
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700263 localizer_resetter->x = 0.4;
264 localizer_resetter->y = -3.4;
265 localizer_resetter->theta = 0.0;
266
267 if (!localizer_resetter.Send()) {
268 LOG(ERROR, "Failed to reset localizer.\n");
269 }
270 }
271
272 if (data.PosEdge(kResetLocalizerRightBackwards)) {
Austin Schuheb99d072019-05-12 21:03:38 -0700273 auto localizer_resetter = localizer_control_sender_.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700274 // Start at the right feeder station.
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700275 localizer_resetter->x = 0.4;
276 localizer_resetter->y = -3.4;
277 localizer_resetter->theta = M_PI;
278
James Kuszmauld8deb682019-03-10 10:38:42 -0700279 if (!localizer_resetter.Send()) {
280 LOG(ERROR, "Failed to reset localizer.\n");
281 }
282 }
283
James Kuszmaul13738862019-04-14 10:48:00 -0700284 if (data.PosEdge(kRelease) &&
285 monotonic_now >
286 last_release_button_press_ + ::std::chrono::milliseconds(500)) {
287 if (superstructure_queue.status->has_piece) {
288 release_mode_ = ReleaseButtonMode::kRelease;
289 } else {
290 release_mode_ = ReleaseButtonMode::kBallIntake;
291 }
292 }
293
294 if (data.IsPressed(kRelease)) {
295 last_release_button_press_ = monotonic_now;
296 }
297
Austin Schuh1a8fb572019-05-08 20:07:58 -0700298 LOG(INFO, "has_piece: %d\n", superstructure_queue.status->has_piece);
Austin Schuh1a17e132019-02-17 15:05:06 -0800299 if (data.IsPressed(kSuctionBall)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800300 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800301 } else if (data.IsPressed(kSuctionHatch)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800302 grab_piece_ = true;
James Kuszmaul13738862019-04-14 10:48:00 -0700303 } else if ((release_mode_ == ReleaseButtonMode::kRelease &&
304 data.IsPressed(kRelease)) ||
Tyler Chatow5eeee902019-04-12 20:47:48 -0700305 data.IsPressed(kReleaseButtonBoard) ||
Austin Schuh1a17e132019-02-17 15:05:06 -0800306 !superstructure_queue.status->has_piece) {
Sabina Davisc6329342019-03-01 20:44:42 -0800307 grab_piece_ = false;
Austin Schuh1a8fb572019-05-08 20:07:58 -0700308 LOG(INFO, "releasing due to other thing\n");
Austin Schuh1a17e132019-02-17 15:05:06 -0800309 }
Sabina Davis91b23602019-01-21 00:06:01 -0800310
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800311 if (data.IsPressed(kRocketBackwardUnpressed)) {
312 elevator_wrist_pos_ = kStowPos;
313 }
314 new_superstructure_goal->intake.unsafe_goal = -1.2;
315 new_superstructure_goal->roller_voltage = 0.0;
316
James Kuszmaul13738862019-04-14 10:48:00 -0700317 const bool kDoBallIntake =
318 (!climbed_ && release_mode_ == ReleaseButtonMode::kBallIntake &&
319 data.IsPressed(kRelease)) ||
320 data.GetAxis(kBallIntake) > 0.9;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800321 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
322
323 if (data.IsPressed(kPanelSwitch)) {
324 switch_ball_ = false;
325 } else if (data.IsPressed(kCargoSwitch)) {
326 switch_ball_ = true;
327 }
328
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800329 if (switch_ball_) {
330 if (superstructure_queue.status->has_piece) {
331 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800332 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800333
334 // Go to intake position and apply vacuum
335 if (data.IsPressed(kBallHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800336 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800337 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
338 } else if (data.IsPressed(kBallHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800339 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800340 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
341 }
342
343 // Go to elevator/wrist position. Overrides intake position if pressed so
344 // we can re-grab the ball.
345 if (data.IsPressed(kRocketForwardLower)) {
346 elevator_wrist_pos_ = kBallForwardLowerPos;
347 } else if (data.IsPressed(kRocketBackwardLower)) {
348 elevator_wrist_pos_ = kBallBackwardLowerPos;
349 } else if (data.IsPressed(kRocketForwardMiddle)) {
350 elevator_wrist_pos_ = kBallForwardMiddlePos;
351 } else if (data.IsPressed(kRocketBackwardMiddle)) {
352 elevator_wrist_pos_ = kBallBackwardMiddlePos;
353 } else if (data.IsPressed(kRocketForwardUpper)) {
354 elevator_wrist_pos_ = kBallForwardUpperPos;
355 } else if (data.IsPressed(kRocketBackwardUpper)) {
356 elevator_wrist_pos_ = kBallBackwardUpperPos;
357 } else if (data.IsPressed(kCargoForward)) {
358 elevator_wrist_pos_ = kBallCargoForwardPos;
359 } else if (data.IsPressed(kCargoBackward)) {
360 elevator_wrist_pos_ = kBallCargoBackwardPos;
361 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800362 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800363 if (data.IsPressed(kPanelHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800364 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800365 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
366 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800367 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800368 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
369 }
370
371 // Go to elevator/wrist position. Overrides intake position if pressed so
372 // we can re-grab the panel.
373 if (data.IsPressed(kRocketForwardLower)) {
374 elevator_wrist_pos_ = kPanelForwardLowerPos;
375 } else if (data.IsPressed(kRocketBackwardLower)) {
376 elevator_wrist_pos_ = kPanelBackwardLowerPos;
377 } else if (data.IsPressed(kRocketForwardMiddle)) {
378 elevator_wrist_pos_ = kPanelForwardMiddlePos;
379 } else if (data.IsPressed(kRocketBackwardMiddle)) {
380 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
381 } else if (data.IsPressed(kRocketForwardUpper)) {
382 elevator_wrist_pos_ = kPanelForwardUpperPos;
383 } else if (data.IsPressed(kRocketBackwardUpper)) {
384 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800385 } else if (data.IsPressed(kCargoForward)) {
386 elevator_wrist_pos_ = kPanelCargoForwardPos;
387 } else if (data.IsPressed(kCargoBackward)) {
388 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800389 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800390 }
391
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800392 if (switch_ball_) {
393 if (kDoBallOutake ||
Austin Schuhaab7e162019-03-13 22:44:58 -0700394 (kDoBallIntake &&
Austin Schuh20dc0502019-03-30 07:24:07 -0700395 monotonic_now < last_not_has_piece_ + chrono::milliseconds(200))) {
Austin Schuhd8098b42019-05-08 20:48:17 -0700396 new_superstructure_goal->intake.unsafe_goal = 0.83;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800397 }
Austin Schuh23a51632019-02-19 16:50:36 -0800398
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800399 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
400 elevator_wrist_pos_ = kBallIntakePos;
401 new_superstructure_goal->roller_voltage = 9.0;
Sabina Davisc6329342019-03-01 20:44:42 -0800402 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800403 } else {
404 if (kDoBallOutake) {
405 new_superstructure_goal->roller_voltage = -6.0;
406 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800407 new_superstructure_goal->roller_voltage = 0.0;
408 }
409 }
Austin Schuh23a51632019-02-19 16:50:36 -0800410 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800411
Austin Schuh457bde32019-03-17 18:16:41 -0700412 constexpr double kDeployStiltPosition = 0.5;
413
Austin Schuh457bde32019-03-17 18:16:41 -0700414 if (data.IsPressed(kFallOver)) {
415 new_superstructure_goal->stilts.unsafe_goal = 0.71;
416 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
417 new_superstructure_goal->stilts.profile_params.max_acceleration = 1.25;
Austin Schuhe2f22482019-04-13 23:05:43 -0700418 } else if (data.IsPressed(kHalfStilt)) {
419 was_above_ = false;
420 new_superstructure_goal->stilts.unsafe_goal = 0.345;
421 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
422 } else if (data.IsPressed(kDeployStilt) || was_above_) {
Austin Schuh457bde32019-03-17 18:16:41 -0700423 new_superstructure_goal->stilts.unsafe_goal = kDeployStiltPosition;
424 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
425 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Austin Schuh457bde32019-03-17 18:16:41 -0700426 } else {
427 new_superstructure_goal->stilts.unsafe_goal = 0.005;
428 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
429 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
430 }
431
James Kuszmaul0448f142019-03-24 16:22:34 -0700432 if (superstructure_queue.status->stilts.position > 0.1) {
Austin Schuh02be8b92019-03-24 16:04:14 -0700433 elevator_wrist_pos_ = kClimbPos;
James Kuszmaul13738862019-04-14 10:48:00 -0700434 climbed_ = true;
Austin Schuhd8a18bb2019-03-24 19:21:38 -0700435 new_superstructure_goal->wrist.profile_params.max_acceleration = 10;
436 new_superstructure_goal->elevator.profile_params.max_acceleration = 6;
Austin Schuh02be8b92019-03-24 16:04:14 -0700437 }
438
Austin Schuhe2f22482019-04-13 23:05:43 -0700439 // If we've been asked to go above deploy and made it up that high, latch
440 // was_above.
441 if (new_superstructure_goal->stilts.unsafe_goal > kDeployStiltPosition &&
442 superstructure_queue.status->stilts.position >= kDeployStiltPosition) {
443 was_above_ = true;
Austin Schuh255ff342019-04-14 19:56:12 -0700444 } else if ((superstructure_queue.position->platform_left_detect &&
Austin Schuhe2f22482019-04-13 23:05:43 -0700445 superstructure_queue.position->platform_right_detect) &&
446 !data.IsPressed(kDeployStilt) && !data.IsPressed(kFallOver)) {
Austin Schuhe2f22482019-04-13 23:05:43 -0700447 was_above_ = false;
448 }
449
Austin Schuh457bde32019-03-17 18:16:41 -0700450 if (superstructure_queue.status->stilts.position > kDeployStiltPosition &&
451 new_superstructure_goal->stilts.unsafe_goal == kDeployStiltPosition) {
452 new_superstructure_goal->stilts.profile_params.max_velocity = 0.30;
453 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
454 }
455
James Kuszmaul13738862019-04-14 10:48:00 -0700456 if ((release_mode_ == ReleaseButtonMode::kRelease &&
457 data.IsPressed(kRelease)) ||
458 data.IsPressed(kReleaseButtonBoard)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800459 grab_piece_ = false;
Austin Schuh1a8fb572019-05-08 20:07:58 -0700460 LOG(INFO, "Releasing due to button\n");
Austin Schuh1a17e132019-02-17 15:05:06 -0800461 }
462
Sabina Davisc6329342019-03-01 20:44:42 -0800463 if (switch_ball_) {
464 new_superstructure_goal->suction.gamepiece_mode = 0;
465 } else {
466 new_superstructure_goal->suction.gamepiece_mode = 1;
467 }
468
Tyler Chatowe0241452019-03-08 21:07:50 -0800469 vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0);
470
Sabina Davisc6329342019-03-01 20:44:42 -0800471 new_superstructure_goal->suction.grab_piece = grab_piece_;
Sabina Davis91b23602019-01-21 00:06:01 -0800472
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800473 new_superstructure_goal->elevator.unsafe_goal =
474 elevator_wrist_pos_.elevator;
475 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800476
477 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
478 if (!new_superstructure_goal.Send()) {
479 LOG(ERROR, "Sending superstructure goal failed.\n");
480 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800481
Austin Schuhaab7e162019-03-13 22:44:58 -0700482 if (monotonic_now >
483 last_vision_control_ + ::std::chrono::milliseconds(50)) {
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700484 video_tx_->Send(vision_control_);
Austin Schuhaab7e162019-03-13 22:44:58 -0700485 last_vision_control_ = monotonic_now;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700486 }
Austin Schuh4e2629d2019-03-28 14:44:37 -0700487
488 {
Austin Schuh5671a8c2019-05-19 17:01:04 -0700489 auto camera_log_message = camera_log_sender_.MakeMessage();
Austin Schuh4e2629d2019-03-28 14:44:37 -0700490 camera_log_message->log = data.IsPressed(kCameraLog);
491 LOG_STRUCT(DEBUG, "camera_log", *camera_log_message);
492 camera_log_message.Send();
493 }
Sabina Davis91b23602019-01-21 00:06:01 -0800494 }
495
496 private:
Austin Schuha9644062019-03-28 14:31:52 -0700497 uint32_t GetAutonomousMode() override {
498 ::frc971::autonomous::auto_mode.FetchLatest();
499 if (::frc971::autonomous::auto_mode.get() == nullptr) {
500 LOG(WARNING, "no auto mode values\n");
501 return 0;
502 }
503 return ::frc971::autonomous::auto_mode->mode;
504 }
505
Austin Schuhc087b102019-05-12 15:33:12 -0700506 ::aos::Sender<::y2019::control_loops::drivetrain::TargetSelectorHint>
507 target_selector_hint_sender_;
508
Austin Schuheb99d072019-05-12 21:03:38 -0700509 ::aos::Sender<::frc971::control_loops::drivetrain::LocalizerControl>
510 localizer_control_sender_;
511
Austin Schuh5671a8c2019-05-19 17:01:04 -0700512 ::aos::Sender<::y2019::CameraLog> camera_log_sender_;
513
Austin Schuhe2f22482019-04-13 23:05:43 -0700514 // Bool to track if we've been above the deploy position. Once this bool is
515 // set, don't let the stilts retract until we see the platform.
516 bool was_above_ = false;
517
Sabina Davis91b23602019-01-21 00:06:01 -0800518 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800519 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Sabina Davisc6329342019-03-01 20:44:42 -0800520 bool grab_piece_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800521
522 bool switch_ball_ = false;
Tyler Chatowe0241452019-03-08 21:07:50 -0800523
James Kuszmaul13738862019-04-14 10:48:00 -0700524 bool climbed_ = false;
525
526 enum class ReleaseButtonMode {
527 kBallIntake,
528 kRelease,
529 };
530
531 ReleaseButtonMode release_mode_ = ReleaseButtonMode::kRelease;
532 aos::monotonic_clock::time_point last_release_button_press_ =
533 aos::monotonic_clock::min_time;
534
Tyler Chatowe0241452019-03-08 21:07:50 -0800535 VisionControl vision_control_;
536 ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700537 ::aos::monotonic_clock::time_point last_vision_control_ =
538 ::aos::monotonic_clock::time_point::min();
Austin Schuhaab7e162019-03-13 22:44:58 -0700539
540 // Time at which we last did not have a game piece.
541 ::aos::monotonic_clock::time_point last_not_has_piece_ =
542 ::aos::monotonic_clock::time_point::min();
Sabina Davis91b23602019-01-21 00:06:01 -0800543};
544
545} // namespace joysticks
546} // namespace input
547} // namespace y2019
548
549int main() {
550 ::aos::Init(-1);
551 ::aos::ShmEventLoop event_loop;
552 ::y2019::input::joysticks::Reader reader(&event_loop);
553 reader.Run();
554 ::aos::Cleanup();
555}