blob: 502c83e56fcad0ec03c86cde01d01998d1a2b1ab [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"
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080020#include "frc971/actors/fridge_profile_actor.h"
Austin Schuhd8b2a242015-02-22 21:46:53 -080021#include "frc971/actors/pickup_actor.h"
22#include "frc971/actors/stack_actor.h"
23#include "frc971/actors/lift_actor.h"
Austin Schuh994d42c2015-03-01 00:02:17 -080024#include "frc971/actors/can_pickup_actor.h"
Brian Silverman20141f92015-01-05 17:39:01 -080025
Daniel Petti61896522015-02-15 18:01:43 -080026using ::frc971::control_loops::claw_queue;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050027using ::frc971::control_loops::drivetrain_queue;
Daniel Petti61896522015-02-15 18:01:43 -080028using ::frc971::control_loops::fridge_queue;
Brian Silverman20141f92015-01-05 17:39:01 -080029using ::frc971::sensors::gyro_reading;
30
31using ::aos::input::driver_station::ButtonLocation;
32using ::aos::input::driver_station::JoystickAxis;
33using ::aos::input::driver_station::ControlBit;
34
35namespace frc971 {
36namespace input {
37namespace joysticks {
38
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080039// preset motion limits
Austin Schuhd8b2a242015-02-22 21:46:53 -080040static const double kArmDebugVelocity = 0.40;
41static const double kArmDebugAcceleration = 1.0;
42static const double kElevatorDebugVelocity = 0.5;
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080043static const double kElevatorDebugAcceleration = 2.2;
44
Brian Silverman20141f92015-01-05 17:39:01 -080045const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
46const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
47const ButtonLocation kQuickTurn(1, 5);
48
Daniel Petti61896522015-02-15 18:01:43 -080049// TODO(danielp): Real buttons for all of these.
Austin Schuh8a436e82015-02-16 23:31:28 -080050const ButtonLocation kElevatorUp(3, 10);
Austin Schuhcde7ffe2015-02-20 22:10:41 -080051const ButtonLocation kElevatorDown(3, 3);
Austin Schuh8a436e82015-02-16 23:31:28 -080052const ButtonLocation kArmUp(3, 8);
Austin Schuh994d42c2015-03-01 00:02:17 -080053const ButtonLocation kCanPickup(3, 8);
Austin Schuhcde7ffe2015-02-20 22:10:41 -080054const ButtonLocation kArmDown(2, 6);
Austin Schuh8a436e82015-02-16 23:31:28 -080055const ButtonLocation kClawUp(3, 7);
56const ButtonLocation kClawDown(3, 6);
Austin Schuhcde7ffe2015-02-20 22:10:41 -080057const ButtonLocation kClawOpen(3, 11);
58const ButtonLocation kClawClosed(3, 5);
59const ButtonLocation kFridgeOpen(3, 1);
60const ButtonLocation kFridgeClosed(2, 11);
61const ButtonLocation kRollersIn(3, 4);
62const ButtonLocation kClawMiddle(3, 2);
Austin Schuhd8b2a242015-02-22 21:46:53 -080063const ButtonLocation kPickup(2, 10);
64const ButtonLocation kZero(2, 7);
65
66const ButtonLocation kStack(3, 9);
Daniel Petti61896522015-02-15 18:01:43 -080067
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080068// TODO(ben): Real buttons for all of these.
69const ButtonLocation kArmPresetOne(99, 99);
70const ButtonLocation kArmPresetTwo(99, 99);
71const ButtonLocation kElevatorPresetOne(99, 99);
72const ButtonLocation kElevatorPresetTwo(99, 99);
73
Daniel Petti61896522015-02-15 18:01:43 -080074// Testing mode.
Austin Schuh8a436e82015-02-16 23:31:28 -080075const double kElevatorVelocity = 0.5;
76const double kArmVelocity = 0.5;
77const double kClawVelocity = 2.0;
Daniel Petti61896522015-02-15 18:01:43 -080078// TODO(danielp): Verify.
79const double kJoystickDt = 0.01;
Brian Silverman20141f92015-01-05 17:39:01 -080080
81class Reader : public ::aos::input::JoystickInput {
82 public:
Austin Schuh331e13d2015-02-15 00:16:51 -080083 Reader() : was_running_(false) {}
Brian Silverman20141f92015-01-05 17:39:01 -080084
85 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Austin Schuh6182f8d2015-02-14 22:15:04 -080086 bool last_auto_running = auto_running_;
87 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
Brian Silvermane1103e62015-02-15 02:03:38 -050088 data.GetControlBit(ControlBit::kEnabled);
Austin Schuh6182f8d2015-02-14 22:15:04 -080089 if (auto_running_ != last_auto_running) {
90 if (auto_running_) {
91 StartAuto();
92 } else {
93 StopAuto();
Brian Silverman20141f92015-01-05 17:39:01 -080094 }
Austin Schuh6182f8d2015-02-14 22:15:04 -080095 }
96
97 if (!data.GetControlBit(ControlBit::kAutonomous)) {
Daniel Petti61896522015-02-15 18:01:43 -080098 HandleDrivetrain(data);
99 if (data.GetControlBit(ControlBit::kTestMode)) {
100 HandleTest(data);
101 } else {
102 HandleTeleop(data);
103 }
Brian Silverman20141f92015-01-05 17:39:01 -0800104 }
105 }
106
107 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -0800108 const double wheel = -data.GetAxis(kSteeringWheel);
109 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman20141f92015-01-05 17:39:01 -0800110
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500111 if (!drivetrain_queue.goal.MakeWithBuilder()
Brian Silverman20141f92015-01-05 17:39:01 -0800112 .steering(wheel)
113 .throttle(throttle)
Brian Silverman20141f92015-01-05 17:39:01 -0800114 .quickturn(data.IsPressed(kQuickTurn))
Austin Schuh331e13d2015-02-15 00:16:51 -0800115 .control_loop_driving(false)
Brian Silverman20141f92015-01-05 17:39:01 -0800116 .Send()) {
117 LOG(WARNING, "sending stick values failed\n");
118 }
Brian Silverman20141f92015-01-05 17:39:01 -0800119 }
120
121 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -0800122 if (!data.GetControlBit(ControlBit::kEnabled)) {
123 action_queue_.CancelAllActions();
Austin Schuh6182f8d2015-02-14 22:15:04 -0800124 LOG(DEBUG, "Canceling\n");
Brian Silverman20141f92015-01-05 17:39:01 -0800125 }
126
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800127 if (data.PosEdge(kElevatorUp)) {
Austin Schuhd8b2a242015-02-22 21:46:53 -0800128 actors::LiftParams params;
129 params.lift_height = 0.45;
130 params.lift_arm = 0.2;
131 action_queue_.EnqueueAction(actors::MakeLiftAction(params));
132
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800133 claw_goal_ = 0.0;
Austin Schuhd8b2a242015-02-22 21:46:53 -0800134 if (!claw_queue.goal.MakeWithBuilder()
135 .angle(claw_goal_)
136 .rollers_closed(claw_rollers_closed_)
137 .intake(0.0)
138 .Send()) {
139 LOG(ERROR, "Sending claw goal failed.\n");
140 }
141 }
142 if (data.PosEdge(kStack)) {
143 actors::StackParams params;
144 params.claw_out_angle = 0.6;
145 action_queue_.EnqueueAction(actors::MakeStackAction(params));
146 }
Austin Schuh994d42c2015-03-01 00:02:17 -0800147
148 if (data.PosEdge(kCanPickup)) {
149 actors::CanPickupParams params;
150 params.pickup_angle = -0.93;
151 params.pickup_height = 0.265;
152 params.lift_height = 0.65;
153 params.end_height = 0.4;
154 params.end_angle = 0.0;
155 action_queue_.EnqueueAction(actors::MakeCanPickupAction(params));
156 }
157
Austin Schuhd8b2a242015-02-22 21:46:53 -0800158 if (data.PosEdge(kPickup)) {
159 actors::PickupParams params;
Austin Schuhe4e59ef2015-03-01 00:05:37 -0800160 // Lift to here initially.
161 params.pickup_angle = 0.9;
162 // Start sucking here
163 params.suck_angle = 0.8;
164 // Go back down to here to finish sucking.
Austin Schuhd8b2a242015-02-22 21:46:53 -0800165 params.suck_angle_finish = 0.4;
Austin Schuhe4e59ef2015-03-01 00:05:37 -0800166 // Pack the box back in here.
167 params.pickup_finish_angle = 0.95;
168 params.intake_time = 0.8;
169 params.intake_voltage = 9.0;
Austin Schuhd8b2a242015-02-22 21:46:53 -0800170 action_queue_.EnqueueAction(actors::MakePickupAction(params));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800171 }
172 if (data.PosEdge(kElevatorDown)) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800173 claw_goal_ = 0.0;
Austin Schuhd8b2a242015-02-22 21:46:53 -0800174
175 actors::FridgeProfileParams fridge_params;
176 fridge_params.arm_max_velocity = kArmDebugVelocity;
177 fridge_params.arm_max_acceleration = kArmDebugAcceleration;
178 fridge_params.elevator_max_velocity = kElevatorDebugVelocity;
179 fridge_params.elevator_max_acceleration = kElevatorDebugAcceleration;
180
181 fridge_params.arm_angle = 0.0;
182 fridge_params.elevator_height = 0.035;
183
184 fridge_params.top_front_grabber = fridge_closed_;
185 fridge_params.top_back_grabber = fridge_closed_;
186 fridge_params.bottom_front_grabber = fridge_closed_;
187 fridge_params.bottom_back_grabber = fridge_closed_;
188 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800189 }
190
191 if (data.PosEdge(kClawMiddle)) {
Austin Schuhd8b2a242015-02-22 21:46:53 -0800192 claw_goal_ = 0.8;
193 }
194
195 if (data.PosEdge(kZero)) {
196 elevator_goal_ = 0.0;
197 arm_goal_ = 0.0;
198 claw_goal_ = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800199 }
200
201 if (data.PosEdge(kClawClosed)) {
202 claw_rollers_closed_ = true;
203 }
204 if (data.PosEdge(kClawOpen)) {
205 claw_rollers_closed_ = false;
206 }
207
208 if (data.PosEdge(kFridgeClosed)) {
209 fridge_closed_ = true;
210 }
211 if (data.PosEdge(kFridgeOpen)) {
212 fridge_closed_ = false;
213 }
214
215 if (data.PosEdge(ControlBit::kEnabled)) {
216 // If we got enabled, wait for everything to zero.
217 LOG(INFO, "Waiting for zero.\n");
218 waiting_for_zero_ = true;
219 }
220
221 if (waiting_for_zero_) {
222 claw_queue.status.FetchLatest();
223 fridge_queue.status.FetchLatest();
224 if (!claw_queue.status.get()) {
225 LOG(ERROR, "Got no claw status packet.\n");
226 // Not safe to continue.
227 return;
228 }
229 if (!fridge_queue.status.get()) {
230 LOG(ERROR, "Got no fridge status packet.\n");
231 return;
232 }
233
234 if (claw_queue.status->zeroed && fridge_queue.status->zeroed) {
235 LOG(INFO, "Zeroed! Starting teleop mode.\n");
236 waiting_for_zero_ = false;
237
238 // Set the initial goals to where we are now.
239 elevator_goal_ = fridge_queue.status->goal_height;
240 arm_goal_ = fridge_queue.status->goal_angle;
241 claw_goal_ = claw_queue.status->angle;
242 } else {
243 return;
244 }
245 } else {
246 if (!action_queue_.Running()) {
247 auto new_fridge_goal = fridge_queue.goal.MakeMessage();
248 new_fridge_goal->height = elevator_goal_;
249 new_fridge_goal->angle = arm_goal_;
250 new_fridge_goal->angular_velocity = 0.0;
251 new_fridge_goal->velocity = 0.0;
252 new_fridge_goal->grabbers.top_front = fridge_closed_;
253 new_fridge_goal->grabbers.top_back = fridge_closed_;
254 new_fridge_goal->grabbers.bottom_front = fridge_closed_;
255 new_fridge_goal->grabbers.bottom_back = fridge_closed_;
256
257 if (!new_fridge_goal.Send()) {
258 LOG(ERROR, "Sending fridge goal failed.\n");
259 } else {
260 LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_,
261 arm_goal_);
262 }
263 if (!claw_queue.goal.MakeWithBuilder()
264 .angle(claw_goal_)
265 .rollers_closed(claw_rollers_closed_)
266 .intake(data.IsPressed(kRollersIn) ? 12.0 : 0.0)
267 .Send()) {
268 LOG(ERROR, "Sending claw goal failed.\n");
269 }
270 }
271 }
272
Austin Schuhd8b2a242015-02-22 21:46:53 -0800273 if (action_queue_.Running()) {
274 // If we are running an action, update our goals to the current goals.
275 control_loops::fridge_queue.status.FetchLatest();
276 if (control_loops::fridge_queue.status.get()) {
277 arm_goal_ = control_loops::fridge_queue.status->goal_angle;
278 elevator_goal_ = control_loops::fridge_queue.status->goal_height;
279 } else {
280 LOG(ERROR, "No fridge status!\n");
281 }
282
283 // If we are running an action, update our goals to the current goals.
284 control_loops::claw_queue.status.FetchLatest();
285 if (control_loops::claw_queue.status.get()) {
286 claw_goal_ = control_loops::claw_queue.status->goal_angle;
287 } else {
288 LOG(ERROR, "No fridge status!\n");
289 }
290 }
Brian Silverman20141f92015-01-05 17:39:01 -0800291 action_queue_.Tick();
292 was_running_ = action_queue_.Running();
293 }
294
Daniel Petti61896522015-02-15 18:01:43 -0800295 void HandleTest(const ::aos::input::driver_station::Data &data) {
296 if (action_queue_.Running()) {
297 // We don't really want any actions running.
298 LOG(DEBUG, "Cancelling actions for test mode.\n");
299 action_queue_.CancelAllActions();
300 }
301
302 if (data.GetControlBit(ControlBit::kEnabled)) {
303 if (data.PosEdge(ControlBit::kEnabled)) {
304 // If we got enabled, wait for everything to zero.
305 LOG(INFO, "Waiting for zero.\n");
306 waiting_for_zero_ = true;
307 }
308 if (waiting_for_zero_) {
309 claw_queue.status.FetchLatest();
310 fridge_queue.status.FetchLatest();
311 if (!claw_queue.status.get()) {
312 LOG(ERROR, "Got no claw status packet.\n");
313 // Not safe to continue.
314 return;
315 }
316 if (!fridge_queue.status.get()) {
317 LOG(ERROR, "Got no fridge status packet.\n");
318 return;
319 }
320
321 if (claw_queue.status->zeroed && fridge_queue.status->zeroed) {
322 LOG(INFO, "Zeroed! Starting test mode.\n");
323 waiting_for_zero_ = false;
324
325 // Set the initial goals to where we are now.
326 elevator_goal_ = fridge_queue.status->height;
327 arm_goal_ = fridge_queue.status->angle;
328 claw_goal_ = claw_queue.status->angle;
329 } else {
330 return;
331 }
332 }
333
334 // These buttons move a subsystem up or down for as long as they are
335 // pressed, at low velocity.
336 if (data.IsPressed(kElevatorUp)) {
337 elevator_goal_ += kElevatorVelocity * kJoystickDt;
338 }
339 if (data.IsPressed(kElevatorDown)) {
340 elevator_goal_ -= kElevatorVelocity * kJoystickDt;
341 }
342 if (data.IsPressed(kArmUp)) {
343 arm_goal_ += kArmVelocity * kJoystickDt;
344 }
345 if (data.IsPressed(kArmDown)) {
346 arm_goal_ -= kArmVelocity * kJoystickDt;
347 }
348 if (data.IsPressed(kClawUp)) {
349 claw_goal_ += kClawVelocity * kJoystickDt;
350 }
351 if (data.IsPressed(kClawDown)) {
352 claw_goal_ -= kClawVelocity * kJoystickDt;
353 }
354
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800355 if (!action_queue_.Running()) {
356 if (!fridge_queue.goal.MakeWithBuilder()
357 .height(elevator_goal_)
358 .angle(arm_goal_)
359 .Send()) {
360 LOG(ERROR, "Sending fridge goal failed.\n");
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800361 } else {
362 LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_,
363 arm_goal_);
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800364 }
365 if (!claw_queue.goal.MakeWithBuilder().angle(claw_goal_).Send()) {
366 LOG(ERROR, "Sending claw goal failed.\n");
367 }
Daniel Petti61896522015-02-15 18:01:43 -0800368 }
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800369 /*
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800370 if (data.IsPressed(kArmPresetOne) || data.IsPressed(kArmPresetTwo)) {
371 actors::FridgeProfileParams fridge_params;
372 fridge_params.arm_max_velocity = kArmDebugVelocity;
373 fridge_params.arm_max_acceleration = kArmDebugAcceleration;
374 if (data.IsPressed(kArmPresetOne)) {
375 LOG(INFO, "Preset asked for test arm position one position.\n");
376 fridge_params.arm_angle = M_PI / 4.0;
377 fridge_params.top_front_grabber = false;
378 fridge_params.top_back_grabber = false;
379 fridge_params.bottom_front_grabber = false;
380 fridge_params.bottom_back_grabber = false;
381 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
382 } else if (data.IsPressed(kArmPresetTwo)) {
383 LOG(INFO, "Preset asked for test arm position two position.\n");
384 fridge_params.arm_angle = -M_PI / 4.0;
385 fridge_params.top_front_grabber = true;
386 fridge_params.top_back_grabber = true;
387 fridge_params.bottom_front_grabber = true;
388 fridge_params.bottom_back_grabber = true;
389 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
390 }
391 } else if (data.IsPressed(kElevatorPresetOne) ||
392 data.IsPressed(kElevatorPresetOne)) {
393 actors::FridgeProfileParams fridge_params;
394 fridge_params.elevator_max_velocity = kElevatorDebugVelocity;
395 fridge_params.elevator_max_acceleration = kElevatorDebugAcceleration;
396 if (data.IsPressed(kElevatorPresetOne)) {
397 LOG(INFO, "Preset asked for test elevator position one position.\n");
398 fridge_params.elevator_height = 0.5;
399 fridge_params.top_front_grabber = false;
400 fridge_params.top_back_grabber = false;
401 fridge_params.bottom_front_grabber = false;
402 fridge_params.bottom_back_grabber = false;
403 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
404 } else if (data.IsPressed(kElevatorPresetTwo)) {
405 LOG(INFO, "Preset asked for test elevator position two position.\n");
406 fridge_params.elevator_height = 1.2;
407 fridge_params.top_front_grabber = true;
408 fridge_params.top_back_grabber = true;
409 fridge_params.bottom_front_grabber = true;
410 fridge_params.bottom_back_grabber = true;
411 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
412 }
Daniel Petti61896522015-02-15 18:01:43 -0800413 }
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800414 */
Daniel Petti61896522015-02-15 18:01:43 -0800415 }
416 }
417
Brian Silverman20141f92015-01-05 17:39:01 -0800418 private:
Austin Schuh6182f8d2015-02-14 22:15:04 -0800419 void StartAuto() {
420 LOG(INFO, "Starting auto mode\n");
421 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
422 }
423
424 void StopAuto() {
425 LOG(INFO, "Stopping auto mode\n");
426 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
427 }
428
Brian Silverman20141f92015-01-05 17:39:01 -0800429 bool was_running_;
Daniel Petti61896522015-02-15 18:01:43 -0800430
431 // Previous goals for systems.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800432 double elevator_goal_ = 0.2;
Daniel Petti61896522015-02-15 18:01:43 -0800433 double arm_goal_ = 0.0;
434 double claw_goal_ = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800435 bool claw_rollers_closed_ = false;
436 bool fridge_closed_ = false;
Daniel Petti61896522015-02-15 18:01:43 -0800437
438 // If we're waiting for the subsystems to zero.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800439 bool waiting_for_zero_ = true;
Daniel Petti61896522015-02-15 18:01:43 -0800440
Austin Schuh6182f8d2015-02-14 22:15:04 -0800441 bool auto_running_ = false;
442
Austin Schuh331e13d2015-02-15 00:16:51 -0800443 ::aos::common::actions::ActionQueue action_queue_;
Brian Silverman20141f92015-01-05 17:39:01 -0800444
445 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
446 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
447 "no drivetrain status");
448};
449
450} // namespace joysticks
451} // namespace input
452} // namespace frc971
453
454int main() {
455 ::aos::Init();
456 ::frc971::input::joysticks::Reader reader;
457 reader.Run();
458 ::aos::Cleanup();
459}