blob: d9bca585212d638d0f27ddec8f60a08fa9ce21d0 [file] [log] [blame]
Sabina Davis82b19182017-11-10 09:30:25 -08001#include <math.h>
Campbell Crowley71b5f132017-02-18 13:16:08 -08002#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Campbell Crowley71b5f132017-02-18 13:16:08 -08005
John Park33858a32018-09-28 23:05:48 -07006#include "aos/actions/actions.h"
7#include "aos/input/driver_station_data.h"
8#include "aos/logging/logging.h"
9#include "aos/time/time.h"
10#include "aos/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"
John Park398c74a2018-10-20 21:17:39 -070013#include "aos/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"
Sabina Davis82b19182017-11-10 09:30:25 -080019#include "y2017/control_loops/drivetrain/drivetrain_base.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -080020
21using ::frc971::control_loops::drivetrain_queue;
22using ::y2017::control_loops::superstructure_queue;
23
24using ::aos::input::driver_station::ButtonLocation;
25using ::aos::input::driver_station::ControlBit;
26using ::aos::input::driver_station::JoystickAxis;
27using ::aos::input::driver_station::POVLocation;
Sabina Davis92d2efa2017-11-04 22:35:25 -070028using ::aos::input::DrivetrainInputReader;
Campbell Crowley71b5f132017-02-18 13:16:08 -080029
30namespace y2017 {
31namespace input {
32namespace joysticks {
33
Austin Schuh55c8d302017-04-05 19:25:37 -070034const ButtonLocation kGearSlotBack(2, 11);
35
Campbell Crowley71b5f132017-02-18 13:16:08 -080036const ButtonLocation kIntakeDown(3, 9);
Austin Schuhd0629b12017-03-22 22:37:16 -070037const POVLocation kIntakeUp(3, 90);
Campbell Crowley71b5f132017-02-18 13:16:08 -080038const ButtonLocation kIntakeIn(3, 12);
39const ButtonLocation kIntakeOut(3, 8);
Campbell Crowley71b5f132017-02-18 13:16:08 -080040const ButtonLocation kFire(3, 3);
Parker Schuh208a58d2017-04-12 20:51:38 -070041const ButtonLocation kVisionDistanceShot(3, 7);
Campbell Crowley71b5f132017-02-18 13:16:08 -080042const ButtonLocation kMiddleShot(3, 6);
43const POVLocation kFarShot(3, 270);
44
45const ButtonLocation kVisionAlign(3, 5);
46
47const ButtonLocation kReverseIndexer(3, 4);
48const ButtonLocation kExtra1(3, 11);
49const ButtonLocation kExtra2(3, 10);
Austin Schuhd0629b12017-03-22 22:37:16 -070050const ButtonLocation kHang(3, 2);
Campbell Crowley71b5f132017-02-18 13:16:08 -080051
Sabina Davis92d2efa2017-11-04 22:35:25 -070052std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
53
Campbell Crowley71b5f132017-02-18 13:16:08 -080054class Reader : public ::aos::input::JoystickInput {
55 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080056 Reader(::aos::EventLoop *event_loop)
57 : ::aos::input::JoystickInput(event_loop) {
Sabina Davis92d2efa2017-11-04 22:35:25 -070058 drivetrain_input_reader_ = DrivetrainInputReader::Make(
Sabina Davis82b19182017-11-10 09:30:25 -080059 DrivetrainInputReader::InputType::kSteeringWheel,
60 ::y2017::control_loops::drivetrain::GetDrivetrainConfig());
Sabina Davis92d2efa2017-11-04 22:35:25 -070061 }
Campbell Crowley71b5f132017-02-18 13:16:08 -080062
Parker Schuh208a58d2017-04-12 20:51:38 -070063 enum class ShotDistance { VISION_SHOT, MIDDLE_SHOT, FAR_SHOT };
Austin Schuhd0629b12017-03-22 22:37:16 -070064
Austin Schuh4b5f7df2017-04-16 20:31:12 -070065 ShotDistance last_shot_distance_ = ShotDistance::VISION_SHOT;
Austin Schuhd0629b12017-03-22 22:37:16 -070066
Campbell Crowley71b5f132017-02-18 13:16:08 -080067 void RunIteration(const ::aos::input::driver_station::Data &data) override {
68 bool last_auto_running = auto_running_;
69 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
70 data.GetControlBit(ControlBit::kEnabled);
71 if (auto_running_ != last_auto_running) {
72 if (auto_running_) {
73 StartAuto();
74 } else {
75 StopAuto();
76 }
77 }
78
Campbell Crowley71b5f132017-02-18 13:16:08 -080079 if (!auto_running_) {
80 HandleDrivetrain(data);
81 HandleTeleop(data);
82 }
83
84 // Process any pending actions.
85 action_queue_.Tick();
86 was_running_ = action_queue_.Running();
87 }
Austin Schuhd0629b12017-03-22 22:37:16 -070088 int intake_accumulator_ = 0;
Campbell Crowley71b5f132017-02-18 13:16:08 -080089
90 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Sabina Davis92d2efa2017-11-04 22:35:25 -070091 drivetrain_input_reader_->HandleDrivetrain(data);
92 robot_velocity_ = drivetrain_input_reader_->robot_velocity();
Campbell Crowley71b5f132017-02-18 13:16:08 -080093 }
94
95 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
96 // Default the intake to in.
Austin Schuhd0629b12017-03-22 22:37:16 -070097 intake_goal_ = 0.07;
Adam Snaidere0554ef2017-03-11 23:02:45 -080098 bool lights_on = false;
Austin Schuhd0629b12017-03-22 22:37:16 -070099 bool vision_track = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800100
101 if (!data.GetControlBit(ControlBit::kEnabled)) {
102 action_queue_.CancelAllActions();
103 LOG(DEBUG, "Canceling\n");
104 }
105
106 superstructure_queue.status.FetchLatest();
107 if (!superstructure_queue.status.get()) {
108 LOG(ERROR, "Got no superstructure status packet.\n");
109 return;
110 }
111
Austin Schuhd0629b12017-03-22 22:37:16 -0700112 if (data.IsPressed(kIntakeUp)) {
113 intake_goal_ = 0.0;
Austin Schuh3ae47432017-04-16 19:15:46 -0700114 turret_goal_ = M_PI / 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700115 }
Austin Schuh405724e2017-04-09 18:34:18 -0700116 if (data.IsPressed(kIntakeDown)) {
117 intake_goal_ = 0.235;
118 // Don't go quite so far out since we have a gear mech out now.
119 if (data.IsPressed(kIntakeUp)) {
120 intake_goal_ = 0.160;
121 }
122 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700123
Campbell Crowley71b5f132017-02-18 13:16:08 -0800124 if (data.IsPressed(kVisionAlign)) {
125 // Align shot using vision
126 // TODO(campbell): Add vision aligning.
Adam Snaidere0554ef2017-03-11 23:02:45 -0800127 lights_on = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700128 vision_track = true;
129 }
130 if (data.PosEdge(kMiddleShot)) {
131 turret_goal_ = -M_PI;
132 }
133 if (data.PosEdge(kFarShot)) {
134 turret_goal_ = 0.0;
135 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700136 if (data.PosEdge(kVisionDistanceShot)) {
137 turret_goal_ = 0.0;
138 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700139
Parker Schuh208a58d2017-04-12 20:51:38 -0700140 if (data.IsPressed(kVisionDistanceShot)) {
141 last_shot_distance_ = ShotDistance::VISION_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800142 } else if (data.IsPressed(kMiddleShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700143 last_shot_distance_ = ShotDistance::MIDDLE_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800144 } else if (data.IsPressed(kFarShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700145 last_shot_distance_ = ShotDistance::FAR_SHOT;
146 }
147
Parker Schuh208a58d2017-04-12 20:51:38 -0700148 if (data.IsPressed(kVisionAlign) ||
Austin Schuhd0629b12017-03-22 22:37:16 -0700149 data.IsPressed(kMiddleShot) || data.IsPressed(kFarShot) ||
150 data.IsPressed(kFire)) {
151 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700152 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700153 hood_goal_ = 0.10;
154 shooter_velocity_ = 300.0;
155
Parker Schuh208a58d2017-04-12 20:51:38 -0700156 vision_distance_shot_ = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700157 break;
158 case ShotDistance::MIDDLE_SHOT:
Austin Schuh405724e2017-04-09 18:34:18 -0700159 hood_goal_ = 0.43 - 0.00;
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700160 shooter_velocity_ = 364.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700161 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700162 break;
163 case ShotDistance::FAR_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700164 hood_goal_ = 0.47;
165 shooter_velocity_ = 410.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700166 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700167 break;
168 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800169 } else {
Austin Schuhd0629b12017-03-22 22:37:16 -0700170 //hood_goal_ = 0.15;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800171 shooter_velocity_ = 0.0;
172 }
173
174 if (data.IsPressed(kExtra1)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700175 //turret_goal_ = -M_PI * 3.0 / 4.0;
176 turret_goal_ += 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800177 }
178 if (data.IsPressed(kExtra2)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700179 //turret_goal_ = M_PI * 3.0 / 4.0;
180 turret_goal_ -= 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800181 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700182 turret_goal_ = ::std::max(::std::min(turret_goal_, M_PI), -M_PI);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800183
184 fire_ = data.IsPressed(kFire) && shooter_velocity_ != 0.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700185 if (data.IsPressed(kVisionAlign)) {
186 fire_ = fire_ && superstructure_queue.status->turret.vision_tracking;
187 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800188
189 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
Austin Schuhd0629b12017-03-22 22:37:16 -0700190 if (data.IsPressed(kHang)) {
191 intake_goal_ = 0.23;
192 }
193
Campbell Crowley71b5f132017-02-18 13:16:08 -0800194 new_superstructure_goal->intake.distance = intake_goal_;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800195 new_superstructure_goal->intake.disable_intake = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800196 new_superstructure_goal->turret.angle = turret_goal_;
Austin Schuhd0629b12017-03-22 22:37:16 -0700197 new_superstructure_goal->turret.track = vision_track;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800198 new_superstructure_goal->hood.angle = hood_goal_;
199 new_superstructure_goal->shooter.angular_velocity = shooter_velocity_;
200
Austin Schuh6a8131b2017-04-08 15:39:22 -0700201 if (data.IsPressed(kIntakeUp)) {
Austin Schuhd6fa5e02017-04-12 20:52:17 -0700202 new_superstructure_goal->intake.gear_servo = 0.30;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700203 } else {
204 // Clamp the gear
Austin Schuhd6fa5e02017-04-12 20:52:17 -0700205 new_superstructure_goal->intake.gear_servo = 0.66;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700206 }
207
Campbell Crowley71b5f132017-02-18 13:16:08 -0800208 new_superstructure_goal->intake.profile_params.max_velocity = 0.50;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800209 new_superstructure_goal->hood.profile_params.max_velocity = 5.0;
210
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700211 new_superstructure_goal->intake.profile_params.max_acceleration = 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700212 if (vision_track) {
213 new_superstructure_goal->turret.profile_params.max_acceleration = 35.0;
214 new_superstructure_goal->turret.profile_params.max_velocity = 10.0;
215 } else {
216 new_superstructure_goal->turret.profile_params.max_acceleration = 15.0;
217 new_superstructure_goal->turret.profile_params.max_velocity = 6.0;
218 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800219 new_superstructure_goal->hood.profile_params.max_acceleration = 25.0;
220
Austin Schuh3028b1d2017-03-11 22:12:13 -0800221 new_superstructure_goal->intake.voltage_rollers = 0.0;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800222 new_superstructure_goal->lights_on = lights_on;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800223
Parker Schuh208a58d2017-04-12 20:51:38 -0700224 if (data.IsPressed(kVisionAlign) && vision_distance_shot_) {
225 new_superstructure_goal->use_vision_for_shots = true;
226 } else {
227 new_superstructure_goal->use_vision_for_shots = false;
228 }
229
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700230 if (superstructure_queue.status->intake.position >
231 superstructure_queue.status->intake.unprofiled_goal_position + 0.01) {
232 intake_accumulator_ = 10;
233 }
234 if (intake_accumulator_ > 0) {
235 --intake_accumulator_;
236 if (!superstructure_queue.status->intake.estopped) {
237 new_superstructure_goal->intake.voltage_rollers = 10.0;
238 }
239 }
240
Campbell Crowley71b5f132017-02-18 13:16:08 -0800241 if (data.IsPressed(kHang)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700242 new_superstructure_goal->intake.voltage_rollers = -10.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800243 new_superstructure_goal->intake.disable_intake = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700244 } else if (data.IsPressed(kIntakeIn) || data.IsPressed(kFire)) {
245 if (robot_velocity_ > 2.0) {
246 new_superstructure_goal->intake.voltage_rollers = 12.0;
247 } else {
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700248 new_superstructure_goal->intake.voltage_rollers = 11.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700249 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800250 } else if (data.IsPressed(kIntakeOut)) {
251 new_superstructure_goal->intake.voltage_rollers = -8.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800252 }
253 if (intake_goal_ < 0.1) {
254 new_superstructure_goal->intake.voltage_rollers =
255 ::std::min(8.0, new_superstructure_goal->intake.voltage_rollers);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800256 }
257
258 if (data.IsPressed(kReverseIndexer)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700259 new_superstructure_goal->indexer.voltage_rollers = -12.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800260 new_superstructure_goal->indexer.angular_velocity = 4.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800261 new_superstructure_goal->indexer.angular_velocity = 1.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800262 } else if (fire_) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700263 new_superstructure_goal->indexer.voltage_rollers = 12.0;
264 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700265 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700266 new_superstructure_goal->indexer.angular_velocity = -1.25 * M_PI;
Austin Schuhd0629b12017-03-22 22:37:16 -0700267 break;
268 case ShotDistance::MIDDLE_SHOT:
269 case ShotDistance::FAR_SHOT:
270 new_superstructure_goal->indexer.angular_velocity = -2.25 * M_PI;
271 break;
272 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800273 } else {
274 new_superstructure_goal->indexer.voltage_rollers = 0.0;
275 new_superstructure_goal->indexer.angular_velocity = 0.0;
276 }
277
278 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
279 if (!new_superstructure_goal.Send()) {
280 LOG(ERROR, "Sending superstructure goal failed.\n");
281 }
282 }
283
284 private:
Austin Schuh3028b1d2017-03-11 22:12:13 -0800285 void StartAuto() {
286 LOG(INFO, "Starting auto mode\n");
287
288 ::frc971::autonomous::AutonomousActionParams params;
289 ::frc971::autonomous::auto_mode.FetchLatest();
290 if (::frc971::autonomous::auto_mode.get() != nullptr) {
291 params.mode = ::frc971::autonomous::auto_mode->mode;
292 } else {
293 LOG(WARNING, "no auto mode values\n");
294 params.mode = 0;
295 }
296 action_queue_.EnqueueAction(
297 ::frc971::autonomous::MakeAutonomousAction(params));
298 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800299
300 void StopAuto() {
301 LOG(INFO, "Stopping auto mode\n");
302 action_queue_.CancelAllActions();
303 }
304
305 // Current goals to send to the robot.
306 double intake_goal_ = 0.0;
307 double turret_goal_ = 0.0;
308 double hood_goal_ = 0.3;
309 double shooter_velocity_ = 0.0;
310
Campbell Crowley71b5f132017-02-18 13:16:08 -0800311 bool was_running_ = false;
312 bool auto_running_ = false;
313
Parker Schuh208a58d2017-04-12 20:51:38 -0700314 bool vision_distance_shot_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800315
316 bool fire_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700317 double robot_velocity_ = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800318
319 ::aos::common::actions::ActionQueue action_queue_;
320};
321
322} // namespace joysticks
323} // namespace input
324} // namespace y2017
325
326int main() {
327 ::aos::Init(-1);
Austin Schuh3e45c752019-02-02 12:19:11 -0800328 ::aos::ShmEventLoop event_loop;
329 ::y2017::input::joysticks::Reader reader(&event_loop);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800330 reader.Run();
331 ::aos::Cleanup();
332}