blob: 69b40f5de3c063e386ebea7cd058f784c59e7fba [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
Austin Schuh1a17e132019-02-17 15:05:06 -080047const ButtonLocation kSuctionBall(3, 13);
48const ButtonLocation kSuctionHatch(3, 12);
49const ButtonLocation kDeployStilt(3, 8);
Sabina Davis069cbca2019-03-08 23:35:18 -080050const ButtonLocation kHalfStilt(3, 6);
Austin Schuh1a17e132019-02-17 15:05:06 -080051const ButtonLocation kFallOver(3, 9);
Austin Schuh2cf16b82019-02-15 23:23:22 -080052
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080053struct ElevatorWristPosition {
54 double elevator;
55 double wrist;
56};
Sabina Davis91b23602019-01-21 00:06:01 -080057
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);
Austin Schuhbb1df6f2019-03-17 18:15:43 -070081const ButtonLocation kResetLocalizerLeftForwards(3, 14);
82const ButtonLocation kResetLocalizerLeftBackwards(4, 12);
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080083
Austin Schuhbb1df6f2019-03-17 18:15:43 -070084const ButtonLocation kResetLocalizerRightForwards(4, 1);
85const ButtonLocation kResetLocalizerRightBackwards(4, 11);
Tyler Chatow07c906b2019-03-09 21:29:06 -080086
James Kuszmaul7d1ef442019-03-23 20:20:50 -070087const ButtonLocation kNearCargoHint(3, 15);
88const ButtonLocation kMidCargoHint(3, 16);
89const ButtonLocation kFarCargoHint(4, 2);
90
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080091const ElevatorWristPosition kStowPos{0.36, 0.0};
92
Austin Schuh139f59d2019-03-17 18:16:13 -070093const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.01, M_PI / 2.0};
94const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.015, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080095
96const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
97const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
98
Sabina Davise48004f2019-03-02 23:15:24 -080099const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0};
100const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800101
Sabina Davise48004f2019-03-02 23:15:24 -0800102const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0};
103const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800104
Sabina Davise6fe6c52019-03-03 15:48:51 -0800105const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0};
106const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0};
107
Austin Schuh139f59d2019-03-17 18:16:13 -0700108const ElevatorWristPosition kBallForwardLowerPos{0.42, M_PI / 2.0};
109const ElevatorWristPosition kBallBackwardLowerPos{0.14, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800110
Sabina Davise48004f2019-03-02 23:15:24 -0800111const ElevatorWristPosition kBallForwardMiddlePos{1.16, 1.546};
Austin Schuh139f59d2019-03-17 18:16:13 -0700112const ElevatorWristPosition kBallBackwardMiddlePos{0.90, -1.546};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800113
Austin Schuh139f59d2019-03-17 18:16:13 -0700114const ElevatorWristPosition kBallForwardUpperPos{1.51, 0.961};
115const ElevatorWristPosition kBallBackwardUpperPos{1.44, -1.217};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800116
Austin Schuh139f59d2019-03-17 18:16:13 -0700117const ElevatorWristPosition kBallCargoForwardPos{0.739044, 1.353};
118const ElevatorWristPosition kBallCargoBackwardPos{0.868265, -1.999};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800119
Sabina Davise6fe6c52019-03-03 15:48:51 -0800120const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097};
121const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800122
123const ElevatorWristPosition kBallIntakePos{0.29, 2.14};
Austin Schuh2cf16b82019-02-15 23:23:22 -0800124
Sabina Davis91b23602019-01-21 00:06:01 -0800125class Reader : public ::aos::input::ActionJoystickInput {
126 public:
127 Reader(::aos::EventLoop *event_loop)
128 : ::aos::input::ActionJoystickInput(
129 event_loop,
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800130 ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {
Austin Schuh30cc40a2019-03-12 21:02:13 -0700131 // Set teleop to immediately resume after auto ends for sandstorm mode.
132 set_run_teleop_in_auto(true);
133
Tyler Chatowe0241452019-03-08 21:07:50 -0800134 const uint16_t team = ::aos::network::GetTeamNumber();
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800135 superstructure_queue.goal.FetchLatest();
136 if (superstructure_queue.goal.get()) {
Sabina Davisc6329342019-03-01 20:44:42 -0800137 grab_piece_ = superstructure_queue.goal->suction.grab_piece;
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700138 switch_ball_ = superstructure_queue.goal->suction.gamepiece_mode == 0;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800139 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800140 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
141 StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000));
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800142 }
Sabina Davis91b23602019-01-21 00:06:01 -0800143
Austin Schuha78857f2019-03-13 22:43:41 -0700144 void AutoEnded() override {
145 LOG(INFO, "Auto ended, assuming disc and have piece\n");
146 grab_piece_ = true;
147 switch_ball_ = false;
148 }
149
150 void HandleTeleop(const ::aos::input::driver_station::Data &data) override {
Austin Schuhaab7e162019-03-13 22:44:58 -0700151 ::aos::monotonic_clock::time_point monotonic_now =
152 ::aos::monotonic_clock::now();
Sabina Davis91b23602019-01-21 00:06:01 -0800153 superstructure_queue.position.FetchLatest();
154 superstructure_queue.status.FetchLatest();
155 if (!superstructure_queue.status.get() ||
156 !superstructure_queue.position.get()) {
157 LOG(ERROR, "Got no superstructure status packet.\n");
158 return;
159 }
160
Austin Schuhaab7e162019-03-13 22:44:58 -0700161 if (!superstructure_queue.status->has_piece) {
162 last_not_has_piece_ = monotonic_now;
163 }
164
Sabina Davis91b23602019-01-21 00:06:01 -0800165 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
166
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700167 {
168 auto target_hint = target_selector_hint.MakeMessage();
169 if (data.IsPressed(kNearCargoHint)) {
170 target_hint->suggested_target = 1;
171 } else if (data.IsPressed(kMidCargoHint)) {
172 target_hint->suggested_target = 2;
173 } else if (data.IsPressed(kFarCargoHint)) {
174 target_hint->suggested_target = 3;
175 } else {
176 target_hint->suggested_target = 0;
177 }
178 if (!target_hint.Send()) {
179 LOG(ERROR, "Failed to send target selector hint.\n");
180 }
181 }
182
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700183 if (data.PosEdge(kResetLocalizerLeftForwards)) {
James Kuszmauld8deb682019-03-10 10:38:42 -0700184 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700185 // Start at the left feeder station.
James Kuszmauld8deb682019-03-10 10:38:42 -0700186 localizer_resetter->x = 0.4;
187 localizer_resetter->y = 3.4;
188 localizer_resetter->theta = 0.0;
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700189
190 if (!localizer_resetter.Send()) {
191 LOG(ERROR, "Failed to reset localizer.\n");
192 }
193 }
194
195 if (data.PosEdge(kResetLocalizerLeftBackwards)) {
196 auto localizer_resetter = localizer_control.MakeMessage();
197 // Start at the left feeder station.
198 localizer_resetter->x = 0.4;
199 localizer_resetter->y = 3.4;
200 localizer_resetter->theta = M_PI;
201
202 if (!localizer_resetter.Send()) {
203 LOG(ERROR, "Failed to reset localizer.\n");
204 }
205 }
206
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700207 if (data.PosEdge(kResetLocalizerRightForwards)) {
208 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700209 // Start at the right feeder station.
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700210 localizer_resetter->x = 0.4;
211 localizer_resetter->y = -3.4;
212 localizer_resetter->theta = 0.0;
213
214 if (!localizer_resetter.Send()) {
215 LOG(ERROR, "Failed to reset localizer.\n");
216 }
217 }
218
219 if (data.PosEdge(kResetLocalizerRightBackwards)) {
220 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700221 // Start at the right feeder station.
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700222 localizer_resetter->x = 0.4;
223 localizer_resetter->y = -3.4;
224 localizer_resetter->theta = M_PI;
225
James Kuszmauld8deb682019-03-10 10:38:42 -0700226 if (!localizer_resetter.Send()) {
227 LOG(ERROR, "Failed to reset localizer.\n");
228 }
229 }
230
Austin Schuh1a17e132019-02-17 15:05:06 -0800231 if (data.IsPressed(kSuctionBall)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800232 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800233 } else if (data.IsPressed(kSuctionHatch)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800234 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800235 } else if (data.IsPressed(kRelease) ||
236 !superstructure_queue.status->has_piece) {
Sabina Davisc6329342019-03-01 20:44:42 -0800237 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800238 }
Sabina Davis91b23602019-01-21 00:06:01 -0800239
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800240 if (data.IsPressed(kRocketBackwardUnpressed)) {
241 elevator_wrist_pos_ = kStowPos;
242 }
243 new_superstructure_goal->intake.unsafe_goal = -1.2;
244 new_superstructure_goal->roller_voltage = 0.0;
245
246 const bool kDoBallIntake = data.GetAxis(kBallIntake) > 0.9;
247 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
248
249 if (data.IsPressed(kPanelSwitch)) {
250 switch_ball_ = false;
251 } else if (data.IsPressed(kCargoSwitch)) {
252 switch_ball_ = true;
253 }
254
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800255 if (switch_ball_) {
256 if (superstructure_queue.status->has_piece) {
257 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800258 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800259
260 // Go to intake position and apply vacuum
261 if (data.IsPressed(kBallHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800262 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800263 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
264 } else if (data.IsPressed(kBallHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800265 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800266 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
267 }
268
269 // Go to elevator/wrist position. Overrides intake position if pressed so
270 // we can re-grab the ball.
271 if (data.IsPressed(kRocketForwardLower)) {
272 elevator_wrist_pos_ = kBallForwardLowerPos;
273 } else if (data.IsPressed(kRocketBackwardLower)) {
274 elevator_wrist_pos_ = kBallBackwardLowerPos;
275 } else if (data.IsPressed(kRocketForwardMiddle)) {
276 elevator_wrist_pos_ = kBallForwardMiddlePos;
277 } else if (data.IsPressed(kRocketBackwardMiddle)) {
278 elevator_wrist_pos_ = kBallBackwardMiddlePos;
279 } else if (data.IsPressed(kRocketForwardUpper)) {
280 elevator_wrist_pos_ = kBallForwardUpperPos;
281 } else if (data.IsPressed(kRocketBackwardUpper)) {
282 elevator_wrist_pos_ = kBallBackwardUpperPos;
283 } else if (data.IsPressed(kCargoForward)) {
284 elevator_wrist_pos_ = kBallCargoForwardPos;
285 } else if (data.IsPressed(kCargoBackward)) {
286 elevator_wrist_pos_ = kBallCargoBackwardPos;
287 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800288 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800289 if (data.IsPressed(kPanelHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800290 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800291 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
292 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800293 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800294 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
295 }
296
297 // Go to elevator/wrist position. Overrides intake position if pressed so
298 // we can re-grab the panel.
299 if (data.IsPressed(kRocketForwardLower)) {
300 elevator_wrist_pos_ = kPanelForwardLowerPos;
301 } else if (data.IsPressed(kRocketBackwardLower)) {
302 elevator_wrist_pos_ = kPanelBackwardLowerPos;
303 } else if (data.IsPressed(kRocketForwardMiddle)) {
304 elevator_wrist_pos_ = kPanelForwardMiddlePos;
305 } else if (data.IsPressed(kRocketBackwardMiddle)) {
306 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
307 } else if (data.IsPressed(kRocketForwardUpper)) {
308 elevator_wrist_pos_ = kPanelForwardUpperPos;
309 } else if (data.IsPressed(kRocketBackwardUpper)) {
310 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800311 } else if (data.IsPressed(kCargoForward)) {
312 elevator_wrist_pos_ = kPanelCargoForwardPos;
313 } else if (data.IsPressed(kCargoBackward)) {
314 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800315 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800316 }
317
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800318 if (switch_ball_) {
319 if (kDoBallOutake ||
Austin Schuhaab7e162019-03-13 22:44:58 -0700320 (kDoBallIntake &&
321 monotonic_now < last_not_has_piece_ + chrono::milliseconds(100))) {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800322 new_superstructure_goal->intake.unsafe_goal = 0.959327;
323 }
Austin Schuh23a51632019-02-19 16:50:36 -0800324
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800325 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
326 elevator_wrist_pos_ = kBallIntakePos;
327 new_superstructure_goal->roller_voltage = 9.0;
Sabina Davisc6329342019-03-01 20:44:42 -0800328 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800329 } else {
330 if (kDoBallOutake) {
331 new_superstructure_goal->roller_voltage = -6.0;
332 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800333 new_superstructure_goal->roller_voltage = 0.0;
334 }
335 }
Austin Schuh23a51632019-02-19 16:50:36 -0800336 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800337
Austin Schuh457bde32019-03-17 18:16:41 -0700338 constexpr double kDeployStiltPosition = 0.5;
339
340 // TODO(sabina): max height please?
341 if (data.IsPressed(kFallOver)) {
342 new_superstructure_goal->stilts.unsafe_goal = 0.71;
343 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
344 new_superstructure_goal->stilts.profile_params.max_acceleration = 1.25;
345 } else if (data.IsPressed(kDeployStilt)) {
346 new_superstructure_goal->stilts.unsafe_goal = kDeployStiltPosition;
347 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
348 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
349 } else if (data.IsPressed(kHalfStilt)) {
350 new_superstructure_goal->stilts.unsafe_goal = 0.345;
351 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
352 } else {
353 new_superstructure_goal->stilts.unsafe_goal = 0.005;
354 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
355 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
356 }
357
358 if (superstructure_queue.status->stilts.position > kDeployStiltPosition &&
359 new_superstructure_goal->stilts.unsafe_goal == kDeployStiltPosition) {
360 new_superstructure_goal->stilts.profile_params.max_velocity = 0.30;
361 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
362 }
363
364
Austin Schuh1a17e132019-02-17 15:05:06 -0800365 if (data.IsPressed(kRelease)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800366 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800367 }
368
Sabina Davisc6329342019-03-01 20:44:42 -0800369 if (switch_ball_) {
370 new_superstructure_goal->suction.gamepiece_mode = 0;
371 } else {
372 new_superstructure_goal->suction.gamepiece_mode = 1;
373 }
374
Tyler Chatowe0241452019-03-08 21:07:50 -0800375 vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0);
376
Sabina Davisc6329342019-03-01 20:44:42 -0800377 new_superstructure_goal->suction.grab_piece = grab_piece_;
Sabina Davis91b23602019-01-21 00:06:01 -0800378
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800379 new_superstructure_goal->elevator.unsafe_goal =
380 elevator_wrist_pos_.elevator;
381 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800382
383 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
384 if (!new_superstructure_goal.Send()) {
385 LOG(ERROR, "Sending superstructure goal failed.\n");
386 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800387
Austin Schuhaab7e162019-03-13 22:44:58 -0700388 if (monotonic_now >
389 last_vision_control_ + ::std::chrono::milliseconds(50)) {
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700390 video_tx_->Send(vision_control_);
Austin Schuhaab7e162019-03-13 22:44:58 -0700391 last_vision_control_ = monotonic_now;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700392 }
Sabina Davis91b23602019-01-21 00:06:01 -0800393 }
394
395 private:
396 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800397 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Sabina Davisc6329342019-03-01 20:44:42 -0800398 bool grab_piece_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800399
400 bool switch_ball_ = false;
Tyler Chatowe0241452019-03-08 21:07:50 -0800401
402 VisionControl vision_control_;
403 ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700404 ::aos::monotonic_clock::time_point last_vision_control_ =
405 ::aos::monotonic_clock::time_point::min();
Austin Schuhaab7e162019-03-13 22:44:58 -0700406
407 // Time at which we last did not have a game piece.
408 ::aos::monotonic_clock::time_point last_not_has_piece_ =
409 ::aos::monotonic_clock::time_point::min();
Sabina Davis91b23602019-01-21 00:06:01 -0800410};
411
412} // namespace joysticks
413} // namespace input
414} // namespace y2019
415
416int main() {
417 ::aos::Init(-1);
418 ::aos::ShmEventLoop event_loop;
419 ::y2019::input::joysticks::Reader reader(&event_loop);
420 reader.Run();
421 ::aos::Cleanup();
422}