blob: 179fac6ce5586f1153b0c228963d9e2b3d3caee4 [file] [log] [blame]
Campbell Crowley71b5f132017-02-18 13:16:08 -08001#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"
15#include "y2017/control_loops/superstructure/superstructure.q.h"
16
17#include "y2017/constants.h"
18#include "frc971/autonomous/auto.q.h"
19
20using ::frc971::control_loops::drivetrain_queue;
21using ::y2017::control_loops::superstructure_queue;
22
23using ::aos::input::driver_station::ButtonLocation;
24using ::aos::input::driver_station::ControlBit;
25using ::aos::input::driver_station::JoystickAxis;
26using ::aos::input::driver_station::POVLocation;
27
28namespace y2017 {
29namespace input {
30namespace joysticks {
31
32const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
33const ButtonLocation kQuickTurn(1, 5);
34
35const ButtonLocation kTurn1(1, 7);
36const ButtonLocation kTurn2(1, 11);
37
38const ButtonLocation kIntakeDown(3, 9);
39const ButtonLocation kIntakeIn(3, 12);
40const ButtonLocation kIntakeOut(3, 8);
41const POVLocation kHang(3, 90);
42const ButtonLocation kFire(3, 3);
43const ButtonLocation kCloseShot(3, 7);
44const ButtonLocation kMiddleShot(3, 6);
45const POVLocation kFarShot(3, 270);
46
47const ButtonLocation kVisionAlign(3, 5);
48
49const ButtonLocation kReverseIndexer(3, 4);
50const ButtonLocation kExtra1(3, 11);
51const ButtonLocation kExtra2(3, 10);
52const ButtonLocation kExtra3(3, 2);
53
54class Reader : public ::aos::input::JoystickInput {
55 public:
56 Reader() {}
57
58 void RunIteration(const ::aos::input::driver_station::Data &data) override {
59 bool last_auto_running = auto_running_;
60 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
61 data.GetControlBit(ControlBit::kEnabled);
62 if (auto_running_ != last_auto_running) {
63 if (auto_running_) {
64 StartAuto();
65 } else {
66 StopAuto();
67 }
68 }
69
70 vision_valid_ = false;
71
72 if (!auto_running_) {
73 HandleDrivetrain(data);
74 HandleTeleop(data);
75 }
76
77 // Process any pending actions.
78 action_queue_.Tick();
79 was_running_ = action_queue_.Running();
80 }
81
82 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
83 bool is_control_loop_driving = false;
84
85 const double wheel = -data.GetAxis(kSteeringWheel);
86 const double throttle = -data.GetAxis(kDriveThrottle);
87 drivetrain_queue.status.FetchLatest();
88
89 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
90 if (drivetrain_queue.status.get()) {
91 left_goal_ = drivetrain_queue.status->estimated_left_position;
92 right_goal_ = drivetrain_queue.status->estimated_right_position;
93 }
94 }
95 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
96 is_control_loop_driving = true;
97 }
98 if (!drivetrain_queue.goal.MakeWithBuilder()
99 .steering(wheel)
100 .throttle(throttle)
101 .quickturn(data.IsPressed(kQuickTurn))
102 .control_loop_driving(is_control_loop_driving)
103 .left_goal(left_goal_ - wheel * 0.5 + throttle * 0.3)
104 .right_goal(right_goal_ + wheel * 0.5 + throttle * 0.3)
105 .left_velocity_goal(0)
106 .right_velocity_goal(0)
107 .Send()) {
108 LOG(WARNING, "sending stick values failed\n");
109 }
110 }
111
112 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
113 // Default the intake to in.
114 intake_goal_ = constants::Values::kIntakeRange.lower;
115
116 if (!data.GetControlBit(ControlBit::kEnabled)) {
117 action_queue_.CancelAllActions();
118 LOG(DEBUG, "Canceling\n");
119 }
120
121 superstructure_queue.status.FetchLatest();
122 if (!superstructure_queue.status.get()) {
123 LOG(ERROR, "Got no superstructure status packet.\n");
124 return;
125 }
126
127 if (data.IsPressed(kIntakeDown)) {
128 intake_goal_ = 0.23;
129 }
130
131 if (data.IsPressed(kVisionAlign)) {
132 // Align shot using vision
133 // TODO(campbell): Add vision aligning.
134 shooter_velocity_ = 100.0;
135 } else if (data.IsPressed(kCloseShot)) {
136 // Close shot
137 hood_goal_ = 0.5;
138 shooter_velocity_ = 350.0;
139 } else if (data.IsPressed(kMiddleShot)) {
140 // Medium distance shot
141 hood_goal_ = 0.4;
142 shooter_velocity_ = 350.0;
143 } else if (data.IsPressed(kFarShot)) {
144 // Far shot
145 hood_goal_ = 0.6;
146 shooter_velocity_ = 250.0;
147 } else {
148 hood_goal_ = 0.15;
149 shooter_velocity_ = 0.0;
150 }
151
152 if (data.IsPressed(kExtra1)) {
153 turret_goal_ += -0.1;
154 }
155 if (data.IsPressed(kExtra2)) {
156 turret_goal_ = 0.0;
157 }
158 if (data.IsPressed(kExtra3)) {
159 turret_goal_ += 0.1;
160 }
161
162 fire_ = data.IsPressed(kFire) && shooter_velocity_ != 0.0;
163
164 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
165 new_superstructure_goal->intake.distance = intake_goal_;
166 new_superstructure_goal->turret.angle = turret_goal_;
167 new_superstructure_goal->hood.angle = hood_goal_;
168 new_superstructure_goal->shooter.angular_velocity = shooter_velocity_;
169
170 new_superstructure_goal->intake.profile_params.max_velocity = 0.50;
171 new_superstructure_goal->turret.profile_params.max_velocity = 6.0;
172 new_superstructure_goal->hood.profile_params.max_velocity = 5.0;
173
174 new_superstructure_goal->intake.profile_params.max_acceleration = 5.0;
175 new_superstructure_goal->turret.profile_params.max_acceleration = 15.0;
176 new_superstructure_goal->hood.profile_params.max_acceleration = 25.0;
177
178 if (data.IsPressed(kHang)) {
179 new_superstructure_goal->intake.voltage_rollers = -12.0;
180 } else if (data.IsPressed(kIntakeIn)) {
181 new_superstructure_goal->intake.voltage_rollers = 12.0;
182 } else if (data.IsPressed(kIntakeOut)) {
183 new_superstructure_goal->intake.voltage_rollers = -8.0;
184 } else {
185 new_superstructure_goal->intake.voltage_rollers = 0.0;
186 }
187
188 if (data.IsPressed(kReverseIndexer)) {
189 new_superstructure_goal->indexer.voltage_rollers = -4.0;
190 new_superstructure_goal->indexer.angular_velocity = -4.0;
191 new_superstructure_goal->indexer.angular_velocity = -1.0;
192 } else if (fire_) {
193 new_superstructure_goal->indexer.voltage_rollers = 2.0;
194 new_superstructure_goal->indexer.angular_velocity = 3.0 * M_PI;
195 new_superstructure_goal->indexer.angular_velocity = 1.0;
196 } else {
197 new_superstructure_goal->indexer.voltage_rollers = 0.0;
198 new_superstructure_goal->indexer.angular_velocity = 0.0;
199 }
200
201 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
202 if (!new_superstructure_goal.Send()) {
203 LOG(ERROR, "Sending superstructure goal failed.\n");
204 }
205 }
206
207 private:
208 void StartAuto() { LOG(INFO, "Starting auto mode\n"); }
209
210 void StopAuto() {
211 LOG(INFO, "Stopping auto mode\n");
212 action_queue_.CancelAllActions();
213 }
214
215 // Current goals to send to the robot.
216 double intake_goal_ = 0.0;
217 double turret_goal_ = 0.0;
218 double hood_goal_ = 0.3;
219 double shooter_velocity_ = 0.0;
220
221 // Goals to send to the drivetrain in closed loop mode.
222 double left_goal_;
223 double right_goal_;
224
225 bool was_running_ = false;
226 bool auto_running_ = false;
227
228 bool vision_valid_ = false;
229
230 bool fire_ = false;
231
232 ::aos::common::actions::ActionQueue action_queue_;
233};
234
235} // namespace joysticks
236} // namespace input
237} // namespace y2017
238
239int main() {
240 ::aos::Init(-1);
241 ::y2017::input::joysticks::Reader reader;
242 reader.Run();
243 ::aos::Cleanup();
244}