blob: 5f5f06cfdfc5b8f5a9e960c8f18e169fd471ee0e [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
Sabina Davis91b23602019-01-21 00:06:01 -080030using ::aos::input::driver_station::ButtonLocation;
31using ::aos::input::driver_station::ControlBit;
32using ::aos::input::driver_station::JoystickAxis;
33using ::aos::input::driver_station::POVLocation;
Tyler Chatowe0241452019-03-08 21:07:50 -080034using ::aos::events::ProtoTXUdpSocket;
Sabina Davis91b23602019-01-21 00:06:01 -080035
Austin Schuhaab7e162019-03-13 22:44:58 -070036namespace chrono = ::std::chrono;
37
Sabina Davis91b23602019-01-21 00:06:01 -080038namespace y2019 {
39namespace input {
40namespace joysticks {
41
Tyler Chatowe0241452019-03-08 21:07:50 -080042using google::protobuf::StringPrintf;
43
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080044struct ElevatorWristPosition {
45 double elevator;
46 double wrist;
47};
Sabina Davis91b23602019-01-21 00:06:01 -080048
Tyler Chatow5eeee902019-04-12 20:47:48 -070049const ButtonLocation kSuctionBall(4, 2);
50const ButtonLocation kSuctionHatch(3, 15);
51const ButtonLocation kDeployStilt(4, 1);
52const ButtonLocation kHalfStilt(4, 3);
53const ButtonLocation kFallOver(3, 16);
54
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080055const ButtonLocation kRocketForwardLower(5, 1);
56const ButtonLocation kRocketForwardMiddle(5, 2);
57const ButtonLocation kRocketForwardUpper(5, 4);
58const ButtonLocation kCargoForward(5, 3);
59
60const POVLocation kRocketBackwardUnpressed(5, -1);
61const POVLocation kRocketBackwardLower(5, 180);
62const POVLocation kRocketBackwardMiddle(5, 90);
63const POVLocation kRocketBackwardUpper(5, 0);
64const POVLocation kCargoBackward(5, 270);
65
66const ButtonLocation kPanelSwitch(5, 7);
67const ButtonLocation kCargoSwitch(5, 8);
68
69const ButtonLocation kBallHPIntakeForward(5, 6);
70const ButtonLocation kBallHPIntakeBackward(5, 5);
71const JoystickAxis kBallOutake(5, 3);
72const JoystickAxis kBallIntake(5, 4);
73
74const ButtonLocation kPanelHPIntakeForward(5, 6);
75const ButtonLocation kPanelHPIntakeBackward(5, 5);
76
77const ButtonLocation kRelease(2, 4);
James Kuszmaulccc368b2019-04-11 20:00:07 -070078// Reuse quickturn for the cancel button.
79const ButtonLocation kCancelAutoMode(2, 3);
Tyler Chatow5eeee902019-04-12 20:47:48 -070080const ButtonLocation kReleaseButtonBoard(3, 4);
81const ButtonLocation kResetLocalizerLeftForwards(3, 10);
82const ButtonLocation kResetLocalizerLeftBackwards(3, 9);
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080083
Tyler Chatow5eeee902019-04-12 20:47:48 -070084const ButtonLocation kResetLocalizerRightForwards(3, 8);
85const ButtonLocation kResetLocalizerRightBackwards(3, 7);
Tyler Chatow07c906b2019-03-09 21:29:06 -080086
James Kuszmaul518640d2019-04-13 15:50:50 -070087const ButtonLocation kResetLocalizerLeft(3, 11);
88const ButtonLocation kResetLocalizerRight(3, 13);
89
Tyler Chatow5eeee902019-04-12 20:47:48 -070090const ButtonLocation kNearCargoHint(3, 3);
91const ButtonLocation kMidCargoHint(3, 5);
92const ButtonLocation kFarCargoHint(3, 6);
James Kuszmaul7d1ef442019-03-23 20:20:50 -070093
James Kuszmaul518640d2019-04-13 15:50:50 -070094const JoystickAxis kCargoSelectorY(5, 6);
95const JoystickAxis kCargoSelectorX(5, 5);
96
Tyler Chatow5eeee902019-04-12 20:47:48 -070097const ButtonLocation kCameraLog(3, 14);
Austin Schuh4e2629d2019-03-28 14:44:37 -070098
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080099const ElevatorWristPosition kStowPos{0.36, 0.0};
Austin Schuh02be8b92019-03-24 16:04:14 -0700100const ElevatorWristPosition kClimbPos{0.0, M_PI / 4.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800101
Austin Schuh139f59d2019-03-17 18:16:13 -0700102const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.01, M_PI / 2.0};
103const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.015, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800104
105const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
106const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
107
Sabina Davise48004f2019-03-02 23:15:24 -0800108const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0};
109const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800110
Sabina Davise48004f2019-03-02 23:15:24 -0800111const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0};
112const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800113
Sabina Davise6fe6c52019-03-03 15:48:51 -0800114const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0};
115const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0};
116
Austin Schuh6c5deb12019-03-24 16:48:32 -0700117const ElevatorWristPosition kBallForwardLowerPos{0.21, 1.27};
Austin Schuh20dc0502019-03-30 07:24:07 -0700118const ElevatorWristPosition kBallBackwardLowerPos{0.43, -1.99};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800119
Austin Schuh20dc0502019-03-30 07:24:07 -0700120const ElevatorWristPosition kBallForwardMiddlePos{0.925, 1.21};
121const ElevatorWristPosition kBallBackwardMiddlePos{1.19, -1.98};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800122
Austin Schuh139f59d2019-03-17 18:16:13 -0700123const ElevatorWristPosition kBallForwardUpperPos{1.51, 0.961};
124const ElevatorWristPosition kBallBackwardUpperPos{1.44, -1.217};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800125
Austin Schuh20dc0502019-03-30 07:24:07 -0700126const ElevatorWristPosition kBallCargoForwardPos{0.59, 1.2};
127const ElevatorWristPosition kBallCargoBackwardPos{0.868265, -2.1};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800128
Sabina Davise6fe6c52019-03-03 15:48:51 -0800129const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097};
130const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800131
Austin Schuh20dc0502019-03-30 07:24:07 -0700132const ElevatorWristPosition kBallIntakePos{0.309, 2.13};
Austin Schuh2cf16b82019-02-15 23:23:22 -0800133
Sabina Davis91b23602019-01-21 00:06:01 -0800134class Reader : public ::aos::input::ActionJoystickInput {
135 public:
136 Reader(::aos::EventLoop *event_loop)
137 : ::aos::input::ActionJoystickInput(
138 event_loop,
James Kuszmaulccc368b2019-04-11 20:00:07 -0700139 ::y2019::control_loops::drivetrain::GetDrivetrainConfig(),
Austin Schuhbfb04122019-05-22 21:16:51 -0700140 ::aos::input::DrivetrainInputReader::InputType::kPistol,
James Kuszmaulccc368b2019-04-11 20:00:07 -0700141 {.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_(
Austin Schuh170f4952019-06-29 18:58:30 -0700152 event_loop->MakeSender<::y2019::CameraLog>(".y2019.camera_log")),
153 superstructure_goal_fetcher_(event_loop->MakeFetcher<
154 ::y2019::control_loops::superstructure::
155 SuperstructureQueue::Goal>(
156 ".y2019.control_loops.superstructure.superstructure_queue.goal")),
157 superstructure_goal_sender_(event_loop->MakeSender<
158 ::y2019::control_loops::superstructure::
159 SuperstructureQueue::Goal>(
160 ".y2019.control_loops.superstructure.superstructure_queue.goal")),
161 superstructure_position_fetcher_(
162 event_loop->MakeFetcher<::y2019::control_loops::superstructure::
163 SuperstructureQueue::Position>(
164 ".y2019.control_loops.superstructure.superstructure_queue."
165 "position")),
166 superstructure_status_fetcher_(
167 event_loop->MakeFetcher<::y2019::control_loops::superstructure::
168 SuperstructureQueue::Status>(
169 ".y2019.control_loops.superstructure.superstructure_queue."
170 "status")) {
Tyler Chatowe0241452019-03-08 21:07:50 -0800171 const uint16_t team = ::aos::network::GetTeamNumber();
Austin Schuh170f4952019-06-29 18:58:30 -0700172 superstructure_goal_fetcher_.Fetch();
173 if (superstructure_goal_fetcher_.get()) {
174 grab_piece_ = superstructure_goal_fetcher_->suction.grab_piece;
175 switch_ball_ = superstructure_goal_fetcher_->suction.gamepiece_mode == 0;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800176 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800177 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
178 StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000));
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800179 }
Sabina Davis91b23602019-01-21 00:06:01 -0800180
Austin Schuha78857f2019-03-13 22:43:41 -0700181 void AutoEnded() override {
182 LOG(INFO, "Auto ended, assuming disc and have piece\n");
183 grab_piece_ = true;
184 switch_ball_ = false;
185 }
186
187 void HandleTeleop(const ::aos::input::driver_station::Data &data) override {
Austin Schuhaab7e162019-03-13 22:44:58 -0700188 ::aos::monotonic_clock::time_point monotonic_now =
189 ::aos::monotonic_clock::now();
Austin Schuh170f4952019-06-29 18:58:30 -0700190 superstructure_position_fetcher_.Fetch();
191 superstructure_status_fetcher_.Fetch();
192 if (!superstructure_status_fetcher_.get() ||
193 !superstructure_position_fetcher_.get()) {
Austin Schuhe2f22482019-04-13 23:05:43 -0700194 LOG(ERROR, "Got no superstructure status or position packet.\n");
Sabina Davis91b23602019-01-21 00:06:01 -0800195 return;
196 }
197
Austin Schuh170f4952019-06-29 18:58:30 -0700198 if (!superstructure_status_fetcher_->has_piece) {
Austin Schuhaab7e162019-03-13 22:44:58 -0700199 last_not_has_piece_ = monotonic_now;
200 }
201
Austin Schuh170f4952019-06-29 18:58:30 -0700202 auto new_superstructure_goal = superstructure_goal_sender_.MakeMessage();
Sabina Davis91b23602019-01-21 00:06:01 -0800203
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700204 {
Austin Schuhc087b102019-05-12 15:33:12 -0700205 auto target_hint = target_selector_hint_sender_.MakeMessage();
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700206 if (data.IsPressed(kNearCargoHint)) {
207 target_hint->suggested_target = 1;
208 } else if (data.IsPressed(kMidCargoHint)) {
209 target_hint->suggested_target = 2;
210 } else if (data.IsPressed(kFarCargoHint)) {
211 target_hint->suggested_target = 3;
212 } else {
James Kuszmaul518640d2019-04-13 15:50:50 -0700213 const double cargo_joy_y = data.GetAxis(kCargoSelectorY);
214 const double cargo_joy_x = data.GetAxis(kCargoSelectorX);
215 if (cargo_joy_y > 0.5) {
James Kuszmaul518640d2019-04-13 15:50:50 -0700216 target_hint->suggested_target = 1;
James Kuszmaul1b822b42019-04-14 14:27:35 -0700217 } else if (cargo_joy_y < -0.5) {
218 target_hint->suggested_target = 3;
James Kuszmaul518640d2019-04-13 15:50:50 -0700219 } else if (::std::abs(cargo_joy_x) > 0.5) {
220 target_hint->suggested_target = 2;
221 } else {
222 target_hint->suggested_target = 0;
223 }
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700224 }
225 if (!target_hint.Send()) {
226 LOG(ERROR, "Failed to send target selector hint.\n");
227 }
228 }
229
James Kuszmaul518640d2019-04-13 15:50:50 -0700230 if (data.PosEdge(kResetLocalizerLeft)) {
Austin Schuheb99d072019-05-12 21:03:38 -0700231 auto localizer_resetter = localizer_control_sender_.MakeMessage();
James Kuszmaul518640d2019-04-13 15:50:50 -0700232 // Start at the left feeder station.
233 localizer_resetter->x = 0.6;
234 localizer_resetter->y = 3.4;
235 localizer_resetter->keep_current_theta = true;
236
237 if (!localizer_resetter.Send()) {
238 LOG(ERROR, "Failed to reset localizer.\n");
239 }
240 }
241
242 if (data.PosEdge(kResetLocalizerRight)) {
Austin Schuheb99d072019-05-12 21:03:38 -0700243 auto localizer_resetter = localizer_control_sender_.MakeMessage();
James Kuszmaul518640d2019-04-13 15:50:50 -0700244 // Start at the left feeder station.
245 localizer_resetter->x = 0.6;
246 localizer_resetter->y = -3.4;
247 localizer_resetter->keep_current_theta = true;
248
249 if (!localizer_resetter.Send()) {
250 LOG(ERROR, "Failed to reset localizer.\n");
251 }
252 }
253
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700254 if (data.PosEdge(kResetLocalizerLeftForwards)) {
Austin Schuheb99d072019-05-12 21:03:38 -0700255 auto localizer_resetter = localizer_control_sender_.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700256 // Start at the left feeder station.
James Kuszmauld8deb682019-03-10 10:38:42 -0700257 localizer_resetter->x = 0.4;
258 localizer_resetter->y = 3.4;
259 localizer_resetter->theta = 0.0;
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700260
261 if (!localizer_resetter.Send()) {
262 LOG(ERROR, "Failed to reset localizer.\n");
263 }
264 }
265
266 if (data.PosEdge(kResetLocalizerLeftBackwards)) {
Austin Schuheb99d072019-05-12 21:03:38 -0700267 auto localizer_resetter = localizer_control_sender_.MakeMessage();
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700268 // Start at the left feeder station.
269 localizer_resetter->x = 0.4;
270 localizer_resetter->y = 3.4;
271 localizer_resetter->theta = M_PI;
272
273 if (!localizer_resetter.Send()) {
274 LOG(ERROR, "Failed to reset localizer.\n");
275 }
276 }
277
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700278 if (data.PosEdge(kResetLocalizerRightForwards)) {
Austin Schuheb99d072019-05-12 21:03:38 -0700279 auto localizer_resetter = localizer_control_sender_.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700280 // Start at the right feeder station.
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700281 localizer_resetter->x = 0.4;
282 localizer_resetter->y = -3.4;
283 localizer_resetter->theta = 0.0;
284
285 if (!localizer_resetter.Send()) {
286 LOG(ERROR, "Failed to reset localizer.\n");
287 }
288 }
289
290 if (data.PosEdge(kResetLocalizerRightBackwards)) {
Austin Schuheb99d072019-05-12 21:03:38 -0700291 auto localizer_resetter = localizer_control_sender_.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700292 // Start at the right feeder station.
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700293 localizer_resetter->x = 0.4;
294 localizer_resetter->y = -3.4;
295 localizer_resetter->theta = M_PI;
296
James Kuszmauld8deb682019-03-10 10:38:42 -0700297 if (!localizer_resetter.Send()) {
298 LOG(ERROR, "Failed to reset localizer.\n");
299 }
300 }
301
James Kuszmaul13738862019-04-14 10:48:00 -0700302 if (data.PosEdge(kRelease) &&
303 monotonic_now >
304 last_release_button_press_ + ::std::chrono::milliseconds(500)) {
Austin Schuh170f4952019-06-29 18:58:30 -0700305 if (superstructure_status_fetcher_->has_piece) {
James Kuszmaul13738862019-04-14 10:48:00 -0700306 release_mode_ = ReleaseButtonMode::kRelease;
307 } else {
308 release_mode_ = ReleaseButtonMode::kBallIntake;
309 }
310 }
311
312 if (data.IsPressed(kRelease)) {
313 last_release_button_press_ = monotonic_now;
314 }
315
Austin Schuh170f4952019-06-29 18:58:30 -0700316 LOG(INFO, "has_piece: %d\n", superstructure_status_fetcher_->has_piece);
Austin Schuh1a17e132019-02-17 15:05:06 -0800317 if (data.IsPressed(kSuctionBall)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800318 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800319 } else if (data.IsPressed(kSuctionHatch)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800320 grab_piece_ = true;
James Kuszmaul13738862019-04-14 10:48:00 -0700321 } else if ((release_mode_ == ReleaseButtonMode::kRelease &&
322 data.IsPressed(kRelease)) ||
Tyler Chatow5eeee902019-04-12 20:47:48 -0700323 data.IsPressed(kReleaseButtonBoard) ||
Austin Schuh170f4952019-06-29 18:58:30 -0700324 !superstructure_status_fetcher_->has_piece) {
Sabina Davisc6329342019-03-01 20:44:42 -0800325 grab_piece_ = false;
Austin Schuh1a8fb572019-05-08 20:07:58 -0700326 LOG(INFO, "releasing due to other thing\n");
Austin Schuh1a17e132019-02-17 15:05:06 -0800327 }
Sabina Davis91b23602019-01-21 00:06:01 -0800328
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800329 if (data.IsPressed(kRocketBackwardUnpressed)) {
330 elevator_wrist_pos_ = kStowPos;
331 }
332 new_superstructure_goal->intake.unsafe_goal = -1.2;
333 new_superstructure_goal->roller_voltage = 0.0;
334
James Kuszmaul13738862019-04-14 10:48:00 -0700335 const bool kDoBallIntake =
336 (!climbed_ && release_mode_ == ReleaseButtonMode::kBallIntake &&
337 data.IsPressed(kRelease)) ||
338 data.GetAxis(kBallIntake) > 0.9;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800339 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
340
341 if (data.IsPressed(kPanelSwitch)) {
342 switch_ball_ = false;
343 } else if (data.IsPressed(kCargoSwitch)) {
344 switch_ball_ = true;
345 }
346
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800347 if (switch_ball_) {
Austin Schuh170f4952019-06-29 18:58:30 -0700348 if (superstructure_status_fetcher_->has_piece) {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800349 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800350 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800351
352 // Go to intake position and apply vacuum
353 if (data.IsPressed(kBallHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800354 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800355 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
356 } else if (data.IsPressed(kBallHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800357 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800358 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
359 }
360
361 // Go to elevator/wrist position. Overrides intake position if pressed so
362 // we can re-grab the ball.
363 if (data.IsPressed(kRocketForwardLower)) {
364 elevator_wrist_pos_ = kBallForwardLowerPos;
365 } else if (data.IsPressed(kRocketBackwardLower)) {
366 elevator_wrist_pos_ = kBallBackwardLowerPos;
367 } else if (data.IsPressed(kRocketForwardMiddle)) {
368 elevator_wrist_pos_ = kBallForwardMiddlePos;
369 } else if (data.IsPressed(kRocketBackwardMiddle)) {
370 elevator_wrist_pos_ = kBallBackwardMiddlePos;
371 } else if (data.IsPressed(kRocketForwardUpper)) {
372 elevator_wrist_pos_ = kBallForwardUpperPos;
373 } else if (data.IsPressed(kRocketBackwardUpper)) {
374 elevator_wrist_pos_ = kBallBackwardUpperPos;
375 } else if (data.IsPressed(kCargoForward)) {
376 elevator_wrist_pos_ = kBallCargoForwardPos;
377 } else if (data.IsPressed(kCargoBackward)) {
378 elevator_wrist_pos_ = kBallCargoBackwardPos;
379 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800380 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800381 if (data.IsPressed(kPanelHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800382 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800383 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
384 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800385 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800386 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
387 }
388
389 // Go to elevator/wrist position. Overrides intake position if pressed so
390 // we can re-grab the panel.
391 if (data.IsPressed(kRocketForwardLower)) {
392 elevator_wrist_pos_ = kPanelForwardLowerPos;
393 } else if (data.IsPressed(kRocketBackwardLower)) {
394 elevator_wrist_pos_ = kPanelBackwardLowerPos;
395 } else if (data.IsPressed(kRocketForwardMiddle)) {
396 elevator_wrist_pos_ = kPanelForwardMiddlePos;
397 } else if (data.IsPressed(kRocketBackwardMiddle)) {
398 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
399 } else if (data.IsPressed(kRocketForwardUpper)) {
400 elevator_wrist_pos_ = kPanelForwardUpperPos;
401 } else if (data.IsPressed(kRocketBackwardUpper)) {
402 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800403 } else if (data.IsPressed(kCargoForward)) {
404 elevator_wrist_pos_ = kPanelCargoForwardPos;
405 } else if (data.IsPressed(kCargoBackward)) {
406 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800407 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800408 }
409
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800410 if (switch_ball_) {
411 if (kDoBallOutake ||
Austin Schuhaab7e162019-03-13 22:44:58 -0700412 (kDoBallIntake &&
Austin Schuh20dc0502019-03-30 07:24:07 -0700413 monotonic_now < last_not_has_piece_ + chrono::milliseconds(200))) {
Austin Schuhd8098b42019-05-08 20:48:17 -0700414 new_superstructure_goal->intake.unsafe_goal = 0.83;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800415 }
Austin Schuh23a51632019-02-19 16:50:36 -0800416
Austin Schuh170f4952019-06-29 18:58:30 -0700417 if (kDoBallIntake && !superstructure_status_fetcher_->has_piece) {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800418 elevator_wrist_pos_ = kBallIntakePos;
419 new_superstructure_goal->roller_voltage = 9.0;
Sabina Davisc6329342019-03-01 20:44:42 -0800420 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800421 } else {
422 if (kDoBallOutake) {
423 new_superstructure_goal->roller_voltage = -6.0;
424 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800425 new_superstructure_goal->roller_voltage = 0.0;
426 }
427 }
Austin Schuh23a51632019-02-19 16:50:36 -0800428 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800429
Austin Schuh457bde32019-03-17 18:16:41 -0700430 constexpr double kDeployStiltPosition = 0.5;
431
Austin Schuh457bde32019-03-17 18:16:41 -0700432 if (data.IsPressed(kFallOver)) {
433 new_superstructure_goal->stilts.unsafe_goal = 0.71;
434 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
435 new_superstructure_goal->stilts.profile_params.max_acceleration = 1.25;
Austin Schuhe2f22482019-04-13 23:05:43 -0700436 } else if (data.IsPressed(kHalfStilt)) {
437 was_above_ = false;
438 new_superstructure_goal->stilts.unsafe_goal = 0.345;
439 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
440 } else if (data.IsPressed(kDeployStilt) || was_above_) {
Austin Schuh457bde32019-03-17 18:16:41 -0700441 new_superstructure_goal->stilts.unsafe_goal = kDeployStiltPosition;
442 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
443 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Austin Schuh457bde32019-03-17 18:16:41 -0700444 } else {
445 new_superstructure_goal->stilts.unsafe_goal = 0.005;
446 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
447 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
448 }
449
Austin Schuh170f4952019-06-29 18:58:30 -0700450 if (superstructure_status_fetcher_->stilts.position > 0.1) {
Austin Schuh02be8b92019-03-24 16:04:14 -0700451 elevator_wrist_pos_ = kClimbPos;
James Kuszmaul13738862019-04-14 10:48:00 -0700452 climbed_ = true;
Austin Schuhd8a18bb2019-03-24 19:21:38 -0700453 new_superstructure_goal->wrist.profile_params.max_acceleration = 10;
454 new_superstructure_goal->elevator.profile_params.max_acceleration = 6;
Austin Schuh02be8b92019-03-24 16:04:14 -0700455 }
456
Austin Schuhe2f22482019-04-13 23:05:43 -0700457 // If we've been asked to go above deploy and made it up that high, latch
458 // was_above.
459 if (new_superstructure_goal->stilts.unsafe_goal > kDeployStiltPosition &&
Austin Schuh170f4952019-06-29 18:58:30 -0700460 superstructure_status_fetcher_->stilts.position >=
461 kDeployStiltPosition) {
Austin Schuhe2f22482019-04-13 23:05:43 -0700462 was_above_ = true;
Austin Schuh170f4952019-06-29 18:58:30 -0700463 } else if ((superstructure_position_fetcher_->platform_left_detect &&
464 superstructure_position_fetcher_->platform_right_detect) &&
Austin Schuhe2f22482019-04-13 23:05:43 -0700465 !data.IsPressed(kDeployStilt) && !data.IsPressed(kFallOver)) {
Austin Schuhe2f22482019-04-13 23:05:43 -0700466 was_above_ = false;
467 }
468
Austin Schuh170f4952019-06-29 18:58:30 -0700469 if (superstructure_status_fetcher_->stilts.position >
470 kDeployStiltPosition &&
Austin Schuh457bde32019-03-17 18:16:41 -0700471 new_superstructure_goal->stilts.unsafe_goal == kDeployStiltPosition) {
472 new_superstructure_goal->stilts.profile_params.max_velocity = 0.30;
473 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
474 }
475
James Kuszmaul13738862019-04-14 10:48:00 -0700476 if ((release_mode_ == ReleaseButtonMode::kRelease &&
477 data.IsPressed(kRelease)) ||
478 data.IsPressed(kReleaseButtonBoard)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800479 grab_piece_ = false;
Austin Schuh1a8fb572019-05-08 20:07:58 -0700480 LOG(INFO, "Releasing due to button\n");
Austin Schuh1a17e132019-02-17 15:05:06 -0800481 }
482
Sabina Davisc6329342019-03-01 20:44:42 -0800483 if (switch_ball_) {
484 new_superstructure_goal->suction.gamepiece_mode = 0;
485 } else {
486 new_superstructure_goal->suction.gamepiece_mode = 1;
487 }
488
Tyler Chatowe0241452019-03-08 21:07:50 -0800489 vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0);
490
Sabina Davisc6329342019-03-01 20:44:42 -0800491 new_superstructure_goal->suction.grab_piece = grab_piece_;
Sabina Davis91b23602019-01-21 00:06:01 -0800492
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800493 new_superstructure_goal->elevator.unsafe_goal =
494 elevator_wrist_pos_.elevator;
495 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800496
497 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
498 if (!new_superstructure_goal.Send()) {
499 LOG(ERROR, "Sending superstructure goal failed.\n");
500 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800501
Austin Schuhaab7e162019-03-13 22:44:58 -0700502 if (monotonic_now >
503 last_vision_control_ + ::std::chrono::milliseconds(50)) {
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700504 video_tx_->Send(vision_control_);
Austin Schuhaab7e162019-03-13 22:44:58 -0700505 last_vision_control_ = monotonic_now;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700506 }
Austin Schuh4e2629d2019-03-28 14:44:37 -0700507
508 {
Austin Schuh5671a8c2019-05-19 17:01:04 -0700509 auto camera_log_message = camera_log_sender_.MakeMessage();
Austin Schuh4e2629d2019-03-28 14:44:37 -0700510 camera_log_message->log = data.IsPressed(kCameraLog);
511 LOG_STRUCT(DEBUG, "camera_log", *camera_log_message);
512 camera_log_message.Send();
513 }
Sabina Davis91b23602019-01-21 00:06:01 -0800514 }
515
516 private:
Austin Schuhc087b102019-05-12 15:33:12 -0700517 ::aos::Sender<::y2019::control_loops::drivetrain::TargetSelectorHint>
518 target_selector_hint_sender_;
519
Austin Schuheb99d072019-05-12 21:03:38 -0700520 ::aos::Sender<::frc971::control_loops::drivetrain::LocalizerControl>
521 localizer_control_sender_;
522
Austin Schuh5671a8c2019-05-19 17:01:04 -0700523 ::aos::Sender<::y2019::CameraLog> camera_log_sender_;
524
Austin Schuh170f4952019-06-29 18:58:30 -0700525 ::aos::Fetcher<
526 ::y2019::control_loops::superstructure::SuperstructureQueue::Goal>
527 superstructure_goal_fetcher_;
528
529 ::aos::Sender<
530 ::y2019::control_loops::superstructure::SuperstructureQueue::Goal>
531 superstructure_goal_sender_;
532
533 ::aos::Fetcher<
534 ::y2019::control_loops::superstructure::SuperstructureQueue::Position>
535 superstructure_position_fetcher_;
536 ::aos::Fetcher<
537 ::y2019::control_loops::superstructure::SuperstructureQueue::Status>
538 superstructure_status_fetcher_;
539
540
Austin Schuhe2f22482019-04-13 23:05:43 -0700541 // Bool to track if we've been above the deploy position. Once this bool is
542 // set, don't let the stilts retract until we see the platform.
543 bool was_above_ = false;
544
Sabina Davis91b23602019-01-21 00:06:01 -0800545 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800546 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Sabina Davisc6329342019-03-01 20:44:42 -0800547 bool grab_piece_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800548
549 bool switch_ball_ = false;
Tyler Chatowe0241452019-03-08 21:07:50 -0800550
James Kuszmaul13738862019-04-14 10:48:00 -0700551 bool climbed_ = false;
552
553 enum class ReleaseButtonMode {
554 kBallIntake,
555 kRelease,
556 };
557
558 ReleaseButtonMode release_mode_ = ReleaseButtonMode::kRelease;
559 aos::monotonic_clock::time_point last_release_button_press_ =
560 aos::monotonic_clock::min_time;
561
Tyler Chatowe0241452019-03-08 21:07:50 -0800562 VisionControl vision_control_;
563 ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700564 ::aos::monotonic_clock::time_point last_vision_control_ =
565 ::aos::monotonic_clock::time_point::min();
Austin Schuhaab7e162019-03-13 22:44:58 -0700566
567 // Time at which we last did not have a game piece.
568 ::aos::monotonic_clock::time_point last_not_has_piece_ =
569 ::aos::monotonic_clock::time_point::min();
Sabina Davis91b23602019-01-21 00:06:01 -0800570};
571
572} // namespace joysticks
573} // namespace input
574} // namespace y2019
575
576int main() {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700577 ::aos::InitNRT(true);
578
Sabina Davis91b23602019-01-21 00:06:01 -0800579 ::aos::ShmEventLoop event_loop;
580 ::y2019::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700581
582 event_loop.Run();
583
Sabina Davis91b23602019-01-21 00:06:01 -0800584 ::aos::Cleanup();
585}