blob: 26a8ce1caadcd16aa0c2fde2e96e2e5c491de1d8 [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"
18
19#include "y2019/control_loops/drivetrain/drivetrain_base.h"
20#include "y2019/control_loops/superstructure/superstructure.q.h"
Sabina Davisc6329342019-03-01 20:44:42 -080021#include "y2019/status_light.q.h"
Sabina Davis91b23602019-01-21 00:06:01 -080022
23using ::y2019::control_loops::superstructure::superstructure_queue;
24using ::aos::input::driver_station::ButtonLocation;
25using ::aos::input::driver_station::ControlBit;
26using ::aos::input::driver_station::JoystickAxis;
27using ::aos::input::driver_station::POVLocation;
28
29namespace y2019 {
30namespace input {
31namespace joysticks {
32
Austin Schuh1a17e132019-02-17 15:05:06 -080033const ButtonLocation kSuctionBall(3, 13);
34const ButtonLocation kSuctionHatch(3, 12);
35const ButtonLocation kDeployStilt(3, 8);
36const ButtonLocation kFallOver(3, 9);
Austin Schuh2cf16b82019-02-15 23:23:22 -080037
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080038struct ElevatorWristPosition {
39 double elevator;
40 double wrist;
41};
Sabina Davis91b23602019-01-21 00:06:01 -080042
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080043const ButtonLocation kRocketForwardLower(5, 1);
44const ButtonLocation kRocketForwardMiddle(5, 2);
45const ButtonLocation kRocketForwardUpper(5, 4);
46const ButtonLocation kCargoForward(5, 3);
47
48const POVLocation kRocketBackwardUnpressed(5, -1);
49const POVLocation kRocketBackwardLower(5, 180);
50const POVLocation kRocketBackwardMiddle(5, 90);
51const POVLocation kRocketBackwardUpper(5, 0);
52const POVLocation kCargoBackward(5, 270);
53
54const ButtonLocation kPanelSwitch(5, 7);
55const ButtonLocation kCargoSwitch(5, 8);
56
57const ButtonLocation kBallHPIntakeForward(5, 6);
58const ButtonLocation kBallHPIntakeBackward(5, 5);
59const JoystickAxis kBallOutake(5, 3);
60const JoystickAxis kBallIntake(5, 4);
61
62const ButtonLocation kPanelHPIntakeForward(5, 6);
63const ButtonLocation kPanelHPIntakeBackward(5, 5);
64
65const ButtonLocation kRelease(2, 4);
66
67const ElevatorWristPosition kStowPos{0.36, 0.0};
68
69const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.04, M_PI / 2.0};
Sabina Davise6fe6c52019-03-03 15:48:51 -080070const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.05, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080071
72const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
73const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
74
Sabina Davise48004f2019-03-02 23:15:24 -080075const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0};
76const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080077
Sabina Davise48004f2019-03-02 23:15:24 -080078const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0};
79const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080080
Sabina Davise6fe6c52019-03-03 15:48:51 -080081const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0};
82const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0};
83
84const ElevatorWristPosition kBallForwardLowerPos{0.46, M_PI / 2.0};
85const ElevatorWristPosition kBallBackwardLowerPos{0.15, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080086
Sabina Davise48004f2019-03-02 23:15:24 -080087const ElevatorWristPosition kBallForwardMiddlePos{1.16, 1.546};
88const ElevatorWristPosition kBallBackwardMiddlePos{0.876021, -1.546};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080089
Sabina Davise48004f2019-03-02 23:15:24 -080090const ElevatorWristPosition kBallForwardUpperPos{1.50, 0.961};
91const ElevatorWristPosition kBallBackwardUpperPos{1.41, -1.217};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080092
Sabina Davise48004f2019-03-02 23:15:24 -080093const ElevatorWristPosition kBallCargoForwardPos{0.699044, 1.353};
94const ElevatorWristPosition kBallCargoBackwardPos{0.828265, -1.999};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080095
Sabina Davise6fe6c52019-03-03 15:48:51 -080096const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097};
97const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080098
99const ElevatorWristPosition kBallIntakePos{0.29, 2.14};
Austin Schuh2cf16b82019-02-15 23:23:22 -0800100
Sabina Davis91b23602019-01-21 00:06:01 -0800101class Reader : public ::aos::input::ActionJoystickInput {
102 public:
103 Reader(::aos::EventLoop *event_loop)
104 : ::aos::input::ActionJoystickInput(
105 event_loop,
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800106 ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {
107 superstructure_queue.goal.FetchLatest();
108 if (superstructure_queue.goal.get()) {
Sabina Davisc6329342019-03-01 20:44:42 -0800109 grab_piece_ = superstructure_queue.goal->suction.grab_piece;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800110 }
111 }
Sabina Davis91b23602019-01-21 00:06:01 -0800112
113 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
114 superstructure_queue.position.FetchLatest();
115 superstructure_queue.status.FetchLatest();
116 if (!superstructure_queue.status.get() ||
117 !superstructure_queue.position.get()) {
118 LOG(ERROR, "Got no superstructure status packet.\n");
119 return;
120 }
121
122 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
123
Austin Schuh1a17e132019-02-17 15:05:06 -0800124 if (data.IsPressed(kSuctionBall)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800125 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800126 } else if (data.IsPressed(kSuctionHatch)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800127 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800128 } else if (data.IsPressed(kRelease) ||
129 !superstructure_queue.status->has_piece) {
Sabina Davisc6329342019-03-01 20:44:42 -0800130 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800131 }
Sabina Davis91b23602019-01-21 00:06:01 -0800132
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800133 if (data.IsPressed(kRocketBackwardUnpressed)) {
134 elevator_wrist_pos_ = kStowPos;
135 }
136 new_superstructure_goal->intake.unsafe_goal = -1.2;
137 new_superstructure_goal->roller_voltage = 0.0;
138
139 const bool kDoBallIntake = data.GetAxis(kBallIntake) > 0.9;
140 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
141
142 if (data.IsPressed(kPanelSwitch)) {
143 switch_ball_ = false;
144 } else if (data.IsPressed(kCargoSwitch)) {
145 switch_ball_ = true;
146 }
147
Sabina Davis91b23602019-01-21 00:06:01 -0800148 // TODO(sabina): max height please?
Austin Schuh77ac3212019-02-19 16:50:14 -0800149 if (data.IsPressed(kFallOver)) {
Austin Schuh1a17e132019-02-17 15:05:06 -0800150 new_superstructure_goal->stilts.unsafe_goal = 0.71;
Michael Schuh587dcb52019-02-28 21:31:03 -0800151 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800152 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Austin Schuh77ac3212019-02-19 16:50:14 -0800153 } else if (data.IsPressed(kDeployStilt)) {
154 new_superstructure_goal->stilts.unsafe_goal = 0.50;
Michael Schuh587dcb52019-02-28 21:31:03 -0800155 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800156 if (stilts_was_above_) {
157 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
158 } else {
159 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
160 }
Sabina Davis91b23602019-01-21 00:06:01 -0800161 } else {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800162 new_superstructure_goal->stilts.unsafe_goal = 0.01;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800163 new_superstructure_goal->stilts.profile_params.max_velocity = 0.25;
164 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800165 }
166
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800167 if (superstructure_queue.status->stilts.position > 0.65) {
168 stilts_was_above_ = true;
169 } else if (superstructure_queue.status->stilts.position < 0.1) {
170 stilts_was_above_ = false;
Austin Schuh2cf16b82019-02-15 23:23:22 -0800171 }
172
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800173 if (switch_ball_) {
174 if (superstructure_queue.status->has_piece) {
175 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800176 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800177
178 // Go to intake position and apply vacuum
179 if (data.IsPressed(kBallHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800180 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800181 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
182 } else if (data.IsPressed(kBallHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800183 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800184 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
185 }
186
187 // Go to elevator/wrist position. Overrides intake position if pressed so
188 // we can re-grab the ball.
189 if (data.IsPressed(kRocketForwardLower)) {
190 elevator_wrist_pos_ = kBallForwardLowerPos;
191 } else if (data.IsPressed(kRocketBackwardLower)) {
192 elevator_wrist_pos_ = kBallBackwardLowerPos;
193 } else if (data.IsPressed(kRocketForwardMiddle)) {
194 elevator_wrist_pos_ = kBallForwardMiddlePos;
195 } else if (data.IsPressed(kRocketBackwardMiddle)) {
196 elevator_wrist_pos_ = kBallBackwardMiddlePos;
197 } else if (data.IsPressed(kRocketForwardUpper)) {
198 elevator_wrist_pos_ = kBallForwardUpperPos;
199 } else if (data.IsPressed(kRocketBackwardUpper)) {
200 elevator_wrist_pos_ = kBallBackwardUpperPos;
201 } else if (data.IsPressed(kCargoForward)) {
202 elevator_wrist_pos_ = kBallCargoForwardPos;
203 } else if (data.IsPressed(kCargoBackward)) {
204 elevator_wrist_pos_ = kBallCargoBackwardPos;
205 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800206 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800207 if (data.IsPressed(kPanelHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800208 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800209 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
210 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800211 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800212 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
213 }
214
215 // Go to elevator/wrist position. Overrides intake position if pressed so
216 // we can re-grab the panel.
217 if (data.IsPressed(kRocketForwardLower)) {
218 elevator_wrist_pos_ = kPanelForwardLowerPos;
219 } else if (data.IsPressed(kRocketBackwardLower)) {
220 elevator_wrist_pos_ = kPanelBackwardLowerPos;
221 } else if (data.IsPressed(kRocketForwardMiddle)) {
222 elevator_wrist_pos_ = kPanelForwardMiddlePos;
223 } else if (data.IsPressed(kRocketBackwardMiddle)) {
224 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
225 } else if (data.IsPressed(kRocketForwardUpper)) {
226 elevator_wrist_pos_ = kPanelForwardUpperPos;
227 } else if (data.IsPressed(kRocketBackwardUpper)) {
228 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800229 } else if (data.IsPressed(kCargoForward)) {
230 elevator_wrist_pos_ = kPanelCargoForwardPos;
231 } else if (data.IsPressed(kCargoBackward)) {
232 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800233 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800234 }
235
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800236 if (switch_ball_) {
237 if (kDoBallOutake ||
238 (kDoBallIntake && !superstructure_queue.status->has_piece)) {
239 new_superstructure_goal->intake.unsafe_goal = 0.959327;
240 }
Austin Schuh23a51632019-02-19 16:50:36 -0800241
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800242 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
243 elevator_wrist_pos_ = kBallIntakePos;
244 new_superstructure_goal->roller_voltage = 9.0;
Sabina Davisc6329342019-03-01 20:44:42 -0800245 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800246 } else {
247 if (kDoBallOutake) {
248 new_superstructure_goal->roller_voltage = -6.0;
249 } else {
250 new_superstructure_goal->intake.unsafe_goal = -1.2;
251 new_superstructure_goal->roller_voltage = 0.0;
252 }
253 }
Austin Schuh23a51632019-02-19 16:50:36 -0800254 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800255
256 if (data.IsPressed(kRelease)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800257 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800258 }
259
Sabina Davisc6329342019-03-01 20:44:42 -0800260 if (switch_ball_) {
261 new_superstructure_goal->suction.gamepiece_mode = 0;
262 } else {
263 new_superstructure_goal->suction.gamepiece_mode = 1;
264 }
265
266 new_superstructure_goal->suction.grab_piece = grab_piece_;
Sabina Davis91b23602019-01-21 00:06:01 -0800267
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800268 new_superstructure_goal->elevator.unsafe_goal =
269 elevator_wrist_pos_.elevator;
270 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800271
272 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
273 if (!new_superstructure_goal.Send()) {
274 LOG(ERROR, "Sending superstructure goal failed.\n");
275 }
276 }
277
278 private:
279 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800280 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Sabina Davisc6329342019-03-01 20:44:42 -0800281 bool grab_piece_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800282
283 bool switch_ball_ = false;
284 bool stilts_was_above_ = false;
Sabina Davis91b23602019-01-21 00:06:01 -0800285};
286
287} // namespace joysticks
288} // namespace input
289} // namespace y2019
290
291int main() {
292 ::aos::Init(-1);
293 ::aos::ShmEventLoop event_loop;
294 ::y2019::input::joysticks::Reader reader(&event_loop);
295 reader.Run();
296 ::aos::Cleanup();
297}