blob: b8b8086cd8304975f5d9882b41980790a7ef4f81 [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_action.q.h"
21#include "frc971/actors/fridge_profile_actor.h"
Brian Silverman20141f92015-01-05 17:39:01 -080022
Daniel Petti61896522015-02-15 18:01:43 -080023using ::frc971::control_loops::claw_queue;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050024using ::frc971::control_loops::drivetrain_queue;
Daniel Petti61896522015-02-15 18:01:43 -080025using ::frc971::control_loops::fridge_queue;
Brian Silverman20141f92015-01-05 17:39:01 -080026using ::frc971::sensors::gyro_reading;
27
28using ::aos::input::driver_station::ButtonLocation;
29using ::aos::input::driver_station::JoystickAxis;
30using ::aos::input::driver_station::ControlBit;
31
32namespace frc971 {
33namespace input {
34namespace joysticks {
35
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080036// preset motion limits
37static const double kArmDebugVelocity = 0.17;
38static const double kArmDebugAcceleration = 0.8;
39static const double kElevatorDebugVelocity = 0.2;
40static const double kElevatorDebugAcceleration = 2.2;
41
Brian Silverman20141f92015-01-05 17:39:01 -080042const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
43const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
44const ButtonLocation kQuickTurn(1, 5);
45
Daniel Petti61896522015-02-15 18:01:43 -080046// TODO(danielp): Real buttons for all of these.
Austin Schuh8a436e82015-02-16 23:31:28 -080047const ButtonLocation kElevatorUp(3, 10);
Austin Schuhcde7ffe2015-02-20 22:10:41 -080048const ButtonLocation kElevatorDown(3, 3);
Austin Schuh8a436e82015-02-16 23:31:28 -080049const ButtonLocation kArmUp(3, 8);
Austin Schuhcde7ffe2015-02-20 22:10:41 -080050const ButtonLocation kArmDown(2, 6);
Austin Schuh8a436e82015-02-16 23:31:28 -080051const ButtonLocation kClawUp(3, 7);
52const ButtonLocation kClawDown(3, 6);
Austin Schuhcde7ffe2015-02-20 22:10:41 -080053const ButtonLocation kClawOpen(3, 11);
54const ButtonLocation kClawClosed(3, 5);
55const ButtonLocation kFridgeOpen(3, 1);
56const ButtonLocation kFridgeClosed(2, 11);
57const ButtonLocation kRollersIn(3, 4);
58const ButtonLocation kClawMiddle(3, 2);
Daniel Petti61896522015-02-15 18:01:43 -080059
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080060// TODO(ben): Real buttons for all of these.
61const ButtonLocation kArmPresetOne(99, 99);
62const ButtonLocation kArmPresetTwo(99, 99);
63const ButtonLocation kElevatorPresetOne(99, 99);
64const ButtonLocation kElevatorPresetTwo(99, 99);
65
Daniel Petti61896522015-02-15 18:01:43 -080066// Testing mode.
Austin Schuh8a436e82015-02-16 23:31:28 -080067const double kElevatorVelocity = 0.5;
68const double kArmVelocity = 0.5;
69const double kClawVelocity = 2.0;
Daniel Petti61896522015-02-15 18:01:43 -080070// TODO(danielp): Verify.
71const double kJoystickDt = 0.01;
Brian Silverman20141f92015-01-05 17:39:01 -080072
73class Reader : public ::aos::input::JoystickInput {
74 public:
Austin Schuh331e13d2015-02-15 00:16:51 -080075 Reader() : was_running_(false) {}
Brian Silverman20141f92015-01-05 17:39:01 -080076
77 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Austin Schuh6182f8d2015-02-14 22:15:04 -080078 bool last_auto_running = auto_running_;
79 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
Brian Silvermane1103e62015-02-15 02:03:38 -050080 data.GetControlBit(ControlBit::kEnabled);
Austin Schuh6182f8d2015-02-14 22:15:04 -080081 if (auto_running_ != last_auto_running) {
82 if (auto_running_) {
83 StartAuto();
84 } else {
85 StopAuto();
Brian Silverman20141f92015-01-05 17:39:01 -080086 }
Austin Schuh6182f8d2015-02-14 22:15:04 -080087 }
88
89 if (!data.GetControlBit(ControlBit::kAutonomous)) {
Daniel Petti61896522015-02-15 18:01:43 -080090 HandleDrivetrain(data);
91 if (data.GetControlBit(ControlBit::kTestMode)) {
92 HandleTest(data);
93 } else {
94 HandleTeleop(data);
95 }
Brian Silverman20141f92015-01-05 17:39:01 -080096 }
97 }
98
99 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -0800100 const double wheel = -data.GetAxis(kSteeringWheel);
101 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman20141f92015-01-05 17:39:01 -0800102
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500103 if (!drivetrain_queue.goal.MakeWithBuilder()
Brian Silverman20141f92015-01-05 17:39:01 -0800104 .steering(wheel)
105 .throttle(throttle)
Brian Silverman20141f92015-01-05 17:39:01 -0800106 .quickturn(data.IsPressed(kQuickTurn))
Austin Schuh331e13d2015-02-15 00:16:51 -0800107 .control_loop_driving(false)
Brian Silverman20141f92015-01-05 17:39:01 -0800108 .Send()) {
109 LOG(WARNING, "sending stick values failed\n");
110 }
Brian Silverman20141f92015-01-05 17:39:01 -0800111 }
112
113 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -0800114 if (!data.GetControlBit(ControlBit::kEnabled)) {
115 action_queue_.CancelAllActions();
Austin Schuh6182f8d2015-02-14 22:15:04 -0800116 LOG(DEBUG, "Canceling\n");
Brian Silverman20141f92015-01-05 17:39:01 -0800117 }
118
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800119 if (data.PosEdge(kElevatorUp)) {
120 elevator_goal_ = 0.4;
121 arm_goal_ = 0.1;
122 claw_goal_ = 0.0;
123 }
124 if (data.PosEdge(kElevatorDown)) {
125 elevator_goal_ = 0.03;
126 arm_goal_ = 0.0;
127 claw_goal_ = 0.0;
128 }
129
130 if (data.PosEdge(kClawMiddle)) {
131 claw_goal_ = 0.9;
132 }
133
134 if (data.PosEdge(kClawClosed)) {
135 claw_rollers_closed_ = true;
136 }
137 if (data.PosEdge(kClawOpen)) {
138 claw_rollers_closed_ = false;
139 }
140
141 if (data.PosEdge(kFridgeClosed)) {
142 fridge_closed_ = true;
143 }
144 if (data.PosEdge(kFridgeOpen)) {
145 fridge_closed_ = false;
146 }
147
148 if (data.PosEdge(ControlBit::kEnabled)) {
149 // If we got enabled, wait for everything to zero.
150 LOG(INFO, "Waiting for zero.\n");
151 waiting_for_zero_ = true;
152 }
153
154 if (waiting_for_zero_) {
155 claw_queue.status.FetchLatest();
156 fridge_queue.status.FetchLatest();
157 if (!claw_queue.status.get()) {
158 LOG(ERROR, "Got no claw status packet.\n");
159 // Not safe to continue.
160 return;
161 }
162 if (!fridge_queue.status.get()) {
163 LOG(ERROR, "Got no fridge status packet.\n");
164 return;
165 }
166
167 if (claw_queue.status->zeroed && fridge_queue.status->zeroed) {
168 LOG(INFO, "Zeroed! Starting teleop mode.\n");
169 waiting_for_zero_ = false;
170
171 // Set the initial goals to where we are now.
172 elevator_goal_ = fridge_queue.status->goal_height;
173 arm_goal_ = fridge_queue.status->goal_angle;
174 claw_goal_ = claw_queue.status->angle;
175 } else {
176 return;
177 }
178 } else {
179 if (!action_queue_.Running()) {
180 auto new_fridge_goal = fridge_queue.goal.MakeMessage();
181 new_fridge_goal->height = elevator_goal_;
182 new_fridge_goal->angle = arm_goal_;
183 new_fridge_goal->angular_velocity = 0.0;
184 new_fridge_goal->velocity = 0.0;
185 new_fridge_goal->grabbers.top_front = fridge_closed_;
186 new_fridge_goal->grabbers.top_back = fridge_closed_;
187 new_fridge_goal->grabbers.bottom_front = fridge_closed_;
188 new_fridge_goal->grabbers.bottom_back = fridge_closed_;
189
190 if (!new_fridge_goal.Send()) {
191 LOG(ERROR, "Sending fridge goal failed.\n");
192 } else {
193 LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_,
194 arm_goal_);
195 }
196 if (!claw_queue.goal.MakeWithBuilder()
197 .angle(claw_goal_)
198 .rollers_closed(claw_rollers_closed_)
199 .intake(data.IsPressed(kRollersIn) ? 12.0 : 0.0)
200 .Send()) {
201 LOG(ERROR, "Sending claw goal failed.\n");
202 }
203 }
204 }
205
Brian Silverman20141f92015-01-05 17:39:01 -0800206 action_queue_.Tick();
207 was_running_ = action_queue_.Running();
208 }
209
Daniel Petti61896522015-02-15 18:01:43 -0800210 void HandleTest(const ::aos::input::driver_station::Data &data) {
211 if (action_queue_.Running()) {
212 // We don't really want any actions running.
213 LOG(DEBUG, "Cancelling actions for test mode.\n");
214 action_queue_.CancelAllActions();
215 }
216
217 if (data.GetControlBit(ControlBit::kEnabled)) {
218 if (data.PosEdge(ControlBit::kEnabled)) {
219 // If we got enabled, wait for everything to zero.
220 LOG(INFO, "Waiting for zero.\n");
221 waiting_for_zero_ = true;
222 }
223 if (waiting_for_zero_) {
224 claw_queue.status.FetchLatest();
225 fridge_queue.status.FetchLatest();
226 if (!claw_queue.status.get()) {
227 LOG(ERROR, "Got no claw status packet.\n");
228 // Not safe to continue.
229 return;
230 }
231 if (!fridge_queue.status.get()) {
232 LOG(ERROR, "Got no fridge status packet.\n");
233 return;
234 }
235
236 if (claw_queue.status->zeroed && fridge_queue.status->zeroed) {
237 LOG(INFO, "Zeroed! Starting test mode.\n");
238 waiting_for_zero_ = false;
239
240 // Set the initial goals to where we are now.
241 elevator_goal_ = fridge_queue.status->height;
242 arm_goal_ = fridge_queue.status->angle;
243 claw_goal_ = claw_queue.status->angle;
244 } else {
245 return;
246 }
247 }
248
249 // These buttons move a subsystem up or down for as long as they are
250 // pressed, at low velocity.
251 if (data.IsPressed(kElevatorUp)) {
252 elevator_goal_ += kElevatorVelocity * kJoystickDt;
253 }
254 if (data.IsPressed(kElevatorDown)) {
255 elevator_goal_ -= kElevatorVelocity * kJoystickDt;
256 }
257 if (data.IsPressed(kArmUp)) {
258 arm_goal_ += kArmVelocity * kJoystickDt;
259 }
260 if (data.IsPressed(kArmDown)) {
261 arm_goal_ -= kArmVelocity * kJoystickDt;
262 }
263 if (data.IsPressed(kClawUp)) {
264 claw_goal_ += kClawVelocity * kJoystickDt;
265 }
266 if (data.IsPressed(kClawDown)) {
267 claw_goal_ -= kClawVelocity * kJoystickDt;
268 }
269
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800270 if (!action_queue_.Running()) {
271 if (!fridge_queue.goal.MakeWithBuilder()
272 .height(elevator_goal_)
273 .angle(arm_goal_)
274 .Send()) {
275 LOG(ERROR, "Sending fridge goal failed.\n");
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800276 } else {
277 LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_,
278 arm_goal_);
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800279 }
280 if (!claw_queue.goal.MakeWithBuilder().angle(claw_goal_).Send()) {
281 LOG(ERROR, "Sending claw goal failed.\n");
282 }
Daniel Petti61896522015-02-15 18:01:43 -0800283 }
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800284 /*
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800285 if (data.IsPressed(kArmPresetOne) || data.IsPressed(kArmPresetTwo)) {
286 actors::FridgeProfileParams fridge_params;
287 fridge_params.arm_max_velocity = kArmDebugVelocity;
288 fridge_params.arm_max_acceleration = kArmDebugAcceleration;
289 if (data.IsPressed(kArmPresetOne)) {
290 LOG(INFO, "Preset asked for test arm position one position.\n");
291 fridge_params.arm_angle = M_PI / 4.0;
292 fridge_params.top_front_grabber = false;
293 fridge_params.top_back_grabber = false;
294 fridge_params.bottom_front_grabber = false;
295 fridge_params.bottom_back_grabber = false;
296 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
297 } else if (data.IsPressed(kArmPresetTwo)) {
298 LOG(INFO, "Preset asked for test arm position two position.\n");
299 fridge_params.arm_angle = -M_PI / 4.0;
300 fridge_params.top_front_grabber = true;
301 fridge_params.top_back_grabber = true;
302 fridge_params.bottom_front_grabber = true;
303 fridge_params.bottom_back_grabber = true;
304 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
305 }
306 } else if (data.IsPressed(kElevatorPresetOne) ||
307 data.IsPressed(kElevatorPresetOne)) {
308 actors::FridgeProfileParams fridge_params;
309 fridge_params.elevator_max_velocity = kElevatorDebugVelocity;
310 fridge_params.elevator_max_acceleration = kElevatorDebugAcceleration;
311 if (data.IsPressed(kElevatorPresetOne)) {
312 LOG(INFO, "Preset asked for test elevator position one position.\n");
313 fridge_params.elevator_height = 0.5;
314 fridge_params.top_front_grabber = false;
315 fridge_params.top_back_grabber = false;
316 fridge_params.bottom_front_grabber = false;
317 fridge_params.bottom_back_grabber = false;
318 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
319 } else if (data.IsPressed(kElevatorPresetTwo)) {
320 LOG(INFO, "Preset asked for test elevator position two position.\n");
321 fridge_params.elevator_height = 1.2;
322 fridge_params.top_front_grabber = true;
323 fridge_params.top_back_grabber = true;
324 fridge_params.bottom_front_grabber = true;
325 fridge_params.bottom_back_grabber = true;
326 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
327 }
Daniel Petti61896522015-02-15 18:01:43 -0800328 }
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800329 */
Daniel Petti61896522015-02-15 18:01:43 -0800330 }
331 }
332
Brian Silverman20141f92015-01-05 17:39:01 -0800333 private:
Austin Schuh6182f8d2015-02-14 22:15:04 -0800334 void StartAuto() {
335 LOG(INFO, "Starting auto mode\n");
336 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
337 }
338
339 void StopAuto() {
340 LOG(INFO, "Stopping auto mode\n");
341 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
342 }
343
Brian Silverman20141f92015-01-05 17:39:01 -0800344 bool was_running_;
Daniel Petti61896522015-02-15 18:01:43 -0800345
346 // Previous goals for systems.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800347 double elevator_goal_ = 0.2;
Daniel Petti61896522015-02-15 18:01:43 -0800348 double arm_goal_ = 0.0;
349 double claw_goal_ = 0.0;
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800350 bool claw_rollers_closed_ = false;
351 bool fridge_closed_ = false;
Daniel Petti61896522015-02-15 18:01:43 -0800352
353 // If we're waiting for the subsystems to zero.
Austin Schuhcde7ffe2015-02-20 22:10:41 -0800354 bool waiting_for_zero_ = true;
Daniel Petti61896522015-02-15 18:01:43 -0800355
Austin Schuh6182f8d2015-02-14 22:15:04 -0800356 bool auto_running_ = false;
357
Austin Schuh331e13d2015-02-15 00:16:51 -0800358 ::aos::common::actions::ActionQueue action_queue_;
Brian Silverman20141f92015-01-05 17:39:01 -0800359
360 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
361 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
362 "no drivetrain status");
363};
364
365} // namespace joysticks
366} // namespace input
367} // namespace frc971
368
369int main() {
370 ::aos::Init();
371 ::frc971::input::joysticks::Reader reader;
372 reader.Run();
373 ::aos::Cleanup();
374}