blob: 746feab82f0bd9377954449700d437e882c3bec9 [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"
21
22using ::y2019::control_loops::superstructure::superstructure_queue;
23using ::aos::input::driver_station::ButtonLocation;
24using ::aos::input::driver_station::ControlBit;
25using ::aos::input::driver_station::JoystickAxis;
26using ::aos::input::driver_station::POVLocation;
27
28namespace y2019 {
29namespace input {
30namespace joysticks {
31
Austin Schuh1a17e132019-02-17 15:05:06 -080032const ButtonLocation kSuctionBall(3, 13);
33const ButtonLocation kSuctionHatch(3, 12);
34const ButtonLocation kDeployStilt(3, 8);
35const ButtonLocation kFallOver(3, 9);
Austin Schuh2cf16b82019-02-15 23:23:22 -080036
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080037struct ElevatorWristPosition {
38 double elevator;
39 double wrist;
40};
Sabina Davis91b23602019-01-21 00:06:01 -080041
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080042const ButtonLocation kRocketForwardLower(5, 1);
43const ButtonLocation kRocketForwardMiddle(5, 2);
44const ButtonLocation kRocketForwardUpper(5, 4);
45const ButtonLocation kCargoForward(5, 3);
46
47const POVLocation kRocketBackwardUnpressed(5, -1);
48const POVLocation kRocketBackwardLower(5, 180);
49const POVLocation kRocketBackwardMiddle(5, 90);
50const POVLocation kRocketBackwardUpper(5, 0);
51const POVLocation kCargoBackward(5, 270);
52
53const ButtonLocation kPanelSwitch(5, 7);
54const ButtonLocation kCargoSwitch(5, 8);
55
56const ButtonLocation kBallHPIntakeForward(5, 6);
57const ButtonLocation kBallHPIntakeBackward(5, 5);
58const JoystickAxis kBallOutake(5, 3);
59const JoystickAxis kBallIntake(5, 4);
60
61const ButtonLocation kPanelHPIntakeForward(5, 6);
62const ButtonLocation kPanelHPIntakeBackward(5, 5);
63
64const ButtonLocation kRelease(2, 4);
65
66const ElevatorWristPosition kStowPos{0.36, 0.0};
67
68const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.04, M_PI / 2.0};
69const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.04, -M_PI / 2.0};
70
71const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
72const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
73
74const ElevatorWristPosition kPanelForwardMiddlePos{0.7412, M_PI / 2.0};
75const ElevatorWristPosition kPanelBackwardMiddlePos{0.7412, -M_PI / 2.0};
76
77const ElevatorWristPosition kPanelForwardUpperPos{1.4524, M_PI / 2.0};
78const ElevatorWristPosition kPanelBackwardUpperPos{1.4524, -M_PI / 2.0};
79
80const ElevatorWristPosition kBallForwardLowerPos{0.0, 0.98};
81const ElevatorWristPosition kBallBackwardLowerPos{0.607, -2.281};
82
83const ElevatorWristPosition kBallForwardMiddlePos{0.67945, 1.22};
84const ElevatorWristPosition kBallBackwardMiddlePos{0.93345, -1.83};
85
86const ElevatorWristPosition kBallForwardUpperPos{1.41605, 1.22};
87const ElevatorWristPosition kBallBackwardUpperPos{1.41605, -1.36};
88
89const ElevatorWristPosition kBallCargoForwardPos{0.73025, M_PI / 2};
90const ElevatorWristPosition kBallCargoBackwardPos{0.80645, -1.92};
91
92const ElevatorWristPosition kBallHPIntakeForwardPos{0.340, 0.737};
93const ElevatorWristPosition kBallHPIntakeBackwardPos{0.52, -1.1};
94
95const ElevatorWristPosition kBallIntakePos{0.29, 2.14};
Austin Schuh2cf16b82019-02-15 23:23:22 -080096
Sabina Davis91b23602019-01-21 00:06:01 -080097class Reader : public ::aos::input::ActionJoystickInput {
98 public:
99 Reader(::aos::EventLoop *event_loop)
100 : ::aos::input::ActionJoystickInput(
101 event_loop,
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800102 ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {
103 superstructure_queue.goal.FetchLatest();
104 if (superstructure_queue.goal.get()) {
105 top_ = superstructure_queue.goal->suction.top;
106 bottom_ = superstructure_queue.goal->suction.bottom;
107 }
108 }
Sabina Davis91b23602019-01-21 00:06:01 -0800109
110 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
111 superstructure_queue.position.FetchLatest();
112 superstructure_queue.status.FetchLatest();
113 if (!superstructure_queue.status.get() ||
114 !superstructure_queue.position.get()) {
115 LOG(ERROR, "Got no superstructure status packet.\n");
116 return;
117 }
118
119 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
120
Austin Schuh1a17e132019-02-17 15:05:06 -0800121 if (data.IsPressed(kSuctionBall)) {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800122 Ball();
Austin Schuh1a17e132019-02-17 15:05:06 -0800123 } else if (data.IsPressed(kSuctionHatch)) {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800124 Disc();
Austin Schuh1a17e132019-02-17 15:05:06 -0800125 } else if (data.IsPressed(kRelease) ||
126 !superstructure_queue.status->has_piece) {
127 top_ = false;
128 bottom_ = false;
129 }
Sabina Davis91b23602019-01-21 00:06:01 -0800130
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800131 if (data.IsPressed(kRocketBackwardUnpressed)) {
132 elevator_wrist_pos_ = kStowPos;
133 }
134 new_superstructure_goal->intake.unsafe_goal = -1.2;
135 new_superstructure_goal->roller_voltage = 0.0;
136
137 const bool kDoBallIntake = data.GetAxis(kBallIntake) > 0.9;
138 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
139
140 if (data.IsPressed(kPanelSwitch)) {
141 switch_ball_ = false;
142 } else if (data.IsPressed(kCargoSwitch)) {
143 switch_ball_ = true;
144 }
145
Sabina Davis91b23602019-01-21 00:06:01 -0800146 // TODO(sabina): max height please?
Austin Schuh77ac3212019-02-19 16:50:14 -0800147 if (data.IsPressed(kFallOver)) {
Austin Schuh1a17e132019-02-17 15:05:06 -0800148 new_superstructure_goal->stilts.unsafe_goal = 0.71;
Austin Schuh08bd22d2019-02-22 20:48:20 -0800149 new_superstructure_goal->stilts.profile_params.max_velocity = 0.45;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800150 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Austin Schuh77ac3212019-02-19 16:50:14 -0800151 } else if (data.IsPressed(kDeployStilt)) {
152 new_superstructure_goal->stilts.unsafe_goal = 0.50;
Austin Schuh08bd22d2019-02-22 20:48:20 -0800153 new_superstructure_goal->stilts.profile_params.max_velocity = 0.45;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800154 if (stilts_was_above_) {
155 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
156 } else {
157 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
158 }
Sabina Davis91b23602019-01-21 00:06:01 -0800159 } else {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800160 new_superstructure_goal->stilts.unsafe_goal = 0.01;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800161 new_superstructure_goal->stilts.profile_params.max_velocity = 0.25;
162 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800163 }
164
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800165 if (superstructure_queue.status->stilts.position > 0.65) {
166 stilts_was_above_ = true;
167 } else if (superstructure_queue.status->stilts.position < 0.1) {
168 stilts_was_above_ = false;
Austin Schuh2cf16b82019-02-15 23:23:22 -0800169 }
170
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800171 if (switch_ball_) {
172 if (superstructure_queue.status->has_piece) {
173 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800174 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800175
176 // Go to intake position and apply vacuum
177 if (data.IsPressed(kBallHPIntakeForward)) {
178 Ball();
179 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
180 } else if (data.IsPressed(kBallHPIntakeBackward)) {
181 Ball();
182 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
183 }
184
185 // Go to elevator/wrist position. Overrides intake position if pressed so
186 // we can re-grab the ball.
187 if (data.IsPressed(kRocketForwardLower)) {
188 elevator_wrist_pos_ = kBallForwardLowerPos;
189 } else if (data.IsPressed(kRocketBackwardLower)) {
190 elevator_wrist_pos_ = kBallBackwardLowerPos;
191 } else if (data.IsPressed(kRocketForwardMiddle)) {
192 elevator_wrist_pos_ = kBallForwardMiddlePos;
193 } else if (data.IsPressed(kRocketBackwardMiddle)) {
194 elevator_wrist_pos_ = kBallBackwardMiddlePos;
195 } else if (data.IsPressed(kRocketForwardUpper)) {
196 elevator_wrist_pos_ = kBallForwardUpperPos;
197 } else if (data.IsPressed(kRocketBackwardUpper)) {
198 elevator_wrist_pos_ = kBallBackwardUpperPos;
199 } else if (data.IsPressed(kCargoForward)) {
200 elevator_wrist_pos_ = kBallCargoForwardPos;
201 } else if (data.IsPressed(kCargoBackward)) {
202 elevator_wrist_pos_ = kBallCargoBackwardPos;
203 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800204 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800205 if (data.IsPressed(kPanelHPIntakeForward)) {
206 Disc();
207 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
208 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
209 Disc();
210 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
211 }
212
213 // Go to elevator/wrist position. Overrides intake position if pressed so
214 // we can re-grab the panel.
215 if (data.IsPressed(kRocketForwardLower)) {
216 elevator_wrist_pos_ = kPanelForwardLowerPos;
217 } else if (data.IsPressed(kRocketBackwardLower)) {
218 elevator_wrist_pos_ = kPanelBackwardLowerPos;
219 } else if (data.IsPressed(kRocketForwardMiddle)) {
220 elevator_wrist_pos_ = kPanelForwardMiddlePos;
221 } else if (data.IsPressed(kRocketBackwardMiddle)) {
222 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
223 } else if (data.IsPressed(kRocketForwardUpper)) {
224 elevator_wrist_pos_ = kPanelForwardUpperPos;
225 } else if (data.IsPressed(kRocketBackwardUpper)) {
226 elevator_wrist_pos_ = kPanelBackwardUpperPos;
227 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800228 }
229
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800230 if (switch_ball_) {
231 if (kDoBallOutake ||
232 (kDoBallIntake && !superstructure_queue.status->has_piece)) {
233 new_superstructure_goal->intake.unsafe_goal = 0.959327;
234 }
Austin Schuh23a51632019-02-19 16:50:36 -0800235
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800236 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
237 elevator_wrist_pos_ = kBallIntakePos;
238 new_superstructure_goal->roller_voltage = 9.0;
239 Ball();
240 } else {
241 if (kDoBallOutake) {
242 new_superstructure_goal->roller_voltage = -6.0;
243 } else {
244 new_superstructure_goal->intake.unsafe_goal = -1.2;
245 new_superstructure_goal->roller_voltage = 0.0;
246 }
247 }
Austin Schuh23a51632019-02-19 16:50:36 -0800248 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800249
250 if (data.IsPressed(kRelease)) {
251 top_ = false;
252 bottom_ = false;
253 }
254
255 new_superstructure_goal->suction.top = top_;
256 new_superstructure_goal->suction.bottom = bottom_;
Sabina Davis91b23602019-01-21 00:06:01 -0800257
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800258 new_superstructure_goal->elevator.unsafe_goal =
259 elevator_wrist_pos_.elevator;
260 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800261
262 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
263 if (!new_superstructure_goal.Send()) {
264 LOG(ERROR, "Sending superstructure goal failed.\n");
265 }
266 }
267
Austin Schuh1a17e132019-02-17 15:05:06 -0800268 void Disc() {
269 top_ = true;
270 bottom_ = true;
271 }
272 void Ball() {
273 top_ = false;
274 bottom_ = true;
275 }
276
Sabina Davis91b23602019-01-21 00:06:01 -0800277 private:
278 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800279 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Austin Schuh1a17e132019-02-17 15:05:06 -0800280 bool top_ = false;
281 bool bottom_ = 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}