blob: c4a1cafb497d05a40ad59a886acdfaa0f79faabf [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>
Tyler Chatow4fedeea2019-03-10 15:33:36 -07005#include <chrono>
Sabina Davis91b23602019-01-21 00:06:01 -08006
7#include "aos/actions/actions.h"
8#include "aos/init.h"
9#include "aos/input/action_joystick_input.h"
10#include "aos/input/driver_station_data.h"
11#include "aos/input/drivetrain_input.h"
12#include "aos/input/joystick_input.h"
13#include "aos/logging/logging.h"
14#include "aos/logging/logging.h"
Tyler Chatowe0241452019-03-08 21:07:50 -080015#include "aos/network/team_number.h"
Sabina Davis91b23602019-01-21 00:06:01 -080016#include "aos/util/log_interval.h"
Tyler Chatowe0241452019-03-08 21:07:50 -080017#include "aos/vision/events/udp.h"
18#include "external/com_google_protobuf/src/google/protobuf/stubs/stringprintf.h"
Sabina Davis91b23602019-01-21 00:06:01 -080019#include "frc971/autonomous/auto.q.h"
20#include "frc971/autonomous/base_autonomous_actor.h"
21#include "frc971/control_loops/drivetrain/drivetrain.q.h"
James Kuszmauld8deb682019-03-10 10:38:42 -070022#include "frc971/control_loops/drivetrain/localizer.q.h"
Sabina Davis91b23602019-01-21 00:06:01 -080023
24#include "y2019/control_loops/drivetrain/drivetrain_base.h"
25#include "y2019/control_loops/superstructure/superstructure.q.h"
Sabina Davisc6329342019-03-01 20:44:42 -080026#include "y2019/status_light.q.h"
Tyler Chatowe0241452019-03-08 21:07:50 -080027#include "y2019/vision.pb.h"
Sabina Davis91b23602019-01-21 00:06:01 -080028
29using ::y2019::control_loops::superstructure::superstructure_queue;
James Kuszmauld8deb682019-03-10 10:38:42 -070030using ::frc971::control_loops::drivetrain::localizer_control;
Sabina Davis91b23602019-01-21 00:06:01 -080031using ::aos::input::driver_station::ButtonLocation;
32using ::aos::input::driver_station::ControlBit;
33using ::aos::input::driver_station::JoystickAxis;
34using ::aos::input::driver_station::POVLocation;
Tyler Chatowe0241452019-03-08 21:07:50 -080035using ::aos::events::ProtoTXUdpSocket;
Sabina Davis91b23602019-01-21 00:06:01 -080036
37namespace y2019 {
38namespace input {
39namespace joysticks {
40
Tyler Chatowe0241452019-03-08 21:07:50 -080041using google::protobuf::StringPrintf;
42
Austin Schuh1a17e132019-02-17 15:05:06 -080043const ButtonLocation kSuctionBall(3, 13);
44const ButtonLocation kSuctionHatch(3, 12);
45const ButtonLocation kDeployStilt(3, 8);
Sabina Davis069cbca2019-03-08 23:35:18 -080046const ButtonLocation kHalfStilt(3, 6);
Austin Schuh1a17e132019-02-17 15:05:06 -080047const ButtonLocation kFallOver(3, 9);
Austin Schuh2cf16b82019-02-15 23:23:22 -080048
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080049struct ElevatorWristPosition {
50 double elevator;
51 double wrist;
52};
Sabina Davis91b23602019-01-21 00:06:01 -080053
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080054const ButtonLocation kRocketForwardLower(5, 1);
55const ButtonLocation kRocketForwardMiddle(5, 2);
56const ButtonLocation kRocketForwardUpper(5, 4);
57const ButtonLocation kCargoForward(5, 3);
58
59const POVLocation kRocketBackwardUnpressed(5, -1);
60const POVLocation kRocketBackwardLower(5, 180);
61const POVLocation kRocketBackwardMiddle(5, 90);
62const POVLocation kRocketBackwardUpper(5, 0);
63const POVLocation kCargoBackward(5, 270);
64
65const ButtonLocation kPanelSwitch(5, 7);
66const ButtonLocation kCargoSwitch(5, 8);
67
68const ButtonLocation kBallHPIntakeForward(5, 6);
69const ButtonLocation kBallHPIntakeBackward(5, 5);
70const JoystickAxis kBallOutake(5, 3);
71const JoystickAxis kBallIntake(5, 4);
72
73const ButtonLocation kPanelHPIntakeForward(5, 6);
74const ButtonLocation kPanelHPIntakeBackward(5, 5);
75
76const ButtonLocation kRelease(2, 4);
James Kuszmauld8deb682019-03-10 10:38:42 -070077const ButtonLocation kResetLocalizer(4, 3);
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080078
Tyler Chatow07c906b2019-03-09 21:29:06 -080079const ButtonLocation kAutoPanel(3, 10);
80const ButtonLocation kAutoPanelIntermediate(4, 6);
81
82const ElevatorWristPosition kAutoPanelPos{0.0, -M_PI / 2.0};
83const ElevatorWristPosition kAutoPanelIntermediatePos{0.34, -M_PI / 2.0};
84
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080085const ElevatorWristPosition kStowPos{0.36, 0.0};
86
87const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.04, M_PI / 2.0};
Sabina Davise6fe6c52019-03-03 15:48:51 -080088const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.05, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080089
90const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
91const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
92
Sabina Davise48004f2019-03-02 23:15:24 -080093const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0};
94const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080095
Sabina Davise48004f2019-03-02 23:15:24 -080096const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0};
97const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080098
Sabina Davise6fe6c52019-03-03 15:48:51 -080099const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0};
100const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0};
101
102const ElevatorWristPosition kBallForwardLowerPos{0.46, M_PI / 2.0};
103const ElevatorWristPosition kBallBackwardLowerPos{0.15, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800104
Sabina Davise48004f2019-03-02 23:15:24 -0800105const ElevatorWristPosition kBallForwardMiddlePos{1.16, 1.546};
106const ElevatorWristPosition kBallBackwardMiddlePos{0.876021, -1.546};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800107
Sabina Davise48004f2019-03-02 23:15:24 -0800108const ElevatorWristPosition kBallForwardUpperPos{1.50, 0.961};
109const ElevatorWristPosition kBallBackwardUpperPos{1.41, -1.217};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800110
Sabina Davise48004f2019-03-02 23:15:24 -0800111const ElevatorWristPosition kBallCargoForwardPos{0.699044, 1.353};
112const ElevatorWristPosition kBallCargoBackwardPos{0.828265, -1.999};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800113
Sabina Davise6fe6c52019-03-03 15:48:51 -0800114const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097};
115const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800116
117const ElevatorWristPosition kBallIntakePos{0.29, 2.14};
Austin Schuh2cf16b82019-02-15 23:23:22 -0800118
Sabina Davis91b23602019-01-21 00:06:01 -0800119class Reader : public ::aos::input::ActionJoystickInput {
120 public:
121 Reader(::aos::EventLoop *event_loop)
122 : ::aos::input::ActionJoystickInput(
123 event_loop,
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800124 ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {
Tyler Chatowe0241452019-03-08 21:07:50 -0800125 const uint16_t team = ::aos::network::GetTeamNumber();
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800126 superstructure_queue.goal.FetchLatest();
127 if (superstructure_queue.goal.get()) {
Sabina Davisc6329342019-03-01 20:44:42 -0800128 grab_piece_ = superstructure_queue.goal->suction.grab_piece;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800129 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800130 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
131 StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000));
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800132 }
Sabina Davis91b23602019-01-21 00:06:01 -0800133
134 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
135 superstructure_queue.position.FetchLatest();
136 superstructure_queue.status.FetchLatest();
137 if (!superstructure_queue.status.get() ||
138 !superstructure_queue.position.get()) {
139 LOG(ERROR, "Got no superstructure status packet.\n");
140 return;
141 }
142
143 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
144
James Kuszmauld8deb682019-03-10 10:38:42 -0700145 if (data.PosEdge(kResetLocalizer)) {
146 auto localizer_resetter = localizer_control.MakeMessage();
147 localizer_resetter->x = 0.4;
148 localizer_resetter->y = 3.4;
149 localizer_resetter->theta = 0.0;
150 if (!localizer_resetter.Send()) {
151 LOG(ERROR, "Failed to reset localizer.\n");
152 }
153 }
154
Austin Schuh1a17e132019-02-17 15:05:06 -0800155 if (data.IsPressed(kSuctionBall)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800156 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800157 } else if (data.IsPressed(kSuctionHatch)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800158 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800159 } else if (data.IsPressed(kRelease) ||
160 !superstructure_queue.status->has_piece) {
Sabina Davisc6329342019-03-01 20:44:42 -0800161 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800162 }
Sabina Davis91b23602019-01-21 00:06:01 -0800163
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800164 if (data.IsPressed(kRocketBackwardUnpressed)) {
165 elevator_wrist_pos_ = kStowPos;
166 }
167 new_superstructure_goal->intake.unsafe_goal = -1.2;
168 new_superstructure_goal->roller_voltage = 0.0;
169
170 const bool kDoBallIntake = data.GetAxis(kBallIntake) > 0.9;
171 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
172
173 if (data.IsPressed(kPanelSwitch)) {
174 switch_ball_ = false;
175 } else if (data.IsPressed(kCargoSwitch)) {
176 switch_ball_ = true;
177 }
178
Sabina Davis91b23602019-01-21 00:06:01 -0800179 // TODO(sabina): max height please?
Austin Schuh77ac3212019-02-19 16:50:14 -0800180 if (data.IsPressed(kFallOver)) {
Austin Schuh1a17e132019-02-17 15:05:06 -0800181 new_superstructure_goal->stilts.unsafe_goal = 0.71;
Michael Schuh587dcb52019-02-28 21:31:03 -0800182 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800183 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Austin Schuh77ac3212019-02-19 16:50:14 -0800184 } else if (data.IsPressed(kDeployStilt)) {
185 new_superstructure_goal->stilts.unsafe_goal = 0.50;
Michael Schuh587dcb52019-02-28 21:31:03 -0800186 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800187 if (stilts_was_above_) {
188 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
189 } else {
190 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
191 }
Sabina Davis069cbca2019-03-08 23:35:18 -0800192 } else if (data.IsPressed(kHalfStilt)) {
193 new_superstructure_goal->stilts.unsafe_goal = 0.345;
194 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
195 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
Sabina Davis91b23602019-01-21 00:06:01 -0800196 } else {
Sabina Davisbfdeb362019-03-09 16:20:01 -0800197 new_superstructure_goal->stilts.unsafe_goal = 0.005;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800198 new_superstructure_goal->stilts.profile_params.max_velocity = 0.25;
199 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800200 }
201
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800202 if (superstructure_queue.status->stilts.position > 0.65) {
203 stilts_was_above_ = true;
204 } else if (superstructure_queue.status->stilts.position < 0.1) {
205 stilts_was_above_ = false;
Austin Schuh2cf16b82019-02-15 23:23:22 -0800206 }
207
Tyler Chatow07c906b2019-03-09 21:29:06 -0800208 if (data.IsPressed(kAutoPanel)) {
209 elevator_wrist_pos_ = kAutoPanelPos;
210 } else if (data.IsPressed(kAutoPanelIntermediate)) {
211 elevator_wrist_pos_ = kAutoPanelIntermediatePos;
212 }
213
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800214 if (switch_ball_) {
215 if (superstructure_queue.status->has_piece) {
216 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800217 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800218
219 // Go to intake position and apply vacuum
220 if (data.IsPressed(kBallHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800221 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800222 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
223 } else if (data.IsPressed(kBallHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800224 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800225 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
226 }
227
228 // Go to elevator/wrist position. Overrides intake position if pressed so
229 // we can re-grab the ball.
230 if (data.IsPressed(kRocketForwardLower)) {
231 elevator_wrist_pos_ = kBallForwardLowerPos;
232 } else if (data.IsPressed(kRocketBackwardLower)) {
233 elevator_wrist_pos_ = kBallBackwardLowerPos;
234 } else if (data.IsPressed(kRocketForwardMiddle)) {
235 elevator_wrist_pos_ = kBallForwardMiddlePos;
236 } else if (data.IsPressed(kRocketBackwardMiddle)) {
237 elevator_wrist_pos_ = kBallBackwardMiddlePos;
238 } else if (data.IsPressed(kRocketForwardUpper)) {
239 elevator_wrist_pos_ = kBallForwardUpperPos;
240 } else if (data.IsPressed(kRocketBackwardUpper)) {
241 elevator_wrist_pos_ = kBallBackwardUpperPos;
242 } else if (data.IsPressed(kCargoForward)) {
243 elevator_wrist_pos_ = kBallCargoForwardPos;
244 } else if (data.IsPressed(kCargoBackward)) {
245 elevator_wrist_pos_ = kBallCargoBackwardPos;
246 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800247 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800248 if (data.IsPressed(kPanelHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800249 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800250 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
251 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800252 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800253 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
254 }
255
256 // Go to elevator/wrist position. Overrides intake position if pressed so
257 // we can re-grab the panel.
258 if (data.IsPressed(kRocketForwardLower)) {
259 elevator_wrist_pos_ = kPanelForwardLowerPos;
260 } else if (data.IsPressed(kRocketBackwardLower)) {
261 elevator_wrist_pos_ = kPanelBackwardLowerPos;
262 } else if (data.IsPressed(kRocketForwardMiddle)) {
263 elevator_wrist_pos_ = kPanelForwardMiddlePos;
264 } else if (data.IsPressed(kRocketBackwardMiddle)) {
265 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
266 } else if (data.IsPressed(kRocketForwardUpper)) {
267 elevator_wrist_pos_ = kPanelForwardUpperPos;
268 } else if (data.IsPressed(kRocketBackwardUpper)) {
269 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800270 } else if (data.IsPressed(kCargoForward)) {
271 elevator_wrist_pos_ = kPanelCargoForwardPos;
272 } else if (data.IsPressed(kCargoBackward)) {
273 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800274 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800275 }
276
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800277 if (switch_ball_) {
278 if (kDoBallOutake ||
279 (kDoBallIntake && !superstructure_queue.status->has_piece)) {
280 new_superstructure_goal->intake.unsafe_goal = 0.959327;
281 }
Austin Schuh23a51632019-02-19 16:50:36 -0800282
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800283 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
284 elevator_wrist_pos_ = kBallIntakePos;
285 new_superstructure_goal->roller_voltage = 9.0;
Sabina Davisc6329342019-03-01 20:44:42 -0800286 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800287 } else {
288 if (kDoBallOutake) {
289 new_superstructure_goal->roller_voltage = -6.0;
290 } else {
291 new_superstructure_goal->intake.unsafe_goal = -1.2;
292 new_superstructure_goal->roller_voltage = 0.0;
293 }
294 }
Austin Schuh23a51632019-02-19 16:50:36 -0800295 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800296
297 if (data.IsPressed(kRelease)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800298 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800299 }
300
Sabina Davisc6329342019-03-01 20:44:42 -0800301 if (switch_ball_) {
302 new_superstructure_goal->suction.gamepiece_mode = 0;
303 } else {
304 new_superstructure_goal->suction.gamepiece_mode = 1;
305 }
306
Tyler Chatowe0241452019-03-08 21:07:50 -0800307 vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0);
308
Sabina Davisc6329342019-03-01 20:44:42 -0800309 new_superstructure_goal->suction.grab_piece = grab_piece_;
Sabina Davis91b23602019-01-21 00:06:01 -0800310
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800311 new_superstructure_goal->elevator.unsafe_goal =
312 elevator_wrist_pos_.elevator;
313 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800314
315 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
316 if (!new_superstructure_goal.Send()) {
317 LOG(ERROR, "Sending superstructure goal failed.\n");
318 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800319
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700320 auto time_now = ::aos::monotonic_clock::now();
321 if (time_now > last_vision_control_ + ::std::chrono::milliseconds(50)) {
322 video_tx_->Send(vision_control_);
323 last_vision_control_ = time_now;
324 }
Sabina Davis91b23602019-01-21 00:06:01 -0800325 }
326
327 private:
328 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800329 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Sabina Davisc6329342019-03-01 20:44:42 -0800330 bool grab_piece_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800331
332 bool switch_ball_ = false;
333 bool stilts_was_above_ = false;
Tyler Chatowe0241452019-03-08 21:07:50 -0800334
335 VisionControl vision_control_;
336 ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700337 ::aos::monotonic_clock::time_point last_vision_control_ =
338 ::aos::monotonic_clock::time_point::min();
Sabina Davis91b23602019-01-21 00:06:01 -0800339};
340
341} // namespace joysticks
342} // namespace input
343} // namespace y2019
344
345int main() {
346 ::aos::Init(-1);
347 ::aos::ShmEventLoop event_loop;
348 ::y2019::input::joysticks::Reader reader(&event_loop);
349 reader.Run();
350 ::aos::Cleanup();
351}