blob: 87ebaf6e58dae127cb21ecd9015b55096b80b4de [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"
Comran Morshed200dd4b2016-02-16 17:54:58 +000019
Comran Morshed6c6a0a92016-01-17 12:45:16 +000020#include "y2016/constants.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000021#include "frc971/queues/gyro.q.h"
22#include "frc971/autonomous/auto.q.h"
Comran Morshede68e3732016-03-12 14:12:11 +000023#include "y2016/actors/autonomous_actor.h"
Austin Schuh18799112016-03-16 22:09:54 -070024#include "y2016/actors/vision_align_actor.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000025
26using ::frc971::control_loops::drivetrain_queue;
Comran Morshed200dd4b2016-02-16 17:54:58 +000027using ::y2016::control_loops::shooter::shooter_queue;
28using ::y2016::control_loops::superstructure_queue;
Comran Morshed9a9948c2016-01-16 15:58:04 +000029
30using ::aos::input::driver_station::ButtonLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000031using ::aos::input::driver_station::ControlBit;
Austin Schuh4ea06c12016-03-12 17:54:31 -080032using ::aos::input::driver_station::JoystickAxis;
33using ::aos::input::driver_station::POVLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000034
Comran Morshed6c6a0a92016-01-17 12:45:16 +000035namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000036namespace input {
37namespace joysticks {
38
Austin Schuh45d07f62016-03-13 15:33:31 -070039namespace {
40
41constexpr double kMaxIntakeAngleBeforeArmInterference = control_loops::
42 superstructure::CollisionAvoidance::kMaxIntakeAngleBeforeArmInterference;
43
44} // namespace
45
Comran Morshed9a9948c2016-01-16 15:58:04 +000046const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080047const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
Comran Morshed9a9948c2016-01-16 15:58:04 +000048const ButtonLocation kQuickTurn(1, 5);
49
Austin Schuh781cdcc2016-03-12 13:03:12 -080050const ButtonLocation kTurn1(1, 7);
51const ButtonLocation kTurn2(1, 11);
52
Comran Morshed200dd4b2016-02-16 17:54:58 +000053// Buttons on the lexan driver station to get things running on bring-up day.
Austin Schuh4ea06c12016-03-12 17:54:31 -080054const ButtonLocation kIntakeDown(3, 11);
55const POVLocation kFrontLong(3, 180);
56const POVLocation kBackLong(3, 0);
Austin Schuh45d07f62016-03-13 15:33:31 -070057const POVLocation kBackFender(3, 90);
58const POVLocation kFrontFender(3, 270);
Austin Schuh4ea06c12016-03-12 17:54:31 -080059const ButtonLocation kTest3(3, 7);
60const ButtonLocation kIntakeIn(3, 12);
Comran Morshed200dd4b2016-02-16 17:54:58 +000061const ButtonLocation kTest5(3, 8);
Austin Schuh4ea06c12016-03-12 17:54:31 -080062const ButtonLocation kFire(3, 3);
Comran Morshed200dd4b2016-02-16 17:54:58 +000063const ButtonLocation kTest7(3, 5);
Austin Schuh4ea06c12016-03-12 17:54:31 -080064const ButtonLocation kIntakeOut(3, 9);
Comran Morshed200dd4b2016-02-16 17:54:58 +000065
Austin Schuh18799112016-03-16 22:09:54 -070066const ButtonLocation kVisionAlign(1, 6);
67
Comran Morshed9a9948c2016-01-16 15:58:04 +000068class Reader : public ::aos::input::JoystickInput {
69 public:
70 Reader()
Austin Schuh94596dd2016-03-13 21:41:26 -070071 : is_high_gear_(true),
Comran Morshed200dd4b2016-02-16 17:54:58 +000072 intake_goal_(0.0),
73 shoulder_goal_(M_PI / 2.0),
74 wrist_goal_(0.0) {}
Comran Morshed9a9948c2016-01-16 15:58:04 +000075
76 void RunIteration(const ::aos::input::driver_station::Data &data) override {
77 bool last_auto_running = auto_running_;
78 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
79 data.GetControlBit(ControlBit::kEnabled);
80 if (auto_running_ != last_auto_running) {
81 if (auto_running_) {
82 StartAuto();
83 } else {
84 StopAuto();
85 }
86 }
87
Austin Schuh94596dd2016-03-13 21:41:26 -070088 if (!data.GetControlBit(ControlBit::kEnabled)) {
89 // If we are not enabled, reset the waiting for zero bit.
90 LOG(DEBUG, "Waiting for zero.\n");
91 waiting_for_zero_ = true;
92 is_high_gear_ = true;
93 }
94
95 if (!auto_running_) {
Comran Morshed9a9948c2016-01-16 15:58:04 +000096 HandleDrivetrain(data);
97 HandleTeleop(data);
98 }
Austin Schuh81d71db2016-03-15 20:56:24 -070099
100 // Process any pending actions.
101 action_queue_.Tick();
102 was_running_ = action_queue_.Running();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000103 }
104
105 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
106 bool is_control_loop_driving = false;
Austin Schuh781cdcc2016-03-12 13:03:12 -0800107 static double left_goal = 0.0;
108 static double right_goal = 0.0;
109
Comran Morshed9a9948c2016-01-16 15:58:04 +0000110 const double wheel = -data.GetAxis(kSteeringWheel);
111 const double throttle = -data.GetAxis(kDriveThrottle);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000112
Austin Schuh18799112016-03-16 22:09:54 -0700113 if (data.PosEdge(kVisionAlign)) {
114 actors::VisionAlignActionParams params;
115 action_queue_.EnqueueAction(actors::MakeVisionAlignAction(params));
116 }
117
118 if (data.NegEdge(kVisionAlign)) {
119 action_queue_.CancelAllActions();
120 }
121
122 // Don't do any normal drivetrain stuff if vision is in charge.
123 if (was_running_) {
124 return;
125 }
126
Austin Schuh781cdcc2016-03-12 13:03:12 -0800127 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
128 drivetrain_queue.status.FetchLatest();
129 if (drivetrain_queue.status.get()) {
Austin Schuh45d07f62016-03-13 15:33:31 -0700130 left_goal = drivetrain_queue.status->estimated_left_position;
131 right_goal = drivetrain_queue.status->estimated_right_position;
Austin Schuh781cdcc2016-03-12 13:03:12 -0800132 }
133 }
134 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
135 is_control_loop_driving = true;
136 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000137 if (!drivetrain_queue.goal.MakeWithBuilder()
138 .steering(wheel)
139 .throttle(throttle)
140 .highgear(is_high_gear_)
141 .quickturn(data.IsPressed(kQuickTurn))
142 .control_loop_driving(is_control_loop_driving)
Austin Schuh45d07f62016-03-13 15:33:31 -0700143 .left_goal(left_goal - wheel * 0.5 + throttle * 0.3)
144 .right_goal(right_goal + wheel * 0.5 + throttle * 0.3)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000145 .left_velocity_goal(0)
146 .right_velocity_goal(0)
147 .Send()) {
148 LOG(WARNING, "sending stick values failed\n");
149 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000150
Campbell Crowley5b27f022016-02-20 16:55:35 -0800151 if (data.PosEdge(kShiftLow)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000152 is_high_gear_ = false;
153 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000154
Campbell Crowley5b27f022016-02-20 16:55:35 -0800155 if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000156 is_high_gear_ = true;
157 }
158 }
159
Comran Morshed9a9948c2016-01-16 15:58:04 +0000160 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuh45d07f62016-03-13 15:33:31 -0700161 // Default the intake to up.
162 intake_goal_ = constants::Values::kIntakeRange.upper - 0.04;
163
164 bool force_lights_on = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000165 if (!data.GetControlBit(ControlBit::kEnabled)) {
166 action_queue_.CancelAllActions();
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000167 LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +0000168 }
169
Comran Morshed200dd4b2016-02-16 17:54:58 +0000170 superstructure_queue.status.FetchLatest();
171 if (!superstructure_queue.status.get()) {
172 LOG(ERROR, "Got no superstructure status packet.\n");
173 }
174
175 if (superstructure_queue.status.get() &&
176 superstructure_queue.status->zeroed) {
177 if (waiting_for_zero_) {
Austin Schuh94596dd2016-03-13 21:41:26 -0700178 LOG(DEBUG, "Zeroed! Starting teleop mode.\n");
Comran Morshed200dd4b2016-02-16 17:54:58 +0000179 waiting_for_zero_ = false;
180 }
181 } else {
182 waiting_for_zero_ = true;
183 }
184
Austin Schuh4ea06c12016-03-12 17:54:31 -0800185 if (data.IsPressed(kFrontLong)) {
186 // Forwards shot
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800187 shoulder_goal_ = M_PI / 2.0 - 0.2;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800188 wrist_goal_ = M_PI + 0.42;
189 shooter_velocity_ = 640.0;
Austin Schuh45d07f62016-03-13 15:33:31 -0700190 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800191 } else if (data.IsPressed(kBackLong)) {
192 // Backwards shot
193 shoulder_goal_ = M_PI / 2.0 - 0.2;
194 wrist_goal_ = -0.59;
195 shooter_velocity_ = 640.0;
Austin Schuh45d07f62016-03-13 15:33:31 -0700196 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
197 } else if (data.IsPressed(kBackFender)) {
198 // Fender shot back
199 shoulder_goal_ = 0.65;
200 wrist_goal_ = -1.0;
201 shooter_velocity_ = 550.0;
202 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
203 } else if (data.IsPressed(kFrontFender)) {
204 // Fender shot back
205 shoulder_goal_ = 1.45;
206 wrist_goal_ = 2.5 + 1.7;
207 shooter_velocity_ = 550.0;
208 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
Austin Schuhde802e92016-02-27 14:49:03 -0800209 } else {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800210 wrist_goal_ = 0.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800211 shoulder_goal_ = -0.010;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800212 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000213 }
214
Austin Schuhde802e92016-02-27 14:49:03 -0800215 if (data.IsPressed(kTest3)) {
216 wrist_goal_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000217 }
218
Comran Morshedaa0573c2016-03-05 19:05:54 +0000219 bool ball_detected = false;
220 ::y2016::sensors::ball_detector.FetchLatest();
221 if (::y2016::sensors::ball_detector.get()) {
222 ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
223 }
224 if (data.PosEdge(kIntakeIn)) {
225 saw_ball_when_started_intaking_ = ball_detected;
226 }
227
Austin Schuh45d07f62016-03-13 15:33:31 -0700228 if (data.IsPressed(kIntakeIn)) {
229 is_intaking_ = (!ball_detected || saw_ball_when_started_intaking_);
230 if (ball_detected) {
231 force_lights_on = true;
232 }
233 } else {
234 is_intaking_ = false;
235 }
236
237 if (data.IsPressed(kIntakeDown)) {
238 if (is_intaking_) {
239 intake_goal_ = 0.1;
240 } else {
241 intake_goal_ = -0.05;
242 }
243 }
Austin Schuhde802e92016-02-27 14:49:03 -0800244
Austin Schuh4ea06c12016-03-12 17:54:31 -0800245 if (data.IsPressed(kFire) && shooter_velocity_ != 0.0) {
Austin Schuhde802e92016-02-27 14:49:03 -0800246 fire_ = true;
247 } else {
248 fire_ = false;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000249 }
250
251 if (data.PosEdge(kTest7)) {
252 }
253
Austin Schuh4ea06c12016-03-12 17:54:31 -0800254 is_outtaking_ = data.IsPressed(kIntakeOut);
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800255
Comran Morshed200dd4b2016-02-16 17:54:58 +0000256 if (!waiting_for_zero_) {
257 if (!action_queue_.Running()) {
258 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
259 new_superstructure_goal->angle_intake = intake_goal_;
260 new_superstructure_goal->angle_shoulder = shoulder_goal_;
261 new_superstructure_goal->angle_wrist = wrist_goal_;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800262
263 new_superstructure_goal->max_angular_velocity_intake = 7.0;
264 new_superstructure_goal->max_angular_velocity_shoulder = 4.0;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800265 new_superstructure_goal->max_angular_velocity_wrist = 10.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800266 new_superstructure_goal->max_angular_acceleration_intake = 40.0;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800267 new_superstructure_goal->max_angular_acceleration_shoulder = 10.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800268 new_superstructure_goal->max_angular_acceleration_wrist = 25.0;
269
Austin Schuh3f0b1192016-03-12 13:03:56 -0800270 // Granny mode
Austin Schuh4ea06c12016-03-12 17:54:31 -0800271 /*
Austin Schuh3f0b1192016-03-12 13:03:56 -0800272 new_superstructure_goal->max_angular_velocity_intake = 0.2;
273 new_superstructure_goal->max_angular_velocity_shoulder = 0.2;
274 new_superstructure_goal->max_angular_velocity_wrist = 0.2;
275 new_superstructure_goal->max_angular_acceleration_intake = 1.0;
276 new_superstructure_goal->max_angular_acceleration_shoulder = 1.0;
277 new_superstructure_goal->max_angular_acceleration_wrist = 1.0;
278 */
Austin Schuhde802e92016-02-27 14:49:03 -0800279 if (is_intaking_) {
280 new_superstructure_goal->voltage_top_rollers = 12.0;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800281 new_superstructure_goal->voltage_bottom_rollers = 12.0;
282 } else if (is_outtaking_) {
283 new_superstructure_goal->voltage_top_rollers = -12.0;
284 new_superstructure_goal->voltage_bottom_rollers = -7.0;
Austin Schuhde802e92016-02-27 14:49:03 -0800285 } else {
286 new_superstructure_goal->voltage_top_rollers = 0.0;
287 new_superstructure_goal->voltage_bottom_rollers = 0.0;
288 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000289
290 if (!new_superstructure_goal.Send()) {
291 LOG(ERROR, "Sending superstructure goal failed.\n");
292 } else {
293 LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
294 intake_goal_, shoulder_goal_, wrist_goal_);
295 }
296
297 if (!shooter_queue.goal.MakeWithBuilder()
Austin Schuhde802e92016-02-27 14:49:03 -0800298 .angular_velocity(shooter_velocity_)
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800299 .clamp_open(is_intaking_ || is_outtaking_)
Austin Schuhde802e92016-02-27 14:49:03 -0800300 .push_to_shooter(fire_)
Austin Schuh45d07f62016-03-13 15:33:31 -0700301 .force_lights_on(force_lights_on)
Comran Morshed200dd4b2016-02-16 17:54:58 +0000302 .Send()) {
303 LOG(ERROR, "Sending shooter goal failed.\n");
304 }
305 }
306 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000307 }
308
Comran Morshed9a9948c2016-01-16 15:58:04 +0000309 private:
310 void StartAuto() {
311 LOG(INFO, "Starting auto mode\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000312
313 actors::AutonomousActionParams params;
314 params.mode = 0;
315 action_queue_.EnqueueAction(actors::MakeAutonomousAction(params));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000316 }
317
318 void StopAuto() {
319 LOG(INFO, "Stopping auto mode\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000320 action_queue_.CancelAllActions();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000321 }
322
323 bool is_high_gear_;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000324 // Whatever these are set to are our default goals to send out after zeroing.
325 double intake_goal_;
326 double shoulder_goal_;
327 double wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800328 double shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000329
330 bool was_running_ = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000331 bool auto_running_ = false;
332
Comran Morshed200dd4b2016-02-16 17:54:58 +0000333 // If we're waiting for the subsystems to zero.
334 bool waiting_for_zero_ = true;
335
Comran Morshedaa0573c2016-03-05 19:05:54 +0000336 // If true, the ball was present when the intaking button was pressed.
337 bool saw_ball_when_started_intaking_ = false;
338
Austin Schuhde802e92016-02-27 14:49:03 -0800339 bool is_intaking_ = false;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800340 bool is_outtaking_ = false;
Austin Schuhde802e92016-02-27 14:49:03 -0800341 bool fire_ = false;
342
Comran Morshed9a9948c2016-01-16 15:58:04 +0000343 ::aos::common::actions::ActionQueue action_queue_;
344
345 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
346 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
347 "no drivetrain status");
348};
349
350} // namespace joysticks
351} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000352} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000353
354int main() {
355 ::aos::Init(-1);
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000356 ::y2016::input::joysticks::Reader reader;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000357 reader.Run();
358 ::aos::Cleanup();
359}