blob: 01b543156fff1b1a195baada0fb45fec88eeb2aa [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
Brian Silvermanb691f5e2015-08-02 11:37:55 -070014#include "y2015/control_loops/claw/claw.q.h"
15#include "y2015/control_loops/drivetrain/drivetrain.q.h"
16#include "y2015/control_loops/fridge/fridge.q.h"
17#include "y2015/constants.h"
Brian Silverman20141f92015-01-05 17:39:01 -080018#include "frc971/queues/gyro.q.h"
19#include "frc971/autonomous/auto.q.h"
Brian Silvermanb691f5e2015-08-02 11:37:55 -070020#include "y2015/actors/pickup_actor.h"
21#include "y2015/actors/stack_actor.h"
22#include "y2015/actors/score_actor.h"
23#include "y2015/actors/stack_and_lift_actor.h"
24#include "y2015/actors/stack_and_hold_actor.h"
25#include "y2015/actors/held_to_lift_actor.h"
26#include "y2015/actors/lift_actor.h"
27#include "y2015/actors/can_pickup_actor.h"
28#include "y2015/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 Schuh4db921a2015-04-19 19:59:24 -0700230 params.pickup_angle = 0.80 + kHorizontalCanClawAngle;
231 params.spit_time = 0.01;
232 params.spit_power = -8.0;
233
Austin Schuha534ebe2015-03-29 18:23:35 -0700234 params.suck_time = 0.10;
235 params.suck_power = 10.0;
Austin Schuh700b9222015-03-01 03:03:15 -0800236
237 params.claw_settle_time = 0.05;
238 params.claw_settle_power = 5.0;
239 params.claw_full_lift_angle = 1.35;
240 params.claw_end_angle = 0.5;
Austin Schuhf14727e2015-03-08 18:49:12 -0700241
Brian Silverman10043572015-03-21 23:43:50 -0700242 // End low so we don't drop it.
Austin Schuh335fd0d2015-04-19 20:02:38 -0700243 params.elevator_end_height = 0.28;
Brian Silverman10043572015-03-21 23:43:50 -0700244 params.arm_end_angle = 0.0;
Austin Schuh700b9222015-03-01 03:03:15 -0800245 action_queue_.EnqueueAction(
246 actors::MakeHorizontalCanPickupAction(params));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800247 fridge_closed_ = true;
248 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700249
250 // -0.942503 arm, 0.374752 elevator
251 // Vertical can pickup.
252 if (data.PosEdge(kCanPickup)) {
253 actors::CanPickupParams params;
Austin Schuh90f28542015-03-29 13:46:03 -0700254 params.pickup_x = 0.6;
255 params.pickup_y = 0.1;
Austin Schuh335fd0d2015-04-19 20:02:38 -0700256 params.lift_height = 0.25;
Austin Schuh90f28542015-03-29 13:46:03 -0700257 params.pickup_goal_before_move_height = 0.3;
258 params.start_lowering_x = 0.1;
Brian Silverman10043572015-03-21 23:43:50 -0700259 // End low so the can is supported.
Austin Schuh90f28542015-03-29 13:46:03 -0700260 params.before_place_height = 0.4;
Austin Schuh335fd0d2015-04-19 20:02:38 -0700261 params.end_height = 0.28;
Brian Silverman10043572015-03-21 23:43:50 -0700262 params.end_angle = 0.0;
Austin Schuhf14727e2015-03-08 18:49:12 -0700263 action_queue_.EnqueueAction(actors::MakeCanPickupAction(params));
264 fridge_closed_ = true;
265 }
266
Brian Silvermancb615cf2015-03-21 21:42:35 -0700267 if (data.PosEdge(kCanReset)) {
268 action_queue_.CancelAllActions();
Austin Schuh335fd0d2015-04-19 20:02:38 -0700269 elevator_goal_ = 0.28;
Brian Silvermancb615cf2015-03-21 21:42:35 -0700270 arm_goal_ = 0.0;
271 }
272
Austin Schuhf14727e2015-03-08 18:49:12 -0700273 // Tote chute pull in when button is pressed, pack when done.
274 if (data.IsPressed(kToteChute)) {
Austin Schuhe301c712015-04-17 19:29:21 -0700275 claw_goal_ = 0.8;
276 intake_power = 6.0;
Austin Schuhf14727e2015-03-08 18:49:12 -0700277 }
278 if (data.NegEdge(kToteChute)) {
279 claw_goal_ = kClawTotePackAngle;
280 }
281
282 if (data.PosEdge(kStackAndLift)) {
283 actors::StackAndLiftParams params;
Austin Schuh959d2022015-03-29 13:51:11 -0700284 params.stack_params.claw_out_angle = kClawTotePackAngle;
Austin Schuhf14727e2015-03-08 18:49:12 -0700285 params.stack_params.bottom = 0.020;
286 params.stack_params.over_box_before_place_height = 0.39;
Brian Silverman10043572015-03-21 23:43:50 -0700287 params.stack_params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700288
289 params.grab_after_stack = true;
Austin Schuh959d2022015-03-29 13:51:11 -0700290 params.clamp_pause_time = 0.0;
Brian Silverman697d07b2015-03-19 23:41:11 -0700291 params.lift_params.lift_height = kStackUpHeight;
292 params.lift_params.lift_arm = kStackUpArm;
Austin Schuh3cba3792015-04-19 20:01:22 -0700293 params.lift_params.second_lift = false;
Austin Schuhf14727e2015-03-08 18:49:12 -0700294 params.grab_after_lift = true;
295 fridge_closed_ = true;
296
297 action_queue_.EnqueueAction(actors::MakeStackAndLiftAction(params));
298 }
299
Brian Silverman10043572015-03-21 23:43:50 -0700300 if (data.PosEdge(kStackAndHold) || data.PosEdge(kSetStackDownAndHold)) {
Austin Schuhf14727e2015-03-08 18:49:12 -0700301 actors::StackAndHoldParams params;
302 params.bottom = 0.020;
303 params.over_box_before_place_height = 0.39;
304
Brian Silverman10043572015-03-21 23:43:50 -0700305 params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700306 params.clamp_pause_time = 0.25;
Brian Silverman10043572015-03-21 23:43:50 -0700307 params.claw_clamp_angle = kClawTotePackAngle;
Austin Schuhf14727e2015-03-08 18:49:12 -0700308
309 params.hold_height = 0.68;
Austin Schuh959d2022015-03-29 13:51:11 -0700310 params.claw_out_angle = kClawTotePackAngle;
Brian Silverman10043572015-03-21 23:43:50 -0700311
312 if (data.PosEdge(kSetStackDownAndHold)) {
313 params.place_not_stack = true;
314 params.clamp_pause_time = 0.1;
315 //params.claw_clamp_angle = kClawTotePackAngle - 0.5;
316 } else {
317 params.place_not_stack = false;
318 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700319
320 action_queue_.EnqueueAction(actors::MakeStackAndHoldAction(params));
321 fridge_closed_ = true;
322 }
323
324 // TODO(austin): Figure out what action needed to pull the 6th tote into the
325 // claw.
326
327 // Lower the fridge from holding the stack, grab the stack, and then lift.
328 if (data.PosEdge(kHeldToLift)) {
329 actors::HeldToLiftParams params;
Austin Schuh4fef0df2015-04-17 19:29:32 -0700330 params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700331 params.clamp_pause_time = 0.1;
Brian Silverman10043572015-03-21 23:43:50 -0700332 params.before_lift_settle_time = 0.1;
Austin Schuhf14727e2015-03-08 18:49:12 -0700333 params.bottom_height = 0.020;
Austin Schuh4fef0df2015-04-17 19:29:32 -0700334 params.claw_out_angle = kClawStackClearance;
Brian Silverman697d07b2015-03-19 23:41:11 -0700335 params.lift_params.lift_height = kStackUpHeight;
336 params.lift_params.lift_arm = kStackUpArm;
Austin Schuh3cba3792015-04-19 20:01:22 -0700337 params.lift_params.second_lift = false;
Austin Schuhf14727e2015-03-08 18:49:12 -0700338 fridge_closed_ = true;
339
340 action_queue_.EnqueueAction(actors::MakeHeldToLiftAction(params));
341 }
342
Brian Silverman10043572015-03-21 23:43:50 -0700343 // Lift the can up.
344 if (data.PosEdge(kCanUp)) {
345 actors::LiftParams params;
346 params.lift_height = 0.68;
347 params.lift_arm = 0.3;
Brian Silverman697d07b2015-03-19 23:41:11 -0700348 params.pack_claw = false;
349 params.pack_claw_angle = 0;
Austin Schuh3cba3792015-04-19 20:01:22 -0700350 params.intermediate_lift_height = 0.37;
351 params.second_lift = true;
Brian Silverman10043572015-03-21 23:43:50 -0700352 fridge_closed_ = true;
353
354 action_queue_.EnqueueAction(actors::MakeLiftAction(params));
355 }
356
Austin Schuhf14727e2015-03-08 18:49:12 -0700357 // Pick up a tote from the ground and put it in the bottom of the bot.
358 if (data.PosEdge(kPickup)) {
359 actors::PickupParams params;
Austin Schuh7f925ac2015-04-18 22:56:22 -0700360 // TODO(austin): These parameters are coppied into auto right now, need
361 // to dedup...
362
Austin Schuhf14727e2015-03-08 18:49:12 -0700363 // Lift to here initially.
364 params.pickup_angle = 0.9;
365 // Start sucking here
366 params.suck_angle = 0.8;
367 // Go back down to here to finish sucking.
368 params.suck_angle_finish = 0.4;
369 // Pack the box back in here.
370 params.pickup_finish_angle = kClawTotePackAngle;
371 params.intake_time = 0.8;
372 params.intake_voltage = 7.0;
373 action_queue_.EnqueueAction(actors::MakePickupAction(params));
374 }
375
376 // Place stack on a tote in the tray, and grab it.
377 if (data.PosEdge(kStack)) {
378 actors::StackParams params;
Austin Schuh959d2022015-03-29 13:51:11 -0700379 params.claw_out_angle = kClawTotePackAngle;
Austin Schuhf14727e2015-03-08 18:49:12 -0700380 params.bottom = 0.020;
Brian Silverman10043572015-03-21 23:43:50 -0700381 params.only_place = false;
382 params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700383 params.over_box_before_place_height = 0.39;
384 action_queue_.EnqueueAction(actors::MakeStackAction(params));
385 claw_rollers_closed_ = true;
Brian Silverman10043572015-03-21 23:43:50 -0700386 fridge_closed_ = true;
Austin Schuhf14727e2015-03-08 18:49:12 -0700387 }
388
Brian Silverman10043572015-03-21 23:43:50 -0700389 if (data.PosEdge(kScore)) {
Brian Silverman913eafa2015-03-19 23:42:16 -0700390 action_queue_.EnqueueAction(
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700391 actors::MakeScoreAction(MakeScoreParams()));
Brian Silverman913eafa2015-03-19 23:42:16 -0700392 fridge_closed_ = false;
Brian Silverman10043572015-03-21 23:43:50 -0700393 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700394
Brian Silverman913eafa2015-03-19 23:42:16 -0700395 if (data.PosEdge(kCoopTop)) {
396 action_queue_.EnqueueAction(
397 actors::MakeScoreAction(MakeCoopTopParams(false)));
398 }
399 if (data.PosEdge(kCoopTopRetract)) {
400 action_queue_.EnqueueAction(
401 actors::MakeScoreAction(MakeCoopTopParams(true)));
402 fridge_closed_ = false;
403 }
404
405 if (data.PosEdge(kCoopBottom)) {
406 action_queue_.EnqueueAction(
407 actors::MakeScoreAction(MakeCoopBottomParams(false)));
408 }
409 if (data.PosEdge(kCoopBottomRetract)) {
410 action_queue_.EnqueueAction(
411 actors::MakeScoreAction(MakeCoopBottomParams(true)));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800412 fridge_closed_ = false;
413 }
414
Brian Silverman10043572015-03-21 23:43:50 -0700415 if (data.PosEdge(kFridgeToggle)) {
416 fridge_closed_ = !fridge_closed_;
417 }
418
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800419 if (data.PosEdge(ControlBit::kEnabled)) {
420 // If we got enabled, wait for everything to zero.
421 LOG(INFO, "Waiting for zero.\n");
422 waiting_for_zero_ = true;
423 }
424
Austin Schuh700b9222015-03-01 03:03:15 -0800425 claw_queue.status.FetchLatest();
426 fridge_queue.status.FetchLatest();
427 if (!claw_queue.status.get()) {
428 LOG(ERROR, "Got no claw status packet.\n");
Austin Schuh700b9222015-03-01 03:03:15 -0800429 }
430 if (!fridge_queue.status.get()) {
431 LOG(ERROR, "Got no fridge status packet.\n");
Austin Schuh700b9222015-03-01 03:03:15 -0800432 }
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800433
Brian Silvermanea91f592015-03-28 18:25:57 -0400434 if (claw_queue.status.get() && fridge_queue.status.get() &&
435 claw_queue.status->zeroed && fridge_queue.status->zeroed) {
Austin Schuh700b9222015-03-01 03:03:15 -0800436 if (waiting_for_zero_) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800437 LOG(INFO, "Zeroed! Starting teleop mode.\n");
438 waiting_for_zero_ = false;
439
440 // Set the initial goals to where we are now.
Austin Schuh2cee0b52015-03-29 13:41:39 -0700441 elevator_goal_ = 0.3;
442 arm_goal_ = 0.0;
443 claw_goal_ = 0.6;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800444 }
445 } else {
Austin Schuh700b9222015-03-01 03:03:15 -0800446 waiting_for_zero_ = true;
Austin Schuh700b9222015-03-01 03:03:15 -0800447 }
448
449 if (!waiting_for_zero_) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800450 if (!action_queue_.Running()) {
451 auto new_fridge_goal = fridge_queue.goal.MakeMessage();
Austin Schuh6e242ac2015-03-07 17:08:21 -0800452 new_fridge_goal->max_velocity = elevator_params_.velocity;
453 new_fridge_goal->max_acceleration = elevator_params_.acceleration;
Austin Schuh1d44bd42015-03-15 16:40:45 -0700454 new_fridge_goal->profiling_type = 0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800455 new_fridge_goal->height = elevator_goal_;
Austin Schuh6e242ac2015-03-07 17:08:21 -0800456 new_fridge_goal->velocity = 0.0;
457 new_fridge_goal->max_angular_velocity = arm_params_.velocity;
458 new_fridge_goal->max_angular_acceleration = arm_params_.acceleration;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800459 new_fridge_goal->angle = arm_goal_;
460 new_fridge_goal->angular_velocity = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800461 new_fridge_goal->grabbers.top_front = fridge_closed_;
462 new_fridge_goal->grabbers.top_back = fridge_closed_;
463 new_fridge_goal->grabbers.bottom_front = fridge_closed_;
464 new_fridge_goal->grabbers.bottom_back = fridge_closed_;
465
466 if (!new_fridge_goal.Send()) {
467 LOG(ERROR, "Sending fridge goal failed.\n");
468 } else {
469 LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_,
470 arm_goal_);
471 }
472 if (!claw_queue.goal.MakeWithBuilder()
473 .angle(claw_goal_)
474 .rollers_closed(claw_rollers_closed_)
Austin Schuha534ebe2015-03-29 18:23:35 -0700475 .max_velocity(5.0)
Brian Silverman10043572015-03-21 23:43:50 -0700476 .max_acceleration(6.0)
Austin Schuhf14727e2015-03-08 18:49:12 -0700477 .intake(intake_power)
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800478 .Send()) {
479 LOG(ERROR, "Sending claw goal failed.\n");
480 }
481 }
482 }
483
Austin Schuhd8b2a242015-02-22 21:46:53 -0800484 if (action_queue_.Running()) {
485 // If we are running an action, update our goals to the current goals.
486 control_loops::fridge_queue.status.FetchLatest();
487 if (control_loops::fridge_queue.status.get()) {
488 arm_goal_ = control_loops::fridge_queue.status->goal_angle;
489 elevator_goal_ = control_loops::fridge_queue.status->goal_height;
490 } else {
491 LOG(ERROR, "No fridge status!\n");
492 }
493
494 // If we are running an action, update our goals to the current goals.
495 control_loops::claw_queue.status.FetchLatest();
496 if (control_loops::claw_queue.status.get()) {
497 claw_goal_ = control_loops::claw_queue.status->goal_angle;
498 } else {
Brian Silvermanea91f592015-03-28 18:25:57 -0400499 LOG(ERROR, "No claw status!\n");
Austin Schuhd8b2a242015-02-22 21:46:53 -0800500 }
501 }
Brian Silverman20141f92015-01-05 17:39:01 -0800502 action_queue_.Tick();
503 was_running_ = action_queue_.Running();
504 }
505
506 private:
Austin Schuh6182f8d2015-02-14 22:15:04 -0800507 void StartAuto() {
508 LOG(INFO, "Starting auto mode\n");
509 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
510 }
511
512 void StopAuto() {
513 LOG(INFO, "Stopping auto mode\n");
514 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
515 }
516
Brian Silverman20141f92015-01-05 17:39:01 -0800517 bool was_running_;
Daniel Petti61896522015-02-15 18:01:43 -0800518
519 // Previous goals for systems.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800520 double elevator_goal_ = 0.2;
Daniel Petti61896522015-02-15 18:01:43 -0800521 double arm_goal_ = 0.0;
522 double claw_goal_ = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800523 bool claw_rollers_closed_ = false;
524 bool fridge_closed_ = false;
Austin Schuh6e242ac2015-03-07 17:08:21 -0800525 actors::ProfileParams arm_params_ = kArmMove;
526 actors::ProfileParams elevator_params_ = kElevatorMove;
Daniel Petti61896522015-02-15 18:01:43 -0800527
528 // If we're waiting for the subsystems to zero.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800529 bool waiting_for_zero_ = true;
Daniel Petti61896522015-02-15 18:01:43 -0800530
Austin Schuh6182f8d2015-02-14 22:15:04 -0800531 bool auto_running_ = false;
532
Austin Schuh331e13d2015-02-15 00:16:51 -0800533 ::aos::common::actions::ActionQueue action_queue_;
Brian Silverman20141f92015-01-05 17:39:01 -0800534
535 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
536 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
537 "no drivetrain status");
538};
539
540} // namespace joysticks
541} // namespace input
542} // namespace frc971
543
544int main() {
545 ::aos::Init();
546 ::frc971::input::joysticks::Reader reader;
547 reader.Run();
548 ::aos::Cleanup();
549}