blob: 474e2fd2961d8de00e36ae1b8a1b9a740a075fa6 [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
109const double kClawTotePackAngle = 0.95;
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
Brian Silverman697d07b2015-03-19 23:41:11 -0700113const double kStackUpHeight = 0.55;
114const double kStackUpArm = 0.1;
115
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;
126 r.place_height = -0.10;
127 r.home_return_height = 0.1;
128 return r;
129 }
130
131 static actors::ScoreParams MakeCoopTopParams(bool place_the_stack) {
132 actors::ScoreParams r;
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700133 r.move_the_stack = !place_the_stack;
Brian Silverman913eafa2015-03-19 23:42:16 -0700134 r.place_the_stack = place_the_stack;
135 r.upper_move_height = 0.52;
136 r.begin_horizontal_move_height = 0.5;
137 r.horizontal_move_target = -0.48;
138 r.place_height = 0.39;
139 r.home_return_height = 0.1;
140 return r;
141 }
142
143 static actors::ScoreParams MakeCoopBottomParams(bool place_the_stack) {
144 actors::ScoreParams r;
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700145 r.move_the_stack = !place_the_stack;
Brian Silverman913eafa2015-03-19 23:42:16 -0700146 r.place_the_stack = place_the_stack;
147 r.upper_move_height = 0.17;
148 r.begin_horizontal_move_height = 0.16;
149 r.horizontal_move_target = -0.7;
150 r.place_height = 0.0;
151 r.home_return_height = 0.1;
152 return r;
153 }
154
Brian Silverman20141f92015-01-05 17:39:01 -0800155 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Austin Schuh6182f8d2015-02-14 22:15:04 -0800156 bool last_auto_running = auto_running_;
157 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
Brian Silvermane1103e62015-02-15 02:03:38 -0500158 data.GetControlBit(ControlBit::kEnabled);
Austin Schuh6182f8d2015-02-14 22:15:04 -0800159 if (auto_running_ != last_auto_running) {
160 if (auto_running_) {
161 StartAuto();
162 } else {
163 StopAuto();
Brian Silverman20141f92015-01-05 17:39:01 -0800164 }
Austin Schuh6182f8d2015-02-14 22:15:04 -0800165 }
166
167 if (!data.GetControlBit(ControlBit::kAutonomous)) {
Daniel Petti61896522015-02-15 18:01:43 -0800168 HandleDrivetrain(data);
Austin Schuhf14727e2015-03-08 18:49:12 -0700169 HandleTeleop(data);
Brian Silverman20141f92015-01-05 17:39:01 -0800170 }
171 }
172
173 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -0800174 const double wheel = -data.GetAxis(kSteeringWheel);
175 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman20141f92015-01-05 17:39:01 -0800176
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500177 if (!drivetrain_queue.goal.MakeWithBuilder()
Brian Silverman20141f92015-01-05 17:39:01 -0800178 .steering(wheel)
179 .throttle(throttle)
Brian Silverman20141f92015-01-05 17:39:01 -0800180 .quickturn(data.IsPressed(kQuickTurn))
Austin Schuh331e13d2015-02-15 00:16:51 -0800181 .control_loop_driving(false)
Brian Silverman20141f92015-01-05 17:39:01 -0800182 .Send()) {
183 LOG(WARNING, "sending stick values failed\n");
184 }
Brian Silverman20141f92015-01-05 17:39:01 -0800185 }
186
187 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuhf14727e2015-03-08 18:49:12 -0700188 double intake_power = 0.0;
Brian Silverman20141f92015-01-05 17:39:01 -0800189 if (!data.GetControlBit(ControlBit::kEnabled)) {
190 action_queue_.CancelAllActions();
Austin Schuh6182f8d2015-02-14 22:15:04 -0800191 LOG(DEBUG, "Canceling\n");
Brian Silverman20141f92015-01-05 17:39:01 -0800192 }
193
Austin Schuhf14727e2015-03-08 18:49:12 -0700194 if (data.IsPressed(kRollersIn)) {
195 intake_power = 10.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800196 claw_goal_ = 0.0;
Austin Schuhd8b2a242015-02-22 21:46:53 -0800197 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700198 if (data.IsPressed(kSpit)) {
199 intake_power = -12.0;
Austin Schuhd8b2a242015-02-22 21:46:53 -0800200 }
Austin Schuh994d42c2015-03-01 00:02:17 -0800201
Austin Schuhf14727e2015-03-08 18:49:12 -0700202 // Toggle button for the claw
203 if (data.PosEdge(kClawToggle)) {
204 claw_rollers_closed_ = !claw_rollers_closed_;
Austin Schuh994d42c2015-03-01 00:02:17 -0800205 }
206
Austin Schuhf14727e2015-03-08 18:49:12 -0700207 /*
208 if (data.IsPressed(kClawClosed)) {
209 claw_rollers_closed_ = true;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800210 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700211 */
Austin Schuh700b9222015-03-01 03:03:15 -0800212
Austin Schuhf14727e2015-03-08 18:49:12 -0700213 // Horizontal can pickup.
Austin Schuh700b9222015-03-01 03:03:15 -0800214 if (data.PosEdge(kElevatorCanUp)) {
215 actors::HorizontalCanPickupParams params;
216 params.elevator_height = 0.3;
Brian Silverman10043572015-03-21 23:43:50 -0700217 params.pickup_angle = 0.40;
218 params.suck_time = 0.08;
Austin Schuh700b9222015-03-01 03:03:15 -0800219 params.suck_power = 8.0;
220
221 params.claw_settle_time = 0.05;
222 params.claw_settle_power = 5.0;
223 params.claw_full_lift_angle = 1.35;
224 params.claw_end_angle = 0.5;
Austin Schuhf14727e2015-03-08 18:49:12 -0700225
Brian Silverman10043572015-03-21 23:43:50 -0700226 // End low so we don't drop it.
227 params.elevator_end_height = 0.3;
228 params.arm_end_angle = 0.0;
Austin Schuh700b9222015-03-01 03:03:15 -0800229 action_queue_.EnqueueAction(
230 actors::MakeHorizontalCanPickupAction(params));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800231 fridge_closed_ = true;
232 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700233
234 // -0.942503 arm, 0.374752 elevator
235 // Vertical can pickup.
236 if (data.PosEdge(kCanPickup)) {
237 actors::CanPickupParams params;
Austin Schuh90f28542015-03-29 13:46:03 -0700238 params.pickup_x = 0.6;
239 params.pickup_y = 0.1;
240 params.lift_height = 0.2;
241 params.pickup_goal_before_move_height = 0.3;
242 params.start_lowering_x = 0.1;
Brian Silverman10043572015-03-21 23:43:50 -0700243 // End low so the can is supported.
Austin Schuh90f28542015-03-29 13:46:03 -0700244 params.before_place_height = 0.4;
Brian Silverman10043572015-03-21 23:43:50 -0700245 params.end_height = 0.3;
246 params.end_angle = 0.0;
Austin Schuhf14727e2015-03-08 18:49:12 -0700247 action_queue_.EnqueueAction(actors::MakeCanPickupAction(params));
248 fridge_closed_ = true;
249 }
250
Brian Silvermancb615cf2015-03-21 21:42:35 -0700251 if (data.PosEdge(kCanReset)) {
252 action_queue_.CancelAllActions();
253 elevator_goal_ = 0.3;
254 arm_goal_ = 0.0;
255 }
256
Austin Schuhf14727e2015-03-08 18:49:12 -0700257 // Tote chute pull in when button is pressed, pack when done.
258 if (data.IsPressed(kToteChute)) {
259 claw_goal_ = 0.8;
260 intake_power = 7.0;
261 }
262 if (data.NegEdge(kToteChute)) {
263 claw_goal_ = kClawTotePackAngle;
264 }
265
266 if (data.PosEdge(kStackAndLift)) {
267 actors::StackAndLiftParams params;
Brian Silverman654b5122015-03-20 16:58:04 -0700268 params.stack_params.claw_out_angle = kClawStackClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700269 params.stack_params.bottom = 0.020;
270 params.stack_params.over_box_before_place_height = 0.39;
Brian Silverman10043572015-03-21 23:43:50 -0700271 params.stack_params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700272
273 params.grab_after_stack = true;
274 params.clamp_pause_time = 0.1;
Brian Silverman697d07b2015-03-19 23:41:11 -0700275 params.lift_params.lift_height = kStackUpHeight;
276 params.lift_params.lift_arm = kStackUpArm;
Austin Schuhf14727e2015-03-08 18:49:12 -0700277 params.grab_after_lift = true;
278 fridge_closed_ = true;
279
280 action_queue_.EnqueueAction(actors::MakeStackAndLiftAction(params));
281 }
282
Brian Silverman10043572015-03-21 23:43:50 -0700283 if (data.PosEdge(kStackAndHold) || data.PosEdge(kSetStackDownAndHold)) {
Austin Schuhf14727e2015-03-08 18:49:12 -0700284 actors::StackAndHoldParams params;
285 params.bottom = 0.020;
286 params.over_box_before_place_height = 0.39;
287
Brian Silverman10043572015-03-21 23:43:50 -0700288 params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700289 params.clamp_pause_time = 0.25;
Brian Silverman10043572015-03-21 23:43:50 -0700290 params.claw_clamp_angle = kClawTotePackAngle;
Austin Schuhf14727e2015-03-08 18:49:12 -0700291
292 params.hold_height = 0.68;
Brian Silverman654b5122015-03-20 16:58:04 -0700293 params.claw_out_angle = kClawStackClearance;
Brian Silverman10043572015-03-21 23:43:50 -0700294
295 if (data.PosEdge(kSetStackDownAndHold)) {
296 params.place_not_stack = true;
297 params.clamp_pause_time = 0.1;
298 //params.claw_clamp_angle = kClawTotePackAngle - 0.5;
299 } else {
300 params.place_not_stack = false;
301 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700302
303 action_queue_.EnqueueAction(actors::MakeStackAndHoldAction(params));
304 fridge_closed_ = true;
305 }
306
307 // TODO(austin): Figure out what action needed to pull the 6th tote into the
308 // claw.
309
310 // Lower the fridge from holding the stack, grab the stack, and then lift.
311 if (data.PosEdge(kHeldToLift)) {
312 actors::HeldToLiftParams params;
Brian Silverman10043572015-03-21 23:43:50 -0700313 params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700314 params.clamp_pause_time = 0.1;
Brian Silverman10043572015-03-21 23:43:50 -0700315 params.before_lift_settle_time = 0.1;
Austin Schuhf14727e2015-03-08 18:49:12 -0700316 params.bottom_height = 0.020;
Brian Silverman654b5122015-03-20 16:58:04 -0700317 params.claw_out_angle = kClawStackClearance;
Brian Silverman697d07b2015-03-19 23:41:11 -0700318 params.lift_params.lift_height = kStackUpHeight;
319 params.lift_params.lift_arm = kStackUpArm;
Austin Schuhf14727e2015-03-08 18:49:12 -0700320 fridge_closed_ = true;
321
322 action_queue_.EnqueueAction(actors::MakeHeldToLiftAction(params));
323 }
324
Brian Silverman10043572015-03-21 23:43:50 -0700325 // Lift the can up.
326 if (data.PosEdge(kCanUp)) {
327 actors::LiftParams params;
328 params.lift_height = 0.68;
329 params.lift_arm = 0.3;
Brian Silverman697d07b2015-03-19 23:41:11 -0700330 params.pack_claw = false;
331 params.pack_claw_angle = 0;
Brian Silverman10043572015-03-21 23:43:50 -0700332 fridge_closed_ = true;
333
334 action_queue_.EnqueueAction(actors::MakeLiftAction(params));
335 }
336
Austin Schuhf14727e2015-03-08 18:49:12 -0700337 // Pick up a tote from the ground and put it in the bottom of the bot.
338 if (data.PosEdge(kPickup)) {
339 actors::PickupParams params;
340 // Lift to here initially.
341 params.pickup_angle = 0.9;
342 // Start sucking here
343 params.suck_angle = 0.8;
344 // Go back down to here to finish sucking.
345 params.suck_angle_finish = 0.4;
346 // Pack the box back in here.
347 params.pickup_finish_angle = kClawTotePackAngle;
348 params.intake_time = 0.8;
349 params.intake_voltage = 7.0;
350 action_queue_.EnqueueAction(actors::MakePickupAction(params));
351 }
352
353 // Place stack on a tote in the tray, and grab it.
354 if (data.PosEdge(kStack)) {
355 actors::StackParams params;
Brian Silverman654b5122015-03-20 16:58:04 -0700356 params.claw_out_angle = kClawStackClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700357 params.bottom = 0.020;
Brian Silverman10043572015-03-21 23:43:50 -0700358 params.only_place = false;
359 params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700360 params.over_box_before_place_height = 0.39;
361 action_queue_.EnqueueAction(actors::MakeStackAction(params));
362 claw_rollers_closed_ = true;
Brian Silverman10043572015-03-21 23:43:50 -0700363 fridge_closed_ = true;
Austin Schuhf14727e2015-03-08 18:49:12 -0700364 }
365
Brian Silverman10043572015-03-21 23:43:50 -0700366 if (data.PosEdge(kScore)) {
Brian Silverman913eafa2015-03-19 23:42:16 -0700367 action_queue_.EnqueueAction(
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700368 actors::MakeScoreAction(MakeScoreParams()));
Brian Silverman913eafa2015-03-19 23:42:16 -0700369 fridge_closed_ = false;
Brian Silverman10043572015-03-21 23:43:50 -0700370 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700371
Brian Silverman913eafa2015-03-19 23:42:16 -0700372 if (data.PosEdge(kCoopTop)) {
373 action_queue_.EnqueueAction(
374 actors::MakeScoreAction(MakeCoopTopParams(false)));
375 }
376 if (data.PosEdge(kCoopTopRetract)) {
377 action_queue_.EnqueueAction(
378 actors::MakeScoreAction(MakeCoopTopParams(true)));
379 fridge_closed_ = false;
380 }
381
382 if (data.PosEdge(kCoopBottom)) {
383 action_queue_.EnqueueAction(
384 actors::MakeScoreAction(MakeCoopBottomParams(false)));
385 }
386 if (data.PosEdge(kCoopBottomRetract)) {
387 action_queue_.EnqueueAction(
388 actors::MakeScoreAction(MakeCoopBottomParams(true)));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800389 fridge_closed_ = false;
390 }
391
Brian Silverman10043572015-03-21 23:43:50 -0700392 if (data.PosEdge(kFridgeToggle)) {
393 fridge_closed_ = !fridge_closed_;
394 }
395
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800396 if (data.PosEdge(ControlBit::kEnabled)) {
397 // If we got enabled, wait for everything to zero.
398 LOG(INFO, "Waiting for zero.\n");
399 waiting_for_zero_ = true;
400 }
401
Austin Schuh700b9222015-03-01 03:03:15 -0800402 claw_queue.status.FetchLatest();
403 fridge_queue.status.FetchLatest();
404 if (!claw_queue.status.get()) {
405 LOG(ERROR, "Got no claw status packet.\n");
Austin Schuh700b9222015-03-01 03:03:15 -0800406 }
407 if (!fridge_queue.status.get()) {
408 LOG(ERROR, "Got no fridge status packet.\n");
Austin Schuh700b9222015-03-01 03:03:15 -0800409 }
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800410
Brian Silvermanea91f592015-03-28 18:25:57 -0400411 if (claw_queue.status.get() && fridge_queue.status.get() &&
412 claw_queue.status->zeroed && fridge_queue.status->zeroed) {
Austin Schuh700b9222015-03-01 03:03:15 -0800413 if (waiting_for_zero_) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800414 LOG(INFO, "Zeroed! Starting teleop mode.\n");
415 waiting_for_zero_ = false;
416
417 // Set the initial goals to where we are now.
Austin Schuh2cee0b52015-03-29 13:41:39 -0700418 elevator_goal_ = 0.3;
419 arm_goal_ = 0.0;
420 claw_goal_ = 0.6;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800421 }
422 } else {
Austin Schuh700b9222015-03-01 03:03:15 -0800423 waiting_for_zero_ = true;
Austin Schuh700b9222015-03-01 03:03:15 -0800424 }
425
426 if (!waiting_for_zero_) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800427 if (!action_queue_.Running()) {
428 auto new_fridge_goal = fridge_queue.goal.MakeMessage();
Austin Schuh6e242ac2015-03-07 17:08:21 -0800429 new_fridge_goal->max_velocity = elevator_params_.velocity;
430 new_fridge_goal->max_acceleration = elevator_params_.acceleration;
Austin Schuh1d44bd42015-03-15 16:40:45 -0700431 new_fridge_goal->profiling_type = 0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800432 new_fridge_goal->height = elevator_goal_;
Austin Schuh6e242ac2015-03-07 17:08:21 -0800433 new_fridge_goal->velocity = 0.0;
434 new_fridge_goal->max_angular_velocity = arm_params_.velocity;
435 new_fridge_goal->max_angular_acceleration = arm_params_.acceleration;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800436 new_fridge_goal->angle = arm_goal_;
437 new_fridge_goal->angular_velocity = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800438 new_fridge_goal->grabbers.top_front = fridge_closed_;
439 new_fridge_goal->grabbers.top_back = fridge_closed_;
440 new_fridge_goal->grabbers.bottom_front = fridge_closed_;
441 new_fridge_goal->grabbers.bottom_back = fridge_closed_;
442
443 if (!new_fridge_goal.Send()) {
444 LOG(ERROR, "Sending fridge goal failed.\n");
445 } else {
446 LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_,
447 arm_goal_);
448 }
449 if (!claw_queue.goal.MakeWithBuilder()
450 .angle(claw_goal_)
451 .rollers_closed(claw_rollers_closed_)
Brian Silverman10043572015-03-21 23:43:50 -0700452 .max_velocity(4.0)
453 .max_acceleration(6.0)
Austin Schuhf14727e2015-03-08 18:49:12 -0700454 .intake(intake_power)
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800455 .Send()) {
456 LOG(ERROR, "Sending claw goal failed.\n");
457 }
458 }
459 }
460
Austin Schuhd8b2a242015-02-22 21:46:53 -0800461 if (action_queue_.Running()) {
462 // If we are running an action, update our goals to the current goals.
463 control_loops::fridge_queue.status.FetchLatest();
464 if (control_loops::fridge_queue.status.get()) {
465 arm_goal_ = control_loops::fridge_queue.status->goal_angle;
466 elevator_goal_ = control_loops::fridge_queue.status->goal_height;
467 } else {
468 LOG(ERROR, "No fridge status!\n");
469 }
470
471 // If we are running an action, update our goals to the current goals.
472 control_loops::claw_queue.status.FetchLatest();
473 if (control_loops::claw_queue.status.get()) {
474 claw_goal_ = control_loops::claw_queue.status->goal_angle;
475 } else {
Brian Silvermanea91f592015-03-28 18:25:57 -0400476 LOG(ERROR, "No claw status!\n");
Austin Schuhd8b2a242015-02-22 21:46:53 -0800477 }
478 }
Brian Silverman20141f92015-01-05 17:39:01 -0800479 action_queue_.Tick();
480 was_running_ = action_queue_.Running();
481 }
482
483 private:
Austin Schuh6182f8d2015-02-14 22:15:04 -0800484 void StartAuto() {
485 LOG(INFO, "Starting auto mode\n");
486 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
487 }
488
489 void StopAuto() {
490 LOG(INFO, "Stopping auto mode\n");
491 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
492 }
493
Brian Silverman20141f92015-01-05 17:39:01 -0800494 bool was_running_;
Daniel Petti61896522015-02-15 18:01:43 -0800495
496 // Previous goals for systems.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800497 double elevator_goal_ = 0.2;
Daniel Petti61896522015-02-15 18:01:43 -0800498 double arm_goal_ = 0.0;
499 double claw_goal_ = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800500 bool claw_rollers_closed_ = false;
501 bool fridge_closed_ = false;
Austin Schuh6e242ac2015-03-07 17:08:21 -0800502 actors::ProfileParams arm_params_ = kArmMove;
503 actors::ProfileParams elevator_params_ = kElevatorMove;
Daniel Petti61896522015-02-15 18:01:43 -0800504
505 // If we're waiting for the subsystems to zero.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800506 bool waiting_for_zero_ = true;
Daniel Petti61896522015-02-15 18:01:43 -0800507
Austin Schuh6182f8d2015-02-14 22:15:04 -0800508 bool auto_running_ = false;
509
Austin Schuh331e13d2015-02-15 00:16:51 -0800510 ::aos::common::actions::ActionQueue action_queue_;
Brian Silverman20141f92015-01-05 17:39:01 -0800511
512 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
513 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
514 "no drivetrain status");
515};
516
517} // namespace joysticks
518} // namespace input
519} // namespace frc971
520
521int main() {
522 ::aos::Init();
523 ::frc971::input::joysticks::Reader reader;
524 reader.Run();
525 ::aos::Cleanup();
526}