blob: 32a48b4eb7c021d038599f2c89eda35b1af2cb5d [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)) {
133 intake_goal_ = 0.0;
134 } 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)) {
139 shoulder_goal_ = M_PI / 2.0;
140 } 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 {
147 wrist_goal_ = -0.4;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000148 }
149
Austin Schuhde802e92016-02-27 14:49:03 -0800150 is_intaking_ = data.IsPressed(kTest4);
151
152 if (data.IsPressed(kTest5)) {
153 shooter_velocity_ = 600.0;
154 } else {
155 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000156 }
157
Austin Schuhde802e92016-02-27 14:49:03 -0800158 if (data.IsPressed(kTest6)) {
159 fire_ = true;
160 } else {
161 fire_ = false;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000162 }
163
164 if (data.PosEdge(kTest7)) {
165 }
166
167 if (data.PosEdge(kTest8)) {
168 }
169
170 if (!waiting_for_zero_) {
171 if (!action_queue_.Running()) {
172 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
173 new_superstructure_goal->angle_intake = intake_goal_;
174 new_superstructure_goal->angle_shoulder = shoulder_goal_;
175 new_superstructure_goal->angle_wrist = wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800176 new_superstructure_goal->max_angular_velocity_intake = 4.0;
177 new_superstructure_goal->max_angular_velocity_shoulder = 1.0;
178 new_superstructure_goal->max_angular_velocity_wrist = 1.0;
179 new_superstructure_goal->max_angular_acceleration_intake = 5.0;
180 new_superstructure_goal->max_angular_acceleration_shoulder = 3.0;
181 new_superstructure_goal->max_angular_acceleration_wrist = 3.0;
182 if (is_intaking_) {
183 new_superstructure_goal->voltage_top_rollers = 12.0;
184 new_superstructure_goal->voltage_bottom_rollers = 6.0;
185 } else {
186 new_superstructure_goal->voltage_top_rollers = 0.0;
187 new_superstructure_goal->voltage_bottom_rollers = 0.0;
188 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000189
190 if (!new_superstructure_goal.Send()) {
191 LOG(ERROR, "Sending superstructure goal failed.\n");
192 } else {
193 LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
194 intake_goal_, shoulder_goal_, wrist_goal_);
195 }
196
197 if (!shooter_queue.goal.MakeWithBuilder()
Austin Schuhde802e92016-02-27 14:49:03 -0800198 .angular_velocity(shooter_velocity_)
199 .clamp_open(is_intaking_)
200 .push_to_shooter(fire_)
Comran Morshed200dd4b2016-02-16 17:54:58 +0000201 .Send()) {
202 LOG(ERROR, "Sending shooter goal failed.\n");
203 }
204 }
205 }
206
Comran Morshed9a9948c2016-01-16 15:58:04 +0000207 was_running_ = action_queue_.Running();
208 }
209
Comran Morshed9a9948c2016-01-16 15:58:04 +0000210 private:
211 void StartAuto() {
212 LOG(INFO, "Starting auto mode\n");
213 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
214 }
215
216 void StopAuto() {
217 LOG(INFO, "Stopping auto mode\n");
218 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
219 }
220
221 bool is_high_gear_;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000222 // Whatever these are set to are our default goals to send out after zeroing.
223 double intake_goal_;
224 double shoulder_goal_;
225 double wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800226 double shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000227
228 bool was_running_ = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000229 bool auto_running_ = false;
230
Comran Morshed200dd4b2016-02-16 17:54:58 +0000231 // If we're waiting for the subsystems to zero.
232 bool waiting_for_zero_ = true;
233
Austin Schuhde802e92016-02-27 14:49:03 -0800234 bool is_intaking_ = false;
235 bool fire_ = false;
236
Comran Morshed9a9948c2016-01-16 15:58:04 +0000237 ::aos::common::actions::ActionQueue action_queue_;
238
239 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
240 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
241 "no drivetrain status");
242};
243
244} // namespace joysticks
245} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000246} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000247
248int main() {
249 ::aos::Init(-1);
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000250 ::y2016::input::joysticks::Reader reader;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000251 reader.Run();
252 ::aos::Cleanup();
253}