blob: c2d1b2ba5b58816de9b853c595cfd43c74f4de7e [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 Schuha534ebe2015-03-29 18:23:35 -070074const ButtonLocation kStackAndHold(3, 5);
75const ButtonLocation kRollersIn(3, 2);
76const ButtonLocation kHorizontalCanRollersIn(4, 5);
Austin Schuhf14727e2015-03-08 18:49:12 -070077const ButtonLocation kClawToggle(4, 1);
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080078
Brian Silverman10043572015-03-21 23:43:50 -070079const POVLocation kElevatorCanUp(4, 0);
Austin Schuhf14727e2015-03-08 18:49:12 -070080
81// POV stick 3, 180
82const POVLocation kCanPickup(4, 180);
83const ButtonLocation kToteChute(4, 6);
84const ButtonLocation kStackAndLift(4, 7);
Austin Schuhf14727e2015-03-08 18:49:12 -070085
86// Pull in the 6th tote.
Brian Silverman10043572015-03-21 23:43:50 -070087//const ButtonLocation kSixthTote(4, 10);
88const ButtonLocation kCanUp(4, 10);
Austin Schuhf14727e2015-03-08 18:49:12 -070089
90const ButtonLocation kHeldToLift(4, 11);
91const ButtonLocation kPickup(4, 9);
92
93const ButtonLocation kStack(4, 2);
94
95// Move the fridge out with the stack in preparation for scoring.
Brian Silverman10043572015-03-21 23:43:50 -070096const ButtonLocation kScore(4, 8);
Brian Silverman913eafa2015-03-19 23:42:16 -070097const ButtonLocation kCoopTop(3, 8);
98const ButtonLocation kCoopTopRetract(3, 7);
99const ButtonLocation kCoopBottom(3, 6);
Brian Silvermand4278302015-03-28 00:21:15 -0400100const ButtonLocation kCoopBottomRetract(4, 12);
Brian Silverman913eafa2015-03-19 23:42:16 -0700101
Brian Silvermand4278302015-03-28 00:21:15 -0400102const ButtonLocation kCanReset(3, 9);
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700103
Brian Silverman10043572015-03-21 23:43:50 -0700104const POVLocation kFridgeToggle(4, 270);
Austin Schuhf14727e2015-03-08 18:49:12 -0700105const ButtonLocation kSpit(4, 3);
106
107// Set stack down in the bot.
Brian Silverman10043572015-03-21 23:43:50 -0700108const POVLocation kSetStackDownAndHold(4, 90);
Austin Schuhf14727e2015-03-08 18:49:12 -0700109
Austin Schuh959d2022015-03-29 13:51:11 -0700110const double kClawTotePackAngle = 0.90;
Brian Silverman10043572015-03-21 23:43:50 -0700111const double kArmRaiseLowerClearance = -0.08;
Brian Silverman654b5122015-03-20 16:58:04 -0700112const double kClawStackClearance = 0.55;
Austin Schuha534ebe2015-03-29 18:23:35 -0700113const double kHorizontalCanClawAngle = 0.12;
Brian Silverman20141f92015-01-05 17:39:01 -0800114
Austin Schuh959d2022015-03-29 13:51:11 -0700115const double kStackUpHeight = 0.60;
116const double kStackUpArm = 0.0;
Brian Silverman697d07b2015-03-19 23:41:11 -0700117
Brian Silverman20141f92015-01-05 17:39:01 -0800118class Reader : public ::aos::input::JoystickInput {
119 public:
Austin Schuh331e13d2015-02-15 00:16:51 -0800120 Reader() : was_running_(false) {}
Brian Silverman20141f92015-01-05 17:39:01 -0800121
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700122 static actors::ScoreParams MakeScoreParams() {
Brian Silverman913eafa2015-03-19 23:42:16 -0700123 actors::ScoreParams r;
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700124 r.move_the_stack = r.place_the_stack = true;
Brian Silverman913eafa2015-03-19 23:42:16 -0700125 r.upper_move_height = 0.14;
126 r.begin_horizontal_move_height = 0.13;
127 r.horizontal_move_target = -0.7;
Austin Schuh45ee2c52015-03-29 13:47:26 -0700128 r.horizontal_start_lowering = -0.65;
129 r.home_lift_horizontal_start_position = -0.60;
Brian Silverman913eafa2015-03-19 23:42:16 -0700130 r.place_height = -0.10;
Austin Schuh45ee2c52015-03-29 13:47:26 -0700131 r.home_return_height = 0.05;
Brian Silverman913eafa2015-03-19 23:42:16 -0700132 return r;
133 }
134
135 static actors::ScoreParams MakeCoopTopParams(bool place_the_stack) {
136 actors::ScoreParams r;
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700137 r.move_the_stack = !place_the_stack;
Brian Silverman913eafa2015-03-19 23:42:16 -0700138 r.place_the_stack = place_the_stack;
139 r.upper_move_height = 0.52;
140 r.begin_horizontal_move_height = 0.5;
141 r.horizontal_move_target = -0.48;
Austin Schuh45ee2c52015-03-29 13:47:26 -0700142 r.horizontal_start_lowering = r.horizontal_move_target;
143 r.home_lift_horizontal_start_position = -0.3;
Austin Schuha534ebe2015-03-29 18:23:35 -0700144 r.place_height = 0.35;
Brian Silverman913eafa2015-03-19 23:42:16 -0700145 r.home_return_height = 0.1;
146 return r;
147 }
148
149 static actors::ScoreParams MakeCoopBottomParams(bool place_the_stack) {
150 actors::ScoreParams r;
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700151 r.move_the_stack = !place_the_stack;
Brian Silverman913eafa2015-03-19 23:42:16 -0700152 r.place_the_stack = place_the_stack;
153 r.upper_move_height = 0.17;
154 r.begin_horizontal_move_height = 0.16;
155 r.horizontal_move_target = -0.7;
Austin Schuh45ee2c52015-03-29 13:47:26 -0700156 r.horizontal_start_lowering = r.horizontal_move_target;
157 r.home_lift_horizontal_start_position = -0.3;
Brian Silverman913eafa2015-03-19 23:42:16 -0700158 r.place_height = 0.0;
159 r.home_return_height = 0.1;
160 return r;
161 }
162
Brian Silverman20141f92015-01-05 17:39:01 -0800163 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Austin Schuh6182f8d2015-02-14 22:15:04 -0800164 bool last_auto_running = auto_running_;
165 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
Brian Silvermane1103e62015-02-15 02:03:38 -0500166 data.GetControlBit(ControlBit::kEnabled);
Austin Schuh6182f8d2015-02-14 22:15:04 -0800167 if (auto_running_ != last_auto_running) {
168 if (auto_running_) {
169 StartAuto();
170 } else {
171 StopAuto();
Brian Silverman20141f92015-01-05 17:39:01 -0800172 }
Austin Schuh6182f8d2015-02-14 22:15:04 -0800173 }
174
175 if (!data.GetControlBit(ControlBit::kAutonomous)) {
Daniel Petti61896522015-02-15 18:01:43 -0800176 HandleDrivetrain(data);
Austin Schuhf14727e2015-03-08 18:49:12 -0700177 HandleTeleop(data);
Brian Silverman20141f92015-01-05 17:39:01 -0800178 }
179 }
180
181 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -0800182 const double wheel = -data.GetAxis(kSteeringWheel);
183 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman20141f92015-01-05 17:39:01 -0800184
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500185 if (!drivetrain_queue.goal.MakeWithBuilder()
Brian Silverman20141f92015-01-05 17:39:01 -0800186 .steering(wheel)
187 .throttle(throttle)
Brian Silverman20141f92015-01-05 17:39:01 -0800188 .quickturn(data.IsPressed(kQuickTurn))
Austin Schuh331e13d2015-02-15 00:16:51 -0800189 .control_loop_driving(false)
Brian Silverman20141f92015-01-05 17:39:01 -0800190 .Send()) {
191 LOG(WARNING, "sending stick values failed\n");
192 }
Brian Silverman20141f92015-01-05 17:39:01 -0800193 }
194
195 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuhf14727e2015-03-08 18:49:12 -0700196 double intake_power = 0.0;
Brian Silverman20141f92015-01-05 17:39:01 -0800197 if (!data.GetControlBit(ControlBit::kEnabled)) {
198 action_queue_.CancelAllActions();
Austin Schuh6182f8d2015-02-14 22:15:04 -0800199 LOG(DEBUG, "Canceling\n");
Brian Silverman20141f92015-01-05 17:39:01 -0800200 }
201
Austin Schuhf14727e2015-03-08 18:49:12 -0700202 if (data.IsPressed(kRollersIn)) {
203 intake_power = 10.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800204 claw_goal_ = 0.0;
Austin Schuhd8b2a242015-02-22 21:46:53 -0800205 }
Austin Schuha534ebe2015-03-29 18:23:35 -0700206 if (data.IsPressed(kHorizontalCanRollersIn)) {
207 intake_power = 10.0;
208 claw_goal_ = kHorizontalCanClawAngle;
209 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700210 if (data.IsPressed(kSpit)) {
211 intake_power = -12.0;
Austin Schuha534ebe2015-03-29 18:23:35 -0700212 action_queue_.CancelAllActions();
Austin Schuhd8b2a242015-02-22 21:46:53 -0800213 }
Austin Schuh994d42c2015-03-01 00:02:17 -0800214
Austin Schuhf14727e2015-03-08 18:49:12 -0700215 // Toggle button for the claw
216 if (data.PosEdge(kClawToggle)) {
217 claw_rollers_closed_ = !claw_rollers_closed_;
Austin Schuh994d42c2015-03-01 00:02:17 -0800218 }
219
Austin Schuhf14727e2015-03-08 18:49:12 -0700220 /*
221 if (data.IsPressed(kClawClosed)) {
222 claw_rollers_closed_ = true;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800223 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700224 */
Austin Schuh700b9222015-03-01 03:03:15 -0800225
Austin Schuhf14727e2015-03-08 18:49:12 -0700226 // Horizontal can pickup.
Austin Schuh700b9222015-03-01 03:03:15 -0800227 if (data.PosEdge(kElevatorCanUp)) {
228 actors::HorizontalCanPickupParams params;
229 params.elevator_height = 0.3;
Austin Schuha534ebe2015-03-29 18:23:35 -0700230 params.pickup_angle = 0.60 + kHorizontalCanClawAngle;
231 params.suck_time = 0.10;
232 params.suck_power = 10.0;
Austin Schuh700b9222015-03-01 03:03:15 -0800233
234 params.claw_settle_time = 0.05;
235 params.claw_settle_power = 5.0;
236 params.claw_full_lift_angle = 1.35;
237 params.claw_end_angle = 0.5;
Austin Schuhf14727e2015-03-08 18:49:12 -0700238
Brian Silverman10043572015-03-21 23:43:50 -0700239 // End low so we don't drop it.
240 params.elevator_end_height = 0.3;
241 params.arm_end_angle = 0.0;
Austin Schuh700b9222015-03-01 03:03:15 -0800242 action_queue_.EnqueueAction(
243 actors::MakeHorizontalCanPickupAction(params));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800244 fridge_closed_ = true;
245 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700246
247 // -0.942503 arm, 0.374752 elevator
248 // Vertical can pickup.
249 if (data.PosEdge(kCanPickup)) {
250 actors::CanPickupParams params;
Austin Schuh90f28542015-03-29 13:46:03 -0700251 params.pickup_x = 0.6;
252 params.pickup_y = 0.1;
253 params.lift_height = 0.2;
254 params.pickup_goal_before_move_height = 0.3;
255 params.start_lowering_x = 0.1;
Brian Silverman10043572015-03-21 23:43:50 -0700256 // End low so the can is supported.
Austin Schuh90f28542015-03-29 13:46:03 -0700257 params.before_place_height = 0.4;
Brian Silverman10043572015-03-21 23:43:50 -0700258 params.end_height = 0.3;
259 params.end_angle = 0.0;
Austin Schuhf14727e2015-03-08 18:49:12 -0700260 action_queue_.EnqueueAction(actors::MakeCanPickupAction(params));
261 fridge_closed_ = true;
262 }
263
Brian Silvermancb615cf2015-03-21 21:42:35 -0700264 if (data.PosEdge(kCanReset)) {
265 action_queue_.CancelAllActions();
266 elevator_goal_ = 0.3;
267 arm_goal_ = 0.0;
268 }
269
Austin Schuhf14727e2015-03-08 18:49:12 -0700270 // Tote chute pull in when button is pressed, pack when done.
271 if (data.IsPressed(kToteChute)) {
Austin Schuhe301c712015-04-17 19:29:21 -0700272 claw_goal_ = 0.8;
273 intake_power = 6.0;
Austin Schuhf14727e2015-03-08 18:49:12 -0700274 }
275 if (data.NegEdge(kToteChute)) {
276 claw_goal_ = kClawTotePackAngle;
277 }
278
279 if (data.PosEdge(kStackAndLift)) {
280 actors::StackAndLiftParams params;
Austin Schuh959d2022015-03-29 13:51:11 -0700281 params.stack_params.claw_out_angle = kClawTotePackAngle;
Austin Schuhf14727e2015-03-08 18:49:12 -0700282 params.stack_params.bottom = 0.020;
283 params.stack_params.over_box_before_place_height = 0.39;
Brian Silverman10043572015-03-21 23:43:50 -0700284 params.stack_params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700285
286 params.grab_after_stack = true;
Austin Schuh959d2022015-03-29 13:51:11 -0700287 params.clamp_pause_time = 0.0;
Brian Silverman697d07b2015-03-19 23:41:11 -0700288 params.lift_params.lift_height = kStackUpHeight;
289 params.lift_params.lift_arm = kStackUpArm;
Austin Schuhf14727e2015-03-08 18:49:12 -0700290 params.grab_after_lift = true;
291 fridge_closed_ = true;
292
293 action_queue_.EnqueueAction(actors::MakeStackAndLiftAction(params));
294 }
295
Brian Silverman10043572015-03-21 23:43:50 -0700296 if (data.PosEdge(kStackAndHold) || data.PosEdge(kSetStackDownAndHold)) {
Austin Schuhf14727e2015-03-08 18:49:12 -0700297 actors::StackAndHoldParams params;
298 params.bottom = 0.020;
299 params.over_box_before_place_height = 0.39;
300
Brian Silverman10043572015-03-21 23:43:50 -0700301 params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700302 params.clamp_pause_time = 0.25;
Brian Silverman10043572015-03-21 23:43:50 -0700303 params.claw_clamp_angle = kClawTotePackAngle;
Austin Schuhf14727e2015-03-08 18:49:12 -0700304
305 params.hold_height = 0.68;
Austin Schuh959d2022015-03-29 13:51:11 -0700306 params.claw_out_angle = kClawTotePackAngle;
Brian Silverman10043572015-03-21 23:43:50 -0700307
308 if (data.PosEdge(kSetStackDownAndHold)) {
309 params.place_not_stack = true;
310 params.clamp_pause_time = 0.1;
311 //params.claw_clamp_angle = kClawTotePackAngle - 0.5;
312 } else {
313 params.place_not_stack = false;
314 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700315
316 action_queue_.EnqueueAction(actors::MakeStackAndHoldAction(params));
317 fridge_closed_ = true;
318 }
319
320 // TODO(austin): Figure out what action needed to pull the 6th tote into the
321 // claw.
322
323 // Lower the fridge from holding the stack, grab the stack, and then lift.
324 if (data.PosEdge(kHeldToLift)) {
325 actors::HeldToLiftParams params;
Austin Schuh959d2022015-03-29 13:51:11 -0700326 params.arm_clearance = kClawStackClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700327 params.clamp_pause_time = 0.1;
Brian Silverman10043572015-03-21 23:43:50 -0700328 params.before_lift_settle_time = 0.1;
Austin Schuhf14727e2015-03-08 18:49:12 -0700329 params.bottom_height = 0.020;
Austin Schuh959d2022015-03-29 13:51:11 -0700330 params.claw_out_angle = kClawTotePackAngle;
Brian Silverman697d07b2015-03-19 23:41:11 -0700331 params.lift_params.lift_height = kStackUpHeight;
332 params.lift_params.lift_arm = kStackUpArm;
Austin Schuhf14727e2015-03-08 18:49:12 -0700333 fridge_closed_ = true;
334
335 action_queue_.EnqueueAction(actors::MakeHeldToLiftAction(params));
336 }
337
Brian Silverman10043572015-03-21 23:43:50 -0700338 // Lift the can up.
339 if (data.PosEdge(kCanUp)) {
340 actors::LiftParams params;
341 params.lift_height = 0.68;
342 params.lift_arm = 0.3;
Brian Silverman697d07b2015-03-19 23:41:11 -0700343 params.pack_claw = false;
344 params.pack_claw_angle = 0;
Brian Silverman10043572015-03-21 23:43:50 -0700345 fridge_closed_ = true;
346
347 action_queue_.EnqueueAction(actors::MakeLiftAction(params));
348 }
349
Austin Schuhf14727e2015-03-08 18:49:12 -0700350 // Pick up a tote from the ground and put it in the bottom of the bot.
351 if (data.PosEdge(kPickup)) {
352 actors::PickupParams params;
353 // Lift to here initially.
354 params.pickup_angle = 0.9;
355 // Start sucking here
356 params.suck_angle = 0.8;
357 // Go back down to here to finish sucking.
358 params.suck_angle_finish = 0.4;
359 // Pack the box back in here.
360 params.pickup_finish_angle = kClawTotePackAngle;
361 params.intake_time = 0.8;
362 params.intake_voltage = 7.0;
363 action_queue_.EnqueueAction(actors::MakePickupAction(params));
364 }
365
366 // Place stack on a tote in the tray, and grab it.
367 if (data.PosEdge(kStack)) {
368 actors::StackParams params;
Austin Schuh959d2022015-03-29 13:51:11 -0700369 params.claw_out_angle = kClawTotePackAngle;
Austin Schuhf14727e2015-03-08 18:49:12 -0700370 params.bottom = 0.020;
Brian Silverman10043572015-03-21 23:43:50 -0700371 params.only_place = false;
372 params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700373 params.over_box_before_place_height = 0.39;
374 action_queue_.EnqueueAction(actors::MakeStackAction(params));
375 claw_rollers_closed_ = true;
Brian Silverman10043572015-03-21 23:43:50 -0700376 fridge_closed_ = true;
Austin Schuhf14727e2015-03-08 18:49:12 -0700377 }
378
Brian Silverman10043572015-03-21 23:43:50 -0700379 if (data.PosEdge(kScore)) {
Brian Silverman913eafa2015-03-19 23:42:16 -0700380 action_queue_.EnqueueAction(
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700381 actors::MakeScoreAction(MakeScoreParams()));
Brian Silverman913eafa2015-03-19 23:42:16 -0700382 fridge_closed_ = false;
Brian Silverman10043572015-03-21 23:43:50 -0700383 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700384
Brian Silverman913eafa2015-03-19 23:42:16 -0700385 if (data.PosEdge(kCoopTop)) {
386 action_queue_.EnqueueAction(
387 actors::MakeScoreAction(MakeCoopTopParams(false)));
388 }
389 if (data.PosEdge(kCoopTopRetract)) {
390 action_queue_.EnqueueAction(
391 actors::MakeScoreAction(MakeCoopTopParams(true)));
392 fridge_closed_ = false;
393 }
394
395 if (data.PosEdge(kCoopBottom)) {
396 action_queue_.EnqueueAction(
397 actors::MakeScoreAction(MakeCoopBottomParams(false)));
398 }
399 if (data.PosEdge(kCoopBottomRetract)) {
400 action_queue_.EnqueueAction(
401 actors::MakeScoreAction(MakeCoopBottomParams(true)));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800402 fridge_closed_ = false;
403 }
404
Brian Silverman10043572015-03-21 23:43:50 -0700405 if (data.PosEdge(kFridgeToggle)) {
406 fridge_closed_ = !fridge_closed_;
407 }
408
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800409 if (data.PosEdge(ControlBit::kEnabled)) {
410 // If we got enabled, wait for everything to zero.
411 LOG(INFO, "Waiting for zero.\n");
412 waiting_for_zero_ = true;
413 }
414
Austin Schuh700b9222015-03-01 03:03:15 -0800415 claw_queue.status.FetchLatest();
416 fridge_queue.status.FetchLatest();
417 if (!claw_queue.status.get()) {
418 LOG(ERROR, "Got no claw status packet.\n");
Austin Schuh700b9222015-03-01 03:03:15 -0800419 }
420 if (!fridge_queue.status.get()) {
421 LOG(ERROR, "Got no fridge status packet.\n");
Austin Schuh700b9222015-03-01 03:03:15 -0800422 }
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800423
Brian Silvermanea91f592015-03-28 18:25:57 -0400424 if (claw_queue.status.get() && fridge_queue.status.get() &&
425 claw_queue.status->zeroed && fridge_queue.status->zeroed) {
Austin Schuh700b9222015-03-01 03:03:15 -0800426 if (waiting_for_zero_) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800427 LOG(INFO, "Zeroed! Starting teleop mode.\n");
428 waiting_for_zero_ = false;
429
430 // Set the initial goals to where we are now.
Austin Schuh2cee0b52015-03-29 13:41:39 -0700431 elevator_goal_ = 0.3;
432 arm_goal_ = 0.0;
433 claw_goal_ = 0.6;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800434 }
435 } else {
Austin Schuh700b9222015-03-01 03:03:15 -0800436 waiting_for_zero_ = true;
Austin Schuh700b9222015-03-01 03:03:15 -0800437 }
438
439 if (!waiting_for_zero_) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800440 if (!action_queue_.Running()) {
441 auto new_fridge_goal = fridge_queue.goal.MakeMessage();
Austin Schuh6e242ac2015-03-07 17:08:21 -0800442 new_fridge_goal->max_velocity = elevator_params_.velocity;
443 new_fridge_goal->max_acceleration = elevator_params_.acceleration;
Austin Schuh1d44bd42015-03-15 16:40:45 -0700444 new_fridge_goal->profiling_type = 0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800445 new_fridge_goal->height = elevator_goal_;
Austin Schuh6e242ac2015-03-07 17:08:21 -0800446 new_fridge_goal->velocity = 0.0;
447 new_fridge_goal->max_angular_velocity = arm_params_.velocity;
448 new_fridge_goal->max_angular_acceleration = arm_params_.acceleration;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800449 new_fridge_goal->angle = arm_goal_;
450 new_fridge_goal->angular_velocity = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800451 new_fridge_goal->grabbers.top_front = fridge_closed_;
452 new_fridge_goal->grabbers.top_back = fridge_closed_;
453 new_fridge_goal->grabbers.bottom_front = fridge_closed_;
454 new_fridge_goal->grabbers.bottom_back = fridge_closed_;
455
456 if (!new_fridge_goal.Send()) {
457 LOG(ERROR, "Sending fridge goal failed.\n");
458 } else {
459 LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_,
460 arm_goal_);
461 }
462 if (!claw_queue.goal.MakeWithBuilder()
463 .angle(claw_goal_)
464 .rollers_closed(claw_rollers_closed_)
Austin Schuha534ebe2015-03-29 18:23:35 -0700465 .max_velocity(5.0)
Brian Silverman10043572015-03-21 23:43:50 -0700466 .max_acceleration(6.0)
Austin Schuhf14727e2015-03-08 18:49:12 -0700467 .intake(intake_power)
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800468 .Send()) {
469 LOG(ERROR, "Sending claw goal failed.\n");
470 }
471 }
472 }
473
Austin Schuhd8b2a242015-02-22 21:46:53 -0800474 if (action_queue_.Running()) {
475 // If we are running an action, update our goals to the current goals.
476 control_loops::fridge_queue.status.FetchLatest();
477 if (control_loops::fridge_queue.status.get()) {
478 arm_goal_ = control_loops::fridge_queue.status->goal_angle;
479 elevator_goal_ = control_loops::fridge_queue.status->goal_height;
480 } else {
481 LOG(ERROR, "No fridge status!\n");
482 }
483
484 // If we are running an action, update our goals to the current goals.
485 control_loops::claw_queue.status.FetchLatest();
486 if (control_loops::claw_queue.status.get()) {
487 claw_goal_ = control_loops::claw_queue.status->goal_angle;
488 } else {
Brian Silvermanea91f592015-03-28 18:25:57 -0400489 LOG(ERROR, "No claw status!\n");
Austin Schuhd8b2a242015-02-22 21:46:53 -0800490 }
491 }
Brian Silverman20141f92015-01-05 17:39:01 -0800492 action_queue_.Tick();
493 was_running_ = action_queue_.Running();
494 }
495
496 private:
Austin Schuh6182f8d2015-02-14 22:15:04 -0800497 void StartAuto() {
498 LOG(INFO, "Starting auto mode\n");
499 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
500 }
501
502 void StopAuto() {
503 LOG(INFO, "Stopping auto mode\n");
504 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
505 }
506
Brian Silverman20141f92015-01-05 17:39:01 -0800507 bool was_running_;
Daniel Petti61896522015-02-15 18:01:43 -0800508
509 // Previous goals for systems.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800510 double elevator_goal_ = 0.2;
Daniel Petti61896522015-02-15 18:01:43 -0800511 double arm_goal_ = 0.0;
512 double claw_goal_ = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800513 bool claw_rollers_closed_ = false;
514 bool fridge_closed_ = false;
Austin Schuh6e242ac2015-03-07 17:08:21 -0800515 actors::ProfileParams arm_params_ = kArmMove;
516 actors::ProfileParams elevator_params_ = kElevatorMove;
Daniel Petti61896522015-02-15 18:01:43 -0800517
518 // If we're waiting for the subsystems to zero.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800519 bool waiting_for_zero_ = true;
Daniel Petti61896522015-02-15 18:01:43 -0800520
Austin Schuh6182f8d2015-02-14 22:15:04 -0800521 bool auto_running_ = false;
522
Austin Schuh331e13d2015-02-15 00:16:51 -0800523 ::aos::common::actions::ActionQueue action_queue_;
Brian Silverman20141f92015-01-05 17:39:01 -0800524
525 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
526 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
527 "no drivetrain status");
528};
529
530} // namespace joysticks
531} // namespace input
532} // namespace frc971
533
534int main() {
535 ::aos::Init();
536 ::frc971::input::joysticks::Reader reader;
537 reader.Run();
538 ::aos::Cleanup();
539}