blob: c28a0ffaa5397016fccdd9aff4002843c23f1439 [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
Comran Morshed200dd4b2016-02-16 17:54:58 +000038// Buttons on the lexan driver station to get things running on bring-up day.
39const ButtonLocation kTest1(3, 6);
40const ButtonLocation kTest2(3, 2);
41const ButtonLocation kTest3(3, 11);
42const ButtonLocation kTest4(3, 9);
43const ButtonLocation kTest5(3, 8);
44const ButtonLocation kTest6(3, 3);
45const ButtonLocation kTest7(3, 5);
46const ButtonLocation kTest8(3, 4);
47
Comran Morshed9a9948c2016-01-16 15:58:04 +000048class Reader : public ::aos::input::JoystickInput {
49 public:
50 Reader()
51 : is_high_gear_(false),
Comran Morshed200dd4b2016-02-16 17:54:58 +000052 intake_goal_(0.0),
53 shoulder_goal_(M_PI / 2.0),
54 wrist_goal_(0.0) {}
Comran Morshed9a9948c2016-01-16 15:58:04 +000055
56 void RunIteration(const ::aos::input::driver_station::Data &data) override {
57 bool last_auto_running = auto_running_;
58 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
59 data.GetControlBit(ControlBit::kEnabled);
60 if (auto_running_ != last_auto_running) {
61 if (auto_running_) {
62 StartAuto();
63 } else {
64 StopAuto();
65 }
66 }
67
68 if (!data.GetControlBit(ControlBit::kAutonomous)) {
69 HandleDrivetrain(data);
70 HandleTeleop(data);
71 }
72 }
73
74 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
75 bool is_control_loop_driving = false;
76 double left_goal = 0.0;
77 double right_goal = 0.0;
78 const double wheel = -data.GetAxis(kSteeringWheel);
79 const double throttle = -data.GetAxis(kDriveThrottle);
Comran Morshed9a9948c2016-01-16 15:58:04 +000080
Comran Morshed9a9948c2016-01-16 15:58:04 +000081 if (!drivetrain_queue.goal.MakeWithBuilder()
82 .steering(wheel)
83 .throttle(throttle)
84 .highgear(is_high_gear_)
85 .quickturn(data.IsPressed(kQuickTurn))
86 .control_loop_driving(is_control_loop_driving)
87 .left_goal(left_goal)
88 .right_goal(right_goal)
89 .left_velocity_goal(0)
90 .right_velocity_goal(0)
91 .Send()) {
92 LOG(WARNING, "sending stick values failed\n");
93 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +000094
Campbell Crowley5b27f022016-02-20 16:55:35 -080095 if (data.PosEdge(kShiftLow)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +000096 is_high_gear_ = false;
97 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +000098
Campbell Crowley5b27f022016-02-20 16:55:35 -080099 if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000100 is_high_gear_ = true;
101 }
102 }
103
Comran Morshed9a9948c2016-01-16 15:58:04 +0000104 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
105 if (!data.GetControlBit(ControlBit::kEnabled)) {
106 action_queue_.CancelAllActions();
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000107 LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +0000108 }
109
Comran Morshed200dd4b2016-02-16 17:54:58 +0000110 if (data.PosEdge(ControlBit::kEnabled)) {
111 // If we got enabled, wait for everything to zero.
112 LOG(INFO, "Waiting for zero.\n");
113 waiting_for_zero_ = true;
Austin Schuhde802e92016-02-27 14:49:03 -0800114 is_high_gear_ = true;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000115 }
116
117 superstructure_queue.status.FetchLatest();
118 if (!superstructure_queue.status.get()) {
119 LOG(ERROR, "Got no superstructure status packet.\n");
120 }
121
122 if (superstructure_queue.status.get() &&
123 superstructure_queue.status->zeroed) {
124 if (waiting_for_zero_) {
125 LOG(INFO, "Zeroed! Starting teleop mode.\n");
126 waiting_for_zero_ = false;
127 }
128 } else {
129 waiting_for_zero_ = true;
130 }
131
Austin Schuhde802e92016-02-27 14:49:03 -0800132 if (data.IsPressed(kTest1)) {
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800133 intake_goal_ = -0.2;
Austin Schuhde802e92016-02-27 14:49:03 -0800134 } else {
135 intake_goal_ = 1.6;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000136 }
137
Austin Schuhde802e92016-02-27 14:49:03 -0800138 if (data.IsPressed(kTest2)) {
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800139 shoulder_goal_ = M_PI / 2.0 - 0.2;
Austin Schuhde802e92016-02-27 14:49:03 -0800140 } else {
141 shoulder_goal_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000142 }
143
Austin Schuhde802e92016-02-27 14:49:03 -0800144 if (data.IsPressed(kTest3)) {
145 wrist_goal_ = 0.0;
146 } else {
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800147 // Backwards shot
148 //wrist_goal_ = -0.59;
149 // Forwards shot
150 wrist_goal_ = M_PI + 0.42;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000151 }
152
Austin Schuhde802e92016-02-27 14:49:03 -0800153 is_intaking_ = data.IsPressed(kTest4);
154
155 if (data.IsPressed(kTest5)) {
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800156 //shooter_velocity_ = 600.0;
157 shooter_velocity_ = 640.0;
Austin Schuhde802e92016-02-27 14:49:03 -0800158 } else {
159 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000160 }
161
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800162 if (data.IsPressed(kTest6) && shooter_velocity_ != 0.0) {
Austin Schuhde802e92016-02-27 14:49:03 -0800163 fire_ = true;
164 } else {
165 fire_ = false;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000166 }
167
168 if (data.PosEdge(kTest7)) {
169 }
170
171 if (data.PosEdge(kTest8)) {
172 }
173
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800174 is_outtaking_ = data.IsPressed(kTest8);
175
Comran Morshed200dd4b2016-02-16 17:54:58 +0000176 if (!waiting_for_zero_) {
177 if (!action_queue_.Running()) {
178 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
179 new_superstructure_goal->angle_intake = intake_goal_;
180 new_superstructure_goal->angle_shoulder = shoulder_goal_;
181 new_superstructure_goal->angle_wrist = wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800182 new_superstructure_goal->max_angular_velocity_intake = 4.0;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800183 new_superstructure_goal->max_angular_velocity_shoulder = 2.0;
184 new_superstructure_goal->max_angular_velocity_wrist = 7.0;
Austin Schuhde802e92016-02-27 14:49:03 -0800185 new_superstructure_goal->max_angular_acceleration_intake = 5.0;
186 new_superstructure_goal->max_angular_acceleration_shoulder = 3.0;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800187 new_superstructure_goal->max_angular_acceleration_wrist = 15.0;
Austin Schuhde802e92016-02-27 14:49:03 -0800188 if (is_intaking_) {
189 new_superstructure_goal->voltage_top_rollers = 12.0;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800190 new_superstructure_goal->voltage_bottom_rollers = 12.0;
191 } else if (is_outtaking_) {
192 new_superstructure_goal->voltage_top_rollers = -12.0;
193 new_superstructure_goal->voltage_bottom_rollers = -7.0;
Austin Schuhde802e92016-02-27 14:49:03 -0800194 } else {
195 new_superstructure_goal->voltage_top_rollers = 0.0;
196 new_superstructure_goal->voltage_bottom_rollers = 0.0;
197 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000198
199 if (!new_superstructure_goal.Send()) {
200 LOG(ERROR, "Sending superstructure goal failed.\n");
201 } else {
202 LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
203 intake_goal_, shoulder_goal_, wrist_goal_);
204 }
205
206 if (!shooter_queue.goal.MakeWithBuilder()
Austin Schuhde802e92016-02-27 14:49:03 -0800207 .angular_velocity(shooter_velocity_)
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800208 .clamp_open(is_intaking_ || is_outtaking_)
Austin Schuhde802e92016-02-27 14:49:03 -0800209 .push_to_shooter(fire_)
Comran Morshed200dd4b2016-02-16 17:54:58 +0000210 .Send()) {
211 LOG(ERROR, "Sending shooter goal failed.\n");
212 }
213 }
214 }
215
Comran Morshed9a9948c2016-01-16 15:58:04 +0000216 was_running_ = action_queue_.Running();
217 }
218
Comran Morshed9a9948c2016-01-16 15:58:04 +0000219 private:
220 void StartAuto() {
221 LOG(INFO, "Starting auto mode\n");
222 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
223 }
224
225 void StopAuto() {
226 LOG(INFO, "Stopping auto mode\n");
227 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
228 }
229
230 bool is_high_gear_;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000231 // Whatever these are set to are our default goals to send out after zeroing.
232 double intake_goal_;
233 double shoulder_goal_;
234 double wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800235 double shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000236
237 bool was_running_ = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000238 bool auto_running_ = false;
239
Comran Morshed200dd4b2016-02-16 17:54:58 +0000240 // If we're waiting for the subsystems to zero.
241 bool waiting_for_zero_ = true;
242
Austin Schuhde802e92016-02-27 14:49:03 -0800243 bool is_intaking_ = false;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800244 bool is_outtaking_ = false;
Austin Schuhde802e92016-02-27 14:49:03 -0800245 bool fire_ = false;
246
Comran Morshed9a9948c2016-01-16 15:58:04 +0000247 ::aos::common::actions::ActionQueue action_queue_;
248
249 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
250 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
251 "no drivetrain status");
252};
253
254} // namespace joysticks
255} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000256} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000257
258int main() {
259 ::aos::Init(-1);
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000260 ::y2016::input::joysticks::Reader reader;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000261 reader.Run();
262 ::aos::Cleanup();
263}