blob: 76ded4802aca4c29919f6ebe3b6d0fa36c96b201 [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"
Sabina Davisc6329342019-03-01 20:44:42 -080021#include "y2019/status_light.q.h"
Sabina Davis91b23602019-01-21 00:06:01 -080022
23using ::y2019::control_loops::superstructure::superstructure_queue;
24using ::aos::input::driver_station::ButtonLocation;
25using ::aos::input::driver_station::ControlBit;
26using ::aos::input::driver_station::JoystickAxis;
27using ::aos::input::driver_station::POVLocation;
28
29namespace y2019 {
30namespace input {
31namespace joysticks {
32
Austin Schuh1a17e132019-02-17 15:05:06 -080033const ButtonLocation kSuctionBall(3, 13);
34const ButtonLocation kSuctionHatch(3, 12);
35const ButtonLocation kDeployStilt(3, 8);
Sabina Davis069cbca2019-03-08 23:35:18 -080036const ButtonLocation kHalfStilt(3, 6);
Austin Schuh1a17e132019-02-17 15:05:06 -080037const ButtonLocation kFallOver(3, 9);
Austin Schuh2cf16b82019-02-15 23:23:22 -080038
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080039struct ElevatorWristPosition {
40 double elevator;
41 double wrist;
42};
Sabina Davis91b23602019-01-21 00:06:01 -080043
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080044const ButtonLocation kRocketForwardLower(5, 1);
45const ButtonLocation kRocketForwardMiddle(5, 2);
46const ButtonLocation kRocketForwardUpper(5, 4);
47const ButtonLocation kCargoForward(5, 3);
48
49const POVLocation kRocketBackwardUnpressed(5, -1);
50const POVLocation kRocketBackwardLower(5, 180);
51const POVLocation kRocketBackwardMiddle(5, 90);
52const POVLocation kRocketBackwardUpper(5, 0);
53const POVLocation kCargoBackward(5, 270);
54
55const ButtonLocation kPanelSwitch(5, 7);
56const ButtonLocation kCargoSwitch(5, 8);
57
58const ButtonLocation kBallHPIntakeForward(5, 6);
59const ButtonLocation kBallHPIntakeBackward(5, 5);
60const JoystickAxis kBallOutake(5, 3);
61const JoystickAxis kBallIntake(5, 4);
62
63const ButtonLocation kPanelHPIntakeForward(5, 6);
64const ButtonLocation kPanelHPIntakeBackward(5, 5);
65
66const ButtonLocation kRelease(2, 4);
67
68const ElevatorWristPosition kStowPos{0.36, 0.0};
69
70const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.04, M_PI / 2.0};
Sabina Davise6fe6c52019-03-03 15:48:51 -080071const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.05, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080072
73const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
74const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
75
Sabina Davise48004f2019-03-02 23:15:24 -080076const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0};
77const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080078
Sabina Davise48004f2019-03-02 23:15:24 -080079const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0};
80const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080081
Sabina Davise6fe6c52019-03-03 15:48:51 -080082const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0};
83const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0};
84
85const ElevatorWristPosition kBallForwardLowerPos{0.46, M_PI / 2.0};
86const ElevatorWristPosition kBallBackwardLowerPos{0.15, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080087
Sabina Davise48004f2019-03-02 23:15:24 -080088const ElevatorWristPosition kBallForwardMiddlePos{1.16, 1.546};
89const ElevatorWristPosition kBallBackwardMiddlePos{0.876021, -1.546};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080090
Sabina Davise48004f2019-03-02 23:15:24 -080091const ElevatorWristPosition kBallForwardUpperPos{1.50, 0.961};
92const ElevatorWristPosition kBallBackwardUpperPos{1.41, -1.217};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080093
Sabina Davise48004f2019-03-02 23:15:24 -080094const ElevatorWristPosition kBallCargoForwardPos{0.699044, 1.353};
95const ElevatorWristPosition kBallCargoBackwardPos{0.828265, -1.999};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080096
Sabina Davise6fe6c52019-03-03 15:48:51 -080097const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097};
98const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080099
100const ElevatorWristPosition kBallIntakePos{0.29, 2.14};
Austin Schuh2cf16b82019-02-15 23:23:22 -0800101
Sabina Davis91b23602019-01-21 00:06:01 -0800102class Reader : public ::aos::input::ActionJoystickInput {
103 public:
104 Reader(::aos::EventLoop *event_loop)
105 : ::aos::input::ActionJoystickInput(
106 event_loop,
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800107 ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {
108 superstructure_queue.goal.FetchLatest();
109 if (superstructure_queue.goal.get()) {
Sabina Davisc6329342019-03-01 20:44:42 -0800110 grab_piece_ = superstructure_queue.goal->suction.grab_piece;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800111 }
112 }
Sabina Davis91b23602019-01-21 00:06:01 -0800113
114 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
115 superstructure_queue.position.FetchLatest();
116 superstructure_queue.status.FetchLatest();
117 if (!superstructure_queue.status.get() ||
118 !superstructure_queue.position.get()) {
119 LOG(ERROR, "Got no superstructure status packet.\n");
120 return;
121 }
122
123 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
124
Austin Schuh1a17e132019-02-17 15:05:06 -0800125 if (data.IsPressed(kSuctionBall)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800126 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800127 } else if (data.IsPressed(kSuctionHatch)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800128 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800129 } else if (data.IsPressed(kRelease) ||
130 !superstructure_queue.status->has_piece) {
Sabina Davisc6329342019-03-01 20:44:42 -0800131 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800132 }
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 Davis069cbca2019-03-08 23:35:18 -0800162 } else if (data.IsPressed(kHalfStilt)) {
163 new_superstructure_goal->stilts.unsafe_goal = 0.345;
164 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
165 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
Sabina Davis91b23602019-01-21 00:06:01 -0800166 } else {
Sabina Davisbfdeb362019-03-09 16:20:01 -0800167 new_superstructure_goal->stilts.unsafe_goal = 0.005;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800168 new_superstructure_goal->stilts.profile_params.max_velocity = 0.25;
169 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800170 }
171
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800172 if (superstructure_queue.status->stilts.position > 0.65) {
173 stilts_was_above_ = true;
174 } else if (superstructure_queue.status->stilts.position < 0.1) {
175 stilts_was_above_ = false;
Austin Schuh2cf16b82019-02-15 23:23:22 -0800176 }
177
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800178 if (switch_ball_) {
179 if (superstructure_queue.status->has_piece) {
180 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800181 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800182
183 // Go to intake position and apply vacuum
184 if (data.IsPressed(kBallHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800185 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800186 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
187 } else if (data.IsPressed(kBallHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800188 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800189 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
190 }
191
192 // Go to elevator/wrist position. Overrides intake position if pressed so
193 // we can re-grab the ball.
194 if (data.IsPressed(kRocketForwardLower)) {
195 elevator_wrist_pos_ = kBallForwardLowerPos;
196 } else if (data.IsPressed(kRocketBackwardLower)) {
197 elevator_wrist_pos_ = kBallBackwardLowerPos;
198 } else if (data.IsPressed(kRocketForwardMiddle)) {
199 elevator_wrist_pos_ = kBallForwardMiddlePos;
200 } else if (data.IsPressed(kRocketBackwardMiddle)) {
201 elevator_wrist_pos_ = kBallBackwardMiddlePos;
202 } else if (data.IsPressed(kRocketForwardUpper)) {
203 elevator_wrist_pos_ = kBallForwardUpperPos;
204 } else if (data.IsPressed(kRocketBackwardUpper)) {
205 elevator_wrist_pos_ = kBallBackwardUpperPos;
206 } else if (data.IsPressed(kCargoForward)) {
207 elevator_wrist_pos_ = kBallCargoForwardPos;
208 } else if (data.IsPressed(kCargoBackward)) {
209 elevator_wrist_pos_ = kBallCargoBackwardPos;
210 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800211 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800212 if (data.IsPressed(kPanelHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800213 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800214 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
215 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800216 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800217 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
218 }
219
220 // Go to elevator/wrist position. Overrides intake position if pressed so
221 // we can re-grab the panel.
222 if (data.IsPressed(kRocketForwardLower)) {
223 elevator_wrist_pos_ = kPanelForwardLowerPos;
224 } else if (data.IsPressed(kRocketBackwardLower)) {
225 elevator_wrist_pos_ = kPanelBackwardLowerPos;
226 } else if (data.IsPressed(kRocketForwardMiddle)) {
227 elevator_wrist_pos_ = kPanelForwardMiddlePos;
228 } else if (data.IsPressed(kRocketBackwardMiddle)) {
229 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
230 } else if (data.IsPressed(kRocketForwardUpper)) {
231 elevator_wrist_pos_ = kPanelForwardUpperPos;
232 } else if (data.IsPressed(kRocketBackwardUpper)) {
233 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800234 } else if (data.IsPressed(kCargoForward)) {
235 elevator_wrist_pos_ = kPanelCargoForwardPos;
236 } else if (data.IsPressed(kCargoBackward)) {
237 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800238 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800239 }
240
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800241 if (switch_ball_) {
242 if (kDoBallOutake ||
243 (kDoBallIntake && !superstructure_queue.status->has_piece)) {
244 new_superstructure_goal->intake.unsafe_goal = 0.959327;
245 }
Austin Schuh23a51632019-02-19 16:50:36 -0800246
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800247 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
248 elevator_wrist_pos_ = kBallIntakePos;
249 new_superstructure_goal->roller_voltage = 9.0;
Sabina Davisc6329342019-03-01 20:44:42 -0800250 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800251 } else {
252 if (kDoBallOutake) {
253 new_superstructure_goal->roller_voltage = -6.0;
254 } else {
255 new_superstructure_goal->intake.unsafe_goal = -1.2;
256 new_superstructure_goal->roller_voltage = 0.0;
257 }
258 }
Austin Schuh23a51632019-02-19 16:50:36 -0800259 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800260
261 if (data.IsPressed(kRelease)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800262 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800263 }
264
Sabina Davisc6329342019-03-01 20:44:42 -0800265 if (switch_ball_) {
266 new_superstructure_goal->suction.gamepiece_mode = 0;
267 } else {
268 new_superstructure_goal->suction.gamepiece_mode = 1;
269 }
270
271 new_superstructure_goal->suction.grab_piece = grab_piece_;
Sabina Davis91b23602019-01-21 00:06:01 -0800272
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800273 new_superstructure_goal->elevator.unsafe_goal =
274 elevator_wrist_pos_.elevator;
275 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800276
277 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
278 if (!new_superstructure_goal.Send()) {
279 LOG(ERROR, "Sending superstructure goal failed.\n");
280 }
281 }
282
283 private:
284 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800285 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Sabina Davisc6329342019-03-01 20:44:42 -0800286 bool grab_piece_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800287
288 bool switch_ball_ = false;
289 bool stilts_was_above_ = false;
Sabina Davis91b23602019-01-21 00:06:01 -0800290};
291
292} // namespace joysticks
293} // namespace input
294} // namespace y2019
295
296int main() {
297 ::aos::Init(-1);
298 ::aos::ShmEventLoop event_loop;
299 ::y2019::input::joysticks::Reader reader(&event_loop);
300 reader.Run();
301 ::aos::Cleanup();
302}