blob: 8b6cf97f98a4d76f1afbfa6c61ace4c29a157a60 [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"
Austin Schuhf14727e2015-03-08 18:49:12 -070022#include "frc971/actors/stack_and_lift_actor.h"
23#include "frc971/actors/stack_and_hold_actor.h"
24#include "frc971/actors/held_to_lift_actor.h"
Austin Schuhd8b2a242015-02-22 21:46:53 -080025#include "frc971/actors/lift_actor.h"
Austin Schuh994d42c2015-03-01 00:02:17 -080026#include "frc971/actors/can_pickup_actor.h"
Austin Schuh700b9222015-03-01 03:03:15 -080027#include "frc971/actors/horizontal_can_pickup_actor.h"
Brian Silverman20141f92015-01-05 17:39:01 -080028
Daniel Petti61896522015-02-15 18:01:43 -080029using ::frc971::control_loops::claw_queue;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050030using ::frc971::control_loops::drivetrain_queue;
Daniel Petti61896522015-02-15 18:01:43 -080031using ::frc971::control_loops::fridge_queue;
Brian Silverman20141f92015-01-05 17:39:01 -080032using ::frc971::sensors::gyro_reading;
33
34using ::aos::input::driver_station::ButtonLocation;
Austin Schuhf14727e2015-03-08 18:49:12 -070035using ::aos::input::driver_station::POVLocation;
Brian Silverman20141f92015-01-05 17:39:01 -080036using ::aos::input::driver_station::JoystickAxis;
37using ::aos::input::driver_station::ControlBit;
38
39namespace frc971 {
40namespace input {
41namespace joysticks {
42
Austin Schuhf14727e2015-03-08 18:49:12 -070043// Actions needed.
44
45// Human Player Station Intake Button
46// Claw open
47// Claw close
48// Claw down
49
50// Stack + Lift (together)
51// Place
52
53// Hold stack
54
55// Horizontal can pickup
56// Vertical can pickup
57
58// TODO(austin): Pull a lot of the constants below out up here so they can be
59// globally changed easier.
60
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080061// preset motion limits
Austin Schuh6e242ac2015-03-07 17:08:21 -080062constexpr actors::ProfileParams kArmMove{1.00, 1.0};
63constexpr actors::ProfileParams kElevatorMove{1.00, 3.2};
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080064
Brian Silverman20141f92015-01-05 17:39:01 -080065const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
66const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
67const ButtonLocation kQuickTurn(1, 5);
68
Austin Schuhf14727e2015-03-08 18:49:12 -070069//const ButtonLocation kClawClosed(3, 5);
70//const ButtonLocation kFridgeClosed(3, 1);
Austin Schuhd8b2a242015-02-22 21:46:53 -080071
Daniel Petti61896522015-02-15 18:01:43 -080072
Austin Schuhf14727e2015-03-08 18:49:12 -070073const ButtonLocation kRollersIn(4, 5);
74const ButtonLocation kClawToggle(4, 1);
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080075
Austin Schuhf14727e2015-03-08 18:49:12 -070076const POVLocation kElevatorCanUp(3, 0);
77
78// POV stick 3, 180
79const POVLocation kCanPickup(4, 180);
80const ButtonLocation kToteChute(4, 6);
81const ButtonLocation kStackAndLift(4, 7);
82const ButtonLocation kStackAndHold(2, 5);
83
84// Pull in the 6th tote.
85const ButtonLocation kSixthTote(4, 10);
86
87const ButtonLocation kHeldToLift(4, 11);
88const ButtonLocation kPickup(4, 9);
89
90const ButtonLocation kStack(4, 2);
91
92// Move the fridge out with the stack in preparation for scoring.
93// PosEdge(4, 8)
94
95// Release the stack and retract back in.
96// PosEdge(4, 12)
97
98const POVLocation kFridgeOpen(4, 270);
99const ButtonLocation kSpit(4, 3);
100
101// Set stack down in the bot.
102// TODO(austin): Make this work.
103//const POVLocation kSetStackDownAndHold(4, 90);
104
105const double kClawTotePackAngle = 0.95;
Brian Silverman20141f92015-01-05 17:39:01 -0800106
107class Reader : public ::aos::input::JoystickInput {
108 public:
Austin Schuh331e13d2015-02-15 00:16:51 -0800109 Reader() : was_running_(false) {}
Brian Silverman20141f92015-01-05 17:39:01 -0800110
111 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Austin Schuh6182f8d2015-02-14 22:15:04 -0800112 bool last_auto_running = auto_running_;
113 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
Brian Silvermane1103e62015-02-15 02:03:38 -0500114 data.GetControlBit(ControlBit::kEnabled);
Austin Schuh6182f8d2015-02-14 22:15:04 -0800115 if (auto_running_ != last_auto_running) {
116 if (auto_running_) {
117 StartAuto();
118 } else {
119 StopAuto();
Brian Silverman20141f92015-01-05 17:39:01 -0800120 }
Austin Schuh6182f8d2015-02-14 22:15:04 -0800121 }
122
123 if (!data.GetControlBit(ControlBit::kAutonomous)) {
Daniel Petti61896522015-02-15 18:01:43 -0800124 HandleDrivetrain(data);
Austin Schuhf14727e2015-03-08 18:49:12 -0700125 HandleTeleop(data);
Brian Silverman20141f92015-01-05 17:39:01 -0800126 }
127 }
128
129 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -0800130 const double wheel = -data.GetAxis(kSteeringWheel);
131 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman20141f92015-01-05 17:39:01 -0800132
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500133 if (!drivetrain_queue.goal.MakeWithBuilder()
Brian Silverman20141f92015-01-05 17:39:01 -0800134 .steering(wheel)
135 .throttle(throttle)
Brian Silverman20141f92015-01-05 17:39:01 -0800136 .quickturn(data.IsPressed(kQuickTurn))
Austin Schuh331e13d2015-02-15 00:16:51 -0800137 .control_loop_driving(false)
Brian Silverman20141f92015-01-05 17:39:01 -0800138 .Send()) {
139 LOG(WARNING, "sending stick values failed\n");
140 }
Brian Silverman20141f92015-01-05 17:39:01 -0800141 }
142
143 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuhf14727e2015-03-08 18:49:12 -0700144 double intake_power = 0.0;
Brian Silverman20141f92015-01-05 17:39:01 -0800145 if (!data.GetControlBit(ControlBit::kEnabled)) {
146 action_queue_.CancelAllActions();
Austin Schuh6182f8d2015-02-14 22:15:04 -0800147 LOG(DEBUG, "Canceling\n");
Brian Silverman20141f92015-01-05 17:39:01 -0800148 }
149
Austin Schuhf14727e2015-03-08 18:49:12 -0700150 if (data.IsPressed(kRollersIn)) {
151 intake_power = 10.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800152 claw_goal_ = 0.0;
Austin Schuhd8b2a242015-02-22 21:46:53 -0800153 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700154 if (data.IsPressed(kSpit)) {
155 intake_power = -12.0;
Austin Schuhd8b2a242015-02-22 21:46:53 -0800156 }
Austin Schuh994d42c2015-03-01 00:02:17 -0800157
Austin Schuhf14727e2015-03-08 18:49:12 -0700158 // Toggle button for the claw
159 if (data.PosEdge(kClawToggle)) {
160 claw_rollers_closed_ = !claw_rollers_closed_;
Austin Schuh994d42c2015-03-01 00:02:17 -0800161 }
162
Austin Schuhf14727e2015-03-08 18:49:12 -0700163 /*
164 if (data.IsPressed(kClawClosed)) {
165 claw_rollers_closed_ = true;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800166 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700167 */
Austin Schuh700b9222015-03-01 03:03:15 -0800168
Austin Schuhf14727e2015-03-08 18:49:12 -0700169 // Horizontal can pickup.
Austin Schuh700b9222015-03-01 03:03:15 -0800170 if (data.PosEdge(kElevatorCanUp)) {
171 actors::HorizontalCanPickupParams params;
172 params.elevator_height = 0.3;
173 params.pickup_angle = 0.54;
174 params.suck_time = 0.05;
175 params.suck_power = 8.0;
176
177 params.claw_settle_time = 0.05;
178 params.claw_settle_power = 5.0;
179 params.claw_full_lift_angle = 1.35;
180 params.claw_end_angle = 0.5;
Austin Schuhf14727e2015-03-08 18:49:12 -0700181
182 params.elevator_end_height = 0.68;
183 params.arm_end_angle = 0.2;
Austin Schuh700b9222015-03-01 03:03:15 -0800184 action_queue_.EnqueueAction(
185 actors::MakeHorizontalCanPickupAction(params));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800186 fridge_closed_ = true;
187 }
Austin Schuhf14727e2015-03-08 18:49:12 -0700188
189 // -0.942503 arm, 0.374752 elevator
190 // Vertical can pickup.
191 if (data.PosEdge(kCanPickup)) {
192 actors::CanPickupParams params;
193 params.pickup_angle = -0.93;
194 params.pickup_height = 0.265;
195 params.lift_height = 0.65;
196 params.end_height = 0.68;
197 params.end_angle = 0.2;
198 action_queue_.EnqueueAction(actors::MakeCanPickupAction(params));
199 fridge_closed_ = true;
200 }
201
202 // Tote chute pull in when button is pressed, pack when done.
203 if (data.IsPressed(kToteChute)) {
204 claw_goal_ = 0.8;
205 intake_power = 7.0;
206 }
207 if (data.NegEdge(kToteChute)) {
208 claw_goal_ = kClawTotePackAngle;
209 }
210
211 if (data.PosEdge(kStackAndLift)) {
212 actors::StackAndLiftParams params;
213 params.stack_params.claw_out_angle = 0.6;
214 params.stack_params.bottom = 0.020;
215 params.stack_params.over_box_before_place_height = 0.39;
216
217 params.grab_after_stack = true;
218 params.clamp_pause_time = 0.1;
219 params.lift_params.lift_height = 0.45;
220 params.lift_params.lift_arm = 0.3;
221 params.grab_after_lift = true;
222 fridge_closed_ = true;
223
224 action_queue_.EnqueueAction(actors::MakeStackAndLiftAction(params));
225 }
226
227 if (data.PosEdge(kStackAndHold)) {
228 actors::StackAndHoldParams params;
229 params.bottom = 0.020;
230 params.over_box_before_place_height = 0.39;
231
232 params.arm_clearance = -0.05;
233 params.clamp_pause_time = 0.25;
234
235 params.hold_height = 0.68;
236
237 action_queue_.EnqueueAction(actors::MakeStackAndHoldAction(params));
238 fridge_closed_ = true;
239 }
240
241 // TODO(austin): Figure out what action needed to pull the 6th tote into the
242 // claw.
243
244 // Lower the fridge from holding the stack, grab the stack, and then lift.
245 if (data.PosEdge(kHeldToLift)) {
246 actors::HeldToLiftParams params;
247 params.arm_clearance = -0.05;
248 params.clamp_pause_time = 0.1;
249 params.bottom_height = 0.020;
250 params.claw_out_angle = 0.6;
251 params.lift_params.lift_height = 0.45;
252 params.lift_params.lift_arm = 0.3;
253 fridge_closed_ = true;
254
255 action_queue_.EnqueueAction(actors::MakeHeldToLiftAction(params));
256 }
257
258 // Pick up a tote from the ground and put it in the bottom of the bot.
259 if (data.PosEdge(kPickup)) {
260 actors::PickupParams params;
261 // Lift to here initially.
262 params.pickup_angle = 0.9;
263 // Start sucking here
264 params.suck_angle = 0.8;
265 // Go back down to here to finish sucking.
266 params.suck_angle_finish = 0.4;
267 // Pack the box back in here.
268 params.pickup_finish_angle = kClawTotePackAngle;
269 params.intake_time = 0.8;
270 params.intake_voltage = 7.0;
271 action_queue_.EnqueueAction(actors::MakePickupAction(params));
272 }
273
274 // Place stack on a tote in the tray, and grab it.
275 if (data.PosEdge(kStack)) {
276 actors::StackParams params;
277 params.claw_out_angle = 0.6;
278 params.bottom = 0.020;
279 params.over_box_before_place_height = 0.39;
280 action_queue_.EnqueueAction(actors::MakeStackAction(params));
281 claw_rollers_closed_ = true;
282 }
283
284 // TODO(austin): Set down?
285
286 // TODO(austin): Score!
287
288 // TODO(austin): Release.
289
290 // Unknown actions...
291
292 //if (data.PosEdge(kFridgeClosed)) {
293 //fridge_closed_ = true;
294 //}
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800295 if (data.PosEdge(kFridgeOpen)) {
296 fridge_closed_ = false;
297 }
298
299 if (data.PosEdge(ControlBit::kEnabled)) {
300 // If we got enabled, wait for everything to zero.
301 LOG(INFO, "Waiting for zero.\n");
302 waiting_for_zero_ = true;
303 }
304
Austin Schuh700b9222015-03-01 03:03:15 -0800305 claw_queue.status.FetchLatest();
306 fridge_queue.status.FetchLatest();
307 if (!claw_queue.status.get()) {
308 LOG(ERROR, "Got no claw status packet.\n");
309 // Not safe to continue.
310 return;
311 }
312 if (!fridge_queue.status.get()) {
313 LOG(ERROR, "Got no fridge status packet.\n");
314 return;
315 }
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800316
Austin Schuh700b9222015-03-01 03:03:15 -0800317 if (claw_queue.status->zeroed && fridge_queue.status->zeroed) {
318 if (waiting_for_zero_) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800319 LOG(INFO, "Zeroed! Starting teleop mode.\n");
320 waiting_for_zero_ = false;
321
322 // Set the initial goals to where we are now.
323 elevator_goal_ = fridge_queue.status->goal_height;
324 arm_goal_ = fridge_queue.status->goal_angle;
325 claw_goal_ = claw_queue.status->angle;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800326 }
327 } else {
Austin Schuh700b9222015-03-01 03:03:15 -0800328 waiting_for_zero_ = true;
329 return;
330 }
331
332 if (!waiting_for_zero_) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800333 if (!action_queue_.Running()) {
334 auto new_fridge_goal = fridge_queue.goal.MakeMessage();
Austin Schuh6e242ac2015-03-07 17:08:21 -0800335 new_fridge_goal->max_velocity = elevator_params_.velocity;
336 new_fridge_goal->max_acceleration = elevator_params_.acceleration;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800337 new_fridge_goal->height = elevator_goal_;
Austin Schuh6e242ac2015-03-07 17:08:21 -0800338 new_fridge_goal->velocity = 0.0;
339 new_fridge_goal->max_angular_velocity = arm_params_.velocity;
340 new_fridge_goal->max_angular_acceleration = arm_params_.acceleration;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800341 new_fridge_goal->angle = arm_goal_;
342 new_fridge_goal->angular_velocity = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800343 new_fridge_goal->grabbers.top_front = fridge_closed_;
344 new_fridge_goal->grabbers.top_back = fridge_closed_;
345 new_fridge_goal->grabbers.bottom_front = fridge_closed_;
346 new_fridge_goal->grabbers.bottom_back = fridge_closed_;
347
348 if (!new_fridge_goal.Send()) {
349 LOG(ERROR, "Sending fridge goal failed.\n");
350 } else {
351 LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_,
352 arm_goal_);
353 }
354 if (!claw_queue.goal.MakeWithBuilder()
355 .angle(claw_goal_)
356 .rollers_closed(claw_rollers_closed_)
Austin Schuhf14727e2015-03-08 18:49:12 -0700357 .intake(intake_power)
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800358 .Send()) {
359 LOG(ERROR, "Sending claw goal failed.\n");
360 }
361 }
362 }
363
Austin Schuhd8b2a242015-02-22 21:46:53 -0800364 if (action_queue_.Running()) {
365 // If we are running an action, update our goals to the current goals.
366 control_loops::fridge_queue.status.FetchLatest();
367 if (control_loops::fridge_queue.status.get()) {
368 arm_goal_ = control_loops::fridge_queue.status->goal_angle;
369 elevator_goal_ = control_loops::fridge_queue.status->goal_height;
370 } else {
371 LOG(ERROR, "No fridge status!\n");
372 }
373
374 // If we are running an action, update our goals to the current goals.
375 control_loops::claw_queue.status.FetchLatest();
376 if (control_loops::claw_queue.status.get()) {
377 claw_goal_ = control_loops::claw_queue.status->goal_angle;
378 } else {
379 LOG(ERROR, "No fridge status!\n");
380 }
381 }
Brian Silverman20141f92015-01-05 17:39:01 -0800382 action_queue_.Tick();
383 was_running_ = action_queue_.Running();
384 }
385
386 private:
Austin Schuh6182f8d2015-02-14 22:15:04 -0800387 void StartAuto() {
388 LOG(INFO, "Starting auto mode\n");
389 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
390 }
391
392 void StopAuto() {
393 LOG(INFO, "Stopping auto mode\n");
394 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
395 }
396
Brian Silverman20141f92015-01-05 17:39:01 -0800397 bool was_running_;
Daniel Petti61896522015-02-15 18:01:43 -0800398
399 // Previous goals for systems.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800400 double elevator_goal_ = 0.2;
Daniel Petti61896522015-02-15 18:01:43 -0800401 double arm_goal_ = 0.0;
402 double claw_goal_ = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800403 bool claw_rollers_closed_ = false;
404 bool fridge_closed_ = false;
Austin Schuh6e242ac2015-03-07 17:08:21 -0800405 actors::ProfileParams arm_params_ = kArmMove;
406 actors::ProfileParams elevator_params_ = kElevatorMove;
Daniel Petti61896522015-02-15 18:01:43 -0800407
408 // If we're waiting for the subsystems to zero.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800409 bool waiting_for_zero_ = true;
Daniel Petti61896522015-02-15 18:01:43 -0800410
Austin Schuh6182f8d2015-02-14 22:15:04 -0800411 bool auto_running_ = false;
412
Austin Schuh331e13d2015-02-15 00:16:51 -0800413 ::aos::common::actions::ActionQueue action_queue_;
Brian Silverman20141f92015-01-05 17:39:01 -0800414
415 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
416 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
417 "no drivetrain status");
418};
419
420} // namespace joysticks
421} // namespace input
422} // namespace frc971
423
424int main() {
425 ::aos::Init();
426 ::frc971::input::joysticks::Reader reader;
427 reader.Run();
428 ::aos::Cleanup();
429}