blob: e435b96d8d981c182eac747b61c3fa96581b6331 [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()) {
Austin Schuh30cc40a2019-03-12 21:02:13 -0700125 // Set teleop to immediately resume after auto ends for sandstorm mode.
126 set_run_teleop_in_auto(true);
127
Tyler Chatowe0241452019-03-08 21:07:50 -0800128 const uint16_t team = ::aos::network::GetTeamNumber();
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800129 superstructure_queue.goal.FetchLatest();
130 if (superstructure_queue.goal.get()) {
Sabina Davisc6329342019-03-01 20:44:42 -0800131 grab_piece_ = superstructure_queue.goal->suction.grab_piece;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800132 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800133 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
134 StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000));
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800135 }
Sabina Davis91b23602019-01-21 00:06:01 -0800136
137 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
138 superstructure_queue.position.FetchLatest();
139 superstructure_queue.status.FetchLatest();
140 if (!superstructure_queue.status.get() ||
141 !superstructure_queue.position.get()) {
142 LOG(ERROR, "Got no superstructure status packet.\n");
143 return;
144 }
145
146 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
147
James Kuszmauld8deb682019-03-10 10:38:42 -0700148 if (data.PosEdge(kResetLocalizer)) {
149 auto localizer_resetter = localizer_control.MakeMessage();
150 localizer_resetter->x = 0.4;
151 localizer_resetter->y = 3.4;
152 localizer_resetter->theta = 0.0;
153 if (!localizer_resetter.Send()) {
154 LOG(ERROR, "Failed to reset localizer.\n");
155 }
156 }
157
Austin Schuh1a17e132019-02-17 15:05:06 -0800158 if (data.IsPressed(kSuctionBall)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800159 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800160 } else if (data.IsPressed(kSuctionHatch)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800161 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800162 } else if (data.IsPressed(kRelease) ||
163 !superstructure_queue.status->has_piece) {
Sabina Davisc6329342019-03-01 20:44:42 -0800164 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800165 }
Sabina Davis91b23602019-01-21 00:06:01 -0800166
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800167 if (data.IsPressed(kRocketBackwardUnpressed)) {
168 elevator_wrist_pos_ = kStowPos;
169 }
170 new_superstructure_goal->intake.unsafe_goal = -1.2;
171 new_superstructure_goal->roller_voltage = 0.0;
172
173 const bool kDoBallIntake = data.GetAxis(kBallIntake) > 0.9;
174 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
175
176 if (data.IsPressed(kPanelSwitch)) {
177 switch_ball_ = false;
178 } else if (data.IsPressed(kCargoSwitch)) {
179 switch_ball_ = true;
180 }
181
Sabina Davis91b23602019-01-21 00:06:01 -0800182 // TODO(sabina): max height please?
Austin Schuh77ac3212019-02-19 16:50:14 -0800183 if (data.IsPressed(kFallOver)) {
Austin Schuh1a17e132019-02-17 15:05:06 -0800184 new_superstructure_goal->stilts.unsafe_goal = 0.71;
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 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Austin Schuh77ac3212019-02-19 16:50:14 -0800187 } else if (data.IsPressed(kDeployStilt)) {
188 new_superstructure_goal->stilts.unsafe_goal = 0.50;
Michael Schuh587dcb52019-02-28 21:31:03 -0800189 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800190 if (stilts_was_above_) {
191 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
192 } else {
193 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
194 }
Sabina Davis069cbca2019-03-08 23:35:18 -0800195 } else if (data.IsPressed(kHalfStilt)) {
196 new_superstructure_goal->stilts.unsafe_goal = 0.345;
197 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
198 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
Sabina Davis91b23602019-01-21 00:06:01 -0800199 } else {
Sabina Davisbfdeb362019-03-09 16:20:01 -0800200 new_superstructure_goal->stilts.unsafe_goal = 0.005;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800201 new_superstructure_goal->stilts.profile_params.max_velocity = 0.25;
202 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800203 }
204
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800205 if (superstructure_queue.status->stilts.position > 0.65) {
206 stilts_was_above_ = true;
207 } else if (superstructure_queue.status->stilts.position < 0.1) {
208 stilts_was_above_ = false;
Austin Schuh2cf16b82019-02-15 23:23:22 -0800209 }
210
Tyler Chatow07c906b2019-03-09 21:29:06 -0800211 if (data.IsPressed(kAutoPanel)) {
212 elevator_wrist_pos_ = kAutoPanelPos;
213 } else if (data.IsPressed(kAutoPanelIntermediate)) {
214 elevator_wrist_pos_ = kAutoPanelIntermediatePos;
215 }
216
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800217 if (switch_ball_) {
218 if (superstructure_queue.status->has_piece) {
219 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800220 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800221
222 // Go to intake position and apply vacuum
223 if (data.IsPressed(kBallHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800224 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800225 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
226 } else if (data.IsPressed(kBallHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800227 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800228 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
229 }
230
231 // Go to elevator/wrist position. Overrides intake position if pressed so
232 // we can re-grab the ball.
233 if (data.IsPressed(kRocketForwardLower)) {
234 elevator_wrist_pos_ = kBallForwardLowerPos;
235 } else if (data.IsPressed(kRocketBackwardLower)) {
236 elevator_wrist_pos_ = kBallBackwardLowerPos;
237 } else if (data.IsPressed(kRocketForwardMiddle)) {
238 elevator_wrist_pos_ = kBallForwardMiddlePos;
239 } else if (data.IsPressed(kRocketBackwardMiddle)) {
240 elevator_wrist_pos_ = kBallBackwardMiddlePos;
241 } else if (data.IsPressed(kRocketForwardUpper)) {
242 elevator_wrist_pos_ = kBallForwardUpperPos;
243 } else if (data.IsPressed(kRocketBackwardUpper)) {
244 elevator_wrist_pos_ = kBallBackwardUpperPos;
245 } else if (data.IsPressed(kCargoForward)) {
246 elevator_wrist_pos_ = kBallCargoForwardPos;
247 } else if (data.IsPressed(kCargoBackward)) {
248 elevator_wrist_pos_ = kBallCargoBackwardPos;
249 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800250 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800251 if (data.IsPressed(kPanelHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800252 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800253 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
254 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800255 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800256 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
257 }
258
259 // Go to elevator/wrist position. Overrides intake position if pressed so
260 // we can re-grab the panel.
261 if (data.IsPressed(kRocketForwardLower)) {
262 elevator_wrist_pos_ = kPanelForwardLowerPos;
263 } else if (data.IsPressed(kRocketBackwardLower)) {
264 elevator_wrist_pos_ = kPanelBackwardLowerPos;
265 } else if (data.IsPressed(kRocketForwardMiddle)) {
266 elevator_wrist_pos_ = kPanelForwardMiddlePos;
267 } else if (data.IsPressed(kRocketBackwardMiddle)) {
268 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
269 } else if (data.IsPressed(kRocketForwardUpper)) {
270 elevator_wrist_pos_ = kPanelForwardUpperPos;
271 } else if (data.IsPressed(kRocketBackwardUpper)) {
272 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800273 } else if (data.IsPressed(kCargoForward)) {
274 elevator_wrist_pos_ = kPanelCargoForwardPos;
275 } else if (data.IsPressed(kCargoBackward)) {
276 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800277 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800278 }
279
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800280 if (switch_ball_) {
281 if (kDoBallOutake ||
282 (kDoBallIntake && !superstructure_queue.status->has_piece)) {
283 new_superstructure_goal->intake.unsafe_goal = 0.959327;
284 }
Austin Schuh23a51632019-02-19 16:50:36 -0800285
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800286 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
287 elevator_wrist_pos_ = kBallIntakePos;
288 new_superstructure_goal->roller_voltage = 9.0;
Sabina Davisc6329342019-03-01 20:44:42 -0800289 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800290 } else {
291 if (kDoBallOutake) {
292 new_superstructure_goal->roller_voltage = -6.0;
293 } else {
294 new_superstructure_goal->intake.unsafe_goal = -1.2;
295 new_superstructure_goal->roller_voltage = 0.0;
296 }
297 }
Austin Schuh23a51632019-02-19 16:50:36 -0800298 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800299
300 if (data.IsPressed(kRelease)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800301 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800302 }
303
Sabina Davisc6329342019-03-01 20:44:42 -0800304 if (switch_ball_) {
305 new_superstructure_goal->suction.gamepiece_mode = 0;
306 } else {
307 new_superstructure_goal->suction.gamepiece_mode = 1;
308 }
309
Tyler Chatowe0241452019-03-08 21:07:50 -0800310 vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0);
311
Sabina Davisc6329342019-03-01 20:44:42 -0800312 new_superstructure_goal->suction.grab_piece = grab_piece_;
Sabina Davis91b23602019-01-21 00:06:01 -0800313
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800314 new_superstructure_goal->elevator.unsafe_goal =
315 elevator_wrist_pos_.elevator;
316 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800317
318 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
319 if (!new_superstructure_goal.Send()) {
320 LOG(ERROR, "Sending superstructure goal failed.\n");
321 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800322
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700323 auto time_now = ::aos::monotonic_clock::now();
324 if (time_now > last_vision_control_ + ::std::chrono::milliseconds(50)) {
325 video_tx_->Send(vision_control_);
326 last_vision_control_ = time_now;
327 }
Sabina Davis91b23602019-01-21 00:06:01 -0800328 }
329
330 private:
331 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800332 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Sabina Davisc6329342019-03-01 20:44:42 -0800333 bool grab_piece_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800334
335 bool switch_ball_ = false;
336 bool stilts_was_above_ = false;
Tyler Chatowe0241452019-03-08 21:07:50 -0800337
338 VisionControl vision_control_;
339 ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_;
Tyler Chatow4fedeea2019-03-10 15:33:36 -0700340 ::aos::monotonic_clock::time_point last_vision_control_ =
341 ::aos::monotonic_clock::time_point::min();
Sabina Davis91b23602019-01-21 00:06:01 -0800342};
343
344} // namespace joysticks
345} // namespace input
346} // namespace y2019
347
348int main() {
349 ::aos::Init(-1);
350 ::aos::ShmEventLoop event_loop;
351 ::y2019::input::joysticks::Reader reader(&event_loop);
352 reader.Run();
353 ::aos::Cleanup();
354}