blob: e040ba6b2cd1fe6a81f6de2bf7d3fefe98f8f2da [file] [log] [blame]
Sabina Davis91b23602019-01-21 00:06:01 -08001#include <math.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Tyler Chatow4fedeea2019-03-10 15:33:36 -07005#include <chrono>
Sabina Davis91b23602019-01-21 00:06:01 -08006
7#include "aos/actions/actions.h"
8#include "aos/init.h"
9#include "aos/input/action_joystick_input.h"
10#include "aos/input/driver_station_data.h"
11#include "aos/input/drivetrain_input.h"
12#include "aos/input/joystick_input.h"
13#include "aos/logging/logging.h"
14#include "aos/logging/logging.h"
Tyler Chatowe0241452019-03-08 21:07:50 -080015#include "aos/network/team_number.h"
Sabina Davis91b23602019-01-21 00:06:01 -080016#include "aos/util/log_interval.h"
Tyler Chatowe0241452019-03-08 21:07:50 -080017#include "aos/vision/events/udp.h"
18#include "external/com_google_protobuf/src/google/protobuf/stubs/stringprintf.h"
Sabina Davis91b23602019-01-21 00:06:01 -080019#include "frc971/autonomous/auto.q.h"
20#include "frc971/autonomous/base_autonomous_actor.h"
21#include "frc971/control_loops/drivetrain/drivetrain.q.h"
James Kuszmauld8deb682019-03-10 10:38:42 -070022#include "frc971/control_loops/drivetrain/localizer.q.h"
Sabina Davis91b23602019-01-21 00:06:01 -080023
24#include "y2019/control_loops/drivetrain/drivetrain_base.h"
James Kuszmaul7d1ef442019-03-23 20:20:50 -070025#include "y2019/control_loops/drivetrain/target_selector.q.h"
Sabina Davis91b23602019-01-21 00:06:01 -080026#include "y2019/control_loops/superstructure/superstructure.q.h"
Sabina Davisc6329342019-03-01 20:44:42 -080027#include "y2019/status_light.q.h"
Tyler Chatowe0241452019-03-08 21:07:50 -080028#include "y2019/vision.pb.h"
Sabina Davis91b23602019-01-21 00:06:01 -080029
30using ::y2019::control_loops::superstructure::superstructure_queue;
James Kuszmaul7d1ef442019-03-23 20:20:50 -070031using ::y2019::control_loops::drivetrain::target_selector_hint;
James Kuszmauld8deb682019-03-10 10:38:42 -070032using ::frc971::control_loops::drivetrain::localizer_control;
Sabina Davis91b23602019-01-21 00:06:01 -080033using ::aos::input::driver_station::ButtonLocation;
34using ::aos::input::driver_station::ControlBit;
35using ::aos::input::driver_station::JoystickAxis;
36using ::aos::input::driver_station::POVLocation;
Tyler Chatowe0241452019-03-08 21:07:50 -080037using ::aos::events::ProtoTXUdpSocket;
Sabina Davis91b23602019-01-21 00:06:01 -080038
Austin Schuhaab7e162019-03-13 22:44:58 -070039namespace chrono = ::std::chrono;
40
Sabina Davis91b23602019-01-21 00:06:01 -080041namespace y2019 {
42namespace input {
43namespace joysticks {
44
Tyler Chatowe0241452019-03-08 21:07:50 -080045using google::protobuf::StringPrintf;
46
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080047struct ElevatorWristPosition {
48 double elevator;
49 double wrist;
50};
Sabina Davis91b23602019-01-21 00:06:01 -080051
Tyler Chatow5eeee902019-04-12 20:47:48 -070052const ButtonLocation kSuctionBall(4, 2);
53const ButtonLocation kSuctionHatch(3, 15);
54const ButtonLocation kDeployStilt(4, 1);
55const ButtonLocation kHalfStilt(4, 3);
56const ButtonLocation kFallOver(3, 16);
57
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080058const ButtonLocation kRocketForwardLower(5, 1);
59const ButtonLocation kRocketForwardMiddle(5, 2);
60const ButtonLocation kRocketForwardUpper(5, 4);
61const ButtonLocation kCargoForward(5, 3);
62
63const POVLocation kRocketBackwardUnpressed(5, -1);
64const POVLocation kRocketBackwardLower(5, 180);
65const POVLocation kRocketBackwardMiddle(5, 90);
66const POVLocation kRocketBackwardUpper(5, 0);
67const POVLocation kCargoBackward(5, 270);
68
69const ButtonLocation kPanelSwitch(5, 7);
70const ButtonLocation kCargoSwitch(5, 8);
71
72const ButtonLocation kBallHPIntakeForward(5, 6);
73const ButtonLocation kBallHPIntakeBackward(5, 5);
74const JoystickAxis kBallOutake(5, 3);
75const JoystickAxis kBallIntake(5, 4);
76
77const ButtonLocation kPanelHPIntakeForward(5, 6);
78const ButtonLocation kPanelHPIntakeBackward(5, 5);
79
80const ButtonLocation kRelease(2, 4);
James Kuszmaulccc368b2019-04-11 20:00:07 -070081// Reuse quickturn for the cancel button.
82const ButtonLocation kCancelAutoMode(2, 3);
Tyler Chatow5eeee902019-04-12 20:47:48 -070083const ButtonLocation kReleaseButtonBoard(3, 4);
84const ButtonLocation kResetLocalizerLeftForwards(3, 10);
85const ButtonLocation kResetLocalizerLeftBackwards(3, 9);
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080086
Tyler Chatow5eeee902019-04-12 20:47:48 -070087const ButtonLocation kResetLocalizerRightForwards(3, 8);
88const ButtonLocation kResetLocalizerRightBackwards(3, 7);
Tyler Chatow07c906b2019-03-09 21:29:06 -080089
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
Tyler Chatow5eeee902019-04-12 20:47:48 -070094const ButtonLocation kCameraLog(3, 14);
Austin Schuh4e2629d2019-03-28 14:44:37 -070095
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080096const ElevatorWristPosition kStowPos{0.36, 0.0};
Austin Schuh02be8b92019-03-24 16:04:14 -070097const ElevatorWristPosition kClimbPos{0.0, M_PI / 4.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080098
Austin Schuh139f59d2019-03-17 18:16:13 -070099const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.01, M_PI / 2.0};
100const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.015, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800101
102const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
103const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
104
Sabina Davise48004f2019-03-02 23:15:24 -0800105const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0};
106const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800107
Sabina Davise48004f2019-03-02 23:15:24 -0800108const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0};
109const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800110
Sabina Davise6fe6c52019-03-03 15:48:51 -0800111const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0};
112const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0};
113
Austin Schuh6c5deb12019-03-24 16:48:32 -0700114const ElevatorWristPosition kBallForwardLowerPos{0.21, 1.27};
Austin Schuh20dc0502019-03-30 07:24:07 -0700115const ElevatorWristPosition kBallBackwardLowerPos{0.43, -1.99};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800116
Austin Schuh20dc0502019-03-30 07:24:07 -0700117const ElevatorWristPosition kBallForwardMiddlePos{0.925, 1.21};
118const ElevatorWristPosition kBallBackwardMiddlePos{1.19, -1.98};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800119
Austin Schuh139f59d2019-03-17 18:16:13 -0700120const ElevatorWristPosition kBallForwardUpperPos{1.51, 0.961};
121const ElevatorWristPosition kBallBackwardUpperPos{1.44, -1.217};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800122
Austin Schuh20dc0502019-03-30 07:24:07 -0700123const ElevatorWristPosition kBallCargoForwardPos{0.59, 1.2};
124const ElevatorWristPosition kBallCargoBackwardPos{0.868265, -2.1};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800125
Sabina Davise6fe6c52019-03-03 15:48:51 -0800126const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097};
127const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800128
Austin Schuh20dc0502019-03-30 07:24:07 -0700129const ElevatorWristPosition kBallIntakePos{0.309, 2.13};
Austin Schuh2cf16b82019-02-15 23:23:22 -0800130
Sabina Davis91b23602019-01-21 00:06:01 -0800131class Reader : public ::aos::input::ActionJoystickInput {
132 public:
133 Reader(::aos::EventLoop *event_loop)
134 : ::aos::input::ActionJoystickInput(
135 event_loop,
James Kuszmaulccc368b2019-04-11 20:00:07 -0700136 ::y2019::control_loops::drivetrain::GetDrivetrainConfig(),
137 {.run_teleop_in_auto = true,
138 .cancel_auto_button = kCancelAutoMode}) {
Tyler Chatowe0241452019-03-08 21:07:50 -0800139 const uint16_t team = ::aos::network::GetTeamNumber();
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800140 superstructure_queue.goal.FetchLatest();
141 if (superstructure_queue.goal.get()) {
Sabina Davisc6329342019-03-01 20:44:42 -0800142 grab_piece_ = superstructure_queue.goal->suction.grab_piece;
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700143 switch_ball_ = superstructure_queue.goal->suction.gamepiece_mode == 0;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800144 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800145 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
146 StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000));
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800147 }
Sabina Davis91b23602019-01-21 00:06:01 -0800148
Austin Schuha78857f2019-03-13 22:43:41 -0700149 void AutoEnded() override {
150 LOG(INFO, "Auto ended, assuming disc and have piece\n");
151 grab_piece_ = true;
152 switch_ball_ = false;
153 }
154
155 void HandleTeleop(const ::aos::input::driver_station::Data &data) override {
Austin Schuhaab7e162019-03-13 22:44:58 -0700156 ::aos::monotonic_clock::time_point monotonic_now =
157 ::aos::monotonic_clock::now();
Sabina Davis91b23602019-01-21 00:06:01 -0800158 superstructure_queue.position.FetchLatest();
159 superstructure_queue.status.FetchLatest();
160 if (!superstructure_queue.status.get() ||
161 !superstructure_queue.position.get()) {
162 LOG(ERROR, "Got no superstructure status packet.\n");
163 return;
164 }
165
Austin Schuhaab7e162019-03-13 22:44:58 -0700166 if (!superstructure_queue.status->has_piece) {
167 last_not_has_piece_ = monotonic_now;
168 }
169
Sabina Davis91b23602019-01-21 00:06:01 -0800170 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
171
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700172 {
173 auto target_hint = target_selector_hint.MakeMessage();
174 if (data.IsPressed(kNearCargoHint)) {
175 target_hint->suggested_target = 1;
176 } else if (data.IsPressed(kMidCargoHint)) {
177 target_hint->suggested_target = 2;
178 } else if (data.IsPressed(kFarCargoHint)) {
179 target_hint->suggested_target = 3;
180 } else {
181 target_hint->suggested_target = 0;
182 }
183 if (!target_hint.Send()) {
184 LOG(ERROR, "Failed to send target selector hint.\n");
185 }
186 }
187
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700188 if (data.PosEdge(kResetLocalizerLeftForwards)) {
James Kuszmauld8deb682019-03-10 10:38:42 -0700189 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700190 // Start at the left feeder station.
James Kuszmauld8deb682019-03-10 10:38:42 -0700191 localizer_resetter->x = 0.4;
192 localizer_resetter->y = 3.4;
193 localizer_resetter->theta = 0.0;
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700194
195 if (!localizer_resetter.Send()) {
196 LOG(ERROR, "Failed to reset localizer.\n");
197 }
198 }
199
200 if (data.PosEdge(kResetLocalizerLeftBackwards)) {
201 auto localizer_resetter = localizer_control.MakeMessage();
202 // Start at the left feeder station.
203 localizer_resetter->x = 0.4;
204 localizer_resetter->y = 3.4;
205 localizer_resetter->theta = M_PI;
206
207 if (!localizer_resetter.Send()) {
208 LOG(ERROR, "Failed to reset localizer.\n");
209 }
210 }
211
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700212 if (data.PosEdge(kResetLocalizerRightForwards)) {
213 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700214 // Start at the right feeder station.
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700215 localizer_resetter->x = 0.4;
216 localizer_resetter->y = -3.4;
217 localizer_resetter->theta = 0.0;
218
219 if (!localizer_resetter.Send()) {
220 LOG(ERROR, "Failed to reset localizer.\n");
221 }
222 }
223
224 if (data.PosEdge(kResetLocalizerRightBackwards)) {
225 auto localizer_resetter = localizer_control.MakeMessage();
Austin Schuh3632f8d2019-03-22 21:14:14 -0700226 // Start at the right feeder station.
Austin Schuhbb1df6f2019-03-17 18:15:43 -0700227 localizer_resetter->x = 0.4;
228 localizer_resetter->y = -3.4;
229 localizer_resetter->theta = M_PI;
230
James Kuszmauld8deb682019-03-10 10:38:42 -0700231 if (!localizer_resetter.Send()) {
232 LOG(ERROR, "Failed to reset localizer.\n");
233 }
234 }
235
Austin Schuh1a17e132019-02-17 15:05:06 -0800236 if (data.IsPressed(kSuctionBall)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800237 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800238 } else if (data.IsPressed(kSuctionHatch)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800239 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800240 } else if (data.IsPressed(kRelease) ||
Tyler Chatow5eeee902019-04-12 20:47:48 -0700241 data.IsPressed(kReleaseButtonBoard) ||
Austin Schuh1a17e132019-02-17 15:05:06 -0800242 !superstructure_queue.status->has_piece) {
Sabina Davisc6329342019-03-01 20:44:42 -0800243 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800244 }
Sabina Davis91b23602019-01-21 00:06:01 -0800245
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800246 if (data.IsPressed(kRocketBackwardUnpressed)) {
247 elevator_wrist_pos_ = kStowPos;
248 }
249 new_superstructure_goal->intake.unsafe_goal = -1.2;
250 new_superstructure_goal->roller_voltage = 0.0;
251
252 const bool kDoBallIntake = data.GetAxis(kBallIntake) > 0.9;
253 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
254
255 if (data.IsPressed(kPanelSwitch)) {
256 switch_ball_ = false;
257 } else if (data.IsPressed(kCargoSwitch)) {
258 switch_ball_ = true;
259 }
260
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800261 if (switch_ball_) {
262 if (superstructure_queue.status->has_piece) {
263 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800264 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800265
266 // Go to intake position and apply vacuum
267 if (data.IsPressed(kBallHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800268 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800269 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
270 } else if (data.IsPressed(kBallHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800271 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800272 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
273 }
274
275 // Go to elevator/wrist position. Overrides intake position if pressed so
276 // we can re-grab the ball.
277 if (data.IsPressed(kRocketForwardLower)) {
278 elevator_wrist_pos_ = kBallForwardLowerPos;
279 } else if (data.IsPressed(kRocketBackwardLower)) {
280 elevator_wrist_pos_ = kBallBackwardLowerPos;
281 } else if (data.IsPressed(kRocketForwardMiddle)) {
282 elevator_wrist_pos_ = kBallForwardMiddlePos;
283 } else if (data.IsPressed(kRocketBackwardMiddle)) {
284 elevator_wrist_pos_ = kBallBackwardMiddlePos;
285 } else if (data.IsPressed(kRocketForwardUpper)) {
286 elevator_wrist_pos_ = kBallForwardUpperPos;
287 } else if (data.IsPressed(kRocketBackwardUpper)) {
288 elevator_wrist_pos_ = kBallBackwardUpperPos;
289 } else if (data.IsPressed(kCargoForward)) {
290 elevator_wrist_pos_ = kBallCargoForwardPos;
291 } else if (data.IsPressed(kCargoBackward)) {
292 elevator_wrist_pos_ = kBallCargoBackwardPos;
293 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800294 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800295 if (data.IsPressed(kPanelHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800296 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800297 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
298 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800299 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800300 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
301 }
302
303 // Go to elevator/wrist position. Overrides intake position if pressed so
304 // we can re-grab the panel.
305 if (data.IsPressed(kRocketForwardLower)) {
306 elevator_wrist_pos_ = kPanelForwardLowerPos;
307 } else if (data.IsPressed(kRocketBackwardLower)) {
308 elevator_wrist_pos_ = kPanelBackwardLowerPos;
309 } else if (data.IsPressed(kRocketForwardMiddle)) {
310 elevator_wrist_pos_ = kPanelForwardMiddlePos;
311 } else if (data.IsPressed(kRocketBackwardMiddle)) {
312 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
313 } else if (data.IsPressed(kRocketForwardUpper)) {
314 elevator_wrist_pos_ = kPanelForwardUpperPos;
315 } else if (data.IsPressed(kRocketBackwardUpper)) {
316 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800317 } else if (data.IsPressed(kCargoForward)) {
318 elevator_wrist_pos_ = kPanelCargoForwardPos;
319 } else if (data.IsPressed(kCargoBackward)) {
320 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800321 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800322 }
323
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800324 if (switch_ball_) {
325 if (kDoBallOutake ||
Austin Schuhaab7e162019-03-13 22:44:58 -0700326 (kDoBallIntake &&
Austin Schuh20dc0502019-03-30 07:24:07 -0700327 monotonic_now < last_not_has_piece_ + chrono::milliseconds(200))) {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800328 new_superstructure_goal->intake.unsafe_goal = 0.959327;
329 }
Austin Schuh23a51632019-02-19 16:50:36 -0800330
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800331 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
332 elevator_wrist_pos_ = kBallIntakePos;
333 new_superstructure_goal->roller_voltage = 9.0;
Sabina Davisc6329342019-03-01 20:44:42 -0800334 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800335 } else {
336 if (kDoBallOutake) {
337 new_superstructure_goal->roller_voltage = -6.0;
338 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800339 new_superstructure_goal->roller_voltage = 0.0;
340 }
341 }
Austin Schuh23a51632019-02-19 16:50:36 -0800342 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800343
Austin Schuh457bde32019-03-17 18:16:41 -0700344 constexpr double kDeployStiltPosition = 0.5;
345
346 // TODO(sabina): max height please?
347 if (data.IsPressed(kFallOver)) {
348 new_superstructure_goal->stilts.unsafe_goal = 0.71;
349 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
350 new_superstructure_goal->stilts.profile_params.max_acceleration = 1.25;
351 } else if (data.IsPressed(kDeployStilt)) {
352 new_superstructure_goal->stilts.unsafe_goal = kDeployStiltPosition;
353 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
354 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
355 } else if (data.IsPressed(kHalfStilt)) {
356 new_superstructure_goal->stilts.unsafe_goal = 0.345;
357 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
358 } else {
359 new_superstructure_goal->stilts.unsafe_goal = 0.005;
360 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
361 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
362 }
363
James Kuszmaul0448f142019-03-24 16:22:34 -0700364 if (superstructure_queue.status->stilts.position > 0.1) {
Austin Schuh02be8b92019-03-24 16:04:14 -0700365 elevator_wrist_pos_ = kClimbPos;
Austin Schuhd8a18bb2019-03-24 19:21:38 -0700366 new_superstructure_goal->wrist.profile_params.max_acceleration = 10;
367 new_superstructure_goal->elevator.profile_params.max_acceleration = 6;
Austin Schuh02be8b92019-03-24 16:04:14 -0700368 }
369
Austin Schuh457bde32019-03-17 18:16:41 -0700370 if (superstructure_queue.status->stilts.position > kDeployStiltPosition &&
371 new_superstructure_goal->stilts.unsafe_goal == kDeployStiltPosition) {
372 new_superstructure_goal->stilts.profile_params.max_velocity = 0.30;
373 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
374 }
375
Tyler Chatow5eeee902019-04-12 20:47:48 -0700376 if (data.IsPressed(kRelease) || data.IsPressed(kReleaseButtonBoard)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800377 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800378 }
379
Sabina Davisc6329342019-03-01 20:44:42 -0800380 if (switch_ball_) {
381 new_superstructure_goal->suction.gamepiece_mode = 0;
382 } else {
383 new_superstructure_goal->suction.gamepiece_mode = 1;
384 }
385
Tyler Chatowe0241452019-03-08 21:07:50 -0800386 vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0);
387
Sabina Davisc6329342019-03-01 20:44:42 -0800388 new_superstructure_goal->suction.grab_piece = grab_piece_;
Sabina Davis91b23602019-01-21 00:06:01 -0800389
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800390 new_superstructure_goal->elevator.unsafe_goal =
391 elevator_wrist_pos_.elevator;
392 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800393
394 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
395 if (!new_superstructure_goal.Send()) {
396 LOG(ERROR, "Sending superstructure goal failed.\n");
397 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800398
Austin Schuhaab7e162019-03-13 22:44:58 -0700399 if (monotonic_now >
400 last_vision_control_ + ::std::chrono::milliseconds(50)) {
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700401 video_tx_->Send(vision_control_);
Austin Schuhaab7e162019-03-13 22:44:58 -0700402 last_vision_control_ = monotonic_now;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700403 }
Austin Schuh4e2629d2019-03-28 14:44:37 -0700404
405 {
406 auto camera_log_message = camera_log.MakeMessage();
407 camera_log_message->log = data.IsPressed(kCameraLog);
408 LOG_STRUCT(DEBUG, "camera_log", *camera_log_message);
409 camera_log_message.Send();
410 }
Sabina Davis91b23602019-01-21 00:06:01 -0800411 }
412
413 private:
Austin Schuha9644062019-03-28 14:31:52 -0700414 uint32_t GetAutonomousMode() override {
415 ::frc971::autonomous::auto_mode.FetchLatest();
416 if (::frc971::autonomous::auto_mode.get() == nullptr) {
417 LOG(WARNING, "no auto mode values\n");
418 return 0;
419 }
420 return ::frc971::autonomous::auto_mode->mode;
421 }
422
Sabina Davis91b23602019-01-21 00:06:01 -0800423 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800424 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Sabina Davisc6329342019-03-01 20:44:42 -0800425 bool grab_piece_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800426
427 bool switch_ball_ = false;
Tyler Chatowe0241452019-03-08 21:07:50 -0800428
429 VisionControl vision_control_;
430 ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700431 ::aos::monotonic_clock::time_point last_vision_control_ =
432 ::aos::monotonic_clock::time_point::min();
Austin Schuhaab7e162019-03-13 22:44:58 -0700433
434 // Time at which we last did not have a game piece.
435 ::aos::monotonic_clock::time_point last_not_has_piece_ =
436 ::aos::monotonic_clock::time_point::min();
Sabina Davis91b23602019-01-21 00:06:01 -0800437};
438
439} // namespace joysticks
440} // namespace input
441} // namespace y2019
442
443int main() {
444 ::aos::Init(-1);
445 ::aos::ShmEventLoop event_loop;
446 ::y2019::input::joysticks::Reader reader(&event_loop);
447 reader.Run();
448 ::aos::Cleanup();
449}