blob: 190692f0db1814cc22ea6eb14cf657232d43d078 [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;
114 }
115
116 superstructure_queue.status.FetchLatest();
117 if (!superstructure_queue.status.get()) {
118 LOG(ERROR, "Got no superstructure status packet.\n");
119 }
120
121 if (superstructure_queue.status.get() &&
122 superstructure_queue.status->zeroed) {
123 if (waiting_for_zero_) {
124 LOG(INFO, "Zeroed! Starting teleop mode.\n");
125 waiting_for_zero_ = false;
126 }
127 } else {
128 waiting_for_zero_ = true;
129 }
130
131 // TODO(robot bringup): Populate these with test goals.
132 if (data.PosEdge(kTest1)) {
133 }
134
135 if (data.PosEdge(kTest2)) {
136 }
137
138 if (data.PosEdge(kTest3)) {
139 }
140
141 if (data.PosEdge(kTest4)) {
142 }
143
144 if (data.PosEdge(kTest5)) {
145 }
146
147 if (data.PosEdge(kTest6)) {
148 }
149
150 if (data.PosEdge(kTest7)) {
151 }
152
153 if (data.PosEdge(kTest8)) {
154 }
155
156 if (!waiting_for_zero_) {
157 if (!action_queue_.Running()) {
158 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
159 new_superstructure_goal->angle_intake = intake_goal_;
160 new_superstructure_goal->angle_shoulder = shoulder_goal_;
161 new_superstructure_goal->angle_wrist = wrist_goal_;
162 new_superstructure_goal->max_angular_velocity_intake = 0.1;
163 new_superstructure_goal->max_angular_velocity_shoulder = 0.1;
164 new_superstructure_goal->max_angular_velocity_wrist = 0.1;
165 new_superstructure_goal->max_angular_acceleration_intake = 0.05;
166 new_superstructure_goal->max_angular_acceleration_shoulder = 0.05;
167 new_superstructure_goal->max_angular_acceleration_wrist = 0.05;
168 new_superstructure_goal->voltage_rollers = 0.0;
169
170 if (!new_superstructure_goal.Send()) {
171 LOG(ERROR, "Sending superstructure goal failed.\n");
172 } else {
173 LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
174 intake_goal_, shoulder_goal_, wrist_goal_);
175 }
176
177 if (!shooter_queue.goal.MakeWithBuilder()
178 .angular_velocity(0.0)
179 .clamp_open(false)
180 .push_to_shooter(false)
181 .Send()) {
182 LOG(ERROR, "Sending shooter goal failed.\n");
183 }
184 }
185 }
186
Comran Morshed9a9948c2016-01-16 15:58:04 +0000187 was_running_ = action_queue_.Running();
188 }
189
Comran Morshed9a9948c2016-01-16 15:58:04 +0000190 private:
191 void StartAuto() {
192 LOG(INFO, "Starting auto mode\n");
193 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
194 }
195
196 void StopAuto() {
197 LOG(INFO, "Stopping auto mode\n");
198 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
199 }
200
201 bool is_high_gear_;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000202 // Whatever these are set to are our default goals to send out after zeroing.
203 double intake_goal_;
204 double shoulder_goal_;
205 double wrist_goal_;
206
207 bool was_running_ = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000208 bool auto_running_ = false;
209
Comran Morshed200dd4b2016-02-16 17:54:58 +0000210 // If we're waiting for the subsystems to zero.
211 bool waiting_for_zero_ = true;
212
Comran Morshed9a9948c2016-01-16 15:58:04 +0000213 ::aos::common::actions::ActionQueue action_queue_;
214
215 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
216 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
217 "no drivetrain status");
218};
219
220} // namespace joysticks
221} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000222} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000223
224int main() {
225 ::aos::Init(-1);
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000226 ::y2016::input::joysticks::Reader reader;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000227 reader.Run();
228 ::aos::Cleanup();
229}