blob: df8fd7d22c7f989eddf6d6d3dacc833c976c5a85 [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"
Comran Morshed9a9948c2016-01-16 15:58:04 +000024
25using ::frc971::control_loops::drivetrain_queue;
Comran Morshed200dd4b2016-02-16 17:54:58 +000026using ::y2016::control_loops::shooter::shooter_queue;
27using ::y2016::control_loops::superstructure_queue;
Comran Morshed9a9948c2016-01-16 15:58:04 +000028
29using ::aos::input::driver_station::ButtonLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000030using ::aos::input::driver_station::ControlBit;
Austin Schuh4ea06c12016-03-12 17:54:31 -080031using ::aos::input::driver_station::JoystickAxis;
32using ::aos::input::driver_station::POVLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000033
Comran Morshed6c6a0a92016-01-17 12:45:16 +000034namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000035namespace input {
36namespace joysticks {
37
Austin Schuh45d07f62016-03-13 15:33:31 -070038namespace {
39
40constexpr double kMaxIntakeAngleBeforeArmInterference = control_loops::
41 superstructure::CollisionAvoidance::kMaxIntakeAngleBeforeArmInterference;
42
43} // namespace
44
Comran Morshed9a9948c2016-01-16 15:58:04 +000045const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080046const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
Comran Morshed9a9948c2016-01-16 15:58:04 +000047const ButtonLocation kQuickTurn(1, 5);
48
Austin Schuh781cdcc2016-03-12 13:03:12 -080049const ButtonLocation kTurn1(1, 7);
50const ButtonLocation kTurn2(1, 11);
51
Comran Morshed200dd4b2016-02-16 17:54:58 +000052// Buttons on the lexan driver station to get things running on bring-up day.
Austin Schuh4ea06c12016-03-12 17:54:31 -080053const ButtonLocation kIntakeDown(3, 11);
54const POVLocation kFrontLong(3, 180);
55const POVLocation kBackLong(3, 0);
Austin Schuh45d07f62016-03-13 15:33:31 -070056const POVLocation kBackFender(3, 90);
57const POVLocation kFrontFender(3, 270);
Austin Schuh4ea06c12016-03-12 17:54:31 -080058const ButtonLocation kTest3(3, 7);
59const ButtonLocation kIntakeIn(3, 12);
Comran Morshed200dd4b2016-02-16 17:54:58 +000060const ButtonLocation kTest5(3, 8);
Austin Schuh4ea06c12016-03-12 17:54:31 -080061const ButtonLocation kFire(3, 3);
Comran Morshed200dd4b2016-02-16 17:54:58 +000062const ButtonLocation kTest7(3, 5);
Austin Schuh4ea06c12016-03-12 17:54:31 -080063const ButtonLocation kIntakeOut(3, 9);
Comran Morshed200dd4b2016-02-16 17:54:58 +000064
Comran Morshed9a9948c2016-01-16 15:58:04 +000065class Reader : public ::aos::input::JoystickInput {
66 public:
67 Reader()
Austin Schuh94596dd2016-03-13 21:41:26 -070068 : is_high_gear_(true),
Comran Morshed200dd4b2016-02-16 17:54:58 +000069 intake_goal_(0.0),
70 shoulder_goal_(M_PI / 2.0),
71 wrist_goal_(0.0) {}
Comran Morshed9a9948c2016-01-16 15:58:04 +000072
73 void RunIteration(const ::aos::input::driver_station::Data &data) override {
74 bool last_auto_running = auto_running_;
75 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
76 data.GetControlBit(ControlBit::kEnabled);
77 if (auto_running_ != last_auto_running) {
78 if (auto_running_) {
79 StartAuto();
80 } else {
81 StopAuto();
82 }
83 }
84
Austin Schuh94596dd2016-03-13 21:41:26 -070085 if (!data.GetControlBit(ControlBit::kEnabled)) {
86 // If we are not enabled, reset the waiting for zero bit.
87 LOG(DEBUG, "Waiting for zero.\n");
88 waiting_for_zero_ = true;
89 is_high_gear_ = true;
90 }
91
92 if (!auto_running_) {
Comran Morshed9a9948c2016-01-16 15:58:04 +000093 HandleDrivetrain(data);
94 HandleTeleop(data);
95 }
Austin Schuh81d71db2016-03-15 20:56:24 -070096
97 // Process any pending actions.
98 action_queue_.Tick();
99 was_running_ = action_queue_.Running();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000100 }
101
102 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
103 bool is_control_loop_driving = false;
Austin Schuh781cdcc2016-03-12 13:03:12 -0800104 static double left_goal = 0.0;
105 static double right_goal = 0.0;
106
Comran Morshed9a9948c2016-01-16 15:58:04 +0000107 const double wheel = -data.GetAxis(kSteeringWheel);
108 const double throttle = -data.GetAxis(kDriveThrottle);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000109
Austin Schuh781cdcc2016-03-12 13:03:12 -0800110 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
111 drivetrain_queue.status.FetchLatest();
112 if (drivetrain_queue.status.get()) {
Austin Schuh45d07f62016-03-13 15:33:31 -0700113 left_goal = drivetrain_queue.status->estimated_left_position;
114 right_goal = drivetrain_queue.status->estimated_right_position;
Austin Schuh781cdcc2016-03-12 13:03:12 -0800115 }
116 }
117 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
118 is_control_loop_driving = true;
119 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000120 if (!drivetrain_queue.goal.MakeWithBuilder()
121 .steering(wheel)
122 .throttle(throttle)
123 .highgear(is_high_gear_)
124 .quickturn(data.IsPressed(kQuickTurn))
125 .control_loop_driving(is_control_loop_driving)
Austin Schuh45d07f62016-03-13 15:33:31 -0700126 .left_goal(left_goal - wheel * 0.5 + throttle * 0.3)
127 .right_goal(right_goal + wheel * 0.5 + throttle * 0.3)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000128 .left_velocity_goal(0)
129 .right_velocity_goal(0)
130 .Send()) {
131 LOG(WARNING, "sending stick values failed\n");
132 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000133
Campbell Crowley5b27f022016-02-20 16:55:35 -0800134 if (data.PosEdge(kShiftLow)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000135 is_high_gear_ = false;
136 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000137
Campbell Crowley5b27f022016-02-20 16:55:35 -0800138 if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000139 is_high_gear_ = true;
140 }
141 }
142
Comran Morshed9a9948c2016-01-16 15:58:04 +0000143 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuh45d07f62016-03-13 15:33:31 -0700144 // Default the intake to up.
145 intake_goal_ = constants::Values::kIntakeRange.upper - 0.04;
146
147 bool force_lights_on = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000148 if (!data.GetControlBit(ControlBit::kEnabled)) {
149 action_queue_.CancelAllActions();
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000150 LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +0000151 }
152
Comran Morshed200dd4b2016-02-16 17:54:58 +0000153 superstructure_queue.status.FetchLatest();
154 if (!superstructure_queue.status.get()) {
155 LOG(ERROR, "Got no superstructure status packet.\n");
156 }
157
158 if (superstructure_queue.status.get() &&
159 superstructure_queue.status->zeroed) {
160 if (waiting_for_zero_) {
Austin Schuh94596dd2016-03-13 21:41:26 -0700161 LOG(DEBUG, "Zeroed! Starting teleop mode.\n");
Comran Morshed200dd4b2016-02-16 17:54:58 +0000162 waiting_for_zero_ = false;
163 }
164 } else {
165 waiting_for_zero_ = true;
166 }
167
Austin Schuh4ea06c12016-03-12 17:54:31 -0800168 if (data.IsPressed(kFrontLong)) {
169 // Forwards shot
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800170 shoulder_goal_ = M_PI / 2.0 - 0.2;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800171 wrist_goal_ = M_PI + 0.42;
172 shooter_velocity_ = 640.0;
Austin Schuh45d07f62016-03-13 15:33:31 -0700173 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800174 } else if (data.IsPressed(kBackLong)) {
175 // Backwards shot
176 shoulder_goal_ = M_PI / 2.0 - 0.2;
177 wrist_goal_ = -0.59;
178 shooter_velocity_ = 640.0;
Austin Schuh45d07f62016-03-13 15:33:31 -0700179 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
180 } else if (data.IsPressed(kBackFender)) {
181 // Fender shot back
182 shoulder_goal_ = 0.65;
183 wrist_goal_ = -1.0;
184 shooter_velocity_ = 550.0;
185 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
186 } else if (data.IsPressed(kFrontFender)) {
187 // Fender shot back
188 shoulder_goal_ = 1.45;
189 wrist_goal_ = 2.5 + 1.7;
190 shooter_velocity_ = 550.0;
191 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
Austin Schuhde802e92016-02-27 14:49:03 -0800192 } else {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800193 wrist_goal_ = 0.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800194 shoulder_goal_ = -0.010;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800195 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000196 }
197
Austin Schuhde802e92016-02-27 14:49:03 -0800198 if (data.IsPressed(kTest3)) {
199 wrist_goal_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000200 }
201
Comran Morshedaa0573c2016-03-05 19:05:54 +0000202 bool ball_detected = false;
203 ::y2016::sensors::ball_detector.FetchLatest();
204 if (::y2016::sensors::ball_detector.get()) {
205 ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
206 }
207 if (data.PosEdge(kIntakeIn)) {
208 saw_ball_when_started_intaking_ = ball_detected;
209 }
210
Austin Schuh45d07f62016-03-13 15:33:31 -0700211 if (data.IsPressed(kIntakeIn)) {
212 is_intaking_ = (!ball_detected || saw_ball_when_started_intaking_);
213 if (ball_detected) {
214 force_lights_on = true;
215 }
216 } else {
217 is_intaking_ = false;
218 }
219
220 if (data.IsPressed(kIntakeDown)) {
221 if (is_intaking_) {
222 intake_goal_ = 0.1;
223 } else {
224 intake_goal_ = -0.05;
225 }
226 }
Austin Schuhde802e92016-02-27 14:49:03 -0800227
Austin Schuh4ea06c12016-03-12 17:54:31 -0800228 if (data.IsPressed(kFire) && shooter_velocity_ != 0.0) {
Austin Schuhde802e92016-02-27 14:49:03 -0800229 fire_ = true;
230 } else {
231 fire_ = false;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000232 }
233
234 if (data.PosEdge(kTest7)) {
235 }
236
Austin Schuh4ea06c12016-03-12 17:54:31 -0800237 is_outtaking_ = data.IsPressed(kIntakeOut);
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800238
Comran Morshed200dd4b2016-02-16 17:54:58 +0000239 if (!waiting_for_zero_) {
240 if (!action_queue_.Running()) {
241 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
242 new_superstructure_goal->angle_intake = intake_goal_;
243 new_superstructure_goal->angle_shoulder = shoulder_goal_;
244 new_superstructure_goal->angle_wrist = wrist_goal_;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800245
246 new_superstructure_goal->max_angular_velocity_intake = 7.0;
247 new_superstructure_goal->max_angular_velocity_shoulder = 4.0;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800248 new_superstructure_goal->max_angular_velocity_wrist = 10.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800249 new_superstructure_goal->max_angular_acceleration_intake = 40.0;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800250 new_superstructure_goal->max_angular_acceleration_shoulder = 10.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800251 new_superstructure_goal->max_angular_acceleration_wrist = 25.0;
252
Austin Schuh3f0b1192016-03-12 13:03:56 -0800253 // Granny mode
Austin Schuh4ea06c12016-03-12 17:54:31 -0800254 /*
Austin Schuh3f0b1192016-03-12 13:03:56 -0800255 new_superstructure_goal->max_angular_velocity_intake = 0.2;
256 new_superstructure_goal->max_angular_velocity_shoulder = 0.2;
257 new_superstructure_goal->max_angular_velocity_wrist = 0.2;
258 new_superstructure_goal->max_angular_acceleration_intake = 1.0;
259 new_superstructure_goal->max_angular_acceleration_shoulder = 1.0;
260 new_superstructure_goal->max_angular_acceleration_wrist = 1.0;
261 */
Austin Schuhde802e92016-02-27 14:49:03 -0800262 if (is_intaking_) {
263 new_superstructure_goal->voltage_top_rollers = 12.0;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800264 new_superstructure_goal->voltage_bottom_rollers = 12.0;
265 } else if (is_outtaking_) {
266 new_superstructure_goal->voltage_top_rollers = -12.0;
267 new_superstructure_goal->voltage_bottom_rollers = -7.0;
Austin Schuhde802e92016-02-27 14:49:03 -0800268 } else {
269 new_superstructure_goal->voltage_top_rollers = 0.0;
270 new_superstructure_goal->voltage_bottom_rollers = 0.0;
271 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000272
273 if (!new_superstructure_goal.Send()) {
274 LOG(ERROR, "Sending superstructure goal failed.\n");
275 } else {
276 LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
277 intake_goal_, shoulder_goal_, wrist_goal_);
278 }
279
280 if (!shooter_queue.goal.MakeWithBuilder()
Austin Schuhde802e92016-02-27 14:49:03 -0800281 .angular_velocity(shooter_velocity_)
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800282 .clamp_open(is_intaking_ || is_outtaking_)
Austin Schuhde802e92016-02-27 14:49:03 -0800283 .push_to_shooter(fire_)
Austin Schuh45d07f62016-03-13 15:33:31 -0700284 .force_lights_on(force_lights_on)
Comran Morshed200dd4b2016-02-16 17:54:58 +0000285 .Send()) {
286 LOG(ERROR, "Sending shooter goal failed.\n");
287 }
288 }
289 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000290 }
291
Comran Morshed9a9948c2016-01-16 15:58:04 +0000292 private:
293 void StartAuto() {
294 LOG(INFO, "Starting auto mode\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000295
296 actors::AutonomousActionParams params;
297 params.mode = 0;
298 action_queue_.EnqueueAction(actors::MakeAutonomousAction(params));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000299 }
300
301 void StopAuto() {
302 LOG(INFO, "Stopping auto mode\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000303 action_queue_.CancelAllActions();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000304 }
305
306 bool is_high_gear_;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000307 // Whatever these are set to are our default goals to send out after zeroing.
308 double intake_goal_;
309 double shoulder_goal_;
310 double wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800311 double shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000312
313 bool was_running_ = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000314 bool auto_running_ = false;
315
Comran Morshed200dd4b2016-02-16 17:54:58 +0000316 // If we're waiting for the subsystems to zero.
317 bool waiting_for_zero_ = true;
318
Comran Morshedaa0573c2016-03-05 19:05:54 +0000319 // If true, the ball was present when the intaking button was pressed.
320 bool saw_ball_when_started_intaking_ = false;
321
Austin Schuhde802e92016-02-27 14:49:03 -0800322 bool is_intaking_ = false;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800323 bool is_outtaking_ = false;
Austin Schuhde802e92016-02-27 14:49:03 -0800324 bool fire_ = false;
325
Comran Morshed9a9948c2016-01-16 15:58:04 +0000326 ::aos::common::actions::ActionQueue action_queue_;
327
328 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
329 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
330 "no drivetrain status");
331};
332
333} // namespace joysticks
334} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000335} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000336
337int main() {
338 ::aos::Init(-1);
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000339 ::y2016::input::joysticks::Reader reader;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000340 reader.Run();
341 ::aos::Cleanup();
342}