blob: 17cc45f72440f1c2c92feeefe22c79a59995f8a8 [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);
48const ButtonLocation kElevatorIntakingUp(3, 6);
49const 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?
189 if (data.IsPressed(kDeployStilt)) {
Austin Schuh1a17e132019-02-17 15:05:06 -0800190 new_superstructure_goal->stilts.unsafe_goal = 0.50;
191 } else if (data.IsPressed(kFallOver)) {
192 new_superstructure_goal->stilts.unsafe_goal = 0.71;
Sabina Davis91b23602019-01-21 00:06:01 -0800193 } else {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800194 new_superstructure_goal->stilts.unsafe_goal = 0.01;
Sabina Davis91b23602019-01-21 00:06:01 -0800195 }
196
Austin Schuh1a17e132019-02-17 15:05:06 -0800197 if (data.IsPressed(kElevatorFront1)) {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800198 elevator_height_ = 1.5;
Austin Schuh1a17e132019-02-17 15:05:06 -0800199 } else if (data.IsPressed(kElevatorFront2)) {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800200 elevator_height_ = 1.2;
Austin Schuh1a17e132019-02-17 15:05:06 -0800201 } else if (data.IsPressed(kElevatorFront3)) {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800202 elevator_height_ = 0.8;
Austin Schuh1a17e132019-02-17 15:05:06 -0800203 } else if (data.IsPressed(kElevatorFront4)) {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800204 elevator_height_ = 0.3;
Austin Schuh1a17e132019-02-17 15:05:06 -0800205 } else if (data.IsPressed(kElevatorFront5)) {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800206 elevator_height_ = 0.01;
207 }
208
Austin Schuh1a17e132019-02-17 15:05:06 -0800209 /*
Austin Schuh2cf16b82019-02-15 23:23:22 -0800210 if (data.IsPressed(kWristDown)) {
211 wrist_angle_ = -M_PI / 3.0;
212 } else {
213 wrist_angle_ = M_PI / 3.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800214 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800215 */
216 if (data.IsPressed(kWristBackwards)) {
217 // Hatch pannel back
218 elevator_height_ = 0.03;
219 wrist_angle_ = -M_PI / 2.0;
220 Disc();
221 } else if (data.IsPressed(kWristForwards)) {
222 // Hatch pannel front
223 elevator_height_ = 0.03;
224 wrist_angle_ = M_PI / 2.0;
225 Disc();
226 } else if (data.IsPressed(kElevatorFront5)) {
227 // Ball front
228 Ball();
229 elevator_height_ = 0.52;
230 wrist_angle_ = 1.1;
231 } else if (data.IsPressed(kElevatorBack5)) {
232 // Ball back
233 elevator_height_ = 0.52;
234 wrist_angle_ = -1.1;
235 } else if (data.IsPressed(kElevatorFront2)) {
236 elevator_height_ = 1.5;
237 wrist_angle_ = 0.0;
238 } else {
239 wrist_angle_ = 0.0;
240 elevator_height_ = 0.36;
241 }
242 //if (data.IsPressed(kElevatorIntaking)) {
243 //}
244 if (data.IsPressed(kIntakeOut) && !superstructure_queue.status->has_piece) {
245 elevator_height_ = 0.29;
246 wrist_angle_ = 2.14;
247 new_superstructure_goal->intake.unsafe_goal = 0.52;
248 if (data.IsPressed(kElevatorIntaking)) {
249 new_superstructure_goal->roller_voltage = 6.0;
250 } else {
251 new_superstructure_goal->roller_voltage = 0.0;
252 }
253 Ball();
254 } else {
255 new_superstructure_goal->intake.unsafe_goal = -1.2;
256 new_superstructure_goal->roller_voltage = 0.0;
257 }
258
259 if (data.IsPressed(kElevatorIntakingUp)) {
260 elevator_height_ = 0.29 + 0.3;
261 wrist_angle_ = 2.14;
262 }
263
264
265 if (data.IsPressed(kRelease)) {
266 top_ = false;
267 bottom_ = false;
268 }
269
270 new_superstructure_goal->suction.top = top_;
271 new_superstructure_goal->suction.bottom = bottom_;
Sabina Davis91b23602019-01-21 00:06:01 -0800272
Theo Bafrali00e42272019-02-12 01:07:46 -0800273 new_superstructure_goal->elevator.unsafe_goal = elevator_height_;
274 new_superstructure_goal->wrist.unsafe_goal = wrist_angle_;
Sabina Davis91b23602019-01-21 00:06:01 -0800275
276 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
277 if (!new_superstructure_goal.Send()) {
278 LOG(ERROR, "Sending superstructure goal failed.\n");
279 }
280 }
281
Austin Schuh1a17e132019-02-17 15:05:06 -0800282 void Disc() {
283 top_ = true;
284 bottom_ = true;
285 }
286 void Ball() {
287 top_ = false;
288 bottom_ = true;
289 }
290
Sabina Davis91b23602019-01-21 00:06:01 -0800291 private:
292 // Current goals here.
293 double elevator_height_ = 0.0;
294 double wrist_angle_ = 0.0;
Austin Schuh1a17e132019-02-17 15:05:06 -0800295
296 bool top_ = false;
297 bool bottom_ = false;
Sabina Davis91b23602019-01-21 00:06:01 -0800298};
299
300} // namespace joysticks
301} // namespace input
302} // namespace y2019
303
304int main() {
305 ::aos::Init(-1);
306 ::aos::ShmEventLoop event_loop;
307 ::y2019::input::joysticks::Reader reader(&event_loop);
308 reader.Run();
309 ::aos::Cleanup();
310}