blob: f31b708941238fadd8541701a92d35b04774892a [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"
Comran Morshedaa0573c2016-03-05 19:05:54 +000017#include "y2016/queues/ball_detector.q.h"
Comran Morshed200dd4b2016-02-16 17:54:58 +000018
Comran Morshed6c6a0a92016-01-17 12:45:16 +000019#include "y2016/constants.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000020#include "frc971/queues/gyro.q.h"
21#include "frc971/autonomous/auto.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000022
23using ::frc971::control_loops::drivetrain_queue;
Comran Morshed200dd4b2016-02-16 17:54:58 +000024using ::y2016::control_loops::shooter::shooter_queue;
25using ::y2016::control_loops::superstructure_queue;
Comran Morshed9a9948c2016-01-16 15:58:04 +000026
27using ::aos::input::driver_station::ButtonLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000028using ::aos::input::driver_station::ControlBit;
Austin Schuh4ea06c12016-03-12 17:54:31 -080029using ::aos::input::driver_station::JoystickAxis;
30using ::aos::input::driver_station::POVLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000031
Comran Morshed6c6a0a92016-01-17 12:45:16 +000032namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000033namespace input {
34namespace joysticks {
35
Comran Morshed9a9948c2016-01-16 15:58:04 +000036const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080037const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
Comran Morshed9a9948c2016-01-16 15:58:04 +000038const ButtonLocation kQuickTurn(1, 5);
39
Austin Schuh781cdcc2016-03-12 13:03:12 -080040const ButtonLocation kTurn1(1, 7);
41const ButtonLocation kTurn2(1, 11);
42
Comran Morshed200dd4b2016-02-16 17:54:58 +000043// Buttons on the lexan driver station to get things running on bring-up day.
Austin Schuh4ea06c12016-03-12 17:54:31 -080044const ButtonLocation kIntakeDown(3, 11);
45const POVLocation kFrontLong(3, 180);
46const POVLocation kBackLong(3, 0);
47const ButtonLocation kTest3(3, 7);
48const ButtonLocation kIntakeIn(3, 12);
Comran Morshed200dd4b2016-02-16 17:54:58 +000049const ButtonLocation kTest5(3, 8);
Austin Schuh4ea06c12016-03-12 17:54:31 -080050const ButtonLocation kFire(3, 3);
Comran Morshed200dd4b2016-02-16 17:54:58 +000051const ButtonLocation kTest7(3, 5);
Austin Schuh4ea06c12016-03-12 17:54:31 -080052const ButtonLocation kIntakeOut(3, 9);
Comran Morshed200dd4b2016-02-16 17:54:58 +000053
Comran Morshed9a9948c2016-01-16 15:58:04 +000054class Reader : public ::aos::input::JoystickInput {
55 public:
56 Reader()
57 : is_high_gear_(false),
Comran Morshed200dd4b2016-02-16 17:54:58 +000058 intake_goal_(0.0),
59 shoulder_goal_(M_PI / 2.0),
60 wrist_goal_(0.0) {}
Comran Morshed9a9948c2016-01-16 15:58:04 +000061
62 void RunIteration(const ::aos::input::driver_station::Data &data) override {
63 bool last_auto_running = auto_running_;
64 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
65 data.GetControlBit(ControlBit::kEnabled);
66 if (auto_running_ != last_auto_running) {
67 if (auto_running_) {
68 StartAuto();
69 } else {
70 StopAuto();
71 }
72 }
73
74 if (!data.GetControlBit(ControlBit::kAutonomous)) {
75 HandleDrivetrain(data);
76 HandleTeleop(data);
77 }
78 }
79
80 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
81 bool is_control_loop_driving = false;
Austin Schuh781cdcc2016-03-12 13:03:12 -080082 static double left_goal = 0.0;
83 static double right_goal = 0.0;
84
Comran Morshed9a9948c2016-01-16 15:58:04 +000085 const double wheel = -data.GetAxis(kSteeringWheel);
86 const double throttle = -data.GetAxis(kDriveThrottle);
Comran Morshed9a9948c2016-01-16 15:58:04 +000087
Austin Schuh781cdcc2016-03-12 13:03:12 -080088 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
89 drivetrain_queue.status.FetchLatest();
90 if (drivetrain_queue.status.get()) {
91 const double delta = data.PosEdge(kTurn2) ? 0.1 : -0.1;
92 left_goal = drivetrain_queue.status->estimated_left_position + delta;
93 right_goal = drivetrain_queue.status->estimated_right_position - delta;
94 }
95 }
96 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
97 is_control_loop_driving = true;
98 }
Comran Morshed9a9948c2016-01-16 15:58:04 +000099 if (!drivetrain_queue.goal.MakeWithBuilder()
100 .steering(wheel)
101 .throttle(throttle)
102 .highgear(is_high_gear_)
103 .quickturn(data.IsPressed(kQuickTurn))
104 .control_loop_driving(is_control_loop_driving)
Austin Schuh781cdcc2016-03-12 13:03:12 -0800105 .left_goal(left_goal + wheel * 0.5 + throttle * 0.3)
106 .right_goal(right_goal - wheel * 0.5 + throttle * 0.3)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000107 .left_velocity_goal(0)
108 .right_velocity_goal(0)
109 .Send()) {
110 LOG(WARNING, "sending stick values failed\n");
111 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000112
Campbell Crowley5b27f022016-02-20 16:55:35 -0800113 if (data.PosEdge(kShiftLow)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000114 is_high_gear_ = false;
115 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000116
Campbell Crowley5b27f022016-02-20 16:55:35 -0800117 if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000118 is_high_gear_ = true;
119 }
120 }
121
Comran Morshed9a9948c2016-01-16 15:58:04 +0000122 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
123 if (!data.GetControlBit(ControlBit::kEnabled)) {
124 action_queue_.CancelAllActions();
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000125 LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +0000126 }
127
Comran Morshed200dd4b2016-02-16 17:54:58 +0000128 if (data.PosEdge(ControlBit::kEnabled)) {
129 // If we got enabled, wait for everything to zero.
130 LOG(INFO, "Waiting for zero.\n");
131 waiting_for_zero_ = true;
Austin Schuhde802e92016-02-27 14:49:03 -0800132 is_high_gear_ = true;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000133 }
134
135 superstructure_queue.status.FetchLatest();
136 if (!superstructure_queue.status.get()) {
137 LOG(ERROR, "Got no superstructure status packet.\n");
138 }
139
140 if (superstructure_queue.status.get() &&
141 superstructure_queue.status->zeroed) {
142 if (waiting_for_zero_) {
143 LOG(INFO, "Zeroed! Starting teleop mode.\n");
144 waiting_for_zero_ = false;
145 }
146 } else {
147 waiting_for_zero_ = true;
148 }
149
Austin Schuh4ea06c12016-03-12 17:54:31 -0800150 if (data.IsPressed(kIntakeDown)) {
Austin Schuh3f0b1192016-03-12 13:03:56 -0800151 intake_goal_ = 0.1;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800152 } else {
153 intake_goal_ = 1.6;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000154 }
155
Austin Schuh4ea06c12016-03-12 17:54:31 -0800156 if (data.IsPressed(kFrontLong)) {
157 // Forwards shot
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800158 shoulder_goal_ = M_PI / 2.0 - 0.2;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800159 wrist_goal_ = M_PI + 0.42;
160 shooter_velocity_ = 640.0;
161 } else if (data.IsPressed(kBackLong)) {
162 // Backwards shot
163 shoulder_goal_ = M_PI / 2.0 - 0.2;
164 wrist_goal_ = -0.59;
165 shooter_velocity_ = 640.0;
Austin Schuhde802e92016-02-27 14:49:03 -0800166 } else {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800167 wrist_goal_ = 0.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800168 shoulder_goal_ = -0.010;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800169 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000170 }
171
Austin Schuhde802e92016-02-27 14:49:03 -0800172 if (data.IsPressed(kTest3)) {
173 wrist_goal_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000174 }
175
Comran Morshedaa0573c2016-03-05 19:05:54 +0000176 bool ball_detected = false;
177 ::y2016::sensors::ball_detector.FetchLatest();
178 if (::y2016::sensors::ball_detector.get()) {
179 ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
180 }
181 if (data.PosEdge(kIntakeIn)) {
182 saw_ball_when_started_intaking_ = ball_detected;
183 }
184
185 is_intaking_ = data.IsPressed(kIntakeIn) &&
186 (!ball_detected || saw_ball_when_started_intaking_);
Austin Schuhde802e92016-02-27 14:49:03 -0800187
Austin Schuh4ea06c12016-03-12 17:54:31 -0800188 if (data.IsPressed(kFire) && shooter_velocity_ != 0.0) {
Austin Schuhde802e92016-02-27 14:49:03 -0800189 fire_ = true;
190 } else {
191 fire_ = false;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000192 }
193
194 if (data.PosEdge(kTest7)) {
195 }
196
Austin Schuh4ea06c12016-03-12 17:54:31 -0800197 is_outtaking_ = data.IsPressed(kIntakeOut);
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800198
Comran Morshed200dd4b2016-02-16 17:54:58 +0000199 if (!waiting_for_zero_) {
200 if (!action_queue_.Running()) {
201 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
202 new_superstructure_goal->angle_intake = intake_goal_;
203 new_superstructure_goal->angle_shoulder = shoulder_goal_;
204 new_superstructure_goal->angle_wrist = wrist_goal_;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800205
206 new_superstructure_goal->max_angular_velocity_intake = 7.0;
207 new_superstructure_goal->max_angular_velocity_shoulder = 4.0;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800208 new_superstructure_goal->max_angular_velocity_wrist = 10.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800209 new_superstructure_goal->max_angular_acceleration_intake = 40.0;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800210 new_superstructure_goal->max_angular_acceleration_shoulder = 10.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800211 new_superstructure_goal->max_angular_acceleration_wrist = 25.0;
212
Austin Schuh3f0b1192016-03-12 13:03:56 -0800213 // Granny mode
Austin Schuh4ea06c12016-03-12 17:54:31 -0800214 /*
Austin Schuh3f0b1192016-03-12 13:03:56 -0800215 new_superstructure_goal->max_angular_velocity_intake = 0.2;
216 new_superstructure_goal->max_angular_velocity_shoulder = 0.2;
217 new_superstructure_goal->max_angular_velocity_wrist = 0.2;
218 new_superstructure_goal->max_angular_acceleration_intake = 1.0;
219 new_superstructure_goal->max_angular_acceleration_shoulder = 1.0;
220 new_superstructure_goal->max_angular_acceleration_wrist = 1.0;
221 */
Austin Schuhde802e92016-02-27 14:49:03 -0800222 if (is_intaking_) {
223 new_superstructure_goal->voltage_top_rollers = 12.0;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800224 new_superstructure_goal->voltage_bottom_rollers = 12.0;
225 } else if (is_outtaking_) {
226 new_superstructure_goal->voltage_top_rollers = -12.0;
227 new_superstructure_goal->voltage_bottom_rollers = -7.0;
Austin Schuhde802e92016-02-27 14:49:03 -0800228 } else {
229 new_superstructure_goal->voltage_top_rollers = 0.0;
230 new_superstructure_goal->voltage_bottom_rollers = 0.0;
231 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000232
233 if (!new_superstructure_goal.Send()) {
234 LOG(ERROR, "Sending superstructure goal failed.\n");
235 } else {
236 LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
237 intake_goal_, shoulder_goal_, wrist_goal_);
238 }
239
240 if (!shooter_queue.goal.MakeWithBuilder()
Austin Schuhde802e92016-02-27 14:49:03 -0800241 .angular_velocity(shooter_velocity_)
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800242 .clamp_open(is_intaking_ || is_outtaking_)
Austin Schuhde802e92016-02-27 14:49:03 -0800243 .push_to_shooter(fire_)
Comran Morshed200dd4b2016-02-16 17:54:58 +0000244 .Send()) {
245 LOG(ERROR, "Sending shooter goal failed.\n");
246 }
247 }
248 }
249
Comran Morshed9a9948c2016-01-16 15:58:04 +0000250 was_running_ = action_queue_.Running();
251 }
252
Comran Morshed9a9948c2016-01-16 15:58:04 +0000253 private:
254 void StartAuto() {
255 LOG(INFO, "Starting auto mode\n");
256 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
257 }
258
259 void StopAuto() {
260 LOG(INFO, "Stopping auto mode\n");
261 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
262 }
263
264 bool is_high_gear_;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000265 // Whatever these are set to are our default goals to send out after zeroing.
266 double intake_goal_;
267 double shoulder_goal_;
268 double wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800269 double shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000270
271 bool was_running_ = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000272 bool auto_running_ = false;
273
Comran Morshed200dd4b2016-02-16 17:54:58 +0000274 // If we're waiting for the subsystems to zero.
275 bool waiting_for_zero_ = true;
276
Comran Morshedaa0573c2016-03-05 19:05:54 +0000277 // If true, the ball was present when the intaking button was pressed.
278 bool saw_ball_when_started_intaking_ = false;
279
Austin Schuhde802e92016-02-27 14:49:03 -0800280 bool is_intaking_ = false;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800281 bool is_outtaking_ = false;
Austin Schuhde802e92016-02-27 14:49:03 -0800282 bool fire_ = false;
283
Comran Morshed9a9948c2016-01-16 15:58:04 +0000284 ::aos::common::actions::ActionQueue action_queue_;
285
286 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
287 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
288 "no drivetrain status");
289};
290
291} // namespace joysticks
292} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000293} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000294
295int main() {
296 ::aos::Init(-1);
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000297 ::y2016::input::joysticks::Reader reader;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000298 reader.Run();
299 ::aos::Cleanup();
300}