blob: bfc13e4159e343d34d006c9bd26b4a69b15ad327 [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};
Sabina Davise6fe6c52019-03-03 15:48:51 -080069const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.05, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080070
71const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
72const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
73
Sabina Davise48004f2019-03-02 23:15:24 -080074const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0};
75const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080076
Sabina Davise48004f2019-03-02 23:15:24 -080077const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0};
78const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080079
Sabina Davise6fe6c52019-03-03 15:48:51 -080080const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0};
81const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0};
82
83const ElevatorWristPosition kBallForwardLowerPos{0.46, M_PI / 2.0};
84const ElevatorWristPosition kBallBackwardLowerPos{0.15, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080085
Sabina Davise48004f2019-03-02 23:15:24 -080086const ElevatorWristPosition kBallForwardMiddlePos{1.16, 1.546};
87const ElevatorWristPosition kBallBackwardMiddlePos{0.876021, -1.546};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080088
Sabina Davise48004f2019-03-02 23:15:24 -080089const ElevatorWristPosition kBallForwardUpperPos{1.50, 0.961};
90const ElevatorWristPosition kBallBackwardUpperPos{1.41, -1.217};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080091
Sabina Davise48004f2019-03-02 23:15:24 -080092const ElevatorWristPosition kBallCargoForwardPos{0.699044, 1.353};
93const ElevatorWristPosition kBallCargoBackwardPos{0.828265, -1.999};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080094
Sabina Davise6fe6c52019-03-03 15:48:51 -080095const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097};
96const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080097
98const ElevatorWristPosition kBallIntakePos{0.29, 2.14};
Austin Schuh2cf16b82019-02-15 23:23:22 -080099
Sabina Davis91b23602019-01-21 00:06:01 -0800100class Reader : public ::aos::input::ActionJoystickInput {
101 public:
102 Reader(::aos::EventLoop *event_loop)
103 : ::aos::input::ActionJoystickInput(
104 event_loop,
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800105 ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {
106 superstructure_queue.goal.FetchLatest();
107 if (superstructure_queue.goal.get()) {
108 top_ = superstructure_queue.goal->suction.top;
109 bottom_ = superstructure_queue.goal->suction.bottom;
110 }
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)) {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800125 Ball();
Austin Schuh1a17e132019-02-17 15:05:06 -0800126 } else if (data.IsPressed(kSuctionHatch)) {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800127 Disc();
Austin Schuh1a17e132019-02-17 15:05:06 -0800128 } else if (data.IsPressed(kRelease) ||
129 !superstructure_queue.status->has_piece) {
130 top_ = false;
131 bottom_ = false;
132 }
Sabina Davis91b23602019-01-21 00:06:01 -0800133
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800134 if (data.IsPressed(kRocketBackwardUnpressed)) {
135 elevator_wrist_pos_ = kStowPos;
136 }
137 new_superstructure_goal->intake.unsafe_goal = -1.2;
138 new_superstructure_goal->roller_voltage = 0.0;
139
140 const bool kDoBallIntake = data.GetAxis(kBallIntake) > 0.9;
141 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
142
143 if (data.IsPressed(kPanelSwitch)) {
144 switch_ball_ = false;
145 } else if (data.IsPressed(kCargoSwitch)) {
146 switch_ball_ = true;
147 }
148
Sabina Davis91b23602019-01-21 00:06:01 -0800149 // TODO(sabina): max height please?
Austin Schuh77ac3212019-02-19 16:50:14 -0800150 if (data.IsPressed(kFallOver)) {
Austin Schuh1a17e132019-02-17 15:05:06 -0800151 new_superstructure_goal->stilts.unsafe_goal = 0.71;
Michael Schuh587dcb52019-02-28 21:31:03 -0800152 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800153 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Austin Schuh77ac3212019-02-19 16:50:14 -0800154 } else if (data.IsPressed(kDeployStilt)) {
155 new_superstructure_goal->stilts.unsafe_goal = 0.50;
Michael Schuh587dcb52019-02-28 21:31:03 -0800156 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800157 if (stilts_was_above_) {
158 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
159 } else {
160 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
161 }
Sabina Davis91b23602019-01-21 00:06:01 -0800162 } else {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800163 new_superstructure_goal->stilts.unsafe_goal = 0.01;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800164 new_superstructure_goal->stilts.profile_params.max_velocity = 0.25;
165 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800166 }
167
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800168 if (superstructure_queue.status->stilts.position > 0.65) {
169 stilts_was_above_ = true;
170 } else if (superstructure_queue.status->stilts.position < 0.1) {
171 stilts_was_above_ = false;
Austin Schuh2cf16b82019-02-15 23:23:22 -0800172 }
173
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800174 if (switch_ball_) {
175 if (superstructure_queue.status->has_piece) {
176 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800177 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800178
179 // Go to intake position and apply vacuum
180 if (data.IsPressed(kBallHPIntakeForward)) {
181 Ball();
182 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
183 } else if (data.IsPressed(kBallHPIntakeBackward)) {
184 Ball();
185 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
186 }
187
188 // Go to elevator/wrist position. Overrides intake position if pressed so
189 // we can re-grab the ball.
190 if (data.IsPressed(kRocketForwardLower)) {
191 elevator_wrist_pos_ = kBallForwardLowerPos;
192 } else if (data.IsPressed(kRocketBackwardLower)) {
193 elevator_wrist_pos_ = kBallBackwardLowerPos;
194 } else if (data.IsPressed(kRocketForwardMiddle)) {
195 elevator_wrist_pos_ = kBallForwardMiddlePos;
196 } else if (data.IsPressed(kRocketBackwardMiddle)) {
197 elevator_wrist_pos_ = kBallBackwardMiddlePos;
198 } else if (data.IsPressed(kRocketForwardUpper)) {
199 elevator_wrist_pos_ = kBallForwardUpperPos;
200 } else if (data.IsPressed(kRocketBackwardUpper)) {
201 elevator_wrist_pos_ = kBallBackwardUpperPos;
202 } else if (data.IsPressed(kCargoForward)) {
203 elevator_wrist_pos_ = kBallCargoForwardPos;
204 } else if (data.IsPressed(kCargoBackward)) {
205 elevator_wrist_pos_ = kBallCargoBackwardPos;
206 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800207 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800208 if (data.IsPressed(kPanelHPIntakeForward)) {
209 Disc();
210 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
211 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
212 Disc();
213 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
214 }
215
216 // Go to elevator/wrist position. Overrides intake position if pressed so
217 // we can re-grab the panel.
218 if (data.IsPressed(kRocketForwardLower)) {
219 elevator_wrist_pos_ = kPanelForwardLowerPos;
220 } else if (data.IsPressed(kRocketBackwardLower)) {
221 elevator_wrist_pos_ = kPanelBackwardLowerPos;
222 } else if (data.IsPressed(kRocketForwardMiddle)) {
223 elevator_wrist_pos_ = kPanelForwardMiddlePos;
224 } else if (data.IsPressed(kRocketBackwardMiddle)) {
225 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
226 } else if (data.IsPressed(kRocketForwardUpper)) {
227 elevator_wrist_pos_ = kPanelForwardUpperPos;
228 } else if (data.IsPressed(kRocketBackwardUpper)) {
229 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800230 } else if (data.IsPressed(kCargoForward)) {
231 elevator_wrist_pos_ = kPanelCargoForwardPos;
232 } else if (data.IsPressed(kCargoBackward)) {
233 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800234 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800235 }
236
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800237 if (switch_ball_) {
238 if (kDoBallOutake ||
239 (kDoBallIntake && !superstructure_queue.status->has_piece)) {
240 new_superstructure_goal->intake.unsafe_goal = 0.959327;
241 }
Austin Schuh23a51632019-02-19 16:50:36 -0800242
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800243 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
244 elevator_wrist_pos_ = kBallIntakePos;
245 new_superstructure_goal->roller_voltage = 9.0;
246 Ball();
247 } else {
248 if (kDoBallOutake) {
249 new_superstructure_goal->roller_voltage = -6.0;
250 } else {
251 new_superstructure_goal->intake.unsafe_goal = -1.2;
252 new_superstructure_goal->roller_voltage = 0.0;
253 }
254 }
Austin Schuh23a51632019-02-19 16:50:36 -0800255 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800256
257 if (data.IsPressed(kRelease)) {
258 top_ = false;
259 bottom_ = false;
260 }
261
262 new_superstructure_goal->suction.top = top_;
263 new_superstructure_goal->suction.bottom = bottom_;
Sabina Davis91b23602019-01-21 00:06:01 -0800264
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800265 new_superstructure_goal->elevator.unsafe_goal =
266 elevator_wrist_pos_.elevator;
267 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800268
269 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
270 if (!new_superstructure_goal.Send()) {
271 LOG(ERROR, "Sending superstructure goal failed.\n");
272 }
273 }
274
Austin Schuh1a17e132019-02-17 15:05:06 -0800275 void Disc() {
276 top_ = true;
277 bottom_ = true;
278 }
279 void Ball() {
280 top_ = false;
281 bottom_ = true;
282 }
283
Sabina Davis91b23602019-01-21 00:06:01 -0800284 private:
285 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800286 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Austin Schuh1a17e132019-02-17 15:05:06 -0800287 bool top_ = false;
288 bool bottom_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800289
290 bool switch_ball_ = false;
291 bool stilts_was_above_ = false;
Sabina Davis91b23602019-01-21 00:06:01 -0800292};
293
294} // namespace joysticks
295} // namespace input
296} // namespace y2019
297
298int main() {
299 ::aos::Init(-1);
300 ::aos::ShmEventLoop event_loop;
301 ::y2019::input::joysticks::Reader reader(&event_loop);
302 reader.Run();
303 ::aos::Cleanup();
304}