blob: 450eaa1b388cb83825465ae3912c05c27880c7d0 [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;
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800168 new_superstructure_goal->voltage_top_rollers = 0.0;
169 new_superstructure_goal->voltage_bottom_rollers = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000170
171 if (!new_superstructure_goal.Send()) {
172 LOG(ERROR, "Sending superstructure goal failed.\n");
173 } else {
174 LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
175 intake_goal_, shoulder_goal_, wrist_goal_);
176 }
177
178 if (!shooter_queue.goal.MakeWithBuilder()
179 .angular_velocity(0.0)
180 .clamp_open(false)
181 .push_to_shooter(false)
182 .Send()) {
183 LOG(ERROR, "Sending shooter goal failed.\n");
184 }
185 }
186 }
187
Comran Morshed9a9948c2016-01-16 15:58:04 +0000188 was_running_ = action_queue_.Running();
189 }
190
Comran Morshed9a9948c2016-01-16 15:58:04 +0000191 private:
192 void StartAuto() {
193 LOG(INFO, "Starting auto mode\n");
194 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
195 }
196
197 void StopAuto() {
198 LOG(INFO, "Stopping auto mode\n");
199 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
200 }
201
202 bool is_high_gear_;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000203 // Whatever these are set to are our default goals to send out after zeroing.
204 double intake_goal_;
205 double shoulder_goal_;
206 double wrist_goal_;
207
208 bool was_running_ = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000209 bool auto_running_ = false;
210
Comran Morshed200dd4b2016-02-16 17:54:58 +0000211 // If we're waiting for the subsystems to zero.
212 bool waiting_for_zero_ = true;
213
Comran Morshed9a9948c2016-01-16 15:58:04 +0000214 ::aos::common::actions::ActionQueue action_queue_;
215
216 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
217 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
218 "no drivetrain status");
219};
220
221} // namespace joysticks
222} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000223} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000224
225int main() {
226 ::aos::Init(-1);
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000227 ::y2016::input::joysticks::Reader reader;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000228 reader.Run();
229 ::aos::Cleanup();
230}