blob: 310f41f6f5848e8a81e399370f3f6025ddfb2cb5 [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
32// TODO(sabina): update button locations when the board is done
Austin Schuh2cf16b82019-02-15 23:23:22 -080033const ButtonLocation kIntakeOut(3, 3);
Sabina Davis91b23602019-01-21 00:06:01 -080034const ButtonLocation kElevatorDown(0, 0);
Austin Schuh1a17e132019-02-17 15:05:06 -080035const ButtonLocation kElevatorFront1(4, 1);
36const ButtonLocation kElevatorFront2(4, 11);
37const ButtonLocation kElevatorFront3(4, 9);
38const ButtonLocation kElevatorFront4(4, 7);
39const ButtonLocation kElevatorFront5(4, 5);
40
41const ButtonLocation kElevatorBack1(3, 14);
42const ButtonLocation kElevatorBack2(4, 12);
43const ButtonLocation kElevatorBack3(4, 10);
44const ButtonLocation kElevatorBack4(4, 8);
45const ButtonLocation kElevatorBack5(4, 6);
46
47const ButtonLocation kElevatorIntaking(3, 4);
Austin Schuh23a51632019-02-19 16:50:36 -080048const ButtonLocation kElevatorOuttake(3, 6);
Austin Schuh1a17e132019-02-17 15:05:06 -080049const ButtonLocation kRelease(4, 4);
50
51const ButtonLocation kSuctionBall(3, 13);
52const ButtonLocation kSuctionHatch(3, 12);
53const ButtonLocation kDeployStilt(3, 8);
54const ButtonLocation kFallOver(3, 9);
Austin Schuh2cf16b82019-02-15 23:23:22 -080055
Sabina Davis91b23602019-01-21 00:06:01 -080056const ButtonLocation kDiskLoad(0, 0);
57const ButtonLocation kDiskRocketMiddle(0, 0);
58const ButtonLocation kDiskRocketTop(0, 0);
59const ButtonLocation kCargoLoad(0, 0);
60const ButtonLocation kCargoBay(0, 0);
61const ButtonLocation kCargoRocketBase(0, 0);
62const ButtonLocation kCargoRocketMiddle(0, 0);
63const ButtonLocation kCargoRocketTop(0, 0);
64const ButtonLocation kStow(0, 0);
65const ButtonLocation kIntakeExtend(0, 0);
66const ButtonLocation kIntake(0, 0);
67const ButtonLocation kSpit(0, 0);
68const ButtonLocation kCargoSuction(0, 0);
69const ButtonLocation kDiskSuction(0, 0);
70const ButtonLocation kSuctionOut(0, 0);
Sabina Davis91b23602019-01-21 00:06:01 -080071const ButtonLocation kRetractStilt(0, 0);
72const ButtonLocation kBackwards(0, 0);
73
Austin Schuh1a17e132019-02-17 15:05:06 -080074const ButtonLocation kWristBackwards(3, 10);
75const ButtonLocation kWristForwards(3, 7);
Austin Schuh2cf16b82019-02-15 23:23:22 -080076
Sabina Davis91b23602019-01-21 00:06:01 -080077class Reader : public ::aos::input::ActionJoystickInput {
78 public:
79 Reader(::aos::EventLoop *event_loop)
80 : ::aos::input::ActionJoystickInput(
81 event_loop,
82 ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {}
83
84 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
85 superstructure_queue.position.FetchLatest();
86 superstructure_queue.status.FetchLatest();
87 if (!superstructure_queue.status.get() ||
88 !superstructure_queue.position.get()) {
89 LOG(ERROR, "Got no superstructure status packet.\n");
90 return;
91 }
92
93 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
94
Austin Schuh2cf16b82019-02-15 23:23:22 -080095 /*
Sabina Davis91b23602019-01-21 00:06:01 -080096 if (data.IsPressed(kElevatorUp)) {
97 elevator_height_ += 0.1;
98 } else if (data.IsPressed(kElevatorDown)) {
99 elevator_height_ -= 0.1;
100 } else if (data.IsPressed(kDiskLoad)) {
101 elevator_height_ = 0.48;
102 wrist_angle_ = M_PI;
103 } else if (data.IsPressed(kDiskRocketMiddle)) {
104 elevator_height_ = 1.19;
105 wrist_angle_ = M_PI;
106 } else if (data.IsPressed(kDiskRocketTop)) {
107 elevator_height_ = 1.90;
108 wrist_angle_ = M_PI;
109 }
110
111 // TODO(sabina): do we need an angle here?
112 else if (data.IsPressed(kCargoLoad)) {
113 elevator_height_ = 1.12;
114 wrist_angle_ = M_PI;
115 } else if (data.IsPressed(kCargoBay)) {
116 elevator_height_ = 0.0;
117 wrist_angle_ = M_PI / 3;
118 } else if (data.IsPressed(kCargoRocketBase)) {
119 elevator_height_ = 0.7;
120 wrist_angle_ = M_PI;
121 } else if (data.IsPressed(kCargoRocketMiddle)) {
122 elevator_height_ = 1.41;
123 wrist_angle_ = M_PI;
124 } else if (data.IsPressed(kCargoRocketTop)) {
125 elevator_height_ = 2.12;
126 wrist_angle_ = M_PI;
127 } else if (data.IsPressed(kStow)) {
128 elevator_height_ = 0.5;
129 wrist_angle_ = 0.0;
130 } else {
131 }
Austin Schuh2cf16b82019-02-15 23:23:22 -0800132 */
Sabina Davis91b23602019-01-21 00:06:01 -0800133
Austin Schuh2cf16b82019-02-15 23:23:22 -0800134 /*
Sabina Davis91b23602019-01-21 00:06:01 -0800135 // TODO(sabina): get accurate angle.
136 if (data.IsPressed(kIntakeExtend)) {
Theo Bafrali00e42272019-02-12 01:07:46 -0800137 new_superstructure_goal->intake.unsafe_goal = 0.5;
Sabina Davis91b23602019-01-21 00:06:01 -0800138 } else {
Theo Bafrali00e42272019-02-12 01:07:46 -0800139 new_superstructure_goal->intake.unsafe_goal = 0.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800140 }
141
142 if (data.IsPressed(kIntake)) {
143 new_superstructure_goal->suction.bottom = true;
144 if (superstructure_queue.status->has_piece == false) {
Theo Bafrali00e42272019-02-12 01:07:46 -0800145 new_superstructure_goal->roller_voltage = 12.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800146 } else {
Theo Bafrali00e42272019-02-12 01:07:46 -0800147 new_superstructure_goal->roller_voltage = 0.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800148 }
149 } else if (data.IsPressed(kSpit)) {
150 new_superstructure_goal->suction.bottom = false;
151 if (superstructure_queue.status->has_piece == false) {
Theo Bafrali00e42272019-02-12 01:07:46 -0800152 new_superstructure_goal->roller_voltage = 12.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800153 } else {
Theo Bafrali00e42272019-02-12 01:07:46 -0800154 new_superstructure_goal->roller_voltage = 0.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800155 }
156 } else {
Theo Bafrali00e42272019-02-12 01:07:46 -0800157 new_superstructure_goal->roller_voltage = 0.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800158 }
Austin Schuh2cf16b82019-02-15 23:23:22 -0800159 */
Sabina Davis91b23602019-01-21 00:06:01 -0800160
161 // TODO(sabina): decide if we should really have disk suction as its own
162 // button
Austin Schuh1a17e132019-02-17 15:05:06 -0800163 /*
Sabina Davis91b23602019-01-21 00:06:01 -0800164 if (data.IsPressed(kCargoSuction)) {
165 new_superstructure_goal->suction.top = false;
166 new_superstructure_goal->suction.bottom = true;
167 } else if (data.IsPressed(kDiskSuction)) {
168 new_superstructure_goal->suction.top = true;
169 new_superstructure_goal->suction.bottom = true;
170 } else if (data.IsPressed(kSuctionOut)) {
171 new_superstructure_goal->suction.top = true;
172 new_superstructure_goal->suction.bottom = true;
Sabina Davis91b23602019-01-21 00:06:01 -0800173 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800174 */
175
176 if (data.IsPressed(kSuctionBall)) {
177 top_ = false;
178 bottom_ = true;
179 } else if (data.IsPressed(kSuctionHatch)) {
180 top_ = true;
181 bottom_ = true;
182 } else if (data.IsPressed(kRelease) ||
183 !superstructure_queue.status->has_piece) {
184 top_ = false;
185 bottom_ = false;
186 }
Sabina Davis91b23602019-01-21 00:06:01 -0800187
188 // TODO(sabina): max height please?
Austin Schuh77ac3212019-02-19 16:50:14 -0800189 if (data.IsPressed(kFallOver)) {
Austin Schuh1a17e132019-02-17 15:05:06 -0800190 new_superstructure_goal->stilts.unsafe_goal = 0.71;
Austin Schuh08bd22d2019-02-22 20:48:20 -0800191 new_superstructure_goal->stilts.profile_params.max_velocity = 0.45;
192 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.5;
Austin Schuh77ac3212019-02-19 16:50:14 -0800193 } else if (data.IsPressed(kDeployStilt)) {
194 new_superstructure_goal->stilts.unsafe_goal = 0.50;
Austin Schuh08bd22d2019-02-22 20:48:20 -0800195 new_superstructure_goal->stilts.profile_params.max_velocity = 0.45;
196 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.5;
Sabina Davis91b23602019-01-21 00:06:01 -0800197 } else {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800198 new_superstructure_goal->stilts.unsafe_goal = 0.01;
Sabina Davis91b23602019-01-21 00:06:01 -0800199 }
200
Austin Schuh1a17e132019-02-17 15:05:06 -0800201 if (data.IsPressed(kElevatorFront1)) {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800202 elevator_height_ = 1.5;
Austin Schuh1a17e132019-02-17 15:05:06 -0800203 } else if (data.IsPressed(kElevatorFront2)) {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800204 elevator_height_ = 1.2;
Austin Schuh1a17e132019-02-17 15:05:06 -0800205 } else if (data.IsPressed(kElevatorFront3)) {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800206 elevator_height_ = 0.8;
Austin Schuh1a17e132019-02-17 15:05:06 -0800207 } else if (data.IsPressed(kElevatorFront4)) {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800208 elevator_height_ = 0.3;
Austin Schuh1a17e132019-02-17 15:05:06 -0800209 } else if (data.IsPressed(kElevatorFront5)) {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800210 elevator_height_ = 0.01;
211 }
212
Austin Schuh1a17e132019-02-17 15:05:06 -0800213 /*
Austin Schuh2cf16b82019-02-15 23:23:22 -0800214 if (data.IsPressed(kWristDown)) {
215 wrist_angle_ = -M_PI / 3.0;
216 } else {
217 wrist_angle_ = M_PI / 3.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800218 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800219 */
220 if (data.IsPressed(kWristBackwards)) {
221 // Hatch pannel back
222 elevator_height_ = 0.03;
223 wrist_angle_ = -M_PI / 2.0;
224 Disc();
225 } else if (data.IsPressed(kWristForwards)) {
226 // Hatch pannel front
227 elevator_height_ = 0.03;
228 wrist_angle_ = M_PI / 2.0;
229 Disc();
230 } else if (data.IsPressed(kElevatorFront5)) {
231 // Ball front
232 Ball();
233 elevator_height_ = 0.52;
234 wrist_angle_ = 1.1;
235 } else if (data.IsPressed(kElevatorBack5)) {
236 // Ball back
237 elevator_height_ = 0.52;
238 wrist_angle_ = -1.1;
239 } else if (data.IsPressed(kElevatorFront2)) {
240 elevator_height_ = 1.5;
241 wrist_angle_ = 0.0;
242 } else {
243 wrist_angle_ = 0.0;
244 elevator_height_ = 0.36;
245 }
Austin Schuh23a51632019-02-19 16:50:36 -0800246
247 if (data.IsPressed(kElevatorOuttake) ||
248 (data.IsPressed(kIntakeOut) &&
249 !superstructure_queue.status->has_piece)) {
250 new_superstructure_goal->intake.unsafe_goal = 0.959327;
251 }
252
Austin Schuh1a17e132019-02-17 15:05:06 -0800253 if (data.IsPressed(kIntakeOut) && !superstructure_queue.status->has_piece) {
254 elevator_height_ = 0.29;
255 wrist_angle_ = 2.14;
Austin Schuh1a17e132019-02-17 15:05:06 -0800256 if (data.IsPressed(kElevatorIntaking)) {
Austin Schuh23a51632019-02-19 16:50:36 -0800257 new_superstructure_goal->roller_voltage = 9.0;
Austin Schuh1a17e132019-02-17 15:05:06 -0800258 } else {
259 new_superstructure_goal->roller_voltage = 0.0;
260 }
261 Ball();
262 } else {
263 new_superstructure_goal->intake.unsafe_goal = -1.2;
264 new_superstructure_goal->roller_voltage = 0.0;
265 }
266
Austin Schuh23a51632019-02-19 16:50:36 -0800267 if (data.IsPressed(kElevatorOuttake)) {
268 new_superstructure_goal->roller_voltage = -6.0;
269 }
270
271 if (data.IsPressed(kElevatorBack1)) {
272 wrist_angle_ = -2.451824;
273 elevator_height_ = 0.430478;
274 //new_superstructure_goal->wrist.profile_params.max_velocity = 2.0;
275 //new_superstructure_goal->wrist.profile_params.max_acceleration = 20.0;
276 }
277 if (data.IsPressed(kElevatorBack2)) {
278 wrist_angle_ = -2.400;
279 elevator_height_ = 0.364108;
280 new_superstructure_goal->elevator.profile_params.max_velocity = 2.0;
281 new_superstructure_goal->elevator.profile_params.max_acceleration = 5.0;
282 new_superstructure_goal->wrist.profile_params.max_velocity = 0.35;
283 new_superstructure_goal->wrist.profile_params.max_acceleration = 10.0;
284 }
285 if (data.IsPressed(kElevatorBack3)) {
286 wrist_angle_ = -2.211173;
287 elevator_height_ = 0.25;
288 new_superstructure_goal->wrist.profile_params.max_velocity = 2.0;
289 new_superstructure_goal->wrist.profile_params.max_acceleration = 10.0;
Austin Schuh1a17e132019-02-17 15:05:06 -0800290 }
291
292
293 if (data.IsPressed(kRelease)) {
294 top_ = false;
295 bottom_ = false;
296 }
297
298 new_superstructure_goal->suction.top = top_;
299 new_superstructure_goal->suction.bottom = bottom_;
Sabina Davis91b23602019-01-21 00:06:01 -0800300
Theo Bafrali00e42272019-02-12 01:07:46 -0800301 new_superstructure_goal->elevator.unsafe_goal = elevator_height_;
302 new_superstructure_goal->wrist.unsafe_goal = wrist_angle_;
Sabina Davis91b23602019-01-21 00:06:01 -0800303
304 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
305 if (!new_superstructure_goal.Send()) {
306 LOG(ERROR, "Sending superstructure goal failed.\n");
307 }
308 }
309
Austin Schuh1a17e132019-02-17 15:05:06 -0800310 void Disc() {
311 top_ = true;
312 bottom_ = true;
313 }
314 void Ball() {
315 top_ = false;
316 bottom_ = true;
317 }
318
Sabina Davis91b23602019-01-21 00:06:01 -0800319 private:
320 // Current goals here.
321 double elevator_height_ = 0.0;
322 double wrist_angle_ = 0.0;
Austin Schuh1a17e132019-02-17 15:05:06 -0800323
324 bool top_ = false;
325 bool bottom_ = false;
Sabina Davis91b23602019-01-21 00:06:01 -0800326};
327
328} // namespace joysticks
329} // namespace input
330} // namespace y2019
331
332int main() {
333 ::aos::Init(-1);
334 ::aos::ShmEventLoop event_loop;
335 ::y2019::input::joysticks::Reader reader(&event_loop);
336 reader.Run();
337 ::aos::Cleanup();
338}