blob: 7cbd7e55c0cbce499f95cdf2b75d9f82cc397008 [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"
Brian Silvermanc2065732015-11-28 22:55:30 +00007#include "aos/input/joystick_input.h"
Brian Silverman20141f92015-01-05 17:39:01 -08008#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"
Austin Schuh70810b92016-11-26 14:55:34 -080015#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Brian Silvermanb691f5e2015-08-02 11:37:55 -070016#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"
Austin Schuhbb227f82015-09-06 15:27:52 -070020#include "y2015/autonomous/auto.q.h"
Brian Silvermanb691f5e2015-08-02 11:37:55 -070021#include "y2015/actors/pickup_actor.h"
22#include "y2015/actors/stack_actor.h"
23#include "y2015/actors/score_actor.h"
24#include "y2015/actors/stack_and_lift_actor.h"
25#include "y2015/actors/stack_and_hold_actor.h"
26#include "y2015/actors/held_to_lift_actor.h"
27#include "y2015/actors/lift_actor.h"
28#include "y2015/actors/can_pickup_actor.h"
29#include "y2015/actors/horizontal_can_pickup_actor.h"
Brian Silverman20141f92015-01-05 17:39:01 -080030
Daniel Petti61896522015-02-15 18:01:43 -080031using ::frc971::control_loops::claw_queue;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050032using ::frc971::control_loops::drivetrain_queue;
Daniel Petti61896522015-02-15 18:01:43 -080033using ::frc971::control_loops::fridge_queue;
Brian Silverman20141f92015-01-05 17:39:01 -080034using ::frc971::sensors::gyro_reading;
35
36using ::aos::input::driver_station::ButtonLocation;
Austin Schuhf14727e2015-03-08 18:49:12 -070037using ::aos::input::driver_station::POVLocation;
Brian Silverman20141f92015-01-05 17:39:01 -080038using ::aos::input::driver_station::JoystickAxis;
39using ::aos::input::driver_station::ControlBit;
40
41namespace frc971 {
42namespace input {
43namespace joysticks {
44
Austin Schuhf14727e2015-03-08 18:49:12 -070045// Actions needed.
46
47// Human Player Station Intake Button
48// Claw open
49// Claw close
50// Claw down
51
52// Stack + Lift (together)
53// Place
54
55// Hold stack
56
57// Horizontal can pickup
58// Vertical can pickup
59
60// TODO(austin): Pull a lot of the constants below out up here so they can be
61// globally changed easier.
62
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080063// preset motion limits
Brian Silvermancb615cf2015-03-21 21:42:35 -070064constexpr actors::ProfileParams kArmMove{0.5, 1.0};
65constexpr actors::ProfileParams kElevatorMove{0.3, 1.0};
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080066
Brian Silverman20141f92015-01-05 17:39:01 -080067const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080068const ButtonLocation kShiftHigh(2, 3), kShiftLow(2, 1);
Brian Silverman20141f92015-01-05 17:39:01 -080069const ButtonLocation kQuickTurn(1, 5);
70
Austin Schuhf14727e2015-03-08 18:49:12 -070071//const ButtonLocation kClawClosed(3, 5);
72//const ButtonLocation kFridgeClosed(3, 1);
Austin Schuhd8b2a242015-02-22 21:46:53 -080073
Daniel Petti61896522015-02-15 18:01:43 -080074
Austin Schuha534ebe2015-03-29 18:23:35 -070075const ButtonLocation kStackAndHold(3, 5);
76const ButtonLocation kRollersIn(3, 2);
77const ButtonLocation kHorizontalCanRollersIn(4, 5);
Austin Schuhf14727e2015-03-08 18:49:12 -070078const ButtonLocation kClawToggle(4, 1);
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080079
Brian Silverman10043572015-03-21 23:43:50 -070080const POVLocation kElevatorCanUp(4, 0);
Austin Schuhf14727e2015-03-08 18:49:12 -070081
82// POV stick 3, 180
83const POVLocation kCanPickup(4, 180);
84const ButtonLocation kToteChute(4, 6);
85const ButtonLocation kStackAndLift(4, 7);
Austin Schuhf14727e2015-03-08 18:49:12 -070086
87// Pull in the 6th tote.
Brian Silverman10043572015-03-21 23:43:50 -070088//const ButtonLocation kSixthTote(4, 10);
89const ButtonLocation kCanUp(4, 10);
Austin Schuhf14727e2015-03-08 18:49:12 -070090
91const ButtonLocation kHeldToLift(4, 11);
92const ButtonLocation kPickup(4, 9);
93
94const ButtonLocation kStack(4, 2);
95
96// Move the fridge out with the stack in preparation for scoring.
Brian Silverman10043572015-03-21 23:43:50 -070097const ButtonLocation kScore(4, 8);
Brian Silverman913eafa2015-03-19 23:42:16 -070098const ButtonLocation kCoopTop(3, 8);
99const ButtonLocation kCoopTopRetract(3, 7);
100const ButtonLocation kCoopBottom(3, 6);
Brian Silvermand4278302015-03-28 00:21:15 -0400101const ButtonLocation kCoopBottomRetract(4, 12);
Brian Silverman913eafa2015-03-19 23:42:16 -0700102
Brian Silvermand4278302015-03-28 00:21:15 -0400103const ButtonLocation kCanReset(3, 9);
Austin Schuhbb227f82015-09-06 15:27:52 -0700104const ButtonLocation kCanGrabberLift(2, 1);
105const ButtonLocation kFastCanGrabberLift(2, 3);
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700106
Brian Silverman10043572015-03-21 23:43:50 -0700107const POVLocation kFridgeToggle(4, 270);
Austin Schuhf14727e2015-03-08 18:49:12 -0700108const ButtonLocation kSpit(4, 3);
109
110// Set stack down in the bot.
Brian Silverman10043572015-03-21 23:43:50 -0700111const POVLocation kSetStackDownAndHold(4, 90);
Austin Schuhf14727e2015-03-08 18:49:12 -0700112
Austin Schuh959d2022015-03-29 13:51:11 -0700113const double kClawTotePackAngle = 0.90;
Brian Silverman10043572015-03-21 23:43:50 -0700114const double kArmRaiseLowerClearance = -0.08;
Brian Silverman654b5122015-03-20 16:58:04 -0700115const double kClawStackClearance = 0.55;
Austin Schuha534ebe2015-03-29 18:23:35 -0700116const double kHorizontalCanClawAngle = 0.12;
Brian Silverman20141f92015-01-05 17:39:01 -0800117
Austin Schuh959d2022015-03-29 13:51:11 -0700118const double kStackUpHeight = 0.60;
119const double kStackUpArm = 0.0;
Brian Silverman697d07b2015-03-19 23:41:11 -0700120
Brian Silverman20141f92015-01-05 17:39:01 -0800121class Reader : public ::aos::input::JoystickInput {
122 public:
Austin Schuh331e13d2015-02-15 00:16:51 -0800123 Reader() : was_running_(false) {}
Brian Silverman20141f92015-01-05 17:39:01 -0800124
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700125 static actors::ScoreParams MakeScoreParams() {
Brian Silverman913eafa2015-03-19 23:42:16 -0700126 actors::ScoreParams r;
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700127 r.move_the_stack = r.place_the_stack = true;
Brian Silverman913eafa2015-03-19 23:42:16 -0700128 r.upper_move_height = 0.14;
129 r.begin_horizontal_move_height = 0.13;
130 r.horizontal_move_target = -0.7;
Austin Schuh45ee2c52015-03-29 13:47:26 -0700131 r.horizontal_start_lowering = -0.65;
132 r.home_lift_horizontal_start_position = -0.60;
Brian Silverman913eafa2015-03-19 23:42:16 -0700133 r.place_height = -0.10;
Austin Schuh45ee2c52015-03-29 13:47:26 -0700134 r.home_return_height = 0.05;
Brian Silverman913eafa2015-03-19 23:42:16 -0700135 return r;
136 }
137
138 static actors::ScoreParams MakeCoopTopParams(bool place_the_stack) {
139 actors::ScoreParams r;
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700140 r.move_the_stack = !place_the_stack;
Brian Silverman913eafa2015-03-19 23:42:16 -0700141 r.place_the_stack = place_the_stack;
142 r.upper_move_height = 0.52;
143 r.begin_horizontal_move_height = 0.5;
144 r.horizontal_move_target = -0.48;
Austin Schuh45ee2c52015-03-29 13:47:26 -0700145 r.horizontal_start_lowering = r.horizontal_move_target;
146 r.home_lift_horizontal_start_position = -0.3;
Austin Schuha534ebe2015-03-29 18:23:35 -0700147 r.place_height = 0.35;
Brian Silverman913eafa2015-03-19 23:42:16 -0700148 r.home_return_height = 0.1;
149 return r;
150 }
151
152 static actors::ScoreParams MakeCoopBottomParams(bool place_the_stack) {
153 actors::ScoreParams r;
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700154 r.move_the_stack = !place_the_stack;
Brian Silverman913eafa2015-03-19 23:42:16 -0700155 r.place_the_stack = place_the_stack;
156 r.upper_move_height = 0.17;
157 r.begin_horizontal_move_height = 0.16;
158 r.horizontal_move_target = -0.7;
Austin Schuh45ee2c52015-03-29 13:47:26 -0700159 r.horizontal_start_lowering = r.horizontal_move_target;
160 r.home_lift_horizontal_start_position = -0.3;
Brian Silverman913eafa2015-03-19 23:42:16 -0700161 r.place_height = 0.0;
162 r.home_return_height = 0.1;
163 return r;
164 }
165
Brian Silverman20141f92015-01-05 17:39:01 -0800166 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Austin Schuh6182f8d2015-02-14 22:15:04 -0800167 bool last_auto_running = auto_running_;
168 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
Brian Silvermane1103e62015-02-15 02:03:38 -0500169 data.GetControlBit(ControlBit::kEnabled);
Austin Schuh6182f8d2015-02-14 22:15:04 -0800170 if (auto_running_ != last_auto_running) {
171 if (auto_running_) {
172 StartAuto();
173 } else {
174 StopAuto();
Brian Silverman20141f92015-01-05 17:39:01 -0800175 }
Austin Schuh6182f8d2015-02-14 22:15:04 -0800176 }
177
178 if (!data.GetControlBit(ControlBit::kAutonomous)) {
Daniel Petti61896522015-02-15 18:01:43 -0800179 HandleDrivetrain(data);
Austin Schuhf14727e2015-03-08 18:49:12 -0700180 HandleTeleop(data);
Brian Silverman20141f92015-01-05 17:39:01 -0800181 }
182 }
183
184 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -0800185 const double wheel = -data.GetAxis(kSteeringWheel);
186 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman20141f92015-01-05 17:39:01 -0800187
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500188 if (!drivetrain_queue.goal.MakeWithBuilder()
Brian Silverman20141f92015-01-05 17:39:01 -0800189 .steering(wheel)
190 .throttle(throttle)
Brian Silverman20141f92015-01-05 17:39:01 -0800191 .quickturn(data.IsPressed(kQuickTurn))
Austin Schuh331e13d2015-02-15 00:16:51 -0800192 .control_loop_driving(false)
Brian Silverman20141f92015-01-05 17:39:01 -0800193 .Send()) {
194 LOG(WARNING, "sending stick values failed\n");
195 }
Brian Silverman20141f92015-01-05 17:39:01 -0800196 }
197
198 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuhf14727e2015-03-08 18:49:12 -0700199 double intake_power = 0.0;
Brian Silverman20141f92015-01-05 17:39:01 -0800200 if (!data.GetControlBit(ControlBit::kEnabled)) {
201 action_queue_.CancelAllActions();
Austin Schuh6182f8d2015-02-14 22:15:04 -0800202 LOG(DEBUG, "Canceling\n");
Brian Silverman20141f92015-01-05 17:39:01 -0800203 }
Austin Schuhbb227f82015-09-06 15:27:52 -0700204 if (data.IsPressed(kCanGrabberLift)) {
205 autonomous::can_control.MakeWithBuilder().can_voltage(-4).Send();
206 } else if (data.IsPressed(kFastCanGrabberLift)) {
207 autonomous::can_control.MakeWithBuilder().can_voltage(-12).Send();
208 }
Brian Silverman20141f92015-01-05 17:39:01 -0800209
Austin Schuhf14727e2015-03-08 18:49:12 -0700210 if (data.IsPressed(kRollersIn)) {
211 intake_power = 10.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800212 claw_goal_ = 0.0;
Austin Schuhd8b2a242015-02-22 21:46:53 -0800213 }
Austin Schuha534ebe2015-03-29 18:23:35 -0700214 if (data.IsPressed(kHorizontalCanRollersIn)) {
215 intake_power = 10.0;
216 claw_goal_ = kHorizontalCanClawAngle;
217 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700218 if (data.IsPressed(kSpit)) {
219 intake_power = -12.0;
Austin Schuha534ebe2015-03-29 18:23:35 -0700220 action_queue_.CancelAllActions();
Austin Schuhd8b2a242015-02-22 21:46:53 -0800221 }
Austin Schuh994d42c2015-03-01 00:02:17 -0800222
Austin Schuhf14727e2015-03-08 18:49:12 -0700223 // Toggle button for the claw
224 if (data.PosEdge(kClawToggle)) {
225 claw_rollers_closed_ = !claw_rollers_closed_;
Austin Schuh994d42c2015-03-01 00:02:17 -0800226 }
227
Austin Schuhf14727e2015-03-08 18:49:12 -0700228 /*
229 if (data.IsPressed(kClawClosed)) {
230 claw_rollers_closed_ = true;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800231 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700232 */
Austin Schuh700b9222015-03-01 03:03:15 -0800233
Austin Schuhf14727e2015-03-08 18:49:12 -0700234 // Horizontal can pickup.
Austin Schuh700b9222015-03-01 03:03:15 -0800235 if (data.PosEdge(kElevatorCanUp)) {
236 actors::HorizontalCanPickupParams params;
237 params.elevator_height = 0.3;
Austin Schuh4db921a2015-04-19 19:59:24 -0700238 params.pickup_angle = 0.80 + kHorizontalCanClawAngle;
239 params.spit_time = 0.01;
240 params.spit_power = -8.0;
241
Austin Schuhbb227f82015-09-06 15:27:52 -0700242 params.suck_time = 0.040;
Austin Schuha534ebe2015-03-29 18:23:35 -0700243 params.suck_power = 10.0;
Austin Schuh700b9222015-03-01 03:03:15 -0800244
245 params.claw_settle_time = 0.05;
246 params.claw_settle_power = 5.0;
247 params.claw_full_lift_angle = 1.35;
248 params.claw_end_angle = 0.5;
Austin Schuhf14727e2015-03-08 18:49:12 -0700249
Brian Silverman10043572015-03-21 23:43:50 -0700250 // End low so we don't drop it.
Austin Schuh335fd0d2015-04-19 20:02:38 -0700251 params.elevator_end_height = 0.28;
Brian Silverman10043572015-03-21 23:43:50 -0700252 params.arm_end_angle = 0.0;
Austin Schuh700b9222015-03-01 03:03:15 -0800253 action_queue_.EnqueueAction(
254 actors::MakeHorizontalCanPickupAction(params));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800255 fridge_closed_ = true;
256 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700257
258 // -0.942503 arm, 0.374752 elevator
259 // Vertical can pickup.
260 if (data.PosEdge(kCanPickup)) {
261 actors::CanPickupParams params;
Austin Schuh90f28542015-03-29 13:46:03 -0700262 params.pickup_x = 0.6;
263 params.pickup_y = 0.1;
Austin Schuh335fd0d2015-04-19 20:02:38 -0700264 params.lift_height = 0.25;
Austin Schuh90f28542015-03-29 13:46:03 -0700265 params.pickup_goal_before_move_height = 0.3;
266 params.start_lowering_x = 0.1;
Brian Silverman10043572015-03-21 23:43:50 -0700267 // End low so the can is supported.
Austin Schuh90f28542015-03-29 13:46:03 -0700268 params.before_place_height = 0.4;
Austin Schuh335fd0d2015-04-19 20:02:38 -0700269 params.end_height = 0.28;
Brian Silverman10043572015-03-21 23:43:50 -0700270 params.end_angle = 0.0;
Austin Schuhf14727e2015-03-08 18:49:12 -0700271 action_queue_.EnqueueAction(actors::MakeCanPickupAction(params));
272 fridge_closed_ = true;
273 }
274
Brian Silvermancb615cf2015-03-21 21:42:35 -0700275 if (data.PosEdge(kCanReset)) {
276 action_queue_.CancelAllActions();
Austin Schuh335fd0d2015-04-19 20:02:38 -0700277 elevator_goal_ = 0.28;
Brian Silvermancb615cf2015-03-21 21:42:35 -0700278 arm_goal_ = 0.0;
279 }
280
Austin Schuhf14727e2015-03-08 18:49:12 -0700281 // Tote chute pull in when button is pressed, pack when done.
282 if (data.IsPressed(kToteChute)) {
Austin Schuhe301c712015-04-17 19:29:21 -0700283 claw_goal_ = 0.8;
284 intake_power = 6.0;
Austin Schuhf14727e2015-03-08 18:49:12 -0700285 }
286 if (data.NegEdge(kToteChute)) {
287 claw_goal_ = kClawTotePackAngle;
288 }
289
290 if (data.PosEdge(kStackAndLift)) {
291 actors::StackAndLiftParams params;
Austin Schuh959d2022015-03-29 13:51:11 -0700292 params.stack_params.claw_out_angle = kClawTotePackAngle;
Austin Schuhf14727e2015-03-08 18:49:12 -0700293 params.stack_params.bottom = 0.020;
294 params.stack_params.over_box_before_place_height = 0.39;
Brian Silverman10043572015-03-21 23:43:50 -0700295 params.stack_params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700296
297 params.grab_after_stack = true;
Austin Schuh959d2022015-03-29 13:51:11 -0700298 params.clamp_pause_time = 0.0;
Brian Silverman697d07b2015-03-19 23:41:11 -0700299 params.lift_params.lift_height = kStackUpHeight;
300 params.lift_params.lift_arm = kStackUpArm;
Austin Schuh3cba3792015-04-19 20:01:22 -0700301 params.lift_params.second_lift = false;
Austin Schuhf14727e2015-03-08 18:49:12 -0700302 params.grab_after_lift = true;
303 fridge_closed_ = true;
304
305 action_queue_.EnqueueAction(actors::MakeStackAndLiftAction(params));
306 }
307
Brian Silverman10043572015-03-21 23:43:50 -0700308 if (data.PosEdge(kStackAndHold) || data.PosEdge(kSetStackDownAndHold)) {
Austin Schuhf14727e2015-03-08 18:49:12 -0700309 actors::StackAndHoldParams params;
310 params.bottom = 0.020;
311 params.over_box_before_place_height = 0.39;
312
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.25;
Brian Silverman10043572015-03-21 23:43:50 -0700315 params.claw_clamp_angle = kClawTotePackAngle;
Austin Schuhf14727e2015-03-08 18:49:12 -0700316
317 params.hold_height = 0.68;
Austin Schuh959d2022015-03-29 13:51:11 -0700318 params.claw_out_angle = kClawTotePackAngle;
Brian Silverman10043572015-03-21 23:43:50 -0700319
320 if (data.PosEdge(kSetStackDownAndHold)) {
321 params.place_not_stack = true;
322 params.clamp_pause_time = 0.1;
323 //params.claw_clamp_angle = kClawTotePackAngle - 0.5;
324 } else {
325 params.place_not_stack = false;
326 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700327
328 action_queue_.EnqueueAction(actors::MakeStackAndHoldAction(params));
329 fridge_closed_ = true;
330 }
331
332 // TODO(austin): Figure out what action needed to pull the 6th tote into the
333 // claw.
334
335 // Lower the fridge from holding the stack, grab the stack, and then lift.
336 if (data.PosEdge(kHeldToLift)) {
337 actors::HeldToLiftParams params;
Austin Schuh4fef0df2015-04-17 19:29:32 -0700338 params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700339 params.clamp_pause_time = 0.1;
Brian Silverman10043572015-03-21 23:43:50 -0700340 params.before_lift_settle_time = 0.1;
Austin Schuhf14727e2015-03-08 18:49:12 -0700341 params.bottom_height = 0.020;
Austin Schuh4fef0df2015-04-17 19:29:32 -0700342 params.claw_out_angle = kClawStackClearance;
Brian Silverman697d07b2015-03-19 23:41:11 -0700343 params.lift_params.lift_height = kStackUpHeight;
344 params.lift_params.lift_arm = kStackUpArm;
Austin Schuh3cba3792015-04-19 20:01:22 -0700345 params.lift_params.second_lift = false;
Austin Schuhf14727e2015-03-08 18:49:12 -0700346 fridge_closed_ = true;
347
348 action_queue_.EnqueueAction(actors::MakeHeldToLiftAction(params));
349 }
350
Brian Silverman10043572015-03-21 23:43:50 -0700351 // Lift the can up.
352 if (data.PosEdge(kCanUp)) {
353 actors::LiftParams params;
354 params.lift_height = 0.68;
355 params.lift_arm = 0.3;
Brian Silverman697d07b2015-03-19 23:41:11 -0700356 params.pack_claw = false;
357 params.pack_claw_angle = 0;
Austin Schuh3cba3792015-04-19 20:01:22 -0700358 params.intermediate_lift_height = 0.37;
359 params.second_lift = true;
Brian Silverman10043572015-03-21 23:43:50 -0700360 fridge_closed_ = true;
361
362 action_queue_.EnqueueAction(actors::MakeLiftAction(params));
363 }
364
Austin Schuhf14727e2015-03-08 18:49:12 -0700365 // Pick up a tote from the ground and put it in the bottom of the bot.
366 if (data.PosEdge(kPickup)) {
367 actors::PickupParams params;
Austin Schuh7f925ac2015-04-18 22:56:22 -0700368 // TODO(austin): These parameters are coppied into auto right now, need
369 // to dedup...
370
Austin Schuhf14727e2015-03-08 18:49:12 -0700371 // Lift to here initially.
372 params.pickup_angle = 0.9;
373 // Start sucking here
374 params.suck_angle = 0.8;
375 // Go back down to here to finish sucking.
376 params.suck_angle_finish = 0.4;
377 // Pack the box back in here.
378 params.pickup_finish_angle = kClawTotePackAngle;
379 params.intake_time = 0.8;
380 params.intake_voltage = 7.0;
381 action_queue_.EnqueueAction(actors::MakePickupAction(params));
382 }
383
384 // Place stack on a tote in the tray, and grab it.
385 if (data.PosEdge(kStack)) {
386 actors::StackParams params;
Austin Schuh959d2022015-03-29 13:51:11 -0700387 params.claw_out_angle = kClawTotePackAngle;
Austin Schuhf14727e2015-03-08 18:49:12 -0700388 params.bottom = 0.020;
Brian Silverman10043572015-03-21 23:43:50 -0700389 params.only_place = false;
390 params.arm_clearance = kArmRaiseLowerClearance;
Austin Schuhf14727e2015-03-08 18:49:12 -0700391 params.over_box_before_place_height = 0.39;
392 action_queue_.EnqueueAction(actors::MakeStackAction(params));
393 claw_rollers_closed_ = true;
Brian Silverman10043572015-03-21 23:43:50 -0700394 fridge_closed_ = true;
Austin Schuhf14727e2015-03-08 18:49:12 -0700395 }
396
Brian Silverman10043572015-03-21 23:43:50 -0700397 if (data.PosEdge(kScore)) {
Brian Silverman913eafa2015-03-19 23:42:16 -0700398 action_queue_.EnqueueAction(
Brian Silvermanb9811eb2015-03-20 16:54:41 -0700399 actors::MakeScoreAction(MakeScoreParams()));
Brian Silverman913eafa2015-03-19 23:42:16 -0700400 fridge_closed_ = false;
Brian Silverman10043572015-03-21 23:43:50 -0700401 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700402
Brian Silverman913eafa2015-03-19 23:42:16 -0700403 if (data.PosEdge(kCoopTop)) {
404 action_queue_.EnqueueAction(
405 actors::MakeScoreAction(MakeCoopTopParams(false)));
406 }
407 if (data.PosEdge(kCoopTopRetract)) {
408 action_queue_.EnqueueAction(
409 actors::MakeScoreAction(MakeCoopTopParams(true)));
410 fridge_closed_ = false;
411 }
412
413 if (data.PosEdge(kCoopBottom)) {
414 action_queue_.EnqueueAction(
415 actors::MakeScoreAction(MakeCoopBottomParams(false)));
416 }
417 if (data.PosEdge(kCoopBottomRetract)) {
418 action_queue_.EnqueueAction(
419 actors::MakeScoreAction(MakeCoopBottomParams(true)));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800420 fridge_closed_ = false;
421 }
422
Brian Silverman10043572015-03-21 23:43:50 -0700423 if (data.PosEdge(kFridgeToggle)) {
424 fridge_closed_ = !fridge_closed_;
425 }
426
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800427 if (data.PosEdge(ControlBit::kEnabled)) {
428 // If we got enabled, wait for everything to zero.
429 LOG(INFO, "Waiting for zero.\n");
430 waiting_for_zero_ = true;
431 }
432
Austin Schuh700b9222015-03-01 03:03:15 -0800433 claw_queue.status.FetchLatest();
434 fridge_queue.status.FetchLatest();
435 if (!claw_queue.status.get()) {
436 LOG(ERROR, "Got no claw status packet.\n");
Austin Schuh700b9222015-03-01 03:03:15 -0800437 }
438 if (!fridge_queue.status.get()) {
439 LOG(ERROR, "Got no fridge status packet.\n");
Austin Schuh700b9222015-03-01 03:03:15 -0800440 }
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800441
Brian Silvermanea91f592015-03-28 18:25:57 -0400442 if (claw_queue.status.get() && fridge_queue.status.get() &&
443 claw_queue.status->zeroed && fridge_queue.status->zeroed) {
Austin Schuh700b9222015-03-01 03:03:15 -0800444 if (waiting_for_zero_) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800445 LOG(INFO, "Zeroed! Starting teleop mode.\n");
446 waiting_for_zero_ = false;
447
448 // Set the initial goals to where we are now.
Austin Schuh2cee0b52015-03-29 13:41:39 -0700449 elevator_goal_ = 0.3;
450 arm_goal_ = 0.0;
451 claw_goal_ = 0.6;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800452 }
453 } else {
Austin Schuh700b9222015-03-01 03:03:15 -0800454 waiting_for_zero_ = true;
Austin Schuh700b9222015-03-01 03:03:15 -0800455 }
456
457 if (!waiting_for_zero_) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800458 if (!action_queue_.Running()) {
459 auto new_fridge_goal = fridge_queue.goal.MakeMessage();
Austin Schuh6e242ac2015-03-07 17:08:21 -0800460 new_fridge_goal->max_velocity = elevator_params_.velocity;
461 new_fridge_goal->max_acceleration = elevator_params_.acceleration;
Austin Schuh1d44bd42015-03-15 16:40:45 -0700462 new_fridge_goal->profiling_type = 0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800463 new_fridge_goal->height = elevator_goal_;
Austin Schuh6e242ac2015-03-07 17:08:21 -0800464 new_fridge_goal->velocity = 0.0;
465 new_fridge_goal->max_angular_velocity = arm_params_.velocity;
466 new_fridge_goal->max_angular_acceleration = arm_params_.acceleration;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800467 new_fridge_goal->angle = arm_goal_;
468 new_fridge_goal->angular_velocity = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800469 new_fridge_goal->grabbers.top_front = fridge_closed_;
470 new_fridge_goal->grabbers.top_back = fridge_closed_;
471 new_fridge_goal->grabbers.bottom_front = fridge_closed_;
472 new_fridge_goal->grabbers.bottom_back = fridge_closed_;
473
474 if (!new_fridge_goal.Send()) {
475 LOG(ERROR, "Sending fridge goal failed.\n");
476 } else {
477 LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_,
478 arm_goal_);
479 }
480 if (!claw_queue.goal.MakeWithBuilder()
481 .angle(claw_goal_)
482 .rollers_closed(claw_rollers_closed_)
Austin Schuha534ebe2015-03-29 18:23:35 -0700483 .max_velocity(5.0)
Brian Silverman10043572015-03-21 23:43:50 -0700484 .max_acceleration(6.0)
Austin Schuhf14727e2015-03-08 18:49:12 -0700485 .intake(intake_power)
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800486 .Send()) {
487 LOG(ERROR, "Sending claw goal failed.\n");
488 }
489 }
490 }
491
Austin Schuhd8b2a242015-02-22 21:46:53 -0800492 if (action_queue_.Running()) {
493 // If we are running an action, update our goals to the current goals.
494 control_loops::fridge_queue.status.FetchLatest();
495 if (control_loops::fridge_queue.status.get()) {
496 arm_goal_ = control_loops::fridge_queue.status->goal_angle;
497 elevator_goal_ = control_loops::fridge_queue.status->goal_height;
498 } else {
499 LOG(ERROR, "No fridge status!\n");
500 }
501
502 // If we are running an action, update our goals to the current goals.
503 control_loops::claw_queue.status.FetchLatest();
504 if (control_loops::claw_queue.status.get()) {
505 claw_goal_ = control_loops::claw_queue.status->goal_angle;
506 } else {
Brian Silvermanea91f592015-03-28 18:25:57 -0400507 LOG(ERROR, "No claw status!\n");
Austin Schuhd8b2a242015-02-22 21:46:53 -0800508 }
509 }
Brian Silverman20141f92015-01-05 17:39:01 -0800510 action_queue_.Tick();
511 was_running_ = action_queue_.Running();
512 }
513
514 private:
Austin Schuh6182f8d2015-02-14 22:15:04 -0800515 void StartAuto() {
516 LOG(INFO, "Starting auto mode\n");
517 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
518 }
519
520 void StopAuto() {
521 LOG(INFO, "Stopping auto mode\n");
522 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
523 }
524
Brian Silverman20141f92015-01-05 17:39:01 -0800525 bool was_running_;
Daniel Petti61896522015-02-15 18:01:43 -0800526
527 // Previous goals for systems.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800528 double elevator_goal_ = 0.2;
Daniel Petti61896522015-02-15 18:01:43 -0800529 double arm_goal_ = 0.0;
530 double claw_goal_ = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800531 bool claw_rollers_closed_ = false;
532 bool fridge_closed_ = false;
Austin Schuh6e242ac2015-03-07 17:08:21 -0800533 actors::ProfileParams arm_params_ = kArmMove;
534 actors::ProfileParams elevator_params_ = kElevatorMove;
Daniel Petti61896522015-02-15 18:01:43 -0800535
536 // If we're waiting for the subsystems to zero.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800537 bool waiting_for_zero_ = true;
Daniel Petti61896522015-02-15 18:01:43 -0800538
Austin Schuh6182f8d2015-02-14 22:15:04 -0800539 bool auto_running_ = false;
540
Austin Schuh331e13d2015-02-15 00:16:51 -0800541 ::aos::common::actions::ActionQueue action_queue_;
Brian Silverman20141f92015-01-05 17:39:01 -0800542
543 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
544 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
545 "no drivetrain status");
546};
547
548} // namespace joysticks
549} // namespace input
550} // namespace frc971
551
552int main() {
Brian Silverman5090c432016-01-02 14:44:26 -0800553 ::aos::Init(-1);
Brian Silverman20141f92015-01-05 17:39:01 -0800554 ::frc971::input::joysticks::Reader reader;
555 reader.Run();
556 ::aos::Cleanup();
557}