blob: 9fd4441232548773ca3b7ae42a0ac5bfe9e428e8 [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;
160 params.pickup_angle = 0.7;
161 params.suck_angle = 0.5;
162 params.suck_angle_finish = 0.4;
163 params.pickup_finish_angle = 0.87;
164 params.intake_time = 0.5;
165 params.intake_voltage = 12.0;
166 action_queue_.EnqueueAction(actors::MakePickupAction(params));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800167 }
168 if (data.PosEdge(kElevatorDown)) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800169 claw_goal_ = 0.0;
Austin Schuhd8b2a242015-02-22 21:46:53 -0800170
171 actors::FridgeProfileParams fridge_params;
172 fridge_params.arm_max_velocity = kArmDebugVelocity;
173 fridge_params.arm_max_acceleration = kArmDebugAcceleration;
174 fridge_params.elevator_max_velocity = kElevatorDebugVelocity;
175 fridge_params.elevator_max_acceleration = kElevatorDebugAcceleration;
176
177 fridge_params.arm_angle = 0.0;
178 fridge_params.elevator_height = 0.035;
179
180 fridge_params.top_front_grabber = fridge_closed_;
181 fridge_params.top_back_grabber = fridge_closed_;
182 fridge_params.bottom_front_grabber = fridge_closed_;
183 fridge_params.bottom_back_grabber = fridge_closed_;
184 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800185 }
186
187 if (data.PosEdge(kClawMiddle)) {
Austin Schuhd8b2a242015-02-22 21:46:53 -0800188 claw_goal_ = 0.8;
189 }
190
191 if (data.PosEdge(kZero)) {
192 elevator_goal_ = 0.0;
193 arm_goal_ = 0.0;
194 claw_goal_ = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800195 }
196
197 if (data.PosEdge(kClawClosed)) {
198 claw_rollers_closed_ = true;
199 }
200 if (data.PosEdge(kClawOpen)) {
201 claw_rollers_closed_ = false;
202 }
203
204 if (data.PosEdge(kFridgeClosed)) {
205 fridge_closed_ = true;
206 }
207 if (data.PosEdge(kFridgeOpen)) {
208 fridge_closed_ = false;
209 }
210
211 if (data.PosEdge(ControlBit::kEnabled)) {
212 // If we got enabled, wait for everything to zero.
213 LOG(INFO, "Waiting for zero.\n");
214 waiting_for_zero_ = true;
215 }
216
217 if (waiting_for_zero_) {
218 claw_queue.status.FetchLatest();
219 fridge_queue.status.FetchLatest();
220 if (!claw_queue.status.get()) {
221 LOG(ERROR, "Got no claw status packet.\n");
222 // Not safe to continue.
223 return;
224 }
225 if (!fridge_queue.status.get()) {
226 LOG(ERROR, "Got no fridge status packet.\n");
227 return;
228 }
229
230 if (claw_queue.status->zeroed && fridge_queue.status->zeroed) {
231 LOG(INFO, "Zeroed! Starting teleop mode.\n");
232 waiting_for_zero_ = false;
233
234 // Set the initial goals to where we are now.
235 elevator_goal_ = fridge_queue.status->goal_height;
236 arm_goal_ = fridge_queue.status->goal_angle;
237 claw_goal_ = claw_queue.status->angle;
238 } else {
239 return;
240 }
241 } else {
242 if (!action_queue_.Running()) {
243 auto new_fridge_goal = fridge_queue.goal.MakeMessage();
244 new_fridge_goal->height = elevator_goal_;
245 new_fridge_goal->angle = arm_goal_;
246 new_fridge_goal->angular_velocity = 0.0;
247 new_fridge_goal->velocity = 0.0;
248 new_fridge_goal->grabbers.top_front = fridge_closed_;
249 new_fridge_goal->grabbers.top_back = fridge_closed_;
250 new_fridge_goal->grabbers.bottom_front = fridge_closed_;
251 new_fridge_goal->grabbers.bottom_back = fridge_closed_;
252
253 if (!new_fridge_goal.Send()) {
254 LOG(ERROR, "Sending fridge goal failed.\n");
255 } else {
256 LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_,
257 arm_goal_);
258 }
259 if (!claw_queue.goal.MakeWithBuilder()
260 .angle(claw_goal_)
261 .rollers_closed(claw_rollers_closed_)
262 .intake(data.IsPressed(kRollersIn) ? 12.0 : 0.0)
263 .Send()) {
264 LOG(ERROR, "Sending claw goal failed.\n");
265 }
266 }
267 }
268
Austin Schuhd8b2a242015-02-22 21:46:53 -0800269 if (action_queue_.Running()) {
270 // If we are running an action, update our goals to the current goals.
271 control_loops::fridge_queue.status.FetchLatest();
272 if (control_loops::fridge_queue.status.get()) {
273 arm_goal_ = control_loops::fridge_queue.status->goal_angle;
274 elevator_goal_ = control_loops::fridge_queue.status->goal_height;
275 } else {
276 LOG(ERROR, "No fridge status!\n");
277 }
278
279 // If we are running an action, update our goals to the current goals.
280 control_loops::claw_queue.status.FetchLatest();
281 if (control_loops::claw_queue.status.get()) {
282 claw_goal_ = control_loops::claw_queue.status->goal_angle;
283 } else {
284 LOG(ERROR, "No fridge status!\n");
285 }
286 }
Brian Silverman20141f92015-01-05 17:39:01 -0800287 action_queue_.Tick();
288 was_running_ = action_queue_.Running();
289 }
290
Daniel Petti61896522015-02-15 18:01:43 -0800291 void HandleTest(const ::aos::input::driver_station::Data &data) {
292 if (action_queue_.Running()) {
293 // We don't really want any actions running.
294 LOG(DEBUG, "Cancelling actions for test mode.\n");
295 action_queue_.CancelAllActions();
296 }
297
298 if (data.GetControlBit(ControlBit::kEnabled)) {
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 if (waiting_for_zero_) {
305 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 }
316
317 if (claw_queue.status->zeroed && fridge_queue.status->zeroed) {
318 LOG(INFO, "Zeroed! Starting test mode.\n");
319 waiting_for_zero_ = false;
320
321 // Set the initial goals to where we are now.
322 elevator_goal_ = fridge_queue.status->height;
323 arm_goal_ = fridge_queue.status->angle;
324 claw_goal_ = claw_queue.status->angle;
325 } else {
326 return;
327 }
328 }
329
330 // These buttons move a subsystem up or down for as long as they are
331 // pressed, at low velocity.
332 if (data.IsPressed(kElevatorUp)) {
333 elevator_goal_ += kElevatorVelocity * kJoystickDt;
334 }
335 if (data.IsPressed(kElevatorDown)) {
336 elevator_goal_ -= kElevatorVelocity * kJoystickDt;
337 }
338 if (data.IsPressed(kArmUp)) {
339 arm_goal_ += kArmVelocity * kJoystickDt;
340 }
341 if (data.IsPressed(kArmDown)) {
342 arm_goal_ -= kArmVelocity * kJoystickDt;
343 }
344 if (data.IsPressed(kClawUp)) {
345 claw_goal_ += kClawVelocity * kJoystickDt;
346 }
347 if (data.IsPressed(kClawDown)) {
348 claw_goal_ -= kClawVelocity * kJoystickDt;
349 }
350
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800351 if (!action_queue_.Running()) {
352 if (!fridge_queue.goal.MakeWithBuilder()
353 .height(elevator_goal_)
354 .angle(arm_goal_)
355 .Send()) {
356 LOG(ERROR, "Sending fridge goal failed.\n");
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800357 } else {
358 LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_,
359 arm_goal_);
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800360 }
361 if (!claw_queue.goal.MakeWithBuilder().angle(claw_goal_).Send()) {
362 LOG(ERROR, "Sending claw goal failed.\n");
363 }
Daniel Petti61896522015-02-15 18:01:43 -0800364 }
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800365 /*
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800366 if (data.IsPressed(kArmPresetOne) || data.IsPressed(kArmPresetTwo)) {
367 actors::FridgeProfileParams fridge_params;
368 fridge_params.arm_max_velocity = kArmDebugVelocity;
369 fridge_params.arm_max_acceleration = kArmDebugAcceleration;
370 if (data.IsPressed(kArmPresetOne)) {
371 LOG(INFO, "Preset asked for test arm position one position.\n");
372 fridge_params.arm_angle = M_PI / 4.0;
373 fridge_params.top_front_grabber = false;
374 fridge_params.top_back_grabber = false;
375 fridge_params.bottom_front_grabber = false;
376 fridge_params.bottom_back_grabber = false;
377 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
378 } else if (data.IsPressed(kArmPresetTwo)) {
379 LOG(INFO, "Preset asked for test arm position two position.\n");
380 fridge_params.arm_angle = -M_PI / 4.0;
381 fridge_params.top_front_grabber = true;
382 fridge_params.top_back_grabber = true;
383 fridge_params.bottom_front_grabber = true;
384 fridge_params.bottom_back_grabber = true;
385 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
386 }
387 } else if (data.IsPressed(kElevatorPresetOne) ||
388 data.IsPressed(kElevatorPresetOne)) {
389 actors::FridgeProfileParams fridge_params;
390 fridge_params.elevator_max_velocity = kElevatorDebugVelocity;
391 fridge_params.elevator_max_acceleration = kElevatorDebugAcceleration;
392 if (data.IsPressed(kElevatorPresetOne)) {
393 LOG(INFO, "Preset asked for test elevator position one position.\n");
394 fridge_params.elevator_height = 0.5;
395 fridge_params.top_front_grabber = false;
396 fridge_params.top_back_grabber = false;
397 fridge_params.bottom_front_grabber = false;
398 fridge_params.bottom_back_grabber = false;
399 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
400 } else if (data.IsPressed(kElevatorPresetTwo)) {
401 LOG(INFO, "Preset asked for test elevator position two position.\n");
402 fridge_params.elevator_height = 1.2;
403 fridge_params.top_front_grabber = true;
404 fridge_params.top_back_grabber = true;
405 fridge_params.bottom_front_grabber = true;
406 fridge_params.bottom_back_grabber = true;
407 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
408 }
Daniel Petti61896522015-02-15 18:01:43 -0800409 }
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800410 */
Daniel Petti61896522015-02-15 18:01:43 -0800411 }
412 }
413
Brian Silverman20141f92015-01-05 17:39:01 -0800414 private:
Austin Schuh6182f8d2015-02-14 22:15:04 -0800415 void StartAuto() {
416 LOG(INFO, "Starting auto mode\n");
417 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
418 }
419
420 void StopAuto() {
421 LOG(INFO, "Stopping auto mode\n");
422 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
423 }
424
Brian Silverman20141f92015-01-05 17:39:01 -0800425 bool was_running_;
Daniel Petti61896522015-02-15 18:01:43 -0800426
427 // Previous goals for systems.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800428 double elevator_goal_ = 0.2;
Daniel Petti61896522015-02-15 18:01:43 -0800429 double arm_goal_ = 0.0;
430 double claw_goal_ = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800431 bool claw_rollers_closed_ = false;
432 bool fridge_closed_ = false;
Daniel Petti61896522015-02-15 18:01:43 -0800433
434 // If we're waiting for the subsystems to zero.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800435 bool waiting_for_zero_ = true;
Daniel Petti61896522015-02-15 18:01:43 -0800436
Austin Schuh6182f8d2015-02-14 22:15:04 -0800437 bool auto_running_ = false;
438
Austin Schuh331e13d2015-02-15 00:16:51 -0800439 ::aos::common::actions::ActionQueue action_queue_;
Brian Silverman20141f92015-01-05 17:39:01 -0800440
441 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
442 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
443 "no drivetrain status");
444};
445
446} // namespace joysticks
447} // namespace input
448} // namespace frc971
449
450int main() {
451 ::aos::Init();
452 ::frc971::input::joysticks::Reader reader;
453 reader.Run();
454 ::aos::Cleanup();
455}