blob: 57eb4a2e3e4949e3b7d387a01531f91f4fcf6abd [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?
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 }
246 //if (data.IsPressed(kElevatorIntaking)) {
247 //}
248 if (data.IsPressed(kIntakeOut) && !superstructure_queue.status->has_piece) {
249 elevator_height_ = 0.29;
250 wrist_angle_ = 2.14;
251 new_superstructure_goal->intake.unsafe_goal = 0.52;
252 if (data.IsPressed(kElevatorIntaking)) {
253 new_superstructure_goal->roller_voltage = 6.0;
254 } else {
255 new_superstructure_goal->roller_voltage = 0.0;
256 }
257 Ball();
258 } else {
259 new_superstructure_goal->intake.unsafe_goal = -1.2;
260 new_superstructure_goal->roller_voltage = 0.0;
261 }
262
263 if (data.IsPressed(kElevatorIntakingUp)) {
264 elevator_height_ = 0.29 + 0.3;
265 wrist_angle_ = 2.14;
266 }
267
268
269 if (data.IsPressed(kRelease)) {
270 top_ = false;
271 bottom_ = false;
272 }
273
274 new_superstructure_goal->suction.top = top_;
275 new_superstructure_goal->suction.bottom = bottom_;
Sabina Davis91b23602019-01-21 00:06:01 -0800276
Theo Bafrali00e42272019-02-12 01:07:46 -0800277 new_superstructure_goal->elevator.unsafe_goal = elevator_height_;
278 new_superstructure_goal->wrist.unsafe_goal = wrist_angle_;
Sabina Davis91b23602019-01-21 00:06:01 -0800279
280 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
281 if (!new_superstructure_goal.Send()) {
282 LOG(ERROR, "Sending superstructure goal failed.\n");
283 }
284 }
285
Austin Schuh1a17e132019-02-17 15:05:06 -0800286 void Disc() {
287 top_ = true;
288 bottom_ = true;
289 }
290 void Ball() {
291 top_ = false;
292 bottom_ = true;
293 }
294
Sabina Davis91b23602019-01-21 00:06:01 -0800295 private:
296 // Current goals here.
297 double elevator_height_ = 0.0;
298 double wrist_angle_ = 0.0;
Austin Schuh1a17e132019-02-17 15:05:06 -0800299
300 bool top_ = false;
301 bool bottom_ = false;
Sabina Davis91b23602019-01-21 00:06:01 -0800302};
303
304} // namespace joysticks
305} // namespace input
306} // namespace y2019
307
308int main() {
309 ::aos::Init(-1);
310 ::aos::ShmEventLoop event_loop;
311 ::y2019::input::joysticks::Reader reader(&event_loop);
312 reader.Run();
313 ::aos::Cleanup();
314}