blob: fcdf51fe1e1bcff91c2f48b4476ffa83602be672 [file] [log] [blame]
Neil Balchacfca5b2018-01-28 14:04:08 -08001#include <math.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
5
6#include "aos/common/actions/actions.h"
7#include "aos/common/input/driver_station_data.h"
8#include "aos/common/logging/logging.h"
9#include "aos/common/time.h"
10#include "aos/common/util/log_interval.h"
11#include "aos/input/drivetrain_input.h"
12#include "aos/input/joystick_input.h"
13#include "aos/linux_code/init.h"
Austin Schuha3c148e2018-03-09 21:04:05 -080014#include "frc971/autonomous/auto.q.h"
15#include "frc971/autonomous/base_autonomous_actor.h"
Neil Balchacfca5b2018-01-28 14:04:08 -080016#include "frc971/control_loops/drivetrain/drivetrain.q.h"
17#include "y2018/control_loops/drivetrain/drivetrain_base.h"
Austin Schuhab15c4d2018-03-09 21:21:03 -080018#include "y2018/control_loops/superstructure/arm/generated_graph.h"
Neil Balch07fee582018-01-27 15:46:49 -080019#include "y2018/control_loops/superstructure/superstructure.q.h"
Austin Schuh1e2d4982018-03-21 20:34:33 -070020#include "y2018/status_light.q.h"
Neil Balchacfca5b2018-01-28 14:04:08 -080021
22using ::frc971::control_loops::drivetrain_queue;
Neil Balch07fee582018-01-27 15:46:49 -080023using ::y2018::control_loops::superstructure_queue;
Neil Balchacfca5b2018-01-28 14:04:08 -080024
25using ::aos::input::driver_station::ButtonLocation;
26using ::aos::input::driver_station::ControlBit;
27using ::aos::input::driver_station::JoystickAxis;
28using ::aos::input::driver_station::POVLocation;
29using ::aos::input::DrivetrainInputReader;
30
Austin Schuh60cdb3e2018-04-06 21:52:32 -070031using ::y2018::control_loops::superstructure::arm::FrontPoints;
32using ::y2018::control_loops::superstructure::arm::BackPoints;
33
Neil Balchacfca5b2018-01-28 14:04:08 -080034namespace y2018 {
35namespace input {
36namespace joysticks {
37
Austin Schuhab15c4d2018-03-09 21:21:03 -080038namespace arm = ::y2018::control_loops::superstructure::arm;
Neil Balch07fee582018-01-27 15:46:49 -080039
Sabina Davis833ccf62018-04-06 20:52:31 -070040const ButtonLocation kIntakeClosed(3, 2);
41const ButtonLocation kDuck(3, 9);
42const ButtonLocation kSmallBox(3, 1);
Austin Schuh47d74942018-03-04 01:15:59 -080043
Sabina Davis833ccf62018-04-06 20:52:31 -070044const ButtonLocation kIntakeIn(3, 4);
45const ButtonLocation kIntakeOut(3, 3);
Neil Balch07fee582018-01-27 15:46:49 -080046
Sabina Davis833ccf62018-04-06 20:52:31 -070047const ButtonLocation kArmFrontHighBox(4, 11);
48const ButtonLocation kArmFrontExtraHighBox(4, 1);
49const ButtonLocation kArmFrontMiddle2Box(4, 9);
50const ButtonLocation kArmFrontMiddle1Box(4, 7);
51const ButtonLocation kArmFrontLowBox(4, 5);
52const ButtonLocation kArmFrontSwitch(3, 7);
Austin Schuhab15c4d2018-03-09 21:21:03 -080053
Sabina Davis833ccf62018-04-06 20:52:31 -070054const ButtonLocation kArmBackHighBox(4, 12);
55const ButtonLocation kArmBackExtraHighBox(3, 14);
56const ButtonLocation kArmBackMiddle2Box(4, 10);
57const ButtonLocation kArmBackMiddle1Box(4, 8);
58const ButtonLocation kArmBackLowBox(4, 6);
59const ButtonLocation kArmBackSwitch(3, 10);
Austin Schuhab15c4d2018-03-09 21:21:03 -080060
Sabina Davis833ccf62018-04-06 20:52:31 -070061const ButtonLocation kArmAboveHang(3, 15);
62const ButtonLocation kArmBelowHang(3, 16);
Austin Schuh17e484e2018-03-11 01:11:36 -080063
Sabina Davis833ccf62018-04-06 20:52:31 -070064const ButtonLocation kWinch(4, 2);
Austin Schuh17e484e2018-03-11 01:11:36 -080065
Sabina Davis833ccf62018-04-06 20:52:31 -070066const ButtonLocation kArmNeutral(3, 8);
67const ButtonLocation kArmUp(3, 11);
Austin Schuhab15c4d2018-03-09 21:21:03 -080068
Sabina Davis833ccf62018-04-06 20:52:31 -070069const ButtonLocation kArmStepUp(3, 13);
70const ButtonLocation kArmStepDown(3, 12);
Austin Schuhab15c4d2018-03-09 21:21:03 -080071
Sabina Davis833ccf62018-04-06 20:52:31 -070072const ButtonLocation kArmPickupBoxFromIntake(4, 3);
73
74const ButtonLocation kClawOpen(4, 4);
Neil Balchba9cbba2018-04-06 22:26:38 -070075const ButtonLocation kDriverClawOpen(2, 4);
Neil Balch07fee582018-01-27 15:46:49 -080076
Neil Balchacfca5b2018-01-28 14:04:08 -080077std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
78
79class Reader : public ::aos::input::JoystickInput {
80 public:
81 Reader() {
82 drivetrain_input_reader_ = DrivetrainInputReader::Make(
Austin Schuh2b1fce02018-03-02 20:05:20 -080083 DrivetrainInputReader::InputType::kPistol,
Neil Balchacfca5b2018-01-28 14:04:08 -080084 ::y2018::control_loops::drivetrain::GetDrivetrainConfig());
85 }
86
87 void RunIteration(const ::aos::input::driver_station::Data &data) override {
88 bool last_auto_running = auto_running_;
89 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
90 data.GetControlBit(ControlBit::kEnabled);
91 if (auto_running_ != last_auto_running) {
92 if (auto_running_) {
93 StartAuto();
94 } else {
95 StopAuto();
96 }
97 }
98
99 if (!auto_running_) {
100 HandleDrivetrain(data);
101 HandleTeleop(data);
102 }
103
104 // Process any pending actions.
105 action_queue_.Tick();
106 was_running_ = action_queue_.Running();
107 }
108
109 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
110 drivetrain_input_reader_->HandleDrivetrain(data);
Neil Balchacfca5b2018-01-28 14:04:08 -0800111 }
112
Austin Schuh1e2d4982018-03-21 20:34:33 -0700113 void SendColors(float red, float green, float blue) {
114 auto new_status_light = status_light.MakeMessage();
115 new_status_light->red = red;
116 new_status_light->green = green;
117 new_status_light->blue = blue;
118
119 if (!new_status_light.Send()) {
120 LOG(ERROR, "Failed to send lights.\n");
121 }
122 }
123
Neil Balchacfca5b2018-01-28 14:04:08 -0800124 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
125 if (!data.GetControlBit(ControlBit::kEnabled)) {
126 action_queue_.CancelAllActions();
127 LOG(DEBUG, "Canceling\n");
128 }
Neil Balch07fee582018-01-27 15:46:49 -0800129
Austin Schuhab15c4d2018-03-09 21:21:03 -0800130 superstructure_queue.position.FetchLatest();
Neil Balch07fee582018-01-27 15:46:49 -0800131 superstructure_queue.status.FetchLatest();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800132 if (!superstructure_queue.status.get() ||
133 !superstructure_queue.position.get()) {
Neil Balch07fee582018-01-27 15:46:49 -0800134 LOG(ERROR, "Got no superstructure status packet.\n");
135 return;
136 }
137
Neil Balch07fee582018-01-27 15:46:49 -0800138 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
139
Sabina Davisfdd7a112018-02-04 16:16:23 -0800140 new_superstructure_goal->intake.left_intake_angle = intake_goal_;
141 new_superstructure_goal->intake.right_intake_angle = intake_goal_;
Neil Balch07fee582018-01-27 15:46:49 -0800142
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700143 if (data.IsPressed(kIntakeIn)) {
Neil Balch07fee582018-01-27 15:46:49 -0800144 // Turn on the rollers.
Neil Balchba9cbba2018-04-06 22:26:38 -0700145 new_superstructure_goal->intake.roller_voltage = 8.0;
Neil Balch07fee582018-01-27 15:46:49 -0800146 } else if (data.IsPressed(kIntakeOut)) {
147 // Turn off the rollers.
Austin Schuh17dd0892018-03-02 20:06:31 -0800148 new_superstructure_goal->intake.roller_voltage = -12.0;
Neil Balch07fee582018-01-27 15:46:49 -0800149 } else {
150 // We don't want the rollers on.
151 new_superstructure_goal->intake.roller_voltage = 0.0;
152 }
153
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700154 if (superstructure_queue.position->box_back_beambreak_triggered) {
155 SendColors(0.0, 0.5, 0.0);
156 } else if (superstructure_queue.position->box_distance < 0.2) {
157 SendColors(0.0, 0.0, 0.5);
158 } else {
159 SendColors(0.0, 0.0, 0.0);
160 }
161
162 if (data.IsPressed(kSmallBox)) {
163 // Deploy the intake.
164 if (superstructure_queue.position->box_back_beambreak_triggered) {
165 intake_goal_ = 0.30;
166 } else {
167 if (new_superstructure_goal->intake.roller_voltage > 0.1 &&
168 superstructure_queue.position->box_distance < 0.15) {
169 intake_goal_ = 0.18;
170 } else {
171 intake_goal_ = -0.60;
172 }
173 }
174 } else if (data.IsPressed(kIntakeClosed)) {
175 // Deploy the intake.
176 if (superstructure_queue.position->box_back_beambreak_triggered) {
177 intake_goal_ = 0.30;
178 } else {
179 if (new_superstructure_goal->intake.roller_voltage > 0.1) {
Neil Balchba9cbba2018-04-06 22:26:38 -0700180 if (superstructure_queue.position->box_distance < 0.10) {
181 new_superstructure_goal->intake.roller_voltage -= 3.0;
182 intake_goal_ = 0.22;
183 } else if (superstructure_queue.position->box_distance < 0.17) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700184 intake_goal_ = 0.13;
185 } else if (superstructure_queue.position->box_distance < 0.25) {
Neil Balchba9cbba2018-04-06 22:26:38 -0700186 intake_goal_ = 0.05;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700187 } else {
Neil Balchba9cbba2018-04-06 22:26:38 -0700188 intake_goal_ = -0.10;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700189 }
190 } else {
191 intake_goal_ = -0.60;
192 }
193 }
194 } else {
195 // Bring in the intake.
196 intake_goal_ = -3.3;
197 }
198
Neil Balchba9cbba2018-04-06 22:26:38 -0700199 if (new_superstructure_goal->intake.roller_voltage > 0.1 &&
200 intake_goal_ > 0.0) {
201 if (superstructure_queue.position->box_distance < 0.10) {
202 new_superstructure_goal->intake.roller_voltage -= 3.0;
203 }
204 new_superstructure_goal->intake.roller_voltage += 3.0;
205 }
206
Austin Schuhb874fd32018-03-05 00:27:10 -0800207 // If we are disabled, stay at the node closest to where we start. This
208 // should remove long motions when enabled.
Neil Balchba9cbba2018-04-06 22:26:38 -0700209 if (!data.GetControlBit(ControlBit::kEnabled) || never_disabled_) {
Austin Schuhb874fd32018-03-05 00:27:10 -0800210 arm_goal_position_ = superstructure_queue.status->arm.current_node;
Neil Balchba9cbba2018-04-06 22:26:38 -0700211 never_disabled_ = false;
Austin Schuhb874fd32018-03-05 00:27:10 -0800212 }
213
Austin Schuhab15c4d2018-03-09 21:21:03 -0800214 bool grab_box = false;
215 if (data.IsPressed(kArmPickupBoxFromIntake)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800216 grab_box = true;
Neil Balchba9cbba2018-04-06 22:26:38 -0700217 }
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700218 const bool near_goal =
219 superstructure_queue.status->arm.current_node == arm_goal_position_ &&
220 superstructure_queue.status->arm.path_distance_to_go < 1e-3;
221 if (data.PosEdge(kArmStepDown) && near_goal) {
222 uint32_t *front_point = ::std::find(
223 front_points_.begin(), front_points_.end(), arm_goal_position_);
224 uint32_t *back_point = ::std::find(
225 back_points_.begin(), back_points_.end(), arm_goal_position_);
226 LOG(INFO, "Step up\n");
227 if (front_point != front_points_.end()) {
228 LOG(INFO, "In the front list, %d\n",
229 static_cast<int>(
230 ::std::distance(front_points_.begin(), front_point)));
231 ++front_point;
232 if (front_point != front_points_.end()) {
233 LOG(INFO, "Incrementing front\n");
234 arm_goal_position_ = *front_point;
235 }
236 } else if (back_point != back_points_.end()) {
237 LOG(INFO, "In the back list, %d\n",
238 static_cast<int>(
239 ::std::distance(back_points_.begin(), back_point)));
240 ++back_point;
241 if (back_point != back_points_.end()) {
242 LOG(INFO, "Incrementing back\n");
243 arm_goal_position_ = *back_point;
244 }
245 }
246 } else if (data.PosEdge(kArmStepUp) && near_goal) {
247 const uint32_t *front_point = ::std::find(
248 front_points_.begin(), front_points_.end(), arm_goal_position_);
249 const uint32_t *back_point = ::std::find(
250 back_points_.begin(), back_points_.end(), arm_goal_position_);
251 if (front_point != front_points_.end()) {
252 if (front_point != front_points_.begin()) {
253 --front_point;
254 arm_goal_position_ = *front_point;
255 }
256 } else if (back_point != back_points_.end()) {
257 if (back_point != back_points_.begin()) {
258 --back_point;
259 arm_goal_position_ = *back_point;
260 }
261 }
262 } else if (data.PosEdge(kArmPickupBoxFromIntake)) {
Neil Balchba9cbba2018-04-06 22:26:38 -0700263 arm_goal_position_ = arm::NeutralIndex();
264 } else if (data.IsPressed(kDuck)) {
265 arm_goal_position_ = arm::DuckIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800266 } else if (data.IsPressed(kArmNeutral)) {
267 arm_goal_position_ = arm::NeutralIndex();
268 } else if (data.IsPressed(kArmUp)) {
269 arm_goal_position_ = arm::UpIndex();
270 } else if (data.IsPressed(kArmFrontSwitch)) {
271 arm_goal_position_ = arm::FrontSwitchIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700272 } else if (data.IsPressed(kArmFrontExtraHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800273 arm_goal_position_ = arm::FrontHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700274 } else if (data.IsPressed(kArmFrontHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800275 arm_goal_position_ = arm::FrontMiddle2BoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700276 } else if (data.IsPressed(kArmFrontMiddle2Box)) {
277 arm_goal_position_ = arm::FrontMiddle3BoxIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800278 } else if (data.IsPressed(kArmFrontMiddle1Box)) {
279 arm_goal_position_ = arm::FrontMiddle1BoxIndex();
280 } else if (data.IsPressed(kArmFrontLowBox)) {
281 arm_goal_position_ = arm::FrontLowBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700282 } else if (data.IsPressed(kArmBackExtraHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800283 arm_goal_position_ = arm::BackHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700284 } else if (data.IsPressed(kArmBackHighBox) ||
285 data.IsPressed(kArmBackMiddle2Box)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800286 arm_goal_position_ = arm::BackMiddle2BoxIndex();
287 } else if (data.IsPressed(kArmBackMiddle1Box)) {
288 arm_goal_position_ = arm::BackMiddle1BoxIndex();
289 } else if (data.IsPressed(kArmBackLowBox)) {
290 arm_goal_position_ = arm::BackLowBoxIndex();
291 } else if (data.IsPressed(kArmBackSwitch)) {
292 arm_goal_position_ = arm::BackSwitchIndex();
Austin Schuh17e484e2018-03-11 01:11:36 -0800293 } else if (data.IsPressed(kArmAboveHang)) {
294 if (data.IsPressed(kIntakeIn)) {
295 arm_goal_position_ = arm::SelfHangIndex();
296 } else if (data.IsPressed(kIntakeOut)) {
297 arm_goal_position_ = arm::PartnerHangIndex();
298 } else {
299 arm_goal_position_ = arm::AboveHangIndex();
300 }
301 } else if (data.IsPressed(kArmBelowHang)) {
302 arm_goal_position_ = arm::BelowHangIndex();
Neil Balch07fee582018-01-27 15:46:49 -0800303 }
304
Austin Schuh17e484e2018-03-11 01:11:36 -0800305 new_superstructure_goal->deploy_fork =
306 data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen);
307
308 if (new_superstructure_goal->deploy_fork) {
309 intake_goal_ = -2.0;
310 }
311
312 if (data.IsPressed(kWinch)) {
Neil Balchba9cbba2018-04-06 22:26:38 -0700313 LOG(INFO, "Winching\n");
Austin Schuh17e484e2018-03-11 01:11:36 -0800314 new_superstructure_goal->voltage_winch = 12.0;
315 } else {
316 new_superstructure_goal->voltage_winch = 0.0;
317 }
318
319 new_superstructure_goal->hook_release = data.IsPressed(kArmBelowHang);
320
Austin Schuhcb091712018-02-21 20:01:55 -0800321 new_superstructure_goal->arm_goal_position = arm_goal_position_;
322
Neil Balchba9cbba2018-04-06 22:26:38 -0700323 if ((data.IsPressed(kClawOpen) && data.IsPressed(kDriverClawOpen)) ||
324 data.PosEdge(kArmPickupBoxFromIntake)) {
Neil Balch07fee582018-01-27 15:46:49 -0800325 new_superstructure_goal->open_claw = true;
Austin Schuhab15c4d2018-03-09 21:21:03 -0800326 } else {
Neil Balch07fee582018-01-27 15:46:49 -0800327 new_superstructure_goal->open_claw = false;
328 }
329
Austin Schuhab15c4d2018-03-09 21:21:03 -0800330 new_superstructure_goal->grab_box = grab_box;
Neil Balch07fee582018-01-27 15:46:49 -0800331
332 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
333 if (!new_superstructure_goal.Send()) {
334 LOG(ERROR, "Sending superstructure goal failed.\n");
335 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800336 }
337
338 private:
Austin Schuha3c148e2018-03-09 21:04:05 -0800339 void StartAuto() {
340 LOG(INFO, "Starting auto mode\n");
341
342 ::frc971::autonomous::AutonomousActionParams params;
343 ::frc971::autonomous::auto_mode.FetchLatest();
344 if (::frc971::autonomous::auto_mode.get() != nullptr) {
Austin Schuhc231df42018-03-21 20:43:24 -0700345 params.mode = ::frc971::autonomous::auto_mode->mode << 2;
Austin Schuha3c148e2018-03-09 21:04:05 -0800346 } else {
347 LOG(WARNING, "no auto mode values\n");
348 params.mode = 0;
349 }
Austin Schuhc231df42018-03-21 20:43:24 -0700350 // TODO(austin): use the mode later if we care. We don't care right now.
351 params.mode = static_cast<int>(::aos::joystick_state->switch_left) |
352 (static_cast<int>(::aos::joystick_state->scale_left) << 1);
Austin Schuha3c148e2018-03-09 21:04:05 -0800353 action_queue_.EnqueueAction(
354 ::frc971::autonomous::MakeAutonomousAction(params));
355 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800356
357 void StopAuto() {
358 LOG(INFO, "Stopping auto mode\n");
359 action_queue_.CancelAllActions();
360 }
361
Neil Balch07fee582018-01-27 15:46:49 -0800362 // Current goals to send to the robot.
Austin Schuh17e484e2018-03-11 01:11:36 -0800363 double intake_goal_ = 0.0;
Neil Balch07fee582018-01-27 15:46:49 -0800364
Neil Balchacfca5b2018-01-28 14:04:08 -0800365 bool was_running_ = false;
366 bool auto_running_ = false;
Neil Balchba9cbba2018-04-06 22:26:38 -0700367 bool never_disabled_ = true;
Neil Balchacfca5b2018-01-28 14:04:08 -0800368
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700369 uint32_t arm_goal_position_ = 0;
370
371 decltype(FrontPoints()) front_points_ = FrontPoints();
372 decltype(BackPoints()) back_points_ = BackPoints();
Austin Schuhcb091712018-02-21 20:01:55 -0800373
Neil Balchacfca5b2018-01-28 14:04:08 -0800374 ::aos::common::actions::ActionQueue action_queue_;
375};
376
377} // namespace joysticks
378} // namespace input
379} // namespace y2018
380
381int main() {
382 ::aos::Init(-1);
383 ::y2018::input::joysticks::Reader reader;
384 reader.Run();
385 ::aos::Cleanup();
386}