blob: b08d6b62e7b0f266ce72ec70deefe6d379dbb2fd [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"
Tyler Chatowe0241452019-03-08 21:07:50 -080014#include "aos/network/team_number.h"
Sabina Davis91b23602019-01-21 00:06:01 -080015#include "aos/util/log_interval.h"
Tyler Chatowe0241452019-03-08 21:07:50 -080016#include "aos/vision/events/udp.h"
17#include "external/com_google_protobuf/src/google/protobuf/stubs/stringprintf.h"
Sabina Davis91b23602019-01-21 00:06:01 -080018#include "frc971/autonomous/auto.q.h"
19#include "frc971/autonomous/base_autonomous_actor.h"
20#include "frc971/control_loops/drivetrain/drivetrain.q.h"
James Kuszmauld8deb682019-03-10 10:38:42 -070021#include "frc971/control_loops/drivetrain/localizer.q.h"
Sabina Davis91b23602019-01-21 00:06:01 -080022
23#include "y2019/control_loops/drivetrain/drivetrain_base.h"
24#include "y2019/control_loops/superstructure/superstructure.q.h"
Sabina Davisc6329342019-03-01 20:44:42 -080025#include "y2019/status_light.q.h"
Tyler Chatowe0241452019-03-08 21:07:50 -080026#include "y2019/vision.pb.h"
Sabina Davis91b23602019-01-21 00:06:01 -080027
28using ::y2019::control_loops::superstructure::superstructure_queue;
James Kuszmauld8deb682019-03-10 10:38:42 -070029using ::frc971::control_loops::drivetrain::localizer_control;
Sabina Davis91b23602019-01-21 00:06:01 -080030using ::aos::input::driver_station::ButtonLocation;
31using ::aos::input::driver_station::ControlBit;
32using ::aos::input::driver_station::JoystickAxis;
33using ::aos::input::driver_station::POVLocation;
Tyler Chatowe0241452019-03-08 21:07:50 -080034using ::aos::events::ProtoTXUdpSocket;
Sabina Davis91b23602019-01-21 00:06:01 -080035
36namespace y2019 {
37namespace input {
38namespace joysticks {
39
Tyler Chatowe0241452019-03-08 21:07:50 -080040using google::protobuf::StringPrintf;
41
Austin Schuh1a17e132019-02-17 15:05:06 -080042const ButtonLocation kSuctionBall(3, 13);
43const ButtonLocation kSuctionHatch(3, 12);
44const ButtonLocation kDeployStilt(3, 8);
Sabina Davis069cbca2019-03-08 23:35:18 -080045const ButtonLocation kHalfStilt(3, 6);
Austin Schuh1a17e132019-02-17 15:05:06 -080046const ButtonLocation kFallOver(3, 9);
Austin Schuh2cf16b82019-02-15 23:23:22 -080047
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080048struct ElevatorWristPosition {
49 double elevator;
50 double wrist;
51};
Sabina Davis91b23602019-01-21 00:06:01 -080052
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080053const ButtonLocation kRocketForwardLower(5, 1);
54const ButtonLocation kRocketForwardMiddle(5, 2);
55const ButtonLocation kRocketForwardUpper(5, 4);
56const ButtonLocation kCargoForward(5, 3);
57
58const POVLocation kRocketBackwardUnpressed(5, -1);
59const POVLocation kRocketBackwardLower(5, 180);
60const POVLocation kRocketBackwardMiddle(5, 90);
61const POVLocation kRocketBackwardUpper(5, 0);
62const POVLocation kCargoBackward(5, 270);
63
64const ButtonLocation kPanelSwitch(5, 7);
65const ButtonLocation kCargoSwitch(5, 8);
66
67const ButtonLocation kBallHPIntakeForward(5, 6);
68const ButtonLocation kBallHPIntakeBackward(5, 5);
69const JoystickAxis kBallOutake(5, 3);
70const JoystickAxis kBallIntake(5, 4);
71
72const ButtonLocation kPanelHPIntakeForward(5, 6);
73const ButtonLocation kPanelHPIntakeBackward(5, 5);
74
75const ButtonLocation kRelease(2, 4);
James Kuszmauld8deb682019-03-10 10:38:42 -070076const ButtonLocation kResetLocalizer(4, 3);
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080077
Tyler Chatow07c906b2019-03-09 21:29:06 -080078const ButtonLocation kAutoPanel(3, 10);
79const ButtonLocation kAutoPanelIntermediate(4, 6);
80
81const ElevatorWristPosition kAutoPanelPos{0.0, -M_PI / 2.0};
82const ElevatorWristPosition kAutoPanelIntermediatePos{0.34, -M_PI / 2.0};
83
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080084const ElevatorWristPosition kStowPos{0.36, 0.0};
85
86const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.04, M_PI / 2.0};
Sabina Davise6fe6c52019-03-03 15:48:51 -080087const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.05, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080088
89const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
90const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
91
Sabina Davise48004f2019-03-02 23:15:24 -080092const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0};
93const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080094
Sabina Davise48004f2019-03-02 23:15:24 -080095const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0};
96const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080097
Sabina Davise6fe6c52019-03-03 15:48:51 -080098const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0};
99const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0};
100
101const ElevatorWristPosition kBallForwardLowerPos{0.46, M_PI / 2.0};
102const ElevatorWristPosition kBallBackwardLowerPos{0.15, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800103
Sabina Davise48004f2019-03-02 23:15:24 -0800104const ElevatorWristPosition kBallForwardMiddlePos{1.16, 1.546};
105const ElevatorWristPosition kBallBackwardMiddlePos{0.876021, -1.546};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800106
Sabina Davise48004f2019-03-02 23:15:24 -0800107const ElevatorWristPosition kBallForwardUpperPos{1.50, 0.961};
108const ElevatorWristPosition kBallBackwardUpperPos{1.41, -1.217};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800109
Sabina Davise48004f2019-03-02 23:15:24 -0800110const ElevatorWristPosition kBallCargoForwardPos{0.699044, 1.353};
111const ElevatorWristPosition kBallCargoBackwardPos{0.828265, -1.999};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800112
Sabina Davise6fe6c52019-03-03 15:48:51 -0800113const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097};
114const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800115
116const ElevatorWristPosition kBallIntakePos{0.29, 2.14};
Austin Schuh2cf16b82019-02-15 23:23:22 -0800117
Sabina Davis91b23602019-01-21 00:06:01 -0800118class Reader : public ::aos::input::ActionJoystickInput {
119 public:
120 Reader(::aos::EventLoop *event_loop)
121 : ::aos::input::ActionJoystickInput(
122 event_loop,
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800123 ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {
Tyler Chatowe0241452019-03-08 21:07:50 -0800124 const uint16_t team = ::aos::network::GetTeamNumber();
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800125 superstructure_queue.goal.FetchLatest();
126 if (superstructure_queue.goal.get()) {
Sabina Davisc6329342019-03-01 20:44:42 -0800127 grab_piece_ = superstructure_queue.goal->suction.grab_piece;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800128 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800129 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
130 StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000));
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800131 }
Sabina Davis91b23602019-01-21 00:06:01 -0800132
133 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
134 superstructure_queue.position.FetchLatest();
135 superstructure_queue.status.FetchLatest();
136 if (!superstructure_queue.status.get() ||
137 !superstructure_queue.position.get()) {
138 LOG(ERROR, "Got no superstructure status packet.\n");
139 return;
140 }
141
142 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
143
James Kuszmauld8deb682019-03-10 10:38:42 -0700144 if (data.PosEdge(kResetLocalizer)) {
145 auto localizer_resetter = localizer_control.MakeMessage();
146 localizer_resetter->x = 0.4;
147 localizer_resetter->y = 3.4;
148 localizer_resetter->theta = 0.0;
149 if (!localizer_resetter.Send()) {
150 LOG(ERROR, "Failed to reset localizer.\n");
151 }
152 }
153
Austin Schuh1a17e132019-02-17 15:05:06 -0800154 if (data.IsPressed(kSuctionBall)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800155 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800156 } else if (data.IsPressed(kSuctionHatch)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800157 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800158 } else if (data.IsPressed(kRelease) ||
159 !superstructure_queue.status->has_piece) {
Sabina Davisc6329342019-03-01 20:44:42 -0800160 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800161 }
Sabina Davis91b23602019-01-21 00:06:01 -0800162
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800163 if (data.IsPressed(kRocketBackwardUnpressed)) {
164 elevator_wrist_pos_ = kStowPos;
165 }
166 new_superstructure_goal->intake.unsafe_goal = -1.2;
167 new_superstructure_goal->roller_voltage = 0.0;
168
169 const bool kDoBallIntake = data.GetAxis(kBallIntake) > 0.9;
170 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
171
172 if (data.IsPressed(kPanelSwitch)) {
173 switch_ball_ = false;
174 } else if (data.IsPressed(kCargoSwitch)) {
175 switch_ball_ = true;
176 }
177
Sabina Davis91b23602019-01-21 00:06:01 -0800178 // TODO(sabina): max height please?
Austin Schuh77ac3212019-02-19 16:50:14 -0800179 if (data.IsPressed(kFallOver)) {
Austin Schuh1a17e132019-02-17 15:05:06 -0800180 new_superstructure_goal->stilts.unsafe_goal = 0.71;
Michael Schuh587dcb52019-02-28 21:31:03 -0800181 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800182 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Austin Schuh77ac3212019-02-19 16:50:14 -0800183 } else if (data.IsPressed(kDeployStilt)) {
184 new_superstructure_goal->stilts.unsafe_goal = 0.50;
Michael Schuh587dcb52019-02-28 21:31:03 -0800185 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800186 if (stilts_was_above_) {
187 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
188 } else {
189 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
190 }
Sabina Davis069cbca2019-03-08 23:35:18 -0800191 } else if (data.IsPressed(kHalfStilt)) {
192 new_superstructure_goal->stilts.unsafe_goal = 0.345;
193 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
194 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
Sabina Davis91b23602019-01-21 00:06:01 -0800195 } else {
Sabina Davisbfdeb362019-03-09 16:20:01 -0800196 new_superstructure_goal->stilts.unsafe_goal = 0.005;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800197 new_superstructure_goal->stilts.profile_params.max_velocity = 0.25;
198 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800199 }
200
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800201 if (superstructure_queue.status->stilts.position > 0.65) {
202 stilts_was_above_ = true;
203 } else if (superstructure_queue.status->stilts.position < 0.1) {
204 stilts_was_above_ = false;
Austin Schuh2cf16b82019-02-15 23:23:22 -0800205 }
206
Tyler Chatow07c906b2019-03-09 21:29:06 -0800207 if (data.IsPressed(kAutoPanel)) {
208 elevator_wrist_pos_ = kAutoPanelPos;
209 } else if (data.IsPressed(kAutoPanelIntermediate)) {
210 elevator_wrist_pos_ = kAutoPanelIntermediatePos;
211 }
212
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800213 if (switch_ball_) {
214 if (superstructure_queue.status->has_piece) {
215 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800216 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800217
218 // Go to intake position and apply vacuum
219 if (data.IsPressed(kBallHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800220 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800221 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
222 } else if (data.IsPressed(kBallHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800223 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800224 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
225 }
226
227 // Go to elevator/wrist position. Overrides intake position if pressed so
228 // we can re-grab the ball.
229 if (data.IsPressed(kRocketForwardLower)) {
230 elevator_wrist_pos_ = kBallForwardLowerPos;
231 } else if (data.IsPressed(kRocketBackwardLower)) {
232 elevator_wrist_pos_ = kBallBackwardLowerPos;
233 } else if (data.IsPressed(kRocketForwardMiddle)) {
234 elevator_wrist_pos_ = kBallForwardMiddlePos;
235 } else if (data.IsPressed(kRocketBackwardMiddle)) {
236 elevator_wrist_pos_ = kBallBackwardMiddlePos;
237 } else if (data.IsPressed(kRocketForwardUpper)) {
238 elevator_wrist_pos_ = kBallForwardUpperPos;
239 } else if (data.IsPressed(kRocketBackwardUpper)) {
240 elevator_wrist_pos_ = kBallBackwardUpperPos;
241 } else if (data.IsPressed(kCargoForward)) {
242 elevator_wrist_pos_ = kBallCargoForwardPos;
243 } else if (data.IsPressed(kCargoBackward)) {
244 elevator_wrist_pos_ = kBallCargoBackwardPos;
245 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800246 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800247 if (data.IsPressed(kPanelHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800248 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800249 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
250 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800251 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800252 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
253 }
254
255 // Go to elevator/wrist position. Overrides intake position if pressed so
256 // we can re-grab the panel.
257 if (data.IsPressed(kRocketForwardLower)) {
258 elevator_wrist_pos_ = kPanelForwardLowerPos;
259 } else if (data.IsPressed(kRocketBackwardLower)) {
260 elevator_wrist_pos_ = kPanelBackwardLowerPos;
261 } else if (data.IsPressed(kRocketForwardMiddle)) {
262 elevator_wrist_pos_ = kPanelForwardMiddlePos;
263 } else if (data.IsPressed(kRocketBackwardMiddle)) {
264 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
265 } else if (data.IsPressed(kRocketForwardUpper)) {
266 elevator_wrist_pos_ = kPanelForwardUpperPos;
267 } else if (data.IsPressed(kRocketBackwardUpper)) {
268 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800269 } else if (data.IsPressed(kCargoForward)) {
270 elevator_wrist_pos_ = kPanelCargoForwardPos;
271 } else if (data.IsPressed(kCargoBackward)) {
272 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800273 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800274 }
275
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800276 if (switch_ball_) {
277 if (kDoBallOutake ||
278 (kDoBallIntake && !superstructure_queue.status->has_piece)) {
279 new_superstructure_goal->intake.unsafe_goal = 0.959327;
280 }
Austin Schuh23a51632019-02-19 16:50:36 -0800281
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800282 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
283 elevator_wrist_pos_ = kBallIntakePos;
284 new_superstructure_goal->roller_voltage = 9.0;
Sabina Davisc6329342019-03-01 20:44:42 -0800285 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800286 } else {
287 if (kDoBallOutake) {
288 new_superstructure_goal->roller_voltage = -6.0;
289 } else {
290 new_superstructure_goal->intake.unsafe_goal = -1.2;
291 new_superstructure_goal->roller_voltage = 0.0;
292 }
293 }
Austin Schuh23a51632019-02-19 16:50:36 -0800294 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800295
296 if (data.IsPressed(kRelease)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800297 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800298 }
299
Sabina Davisc6329342019-03-01 20:44:42 -0800300 if (switch_ball_) {
301 new_superstructure_goal->suction.gamepiece_mode = 0;
302 } else {
303 new_superstructure_goal->suction.gamepiece_mode = 1;
304 }
305
Tyler Chatowe0241452019-03-08 21:07:50 -0800306 vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0);
307
Sabina Davisc6329342019-03-01 20:44:42 -0800308 new_superstructure_goal->suction.grab_piece = grab_piece_;
Sabina Davis91b23602019-01-21 00:06:01 -0800309
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800310 new_superstructure_goal->elevator.unsafe_goal =
311 elevator_wrist_pos_.elevator;
312 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800313
314 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
315 if (!new_superstructure_goal.Send()) {
316 LOG(ERROR, "Sending superstructure goal failed.\n");
317 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800318
319 video_tx_->Send(vision_control_);
Sabina Davis91b23602019-01-21 00:06:01 -0800320 }
321
322 private:
323 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800324 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Sabina Davisc6329342019-03-01 20:44:42 -0800325 bool grab_piece_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800326
327 bool switch_ball_ = false;
328 bool stilts_was_above_ = false;
Tyler Chatowe0241452019-03-08 21:07:50 -0800329
330 VisionControl vision_control_;
331 ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_;
Sabina Davis91b23602019-01-21 00:06:01 -0800332};
333
334} // namespace joysticks
335} // namespace input
336} // namespace y2019
337
338int main() {
339 ::aos::Init(-1);
340 ::aos::ShmEventLoop event_loop;
341 ::y2019::input::joysticks::Reader reader(&event_loop);
342 reader.Run();
343 ::aos::Cleanup();
344}