blob: bc0e319905d99f479b96978b5f06691af556eb67 [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 Schuh4b652c92019-05-27 13:22:27 -070080 ball_detector_fetcher_(
81 event_loop->MakeFetcher<::y2016::sensors::BallDetector>(
82 ".y2016.sensors.ball_detector")),
Austin Schuh3e45c752019-02-02 12:19:11 -080083 is_high_gear_(true),
Comran Morshed200dd4b2016-02-16 17:54:58 +000084 intake_goal_(0.0),
85 shoulder_goal_(M_PI / 2.0),
Austin Schuhd52df772016-03-19 15:38:41 -070086 wrist_goal_(0.0),
Austin Schuh1bf8a212019-05-26 22:13:14 -070087 dt_config_(control_loops::drivetrain::GetDrivetrainConfig()),
88 autonomous_action_factory_(
89 ::frc971::autonomous::BaseAutonomousActor::MakeFactory(event_loop)),
90 vision_align_action_factory_(
91 actors::VisionAlignActor::MakeFactory(event_loop)),
92 superstructure_action_factory_(
93 actors::SuperstructureActor::MakeFactory(event_loop)) {}
Comran Morshed9a9948c2016-01-16 15:58:04 +000094
Austin Schuh1bf8a212019-05-26 22:13:14 -070095 // TODO(austin): Move this to the ActionJoystickInput class.
Comran Morshed9a9948c2016-01-16 15:58:04 +000096 void RunIteration(const ::aos::input::driver_station::Data &data) override {
97 bool last_auto_running = auto_running_;
98 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
99 data.GetControlBit(ControlBit::kEnabled);
100 if (auto_running_ != last_auto_running) {
101 if (auto_running_) {
102 StartAuto();
103 } else {
104 StopAuto();
105 }
106 }
107
Austin Schuh94596dd2016-03-13 21:41:26 -0700108 if (!data.GetControlBit(ControlBit::kEnabled)) {
109 // If we are not enabled, reset the waiting for zero bit.
110 LOG(DEBUG, "Waiting for zero.\n");
111 waiting_for_zero_ = true;
112 is_high_gear_ = true;
113 }
114
Austin Schuhadd6d792016-03-19 01:20:01 -0700115 vision_valid_ = false;
116
Austin Schuh1bf8a212019-05-26 22:13:14 -0700117 vision_status_fetcher_.Fetch();
Austin Schuhadd6d792016-03-19 01:20:01 -0700118
Austin Schuh1bf8a212019-05-26 22:13:14 -0700119 if (vision_status_fetcher_.get()) {
120 vision_valid_ = (vision_status_fetcher_->left_image_valid &&
121 vision_status_fetcher_->right_image_valid);
122 last_angle_ = vision_status_fetcher_->horizontal_angle;
Austin Schuhadd6d792016-03-19 01:20:01 -0700123 }
124
Austin Schuh94596dd2016-03-13 21:41:26 -0700125 if (!auto_running_) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000126 HandleDrivetrain(data);
127 HandleTeleop(data);
128 }
Austin Schuh81d71db2016-03-15 20:56:24 -0700129
130 // Process any pending actions.
131 action_queue_.Tick();
132 was_running_ = action_queue_.Running();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000133 }
134
135 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
136 bool is_control_loop_driving = false;
Austin Schuh781cdcc2016-03-12 13:03:12 -0800137
Comran Morshed9a9948c2016-01-16 15:58:04 +0000138 const double wheel = -data.GetAxis(kSteeringWheel);
139 const double throttle = -data.GetAxis(kDriveThrottle);
Austin Schuhb0099642016-04-03 21:37:27 -0700140 drivetrain_queue.status.FetchLatest();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000141
Austin Schuh974cf652016-04-20 20:18:13 -0700142 if (data.IsPressed(kVisionAlign)) {
143 if (vision_valid_ && !vision_action_running_) {
144 actors::VisionAlignActionParams params;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700145 action_queue_.EnqueueAction(vision_align_action_factory_.Make(params));
Austin Schuh974cf652016-04-20 20:18:13 -0700146 vision_action_running_ = true;
147 LOG(INFO, "Starting vision align\n");
148 } else {
149 if (!vision_valid_) {
150 LOG(INFO, "Vision align but not valid\n");
151 }
152 }
Austin Schuh18799112016-03-16 22:09:54 -0700153 }
154
155 if (data.NegEdge(kVisionAlign)) {
156 action_queue_.CancelAllActions();
157 }
Austin Schuhadd6d792016-03-19 01:20:01 -0700158 if (!data.IsPressed(kVisionAlign)) {
159 vision_action_running_ = false;
160 }
Austin Schuh18799112016-03-16 22:09:54 -0700161
162 // Don't do any normal drivetrain stuff if vision is in charge.
Austin Schuhfef64ac2016-04-24 19:08:01 -0700163 if (vision_action_running_) {
Austin Schuh18799112016-03-16 22:09:54 -0700164 return;
165 }
166
Austin Schuh781cdcc2016-03-12 13:03:12 -0800167 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
Austin Schuh781cdcc2016-03-12 13:03:12 -0800168 if (drivetrain_queue.status.get()) {
Adam Snaider418bd822016-11-26 14:49:23 -0800169 left_goal_ = drivetrain_queue.status->estimated_left_position;
170 right_goal_ = drivetrain_queue.status->estimated_right_position;
Austin Schuh781cdcc2016-03-12 13:03:12 -0800171 }
172 }
173 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
174 is_control_loop_driving = true;
175 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000176 if (!drivetrain_queue.goal.MakeWithBuilder()
Austin Schuh2b1fce02018-03-02 20:05:20 -0800177 .wheel(wheel)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000178 .throttle(throttle)
179 .highgear(is_high_gear_)
180 .quickturn(data.IsPressed(kQuickTurn))
Austin Schuh78379ea2019-01-04 20:39:45 -0800181 .controller_type(is_control_loop_driving ? 1 : 0)
Adam Snaider418bd822016-11-26 14:49:23 -0800182 .left_goal(left_goal_ - wheel * 0.5 + throttle * 0.3)
183 .right_goal(right_goal_ + wheel * 0.5 + throttle * 0.3)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000184 .Send()) {
185 LOG(WARNING, "sending stick values failed\n");
186 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000187
Campbell Crowley5b27f022016-02-20 16:55:35 -0800188 if (data.PosEdge(kShiftLow)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000189 is_high_gear_ = false;
190 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000191
Campbell Crowley5b27f022016-02-20 16:55:35 -0800192 if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000193 is_high_gear_ = true;
194 }
195 }
196
Comran Morshed9a9948c2016-01-16 15:58:04 +0000197 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700198 float voltage_climber = 0.0;
Austin Schuh45d07f62016-03-13 15:33:31 -0700199 // Default the intake to up.
200 intake_goal_ = constants::Values::kIntakeRange.upper - 0.04;
201
202 bool force_lights_on = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000203 if (!data.GetControlBit(ControlBit::kEnabled)) {
204 action_queue_.CancelAllActions();
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000205 LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +0000206 }
207
Comran Morshed200dd4b2016-02-16 17:54:58 +0000208 superstructure_queue.status.FetchLatest();
209 if (!superstructure_queue.status.get()) {
210 LOG(ERROR, "Got no superstructure status packet.\n");
211 }
212
213 if (superstructure_queue.status.get() &&
214 superstructure_queue.status->zeroed) {
215 if (waiting_for_zero_) {
Austin Schuh94596dd2016-03-13 21:41:26 -0700216 LOG(DEBUG, "Zeroed! Starting teleop mode.\n");
Comran Morshed200dd4b2016-02-16 17:54:58 +0000217 waiting_for_zero_ = false;
218 }
219 } else {
220 waiting_for_zero_ = true;
221 }
222
Austin Schuhadd6d792016-03-19 01:20:01 -0700223 double intake_when_shooting = kMaxIntakeAngleBeforeArmInterference;
224 bool use_slow_profile = false;
225 if (vision_action_running_) {
226 use_slow_profile = true;
227 if (vision_valid_) {
228 intake_when_shooting -= 0.5;
229 }
230 }
231
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700232 if (data.IsPressed(kFrontLong)) {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800233 // Forwards shot
Austin Schuh3d79cc02016-03-20 21:08:53 -0700234 shoulder_goal_ = M_PI / 2.0 + 0.1;
Austin Schuh6a871ff2016-05-01 12:31:23 -0700235 wrist_goal_ = M_PI + 0.41 + 0.02 - 0.005;
Austin Schuhb0099642016-04-03 21:37:27 -0700236 if (drivetrain_queue.status.get()) {
Austin Schuh889fee82016-04-13 22:16:36 -0700237 wrist_goal_ += drivetrain_queue.status->ground_angle;
Austin Schuhb0099642016-04-03 21:37:27 -0700238 }
Austin Schuh4ea06c12016-03-12 17:54:31 -0800239 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700240 intake_goal_ = intake_when_shooting;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800241 } else if (data.IsPressed(kBackLong)) {
242 // Backwards shot
Austin Schuh3d79cc02016-03-20 21:08:53 -0700243 shoulder_goal_ = M_PI / 2.0 - 0.4;
Austin Schuhe153c5a2016-04-20 20:18:42 -0700244 wrist_goal_ = -0.62 - 0.02;
Austin Schuhb0099642016-04-03 21:37:27 -0700245 if (drivetrain_queue.status.get()) {
Austin Schuh889fee82016-04-13 22:16:36 -0700246 wrist_goal_ += drivetrain_queue.status->ground_angle;
Austin Schuhb0099642016-04-03 21:37:27 -0700247 }
Austin Schuh4ea06c12016-03-12 17:54:31 -0800248 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700249 intake_goal_ = intake_when_shooting;
Austin Schuh45d07f62016-03-13 15:33:31 -0700250 } else if (data.IsPressed(kBackFender)) {
Adam Snaidera3271fe2016-10-26 21:03:38 -0700251 // Backwards shot no IMU
252 shoulder_goal_ = M_PI / 2.0 - 0.4;
253 wrist_goal_ = -0.62 - 0.02;
254 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700255 intake_goal_ = intake_when_shooting;
Austin Schuh45d07f62016-03-13 15:33:31 -0700256 } else if (data.IsPressed(kFrontFender)) {
Adam Snaidera3271fe2016-10-26 21:03:38 -0700257 // Forwards shot no IMU
Austin Schuh6a871ff2016-05-01 12:31:23 -0700258 shoulder_goal_ = M_PI / 2.0 + 0.1;
Adam Snaidera3271fe2016-10-26 21:03:38 -0700259 wrist_goal_ = M_PI + 0.41 + 0.02 - 0.005;
Austin Schuh6a871ff2016-05-01 12:31:23 -0700260 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700261 intake_goal_ = intake_when_shooting;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700262 } else if (data.IsPressed(kExpand) || data.IsPressed(kWinch)) {
263 // Set the goals to the hanging position so when the actor finishes, we
264 // will still be at the right spot.
265 shoulder_goal_ = 1.2;
Austin Schuhfef64ac2016-04-24 19:08:01 -0700266 wrist_goal_ = 1.0;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700267 intake_goal_ = 0.0;
268 if (data.PosEdge(kExpand)) {
269 is_expanding_ = true;
270 actors::SuperstructureActionParams params;
271 params.partial_angle = 0.3;
272 params.delay_time = 0.7;
273 params.full_angle = shoulder_goal_;
Austin Schuhfef64ac2016-04-24 19:08:01 -0700274 params.shooter_angle = wrist_goal_;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700275 action_queue_.EnqueueAction(
276 superstructure_action_factory_.Make(params));
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700277 }
278 if (data.IsPressed(kWinch)) {
279 voltage_climber = 12.0;
280 }
Austin Schuhde802e92016-02-27 14:49:03 -0800281 } else {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800282 wrist_goal_ = 0.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800283 shoulder_goal_ = -0.010;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800284 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000285 }
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700286 if (data.NegEdge(kExpand) || voltage_climber > 1.0) {
287 is_expanding_ = false;
288 action_queue_.CancelAllActions();
289 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000290
Comran Morshedaa0573c2016-03-05 19:05:54 +0000291 bool ball_detected = false;
Austin Schuh4b652c92019-05-27 13:22:27 -0700292 ball_detector_fetcher_.Fetch();
293 if (ball_detector_fetcher_.get()) {
294 ball_detected = ball_detector_fetcher_->voltage > 2.5;
Comran Morshedaa0573c2016-03-05 19:05:54 +0000295 }
296 if (data.PosEdge(kIntakeIn)) {
297 saw_ball_when_started_intaking_ = ball_detected;
298 }
299
Austin Schuh45d07f62016-03-13 15:33:31 -0700300 if (data.IsPressed(kIntakeIn)) {
301 is_intaking_ = (!ball_detected || saw_ball_when_started_intaking_);
302 if (ball_detected) {
303 force_lights_on = true;
304 }
305 } else {
306 is_intaking_ = false;
307 }
308
Austin Schuhd52df772016-03-19 15:38:41 -0700309 fire_ = false;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800310 if (data.IsPressed(kFire) && shooter_velocity_ != 0.0) {
Austin Schuhd52df772016-03-19 15:38:41 -0700311 if (data.IsPressed(kVisionAlign)) {
312 // Make sure that we are lined up.
313 drivetrain_queue.status.FetchLatest();
314 drivetrain_queue.goal.FetchLatest();
315 if (drivetrain_queue.status.get() && drivetrain_queue.goal.get()) {
316 const double left_goal = drivetrain_queue.goal->left_goal;
317 const double right_goal = drivetrain_queue.goal->right_goal;
318 const double left_current =
319 drivetrain_queue.status->estimated_left_position;
320 const double right_current =
321 drivetrain_queue.status->estimated_right_position;
322 const double left_velocity =
323 drivetrain_queue.status->estimated_left_velocity;
324 const double right_velocity =
325 drivetrain_queue.status->estimated_right_velocity;
326 if (vision_action_running_ && ::std::abs(last_angle_) < 0.02 &&
327 ::std::abs((left_goal - right_goal) -
328 (left_current - right_current)) /
329 dt_config_.robot_radius / 2.0 <
330 0.02 &&
331 ::std::abs(left_velocity - right_velocity) < 0.01) {
332 ++ready_to_fire_;
333 } else {
334 ready_to_fire_ = 0;
335 }
Austin Schuh3d79cc02016-03-20 21:08:53 -0700336 if (ready_to_fire_ > 9) {
Austin Schuhd52df772016-03-19 15:38:41 -0700337 fire_ = true;
338 }
339 }
340 } else {
341 fire_ = true;
342 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000343 }
344
Austin Schuh4ea06c12016-03-12 17:54:31 -0800345 is_outtaking_ = data.IsPressed(kIntakeOut);
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800346
Austin Schuhd52df772016-03-19 15:38:41 -0700347 if (is_intaking_ || is_outtaking_) {
348 recently_intaking_accumulator_ = 20;
349 }
350
351 if (data.IsPressed(kIntakeDown)) {
352 if (recently_intaking_accumulator_) {
353 intake_goal_ = 0.1;
354 } else {
355 intake_goal_ = -0.05;
356 }
357 }
358
359 if (recently_intaking_accumulator_ > 0) {
360 --recently_intaking_accumulator_;
361 }
362
Austin Schuh843412b2016-03-20 16:48:46 -0700363 if (data.IsPressed(kPortcullis)) {
364 traverse_unlatched_ = true;
365 traverse_down_ = true;
366 } else if (data.IsPressed(kChevalDeFrise)) {
367 traverse_unlatched_ = false;
368 traverse_down_ = true;
369 } else {
370 traverse_unlatched_ = true;
371 traverse_down_ = false;
372 }
373
Comran Morshed200dd4b2016-02-16 17:54:58 +0000374 if (!waiting_for_zero_) {
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700375 if (!is_expanding_) {
376 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
377 new_superstructure_goal->angle_intake = intake_goal_;
378 new_superstructure_goal->angle_shoulder = shoulder_goal_;
379 new_superstructure_goal->angle_wrist = wrist_goal_;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800380
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700381 new_superstructure_goal->max_angular_velocity_intake = 7.0;
382 new_superstructure_goal->max_angular_velocity_shoulder = 4.0;
383 new_superstructure_goal->max_angular_velocity_wrist = 10.0;
384 if (use_slow_profile) {
385 new_superstructure_goal->max_angular_acceleration_intake = 10.0;
386 } else {
387 new_superstructure_goal->max_angular_acceleration_intake = 40.0;
388 }
389 new_superstructure_goal->max_angular_acceleration_shoulder = 10.0;
390 if (shoulder_goal_ > 1.0) {
391 new_superstructure_goal->max_angular_acceleration_wrist = 45.0;
392 } else {
393 new_superstructure_goal->max_angular_acceleration_wrist = 25.0;
394 }
Austin Schuh3f0b1192016-03-12 13:03:56 -0800395
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700396 // Granny mode
397 /*
398 new_superstructure_goal->max_angular_velocity_intake = 0.2;
399 new_superstructure_goal->max_angular_velocity_shoulder = 0.2;
400 new_superstructure_goal->max_angular_velocity_wrist = 0.2;
401 new_superstructure_goal->max_angular_acceleration_intake = 1.0;
402 new_superstructure_goal->max_angular_acceleration_shoulder = 1.0;
403 new_superstructure_goal->max_angular_acceleration_wrist = 1.0;
404 */
405 if (is_intaking_) {
406 new_superstructure_goal->voltage_top_rollers = 12.0;
407 new_superstructure_goal->voltage_bottom_rollers = 12.0;
408 } else if (is_outtaking_) {
409 new_superstructure_goal->voltage_top_rollers = -12.0;
410 new_superstructure_goal->voltage_bottom_rollers = -7.0;
411 } else {
412 new_superstructure_goal->voltage_top_rollers = 0.0;
413 new_superstructure_goal->voltage_bottom_rollers = 0.0;
414 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000415
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700416 new_superstructure_goal->traverse_unlatched = traverse_unlatched_;
417 new_superstructure_goal->unfold_climber = false;
418 new_superstructure_goal->voltage_climber = voltage_climber;
419 new_superstructure_goal->traverse_down = traverse_down_;
420 new_superstructure_goal->force_intake = true;
Austin Schuh843412b2016-03-20 16:48:46 -0700421
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700422 if (!new_superstructure_goal.Send()) {
423 LOG(ERROR, "Sending superstructure goal failed.\n");
424 } else {
425 LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
426 intake_goal_, shoulder_goal_, wrist_goal_);
427 }
Austin Schuhadd6d792016-03-19 01:20:01 -0700428 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000429
Austin Schuhadd6d792016-03-19 01:20:01 -0700430 if (!shooter_queue.goal.MakeWithBuilder()
431 .angular_velocity(shooter_velocity_)
432 .clamp_open(is_intaking_ || is_outtaking_)
433 .push_to_shooter(fire_)
434 .force_lights_on(force_lights_on)
Austin Schuhb2c33382016-04-03 16:09:17 -0700435 .shooting_forwards(wrist_goal_ > 0)
Austin Schuhadd6d792016-03-19 01:20:01 -0700436 .Send()) {
437 LOG(ERROR, "Sending shooter goal failed.\n");
Comran Morshed200dd4b2016-02-16 17:54:58 +0000438 }
439 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000440 }
441
Comran Morshed9a9948c2016-01-16 15:58:04 +0000442 private:
443 void StartAuto() {
444 LOG(INFO, "Starting auto mode\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000445
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000446 ::frc971::autonomous::AutonomousActionParams params;
447 ::frc971::autonomous::auto_mode.FetchLatest();
448 if (::frc971::autonomous::auto_mode.get() != nullptr) {
449 params.mode = ::frc971::autonomous::auto_mode->mode;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700450 } else {
451 LOG(WARNING, "no auto mode values\n");
452 params.mode = 0;
453 }
Austin Schuh1bf8a212019-05-26 22:13:14 -0700454 action_queue_.EnqueueAction(autonomous_action_factory_.Make(params));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000455 }
456
457 void StopAuto() {
458 LOG(INFO, "Stopping auto mode\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000459 action_queue_.CancelAllActions();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000460 }
461
Austin Schuh1bf8a212019-05-26 22:13:14 -0700462 ::aos::Fetcher<::y2016::vision::VisionStatus> vision_status_fetcher_;
Austin Schuh4b652c92019-05-27 13:22:27 -0700463 ::aos::Fetcher<::y2016::sensors::BallDetector> ball_detector_fetcher_;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700464
Comran Morshed9a9948c2016-01-16 15:58:04 +0000465 bool is_high_gear_;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000466 // Whatever these are set to are our default goals to send out after zeroing.
467 double intake_goal_;
468 double shoulder_goal_;
469 double wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800470 double shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000471
Adam Snaidera3271fe2016-10-26 21:03:38 -0700472 // Turning goals
Adam Snaider418bd822016-11-26 14:49:23 -0800473 double left_goal_;
474 double right_goal_;
Adam Snaidera3271fe2016-10-26 21:03:38 -0700475
Comran Morshed200dd4b2016-02-16 17:54:58 +0000476 bool was_running_ = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000477 bool auto_running_ = false;
478
Austin Schuh843412b2016-03-20 16:48:46 -0700479 bool traverse_unlatched_ = false;
480 bool traverse_down_ = false;
481
Comran Morshed200dd4b2016-02-16 17:54:58 +0000482 // If we're waiting for the subsystems to zero.
483 bool waiting_for_zero_ = true;
484
Comran Morshedaa0573c2016-03-05 19:05:54 +0000485 // If true, the ball was present when the intaking button was pressed.
486 bool saw_ball_when_started_intaking_ = false;
487
Austin Schuhde802e92016-02-27 14:49:03 -0800488 bool is_intaking_ = false;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800489 bool is_outtaking_ = false;
Austin Schuhde802e92016-02-27 14:49:03 -0800490 bool fire_ = false;
491
Austin Schuhadd6d792016-03-19 01:20:01 -0700492 bool vision_action_running_ = false;
493 bool vision_valid_ = false;
494
Austin Schuhd52df772016-03-19 15:38:41 -0700495 int recently_intaking_accumulator_ = 0;
496 double last_angle_ = 100;
497
498 int ready_to_fire_ = 0;
499
Comran Morshed9a9948c2016-01-16 15:58:04 +0000500 ::aos::common::actions::ActionQueue action_queue_;
501
Austin Schuhbcce26a2018-03-26 23:41:24 -0700502 const ::frc971::control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
Austin Schuhd52df772016-03-19 15:38:41 -0700503
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700504 bool is_expanding_ = false;
505
Austin Schuh1bf8a212019-05-26 22:13:14 -0700506 ::frc971::autonomous::BaseAutonomousActor::Factory autonomous_action_factory_;
507 actors::VisionAlignActor::Factory vision_align_action_factory_;
508 actors::SuperstructureActor::Factory superstructure_action_factory_;
509
Comran Morshed9a9948c2016-01-16 15:58:04 +0000510 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
Austin Schuh61bdc602016-12-04 19:10:10 -0800511 ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING,
Comran Morshed9a9948c2016-01-16 15:58:04 +0000512 "no drivetrain status");
513};
514
515} // namespace joysticks
516} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000517} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000518
519int main() {
520 ::aos::Init(-1);
Austin Schuh3e45c752019-02-02 12:19:11 -0800521 ::aos::ShmEventLoop event_loop;
522 ::y2016::input::joysticks::Reader reader(&event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000523 reader.Run();
524 ::aos::Cleanup();
525}