blob: 7f9fe6cb454818ab30f563c454c3fe9a7433115b [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>
5
6#include "aos/actions/actions.h"
7#include "aos/init.h"
8#include "aos/input/action_joystick_input.h"
9#include "aos/input/driver_station_data.h"
10#include "aos/input/drivetrain_input.h"
11#include "aos/input/joystick_input.h"
12#include "aos/logging/logging.h"
13#include "aos/logging/logging.h"
14#include "aos/util/log_interval.h"
15#include "frc971/autonomous/auto.q.h"
16#include "frc971/autonomous/base_autonomous_actor.h"
17#include "frc971/control_loops/drivetrain/drivetrain.q.h"
James Kuszmauld8deb682019-03-10 10:38:42 -070018#include "frc971/control_loops/drivetrain/localizer.q.h"
Sabina Davis91b23602019-01-21 00:06:01 -080019
20#include "y2019/control_loops/drivetrain/drivetrain_base.h"
21#include "y2019/control_loops/superstructure/superstructure.q.h"
Sabina Davisc6329342019-03-01 20:44:42 -080022#include "y2019/status_light.q.h"
Sabina Davis91b23602019-01-21 00:06:01 -080023
24using ::y2019::control_loops::superstructure::superstructure_queue;
James Kuszmauld8deb682019-03-10 10:38:42 -070025using ::frc971::control_loops::drivetrain::localizer_control;
Sabina Davis91b23602019-01-21 00:06:01 -080026using ::aos::input::driver_station::ButtonLocation;
27using ::aos::input::driver_station::ControlBit;
28using ::aos::input::driver_station::JoystickAxis;
29using ::aos::input::driver_station::POVLocation;
30
31namespace y2019 {
32namespace input {
33namespace joysticks {
34
Austin Schuh1a17e132019-02-17 15:05:06 -080035const ButtonLocation kSuctionBall(3, 13);
36const ButtonLocation kSuctionHatch(3, 12);
37const ButtonLocation kDeployStilt(3, 8);
Sabina Davis069cbca2019-03-08 23:35:18 -080038const ButtonLocation kHalfStilt(3, 6);
Austin Schuh1a17e132019-02-17 15:05:06 -080039const ButtonLocation kFallOver(3, 9);
Austin Schuh2cf16b82019-02-15 23:23:22 -080040
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080041struct ElevatorWristPosition {
42 double elevator;
43 double wrist;
44};
Sabina Davis91b23602019-01-21 00:06:01 -080045
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080046const ButtonLocation kRocketForwardLower(5, 1);
47const ButtonLocation kRocketForwardMiddle(5, 2);
48const ButtonLocation kRocketForwardUpper(5, 4);
49const ButtonLocation kCargoForward(5, 3);
50
51const POVLocation kRocketBackwardUnpressed(5, -1);
52const POVLocation kRocketBackwardLower(5, 180);
53const POVLocation kRocketBackwardMiddle(5, 90);
54const POVLocation kRocketBackwardUpper(5, 0);
55const POVLocation kCargoBackward(5, 270);
56
57const ButtonLocation kPanelSwitch(5, 7);
58const ButtonLocation kCargoSwitch(5, 8);
59
60const ButtonLocation kBallHPIntakeForward(5, 6);
61const ButtonLocation kBallHPIntakeBackward(5, 5);
62const JoystickAxis kBallOutake(5, 3);
63const JoystickAxis kBallIntake(5, 4);
64
65const ButtonLocation kPanelHPIntakeForward(5, 6);
66const ButtonLocation kPanelHPIntakeBackward(5, 5);
67
68const ButtonLocation kRelease(2, 4);
James Kuszmauld8deb682019-03-10 10:38:42 -070069const ButtonLocation kResetLocalizer(4, 3);
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080070
71const ElevatorWristPosition kStowPos{0.36, 0.0};
72
73const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.04, M_PI / 2.0};
Sabina Davise6fe6c52019-03-03 15:48:51 -080074const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.05, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080075
76const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
77const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
78
Sabina Davise48004f2019-03-02 23:15:24 -080079const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0};
80const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080081
Sabina Davise48004f2019-03-02 23:15:24 -080082const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0};
83const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080084
Sabina Davise6fe6c52019-03-03 15:48:51 -080085const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0};
86const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0};
87
88const ElevatorWristPosition kBallForwardLowerPos{0.46, M_PI / 2.0};
89const ElevatorWristPosition kBallBackwardLowerPos{0.15, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080090
Sabina Davise48004f2019-03-02 23:15:24 -080091const ElevatorWristPosition kBallForwardMiddlePos{1.16, 1.546};
92const ElevatorWristPosition kBallBackwardMiddlePos{0.876021, -1.546};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080093
Sabina Davise48004f2019-03-02 23:15:24 -080094const ElevatorWristPosition kBallForwardUpperPos{1.50, 0.961};
95const ElevatorWristPosition kBallBackwardUpperPos{1.41, -1.217};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080096
Sabina Davise48004f2019-03-02 23:15:24 -080097const ElevatorWristPosition kBallCargoForwardPos{0.699044, 1.353};
98const ElevatorWristPosition kBallCargoBackwardPos{0.828265, -1.999};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080099
Sabina Davise6fe6c52019-03-03 15:48:51 -0800100const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097};
101const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800102
103const ElevatorWristPosition kBallIntakePos{0.29, 2.14};
Austin Schuh2cf16b82019-02-15 23:23:22 -0800104
Sabina Davis91b23602019-01-21 00:06:01 -0800105class Reader : public ::aos::input::ActionJoystickInput {
106 public:
107 Reader(::aos::EventLoop *event_loop)
108 : ::aos::input::ActionJoystickInput(
109 event_loop,
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800110 ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {
111 superstructure_queue.goal.FetchLatest();
112 if (superstructure_queue.goal.get()) {
Sabina Davisc6329342019-03-01 20:44:42 -0800113 grab_piece_ = superstructure_queue.goal->suction.grab_piece;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800114 }
115 }
Sabina Davis91b23602019-01-21 00:06:01 -0800116
117 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
118 superstructure_queue.position.FetchLatest();
119 superstructure_queue.status.FetchLatest();
120 if (!superstructure_queue.status.get() ||
121 !superstructure_queue.position.get()) {
122 LOG(ERROR, "Got no superstructure status packet.\n");
123 return;
124 }
125
126 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
127
James Kuszmauld8deb682019-03-10 10:38:42 -0700128 if (data.PosEdge(kResetLocalizer)) {
129 auto localizer_resetter = localizer_control.MakeMessage();
130 localizer_resetter->x = 0.4;
131 localizer_resetter->y = 3.4;
132 localizer_resetter->theta = 0.0;
133 if (!localizer_resetter.Send()) {
134 LOG(ERROR, "Failed to reset localizer.\n");
135 }
136 }
137
Austin Schuh1a17e132019-02-17 15:05:06 -0800138 if (data.IsPressed(kSuctionBall)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800139 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800140 } else if (data.IsPressed(kSuctionHatch)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800141 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800142 } else if (data.IsPressed(kRelease) ||
143 !superstructure_queue.status->has_piece) {
Sabina Davisc6329342019-03-01 20:44:42 -0800144 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800145 }
Sabina Davis91b23602019-01-21 00:06:01 -0800146
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800147 if (data.IsPressed(kRocketBackwardUnpressed)) {
148 elevator_wrist_pos_ = kStowPos;
149 }
150 new_superstructure_goal->intake.unsafe_goal = -1.2;
151 new_superstructure_goal->roller_voltage = 0.0;
152
153 const bool kDoBallIntake = data.GetAxis(kBallIntake) > 0.9;
154 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
155
156 if (data.IsPressed(kPanelSwitch)) {
157 switch_ball_ = false;
158 } else if (data.IsPressed(kCargoSwitch)) {
159 switch_ball_ = true;
160 }
161
Sabina Davis91b23602019-01-21 00:06:01 -0800162 // TODO(sabina): max height please?
Austin Schuh77ac3212019-02-19 16:50:14 -0800163 if (data.IsPressed(kFallOver)) {
Austin Schuh1a17e132019-02-17 15:05:06 -0800164 new_superstructure_goal->stilts.unsafe_goal = 0.71;
Michael Schuh587dcb52019-02-28 21:31:03 -0800165 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800166 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Austin Schuh77ac3212019-02-19 16:50:14 -0800167 } else if (data.IsPressed(kDeployStilt)) {
168 new_superstructure_goal->stilts.unsafe_goal = 0.50;
Michael Schuh587dcb52019-02-28 21:31:03 -0800169 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800170 if (stilts_was_above_) {
171 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
172 } else {
173 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
174 }
Sabina Davis069cbca2019-03-08 23:35:18 -0800175 } else if (data.IsPressed(kHalfStilt)) {
176 new_superstructure_goal->stilts.unsafe_goal = 0.345;
177 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
178 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
Sabina Davis91b23602019-01-21 00:06:01 -0800179 } else {
Sabina Davisbfdeb362019-03-09 16:20:01 -0800180 new_superstructure_goal->stilts.unsafe_goal = 0.005;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800181 new_superstructure_goal->stilts.profile_params.max_velocity = 0.25;
182 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800183 }
184
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800185 if (superstructure_queue.status->stilts.position > 0.65) {
186 stilts_was_above_ = true;
187 } else if (superstructure_queue.status->stilts.position < 0.1) {
188 stilts_was_above_ = false;
Austin Schuh2cf16b82019-02-15 23:23:22 -0800189 }
190
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800191 if (switch_ball_) {
192 if (superstructure_queue.status->has_piece) {
193 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800194 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800195
196 // Go to intake position and apply vacuum
197 if (data.IsPressed(kBallHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800198 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800199 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
200 } else if (data.IsPressed(kBallHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800201 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800202 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
203 }
204
205 // Go to elevator/wrist position. Overrides intake position if pressed so
206 // we can re-grab the ball.
207 if (data.IsPressed(kRocketForwardLower)) {
208 elevator_wrist_pos_ = kBallForwardLowerPos;
209 } else if (data.IsPressed(kRocketBackwardLower)) {
210 elevator_wrist_pos_ = kBallBackwardLowerPos;
211 } else if (data.IsPressed(kRocketForwardMiddle)) {
212 elevator_wrist_pos_ = kBallForwardMiddlePos;
213 } else if (data.IsPressed(kRocketBackwardMiddle)) {
214 elevator_wrist_pos_ = kBallBackwardMiddlePos;
215 } else if (data.IsPressed(kRocketForwardUpper)) {
216 elevator_wrist_pos_ = kBallForwardUpperPos;
217 } else if (data.IsPressed(kRocketBackwardUpper)) {
218 elevator_wrist_pos_ = kBallBackwardUpperPos;
219 } else if (data.IsPressed(kCargoForward)) {
220 elevator_wrist_pos_ = kBallCargoForwardPos;
221 } else if (data.IsPressed(kCargoBackward)) {
222 elevator_wrist_pos_ = kBallCargoBackwardPos;
223 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800224 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800225 if (data.IsPressed(kPanelHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800226 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800227 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
228 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800229 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800230 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
231 }
232
233 // Go to elevator/wrist position. Overrides intake position if pressed so
234 // we can re-grab the panel.
235 if (data.IsPressed(kRocketForwardLower)) {
236 elevator_wrist_pos_ = kPanelForwardLowerPos;
237 } else if (data.IsPressed(kRocketBackwardLower)) {
238 elevator_wrist_pos_ = kPanelBackwardLowerPos;
239 } else if (data.IsPressed(kRocketForwardMiddle)) {
240 elevator_wrist_pos_ = kPanelForwardMiddlePos;
241 } else if (data.IsPressed(kRocketBackwardMiddle)) {
242 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
243 } else if (data.IsPressed(kRocketForwardUpper)) {
244 elevator_wrist_pos_ = kPanelForwardUpperPos;
245 } else if (data.IsPressed(kRocketBackwardUpper)) {
246 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800247 } else if (data.IsPressed(kCargoForward)) {
248 elevator_wrist_pos_ = kPanelCargoForwardPos;
249 } else if (data.IsPressed(kCargoBackward)) {
250 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800251 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800252 }
253
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800254 if (switch_ball_) {
255 if (kDoBallOutake ||
256 (kDoBallIntake && !superstructure_queue.status->has_piece)) {
257 new_superstructure_goal->intake.unsafe_goal = 0.959327;
258 }
Austin Schuh23a51632019-02-19 16:50:36 -0800259
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800260 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
261 elevator_wrist_pos_ = kBallIntakePos;
262 new_superstructure_goal->roller_voltage = 9.0;
Sabina Davisc6329342019-03-01 20:44:42 -0800263 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800264 } else {
265 if (kDoBallOutake) {
266 new_superstructure_goal->roller_voltage = -6.0;
267 } else {
268 new_superstructure_goal->intake.unsafe_goal = -1.2;
269 new_superstructure_goal->roller_voltage = 0.0;
270 }
271 }
Austin Schuh23a51632019-02-19 16:50:36 -0800272 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800273
274 if (data.IsPressed(kRelease)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800275 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800276 }
277
Sabina Davisc6329342019-03-01 20:44:42 -0800278 if (switch_ball_) {
279 new_superstructure_goal->suction.gamepiece_mode = 0;
280 } else {
281 new_superstructure_goal->suction.gamepiece_mode = 1;
282 }
283
284 new_superstructure_goal->suction.grab_piece = grab_piece_;
Sabina Davis91b23602019-01-21 00:06:01 -0800285
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800286 new_superstructure_goal->elevator.unsafe_goal =
287 elevator_wrist_pos_.elevator;
288 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800289
290 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
291 if (!new_superstructure_goal.Send()) {
292 LOG(ERROR, "Sending superstructure goal failed.\n");
293 }
294 }
295
296 private:
297 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800298 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Sabina Davisc6329342019-03-01 20:44:42 -0800299 bool grab_piece_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800300
301 bool switch_ball_ = false;
302 bool stilts_was_above_ = false;
Sabina Davis91b23602019-01-21 00:06:01 -0800303};
304
305} // namespace joysticks
306} // namespace input
307} // namespace y2019
308
309int main() {
310 ::aos::Init(-1);
311 ::aos::ShmEventLoop event_loop;
312 ::y2019::input::joysticks::Reader reader(&event_loop);
313 reader.Run();
314 ::aos::Cleanup();
315}