blob: a6e6c9339cf5cc0f4f3dec66390aa7027cb3f28d [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;
27using ::aos::input::driver_station::JoystickAxis;
28using ::aos::input::driver_station::ControlBit;
29
Comran Morshed6c6a0a92016-01-17 12:45:16 +000030namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000031namespace input {
32namespace joysticks {
33
Comran Morshed9a9948c2016-01-16 15:58:04 +000034const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080035const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
Comran Morshed9a9948c2016-01-16 15:58:04 +000036const ButtonLocation kQuickTurn(1, 5);
37
Austin Schuh781cdcc2016-03-12 13:03:12 -080038const ButtonLocation kTurn1(1, 7);
39const ButtonLocation kTurn2(1, 11);
40
Comran Morshed200dd4b2016-02-16 17:54:58 +000041// Buttons on the lexan driver station to get things running on bring-up day.
42const ButtonLocation kTest1(3, 6);
43const ButtonLocation kTest2(3, 2);
44const ButtonLocation kTest3(3, 11);
45const ButtonLocation kTest4(3, 9);
46const ButtonLocation kTest5(3, 8);
47const ButtonLocation kTest6(3, 3);
48const ButtonLocation kTest7(3, 5);
49const ButtonLocation kTest8(3, 4);
50
Comran Morshed9a9948c2016-01-16 15:58:04 +000051class Reader : public ::aos::input::JoystickInput {
52 public:
53 Reader()
54 : is_high_gear_(false),
Comran Morshed200dd4b2016-02-16 17:54:58 +000055 intake_goal_(0.0),
56 shoulder_goal_(M_PI / 2.0),
57 wrist_goal_(0.0) {}
Comran Morshed9a9948c2016-01-16 15:58:04 +000058
59 void RunIteration(const ::aos::input::driver_station::Data &data) override {
60 bool last_auto_running = auto_running_;
61 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
62 data.GetControlBit(ControlBit::kEnabled);
63 if (auto_running_ != last_auto_running) {
64 if (auto_running_) {
65 StartAuto();
66 } else {
67 StopAuto();
68 }
69 }
70
71 if (!data.GetControlBit(ControlBit::kAutonomous)) {
72 HandleDrivetrain(data);
73 HandleTeleop(data);
74 }
75 }
76
77 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
78 bool is_control_loop_driving = false;
Austin Schuh781cdcc2016-03-12 13:03:12 -080079 static double left_goal = 0.0;
80 static double right_goal = 0.0;
81
Comran Morshed9a9948c2016-01-16 15:58:04 +000082 const double wheel = -data.GetAxis(kSteeringWheel);
83 const double throttle = -data.GetAxis(kDriveThrottle);
Comran Morshed9a9948c2016-01-16 15:58:04 +000084
Austin Schuh781cdcc2016-03-12 13:03:12 -080085 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
86 drivetrain_queue.status.FetchLatest();
87 if (drivetrain_queue.status.get()) {
88 const double delta = data.PosEdge(kTurn2) ? 0.1 : -0.1;
89 left_goal = drivetrain_queue.status->estimated_left_position + delta;
90 right_goal = drivetrain_queue.status->estimated_right_position - delta;
91 }
92 }
93 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
94 is_control_loop_driving = true;
95 }
Comran Morshed9a9948c2016-01-16 15:58:04 +000096 if (!drivetrain_queue.goal.MakeWithBuilder()
97 .steering(wheel)
98 .throttle(throttle)
99 .highgear(is_high_gear_)
100 .quickturn(data.IsPressed(kQuickTurn))
101 .control_loop_driving(is_control_loop_driving)
Austin Schuh781cdcc2016-03-12 13:03:12 -0800102 .left_goal(left_goal + wheel * 0.5 + throttle * 0.3)
103 .right_goal(right_goal - wheel * 0.5 + throttle * 0.3)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000104 .left_velocity_goal(0)
105 .right_velocity_goal(0)
106 .Send()) {
107 LOG(WARNING, "sending stick values failed\n");
108 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000109
Campbell Crowley5b27f022016-02-20 16:55:35 -0800110 if (data.PosEdge(kShiftLow)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000111 is_high_gear_ = false;
112 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000113
Campbell Crowley5b27f022016-02-20 16:55:35 -0800114 if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000115 is_high_gear_ = true;
116 }
117 }
118
Comran Morshed9a9948c2016-01-16 15:58:04 +0000119 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
120 if (!data.GetControlBit(ControlBit::kEnabled)) {
121 action_queue_.CancelAllActions();
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000122 LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +0000123 }
124
Comran Morshed200dd4b2016-02-16 17:54:58 +0000125 if (data.PosEdge(ControlBit::kEnabled)) {
126 // If we got enabled, wait for everything to zero.
127 LOG(INFO, "Waiting for zero.\n");
128 waiting_for_zero_ = true;
Austin Schuhde802e92016-02-27 14:49:03 -0800129 is_high_gear_ = true;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000130 }
131
132 superstructure_queue.status.FetchLatest();
133 if (!superstructure_queue.status.get()) {
134 LOG(ERROR, "Got no superstructure status packet.\n");
135 }
136
137 if (superstructure_queue.status.get() &&
138 superstructure_queue.status->zeroed) {
139 if (waiting_for_zero_) {
140 LOG(INFO, "Zeroed! Starting teleop mode.\n");
141 waiting_for_zero_ = false;
142 }
143 } else {
144 waiting_for_zero_ = true;
145 }
146
Austin Schuhde802e92016-02-27 14:49:03 -0800147 if (data.IsPressed(kTest1)) {
Austin Schuhde802e92016-02-27 14:49:03 -0800148 intake_goal_ = 1.6;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800149 } else {
150 intake_goal_ = 0.1;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000151 }
152
Austin Schuhde802e92016-02-27 14:49:03 -0800153 if (data.IsPressed(kTest2)) {
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800154 shoulder_goal_ = M_PI / 2.0 - 0.2;
Austin Schuhde802e92016-02-27 14:49:03 -0800155 } else {
Austin Schuh3f0b1192016-03-12 13:03:56 -0800156 shoulder_goal_ = -0.010;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000157 }
158
Austin Schuhde802e92016-02-27 14:49:03 -0800159 if (data.IsPressed(kTest3)) {
160 wrist_goal_ = 0.0;
161 } else {
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800162 // Backwards shot
Austin Schuh3f0b1192016-03-12 13:03:56 -0800163 wrist_goal_ = -0.59;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800164 // Forwards shot
Austin Schuh3f0b1192016-03-12 13:03:56 -0800165 //wrist_goal_ = M_PI + 0.42;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000166 }
167
Austin Schuhde802e92016-02-27 14:49:03 -0800168 is_intaking_ = data.IsPressed(kTest4);
169
170 if (data.IsPressed(kTest5)) {
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800171 //shooter_velocity_ = 600.0;
172 shooter_velocity_ = 640.0;
Austin Schuhde802e92016-02-27 14:49:03 -0800173 } else {
174 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000175 }
176
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800177 if (data.IsPressed(kTest6) && 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
186 if (data.PosEdge(kTest8)) {
187 }
188
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800189 is_outtaking_ = data.IsPressed(kTest8);
190
Comran Morshed200dd4b2016-02-16 17:54:58 +0000191 if (!waiting_for_zero_) {
192 if (!action_queue_.Running()) {
193 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
194 new_superstructure_goal->angle_intake = intake_goal_;
195 new_superstructure_goal->angle_shoulder = shoulder_goal_;
196 new_superstructure_goal->angle_wrist = wrist_goal_;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800197
198 new_superstructure_goal->max_angular_velocity_intake = 7.0;
199 new_superstructure_goal->max_angular_velocity_shoulder = 4.0;
200 new_superstructure_goal->max_angular_velocity_wrist = 11.0;
201 new_superstructure_goal->max_angular_acceleration_intake = 40.0;
202 new_superstructure_goal->max_angular_acceleration_shoulder = 8.0;
203 new_superstructure_goal->max_angular_acceleration_wrist = 25.0;
204
205 /*
206 // Granny mode
207 new_superstructure_goal->max_angular_velocity_intake = 0.2;
208 new_superstructure_goal->max_angular_velocity_shoulder = 0.2;
209 new_superstructure_goal->max_angular_velocity_wrist = 0.2;
210 new_superstructure_goal->max_angular_acceleration_intake = 1.0;
211 new_superstructure_goal->max_angular_acceleration_shoulder = 1.0;
212 new_superstructure_goal->max_angular_acceleration_wrist = 1.0;
213 */
Austin Schuhde802e92016-02-27 14:49:03 -0800214 if (is_intaking_) {
215 new_superstructure_goal->voltage_top_rollers = 12.0;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800216 new_superstructure_goal->voltage_bottom_rollers = 12.0;
217 } else if (is_outtaking_) {
218 new_superstructure_goal->voltage_top_rollers = -12.0;
219 new_superstructure_goal->voltage_bottom_rollers = -7.0;
Austin Schuhde802e92016-02-27 14:49:03 -0800220 } else {
221 new_superstructure_goal->voltage_top_rollers = 0.0;
222 new_superstructure_goal->voltage_bottom_rollers = 0.0;
223 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000224
225 if (!new_superstructure_goal.Send()) {
226 LOG(ERROR, "Sending superstructure goal failed.\n");
227 } else {
228 LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
229 intake_goal_, shoulder_goal_, wrist_goal_);
230 }
231
232 if (!shooter_queue.goal.MakeWithBuilder()
Austin Schuhde802e92016-02-27 14:49:03 -0800233 .angular_velocity(shooter_velocity_)
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800234 .clamp_open(is_intaking_ || is_outtaking_)
Austin Schuhde802e92016-02-27 14:49:03 -0800235 .push_to_shooter(fire_)
Comran Morshed200dd4b2016-02-16 17:54:58 +0000236 .Send()) {
237 LOG(ERROR, "Sending shooter goal failed.\n");
238 }
239 }
240 }
241
Comran Morshed9a9948c2016-01-16 15:58:04 +0000242 was_running_ = action_queue_.Running();
243 }
244
Comran Morshed9a9948c2016-01-16 15:58:04 +0000245 private:
246 void StartAuto() {
247 LOG(INFO, "Starting auto mode\n");
248 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
249 }
250
251 void StopAuto() {
252 LOG(INFO, "Stopping auto mode\n");
253 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
254 }
255
256 bool is_high_gear_;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000257 // Whatever these are set to are our default goals to send out after zeroing.
258 double intake_goal_;
259 double shoulder_goal_;
260 double wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800261 double shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000262
263 bool was_running_ = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000264 bool auto_running_ = false;
265
Comran Morshed200dd4b2016-02-16 17:54:58 +0000266 // If we're waiting for the subsystems to zero.
267 bool waiting_for_zero_ = true;
268
Austin Schuhde802e92016-02-27 14:49:03 -0800269 bool is_intaking_ = false;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800270 bool is_outtaking_ = false;
Austin Schuhde802e92016-02-27 14:49:03 -0800271 bool fire_ = false;
272
Comran Morshed9a9948c2016-01-16 15:58:04 +0000273 ::aos::common::actions::ActionQueue action_queue_;
274
275 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
276 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
277 "no drivetrain status");
278};
279
280} // namespace joysticks
281} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000282} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000283
284int main() {
285 ::aos::Init(-1);
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000286 ::y2016::input::joysticks::Reader reader;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000287 reader.Run();
288 ::aos::Cleanup();
289}