blob: 59684ed2d8c5ff12646c1036221eb2606d428c92 [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"
21
22#include "y2019/control_loops/drivetrain/drivetrain_base.h"
23#include "y2019/control_loops/superstructure/superstructure.q.h"
Sabina Davisc6329342019-03-01 20:44:42 -080024#include "y2019/status_light.q.h"
Tyler Chatowe0241452019-03-08 21:07:50 -080025#include "y2019/vision.pb.h"
Sabina Davis91b23602019-01-21 00:06:01 -080026
27using ::y2019::control_loops::superstructure::superstructure_queue;
28using ::aos::input::driver_station::ButtonLocation;
29using ::aos::input::driver_station::ControlBit;
30using ::aos::input::driver_station::JoystickAxis;
31using ::aos::input::driver_station::POVLocation;
Tyler Chatowe0241452019-03-08 21:07:50 -080032using ::aos::events::ProtoTXUdpSocket;
Sabina Davis91b23602019-01-21 00:06:01 -080033
34namespace y2019 {
35namespace input {
36namespace joysticks {
37
Tyler Chatowe0241452019-03-08 21:07:50 -080038using google::protobuf::StringPrintf;
39
Austin Schuh1a17e132019-02-17 15:05:06 -080040const ButtonLocation kSuctionBall(3, 13);
41const ButtonLocation kSuctionHatch(3, 12);
42const ButtonLocation kDeployStilt(3, 8);
43const ButtonLocation kFallOver(3, 9);
Austin Schuh2cf16b82019-02-15 23:23:22 -080044
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080045struct ElevatorWristPosition {
46 double elevator;
47 double wrist;
48};
Sabina Davis91b23602019-01-21 00:06:01 -080049
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080050const ButtonLocation kRocketForwardLower(5, 1);
51const ButtonLocation kRocketForwardMiddle(5, 2);
52const ButtonLocation kRocketForwardUpper(5, 4);
53const ButtonLocation kCargoForward(5, 3);
54
55const POVLocation kRocketBackwardUnpressed(5, -1);
56const POVLocation kRocketBackwardLower(5, 180);
57const POVLocation kRocketBackwardMiddle(5, 90);
58const POVLocation kRocketBackwardUpper(5, 0);
59const POVLocation kCargoBackward(5, 270);
60
61const ButtonLocation kPanelSwitch(5, 7);
62const ButtonLocation kCargoSwitch(5, 8);
63
64const ButtonLocation kBallHPIntakeForward(5, 6);
65const ButtonLocation kBallHPIntakeBackward(5, 5);
66const JoystickAxis kBallOutake(5, 3);
67const JoystickAxis kBallIntake(5, 4);
68
69const ButtonLocation kPanelHPIntakeForward(5, 6);
70const ButtonLocation kPanelHPIntakeBackward(5, 5);
71
72const ButtonLocation kRelease(2, 4);
73
74const ElevatorWristPosition kStowPos{0.36, 0.0};
75
76const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.04, M_PI / 2.0};
Sabina Davise6fe6c52019-03-03 15:48:51 -080077const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.05, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080078
79const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0};
80const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0};
81
Sabina Davise48004f2019-03-02 23:15:24 -080082const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0};
83const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080084
Sabina Davise48004f2019-03-02 23:15:24 -080085const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0};
86const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080087
Sabina Davise6fe6c52019-03-03 15:48:51 -080088const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0};
89const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0};
90
91const ElevatorWristPosition kBallForwardLowerPos{0.46, M_PI / 2.0};
92const ElevatorWristPosition kBallBackwardLowerPos{0.15, -M_PI / 2.0};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080093
Sabina Davise48004f2019-03-02 23:15:24 -080094const ElevatorWristPosition kBallForwardMiddlePos{1.16, 1.546};
95const ElevatorWristPosition kBallBackwardMiddlePos{0.876021, -1.546};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080096
Sabina Davise48004f2019-03-02 23:15:24 -080097const ElevatorWristPosition kBallForwardUpperPos{1.50, 0.961};
98const ElevatorWristPosition kBallBackwardUpperPos{1.41, -1.217};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -080099
Sabina Davise48004f2019-03-02 23:15:24 -0800100const ElevatorWristPosition kBallCargoForwardPos{0.699044, 1.353};
101const ElevatorWristPosition kBallCargoBackwardPos{0.828265, -1.999};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800102
Sabina Davise6fe6c52019-03-03 15:48:51 -0800103const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097};
104const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018};
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800105
106const ElevatorWristPosition kBallIntakePos{0.29, 2.14};
Austin Schuh2cf16b82019-02-15 23:23:22 -0800107
Sabina Davis91b23602019-01-21 00:06:01 -0800108class Reader : public ::aos::input::ActionJoystickInput {
109 public:
110 Reader(::aos::EventLoop *event_loop)
111 : ::aos::input::ActionJoystickInput(
112 event_loop,
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800113 ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {
Tyler Chatowe0241452019-03-08 21:07:50 -0800114 const uint16_t team = ::aos::network::GetTeamNumber();
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800115 superstructure_queue.goal.FetchLatest();
116 if (superstructure_queue.goal.get()) {
Sabina Davisc6329342019-03-01 20:44:42 -0800117 grab_piece_ = superstructure_queue.goal->suction.grab_piece;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800118 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800119 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
120 StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000));
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800121 }
Sabina Davis91b23602019-01-21 00:06:01 -0800122
123 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
124 superstructure_queue.position.FetchLatest();
125 superstructure_queue.status.FetchLatest();
126 if (!superstructure_queue.status.get() ||
127 !superstructure_queue.position.get()) {
128 LOG(ERROR, "Got no superstructure status packet.\n");
129 return;
130 }
131
132 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
133
Austin Schuh1a17e132019-02-17 15:05:06 -0800134 if (data.IsPressed(kSuctionBall)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800135 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800136 } else if (data.IsPressed(kSuctionHatch)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800137 grab_piece_ = true;
Austin Schuh1a17e132019-02-17 15:05:06 -0800138 } else if (data.IsPressed(kRelease) ||
139 !superstructure_queue.status->has_piece) {
Sabina Davisc6329342019-03-01 20:44:42 -0800140 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800141 }
Sabina Davis91b23602019-01-21 00:06:01 -0800142
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800143 if (data.IsPressed(kRocketBackwardUnpressed)) {
144 elevator_wrist_pos_ = kStowPos;
145 }
146 new_superstructure_goal->intake.unsafe_goal = -1.2;
147 new_superstructure_goal->roller_voltage = 0.0;
148
149 const bool kDoBallIntake = data.GetAxis(kBallIntake) > 0.9;
150 const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9;
151
152 if (data.IsPressed(kPanelSwitch)) {
153 switch_ball_ = false;
154 } else if (data.IsPressed(kCargoSwitch)) {
155 switch_ball_ = true;
156 }
157
Sabina Davis91b23602019-01-21 00:06:01 -0800158 // TODO(sabina): max height please?
Austin Schuh77ac3212019-02-19 16:50:14 -0800159 if (data.IsPressed(kFallOver)) {
Austin Schuh1a17e132019-02-17 15:05:06 -0800160 new_superstructure_goal->stilts.unsafe_goal = 0.71;
Michael Schuh587dcb52019-02-28 21:31:03 -0800161 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800162 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Austin Schuh77ac3212019-02-19 16:50:14 -0800163 } else if (data.IsPressed(kDeployStilt)) {
164 new_superstructure_goal->stilts.unsafe_goal = 0.50;
Michael Schuh587dcb52019-02-28 21:31:03 -0800165 new_superstructure_goal->stilts.profile_params.max_velocity = 0.65;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800166 if (stilts_was_above_) {
167 new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75;
168 } else {
169 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
170 }
Sabina Davis91b23602019-01-21 00:06:01 -0800171 } else {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800172 new_superstructure_goal->stilts.unsafe_goal = 0.01;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800173 new_superstructure_goal->stilts.profile_params.max_velocity = 0.25;
174 new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800175 }
176
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800177 if (superstructure_queue.status->stilts.position > 0.65) {
178 stilts_was_above_ = true;
179 } else if (superstructure_queue.status->stilts.position < 0.1) {
180 stilts_was_above_ = false;
Austin Schuh2cf16b82019-02-15 23:23:22 -0800181 }
182
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800183 if (switch_ball_) {
184 if (superstructure_queue.status->has_piece) {
185 new_superstructure_goal->wrist.profile_params.max_acceleration = 20;
Austin Schuh1a17e132019-02-17 15:05:06 -0800186 }
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800187
188 // Go to intake position and apply vacuum
189 if (data.IsPressed(kBallHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800190 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800191 elevator_wrist_pos_ = kBallHPIntakeForwardPos;
192 } else if (data.IsPressed(kBallHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800193 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800194 elevator_wrist_pos_ = kBallHPIntakeBackwardPos;
195 }
196
197 // Go to elevator/wrist position. Overrides intake position if pressed so
198 // we can re-grab the ball.
199 if (data.IsPressed(kRocketForwardLower)) {
200 elevator_wrist_pos_ = kBallForwardLowerPos;
201 } else if (data.IsPressed(kRocketBackwardLower)) {
202 elevator_wrist_pos_ = kBallBackwardLowerPos;
203 } else if (data.IsPressed(kRocketForwardMiddle)) {
204 elevator_wrist_pos_ = kBallForwardMiddlePos;
205 } else if (data.IsPressed(kRocketBackwardMiddle)) {
206 elevator_wrist_pos_ = kBallBackwardMiddlePos;
207 } else if (data.IsPressed(kRocketForwardUpper)) {
208 elevator_wrist_pos_ = kBallForwardUpperPos;
209 } else if (data.IsPressed(kRocketBackwardUpper)) {
210 elevator_wrist_pos_ = kBallBackwardUpperPos;
211 } else if (data.IsPressed(kCargoForward)) {
212 elevator_wrist_pos_ = kBallCargoForwardPos;
213 } else if (data.IsPressed(kCargoBackward)) {
214 elevator_wrist_pos_ = kBallCargoBackwardPos;
215 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800216 } else {
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800217 if (data.IsPressed(kPanelHPIntakeForward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800218 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800219 elevator_wrist_pos_ = kPanelHPIntakeForwrdPos;
220 } else if (data.IsPressed(kPanelHPIntakeBackward)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800221 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800222 elevator_wrist_pos_ = kPanelHPIntakeBackwardPos;
223 }
224
225 // Go to elevator/wrist position. Overrides intake position if pressed so
226 // we can re-grab the panel.
227 if (data.IsPressed(kRocketForwardLower)) {
228 elevator_wrist_pos_ = kPanelForwardLowerPos;
229 } else if (data.IsPressed(kRocketBackwardLower)) {
230 elevator_wrist_pos_ = kPanelBackwardLowerPos;
231 } else if (data.IsPressed(kRocketForwardMiddle)) {
232 elevator_wrist_pos_ = kPanelForwardMiddlePos;
233 } else if (data.IsPressed(kRocketBackwardMiddle)) {
234 elevator_wrist_pos_ = kPanelBackwardMiddlePos;
235 } else if (data.IsPressed(kRocketForwardUpper)) {
236 elevator_wrist_pos_ = kPanelForwardUpperPos;
237 } else if (data.IsPressed(kRocketBackwardUpper)) {
238 elevator_wrist_pos_ = kPanelBackwardUpperPos;
Sabina Davise6fe6c52019-03-03 15:48:51 -0800239 } else if (data.IsPressed(kCargoForward)) {
240 elevator_wrist_pos_ = kPanelCargoForwardPos;
241 } else if (data.IsPressed(kCargoBackward)) {
242 elevator_wrist_pos_ = kPanelCargoBackwardPos;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800243 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800244 }
245
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800246 if (switch_ball_) {
247 if (kDoBallOutake ||
248 (kDoBallIntake && !superstructure_queue.status->has_piece)) {
249 new_superstructure_goal->intake.unsafe_goal = 0.959327;
250 }
Austin Schuh23a51632019-02-19 16:50:36 -0800251
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800252 if (kDoBallIntake && !superstructure_queue.status->has_piece) {
253 elevator_wrist_pos_ = kBallIntakePos;
254 new_superstructure_goal->roller_voltage = 9.0;
Sabina Davisc6329342019-03-01 20:44:42 -0800255 grab_piece_ = true;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800256 } else {
257 if (kDoBallOutake) {
258 new_superstructure_goal->roller_voltage = -6.0;
259 } else {
260 new_superstructure_goal->intake.unsafe_goal = -1.2;
261 new_superstructure_goal->roller_voltage = 0.0;
262 }
263 }
Austin Schuh23a51632019-02-19 16:50:36 -0800264 }
Austin Schuh1a17e132019-02-17 15:05:06 -0800265
266 if (data.IsPressed(kRelease)) {
Sabina Davisc6329342019-03-01 20:44:42 -0800267 grab_piece_ = false;
Austin Schuh1a17e132019-02-17 15:05:06 -0800268 }
269
Sabina Davisc6329342019-03-01 20:44:42 -0800270 if (switch_ball_) {
271 new_superstructure_goal->suction.gamepiece_mode = 0;
272 } else {
273 new_superstructure_goal->suction.gamepiece_mode = 1;
274 }
275
Tyler Chatowe0241452019-03-08 21:07:50 -0800276 vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0);
277
Sabina Davisc6329342019-03-01 20:44:42 -0800278 new_superstructure_goal->suction.grab_piece = grab_piece_;
Sabina Davis91b23602019-01-21 00:06:01 -0800279
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800280 new_superstructure_goal->elevator.unsafe_goal =
281 elevator_wrist_pos_.elevator;
282 new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist;
Sabina Davis91b23602019-01-21 00:06:01 -0800283
284 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
285 if (!new_superstructure_goal.Send()) {
286 LOG(ERROR, "Sending superstructure goal failed.\n");
287 }
Tyler Chatowe0241452019-03-08 21:07:50 -0800288
289 video_tx_->Send(vision_control_);
Sabina Davis91b23602019-01-21 00:06:01 -0800290 }
291
292 private:
293 // Current goals here.
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800294 ElevatorWristPosition elevator_wrist_pos_ = kStowPos;
Sabina Davisc6329342019-03-01 20:44:42 -0800295 bool grab_piece_ = false;
Tyler Chatow7bcb52f2019-02-24 00:16:54 -0800296
297 bool switch_ball_ = false;
298 bool stilts_was_above_ = false;
Tyler Chatowe0241452019-03-08 21:07:50 -0800299
300 VisionControl vision_control_;
301 ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_;
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}