blob: 4d3355f70e15eeecdd1f935db73d241e22a31102 [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
Austin Schuh3028b1d2017-03-11 22:12:13 -08006#include "aos/common/actions/actions.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -08007#include "aos/common/input/driver_station_data.h"
8#include "aos/common/logging/logging.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -08009#include "aos/common/time.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080010#include "aos/common/util/log_interval.h"
11#include "aos/input/joystick_input.h"
12#include "aos/linux_code/init.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -080013#include "frc971/autonomous/auto.q.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080014#include "frc971/autonomous/base_autonomous_actor.h"
15#include "frc971/control_loops/drivetrain/drivetrain.q.h"
16#include "y2017/constants.h"
17#include "y2017/control_loops/superstructure/superstructure.q.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -080018
19using ::frc971::control_loops::drivetrain_queue;
20using ::y2017::control_loops::superstructure_queue;
21
22using ::aos::input::driver_station::ButtonLocation;
23using ::aos::input::driver_station::ControlBit;
24using ::aos::input::driver_station::JoystickAxis;
25using ::aos::input::driver_station::POVLocation;
26
27namespace y2017 {
28namespace input {
29namespace joysticks {
30
31const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
32const ButtonLocation kQuickTurn(1, 5);
33
34const ButtonLocation kTurn1(1, 7);
35const ButtonLocation kTurn2(1, 11);
36
37const ButtonLocation kIntakeDown(3, 9);
38const ButtonLocation kIntakeIn(3, 12);
39const ButtonLocation kIntakeOut(3, 8);
40const POVLocation kHang(3, 90);
41const ButtonLocation kFire(3, 3);
42const ButtonLocation kCloseShot(3, 7);
43const ButtonLocation kMiddleShot(3, 6);
44const POVLocation kFarShot(3, 270);
45
46const ButtonLocation kVisionAlign(3, 5);
47
48const ButtonLocation kReverseIndexer(3, 4);
49const ButtonLocation kExtra1(3, 11);
50const ButtonLocation kExtra2(3, 10);
51const ButtonLocation kExtra3(3, 2);
52
53class Reader : public ::aos::input::JoystickInput {
54 public:
55 Reader() {}
56
57 void RunIteration(const ::aos::input::driver_station::Data &data) override {
58 bool last_auto_running = auto_running_;
59 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
60 data.GetControlBit(ControlBit::kEnabled);
61 if (auto_running_ != last_auto_running) {
62 if (auto_running_) {
63 StartAuto();
64 } else {
65 StopAuto();
66 }
67 }
68
69 vision_valid_ = false;
70
71 if (!auto_running_) {
72 HandleDrivetrain(data);
73 HandleTeleop(data);
74 }
75
76 // Process any pending actions.
77 action_queue_.Tick();
78 was_running_ = action_queue_.Running();
79 }
80
81 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
82 bool is_control_loop_driving = false;
83
84 const double wheel = -data.GetAxis(kSteeringWheel);
85 const double throttle = -data.GetAxis(kDriveThrottle);
86 drivetrain_queue.status.FetchLatest();
87
88 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
89 if (drivetrain_queue.status.get()) {
90 left_goal_ = drivetrain_queue.status->estimated_left_position;
91 right_goal_ = drivetrain_queue.status->estimated_right_position;
92 }
93 }
94 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
95 is_control_loop_driving = true;
96 }
97 if (!drivetrain_queue.goal.MakeWithBuilder()
98 .steering(wheel)
99 .throttle(throttle)
100 .quickturn(data.IsPressed(kQuickTurn))
101 .control_loop_driving(is_control_loop_driving)
102 .left_goal(left_goal_ - wheel * 0.5 + throttle * 0.3)
103 .right_goal(right_goal_ + wheel * 0.5 + throttle * 0.3)
104 .left_velocity_goal(0)
105 .right_velocity_goal(0)
106 .Send()) {
107 LOG(WARNING, "sending stick values failed\n");
108 }
109 }
110
111 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
112 // Default the intake to in.
113 intake_goal_ = constants::Values::kIntakeRange.lower;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800114 bool lights_on = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800115
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)) {
Austin Schuh3028b1d2017-03-11 22:12:13 -0800128 intake_goal_ = 0.223;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800129 }
130
131 if (data.IsPressed(kVisionAlign)) {
132 // Align shot using vision
133 // TODO(campbell): Add vision aligning.
Adam Snaidere0554ef2017-03-11 23:02:45 -0800134 lights_on = true;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800135 shooter_velocity_ = 100.0;
136 } else if (data.IsPressed(kCloseShot)) {
137 // Close shot
138 hood_goal_ = 0.5;
139 shooter_velocity_ = 350.0;
140 } else if (data.IsPressed(kMiddleShot)) {
141 // Medium distance shot
142 hood_goal_ = 0.4;
143 shooter_velocity_ = 350.0;
144 } else if (data.IsPressed(kFarShot)) {
145 // Far shot
146 hood_goal_ = 0.6;
147 shooter_velocity_ = 250.0;
148 } else {
149 hood_goal_ = 0.15;
150 shooter_velocity_ = 0.0;
151 }
152
153 if (data.IsPressed(kExtra1)) {
Austin Schuh3028b1d2017-03-11 22:12:13 -0800154 turret_goal_ = -M_PI * 3.0 / 4.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800155 }
156 if (data.IsPressed(kExtra2)) {
157 turret_goal_ = 0.0;
158 }
159 if (data.IsPressed(kExtra3)) {
Austin Schuh3028b1d2017-03-11 22:12:13 -0800160 turret_goal_ = M_PI * 3.0 / 4.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800161 }
162
163 fire_ = data.IsPressed(kFire) && shooter_velocity_ != 0.0;
164
165 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
166 new_superstructure_goal->intake.distance = intake_goal_;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800167 new_superstructure_goal->intake.disable_intake = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800168 new_superstructure_goal->turret.angle = turret_goal_;
169 new_superstructure_goal->hood.angle = hood_goal_;
170 new_superstructure_goal->shooter.angular_velocity = shooter_velocity_;
171
172 new_superstructure_goal->intake.profile_params.max_velocity = 0.50;
173 new_superstructure_goal->turret.profile_params.max_velocity = 6.0;
174 new_superstructure_goal->hood.profile_params.max_velocity = 5.0;
175
176 new_superstructure_goal->intake.profile_params.max_acceleration = 5.0;
177 new_superstructure_goal->turret.profile_params.max_acceleration = 15.0;
178 new_superstructure_goal->hood.profile_params.max_acceleration = 25.0;
179
Austin Schuh3028b1d2017-03-11 22:12:13 -0800180 new_superstructure_goal->intake.voltage_rollers = 0.0;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800181 new_superstructure_goal->lights_on = lights_on;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800182
Campbell Crowley71b5f132017-02-18 13:16:08 -0800183 if (data.IsPressed(kHang)) {
184 new_superstructure_goal->intake.voltage_rollers = -12.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800185 new_superstructure_goal->intake.disable_intake = true;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800186 } else if (data.IsPressed(kIntakeIn)) {
187 new_superstructure_goal->intake.voltage_rollers = 12.0;
188 } else if (data.IsPressed(kIntakeOut)) {
189 new_superstructure_goal->intake.voltage_rollers = -8.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800190 }
191 if (intake_goal_ < 0.1) {
192 new_superstructure_goal->intake.voltage_rollers =
193 ::std::min(8.0, new_superstructure_goal->intake.voltage_rollers);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800194 }
195
196 if (data.IsPressed(kReverseIndexer)) {
197 new_superstructure_goal->indexer.voltage_rollers = -4.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800198 new_superstructure_goal->indexer.angular_velocity = 4.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800199 new_superstructure_goal->indexer.angular_velocity = 1.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800200 } else if (fire_) {
201 new_superstructure_goal->indexer.angular_velocity = -3.0 * M_PI;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800202 } else {
203 new_superstructure_goal->indexer.voltage_rollers = 0.0;
204 new_superstructure_goal->indexer.angular_velocity = 0.0;
205 }
206
207 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
208 if (!new_superstructure_goal.Send()) {
209 LOG(ERROR, "Sending superstructure goal failed.\n");
210 }
211 }
212
213 private:
Austin Schuh3028b1d2017-03-11 22:12:13 -0800214 void StartAuto() {
215 LOG(INFO, "Starting auto mode\n");
216
217 ::frc971::autonomous::AutonomousActionParams params;
218 ::frc971::autonomous::auto_mode.FetchLatest();
219 if (::frc971::autonomous::auto_mode.get() != nullptr) {
220 params.mode = ::frc971::autonomous::auto_mode->mode;
221 } else {
222 LOG(WARNING, "no auto mode values\n");
223 params.mode = 0;
224 }
225 action_queue_.EnqueueAction(
226 ::frc971::autonomous::MakeAutonomousAction(params));
227 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800228
229 void StopAuto() {
230 LOG(INFO, "Stopping auto mode\n");
231 action_queue_.CancelAllActions();
232 }
233
234 // Current goals to send to the robot.
235 double intake_goal_ = 0.0;
236 double turret_goal_ = 0.0;
237 double hood_goal_ = 0.3;
238 double shooter_velocity_ = 0.0;
239
240 // Goals to send to the drivetrain in closed loop mode.
Austin Schuh3028b1d2017-03-11 22:12:13 -0800241 double left_goal_ = 0.0;
242 double right_goal_ = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800243
244 bool was_running_ = false;
245 bool auto_running_ = false;
246
247 bool vision_valid_ = false;
248
249 bool fire_ = false;
250
251 ::aos::common::actions::ActionQueue action_queue_;
252};
253
254} // namespace joysticks
255} // namespace input
256} // namespace y2017
257
258int main() {
259 ::aos::Init(-1);
260 ::y2017::input::joysticks::Reader reader;
261 reader.Run();
262 ::aos::Cleanup();
263}