blob: a82079bd28a9d6a701762e65c270f10b0cd58f4b [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);
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
Tyler Chatow5eeee902019-04-12 20:47:48 -070088const ButtonLocation kNearCargoHint(3, 3);
89const ButtonLocation kMidCargoHint(3, 5);
90const ButtonLocation kFarCargoHint(3, 6);
James Kuszmaul7d1ef442019-03-23 20:20:50 -070091
Tyler Chatow5eeee902019-04-12 20:47:48 -070092const ButtonLocation kCameraLog(3, 14);
Austin Schuh4e2629d2019-03-28 14:44:37 -070093
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080094const ElevatorWristPosition kStowPos{0.36, 0.0};
Austin Schuh02be8b92019-03-24 16:04:14 -070095const ElevatorWristPosition kClimbPos{0.0, M_PI / 4.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080096
Austin Schuh139f59d2019-03-17 18:16:13 -070097const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.01, M_PI / 2.0};
98const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.015, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080099
100const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
101const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
102
Sabina Davise48004f2019-03-02 23:15:24 -0800103const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0};
104const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800105
Sabina Davise48004f2019-03-02 23:15:24 -0800106const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0};
107const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800108
Sabina Davise6fe6c52019-03-03 15:48:51 -0800109const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0};
110const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0};
111
Austin Schuh6c5deb12019-03-24 16:48:32 -0700112const ElevatorWristPosition kBallForwardLowerPos{0.21, 1.27};
Austin Schuh20dc0502019-03-30 07:24:07 -0700113const ElevatorWristPosition kBallBackwardLowerPos{0.43, -1.99};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800114
Austin Schuh20dc0502019-03-30 07:24:07 -0700115const ElevatorWristPosition kBallForwardMiddlePos{0.925, 1.21};
116const ElevatorWristPosition kBallBackwardMiddlePos{1.19, -1.98};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800117
Austin Schuh139f59d2019-03-17 18:16:13 -0700118const ElevatorWristPosition kBallForwardUpperPos{1.51, 0.961};
119const ElevatorWristPosition kBallBackwardUpperPos{1.44, -1.217};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800120
Austin Schuh20dc0502019-03-30 07:24:07 -0700121const ElevatorWristPosition kBallCargoForwardPos{0.59, 1.2};
122const ElevatorWristPosition kBallCargoBackwardPos{0.868265, -2.1};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800123
Sabina Davise6fe6c52019-03-03 15:48:51 -0800124const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097};
125const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800126
Austin Schuh20dc0502019-03-30 07:24:07 -0700127const ElevatorWristPosition kBallIntakePos{0.309, 2.13};
Austin Schuh2cf16b82019-02-15 23:23:22 -0800128
Sabina Davis91b23602019-01-21 00:06:01 -0800129class Reader : public ::aos::input::ActionJoystickInput {
130 public:
131 Reader(::aos::EventLoop *event_loop)
132 : ::aos::input::ActionJoystickInput(
133 event_loop,
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800134 ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {
Austin Schuh30cc40a2019-03-12 21:02:13 -0700135 // Set teleop to immediately resume after auto ends for sandstorm mode.
136 set_run_teleop_in_auto(true);
137
Tyler Chatowe0241452019-03-08 21:07:50 -0800138 const uint16_t team = ::aos::network::GetTeamNumber();
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800139 superstructure_queue.goal.FetchLatest();
140 if (superstructure_queue.goal.get()) {
Sabina Davisc6329342019-03-01 20:44:42 -0800141 grab_piece_ = superstructure_queue.goal->suction.grab_piece;
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700142 switch_ball_ = superstructure_queue.goal->suction.gamepiece_mode == 0;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800143 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800144 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
145 StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000));
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800146 }
Sabina Davis91b23602019-01-21 00:06:01 -0800147
Austin Schuha78857f2019-03-13 22:43:41 -0700148 void AutoEnded() override {
149 LOG(INFO, "Auto ended, assuming disc and have piece\n");
150 grab_piece_ = true;
151 switch_ball_ = false;
152 }
153
154 void HandleTeleop(const ::aos::input::driver_station::Data &data) override {
Austin Schuhaab7e162019-03-13 22:44:58 -0700155 ::aos::monotonic_clock::time_point monotonic_now =
156 ::aos::monotonic_clock::now();
Sabina Davis91b23602019-01-21 00:06:01 -0800157 superstructure_queue.position.FetchLatest();
158 superstructure_queue.status.FetchLatest();
159 if (!superstructure_queue.status.get() ||
160 !superstructure_queue.position.get()) {
161 LOG(ERROR, "Got no superstructure status packet.\n");
162 return;
163 }
164
Austin Schuhaab7e162019-03-13 22:44:58 -0700165 if (!superstructure_queue.status->has_piece) {
166 last_not_has_piece_ = monotonic_now;
167 }
168
Sabina Davis91b23602019-01-21 00:06:01 -0800169 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
170
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700171 {
172 auto target_hint = target_selector_hint.MakeMessage();
173 if (data.IsPressed(kNearCargoHint)) {
174 target_hint->suggested_target = 1;
175 } else if (data.IsPressed(kMidCargoHint)) {
176 target_hint->suggested_target = 2;
177 } else if (data.IsPressed(kFarCargoHint)) {
178 target_hint->suggested_target = 3;
179 } else {
180 target_hint->suggested_target = 0;
181 }
182 if (!target_hint.Send()) {
183 LOG(ERROR, "Failed to send target selector hint.\n");
184 }
185 }
186
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700187 if (data.PosEdge(kResetLocalizerLeftForwards)) {
James Kuszmauld8deb682019-03-10 10:38:42 -0700188 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700189 // Start at the left feeder station.
James Kuszmauld8deb682019-03-10 10:38:42 -0700190 localizer_resetter->x = 0.4;
191 localizer_resetter->y = 3.4;
192 localizer_resetter->theta = 0.0;
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700193
194 if (!localizer_resetter.Send()) {
195 LOG(ERROR, "Failed to reset localizer.\n");
196 }
197 }
198
199 if (data.PosEdge(kResetLocalizerLeftBackwards)) {
200 auto localizer_resetter = localizer_control.MakeMessage();
201 // Start at the left feeder station.
202 localizer_resetter->x = 0.4;
203 localizer_resetter->y = 3.4;
204 localizer_resetter->theta = M_PI;
205
206 if (!localizer_resetter.Send()) {
207 LOG(ERROR, "Failed to reset localizer.\n");
208 }
209 }
210
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700211 if (data.PosEdge(kResetLocalizerRightForwards)) {
212 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700213 // Start at the right feeder station.
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700214 localizer_resetter->x = 0.4;
215 localizer_resetter->y = -3.4;
216 localizer_resetter->theta = 0.0;
217
218 if (!localizer_resetter.Send()) {
219 LOG(ERROR, "Failed to reset localizer.\n");
220 }
221 }
222
223 if (data.PosEdge(kResetLocalizerRightBackwards)) {
224 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700225 // Start at the right feeder station.
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700226 localizer_resetter->x = 0.4;
227 localizer_resetter->y = -3.4;
228 localizer_resetter->theta = M_PI;
229
James Kuszmauld8deb682019-03-10 10:38:42 -0700230 if (!localizer_resetter.Send()) {
231 LOG(ERROR, "Failed to reset localizer.\n");
232 }
233 }
234
Austin Schuh1a17e132019-02-17 15:05:06 -0800235 if (data.IsPressed(kSuctionBall)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800236 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800237 } else if (data.IsPressed(kSuctionHatch)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800238 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800239 } else if (data.IsPressed(kRelease) ||
Tyler Chatow5eeee902019-04-12 20:47:48 -0700240 data.IsPressed(kReleaseButtonBoard) ||
Austin Schuh1a17e132019-02-17 15:05:06 -0800241 !superstructure_queue.status->has_piece) {
Sabina Davisc6329342019-03-01 20:44:42 -0800242 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800243 }
Sabina Davis91b23602019-01-21 00:06:01 -0800244
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800245 if (data.IsPressed(kRocketBackwardUnpressed)) {
246 elevator_wrist_pos_ = kStowPos;
247 }
248 new_superstructure_goal->intake.unsafe_goal = -1.2;
249 new_superstructure_goal->roller_voltage = 0.0;
250
251 const bool kDoBallIntake = data.GetAxis(kBallIntake) > 0.9;
252 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
253
254 if (data.IsPressed(kPanelSwitch)) {
255 switch_ball_ = false;
256 } else if (data.IsPressed(kCargoSwitch)) {
257 switch_ball_ = true;
258 }
259
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800260 if (switch_ball_) {
261 if (superstructure_queue.status->has_piece) {
262 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800263 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800264
265 // Go to intake position and apply vacuum
266 if (data.IsPressed(kBallHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800267 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800268 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
269 } else if (data.IsPressed(kBallHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800270 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800271 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
272 }
273
274 // Go to elevator/wrist position. Overrides intake position if pressed so
275 // we can re-grab the ball.
276 if (data.IsPressed(kRocketForwardLower)) {
277 elevator_wrist_pos_ = kBallForwardLowerPos;
278 } else if (data.IsPressed(kRocketBackwardLower)) {
279 elevator_wrist_pos_ = kBallBackwardLowerPos;
280 } else if (data.IsPressed(kRocketForwardMiddle)) {
281 elevator_wrist_pos_ = kBallForwardMiddlePos;
282 } else if (data.IsPressed(kRocketBackwardMiddle)) {
283 elevator_wrist_pos_ = kBallBackwardMiddlePos;
284 } else if (data.IsPressed(kRocketForwardUpper)) {
285 elevator_wrist_pos_ = kBallForwardUpperPos;
286 } else if (data.IsPressed(kRocketBackwardUpper)) {
287 elevator_wrist_pos_ = kBallBackwardUpperPos;
288 } else if (data.IsPressed(kCargoForward)) {
289 elevator_wrist_pos_ = kBallCargoForwardPos;
290 } else if (data.IsPressed(kCargoBackward)) {
291 elevator_wrist_pos_ = kBallCargoBackwardPos;
292 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800293 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800294 if (data.IsPressed(kPanelHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800295 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800296 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
297 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800298 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800299 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
300 }
301
302 // Go to elevator/wrist position. Overrides intake position if pressed so
303 // we can re-grab the panel.
304 if (data.IsPressed(kRocketForwardLower)) {
305 elevator_wrist_pos_ = kPanelForwardLowerPos;
306 } else if (data.IsPressed(kRocketBackwardLower)) {
307 elevator_wrist_pos_ = kPanelBackwardLowerPos;
308 } else if (data.IsPressed(kRocketForwardMiddle)) {
309 elevator_wrist_pos_ = kPanelForwardMiddlePos;
310 } else if (data.IsPressed(kRocketBackwardMiddle)) {
311 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
312 } else if (data.IsPressed(kRocketForwardUpper)) {
313 elevator_wrist_pos_ = kPanelForwardUpperPos;
314 } else if (data.IsPressed(kRocketBackwardUpper)) {
315 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800316 } else if (data.IsPressed(kCargoForward)) {
317 elevator_wrist_pos_ = kPanelCargoForwardPos;
318 } else if (data.IsPressed(kCargoBackward)) {
319 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800320 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800321 }
322
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800323 if (switch_ball_) {
324 if (kDoBallOutake ||
Austin Schuhaab7e162019-03-13 22:44:58 -0700325 (kDoBallIntake &&
Austin Schuh20dc0502019-03-30 07:24:07 -0700326 monotonic_now < last_not_has_piece_ + chrono::milliseconds(200))) {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800327 new_superstructure_goal->intake.unsafe_goal = 0.959327;
328 }
Austin Schuh23a51632019-02-19 16:50:36 -0800329
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800330 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
331 elevator_wrist_pos_ = kBallIntakePos;
332 new_superstructure_goal->roller_voltage = 9.0;
Sabina Davisc6329342019-03-01 20:44:42 -0800333 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800334 } else {
335 if (kDoBallOutake) {
336 new_superstructure_goal->roller_voltage = -6.0;
337 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800338 new_superstructure_goal->roller_voltage = 0.0;
339 }
340 }
Austin Schuh23a51632019-02-19 16:50:36 -0800341 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800342
Austin Schuh457bde32019-03-17 18:16:41 -0700343 constexpr double kDeployStiltPosition = 0.5;
344
345 // TODO(sabina): max height please?
346 if (data.IsPressed(kFallOver)) {
347 new_superstructure_goal->stilts.unsafe_goal = 0.71;
348 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
349 new_superstructure_goal->stilts.profile_params.max_acceleration = 1.25;
350 } else if (data.IsPressed(kDeployStilt)) {
351 new_superstructure_goal->stilts.unsafe_goal = kDeployStiltPosition;
352 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
353 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
354 } else if (data.IsPressed(kHalfStilt)) {
355 new_superstructure_goal->stilts.unsafe_goal = 0.345;
356 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
357 } else {
358 new_superstructure_goal->stilts.unsafe_goal = 0.005;
359 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
360 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
361 }
362
James Kuszmaul0448f142019-03-24 16:22:34 -0700363 if (superstructure_queue.status->stilts.position > 0.1) {
Austin Schuh02be8b92019-03-24 16:04:14 -0700364 elevator_wrist_pos_ = kClimbPos;
Austin Schuhd8a18bb2019-03-24 19:21:38 -0700365 new_superstructure_goal->wrist.profile_params.max_acceleration = 10;
366 new_superstructure_goal->elevator.profile_params.max_acceleration = 6;
Austin Schuh02be8b92019-03-24 16:04:14 -0700367 }
368
Austin Schuh457bde32019-03-17 18:16:41 -0700369 if (superstructure_queue.status->stilts.position > kDeployStiltPosition &&
370 new_superstructure_goal->stilts.unsafe_goal == kDeployStiltPosition) {
371 new_superstructure_goal->stilts.profile_params.max_velocity = 0.30;
372 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
373 }
374
Tyler Chatow5eeee902019-04-12 20:47:48 -0700375 if (data.IsPressed(kRelease) || data.IsPressed(kReleaseButtonBoard)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800376 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800377 }
378
Sabina Davisc6329342019-03-01 20:44:42 -0800379 if (switch_ball_) {
380 new_superstructure_goal->suction.gamepiece_mode = 0;
381 } else {
382 new_superstructure_goal->suction.gamepiece_mode = 1;
383 }
384
Tyler Chatowe0241452019-03-08 21:07:50 -0800385 vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0);
386
Sabina Davisc6329342019-03-01 20:44:42 -0800387 new_superstructure_goal->suction.grab_piece = grab_piece_;
Sabina Davis91b23602019-01-21 00:06:01 -0800388
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800389 new_superstructure_goal->elevator.unsafe_goal =
390 elevator_wrist_pos_.elevator;
391 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800392
393 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
394 if (!new_superstructure_goal.Send()) {
395 LOG(ERROR, "Sending superstructure goal failed.\n");
396 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800397
Austin Schuhaab7e162019-03-13 22:44:58 -0700398 if (monotonic_now >
399 last_vision_control_ + ::std::chrono::milliseconds(50)) {
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700400 video_tx_->Send(vision_control_);
Austin Schuhaab7e162019-03-13 22:44:58 -0700401 last_vision_control_ = monotonic_now;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700402 }
Austin Schuh4e2629d2019-03-28 14:44:37 -0700403
404 {
405 auto camera_log_message = camera_log.MakeMessage();
406 camera_log_message->log = data.IsPressed(kCameraLog);
407 LOG_STRUCT(DEBUG, "camera_log", *camera_log_message);
408 camera_log_message.Send();
409 }
Sabina Davis91b23602019-01-21 00:06:01 -0800410 }
411
412 private:
Austin Schuha9644062019-03-28 14:31:52 -0700413 uint32_t GetAutonomousMode() override {
414 ::frc971::autonomous::auto_mode.FetchLatest();
415 if (::frc971::autonomous::auto_mode.get() == nullptr) {
416 LOG(WARNING, "no auto mode values\n");
417 return 0;
418 }
419 return ::frc971::autonomous::auto_mode->mode;
420 }
421
Sabina Davis91b23602019-01-21 00:06:01 -0800422 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800423 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Sabina Davisc6329342019-03-01 20:44:42 -0800424 bool grab_piece_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800425
426 bool switch_ball_ = false;
Tyler Chatowe0241452019-03-08 21:07:50 -0800427
428 VisionControl vision_control_;
429 ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700430 ::aos::monotonic_clock::time_point last_vision_control_ =
431 ::aos::monotonic_clock::time_point::min();
Austin Schuhaab7e162019-03-13 22:44:58 -0700432
433 // Time at which we last did not have a game piece.
434 ::aos::monotonic_clock::time_point last_not_has_piece_ =
435 ::aos::monotonic_clock::time_point::min();
Sabina Davis91b23602019-01-21 00:06:01 -0800436};
437
438} // namespace joysticks
439} // namespace input
440} // namespace y2019
441
442int main() {
443 ::aos::Init(-1);
444 ::aos::ShmEventLoop event_loop;
445 ::y2019::input::joysticks::Reader reader(&event_loop);
446 reader.Run();
447 ::aos::Cleanup();
448}