blob: e0f66ba33ad5b29fb792f9be9acb21722c4c8a4c [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"
Sabina Davis92d2efa2017-11-04 22:35:25 -070011#include "aos/input/drivetrain_input.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080012#include "aos/input/joystick_input.h"
13#include "aos/linux_code/init.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -080014#include "frc971/autonomous/auto.q.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080015#include "frc971/autonomous/base_autonomous_actor.h"
16#include "frc971/control_loops/drivetrain/drivetrain.q.h"
17#include "y2017/constants.h"
18#include "y2017/control_loops/superstructure/superstructure.q.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -080019
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;
Sabina Davis92d2efa2017-11-04 22:35:25 -070027using ::aos::input::DrivetrainInputReader;
Campbell Crowley71b5f132017-02-18 13:16:08 -080028
29namespace y2017 {
30namespace input {
31namespace joysticks {
32
Austin Schuh55c8d302017-04-05 19:25:37 -070033const ButtonLocation kGearSlotBack(2, 11);
34
Campbell Crowley71b5f132017-02-18 13:16:08 -080035const ButtonLocation kIntakeDown(3, 9);
Austin Schuhd0629b12017-03-22 22:37:16 -070036const POVLocation kIntakeUp(3, 90);
Campbell Crowley71b5f132017-02-18 13:16:08 -080037const ButtonLocation kIntakeIn(3, 12);
38const ButtonLocation kIntakeOut(3, 8);
Campbell Crowley71b5f132017-02-18 13:16:08 -080039const ButtonLocation kFire(3, 3);
Parker Schuh208a58d2017-04-12 20:51:38 -070040const ButtonLocation kVisionDistanceShot(3, 7);
Campbell Crowley71b5f132017-02-18 13:16:08 -080041const ButtonLocation kMiddleShot(3, 6);
42const POVLocation kFarShot(3, 270);
43
44const ButtonLocation kVisionAlign(3, 5);
45
46const ButtonLocation kReverseIndexer(3, 4);
47const ButtonLocation kExtra1(3, 11);
48const ButtonLocation kExtra2(3, 10);
Austin Schuhd0629b12017-03-22 22:37:16 -070049const ButtonLocation kHang(3, 2);
Campbell Crowley71b5f132017-02-18 13:16:08 -080050
Sabina Davis92d2efa2017-11-04 22:35:25 -070051std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
52
Campbell Crowley71b5f132017-02-18 13:16:08 -080053class Reader : public ::aos::input::JoystickInput {
54 public:
Sabina Davis92d2efa2017-11-04 22:35:25 -070055 Reader() {
56 drivetrain_input_reader_ = DrivetrainInputReader::Make(
57 DrivetrainInputReader::InputType::kSteeringWheel);
58 }
Campbell Crowley71b5f132017-02-18 13:16:08 -080059
Parker Schuh208a58d2017-04-12 20:51:38 -070060 enum class ShotDistance { VISION_SHOT, MIDDLE_SHOT, FAR_SHOT };
Austin Schuhd0629b12017-03-22 22:37:16 -070061
Austin Schuh4b5f7df2017-04-16 20:31:12 -070062 ShotDistance last_shot_distance_ = ShotDistance::VISION_SHOT;
Austin Schuhd0629b12017-03-22 22:37:16 -070063
Campbell Crowley71b5f132017-02-18 13:16:08 -080064 void RunIteration(const ::aos::input::driver_station::Data &data) override {
65 bool last_auto_running = auto_running_;
66 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
67 data.GetControlBit(ControlBit::kEnabled);
68 if (auto_running_ != last_auto_running) {
69 if (auto_running_) {
70 StartAuto();
71 } else {
72 StopAuto();
73 }
74 }
75
Campbell Crowley71b5f132017-02-18 13:16:08 -080076 if (!auto_running_) {
77 HandleDrivetrain(data);
78 HandleTeleop(data);
79 }
80
81 // Process any pending actions.
82 action_queue_.Tick();
83 was_running_ = action_queue_.Running();
84 }
Austin Schuhd0629b12017-03-22 22:37:16 -070085 int intake_accumulator_ = 0;
Campbell Crowley71b5f132017-02-18 13:16:08 -080086
87 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Sabina Davis92d2efa2017-11-04 22:35:25 -070088 drivetrain_input_reader_->HandleDrivetrain(data);
89 robot_velocity_ = drivetrain_input_reader_->robot_velocity();
Campbell Crowley71b5f132017-02-18 13:16:08 -080090 }
91
92 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
93 // Default the intake to in.
Austin Schuhd0629b12017-03-22 22:37:16 -070094 intake_goal_ = 0.07;
Adam Snaidere0554ef2017-03-11 23:02:45 -080095 bool lights_on = false;
Austin Schuhd0629b12017-03-22 22:37:16 -070096 bool vision_track = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -080097
98 if (!data.GetControlBit(ControlBit::kEnabled)) {
99 action_queue_.CancelAllActions();
100 LOG(DEBUG, "Canceling\n");
101 }
102
103 superstructure_queue.status.FetchLatest();
104 if (!superstructure_queue.status.get()) {
105 LOG(ERROR, "Got no superstructure status packet.\n");
106 return;
107 }
108
Austin Schuhd0629b12017-03-22 22:37:16 -0700109 if (data.IsPressed(kIntakeUp)) {
110 intake_goal_ = 0.0;
Austin Schuh3ae47432017-04-16 19:15:46 -0700111 turret_goal_ = M_PI / 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700112 }
Austin Schuh405724e2017-04-09 18:34:18 -0700113 if (data.IsPressed(kIntakeDown)) {
114 intake_goal_ = 0.235;
115 // Don't go quite so far out since we have a gear mech out now.
116 if (data.IsPressed(kIntakeUp)) {
117 intake_goal_ = 0.160;
118 }
119 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700120
Campbell Crowley71b5f132017-02-18 13:16:08 -0800121 if (data.IsPressed(kVisionAlign)) {
122 // Align shot using vision
123 // TODO(campbell): Add vision aligning.
Adam Snaidere0554ef2017-03-11 23:02:45 -0800124 lights_on = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700125 vision_track = true;
126 }
127 if (data.PosEdge(kMiddleShot)) {
128 turret_goal_ = -M_PI;
129 }
130 if (data.PosEdge(kFarShot)) {
131 turret_goal_ = 0.0;
132 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700133 if (data.PosEdge(kVisionDistanceShot)) {
134 turret_goal_ = 0.0;
135 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700136
Parker Schuh208a58d2017-04-12 20:51:38 -0700137 if (data.IsPressed(kVisionDistanceShot)) {
138 last_shot_distance_ = ShotDistance::VISION_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800139 } else if (data.IsPressed(kMiddleShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700140 last_shot_distance_ = ShotDistance::MIDDLE_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800141 } else if (data.IsPressed(kFarShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700142 last_shot_distance_ = ShotDistance::FAR_SHOT;
143 }
144
Parker Schuh208a58d2017-04-12 20:51:38 -0700145 if (data.IsPressed(kVisionAlign) ||
Austin Schuhd0629b12017-03-22 22:37:16 -0700146 data.IsPressed(kMiddleShot) || data.IsPressed(kFarShot) ||
147 data.IsPressed(kFire)) {
148 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700149 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700150 hood_goal_ = 0.10;
151 shooter_velocity_ = 300.0;
152
Parker Schuh208a58d2017-04-12 20:51:38 -0700153 vision_distance_shot_ = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700154 break;
155 case ShotDistance::MIDDLE_SHOT:
Austin Schuh405724e2017-04-09 18:34:18 -0700156 hood_goal_ = 0.43 - 0.00;
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700157 shooter_velocity_ = 364.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700158 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700159 break;
160 case ShotDistance::FAR_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700161 hood_goal_ = 0.47;
162 shooter_velocity_ = 410.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700163 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700164 break;
165 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800166 } else {
Austin Schuhd0629b12017-03-22 22:37:16 -0700167 //hood_goal_ = 0.15;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800168 shooter_velocity_ = 0.0;
169 }
170
171 if (data.IsPressed(kExtra1)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700172 //turret_goal_ = -M_PI * 3.0 / 4.0;
173 turret_goal_ += 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800174 }
175 if (data.IsPressed(kExtra2)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700176 //turret_goal_ = M_PI * 3.0 / 4.0;
177 turret_goal_ -= 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800178 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700179 turret_goal_ = ::std::max(::std::min(turret_goal_, M_PI), -M_PI);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800180
181 fire_ = data.IsPressed(kFire) && shooter_velocity_ != 0.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700182 if (data.IsPressed(kVisionAlign)) {
183 fire_ = fire_ && superstructure_queue.status->turret.vision_tracking;
184 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800185
186 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
Austin Schuhd0629b12017-03-22 22:37:16 -0700187 if (data.IsPressed(kHang)) {
188 intake_goal_ = 0.23;
189 }
190
Campbell Crowley71b5f132017-02-18 13:16:08 -0800191 new_superstructure_goal->intake.distance = intake_goal_;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800192 new_superstructure_goal->intake.disable_intake = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800193 new_superstructure_goal->turret.angle = turret_goal_;
Austin Schuhd0629b12017-03-22 22:37:16 -0700194 new_superstructure_goal->turret.track = vision_track;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800195 new_superstructure_goal->hood.angle = hood_goal_;
196 new_superstructure_goal->shooter.angular_velocity = shooter_velocity_;
197
Austin Schuh6a8131b2017-04-08 15:39:22 -0700198 if (data.IsPressed(kIntakeUp)) {
Austin Schuhd6fa5e02017-04-12 20:52:17 -0700199 new_superstructure_goal->intake.gear_servo = 0.30;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700200 } else {
201 // Clamp the gear
Austin Schuhd6fa5e02017-04-12 20:52:17 -0700202 new_superstructure_goal->intake.gear_servo = 0.66;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700203 }
204
Campbell Crowley71b5f132017-02-18 13:16:08 -0800205 new_superstructure_goal->intake.profile_params.max_velocity = 0.50;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800206 new_superstructure_goal->hood.profile_params.max_velocity = 5.0;
207
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700208 new_superstructure_goal->intake.profile_params.max_acceleration = 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700209 if (vision_track) {
210 new_superstructure_goal->turret.profile_params.max_acceleration = 35.0;
211 new_superstructure_goal->turret.profile_params.max_velocity = 10.0;
212 } else {
213 new_superstructure_goal->turret.profile_params.max_acceleration = 15.0;
214 new_superstructure_goal->turret.profile_params.max_velocity = 6.0;
215 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800216 new_superstructure_goal->hood.profile_params.max_acceleration = 25.0;
217
Austin Schuh3028b1d2017-03-11 22:12:13 -0800218 new_superstructure_goal->intake.voltage_rollers = 0.0;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800219 new_superstructure_goal->lights_on = lights_on;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800220
Parker Schuh208a58d2017-04-12 20:51:38 -0700221 if (data.IsPressed(kVisionAlign) && vision_distance_shot_) {
222 new_superstructure_goal->use_vision_for_shots = true;
223 } else {
224 new_superstructure_goal->use_vision_for_shots = false;
225 }
226
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700227 if (superstructure_queue.status->intake.position >
228 superstructure_queue.status->intake.unprofiled_goal_position + 0.01) {
229 intake_accumulator_ = 10;
230 }
231 if (intake_accumulator_ > 0) {
232 --intake_accumulator_;
233 if (!superstructure_queue.status->intake.estopped) {
234 new_superstructure_goal->intake.voltage_rollers = 10.0;
235 }
236 }
237
Campbell Crowley71b5f132017-02-18 13:16:08 -0800238 if (data.IsPressed(kHang)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700239 new_superstructure_goal->intake.voltage_rollers = -10.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800240 new_superstructure_goal->intake.disable_intake = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700241 } else if (data.IsPressed(kIntakeIn) || data.IsPressed(kFire)) {
242 if (robot_velocity_ > 2.0) {
243 new_superstructure_goal->intake.voltage_rollers = 12.0;
244 } else {
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700245 new_superstructure_goal->intake.voltage_rollers = 11.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700246 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800247 } else if (data.IsPressed(kIntakeOut)) {
248 new_superstructure_goal->intake.voltage_rollers = -8.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800249 }
250 if (intake_goal_ < 0.1) {
251 new_superstructure_goal->intake.voltage_rollers =
252 ::std::min(8.0, new_superstructure_goal->intake.voltage_rollers);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800253 }
254
255 if (data.IsPressed(kReverseIndexer)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700256 new_superstructure_goal->indexer.voltage_rollers = -12.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800257 new_superstructure_goal->indexer.angular_velocity = 4.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800258 new_superstructure_goal->indexer.angular_velocity = 1.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800259 } else if (fire_) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700260 new_superstructure_goal->indexer.voltage_rollers = 12.0;
261 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700262 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700263 new_superstructure_goal->indexer.angular_velocity = -1.25 * M_PI;
Austin Schuhd0629b12017-03-22 22:37:16 -0700264 break;
265 case ShotDistance::MIDDLE_SHOT:
266 case ShotDistance::FAR_SHOT:
267 new_superstructure_goal->indexer.angular_velocity = -2.25 * M_PI;
268 break;
269 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800270 } else {
271 new_superstructure_goal->indexer.voltage_rollers = 0.0;
272 new_superstructure_goal->indexer.angular_velocity = 0.0;
273 }
274
275 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
276 if (!new_superstructure_goal.Send()) {
277 LOG(ERROR, "Sending superstructure goal failed.\n");
278 }
279 }
280
281 private:
Austin Schuh3028b1d2017-03-11 22:12:13 -0800282 void StartAuto() {
283 LOG(INFO, "Starting auto mode\n");
284
285 ::frc971::autonomous::AutonomousActionParams params;
286 ::frc971::autonomous::auto_mode.FetchLatest();
287 if (::frc971::autonomous::auto_mode.get() != nullptr) {
288 params.mode = ::frc971::autonomous::auto_mode->mode;
289 } else {
290 LOG(WARNING, "no auto mode values\n");
291 params.mode = 0;
292 }
293 action_queue_.EnqueueAction(
294 ::frc971::autonomous::MakeAutonomousAction(params));
295 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800296
297 void StopAuto() {
298 LOG(INFO, "Stopping auto mode\n");
299 action_queue_.CancelAllActions();
300 }
301
302 // Current goals to send to the robot.
303 double intake_goal_ = 0.0;
304 double turret_goal_ = 0.0;
305 double hood_goal_ = 0.3;
306 double shooter_velocity_ = 0.0;
307
Campbell Crowley71b5f132017-02-18 13:16:08 -0800308 bool was_running_ = false;
309 bool auto_running_ = false;
310
Parker Schuh208a58d2017-04-12 20:51:38 -0700311 bool vision_distance_shot_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800312
313 bool fire_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700314 double robot_velocity_ = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800315
316 ::aos::common::actions::ActionQueue action_queue_;
317};
318
319} // namespace joysticks
320} // namespace input
321} // namespace y2017
322
323int main() {
324 ::aos::Init(-1);
325 ::y2017::input::joysticks::Reader reader;
326 reader.Run();
327 ::aos::Cleanup();
328}