blob: d987268c29c122da55281b8ec977d4b3edcac01e [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()
68 : is_high_gear_(false),
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
85 if (!data.GetControlBit(ControlBit::kAutonomous)) {
86 HandleDrivetrain(data);
87 HandleTeleop(data);
88 }
89 }
90
91 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
92 bool is_control_loop_driving = false;
Austin Schuh781cdcc2016-03-12 13:03:12 -080093 static double left_goal = 0.0;
94 static double right_goal = 0.0;
95
Comran Morshed9a9948c2016-01-16 15:58:04 +000096 const double wheel = -data.GetAxis(kSteeringWheel);
97 const double throttle = -data.GetAxis(kDriveThrottle);
Comran Morshed9a9948c2016-01-16 15:58:04 +000098
Austin Schuh781cdcc2016-03-12 13:03:12 -080099 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
100 drivetrain_queue.status.FetchLatest();
101 if (drivetrain_queue.status.get()) {
Austin Schuh45d07f62016-03-13 15:33:31 -0700102 left_goal = drivetrain_queue.status->estimated_left_position;
103 right_goal = drivetrain_queue.status->estimated_right_position;
Austin Schuh781cdcc2016-03-12 13:03:12 -0800104 }
105 }
106 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
107 is_control_loop_driving = true;
108 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000109 if (!drivetrain_queue.goal.MakeWithBuilder()
110 .steering(wheel)
111 .throttle(throttle)
112 .highgear(is_high_gear_)
113 .quickturn(data.IsPressed(kQuickTurn))
114 .control_loop_driving(is_control_loop_driving)
Austin Schuh45d07f62016-03-13 15:33:31 -0700115 .left_goal(left_goal - wheel * 0.5 + throttle * 0.3)
116 .right_goal(right_goal + wheel * 0.5 + throttle * 0.3)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000117 .left_velocity_goal(0)
118 .right_velocity_goal(0)
119 .Send()) {
120 LOG(WARNING, "sending stick values failed\n");
121 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000122
Campbell Crowley5b27f022016-02-20 16:55:35 -0800123 if (data.PosEdge(kShiftLow)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000124 is_high_gear_ = false;
125 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000126
Campbell Crowley5b27f022016-02-20 16:55:35 -0800127 if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000128 is_high_gear_ = true;
129 }
130 }
131
Comran Morshed9a9948c2016-01-16 15:58:04 +0000132 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuh45d07f62016-03-13 15:33:31 -0700133 // Default the intake to up.
134 intake_goal_ = constants::Values::kIntakeRange.upper - 0.04;
135
136 bool force_lights_on = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000137 if (!data.GetControlBit(ControlBit::kEnabled)) {
138 action_queue_.CancelAllActions();
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000139 LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +0000140 }
141
Comran Morshed200dd4b2016-02-16 17:54:58 +0000142 if (data.PosEdge(ControlBit::kEnabled)) {
143 // If we got enabled, wait for everything to zero.
144 LOG(INFO, "Waiting for zero.\n");
145 waiting_for_zero_ = true;
Austin Schuhde802e92016-02-27 14:49:03 -0800146 is_high_gear_ = true;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000147 }
148
149 superstructure_queue.status.FetchLatest();
150 if (!superstructure_queue.status.get()) {
151 LOG(ERROR, "Got no superstructure status packet.\n");
152 }
153
154 if (superstructure_queue.status.get() &&
155 superstructure_queue.status->zeroed) {
156 if (waiting_for_zero_) {
157 LOG(INFO, "Zeroed! Starting teleop mode.\n");
158 waiting_for_zero_ = false;
159 }
160 } else {
161 waiting_for_zero_ = true;
162 }
163
Austin Schuh4ea06c12016-03-12 17:54:31 -0800164 if (data.IsPressed(kFrontLong)) {
165 // Forwards shot
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800166 shoulder_goal_ = M_PI / 2.0 - 0.2;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800167 wrist_goal_ = M_PI + 0.42;
168 shooter_velocity_ = 640.0;
Austin Schuh45d07f62016-03-13 15:33:31 -0700169 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800170 } else if (data.IsPressed(kBackLong)) {
171 // Backwards shot
172 shoulder_goal_ = M_PI / 2.0 - 0.2;
173 wrist_goal_ = -0.59;
174 shooter_velocity_ = 640.0;
Austin Schuh45d07f62016-03-13 15:33:31 -0700175 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
176 } else if (data.IsPressed(kBackFender)) {
177 // Fender shot back
178 shoulder_goal_ = 0.65;
179 wrist_goal_ = -1.0;
180 shooter_velocity_ = 550.0;
181 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
182 } else if (data.IsPressed(kFrontFender)) {
183 // Fender shot back
184 shoulder_goal_ = 1.45;
185 wrist_goal_ = 2.5 + 1.7;
186 shooter_velocity_ = 550.0;
187 intake_goal_ = kMaxIntakeAngleBeforeArmInterference;
Austin Schuhde802e92016-02-27 14:49:03 -0800188 } else {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800189 wrist_goal_ = 0.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800190 shoulder_goal_ = -0.010;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800191 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000192 }
193
Austin Schuhde802e92016-02-27 14:49:03 -0800194 if (data.IsPressed(kTest3)) {
195 wrist_goal_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000196 }
197
Comran Morshedaa0573c2016-03-05 19:05:54 +0000198 bool ball_detected = false;
199 ::y2016::sensors::ball_detector.FetchLatest();
200 if (::y2016::sensors::ball_detector.get()) {
201 ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
202 }
203 if (data.PosEdge(kIntakeIn)) {
204 saw_ball_when_started_intaking_ = ball_detected;
205 }
206
Austin Schuh45d07f62016-03-13 15:33:31 -0700207 if (data.IsPressed(kIntakeIn)) {
208 is_intaking_ = (!ball_detected || saw_ball_when_started_intaking_);
209 if (ball_detected) {
210 force_lights_on = true;
211 }
212 } else {
213 is_intaking_ = false;
214 }
215
216 if (data.IsPressed(kIntakeDown)) {
217 if (is_intaking_) {
218 intake_goal_ = 0.1;
219 } else {
220 intake_goal_ = -0.05;
221 }
222 }
Austin Schuhde802e92016-02-27 14:49:03 -0800223
Austin Schuh4ea06c12016-03-12 17:54:31 -0800224 if (data.IsPressed(kFire) && shooter_velocity_ != 0.0) {
Austin Schuhde802e92016-02-27 14:49:03 -0800225 fire_ = true;
226 } else {
227 fire_ = false;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000228 }
229
230 if (data.PosEdge(kTest7)) {
231 }
232
Austin Schuh4ea06c12016-03-12 17:54:31 -0800233 is_outtaking_ = data.IsPressed(kIntakeOut);
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800234
Comran Morshed200dd4b2016-02-16 17:54:58 +0000235 if (!waiting_for_zero_) {
236 if (!action_queue_.Running()) {
237 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
238 new_superstructure_goal->angle_intake = intake_goal_;
239 new_superstructure_goal->angle_shoulder = shoulder_goal_;
240 new_superstructure_goal->angle_wrist = wrist_goal_;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800241
242 new_superstructure_goal->max_angular_velocity_intake = 7.0;
243 new_superstructure_goal->max_angular_velocity_shoulder = 4.0;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800244 new_superstructure_goal->max_angular_velocity_wrist = 10.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800245 new_superstructure_goal->max_angular_acceleration_intake = 40.0;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800246 new_superstructure_goal->max_angular_acceleration_shoulder = 10.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800247 new_superstructure_goal->max_angular_acceleration_wrist = 25.0;
248
Austin Schuh3f0b1192016-03-12 13:03:56 -0800249 // Granny mode
Austin Schuh4ea06c12016-03-12 17:54:31 -0800250 /*
Austin Schuh3f0b1192016-03-12 13:03:56 -0800251 new_superstructure_goal->max_angular_velocity_intake = 0.2;
252 new_superstructure_goal->max_angular_velocity_shoulder = 0.2;
253 new_superstructure_goal->max_angular_velocity_wrist = 0.2;
254 new_superstructure_goal->max_angular_acceleration_intake = 1.0;
255 new_superstructure_goal->max_angular_acceleration_shoulder = 1.0;
256 new_superstructure_goal->max_angular_acceleration_wrist = 1.0;
257 */
Austin Schuhde802e92016-02-27 14:49:03 -0800258 if (is_intaking_) {
259 new_superstructure_goal->voltage_top_rollers = 12.0;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800260 new_superstructure_goal->voltage_bottom_rollers = 12.0;
261 } else if (is_outtaking_) {
262 new_superstructure_goal->voltage_top_rollers = -12.0;
263 new_superstructure_goal->voltage_bottom_rollers = -7.0;
Austin Schuhde802e92016-02-27 14:49:03 -0800264 } else {
265 new_superstructure_goal->voltage_top_rollers = 0.0;
266 new_superstructure_goal->voltage_bottom_rollers = 0.0;
267 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000268
269 if (!new_superstructure_goal.Send()) {
270 LOG(ERROR, "Sending superstructure goal failed.\n");
271 } else {
272 LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
273 intake_goal_, shoulder_goal_, wrist_goal_);
274 }
275
276 if (!shooter_queue.goal.MakeWithBuilder()
Austin Schuhde802e92016-02-27 14:49:03 -0800277 .angular_velocity(shooter_velocity_)
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800278 .clamp_open(is_intaking_ || is_outtaking_)
Austin Schuhde802e92016-02-27 14:49:03 -0800279 .push_to_shooter(fire_)
Austin Schuh45d07f62016-03-13 15:33:31 -0700280 .force_lights_on(force_lights_on)
Comran Morshed200dd4b2016-02-16 17:54:58 +0000281 .Send()) {
282 LOG(ERROR, "Sending shooter goal failed.\n");
283 }
284 }
285 }
286
Comran Morshed9a9948c2016-01-16 15:58:04 +0000287 was_running_ = action_queue_.Running();
288 }
289
Comran Morshed9a9948c2016-01-16 15:58:04 +0000290 private:
291 void StartAuto() {
292 LOG(INFO, "Starting auto mode\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000293
294 actors::AutonomousActionParams params;
295 params.mode = 0;
296 action_queue_.EnqueueAction(actors::MakeAutonomousAction(params));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000297 }
298
299 void StopAuto() {
300 LOG(INFO, "Stopping auto mode\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000301 action_queue_.CancelAllActions();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000302 }
303
304 bool is_high_gear_;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000305 // Whatever these are set to are our default goals to send out after zeroing.
306 double intake_goal_;
307 double shoulder_goal_;
308 double wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800309 double shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000310
311 bool was_running_ = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000312 bool auto_running_ = false;
313
Comran Morshed200dd4b2016-02-16 17:54:58 +0000314 // If we're waiting for the subsystems to zero.
315 bool waiting_for_zero_ = true;
316
Comran Morshedaa0573c2016-03-05 19:05:54 +0000317 // If true, the ball was present when the intaking button was pressed.
318 bool saw_ball_when_started_intaking_ = false;
319
Austin Schuhde802e92016-02-27 14:49:03 -0800320 bool is_intaking_ = false;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800321 bool is_outtaking_ = false;
Austin Schuhde802e92016-02-27 14:49:03 -0800322 bool fire_ = false;
323
Comran Morshed9a9948c2016-01-16 15:58:04 +0000324 ::aos::common::actions::ActionQueue action_queue_;
325
326 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
327 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
328 "no drivetrain status");
329};
330
331} // namespace joysticks
332} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000333} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000334
335int main() {
336 ::aos::Init(-1);
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000337 ::y2016::input::joysticks::Reader reader;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000338 reader.Run();
339 ::aos::Cleanup();
340}