blob: 2bdf5c01f885d3de1c9991f7eb68128a6372f518 [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"
Brian Silverman20141f92015-01-05 17:39:01 -080024
Daniel Petti61896522015-02-15 18:01:43 -080025using ::frc971::control_loops::claw_queue;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050026using ::frc971::control_loops::drivetrain_queue;
Daniel Petti61896522015-02-15 18:01:43 -080027using ::frc971::control_loops::fridge_queue;
Brian Silverman20141f92015-01-05 17:39:01 -080028using ::frc971::sensors::gyro_reading;
29
30using ::aos::input::driver_station::ButtonLocation;
31using ::aos::input::driver_station::JoystickAxis;
32using ::aos::input::driver_station::ControlBit;
33
34namespace frc971 {
35namespace input {
36namespace joysticks {
37
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080038// preset motion limits
Austin Schuhd8b2a242015-02-22 21:46:53 -080039static const double kArmDebugVelocity = 0.40;
40static const double kArmDebugAcceleration = 1.0;
41static const double kElevatorDebugVelocity = 0.5;
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080042static const double kElevatorDebugAcceleration = 2.2;
43
Brian Silverman20141f92015-01-05 17:39:01 -080044const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
45const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
46const ButtonLocation kQuickTurn(1, 5);
47
Daniel Petti61896522015-02-15 18:01:43 -080048// TODO(danielp): Real buttons for all of these.
Austin Schuh8a436e82015-02-16 23:31:28 -080049const ButtonLocation kElevatorUp(3, 10);
Austin Schuhcde7ffe2015-02-20 22:10:41 -080050const ButtonLocation kElevatorDown(3, 3);
Austin Schuh8a436e82015-02-16 23:31:28 -080051const ButtonLocation kArmUp(3, 8);
Austin Schuhcde7ffe2015-02-20 22:10:41 -080052const ButtonLocation kArmDown(2, 6);
Austin Schuh8a436e82015-02-16 23:31:28 -080053const ButtonLocation kClawUp(3, 7);
54const ButtonLocation kClawDown(3, 6);
Austin Schuhcde7ffe2015-02-20 22:10:41 -080055const ButtonLocation kClawOpen(3, 11);
56const ButtonLocation kClawClosed(3, 5);
57const ButtonLocation kFridgeOpen(3, 1);
58const ButtonLocation kFridgeClosed(2, 11);
59const ButtonLocation kRollersIn(3, 4);
60const ButtonLocation kClawMiddle(3, 2);
Austin Schuhd8b2a242015-02-22 21:46:53 -080061const ButtonLocation kPickup(2, 10);
62const ButtonLocation kZero(2, 7);
63
64const ButtonLocation kStack(3, 9);
Daniel Petti61896522015-02-15 18:01:43 -080065
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080066// TODO(ben): Real buttons for all of these.
67const ButtonLocation kArmPresetOne(99, 99);
68const ButtonLocation kArmPresetTwo(99, 99);
69const ButtonLocation kElevatorPresetOne(99, 99);
70const ButtonLocation kElevatorPresetTwo(99, 99);
71
Daniel Petti61896522015-02-15 18:01:43 -080072// Testing mode.
Austin Schuh8a436e82015-02-16 23:31:28 -080073const double kElevatorVelocity = 0.5;
74const double kArmVelocity = 0.5;
75const double kClawVelocity = 2.0;
Daniel Petti61896522015-02-15 18:01:43 -080076// TODO(danielp): Verify.
77const double kJoystickDt = 0.01;
Brian Silverman20141f92015-01-05 17:39:01 -080078
79class Reader : public ::aos::input::JoystickInput {
80 public:
Austin Schuh331e13d2015-02-15 00:16:51 -080081 Reader() : was_running_(false) {}
Brian Silverman20141f92015-01-05 17:39:01 -080082
83 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Austin Schuh6182f8d2015-02-14 22:15:04 -080084 bool last_auto_running = auto_running_;
85 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
Brian Silvermane1103e62015-02-15 02:03:38 -050086 data.GetControlBit(ControlBit::kEnabled);
Austin Schuh6182f8d2015-02-14 22:15:04 -080087 if (auto_running_ != last_auto_running) {
88 if (auto_running_) {
89 StartAuto();
90 } else {
91 StopAuto();
Brian Silverman20141f92015-01-05 17:39:01 -080092 }
Austin Schuh6182f8d2015-02-14 22:15:04 -080093 }
94
95 if (!data.GetControlBit(ControlBit::kAutonomous)) {
Daniel Petti61896522015-02-15 18:01:43 -080096 HandleDrivetrain(data);
97 if (data.GetControlBit(ControlBit::kTestMode)) {
98 HandleTest(data);
99 } else {
100 HandleTeleop(data);
101 }
Brian Silverman20141f92015-01-05 17:39:01 -0800102 }
103 }
104
105 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -0800106 const double wheel = -data.GetAxis(kSteeringWheel);
107 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman20141f92015-01-05 17:39:01 -0800108
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500109 if (!drivetrain_queue.goal.MakeWithBuilder()
Brian Silverman20141f92015-01-05 17:39:01 -0800110 .steering(wheel)
111 .throttle(throttle)
Brian Silverman20141f92015-01-05 17:39:01 -0800112 .quickturn(data.IsPressed(kQuickTurn))
Austin Schuh331e13d2015-02-15 00:16:51 -0800113 .control_loop_driving(false)
Brian Silverman20141f92015-01-05 17:39:01 -0800114 .Send()) {
115 LOG(WARNING, "sending stick values failed\n");
116 }
Brian Silverman20141f92015-01-05 17:39:01 -0800117 }
118
119 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -0800120 if (!data.GetControlBit(ControlBit::kEnabled)) {
121 action_queue_.CancelAllActions();
Austin Schuh6182f8d2015-02-14 22:15:04 -0800122 LOG(DEBUG, "Canceling\n");
Brian Silverman20141f92015-01-05 17:39:01 -0800123 }
124
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800125 if (data.PosEdge(kElevatorUp)) {
Austin Schuhd8b2a242015-02-22 21:46:53 -0800126 actors::LiftParams params;
127 params.lift_height = 0.45;
128 params.lift_arm = 0.2;
129 action_queue_.EnqueueAction(actors::MakeLiftAction(params));
130
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800131 claw_goal_ = 0.0;
Austin Schuhd8b2a242015-02-22 21:46:53 -0800132 if (!claw_queue.goal.MakeWithBuilder()
133 .angle(claw_goal_)
134 .rollers_closed(claw_rollers_closed_)
135 .intake(0.0)
136 .Send()) {
137 LOG(ERROR, "Sending claw goal failed.\n");
138 }
139 }
140 if (data.PosEdge(kStack)) {
141 actors::StackParams params;
142 params.claw_out_angle = 0.6;
143 action_queue_.EnqueueAction(actors::MakeStackAction(params));
144 }
145 if (data.PosEdge(kPickup)) {
146 actors::PickupParams params;
147 params.pickup_angle = 0.7;
148 params.suck_angle = 0.5;
149 params.suck_angle_finish = 0.4;
150 params.pickup_finish_angle = 0.87;
151 params.intake_time = 0.5;
152 params.intake_voltage = 12.0;
153 action_queue_.EnqueueAction(actors::MakePickupAction(params));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800154 }
155 if (data.PosEdge(kElevatorDown)) {
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800156 claw_goal_ = 0.0;
Austin Schuhd8b2a242015-02-22 21:46:53 -0800157
158 actors::FridgeProfileParams fridge_params;
159 fridge_params.arm_max_velocity = kArmDebugVelocity;
160 fridge_params.arm_max_acceleration = kArmDebugAcceleration;
161 fridge_params.elevator_max_velocity = kElevatorDebugVelocity;
162 fridge_params.elevator_max_acceleration = kElevatorDebugAcceleration;
163
164 fridge_params.arm_angle = 0.0;
165 fridge_params.elevator_height = 0.035;
166
167 fridge_params.top_front_grabber = fridge_closed_;
168 fridge_params.top_back_grabber = fridge_closed_;
169 fridge_params.bottom_front_grabber = fridge_closed_;
170 fridge_params.bottom_back_grabber = fridge_closed_;
171 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800172 }
173
174 if (data.PosEdge(kClawMiddle)) {
Austin Schuhd8b2a242015-02-22 21:46:53 -0800175 claw_goal_ = 0.8;
176 }
177
178 if (data.PosEdge(kZero)) {
179 elevator_goal_ = 0.0;
180 arm_goal_ = 0.0;
181 claw_goal_ = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800182 }
183
184 if (data.PosEdge(kClawClosed)) {
185 claw_rollers_closed_ = true;
186 }
187 if (data.PosEdge(kClawOpen)) {
188 claw_rollers_closed_ = false;
189 }
190
191 if (data.PosEdge(kFridgeClosed)) {
192 fridge_closed_ = true;
193 }
194 if (data.PosEdge(kFridgeOpen)) {
195 fridge_closed_ = false;
196 }
197
198 if (data.PosEdge(ControlBit::kEnabled)) {
199 // If we got enabled, wait for everything to zero.
200 LOG(INFO, "Waiting for zero.\n");
201 waiting_for_zero_ = true;
202 }
203
204 if (waiting_for_zero_) {
205 claw_queue.status.FetchLatest();
206 fridge_queue.status.FetchLatest();
207 if (!claw_queue.status.get()) {
208 LOG(ERROR, "Got no claw status packet.\n");
209 // Not safe to continue.
210 return;
211 }
212 if (!fridge_queue.status.get()) {
213 LOG(ERROR, "Got no fridge status packet.\n");
214 return;
215 }
216
217 if (claw_queue.status->zeroed && fridge_queue.status->zeroed) {
218 LOG(INFO, "Zeroed! Starting teleop mode.\n");
219 waiting_for_zero_ = false;
220
221 // Set the initial goals to where we are now.
222 elevator_goal_ = fridge_queue.status->goal_height;
223 arm_goal_ = fridge_queue.status->goal_angle;
224 claw_goal_ = claw_queue.status->angle;
225 } else {
226 return;
227 }
228 } else {
229 if (!action_queue_.Running()) {
230 auto new_fridge_goal = fridge_queue.goal.MakeMessage();
231 new_fridge_goal->height = elevator_goal_;
232 new_fridge_goal->angle = arm_goal_;
233 new_fridge_goal->angular_velocity = 0.0;
234 new_fridge_goal->velocity = 0.0;
235 new_fridge_goal->grabbers.top_front = fridge_closed_;
236 new_fridge_goal->grabbers.top_back = fridge_closed_;
237 new_fridge_goal->grabbers.bottom_front = fridge_closed_;
238 new_fridge_goal->grabbers.bottom_back = fridge_closed_;
239
240 if (!new_fridge_goal.Send()) {
241 LOG(ERROR, "Sending fridge goal failed.\n");
242 } else {
243 LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_,
244 arm_goal_);
245 }
246 if (!claw_queue.goal.MakeWithBuilder()
247 .angle(claw_goal_)
248 .rollers_closed(claw_rollers_closed_)
249 .intake(data.IsPressed(kRollersIn) ? 12.0 : 0.0)
250 .Send()) {
251 LOG(ERROR, "Sending claw goal failed.\n");
252 }
253 }
254 }
255
Austin Schuhd8b2a242015-02-22 21:46:53 -0800256 if (action_queue_.Running()) {
257 // If we are running an action, update our goals to the current goals.
258 control_loops::fridge_queue.status.FetchLatest();
259 if (control_loops::fridge_queue.status.get()) {
260 arm_goal_ = control_loops::fridge_queue.status->goal_angle;
261 elevator_goal_ = control_loops::fridge_queue.status->goal_height;
262 } else {
263 LOG(ERROR, "No fridge status!\n");
264 }
265
266 // If we are running an action, update our goals to the current goals.
267 control_loops::claw_queue.status.FetchLatest();
268 if (control_loops::claw_queue.status.get()) {
269 claw_goal_ = control_loops::claw_queue.status->goal_angle;
270 } else {
271 LOG(ERROR, "No fridge status!\n");
272 }
273 }
Brian Silverman20141f92015-01-05 17:39:01 -0800274 action_queue_.Tick();
275 was_running_ = action_queue_.Running();
276 }
277
Daniel Petti61896522015-02-15 18:01:43 -0800278 void HandleTest(const ::aos::input::driver_station::Data &data) {
279 if (action_queue_.Running()) {
280 // We don't really want any actions running.
281 LOG(DEBUG, "Cancelling actions for test mode.\n");
282 action_queue_.CancelAllActions();
283 }
284
285 if (data.GetControlBit(ControlBit::kEnabled)) {
286 if (data.PosEdge(ControlBit::kEnabled)) {
287 // If we got enabled, wait for everything to zero.
288 LOG(INFO, "Waiting for zero.\n");
289 waiting_for_zero_ = true;
290 }
291 if (waiting_for_zero_) {
292 claw_queue.status.FetchLatest();
293 fridge_queue.status.FetchLatest();
294 if (!claw_queue.status.get()) {
295 LOG(ERROR, "Got no claw status packet.\n");
296 // Not safe to continue.
297 return;
298 }
299 if (!fridge_queue.status.get()) {
300 LOG(ERROR, "Got no fridge status packet.\n");
301 return;
302 }
303
304 if (claw_queue.status->zeroed && fridge_queue.status->zeroed) {
305 LOG(INFO, "Zeroed! Starting test mode.\n");
306 waiting_for_zero_ = false;
307
308 // Set the initial goals to where we are now.
309 elevator_goal_ = fridge_queue.status->height;
310 arm_goal_ = fridge_queue.status->angle;
311 claw_goal_ = claw_queue.status->angle;
312 } else {
313 return;
314 }
315 }
316
317 // These buttons move a subsystem up or down for as long as they are
318 // pressed, at low velocity.
319 if (data.IsPressed(kElevatorUp)) {
320 elevator_goal_ += kElevatorVelocity * kJoystickDt;
321 }
322 if (data.IsPressed(kElevatorDown)) {
323 elevator_goal_ -= kElevatorVelocity * kJoystickDt;
324 }
325 if (data.IsPressed(kArmUp)) {
326 arm_goal_ += kArmVelocity * kJoystickDt;
327 }
328 if (data.IsPressed(kArmDown)) {
329 arm_goal_ -= kArmVelocity * kJoystickDt;
330 }
331 if (data.IsPressed(kClawUp)) {
332 claw_goal_ += kClawVelocity * kJoystickDt;
333 }
334 if (data.IsPressed(kClawDown)) {
335 claw_goal_ -= kClawVelocity * kJoystickDt;
336 }
337
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800338 if (!action_queue_.Running()) {
339 if (!fridge_queue.goal.MakeWithBuilder()
340 .height(elevator_goal_)
341 .angle(arm_goal_)
342 .Send()) {
343 LOG(ERROR, "Sending fridge goal failed.\n");
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800344 } else {
345 LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_,
346 arm_goal_);
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800347 }
348 if (!claw_queue.goal.MakeWithBuilder().angle(claw_goal_).Send()) {
349 LOG(ERROR, "Sending claw goal failed.\n");
350 }
Daniel Petti61896522015-02-15 18:01:43 -0800351 }
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800352 /*
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800353 if (data.IsPressed(kArmPresetOne) || data.IsPressed(kArmPresetTwo)) {
354 actors::FridgeProfileParams fridge_params;
355 fridge_params.arm_max_velocity = kArmDebugVelocity;
356 fridge_params.arm_max_acceleration = kArmDebugAcceleration;
357 if (data.IsPressed(kArmPresetOne)) {
358 LOG(INFO, "Preset asked for test arm position one position.\n");
359 fridge_params.arm_angle = M_PI / 4.0;
360 fridge_params.top_front_grabber = false;
361 fridge_params.top_back_grabber = false;
362 fridge_params.bottom_front_grabber = false;
363 fridge_params.bottom_back_grabber = false;
364 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
365 } else if (data.IsPressed(kArmPresetTwo)) {
366 LOG(INFO, "Preset asked for test arm position two position.\n");
367 fridge_params.arm_angle = -M_PI / 4.0;
368 fridge_params.top_front_grabber = true;
369 fridge_params.top_back_grabber = true;
370 fridge_params.bottom_front_grabber = true;
371 fridge_params.bottom_back_grabber = true;
372 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
373 }
374 } else if (data.IsPressed(kElevatorPresetOne) ||
375 data.IsPressed(kElevatorPresetOne)) {
376 actors::FridgeProfileParams fridge_params;
377 fridge_params.elevator_max_velocity = kElevatorDebugVelocity;
378 fridge_params.elevator_max_acceleration = kElevatorDebugAcceleration;
379 if (data.IsPressed(kElevatorPresetOne)) {
380 LOG(INFO, "Preset asked for test elevator position one position.\n");
381 fridge_params.elevator_height = 0.5;
382 fridge_params.top_front_grabber = false;
383 fridge_params.top_back_grabber = false;
384 fridge_params.bottom_front_grabber = false;
385 fridge_params.bottom_back_grabber = false;
386 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
387 } else if (data.IsPressed(kElevatorPresetTwo)) {
388 LOG(INFO, "Preset asked for test elevator position two position.\n");
389 fridge_params.elevator_height = 1.2;
390 fridge_params.top_front_grabber = true;
391 fridge_params.top_back_grabber = true;
392 fridge_params.bottom_front_grabber = true;
393 fridge_params.bottom_back_grabber = true;
394 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
395 }
Daniel Petti61896522015-02-15 18:01:43 -0800396 }
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800397 */
Daniel Petti61896522015-02-15 18:01:43 -0800398 }
399 }
400
Brian Silverman20141f92015-01-05 17:39:01 -0800401 private:
Austin Schuh6182f8d2015-02-14 22:15:04 -0800402 void StartAuto() {
403 LOG(INFO, "Starting auto mode\n");
404 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
405 }
406
407 void StopAuto() {
408 LOG(INFO, "Stopping auto mode\n");
409 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
410 }
411
Brian Silverman20141f92015-01-05 17:39:01 -0800412 bool was_running_;
Daniel Petti61896522015-02-15 18:01:43 -0800413
414 // Previous goals for systems.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800415 double elevator_goal_ = 0.2;
Daniel Petti61896522015-02-15 18:01:43 -0800416 double arm_goal_ = 0.0;
417 double claw_goal_ = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800418 bool claw_rollers_closed_ = false;
419 bool fridge_closed_ = false;
Daniel Petti61896522015-02-15 18:01:43 -0800420
421 // If we're waiting for the subsystems to zero.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800422 bool waiting_for_zero_ = true;
Daniel Petti61896522015-02-15 18:01:43 -0800423
Austin Schuh6182f8d2015-02-14 22:15:04 -0800424 bool auto_running_ = false;
425
Austin Schuh331e13d2015-02-15 00:16:51 -0800426 ::aos::common::actions::ActionQueue action_queue_;
Brian Silverman20141f92015-01-05 17:39:01 -0800427
428 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
429 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
430 "no drivetrain status");
431};
432
433} // namespace joysticks
434} // namespace input
435} // namespace frc971
436
437int main() {
438 ::aos::Init();
439 ::frc971::input::joysticks::Reader reader;
440 reader.Run();
441 ::aos::Cleanup();
442}