blob: f1d71fdce3fc00dcb5f9fc504916c57d7d2cd24f [file] [log] [blame]
Brian Silverman20141f92015-01-05 17:39:01 -08001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <math.h>
5
6#include "aos/linux_code/init.h"
7#include "aos/prime/input/joystick_input.h"
8#include "aos/common/input/driver_station_data.h"
9#include "aos/common/logging/logging.h"
10#include "aos/common/util/log_interval.h"
11#include "aos/common/time.h"
Ben Fredricksond69f38b2015-01-28 20:06:15 -080012#include "aos/common/actions/actions.h"
Brian Silverman20141f92015-01-05 17:39:01 -080013
Daniel Petti61896522015-02-15 18:01:43 -080014#include "frc971/control_loops/claw/claw.q.h"
Brian Silverman20141f92015-01-05 17:39:01 -080015#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Daniel Petti61896522015-02-15 18:01:43 -080016#include "frc971/control_loops/fridge/fridge.q.h"
Brian Silverman20141f92015-01-05 17:39:01 -080017#include "frc971/constants.h"
18#include "frc971/queues/gyro.q.h"
19#include "frc971/autonomous/auto.q.h"
Austin Schuhd8b2a242015-02-22 21:46:53 -080020#include "frc971/actors/pickup_actor.h"
21#include "frc971/actors/stack_actor.h"
Brian Silverman10043572015-03-21 23:43:50 -070022#include "frc971/actors/score_actor.h"
Austin Schuhf14727e2015-03-08 18:49:12 -070023#include "frc971/actors/stack_and_lift_actor.h"
24#include "frc971/actors/stack_and_hold_actor.h"
25#include "frc971/actors/held_to_lift_actor.h"
Austin Schuhd8b2a242015-02-22 21:46:53 -080026#include "frc971/actors/lift_actor.h"
Austin Schuh994d42c2015-03-01 00:02:17 -080027#include "frc971/actors/can_pickup_actor.h"
Austin Schuh700b9222015-03-01 03:03:15 -080028#include "frc971/actors/horizontal_can_pickup_actor.h"
Brian Silverman20141f92015-01-05 17:39:01 -080029
Daniel Petti61896522015-02-15 18:01:43 -080030using ::frc971::control_loops::claw_queue;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050031using ::frc971::control_loops::drivetrain_queue;
Daniel Petti61896522015-02-15 18:01:43 -080032using ::frc971::control_loops::fridge_queue;
Brian Silverman20141f92015-01-05 17:39:01 -080033using ::frc971::sensors::gyro_reading;
34
35using ::aos::input::driver_station::ButtonLocation;
Austin Schuhf14727e2015-03-08 18:49:12 -070036using ::aos::input::driver_station::POVLocation;
Brian Silverman20141f92015-01-05 17:39:01 -080037using ::aos::input::driver_station::JoystickAxis;
38using ::aos::input::driver_station::ControlBit;
39
40namespace frc971 {
41namespace input {
42namespace joysticks {
43
Austin Schuhf14727e2015-03-08 18:49:12 -070044// Actions needed.
45
46// Human Player Station Intake Button
47// Claw open
48// Claw close
49// Claw down
50
51// Stack + Lift (together)
52// Place
53
54// Hold stack
55
56// Horizontal can pickup
57// Vertical can pickup
58
59// TODO(austin): Pull a lot of the constants below out up here so they can be
60// globally changed easier.
61
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080062// preset motion limits
Brian Silvermancb615cf2015-03-21 21:42:35 -070063constexpr actors::ProfileParams kArmMove{0.5, 1.0};
64constexpr actors::ProfileParams kElevatorMove{0.3, 1.0};
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080065
Brian Silverman20141f92015-01-05 17:39:01 -080066const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
67const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
68const ButtonLocation kQuickTurn(1, 5);
69
Austin Schuhf14727e2015-03-08 18:49:12 -070070//const ButtonLocation kClawClosed(3, 5);
71//const ButtonLocation kFridgeClosed(3, 1);
Austin Schuhd8b2a242015-02-22 21:46:53 -080072
Daniel Petti61896522015-02-15 18:01:43 -080073
Austin Schuhf14727e2015-03-08 18:49:12 -070074const ButtonLocation kRollersIn(4, 5);
75const ButtonLocation kClawToggle(4, 1);
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080076
Brian Silverman10043572015-03-21 23:43:50 -070077const POVLocation kElevatorCanUp(4, 0);
Austin Schuhf14727e2015-03-08 18:49:12 -070078
79// POV stick 3, 180
80const POVLocation kCanPickup(4, 180);
81const ButtonLocation kToteChute(4, 6);
82const ButtonLocation kStackAndLift(4, 7);
Brian Silverman10043572015-03-21 23:43:50 -070083const ButtonLocation kStackAndHold(3, 5);
Austin Schuhf14727e2015-03-08 18:49:12 -070084
85// Pull in the 6th tote.
Brian Silverman10043572015-03-21 23:43:50 -070086//const ButtonLocation kSixthTote(4, 10);
87const ButtonLocation kCanUp(4, 10);
Austin Schuhf14727e2015-03-08 18:49:12 -070088
89const ButtonLocation kHeldToLift(4, 11);
90const ButtonLocation kPickup(4, 9);
91
92const ButtonLocation kStack(4, 2);
93
94// Move the fridge out with the stack in preparation for scoring.
Brian Silverman10043572015-03-21 23:43:50 -070095const ButtonLocation kScore(4, 8);
Brian Silverman913eafa2015-03-19 23:42:16 -070096const ButtonLocation kCoopTop(3, 8);
97const ButtonLocation kCoopTopRetract(3, 7);
98const ButtonLocation kCoopBottom(3, 6);
Brian Silvermand4278302015-03-28 00:21:15 -040099const ButtonLocation kCoopBottomRetract(4, 12);
Brian Silverman913eafa2015-03-19 23:42:16 -0700100
Brian Silvermand4278302015-03-28 00:21:15 -0400101const ButtonLocation kCanReset(3, 9);
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700102
Brian Silverman10043572015-03-21 23:43:50 -0700103const POVLocation kFridgeToggle(4, 270);
Austin Schuhf14727e2015-03-08 18:49:12 -0700104const ButtonLocation kSpit(4, 3);
105
106// Set stack down in the bot.
Brian Silverman10043572015-03-21 23:43:50 -0700107const POVLocation kSetStackDownAndHold(4, 90);
Austin Schuhf14727e2015-03-08 18:49:12 -0700108
Austin Schuh959d2022015-03-29 13:51:11 -0700109const double kClawTotePackAngle = 0.90;
Brian Silverman10043572015-03-21 23:43:50 -0700110const double kArmRaiseLowerClearance = -0.08;
Brian Silverman654b5122015-03-20 16:58:04 -0700111const double kClawStackClearance = 0.55;
Brian Silverman20141f92015-01-05 17:39:01 -0800112
Austin Schuh959d2022015-03-29 13:51:11 -0700113const double kStackUpHeight = 0.60;
114const double kStackUpArm = 0.0;
Brian Silverman697d07b2015-03-19 23:41:11 -0700115
Brian Silverman20141f92015-01-05 17:39:01 -0800116class Reader : public ::aos::input::JoystickInput {
117 public:
Austin Schuh331e13d2015-02-15 00:16:51 -0800118 Reader() : was_running_(false) {}
Brian Silverman20141f92015-01-05 17:39:01 -0800119
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700120 static actors::ScoreParams MakeScoreParams() {
Brian Silverman913eafa2015-03-19 23:42:16 -0700121 actors::ScoreParams r;
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700122 r.move_the_stack = r.place_the_stack = true;
Brian Silverman913eafa2015-03-19 23:42:16 -0700123 r.upper_move_height = 0.14;
124 r.begin_horizontal_move_height = 0.13;
125 r.horizontal_move_target = -0.7;
Austin Schuh45ee2c52015-03-29 13:47:26 -0700126 r.horizontal_start_lowering = -0.65;
127 r.home_lift_horizontal_start_position = -0.60;
Brian Silverman913eafa2015-03-19 23:42:16 -0700128 r.place_height = -0.10;
Austin Schuh45ee2c52015-03-29 13:47:26 -0700129 r.home_return_height = 0.05;
Brian Silverman913eafa2015-03-19 23:42:16 -0700130 return r;
131 }
132
133 static actors::ScoreParams MakeCoopTopParams(bool place_the_stack) {
134 actors::ScoreParams r;
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700135 r.move_the_stack = !place_the_stack;
Brian Silverman913eafa2015-03-19 23:42:16 -0700136 r.place_the_stack = place_the_stack;
137 r.upper_move_height = 0.52;
138 r.begin_horizontal_move_height = 0.5;
139 r.horizontal_move_target = -0.48;
Austin Schuh45ee2c52015-03-29 13:47:26 -0700140 r.horizontal_start_lowering = r.horizontal_move_target;
141 r.home_lift_horizontal_start_position = -0.3;
Brian Silverman913eafa2015-03-19 23:42:16 -0700142 r.place_height = 0.39;
143 r.home_return_height = 0.1;
144 return r;
145 }
146
147 static actors::ScoreParams MakeCoopBottomParams(bool place_the_stack) {
148 actors::ScoreParams r;
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700149 r.move_the_stack = !place_the_stack;
Brian Silverman913eafa2015-03-19 23:42:16 -0700150 r.place_the_stack = place_the_stack;
151 r.upper_move_height = 0.17;
152 r.begin_horizontal_move_height = 0.16;
153 r.horizontal_move_target = -0.7;
Austin Schuh45ee2c52015-03-29 13:47:26 -0700154 r.horizontal_start_lowering = r.horizontal_move_target;
155 r.home_lift_horizontal_start_position = -0.3;
Brian Silverman913eafa2015-03-19 23:42:16 -0700156 r.place_height = 0.0;
157 r.home_return_height = 0.1;
158 return r;
159 }
160
Brian Silverman20141f92015-01-05 17:39:01 -0800161 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Austin Schuh6182f8d2015-02-14 22:15:04 -0800162 bool last_auto_running = auto_running_;
163 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
Brian Silvermane1103e62015-02-15 02:03:38 -0500164 data.GetControlBit(ControlBit::kEnabled);
Austin Schuh6182f8d2015-02-14 22:15:04 -0800165 if (auto_running_ != last_auto_running) {
166 if (auto_running_) {
167 StartAuto();
168 } else {
169 StopAuto();
Brian Silverman20141f92015-01-05 17:39:01 -0800170 }
Austin Schuh6182f8d2015-02-14 22:15:04 -0800171 }
172
173 if (!data.GetControlBit(ControlBit::kAutonomous)) {
Daniel Petti61896522015-02-15 18:01:43 -0800174 HandleDrivetrain(data);
Austin Schuhf14727e2015-03-08 18:49:12 -0700175 HandleTeleop(data);
Brian Silverman20141f92015-01-05 17:39:01 -0800176 }
177 }
178
179 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -0800180 const double wheel = -data.GetAxis(kSteeringWheel);
181 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman20141f92015-01-05 17:39:01 -0800182
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500183 if (!drivetrain_queue.goal.MakeWithBuilder()
Brian Silverman20141f92015-01-05 17:39:01 -0800184 .steering(wheel)
185 .throttle(throttle)
Brian Silverman20141f92015-01-05 17:39:01 -0800186 .quickturn(data.IsPressed(kQuickTurn))
Austin Schuh331e13d2015-02-15 00:16:51 -0800187 .control_loop_driving(false)
Brian Silverman20141f92015-01-05 17:39:01 -0800188 .Send()) {
189 LOG(WARNING, "sending stick values failed\n");
190 }
Brian Silverman20141f92015-01-05 17:39:01 -0800191 }
192
193 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuhf14727e2015-03-08 18:49:12 -0700194 double intake_power = 0.0;
Brian Silverman20141f92015-01-05 17:39:01 -0800195 if (!data.GetControlBit(ControlBit::kEnabled)) {
196 action_queue_.CancelAllActions();
Austin Schuh6182f8d2015-02-14 22:15:04 -0800197 LOG(DEBUG, "Canceling\n");
Brian Silverman20141f92015-01-05 17:39:01 -0800198 }
199
Austin Schuhf14727e2015-03-08 18:49:12 -0700200 if (data.IsPressed(kRollersIn)) {
201 intake_power = 10.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800202 claw_goal_ = 0.0;
Austin Schuhd8b2a242015-02-22 21:46:53 -0800203 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700204 if (data.IsPressed(kSpit)) {
205 intake_power = -12.0;
Austin Schuhd8b2a242015-02-22 21:46:53 -0800206 }
Austin Schuh994d42c2015-03-01 00:02:17 -0800207
Austin Schuhf14727e2015-03-08 18:49:12 -0700208 // Toggle button for the claw
209 if (data.PosEdge(kClawToggle)) {
210 claw_rollers_closed_ = !claw_rollers_closed_;
Austin Schuh994d42c2015-03-01 00:02:17 -0800211 }
212
Austin Schuhf14727e2015-03-08 18:49:12 -0700213 /*
214 if (data.IsPressed(kClawClosed)) {
215 claw_rollers_closed_ = true;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800216 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700217 */
Austin Schuh700b9222015-03-01 03:03:15 -0800218
Austin Schuhf14727e2015-03-08 18:49:12 -0700219 // Horizontal can pickup.
Austin Schuh700b9222015-03-01 03:03:15 -0800220 if (data.PosEdge(kElevatorCanUp)) {
221 actors::HorizontalCanPickupParams params;
222 params.elevator_height = 0.3;
Brian Silverman10043572015-03-21 23:43:50 -0700223 params.pickup_angle = 0.40;
224 params.suck_time = 0.08;
Austin Schuh700b9222015-03-01 03:03:15 -0800225 params.suck_power = 8.0;
226
227 params.claw_settle_time = 0.05;
228 params.claw_settle_power = 5.0;
229 params.claw_full_lift_angle = 1.35;
230 params.claw_end_angle = 0.5;
Austin Schuhf14727e2015-03-08 18:49:12 -0700231
Brian Silverman10043572015-03-21 23:43:50 -0700232 // End low so we don't drop it.
233 params.elevator_end_height = 0.3;
234 params.arm_end_angle = 0.0;
Austin Schuh700b9222015-03-01 03:03:15 -0800235 action_queue_.EnqueueAction(
236 actors::MakeHorizontalCanPickupAction(params));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800237 fridge_closed_ = true;
238 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700239
240 // -0.942503 arm, 0.374752 elevator
241 // Vertical can pickup.
242 if (data.PosEdge(kCanPickup)) {
243 actors::CanPickupParams params;
Austin Schuh90f28542015-03-29 13:46:03 -0700244 params.pickup_x = 0.6;
245 params.pickup_y = 0.1;
246 params.lift_height = 0.2;
247 params.pickup_goal_before_move_height = 0.3;
248 params.start_lowering_x = 0.1;
Brian Silverman10043572015-03-21 23:43:50 -0700249 // End low so the can is supported.
Austin Schuh90f28542015-03-29 13:46:03 -0700250 params.before_place_height = 0.4;
Brian Silverman10043572015-03-21 23:43:50 -0700251 params.end_height = 0.3;
252 params.end_angle = 0.0;
Austin Schuhf14727e2015-03-08 18:49:12 -0700253 action_queue_.EnqueueAction(actors::MakeCanPickupAction(params));
254 fridge_closed_ = true;
255 }
256
Brian Silvermancb615cf2015-03-21 21:42:35 -0700257 if (data.PosEdge(kCanReset)) {
258 action_queue_.CancelAllActions();
259 elevator_goal_ = 0.3;
260 arm_goal_ = 0.0;
261 }
262
Austin Schuhf14727e2015-03-08 18:49:12 -0700263 // Tote chute pull in when button is pressed, pack when done.
264 if (data.IsPressed(kToteChute)) {
265 claw_goal_ = 0.8;
266 intake_power = 7.0;
267 }
268 if (data.NegEdge(kToteChute)) {
269 claw_goal_ = kClawTotePackAngle;
270 }
271
272 if (data.PosEdge(kStackAndLift)) {
273 actors::StackAndLiftParams params;
Austin Schuh959d2022015-03-29 13:51:11 -0700274 params.stack_params.claw_out_angle = kClawTotePackAngle;
Austin Schuhf14727e2015-03-08 18:49:12 -0700275 params.stack_params.bottom = 0.020;
276 params.stack_params.over_box_before_place_height = 0.39;
Brian Silverman10043572015-03-21 23:43:50 -0700277 params.stack_params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700278
279 params.grab_after_stack = true;
Austin Schuh959d2022015-03-29 13:51:11 -0700280 params.clamp_pause_time = 0.0;
Brian Silverman697d07b2015-03-19 23:41:11 -0700281 params.lift_params.lift_height = kStackUpHeight;
282 params.lift_params.lift_arm = kStackUpArm;
Austin Schuhf14727e2015-03-08 18:49:12 -0700283 params.grab_after_lift = true;
284 fridge_closed_ = true;
285
286 action_queue_.EnqueueAction(actors::MakeStackAndLiftAction(params));
287 }
288
Brian Silverman10043572015-03-21 23:43:50 -0700289 if (data.PosEdge(kStackAndHold) || data.PosEdge(kSetStackDownAndHold)) {
Austin Schuhf14727e2015-03-08 18:49:12 -0700290 actors::StackAndHoldParams params;
291 params.bottom = 0.020;
292 params.over_box_before_place_height = 0.39;
293
Brian Silverman10043572015-03-21 23:43:50 -0700294 params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700295 params.clamp_pause_time = 0.25;
Brian Silverman10043572015-03-21 23:43:50 -0700296 params.claw_clamp_angle = kClawTotePackAngle;
Austin Schuhf14727e2015-03-08 18:49:12 -0700297
298 params.hold_height = 0.68;
Austin Schuh959d2022015-03-29 13:51:11 -0700299 params.claw_out_angle = kClawTotePackAngle;
Brian Silverman10043572015-03-21 23:43:50 -0700300
301 if (data.PosEdge(kSetStackDownAndHold)) {
302 params.place_not_stack = true;
303 params.clamp_pause_time = 0.1;
304 //params.claw_clamp_angle = kClawTotePackAngle - 0.5;
305 } else {
306 params.place_not_stack = false;
307 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700308
309 action_queue_.EnqueueAction(actors::MakeStackAndHoldAction(params));
310 fridge_closed_ = true;
311 }
312
313 // TODO(austin): Figure out what action needed to pull the 6th tote into the
314 // claw.
315
316 // Lower the fridge from holding the stack, grab the stack, and then lift.
317 if (data.PosEdge(kHeldToLift)) {
318 actors::HeldToLiftParams params;
Austin Schuh959d2022015-03-29 13:51:11 -0700319 params.arm_clearance = kClawStackClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700320 params.clamp_pause_time = 0.1;
Brian Silverman10043572015-03-21 23:43:50 -0700321 params.before_lift_settle_time = 0.1;
Austin Schuhf14727e2015-03-08 18:49:12 -0700322 params.bottom_height = 0.020;
Austin Schuh959d2022015-03-29 13:51:11 -0700323 params.claw_out_angle = kClawTotePackAngle;
Brian Silverman697d07b2015-03-19 23:41:11 -0700324 params.lift_params.lift_height = kStackUpHeight;
325 params.lift_params.lift_arm = kStackUpArm;
Austin Schuhf14727e2015-03-08 18:49:12 -0700326 fridge_closed_ = true;
327
328 action_queue_.EnqueueAction(actors::MakeHeldToLiftAction(params));
329 }
330
Brian Silverman10043572015-03-21 23:43:50 -0700331 // Lift the can up.
332 if (data.PosEdge(kCanUp)) {
333 actors::LiftParams params;
334 params.lift_height = 0.68;
335 params.lift_arm = 0.3;
Brian Silverman697d07b2015-03-19 23:41:11 -0700336 params.pack_claw = false;
337 params.pack_claw_angle = 0;
Brian Silverman10043572015-03-21 23:43:50 -0700338 fridge_closed_ = true;
339
340 action_queue_.EnqueueAction(actors::MakeLiftAction(params));
341 }
342
Austin Schuhf14727e2015-03-08 18:49:12 -0700343 // Pick up a tote from the ground and put it in the bottom of the bot.
344 if (data.PosEdge(kPickup)) {
345 actors::PickupParams params;
346 // Lift to here initially.
347 params.pickup_angle = 0.9;
348 // Start sucking here
349 params.suck_angle = 0.8;
350 // Go back down to here to finish sucking.
351 params.suck_angle_finish = 0.4;
352 // Pack the box back in here.
353 params.pickup_finish_angle = kClawTotePackAngle;
354 params.intake_time = 0.8;
355 params.intake_voltage = 7.0;
356 action_queue_.EnqueueAction(actors::MakePickupAction(params));
357 }
358
359 // Place stack on a tote in the tray, and grab it.
360 if (data.PosEdge(kStack)) {
361 actors::StackParams params;
Austin Schuh959d2022015-03-29 13:51:11 -0700362 params.claw_out_angle = kClawTotePackAngle;
Austin Schuhf14727e2015-03-08 18:49:12 -0700363 params.bottom = 0.020;
Brian Silverman10043572015-03-21 23:43:50 -0700364 params.only_place = false;
365 params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700366 params.over_box_before_place_height = 0.39;
367 action_queue_.EnqueueAction(actors::MakeStackAction(params));
368 claw_rollers_closed_ = true;
Brian Silverman10043572015-03-21 23:43:50 -0700369 fridge_closed_ = true;
Austin Schuhf14727e2015-03-08 18:49:12 -0700370 }
371
Brian Silverman10043572015-03-21 23:43:50 -0700372 if (data.PosEdge(kScore)) {
Brian Silverman913eafa2015-03-19 23:42:16 -0700373 action_queue_.EnqueueAction(
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700374 actors::MakeScoreAction(MakeScoreParams()));
Brian Silverman913eafa2015-03-19 23:42:16 -0700375 fridge_closed_ = false;
Brian Silverman10043572015-03-21 23:43:50 -0700376 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700377
Brian Silverman913eafa2015-03-19 23:42:16 -0700378 if (data.PosEdge(kCoopTop)) {
379 action_queue_.EnqueueAction(
380 actors::MakeScoreAction(MakeCoopTopParams(false)));
381 }
382 if (data.PosEdge(kCoopTopRetract)) {
383 action_queue_.EnqueueAction(
384 actors::MakeScoreAction(MakeCoopTopParams(true)));
385 fridge_closed_ = false;
386 }
387
388 if (data.PosEdge(kCoopBottom)) {
389 action_queue_.EnqueueAction(
390 actors::MakeScoreAction(MakeCoopBottomParams(false)));
391 }
392 if (data.PosEdge(kCoopBottomRetract)) {
393 action_queue_.EnqueueAction(
394 actors::MakeScoreAction(MakeCoopBottomParams(true)));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800395 fridge_closed_ = false;
396 }
397
Brian Silverman10043572015-03-21 23:43:50 -0700398 if (data.PosEdge(kFridgeToggle)) {
399 fridge_closed_ = !fridge_closed_;
400 }
401
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800402 if (data.PosEdge(ControlBit::kEnabled)) {
403 // If we got enabled, wait for everything to zero.
404 LOG(INFO, "Waiting for zero.\n");
405 waiting_for_zero_ = true;
406 }
407
Austin Schuh700b9222015-03-01 03:03:15 -0800408 claw_queue.status.FetchLatest();
409 fridge_queue.status.FetchLatest();
410 if (!claw_queue.status.get()) {
411 LOG(ERROR, "Got no claw status packet.\n");
Austin Schuh700b9222015-03-01 03:03:15 -0800412 }
413 if (!fridge_queue.status.get()) {
414 LOG(ERROR, "Got no fridge status packet.\n");
Austin Schuh700b9222015-03-01 03:03:15 -0800415 }
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800416
Brian Silvermanea91f592015-03-28 18:25:57 -0400417 if (claw_queue.status.get() && fridge_queue.status.get() &&
418 claw_queue.status->zeroed && fridge_queue.status->zeroed) {
Austin Schuh700b9222015-03-01 03:03:15 -0800419 if (waiting_for_zero_) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800420 LOG(INFO, "Zeroed! Starting teleop mode.\n");
421 waiting_for_zero_ = false;
422
423 // Set the initial goals to where we are now.
Austin Schuh2cee0b52015-03-29 13:41:39 -0700424 elevator_goal_ = 0.3;
425 arm_goal_ = 0.0;
426 claw_goal_ = 0.6;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800427 }
428 } else {
Austin Schuh700b9222015-03-01 03:03:15 -0800429 waiting_for_zero_ = true;
Austin Schuh700b9222015-03-01 03:03:15 -0800430 }
431
432 if (!waiting_for_zero_) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800433 if (!action_queue_.Running()) {
434 auto new_fridge_goal = fridge_queue.goal.MakeMessage();
Austin Schuh6e242ac2015-03-07 17:08:21 -0800435 new_fridge_goal->max_velocity = elevator_params_.velocity;
436 new_fridge_goal->max_acceleration = elevator_params_.acceleration;
Austin Schuh1d44bd42015-03-15 16:40:45 -0700437 new_fridge_goal->profiling_type = 0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800438 new_fridge_goal->height = elevator_goal_;
Austin Schuh6e242ac2015-03-07 17:08:21 -0800439 new_fridge_goal->velocity = 0.0;
440 new_fridge_goal->max_angular_velocity = arm_params_.velocity;
441 new_fridge_goal->max_angular_acceleration = arm_params_.acceleration;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800442 new_fridge_goal->angle = arm_goal_;
443 new_fridge_goal->angular_velocity = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800444 new_fridge_goal->grabbers.top_front = fridge_closed_;
445 new_fridge_goal->grabbers.top_back = fridge_closed_;
446 new_fridge_goal->grabbers.bottom_front = fridge_closed_;
447 new_fridge_goal->grabbers.bottom_back = fridge_closed_;
448
449 if (!new_fridge_goal.Send()) {
450 LOG(ERROR, "Sending fridge goal failed.\n");
451 } else {
452 LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_,
453 arm_goal_);
454 }
455 if (!claw_queue.goal.MakeWithBuilder()
456 .angle(claw_goal_)
457 .rollers_closed(claw_rollers_closed_)
Brian Silverman10043572015-03-21 23:43:50 -0700458 .max_velocity(4.0)
459 .max_acceleration(6.0)
Austin Schuhf14727e2015-03-08 18:49:12 -0700460 .intake(intake_power)
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800461 .Send()) {
462 LOG(ERROR, "Sending claw goal failed.\n");
463 }
464 }
465 }
466
Austin Schuhd8b2a242015-02-22 21:46:53 -0800467 if (action_queue_.Running()) {
468 // If we are running an action, update our goals to the current goals.
469 control_loops::fridge_queue.status.FetchLatest();
470 if (control_loops::fridge_queue.status.get()) {
471 arm_goal_ = control_loops::fridge_queue.status->goal_angle;
472 elevator_goal_ = control_loops::fridge_queue.status->goal_height;
473 } else {
474 LOG(ERROR, "No fridge status!\n");
475 }
476
477 // If we are running an action, update our goals to the current goals.
478 control_loops::claw_queue.status.FetchLatest();
479 if (control_loops::claw_queue.status.get()) {
480 claw_goal_ = control_loops::claw_queue.status->goal_angle;
481 } else {
Brian Silvermanea91f592015-03-28 18:25:57 -0400482 LOG(ERROR, "No claw status!\n");
Austin Schuhd8b2a242015-02-22 21:46:53 -0800483 }
484 }
Brian Silverman20141f92015-01-05 17:39:01 -0800485 action_queue_.Tick();
486 was_running_ = action_queue_.Running();
487 }
488
489 private:
Austin Schuh6182f8d2015-02-14 22:15:04 -0800490 void StartAuto() {
491 LOG(INFO, "Starting auto mode\n");
492 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
493 }
494
495 void StopAuto() {
496 LOG(INFO, "Stopping auto mode\n");
497 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
498 }
499
Brian Silverman20141f92015-01-05 17:39:01 -0800500 bool was_running_;
Daniel Petti61896522015-02-15 18:01:43 -0800501
502 // Previous goals for systems.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800503 double elevator_goal_ = 0.2;
Daniel Petti61896522015-02-15 18:01:43 -0800504 double arm_goal_ = 0.0;
505 double claw_goal_ = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800506 bool claw_rollers_closed_ = false;
507 bool fridge_closed_ = false;
Austin Schuh6e242ac2015-03-07 17:08:21 -0800508 actors::ProfileParams arm_params_ = kArmMove;
509 actors::ProfileParams elevator_params_ = kElevatorMove;
Daniel Petti61896522015-02-15 18:01:43 -0800510
511 // If we're waiting for the subsystems to zero.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800512 bool waiting_for_zero_ = true;
Daniel Petti61896522015-02-15 18:01:43 -0800513
Austin Schuh6182f8d2015-02-14 22:15:04 -0800514 bool auto_running_ = false;
515
Austin Schuh331e13d2015-02-15 00:16:51 -0800516 ::aos::common::actions::ActionQueue action_queue_;
Brian Silverman20141f92015-01-05 17:39:01 -0800517
518 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
519 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
520 "no drivetrain status");
521};
522
523} // namespace joysticks
524} // namespace input
525} // namespace frc971
526
527int main() {
528 ::aos::Init();
529 ::frc971::input::joysticks::Reader reader;
530 reader.Run();
531 ::aos::Cleanup();
532}