blob: 96586d320a03e4b534d88dc9efe85b4895bfeb80 [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 Morshed9a9948c2016-01-16 15:58:04 +000023
24using ::frc971::control_loops::drivetrain_queue;
Comran Morshed200dd4b2016-02-16 17:54:58 +000025using ::y2016::control_loops::shooter::shooter_queue;
26using ::y2016::control_loops::superstructure_queue;
Comran Morshed9a9948c2016-01-16 15:58:04 +000027
28using ::aos::input::driver_station::ButtonLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000029using ::aos::input::driver_station::ControlBit;
Austin Schuh4ea06c12016-03-12 17:54:31 -080030using ::aos::input::driver_station::JoystickAxis;
31using ::aos::input::driver_station::POVLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000032
Comran Morshed6c6a0a92016-01-17 12:45:16 +000033namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000034namespace input {
35namespace joysticks {
36
Austin Schuh45d07f62016-03-13 15:33:31 -070037namespace {
38
39constexpr double kMaxIntakeAngleBeforeArmInterference = control_loops::
40 superstructure::CollisionAvoidance::kMaxIntakeAngleBeforeArmInterference;
41
42} // namespace
43
Comran Morshed9a9948c2016-01-16 15:58:04 +000044const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080045const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
Comran Morshed9a9948c2016-01-16 15:58:04 +000046const ButtonLocation kQuickTurn(1, 5);
47
Austin Schuh781cdcc2016-03-12 13:03:12 -080048const ButtonLocation kTurn1(1, 7);
49const ButtonLocation kTurn2(1, 11);
50
Comran Morshed200dd4b2016-02-16 17:54:58 +000051// Buttons on the lexan driver station to get things running on bring-up day.
Austin Schuh4ea06c12016-03-12 17:54:31 -080052const ButtonLocation kIntakeDown(3, 11);
53const POVLocation kFrontLong(3, 180);
54const POVLocation kBackLong(3, 0);
Austin Schuh45d07f62016-03-13 15:33:31 -070055const POVLocation kBackFender(3, 90);
56const POVLocation kFrontFender(3, 270);
Austin Schuh4ea06c12016-03-12 17:54:31 -080057const ButtonLocation kTest3(3, 7);
58const ButtonLocation kIntakeIn(3, 12);
Comran Morshed200dd4b2016-02-16 17:54:58 +000059const ButtonLocation kTest5(3, 8);
Austin Schuh4ea06c12016-03-12 17:54:31 -080060const ButtonLocation kFire(3, 3);
Comran Morshed200dd4b2016-02-16 17:54:58 +000061const ButtonLocation kTest7(3, 5);
Austin Schuh4ea06c12016-03-12 17:54:31 -080062const ButtonLocation kIntakeOut(3, 9);
Comran Morshed200dd4b2016-02-16 17:54:58 +000063
Comran Morshed9a9948c2016-01-16 15:58:04 +000064class Reader : public ::aos::input::JoystickInput {
65 public:
66 Reader()
67 : is_high_gear_(false),
Comran Morshed200dd4b2016-02-16 17:54:58 +000068 intake_goal_(0.0),
69 shoulder_goal_(M_PI / 2.0),
70 wrist_goal_(0.0) {}
Comran Morshed9a9948c2016-01-16 15:58:04 +000071
72 void RunIteration(const ::aos::input::driver_station::Data &data) override {
73 bool last_auto_running = auto_running_;
74 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
75 data.GetControlBit(ControlBit::kEnabled);
76 if (auto_running_ != last_auto_running) {
77 if (auto_running_) {
78 StartAuto();
79 } else {
80 StopAuto();
81 }
82 }
83
84 if (!data.GetControlBit(ControlBit::kAutonomous)) {
85 HandleDrivetrain(data);
86 HandleTeleop(data);
87 }
88 }
89
90 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
91 bool is_control_loop_driving = false;
Austin Schuh781cdcc2016-03-12 13:03:12 -080092 static double left_goal = 0.0;
93 static double right_goal = 0.0;
94
Comran Morshed9a9948c2016-01-16 15:58:04 +000095 const double wheel = -data.GetAxis(kSteeringWheel);
96 const double throttle = -data.GetAxis(kDriveThrottle);
Comran Morshed9a9948c2016-01-16 15:58:04 +000097
Austin Schuh781cdcc2016-03-12 13:03:12 -080098 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
99 drivetrain_queue.status.FetchLatest();
100 if (drivetrain_queue.status.get()) {
Austin Schuh45d07f62016-03-13 15:33:31 -0700101 left_goal = drivetrain_queue.status->estimated_left_position;
102 right_goal = drivetrain_queue.status->estimated_right_position;
Austin Schuh781cdcc2016-03-12 13:03:12 -0800103 }
104 }
105 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
106 is_control_loop_driving = true;
107 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000108 if (!drivetrain_queue.goal.MakeWithBuilder()
109 .steering(wheel)
110 .throttle(throttle)
111 .highgear(is_high_gear_)
112 .quickturn(data.IsPressed(kQuickTurn))
113 .control_loop_driving(is_control_loop_driving)
Austin Schuh45d07f62016-03-13 15:33:31 -0700114 .left_goal(left_goal - wheel * 0.5 + throttle * 0.3)
115 .right_goal(right_goal + wheel * 0.5 + throttle * 0.3)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000116 .left_velocity_goal(0)
117 .right_velocity_goal(0)
118 .Send()) {
119 LOG(WARNING, "sending stick values failed\n");
120 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000121
Campbell Crowley5b27f022016-02-20 16:55:35 -0800122 if (data.PosEdge(kShiftLow)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000123 is_high_gear_ = false;
124 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000125
Campbell Crowley5b27f022016-02-20 16:55:35 -0800126 if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000127 is_high_gear_ = true;
128 }
129 }
130
Comran Morshed9a9948c2016-01-16 15:58:04 +0000131 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuh45d07f62016-03-13 15:33:31 -0700132 // Default the intake to up.
133 intake_goal_ = constants::Values::kIntakeRange.upper - 0.04;
134
135 bool force_lights_on = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000136 if (!data.GetControlBit(ControlBit::kEnabled)) {
137 action_queue_.CancelAllActions();
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000138 LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +0000139 }
140
Comran Morshed200dd4b2016-02-16 17:54:58 +0000141 if (data.PosEdge(ControlBit::kEnabled)) {
142 // If we got enabled, wait for everything to zero.
143 LOG(INFO, "Waiting for zero.\n");
144 waiting_for_zero_ = true;
Austin Schuhde802e92016-02-27 14:49:03 -0800145 is_high_gear_ = true;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000146 }
147
148 superstructure_queue.status.FetchLatest();
149 if (!superstructure_queue.status.get()) {
150 LOG(ERROR, "Got no superstructure status packet.\n");
151 }
152
153 if (superstructure_queue.status.get() &&
154 superstructure_queue.status->zeroed) {
155 if (waiting_for_zero_) {
156 LOG(INFO, "Zeroed! Starting teleop mode.\n");
157 waiting_for_zero_ = false;
158 }
159 } else {
160 waiting_for_zero_ = true;
161 }
162
Austin Schuh4ea06c12016-03-12 17:54:31 -0800163 if (data.IsPressed(kFrontLong)) {
164 // Forwards shot
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800165 shoulder_goal_ = M_PI / 2.0 - 0.2;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800166 wrist_goal_ = M_PI + 0.42;
167 shooter_velocity_ = 640.0;
Austin Schuh45d07f62016-03-13 15:33:31 -0700168 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800169 } else if (data.IsPressed(kBackLong)) {
170 // Backwards shot
171 shoulder_goal_ = M_PI / 2.0 - 0.2;
172 wrist_goal_ = -0.59;
173 shooter_velocity_ = 640.0;
Austin Schuh45d07f62016-03-13 15:33:31 -0700174 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
175 } else if (data.IsPressed(kBackFender)) {
176 // Fender shot back
177 shoulder_goal_ = 0.65;
178 wrist_goal_ = -1.0;
179 shooter_velocity_ = 550.0;
180 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
181 } else if (data.IsPressed(kFrontFender)) {
182 // Fender shot back
183 shoulder_goal_ = 1.45;
184 wrist_goal_ = 2.5 + 1.7;
185 shooter_velocity_ = 550.0;
186 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
Austin Schuhde802e92016-02-27 14:49:03 -0800187 } else {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800188 wrist_goal_ = 0.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800189 shoulder_goal_ = -0.010;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800190 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000191 }
192
Austin Schuhde802e92016-02-27 14:49:03 -0800193 if (data.IsPressed(kTest3)) {
194 wrist_goal_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000195 }
196
Comran Morshedaa0573c2016-03-05 19:05:54 +0000197 bool ball_detected = false;
198 ::y2016::sensors::ball_detector.FetchLatest();
199 if (::y2016::sensors::ball_detector.get()) {
200 ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
201 }
202 if (data.PosEdge(kIntakeIn)) {
203 saw_ball_when_started_intaking_ = ball_detected;
204 }
205
Austin Schuh45d07f62016-03-13 15:33:31 -0700206 if (data.IsPressed(kIntakeIn)) {
207 is_intaking_ = (!ball_detected || saw_ball_when_started_intaking_);
208 if (ball_detected) {
209 force_lights_on = true;
210 }
211 } else {
212 is_intaking_ = false;
213 }
214
215 if (data.IsPressed(kIntakeDown)) {
216 if (is_intaking_) {
217 intake_goal_ = 0.1;
218 } else {
219 intake_goal_ = -0.05;
220 }
221 }
Austin Schuhde802e92016-02-27 14:49:03 -0800222
Austin Schuh4ea06c12016-03-12 17:54:31 -0800223 if (data.IsPressed(kFire) && shooter_velocity_ != 0.0) {
Austin Schuhde802e92016-02-27 14:49:03 -0800224 fire_ = true;
225 } else {
226 fire_ = false;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000227 }
228
229 if (data.PosEdge(kTest7)) {
230 }
231
Austin Schuh4ea06c12016-03-12 17:54:31 -0800232 is_outtaking_ = data.IsPressed(kIntakeOut);
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800233
Comran Morshed200dd4b2016-02-16 17:54:58 +0000234 if (!waiting_for_zero_) {
235 if (!action_queue_.Running()) {
236 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
237 new_superstructure_goal->angle_intake = intake_goal_;
238 new_superstructure_goal->angle_shoulder = shoulder_goal_;
239 new_superstructure_goal->angle_wrist = wrist_goal_;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800240
241 new_superstructure_goal->max_angular_velocity_intake = 7.0;
242 new_superstructure_goal->max_angular_velocity_shoulder = 4.0;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800243 new_superstructure_goal->max_angular_velocity_wrist = 10.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800244 new_superstructure_goal->max_angular_acceleration_intake = 40.0;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800245 new_superstructure_goal->max_angular_acceleration_shoulder = 10.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800246 new_superstructure_goal->max_angular_acceleration_wrist = 25.0;
247
Austin Schuh3f0b1192016-03-12 13:03:56 -0800248 // Granny mode
Austin Schuh4ea06c12016-03-12 17:54:31 -0800249 /*
Austin Schuh3f0b1192016-03-12 13:03:56 -0800250 new_superstructure_goal->max_angular_velocity_intake = 0.2;
251 new_superstructure_goal->max_angular_velocity_shoulder = 0.2;
252 new_superstructure_goal->max_angular_velocity_wrist = 0.2;
253 new_superstructure_goal->max_angular_acceleration_intake = 1.0;
254 new_superstructure_goal->max_angular_acceleration_shoulder = 1.0;
255 new_superstructure_goal->max_angular_acceleration_wrist = 1.0;
256 */
Austin Schuhde802e92016-02-27 14:49:03 -0800257 if (is_intaking_) {
258 new_superstructure_goal->voltage_top_rollers = 12.0;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800259 new_superstructure_goal->voltage_bottom_rollers = 12.0;
260 } else if (is_outtaking_) {
261 new_superstructure_goal->voltage_top_rollers = -12.0;
262 new_superstructure_goal->voltage_bottom_rollers = -7.0;
Austin Schuhde802e92016-02-27 14:49:03 -0800263 } else {
264 new_superstructure_goal->voltage_top_rollers = 0.0;
265 new_superstructure_goal->voltage_bottom_rollers = 0.0;
266 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000267
268 if (!new_superstructure_goal.Send()) {
269 LOG(ERROR, "Sending superstructure goal failed.\n");
270 } else {
271 LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
272 intake_goal_, shoulder_goal_, wrist_goal_);
273 }
274
275 if (!shooter_queue.goal.MakeWithBuilder()
Austin Schuhde802e92016-02-27 14:49:03 -0800276 .angular_velocity(shooter_velocity_)
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800277 .clamp_open(is_intaking_ || is_outtaking_)
Austin Schuhde802e92016-02-27 14:49:03 -0800278 .push_to_shooter(fire_)
Austin Schuh45d07f62016-03-13 15:33:31 -0700279 .force_lights_on(force_lights_on)
Comran Morshed200dd4b2016-02-16 17:54:58 +0000280 .Send()) {
281 LOG(ERROR, "Sending shooter goal failed.\n");
282 }
283 }
284 }
285
Comran Morshed9a9948c2016-01-16 15:58:04 +0000286 was_running_ = action_queue_.Running();
287 }
288
Comran Morshed9a9948c2016-01-16 15:58:04 +0000289 private:
290 void StartAuto() {
291 LOG(INFO, "Starting auto mode\n");
292 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
293 }
294
295 void StopAuto() {
296 LOG(INFO, "Stopping auto mode\n");
297 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
298 }
299
300 bool is_high_gear_;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000301 // Whatever these are set to are our default goals to send out after zeroing.
302 double intake_goal_;
303 double shoulder_goal_;
304 double wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800305 double shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000306
307 bool was_running_ = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000308 bool auto_running_ = false;
309
Comran Morshed200dd4b2016-02-16 17:54:58 +0000310 // If we're waiting for the subsystems to zero.
311 bool waiting_for_zero_ = true;
312
Comran Morshedaa0573c2016-03-05 19:05:54 +0000313 // If true, the ball was present when the intaking button was pressed.
314 bool saw_ball_when_started_intaking_ = false;
315
Austin Schuhde802e92016-02-27 14:49:03 -0800316 bool is_intaking_ = false;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800317 bool is_outtaking_ = false;
Austin Schuhde802e92016-02-27 14:49:03 -0800318 bool fire_ = false;
319
Comran Morshed9a9948c2016-01-16 15:58:04 +0000320 ::aos::common::actions::ActionQueue action_queue_;
321
322 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
323 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
324 "no drivetrain status");
325};
326
327} // namespace joysticks
328} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000329} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000330
331int main() {
332 ::aos::Init(-1);
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000333 ::y2016::input::joysticks::Reader reader;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000334 reader.Run();
335 ::aos::Cleanup();
336}