blob: 0e15eb0f2d6abe0b90b951038c990fbb1b8fb36c [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"
17
Comran Morshed6c6a0a92016-01-17 12:45:16 +000018#include "y2016/constants.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000019#include "frc971/queues/gyro.q.h"
20#include "frc971/autonomous/auto.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000021
22using ::frc971::control_loops::drivetrain_queue;
Comran Morshed200dd4b2016-02-16 17:54:58 +000023using ::y2016::control_loops::shooter::shooter_queue;
24using ::y2016::control_loops::superstructure_queue;
Comran Morshed9a9948c2016-01-16 15:58:04 +000025
26using ::aos::input::driver_station::ButtonLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000027using ::aos::input::driver_station::ControlBit;
Austin Schuh4ea06c12016-03-12 17:54:31 -080028using ::aos::input::driver_station::JoystickAxis;
29using ::aos::input::driver_station::POVLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000030
Comran Morshed6c6a0a92016-01-17 12:45:16 +000031namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000032namespace input {
33namespace joysticks {
34
Comran Morshed9a9948c2016-01-16 15:58:04 +000035const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080036const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
Comran Morshed9a9948c2016-01-16 15:58:04 +000037const ButtonLocation kQuickTurn(1, 5);
38
Austin Schuh781cdcc2016-03-12 13:03:12 -080039const ButtonLocation kTurn1(1, 7);
40const ButtonLocation kTurn2(1, 11);
41
Comran Morshed200dd4b2016-02-16 17:54:58 +000042// Buttons on the lexan driver station to get things running on bring-up day.
Austin Schuh4ea06c12016-03-12 17:54:31 -080043const ButtonLocation kIntakeDown(3, 11);
44const POVLocation kFrontLong(3, 180);
45const POVLocation kBackLong(3, 0);
46const ButtonLocation kTest3(3, 7);
47const ButtonLocation kIntakeIn(3, 12);
Comran Morshed200dd4b2016-02-16 17:54:58 +000048const ButtonLocation kTest5(3, 8);
Austin Schuh4ea06c12016-03-12 17:54:31 -080049const ButtonLocation kFire(3, 3);
Comran Morshed200dd4b2016-02-16 17:54:58 +000050const ButtonLocation kTest7(3, 5);
Austin Schuh4ea06c12016-03-12 17:54:31 -080051const ButtonLocation kIntakeOut(3, 9);
Comran Morshed200dd4b2016-02-16 17:54:58 +000052
Comran Morshed9a9948c2016-01-16 15:58:04 +000053class Reader : public ::aos::input::JoystickInput {
54 public:
55 Reader()
56 : is_high_gear_(false),
Comran Morshed200dd4b2016-02-16 17:54:58 +000057 intake_goal_(0.0),
58 shoulder_goal_(M_PI / 2.0),
59 wrist_goal_(0.0) {}
Comran Morshed9a9948c2016-01-16 15:58:04 +000060
61 void RunIteration(const ::aos::input::driver_station::Data &data) override {
62 bool last_auto_running = auto_running_;
63 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
64 data.GetControlBit(ControlBit::kEnabled);
65 if (auto_running_ != last_auto_running) {
66 if (auto_running_) {
67 StartAuto();
68 } else {
69 StopAuto();
70 }
71 }
72
73 if (!data.GetControlBit(ControlBit::kAutonomous)) {
74 HandleDrivetrain(data);
75 HandleTeleop(data);
76 }
77 }
78
79 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
80 bool is_control_loop_driving = false;
Austin Schuh781cdcc2016-03-12 13:03:12 -080081 static double left_goal = 0.0;
82 static double right_goal = 0.0;
83
Comran Morshed9a9948c2016-01-16 15:58:04 +000084 const double wheel = -data.GetAxis(kSteeringWheel);
85 const double throttle = -data.GetAxis(kDriveThrottle);
Comran Morshed9a9948c2016-01-16 15:58:04 +000086
Austin Schuh781cdcc2016-03-12 13:03:12 -080087 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
88 drivetrain_queue.status.FetchLatest();
89 if (drivetrain_queue.status.get()) {
90 const double delta = data.PosEdge(kTurn2) ? 0.1 : -0.1;
91 left_goal = drivetrain_queue.status->estimated_left_position + delta;
92 right_goal = drivetrain_queue.status->estimated_right_position - delta;
93 }
94 }
95 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
96 is_control_loop_driving = true;
97 }
Comran Morshed9a9948c2016-01-16 15:58:04 +000098 if (!drivetrain_queue.goal.MakeWithBuilder()
99 .steering(wheel)
100 .throttle(throttle)
101 .highgear(is_high_gear_)
102 .quickturn(data.IsPressed(kQuickTurn))
103 .control_loop_driving(is_control_loop_driving)
Austin Schuh781cdcc2016-03-12 13:03:12 -0800104 .left_goal(left_goal + wheel * 0.5 + throttle * 0.3)
105 .right_goal(right_goal - wheel * 0.5 + throttle * 0.3)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000106 .left_velocity_goal(0)
107 .right_velocity_goal(0)
108 .Send()) {
109 LOG(WARNING, "sending stick values failed\n");
110 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000111
Campbell Crowley5b27f022016-02-20 16:55:35 -0800112 if (data.PosEdge(kShiftLow)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000113 is_high_gear_ = false;
114 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000115
Campbell Crowley5b27f022016-02-20 16:55:35 -0800116 if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000117 is_high_gear_ = true;
118 }
119 }
120
Comran Morshed9a9948c2016-01-16 15:58:04 +0000121 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
122 if (!data.GetControlBit(ControlBit::kEnabled)) {
123 action_queue_.CancelAllActions();
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000124 LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +0000125 }
126
Comran Morshed200dd4b2016-02-16 17:54:58 +0000127 if (data.PosEdge(ControlBit::kEnabled)) {
128 // If we got enabled, wait for everything to zero.
129 LOG(INFO, "Waiting for zero.\n");
130 waiting_for_zero_ = true;
Austin Schuhde802e92016-02-27 14:49:03 -0800131 is_high_gear_ = true;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000132 }
133
134 superstructure_queue.status.FetchLatest();
135 if (!superstructure_queue.status.get()) {
136 LOG(ERROR, "Got no superstructure status packet.\n");
137 }
138
139 if (superstructure_queue.status.get() &&
140 superstructure_queue.status->zeroed) {
141 if (waiting_for_zero_) {
142 LOG(INFO, "Zeroed! Starting teleop mode.\n");
143 waiting_for_zero_ = false;
144 }
145 } else {
146 waiting_for_zero_ = true;
147 }
148
Austin Schuh4ea06c12016-03-12 17:54:31 -0800149 if (data.IsPressed(kIntakeDown)) {
Austin Schuh3f0b1192016-03-12 13:03:56 -0800150 intake_goal_ = 0.1;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800151 } else {
152 intake_goal_ = 1.6;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000153 }
154
Austin Schuh4ea06c12016-03-12 17:54:31 -0800155 if (data.IsPressed(kFrontLong)) {
156 // Forwards shot
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800157 shoulder_goal_ = M_PI / 2.0 - 0.2;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800158 wrist_goal_ = M_PI + 0.42;
159 shooter_velocity_ = 640.0;
160 } else if (data.IsPressed(kBackLong)) {
161 // Backwards shot
162 shoulder_goal_ = M_PI / 2.0 - 0.2;
163 wrist_goal_ = -0.59;
164 shooter_velocity_ = 640.0;
Austin Schuhde802e92016-02-27 14:49:03 -0800165 } else {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800166 wrist_goal_ = 0.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800167 shoulder_goal_ = -0.010;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800168 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000169 }
170
Austin Schuhde802e92016-02-27 14:49:03 -0800171 if (data.IsPressed(kTest3)) {
172 wrist_goal_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000173 }
174
Austin Schuh4ea06c12016-03-12 17:54:31 -0800175 is_intaking_ = data.IsPressed(kIntakeIn);
Austin Schuhde802e92016-02-27 14:49:03 -0800176
Austin Schuh4ea06c12016-03-12 17:54:31 -0800177 if (data.IsPressed(kFire) && shooter_velocity_ != 0.0) {
Austin Schuhde802e92016-02-27 14:49:03 -0800178 fire_ = true;
179 } else {
180 fire_ = false;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000181 }
182
183 if (data.PosEdge(kTest7)) {
184 }
185
Austin Schuh4ea06c12016-03-12 17:54:31 -0800186 is_outtaking_ = data.IsPressed(kIntakeOut);
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800187
Comran Morshed200dd4b2016-02-16 17:54:58 +0000188 if (!waiting_for_zero_) {
189 if (!action_queue_.Running()) {
190 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
191 new_superstructure_goal->angle_intake = intake_goal_;
192 new_superstructure_goal->angle_shoulder = shoulder_goal_;
193 new_superstructure_goal->angle_wrist = wrist_goal_;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800194
195 new_superstructure_goal->max_angular_velocity_intake = 7.0;
196 new_superstructure_goal->max_angular_velocity_shoulder = 4.0;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800197 new_superstructure_goal->max_angular_velocity_wrist = 10.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800198 new_superstructure_goal->max_angular_acceleration_intake = 40.0;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800199 new_superstructure_goal->max_angular_acceleration_shoulder = 10.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800200 new_superstructure_goal->max_angular_acceleration_wrist = 25.0;
201
Austin Schuh3f0b1192016-03-12 13:03:56 -0800202 // Granny mode
Austin Schuh4ea06c12016-03-12 17:54:31 -0800203 /*
Austin Schuh3f0b1192016-03-12 13:03:56 -0800204 new_superstructure_goal->max_angular_velocity_intake = 0.2;
205 new_superstructure_goal->max_angular_velocity_shoulder = 0.2;
206 new_superstructure_goal->max_angular_velocity_wrist = 0.2;
207 new_superstructure_goal->max_angular_acceleration_intake = 1.0;
208 new_superstructure_goal->max_angular_acceleration_shoulder = 1.0;
209 new_superstructure_goal->max_angular_acceleration_wrist = 1.0;
210 */
Austin Schuhde802e92016-02-27 14:49:03 -0800211 if (is_intaking_) {
212 new_superstructure_goal->voltage_top_rollers = 12.0;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800213 new_superstructure_goal->voltage_bottom_rollers = 12.0;
214 } else if (is_outtaking_) {
215 new_superstructure_goal->voltage_top_rollers = -12.0;
216 new_superstructure_goal->voltage_bottom_rollers = -7.0;
Austin Schuhde802e92016-02-27 14:49:03 -0800217 } else {
218 new_superstructure_goal->voltage_top_rollers = 0.0;
219 new_superstructure_goal->voltage_bottom_rollers = 0.0;
220 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000221
222 if (!new_superstructure_goal.Send()) {
223 LOG(ERROR, "Sending superstructure goal failed.\n");
224 } else {
225 LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
226 intake_goal_, shoulder_goal_, wrist_goal_);
227 }
228
229 if (!shooter_queue.goal.MakeWithBuilder()
Austin Schuhde802e92016-02-27 14:49:03 -0800230 .angular_velocity(shooter_velocity_)
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800231 .clamp_open(is_intaking_ || is_outtaking_)
Austin Schuhde802e92016-02-27 14:49:03 -0800232 .push_to_shooter(fire_)
Comran Morshed200dd4b2016-02-16 17:54:58 +0000233 .Send()) {
234 LOG(ERROR, "Sending shooter goal failed.\n");
235 }
236 }
237 }
238
Comran Morshed9a9948c2016-01-16 15:58:04 +0000239 was_running_ = action_queue_.Running();
240 }
241
Comran Morshed9a9948c2016-01-16 15:58:04 +0000242 private:
243 void StartAuto() {
244 LOG(INFO, "Starting auto mode\n");
245 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
246 }
247
248 void StopAuto() {
249 LOG(INFO, "Stopping auto mode\n");
250 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
251 }
252
253 bool is_high_gear_;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000254 // Whatever these are set to are our default goals to send out after zeroing.
255 double intake_goal_;
256 double shoulder_goal_;
257 double wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800258 double shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000259
260 bool was_running_ = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000261 bool auto_running_ = false;
262
Comran Morshed200dd4b2016-02-16 17:54:58 +0000263 // If we're waiting for the subsystems to zero.
264 bool waiting_for_zero_ = true;
265
Austin Schuhde802e92016-02-27 14:49:03 -0800266 bool is_intaking_ = false;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800267 bool is_outtaking_ = false;
Austin Schuhde802e92016-02-27 14:49:03 -0800268 bool fire_ = false;
269
Comran Morshed9a9948c2016-01-16 15:58:04 +0000270 ::aos::common::actions::ActionQueue action_queue_;
271
272 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
273 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
274 "no drivetrain status");
275};
276
277} // namespace joysticks
278} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000279} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000280
281int main() {
282 ::aos::Init(-1);
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000283 ::y2016::input::joysticks::Reader reader;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000284 reader.Run();
285 ::aos::Cleanup();
286}