blob: 33244be60270917fb2bdceb4aa0f494142658c0f [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
John Park398c74a2018-10-20 21:17:39 -07006#include "aos/init.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +00007#include "aos/input/joystick_input.h"
John Park33858a32018-09-28 23:05:48 -07008#include "aos/input/driver_station_data.h"
9#include "aos/logging/logging.h"
10#include "aos/util/log_interval.h"
11#include "aos/time/time.h"
12#include "aos/actions/actions.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000013
Austin Schuh3028b1d2017-03-11 22:12:13 -080014#include "frc971/autonomous/auto.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000015#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080016#include "frc971/queues/gyro.q.h"
17#include "y2016/actors/autonomous_actor.h"
18#include "y2016/actors/superstructure_actor.h"
19#include "y2016/actors/vision_align_actor.h"
20#include "y2016/constants.h"
21#include "y2016/control_loops/drivetrain/drivetrain_base.h"
Comran Morshed200dd4b2016-02-16 17:54:58 +000022#include "y2016/control_loops/shooter/shooter.q.h"
Austin Schuh45d07f62016-03-13 15:33:31 -070023#include "y2016/control_loops/superstructure/superstructure.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080024#include "y2016/control_loops/superstructure/superstructure.q.h"
Comran Morshedaa0573c2016-03-05 19:05:54 +000025#include "y2016/queues/ball_detector.q.h"
Austin Schuhadd6d792016-03-19 01:20:01 -070026#include "y2016/vision/vision.q.h"
Comran Morshed200dd4b2016-02-16 17:54:58 +000027
Comran Morshed9a9948c2016-01-16 15:58:04 +000028using ::frc971::control_loops::drivetrain_queue;
Comran Morshed200dd4b2016-02-16 17:54:58 +000029using ::y2016::control_loops::shooter::shooter_queue;
30using ::y2016::control_loops::superstructure_queue;
Comran Morshed9a9948c2016-01-16 15:58:04 +000031
32using ::aos::input::driver_station::ButtonLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000033using ::aos::input::driver_station::ControlBit;
Austin Schuh4ea06c12016-03-12 17:54:31 -080034using ::aos::input::driver_station::JoystickAxis;
35using ::aos::input::driver_station::POVLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000036
Comran Morshed6c6a0a92016-01-17 12:45:16 +000037namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000038namespace input {
39namespace joysticks {
40
Austin Schuh45d07f62016-03-13 15:33:31 -070041namespace {
42
43constexpr double kMaxIntakeAngleBeforeArmInterference = control_loops::
44 superstructure::CollisionAvoidance::kMaxIntakeAngleBeforeArmInterference;
45
46} // namespace
47
Comran Morshed9a9948c2016-01-16 15:58:04 +000048const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080049const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
Comran Morshed9a9948c2016-01-16 15:58:04 +000050const ButtonLocation kQuickTurn(1, 5);
51
Austin Schuh781cdcc2016-03-12 13:03:12 -080052const ButtonLocation kTurn1(1, 7);
53const ButtonLocation kTurn2(1, 11);
54
Comran Morshed200dd4b2016-02-16 17:54:58 +000055// Buttons on the lexan driver station to get things running on bring-up day.
Austin Schuh4ea06c12016-03-12 17:54:31 -080056const ButtonLocation kIntakeDown(3, 11);
57const POVLocation kFrontLong(3, 180);
58const POVLocation kBackLong(3, 0);
Austin Schuh45d07f62016-03-13 15:33:31 -070059const POVLocation kBackFender(3, 90);
60const POVLocation kFrontFender(3, 270);
Austin Schuh4ea06c12016-03-12 17:54:31 -080061const ButtonLocation kIntakeIn(3, 12);
Austin Schuh4ea06c12016-03-12 17:54:31 -080062const ButtonLocation kFire(3, 3);
Austin Schuh4ea06c12016-03-12 17:54:31 -080063const ButtonLocation kIntakeOut(3, 9);
Austin Schuh843412b2016-03-20 16:48:46 -070064const ButtonLocation kPortcullis(3, 7);
65const ButtonLocation kChevalDeFrise(3, 8);
Comran Morshed200dd4b2016-02-16 17:54:58 +000066
Austin Schuhadd6d792016-03-19 01:20:01 -070067const ButtonLocation kVisionAlign(3, 4);
Austin Schuh18799112016-03-16 22:09:54 -070068
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070069const ButtonLocation kExpand(3, 6);
70const ButtonLocation kWinch(3, 5);
71
Austin Schuh1bf8a212019-05-26 22:13:14 -070072// TODO(austin): ActionJoystickInput
Comran Morshed9a9948c2016-01-16 15:58:04 +000073class Reader : public ::aos::input::JoystickInput {
74 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080075 Reader(::aos::EventLoop *event_loop)
76 : ::aos::input::JoystickInput(event_loop),
Austin Schuh1bf8a212019-05-26 22:13:14 -070077 vision_status_fetcher_(
78 event_loop->MakeFetcher<::y2016::vision::VisionStatus>(
79 ".y2016.vision.vision_status")),
Austin Schuh3e45c752019-02-02 12:19:11 -080080 is_high_gear_(true),
Comran Morshed200dd4b2016-02-16 17:54:58 +000081 intake_goal_(0.0),
82 shoulder_goal_(M_PI / 2.0),
Austin Schuhd52df772016-03-19 15:38:41 -070083 wrist_goal_(0.0),
Austin Schuh1bf8a212019-05-26 22:13:14 -070084 dt_config_(control_loops::drivetrain::GetDrivetrainConfig()),
85 autonomous_action_factory_(
86 ::frc971::autonomous::BaseAutonomousActor::MakeFactory(event_loop)),
87 vision_align_action_factory_(
88 actors::VisionAlignActor::MakeFactory(event_loop)),
89 superstructure_action_factory_(
90 actors::SuperstructureActor::MakeFactory(event_loop)) {}
Comran Morshed9a9948c2016-01-16 15:58:04 +000091
Austin Schuh1bf8a212019-05-26 22:13:14 -070092 // TODO(austin): Move this to the ActionJoystickInput class.
Comran Morshed9a9948c2016-01-16 15:58:04 +000093 void RunIteration(const ::aos::input::driver_station::Data &data) override {
94 bool last_auto_running = auto_running_;
95 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
96 data.GetControlBit(ControlBit::kEnabled);
97 if (auto_running_ != last_auto_running) {
98 if (auto_running_) {
99 StartAuto();
100 } else {
101 StopAuto();
102 }
103 }
104
Austin Schuh94596dd2016-03-13 21:41:26 -0700105 if (!data.GetControlBit(ControlBit::kEnabled)) {
106 // If we are not enabled, reset the waiting for zero bit.
107 LOG(DEBUG, "Waiting for zero.\n");
108 waiting_for_zero_ = true;
109 is_high_gear_ = true;
110 }
111
Austin Schuhadd6d792016-03-19 01:20:01 -0700112 vision_valid_ = false;
113
Austin Schuh1bf8a212019-05-26 22:13:14 -0700114 vision_status_fetcher_.Fetch();
Austin Schuhadd6d792016-03-19 01:20:01 -0700115
Austin Schuh1bf8a212019-05-26 22:13:14 -0700116 if (vision_status_fetcher_.get()) {
117 vision_valid_ = (vision_status_fetcher_->left_image_valid &&
118 vision_status_fetcher_->right_image_valid);
119 last_angle_ = vision_status_fetcher_->horizontal_angle;
Austin Schuhadd6d792016-03-19 01:20:01 -0700120 }
121
Austin Schuh94596dd2016-03-13 21:41:26 -0700122 if (!auto_running_) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000123 HandleDrivetrain(data);
124 HandleTeleop(data);
125 }
Austin Schuh81d71db2016-03-15 20:56:24 -0700126
127 // Process any pending actions.
128 action_queue_.Tick();
129 was_running_ = action_queue_.Running();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000130 }
131
132 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
133 bool is_control_loop_driving = false;
Austin Schuh781cdcc2016-03-12 13:03:12 -0800134
Comran Morshed9a9948c2016-01-16 15:58:04 +0000135 const double wheel = -data.GetAxis(kSteeringWheel);
136 const double throttle = -data.GetAxis(kDriveThrottle);
Austin Schuhb0099642016-04-03 21:37:27 -0700137 drivetrain_queue.status.FetchLatest();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000138
Austin Schuh974cf652016-04-20 20:18:13 -0700139 if (data.IsPressed(kVisionAlign)) {
140 if (vision_valid_ && !vision_action_running_) {
141 actors::VisionAlignActionParams params;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700142 action_queue_.EnqueueAction(vision_align_action_factory_.Make(params));
Austin Schuh974cf652016-04-20 20:18:13 -0700143 vision_action_running_ = true;
144 LOG(INFO, "Starting vision align\n");
145 } else {
146 if (!vision_valid_) {
147 LOG(INFO, "Vision align but not valid\n");
148 }
149 }
Austin Schuh18799112016-03-16 22:09:54 -0700150 }
151
152 if (data.NegEdge(kVisionAlign)) {
153 action_queue_.CancelAllActions();
154 }
Austin Schuhadd6d792016-03-19 01:20:01 -0700155 if (!data.IsPressed(kVisionAlign)) {
156 vision_action_running_ = false;
157 }
Austin Schuh18799112016-03-16 22:09:54 -0700158
159 // Don't do any normal drivetrain stuff if vision is in charge.
Austin Schuhfef64ac2016-04-24 19:08:01 -0700160 if (vision_action_running_) {
Austin Schuh18799112016-03-16 22:09:54 -0700161 return;
162 }
163
Austin Schuh781cdcc2016-03-12 13:03:12 -0800164 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
Austin Schuh781cdcc2016-03-12 13:03:12 -0800165 if (drivetrain_queue.status.get()) {
Adam Snaider418bd822016-11-26 14:49:23 -0800166 left_goal_ = drivetrain_queue.status->estimated_left_position;
167 right_goal_ = drivetrain_queue.status->estimated_right_position;
Austin Schuh781cdcc2016-03-12 13:03:12 -0800168 }
169 }
170 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
171 is_control_loop_driving = true;
172 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000173 if (!drivetrain_queue.goal.MakeWithBuilder()
Austin Schuh2b1fce02018-03-02 20:05:20 -0800174 .wheel(wheel)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000175 .throttle(throttle)
176 .highgear(is_high_gear_)
177 .quickturn(data.IsPressed(kQuickTurn))
Austin Schuh78379ea2019-01-04 20:39:45 -0800178 .controller_type(is_control_loop_driving ? 1 : 0)
Adam Snaider418bd822016-11-26 14:49:23 -0800179 .left_goal(left_goal_ - wheel * 0.5 + throttle * 0.3)
180 .right_goal(right_goal_ + wheel * 0.5 + throttle * 0.3)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000181 .Send()) {
182 LOG(WARNING, "sending stick values failed\n");
183 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000184
Campbell Crowley5b27f022016-02-20 16:55:35 -0800185 if (data.PosEdge(kShiftLow)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000186 is_high_gear_ = false;
187 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000188
Campbell Crowley5b27f022016-02-20 16:55:35 -0800189 if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000190 is_high_gear_ = true;
191 }
192 }
193
Comran Morshed9a9948c2016-01-16 15:58:04 +0000194 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700195 float voltage_climber = 0.0;
Austin Schuh45d07f62016-03-13 15:33:31 -0700196 // Default the intake to up.
197 intake_goal_ = constants::Values::kIntakeRange.upper - 0.04;
198
199 bool force_lights_on = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000200 if (!data.GetControlBit(ControlBit::kEnabled)) {
201 action_queue_.CancelAllActions();
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000202 LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +0000203 }
204
Comran Morshed200dd4b2016-02-16 17:54:58 +0000205 superstructure_queue.status.FetchLatest();
206 if (!superstructure_queue.status.get()) {
207 LOG(ERROR, "Got no superstructure status packet.\n");
208 }
209
210 if (superstructure_queue.status.get() &&
211 superstructure_queue.status->zeroed) {
212 if (waiting_for_zero_) {
Austin Schuh94596dd2016-03-13 21:41:26 -0700213 LOG(DEBUG, "Zeroed! Starting teleop mode.\n");
Comran Morshed200dd4b2016-02-16 17:54:58 +0000214 waiting_for_zero_ = false;
215 }
216 } else {
217 waiting_for_zero_ = true;
218 }
219
Austin Schuhadd6d792016-03-19 01:20:01 -0700220 double intake_when_shooting = kMaxIntakeAngleBeforeArmInterference;
221 bool use_slow_profile = false;
222 if (vision_action_running_) {
223 use_slow_profile = true;
224 if (vision_valid_) {
225 intake_when_shooting -= 0.5;
226 }
227 }
228
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700229 if (data.IsPressed(kFrontLong)) {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800230 // Forwards shot
Austin Schuh3d79cc02016-03-20 21:08:53 -0700231 shoulder_goal_ = M_PI / 2.0 + 0.1;
Austin Schuh6a871ff2016-05-01 12:31:23 -0700232 wrist_goal_ = M_PI + 0.41 + 0.02 - 0.005;
Austin Schuhb0099642016-04-03 21:37:27 -0700233 if (drivetrain_queue.status.get()) {
Austin Schuh889fee82016-04-13 22:16:36 -0700234 wrist_goal_ += drivetrain_queue.status->ground_angle;
Austin Schuhb0099642016-04-03 21:37:27 -0700235 }
Austin Schuh4ea06c12016-03-12 17:54:31 -0800236 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700237 intake_goal_ = intake_when_shooting;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800238 } else if (data.IsPressed(kBackLong)) {
239 // Backwards shot
Austin Schuh3d79cc02016-03-20 21:08:53 -0700240 shoulder_goal_ = M_PI / 2.0 - 0.4;
Austin Schuhe153c5a2016-04-20 20:18:42 -0700241 wrist_goal_ = -0.62 - 0.02;
Austin Schuhb0099642016-04-03 21:37:27 -0700242 if (drivetrain_queue.status.get()) {
Austin Schuh889fee82016-04-13 22:16:36 -0700243 wrist_goal_ += drivetrain_queue.status->ground_angle;
Austin Schuhb0099642016-04-03 21:37:27 -0700244 }
Austin Schuh4ea06c12016-03-12 17:54:31 -0800245 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700246 intake_goal_ = intake_when_shooting;
Austin Schuh45d07f62016-03-13 15:33:31 -0700247 } else if (data.IsPressed(kBackFender)) {
Adam Snaidera3271fe2016-10-26 21:03:38 -0700248 // Backwards shot no IMU
249 shoulder_goal_ = M_PI / 2.0 - 0.4;
250 wrist_goal_ = -0.62 - 0.02;
251 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700252 intake_goal_ = intake_when_shooting;
Austin Schuh45d07f62016-03-13 15:33:31 -0700253 } else if (data.IsPressed(kFrontFender)) {
Adam Snaidera3271fe2016-10-26 21:03:38 -0700254 // Forwards shot no IMU
Austin Schuh6a871ff2016-05-01 12:31:23 -0700255 shoulder_goal_ = M_PI / 2.0 + 0.1;
Adam Snaidera3271fe2016-10-26 21:03:38 -0700256 wrist_goal_ = M_PI + 0.41 + 0.02 - 0.005;
Austin Schuh6a871ff2016-05-01 12:31:23 -0700257 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700258 intake_goal_ = intake_when_shooting;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700259 } else if (data.IsPressed(kExpand) || data.IsPressed(kWinch)) {
260 // Set the goals to the hanging position so when the actor finishes, we
261 // will still be at the right spot.
262 shoulder_goal_ = 1.2;
Austin Schuhfef64ac2016-04-24 19:08:01 -0700263 wrist_goal_ = 1.0;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700264 intake_goal_ = 0.0;
265 if (data.PosEdge(kExpand)) {
266 is_expanding_ = true;
267 actors::SuperstructureActionParams params;
268 params.partial_angle = 0.3;
269 params.delay_time = 0.7;
270 params.full_angle = shoulder_goal_;
Austin Schuhfef64ac2016-04-24 19:08:01 -0700271 params.shooter_angle = wrist_goal_;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700272 action_queue_.EnqueueAction(
273 superstructure_action_factory_.Make(params));
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700274 }
275 if (data.IsPressed(kWinch)) {
276 voltage_climber = 12.0;
277 }
Austin Schuhde802e92016-02-27 14:49:03 -0800278 } else {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800279 wrist_goal_ = 0.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800280 shoulder_goal_ = -0.010;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800281 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000282 }
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700283 if (data.NegEdge(kExpand) || voltage_climber > 1.0) {
284 is_expanding_ = false;
285 action_queue_.CancelAllActions();
286 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000287
Comran Morshedaa0573c2016-03-05 19:05:54 +0000288 bool ball_detected = false;
289 ::y2016::sensors::ball_detector.FetchLatest();
290 if (::y2016::sensors::ball_detector.get()) {
291 ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
292 }
293 if (data.PosEdge(kIntakeIn)) {
294 saw_ball_when_started_intaking_ = ball_detected;
295 }
296
Austin Schuh45d07f62016-03-13 15:33:31 -0700297 if (data.IsPressed(kIntakeIn)) {
298 is_intaking_ = (!ball_detected || saw_ball_when_started_intaking_);
299 if (ball_detected) {
300 force_lights_on = true;
301 }
302 } else {
303 is_intaking_ = false;
304 }
305
Austin Schuhd52df772016-03-19 15:38:41 -0700306 fire_ = false;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800307 if (data.IsPressed(kFire) && shooter_velocity_ != 0.0) {
Austin Schuhd52df772016-03-19 15:38:41 -0700308 if (data.IsPressed(kVisionAlign)) {
309 // Make sure that we are lined up.
310 drivetrain_queue.status.FetchLatest();
311 drivetrain_queue.goal.FetchLatest();
312 if (drivetrain_queue.status.get() && drivetrain_queue.goal.get()) {
313 const double left_goal = drivetrain_queue.goal->left_goal;
314 const double right_goal = drivetrain_queue.goal->right_goal;
315 const double left_current =
316 drivetrain_queue.status->estimated_left_position;
317 const double right_current =
318 drivetrain_queue.status->estimated_right_position;
319 const double left_velocity =
320 drivetrain_queue.status->estimated_left_velocity;
321 const double right_velocity =
322 drivetrain_queue.status->estimated_right_velocity;
323 if (vision_action_running_ && ::std::abs(last_angle_) < 0.02 &&
324 ::std::abs((left_goal - right_goal) -
325 (left_current - right_current)) /
326 dt_config_.robot_radius / 2.0 <
327 0.02 &&
328 ::std::abs(left_velocity - right_velocity) < 0.01) {
329 ++ready_to_fire_;
330 } else {
331 ready_to_fire_ = 0;
332 }
Austin Schuh3d79cc02016-03-20 21:08:53 -0700333 if (ready_to_fire_ > 9) {
Austin Schuhd52df772016-03-19 15:38:41 -0700334 fire_ = true;
335 }
336 }
337 } else {
338 fire_ = true;
339 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000340 }
341
Austin Schuh4ea06c12016-03-12 17:54:31 -0800342 is_outtaking_ = data.IsPressed(kIntakeOut);
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800343
Austin Schuhd52df772016-03-19 15:38:41 -0700344 if (is_intaking_ || is_outtaking_) {
345 recently_intaking_accumulator_ = 20;
346 }
347
348 if (data.IsPressed(kIntakeDown)) {
349 if (recently_intaking_accumulator_) {
350 intake_goal_ = 0.1;
351 } else {
352 intake_goal_ = -0.05;
353 }
354 }
355
356 if (recently_intaking_accumulator_ > 0) {
357 --recently_intaking_accumulator_;
358 }
359
Austin Schuh843412b2016-03-20 16:48:46 -0700360 if (data.IsPressed(kPortcullis)) {
361 traverse_unlatched_ = true;
362 traverse_down_ = true;
363 } else if (data.IsPressed(kChevalDeFrise)) {
364 traverse_unlatched_ = false;
365 traverse_down_ = true;
366 } else {
367 traverse_unlatched_ = true;
368 traverse_down_ = false;
369 }
370
Comran Morshed200dd4b2016-02-16 17:54:58 +0000371 if (!waiting_for_zero_) {
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700372 if (!is_expanding_) {
373 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
374 new_superstructure_goal->angle_intake = intake_goal_;
375 new_superstructure_goal->angle_shoulder = shoulder_goal_;
376 new_superstructure_goal->angle_wrist = wrist_goal_;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800377
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700378 new_superstructure_goal->max_angular_velocity_intake = 7.0;
379 new_superstructure_goal->max_angular_velocity_shoulder = 4.0;
380 new_superstructure_goal->max_angular_velocity_wrist = 10.0;
381 if (use_slow_profile) {
382 new_superstructure_goal->max_angular_acceleration_intake = 10.0;
383 } else {
384 new_superstructure_goal->max_angular_acceleration_intake = 40.0;
385 }
386 new_superstructure_goal->max_angular_acceleration_shoulder = 10.0;
387 if (shoulder_goal_ > 1.0) {
388 new_superstructure_goal->max_angular_acceleration_wrist = 45.0;
389 } else {
390 new_superstructure_goal->max_angular_acceleration_wrist = 25.0;
391 }
Austin Schuh3f0b1192016-03-12 13:03:56 -0800392
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700393 // Granny mode
394 /*
395 new_superstructure_goal->max_angular_velocity_intake = 0.2;
396 new_superstructure_goal->max_angular_velocity_shoulder = 0.2;
397 new_superstructure_goal->max_angular_velocity_wrist = 0.2;
398 new_superstructure_goal->max_angular_acceleration_intake = 1.0;
399 new_superstructure_goal->max_angular_acceleration_shoulder = 1.0;
400 new_superstructure_goal->max_angular_acceleration_wrist = 1.0;
401 */
402 if (is_intaking_) {
403 new_superstructure_goal->voltage_top_rollers = 12.0;
404 new_superstructure_goal->voltage_bottom_rollers = 12.0;
405 } else if (is_outtaking_) {
406 new_superstructure_goal->voltage_top_rollers = -12.0;
407 new_superstructure_goal->voltage_bottom_rollers = -7.0;
408 } else {
409 new_superstructure_goal->voltage_top_rollers = 0.0;
410 new_superstructure_goal->voltage_bottom_rollers = 0.0;
411 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000412
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700413 new_superstructure_goal->traverse_unlatched = traverse_unlatched_;
414 new_superstructure_goal->unfold_climber = false;
415 new_superstructure_goal->voltage_climber = voltage_climber;
416 new_superstructure_goal->traverse_down = traverse_down_;
417 new_superstructure_goal->force_intake = true;
Austin Schuh843412b2016-03-20 16:48:46 -0700418
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700419 if (!new_superstructure_goal.Send()) {
420 LOG(ERROR, "Sending superstructure goal failed.\n");
421 } else {
422 LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
423 intake_goal_, shoulder_goal_, wrist_goal_);
424 }
Austin Schuhadd6d792016-03-19 01:20:01 -0700425 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000426
Austin Schuhadd6d792016-03-19 01:20:01 -0700427 if (!shooter_queue.goal.MakeWithBuilder()
428 .angular_velocity(shooter_velocity_)
429 .clamp_open(is_intaking_ || is_outtaking_)
430 .push_to_shooter(fire_)
431 .force_lights_on(force_lights_on)
Austin Schuhb2c33382016-04-03 16:09:17 -0700432 .shooting_forwards(wrist_goal_ > 0)
Austin Schuhadd6d792016-03-19 01:20:01 -0700433 .Send()) {
434 LOG(ERROR, "Sending shooter goal failed.\n");
Comran Morshed200dd4b2016-02-16 17:54:58 +0000435 }
436 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000437 }
438
Comran Morshed9a9948c2016-01-16 15:58:04 +0000439 private:
440 void StartAuto() {
441 LOG(INFO, "Starting auto mode\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000442
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000443 ::frc971::autonomous::AutonomousActionParams params;
444 ::frc971::autonomous::auto_mode.FetchLatest();
445 if (::frc971::autonomous::auto_mode.get() != nullptr) {
446 params.mode = ::frc971::autonomous::auto_mode->mode;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700447 } else {
448 LOG(WARNING, "no auto mode values\n");
449 params.mode = 0;
450 }
Austin Schuh1bf8a212019-05-26 22:13:14 -0700451 action_queue_.EnqueueAction(autonomous_action_factory_.Make(params));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000452 }
453
454 void StopAuto() {
455 LOG(INFO, "Stopping auto mode\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000456 action_queue_.CancelAllActions();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000457 }
458
Austin Schuh1bf8a212019-05-26 22:13:14 -0700459 ::aos::Fetcher<::y2016::vision::VisionStatus> vision_status_fetcher_;
460
Comran Morshed9a9948c2016-01-16 15:58:04 +0000461 bool is_high_gear_;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000462 // Whatever these are set to are our default goals to send out after zeroing.
463 double intake_goal_;
464 double shoulder_goal_;
465 double wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800466 double shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000467
Adam Snaidera3271fe2016-10-26 21:03:38 -0700468 // Turning goals
Adam Snaider418bd822016-11-26 14:49:23 -0800469 double left_goal_;
470 double right_goal_;
Adam Snaidera3271fe2016-10-26 21:03:38 -0700471
Comran Morshed200dd4b2016-02-16 17:54:58 +0000472 bool was_running_ = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000473 bool auto_running_ = false;
474
Austin Schuh843412b2016-03-20 16:48:46 -0700475 bool traverse_unlatched_ = false;
476 bool traverse_down_ = false;
477
Comran Morshed200dd4b2016-02-16 17:54:58 +0000478 // If we're waiting for the subsystems to zero.
479 bool waiting_for_zero_ = true;
480
Comran Morshedaa0573c2016-03-05 19:05:54 +0000481 // If true, the ball was present when the intaking button was pressed.
482 bool saw_ball_when_started_intaking_ = false;
483
Austin Schuhde802e92016-02-27 14:49:03 -0800484 bool is_intaking_ = false;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800485 bool is_outtaking_ = false;
Austin Schuhde802e92016-02-27 14:49:03 -0800486 bool fire_ = false;
487
Austin Schuhadd6d792016-03-19 01:20:01 -0700488 bool vision_action_running_ = false;
489 bool vision_valid_ = false;
490
Austin Schuhd52df772016-03-19 15:38:41 -0700491 int recently_intaking_accumulator_ = 0;
492 double last_angle_ = 100;
493
494 int ready_to_fire_ = 0;
495
Comran Morshed9a9948c2016-01-16 15:58:04 +0000496 ::aos::common::actions::ActionQueue action_queue_;
497
Austin Schuhbcce26a2018-03-26 23:41:24 -0700498 const ::frc971::control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
Austin Schuhd52df772016-03-19 15:38:41 -0700499
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700500 bool is_expanding_ = false;
501
Austin Schuh1bf8a212019-05-26 22:13:14 -0700502 ::frc971::autonomous::BaseAutonomousActor::Factory autonomous_action_factory_;
503 actors::VisionAlignActor::Factory vision_align_action_factory_;
504 actors::SuperstructureActor::Factory superstructure_action_factory_;
505
Comran Morshed9a9948c2016-01-16 15:58:04 +0000506 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
Austin Schuh61bdc602016-12-04 19:10:10 -0800507 ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING,
Comran Morshed9a9948c2016-01-16 15:58:04 +0000508 "no drivetrain status");
509};
510
511} // namespace joysticks
512} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000513} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000514
515int main() {
516 ::aos::Init(-1);
Austin Schuh3e45c752019-02-02 12:19:11 -0800517 ::aos::ShmEventLoop event_loop;
518 ::y2016::input::joysticks::Reader reader(&event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000519 reader.Run();
520 ::aos::Cleanup();
521}