blob: ad20eba645163ee2d223290401d3d330128e7e75 [file] [log] [blame]
Comran Morshed9a9948c2016-01-16 15:58:04 +00001#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/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"
12#include "aos/common/actions/actions.h"
13
14#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Comran Morshed200dd4b2016-02-16 17:54:58 +000015#include "y2016/control_loops/shooter/shooter.q.h"
16#include "y2016/control_loops/superstructure/superstructure.q.h"
Austin Schuh45d07f62016-03-13 15:33:31 -070017#include "y2016/control_loops/superstructure/superstructure.h"
Comran Morshedaa0573c2016-03-05 19:05:54 +000018#include "y2016/queues/ball_detector.q.h"
Austin Schuhadd6d792016-03-19 01:20:01 -070019#include "y2016/vision/vision.q.h"
Comran Morshed200dd4b2016-02-16 17:54:58 +000020
Comran Morshed6c6a0a92016-01-17 12:45:16 +000021#include "y2016/constants.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000022#include "frc971/queues/gyro.q.h"
23#include "frc971/autonomous/auto.q.h"
Comran Morshede68e3732016-03-12 14:12:11 +000024#include "y2016/actors/autonomous_actor.h"
Austin Schuh18799112016-03-16 22:09:54 -070025#include "y2016/actors/vision_align_actor.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000026
27using ::frc971::control_loops::drivetrain_queue;
Comran Morshed200dd4b2016-02-16 17:54:58 +000028using ::y2016::control_loops::shooter::shooter_queue;
29using ::y2016::control_loops::superstructure_queue;
Comran Morshed9a9948c2016-01-16 15:58:04 +000030
31using ::aos::input::driver_station::ButtonLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000032using ::aos::input::driver_station::ControlBit;
Austin Schuh4ea06c12016-03-12 17:54:31 -080033using ::aos::input::driver_station::JoystickAxis;
34using ::aos::input::driver_station::POVLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000035
Comran Morshed6c6a0a92016-01-17 12:45:16 +000036namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000037namespace input {
38namespace joysticks {
39
Austin Schuh45d07f62016-03-13 15:33:31 -070040namespace {
41
42constexpr double kMaxIntakeAngleBeforeArmInterference = control_loops::
43 superstructure::CollisionAvoidance::kMaxIntakeAngleBeforeArmInterference;
44
45} // namespace
46
Comran Morshed9a9948c2016-01-16 15:58:04 +000047const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080048const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
Comran Morshed9a9948c2016-01-16 15:58:04 +000049const ButtonLocation kQuickTurn(1, 5);
50
Austin Schuh781cdcc2016-03-12 13:03:12 -080051const ButtonLocation kTurn1(1, 7);
52const ButtonLocation kTurn2(1, 11);
53
Comran Morshed200dd4b2016-02-16 17:54:58 +000054// Buttons on the lexan driver station to get things running on bring-up day.
Austin Schuh4ea06c12016-03-12 17:54:31 -080055const ButtonLocation kIntakeDown(3, 11);
56const POVLocation kFrontLong(3, 180);
57const POVLocation kBackLong(3, 0);
Austin Schuh45d07f62016-03-13 15:33:31 -070058const POVLocation kBackFender(3, 90);
59const POVLocation kFrontFender(3, 270);
Austin Schuh4ea06c12016-03-12 17:54:31 -080060const ButtonLocation kTest3(3, 7);
61const ButtonLocation kIntakeIn(3, 12);
Comran Morshed200dd4b2016-02-16 17:54:58 +000062const ButtonLocation kTest5(3, 8);
Austin Schuh4ea06c12016-03-12 17:54:31 -080063const ButtonLocation kFire(3, 3);
Comran Morshed200dd4b2016-02-16 17:54:58 +000064const ButtonLocation kTest7(3, 5);
Austin Schuh4ea06c12016-03-12 17:54:31 -080065const ButtonLocation kIntakeOut(3, 9);
Comran Morshed200dd4b2016-02-16 17:54:58 +000066
Austin Schuhadd6d792016-03-19 01:20:01 -070067const ButtonLocation kVisionAlign(3, 4);
Austin Schuh18799112016-03-16 22:09:54 -070068
Comran Morshed9a9948c2016-01-16 15:58:04 +000069class Reader : public ::aos::input::JoystickInput {
70 public:
71 Reader()
Austin Schuh94596dd2016-03-13 21:41:26 -070072 : is_high_gear_(true),
Comran Morshed200dd4b2016-02-16 17:54:58 +000073 intake_goal_(0.0),
74 shoulder_goal_(M_PI / 2.0),
75 wrist_goal_(0.0) {}
Comran Morshed9a9948c2016-01-16 15:58:04 +000076
77 void RunIteration(const ::aos::input::driver_station::Data &data) override {
78 bool last_auto_running = auto_running_;
79 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
80 data.GetControlBit(ControlBit::kEnabled);
81 if (auto_running_ != last_auto_running) {
82 if (auto_running_) {
83 StartAuto();
84 } else {
85 StopAuto();
86 }
87 }
88
Austin Schuh94596dd2016-03-13 21:41:26 -070089 if (!data.GetControlBit(ControlBit::kEnabled)) {
90 // If we are not enabled, reset the waiting for zero bit.
91 LOG(DEBUG, "Waiting for zero.\n");
92 waiting_for_zero_ = true;
93 is_high_gear_ = true;
94 }
95
Austin Schuhadd6d792016-03-19 01:20:01 -070096 vision_valid_ = false;
97
98 ::y2016::vision::vision_status.FetchLatest();
99
100 if (::y2016::vision::vision_status.get()) {
101 vision_valid_ = (::y2016::vision::vision_status->left_image_valid &&
102 ::y2016::vision::vision_status->right_image_valid);
103 }
104
Austin Schuh94596dd2016-03-13 21:41:26 -0700105 if (!auto_running_) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000106 HandleDrivetrain(data);
107 HandleTeleop(data);
108 }
Austin Schuh81d71db2016-03-15 20:56:24 -0700109
110 // Process any pending actions.
111 action_queue_.Tick();
112 was_running_ = action_queue_.Running();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000113 }
114
115 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
116 bool is_control_loop_driving = false;
Austin Schuh781cdcc2016-03-12 13:03:12 -0800117 static double left_goal = 0.0;
118 static double right_goal = 0.0;
119
Comran Morshed9a9948c2016-01-16 15:58:04 +0000120 const double wheel = -data.GetAxis(kSteeringWheel);
121 const double throttle = -data.GetAxis(kDriveThrottle);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000122
Austin Schuhadd6d792016-03-19 01:20:01 -0700123 if (data.IsPressed(kVisionAlign) && vision_valid_ &&
124 !vision_action_running_) {
Austin Schuh18799112016-03-16 22:09:54 -0700125 actors::VisionAlignActionParams params;
126 action_queue_.EnqueueAction(actors::MakeVisionAlignAction(params));
Austin Schuhadd6d792016-03-19 01:20:01 -0700127 vision_action_running_ = true;
Austin Schuh18799112016-03-16 22:09:54 -0700128 }
129
130 if (data.NegEdge(kVisionAlign)) {
131 action_queue_.CancelAllActions();
132 }
Austin Schuhadd6d792016-03-19 01:20:01 -0700133 if (!data.IsPressed(kVisionAlign)) {
134 vision_action_running_ = false;
135 }
Austin Schuh18799112016-03-16 22:09:54 -0700136
137 // Don't do any normal drivetrain stuff if vision is in charge.
138 if (was_running_) {
139 return;
140 }
141
Austin Schuh781cdcc2016-03-12 13:03:12 -0800142 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
143 drivetrain_queue.status.FetchLatest();
144 if (drivetrain_queue.status.get()) {
Austin Schuh45d07f62016-03-13 15:33:31 -0700145 left_goal = drivetrain_queue.status->estimated_left_position;
146 right_goal = drivetrain_queue.status->estimated_right_position;
Austin Schuh781cdcc2016-03-12 13:03:12 -0800147 }
148 }
149 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
150 is_control_loop_driving = true;
151 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000152 if (!drivetrain_queue.goal.MakeWithBuilder()
153 .steering(wheel)
154 .throttle(throttle)
155 .highgear(is_high_gear_)
156 .quickturn(data.IsPressed(kQuickTurn))
157 .control_loop_driving(is_control_loop_driving)
Austin Schuh45d07f62016-03-13 15:33:31 -0700158 .left_goal(left_goal - wheel * 0.5 + throttle * 0.3)
159 .right_goal(right_goal + wheel * 0.5 + throttle * 0.3)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000160 .left_velocity_goal(0)
161 .right_velocity_goal(0)
162 .Send()) {
163 LOG(WARNING, "sending stick values failed\n");
164 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000165
Campbell Crowley5b27f022016-02-20 16:55:35 -0800166 if (data.PosEdge(kShiftLow)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000167 is_high_gear_ = false;
168 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000169
Campbell Crowley5b27f022016-02-20 16:55:35 -0800170 if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000171 is_high_gear_ = true;
172 }
173 }
174
Comran Morshed9a9948c2016-01-16 15:58:04 +0000175 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuh45d07f62016-03-13 15:33:31 -0700176 // Default the intake to up.
177 intake_goal_ = constants::Values::kIntakeRange.upper - 0.04;
178
179 bool force_lights_on = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000180 if (!data.GetControlBit(ControlBit::kEnabled)) {
181 action_queue_.CancelAllActions();
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000182 LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +0000183 }
184
Comran Morshed200dd4b2016-02-16 17:54:58 +0000185 superstructure_queue.status.FetchLatest();
186 if (!superstructure_queue.status.get()) {
187 LOG(ERROR, "Got no superstructure status packet.\n");
188 }
189
190 if (superstructure_queue.status.get() &&
191 superstructure_queue.status->zeroed) {
192 if (waiting_for_zero_) {
Austin Schuh94596dd2016-03-13 21:41:26 -0700193 LOG(DEBUG, "Zeroed! Starting teleop mode.\n");
Comran Morshed200dd4b2016-02-16 17:54:58 +0000194 waiting_for_zero_ = false;
195 }
196 } else {
197 waiting_for_zero_ = true;
198 }
199
Austin Schuhadd6d792016-03-19 01:20:01 -0700200 double intake_when_shooting = kMaxIntakeAngleBeforeArmInterference;
201 bool use_slow_profile = false;
202 if (vision_action_running_) {
203 use_slow_profile = true;
204 if (vision_valid_) {
205 intake_when_shooting -= 0.5;
206 }
207 }
208
Austin Schuh4ea06c12016-03-12 17:54:31 -0800209 if (data.IsPressed(kFrontLong)) {
210 // Forwards shot
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800211 shoulder_goal_ = M_PI / 2.0 - 0.2;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800212 wrist_goal_ = M_PI + 0.42;
213 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700214 intake_goal_ = intake_when_shooting;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800215 } else if (data.IsPressed(kBackLong)) {
216 // Backwards shot
217 shoulder_goal_ = M_PI / 2.0 - 0.2;
218 wrist_goal_ = -0.59;
219 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700220 intake_goal_ = intake_when_shooting;
Austin Schuh45d07f62016-03-13 15:33:31 -0700221 } else if (data.IsPressed(kBackFender)) {
222 // Fender shot back
223 shoulder_goal_ = 0.65;
224 wrist_goal_ = -1.0;
225 shooter_velocity_ = 550.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700226 intake_goal_ = intake_when_shooting;
Austin Schuh45d07f62016-03-13 15:33:31 -0700227 } else if (data.IsPressed(kFrontFender)) {
228 // Fender shot back
229 shoulder_goal_ = 1.45;
230 wrist_goal_ = 2.5 + 1.7;
231 shooter_velocity_ = 550.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700232 intake_goal_ = intake_when_shooting;
Austin Schuhde802e92016-02-27 14:49:03 -0800233 } else {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800234 wrist_goal_ = 0.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800235 shoulder_goal_ = -0.010;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800236 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000237 }
238
Austin Schuhde802e92016-02-27 14:49:03 -0800239 if (data.IsPressed(kTest3)) {
240 wrist_goal_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000241 }
242
Comran Morshedaa0573c2016-03-05 19:05:54 +0000243 bool ball_detected = false;
244 ::y2016::sensors::ball_detector.FetchLatest();
245 if (::y2016::sensors::ball_detector.get()) {
246 ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
247 }
248 if (data.PosEdge(kIntakeIn)) {
249 saw_ball_when_started_intaking_ = ball_detected;
250 }
251
Austin Schuh45d07f62016-03-13 15:33:31 -0700252 if (data.IsPressed(kIntakeIn)) {
253 is_intaking_ = (!ball_detected || saw_ball_when_started_intaking_);
254 if (ball_detected) {
255 force_lights_on = true;
256 }
257 } else {
258 is_intaking_ = false;
259 }
260
261 if (data.IsPressed(kIntakeDown)) {
262 if (is_intaking_) {
263 intake_goal_ = 0.1;
264 } else {
265 intake_goal_ = -0.05;
266 }
267 }
Austin Schuhde802e92016-02-27 14:49:03 -0800268
Austin Schuh4ea06c12016-03-12 17:54:31 -0800269 if (data.IsPressed(kFire) && shooter_velocity_ != 0.0) {
Austin Schuhde802e92016-02-27 14:49:03 -0800270 fire_ = true;
271 } else {
272 fire_ = false;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000273 }
274
275 if (data.PosEdge(kTest7)) {
276 }
277
Austin Schuh4ea06c12016-03-12 17:54:31 -0800278 is_outtaking_ = data.IsPressed(kIntakeOut);
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800279
Comran Morshed200dd4b2016-02-16 17:54:58 +0000280 if (!waiting_for_zero_) {
Austin Schuhadd6d792016-03-19 01:20:01 -0700281 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
282 new_superstructure_goal->angle_intake = intake_goal_;
283 new_superstructure_goal->angle_shoulder = shoulder_goal_;
284 new_superstructure_goal->angle_wrist = wrist_goal_;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800285
Austin Schuhadd6d792016-03-19 01:20:01 -0700286 new_superstructure_goal->max_angular_velocity_intake = 7.0;
287 new_superstructure_goal->max_angular_velocity_shoulder = 4.0;
288 new_superstructure_goal->max_angular_velocity_wrist = 10.0;
289 if (use_slow_profile) {
290 new_superstructure_goal->max_angular_acceleration_intake = 10.0;
291 } else {
Austin Schuh3f0b1192016-03-12 13:03:56 -0800292 new_superstructure_goal->max_angular_acceleration_intake = 40.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700293 }
294 new_superstructure_goal->max_angular_acceleration_shoulder = 10.0;
295 new_superstructure_goal->max_angular_acceleration_wrist = 25.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800296
Austin Schuhadd6d792016-03-19 01:20:01 -0700297 // Granny mode
298 /*
299 new_superstructure_goal->max_angular_velocity_intake = 0.2;
300 new_superstructure_goal->max_angular_velocity_shoulder = 0.2;
301 new_superstructure_goal->max_angular_velocity_wrist = 0.2;
302 new_superstructure_goal->max_angular_acceleration_intake = 1.0;
303 new_superstructure_goal->max_angular_acceleration_shoulder = 1.0;
304 new_superstructure_goal->max_angular_acceleration_wrist = 1.0;
305 */
306 if (is_intaking_) {
307 new_superstructure_goal->voltage_top_rollers = 12.0;
308 new_superstructure_goal->voltage_bottom_rollers = 12.0;
309 } else if (is_outtaking_) {
310 new_superstructure_goal->voltage_top_rollers = -12.0;
311 new_superstructure_goal->voltage_bottom_rollers = -7.0;
312 } else {
313 new_superstructure_goal->voltage_top_rollers = 0.0;
314 new_superstructure_goal->voltage_bottom_rollers = 0.0;
315 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000316
Austin Schuhadd6d792016-03-19 01:20:01 -0700317 if (!new_superstructure_goal.Send()) {
318 LOG(ERROR, "Sending superstructure goal failed.\n");
319 } else {
320 LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
321 intake_goal_, shoulder_goal_, wrist_goal_);
322 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000323
Austin Schuhadd6d792016-03-19 01:20:01 -0700324 if (!shooter_queue.goal.MakeWithBuilder()
325 .angular_velocity(shooter_velocity_)
326 .clamp_open(is_intaking_ || is_outtaking_)
327 .push_to_shooter(fire_)
328 .force_lights_on(force_lights_on)
329 .Send()) {
330 LOG(ERROR, "Sending shooter goal failed.\n");
Comran Morshed200dd4b2016-02-16 17:54:58 +0000331 }
332 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000333 }
334
Comran Morshed9a9948c2016-01-16 15:58:04 +0000335 private:
336 void StartAuto() {
337 LOG(INFO, "Starting auto mode\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000338
339 actors::AutonomousActionParams params;
340 params.mode = 0;
341 action_queue_.EnqueueAction(actors::MakeAutonomousAction(params));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000342 }
343
344 void StopAuto() {
345 LOG(INFO, "Stopping auto mode\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000346 action_queue_.CancelAllActions();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000347 }
348
349 bool is_high_gear_;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000350 // Whatever these are set to are our default goals to send out after zeroing.
351 double intake_goal_;
352 double shoulder_goal_;
353 double wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800354 double shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000355
356 bool was_running_ = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000357 bool auto_running_ = false;
358
Comran Morshed200dd4b2016-02-16 17:54:58 +0000359 // If we're waiting for the subsystems to zero.
360 bool waiting_for_zero_ = true;
361
Comran Morshedaa0573c2016-03-05 19:05:54 +0000362 // If true, the ball was present when the intaking button was pressed.
363 bool saw_ball_when_started_intaking_ = false;
364
Austin Schuhde802e92016-02-27 14:49:03 -0800365 bool is_intaking_ = false;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800366 bool is_outtaking_ = false;
Austin Schuhde802e92016-02-27 14:49:03 -0800367 bool fire_ = false;
368
Austin Schuhadd6d792016-03-19 01:20:01 -0700369 bool vision_action_running_ = false;
370 bool vision_valid_ = false;
371
Comran Morshed9a9948c2016-01-16 15:58:04 +0000372 ::aos::common::actions::ActionQueue action_queue_;
373
374 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
375 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
376 "no drivetrain status");
377};
378
379} // namespace joysticks
380} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000381} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000382
383int main() {
384 ::aos::Init(-1);
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000385 ::y2016::input::joysticks::Reader reader;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000386 reader.Run();
387 ::aos::Cleanup();
388}