blob: 8ef6c7c62b539d1d52452c219dab1861d7adbd65 [file] [log] [blame]
Brian Silverman756f9ff2014-01-17 23:40:23 -08001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <math.h>
5
6#include "aos/linux_code/init.h"
7#include "aos/prime/input/joystick_input.h"
Austin Schuh06cbbf12014-02-22 02:07:31 -08008#include "aos/common/input/driver_station_data.h"
Brian Silverman756f9ff2014-01-17 23:40:23 -08009#include "aos/common/logging/logging.h"
Brian Silvermana379f002014-03-22 19:34:53 -070010#include "aos/common/util/log_interval.h"
11#include "aos/common/time.h"
Brian Silverman756f9ff2014-01-17 23:40:23 -080012
13#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh5d8c5e72014-03-07 20:24:34 -080014#include "frc971/constants.h"
Brian Silverman6bf0d3c2014-03-08 12:52:54 -080015#include "frc971/queues/other_sensors.q.h"
Brian Silverman756f9ff2014-01-17 23:40:23 -080016#include "frc971/autonomous/auto.q.h"
Brian Silvermanfac5c292014-02-17 15:26:57 -080017#include "frc971/control_loops/claw/claw.q.h"
18#include "frc971/control_loops/shooter/shooter.q.h"
Ben Fredricksonaa450452014-03-01 09:41:18 +000019#include "frc971/actions/shoot_action.q.h"
Austin Schuh80ff2e12014-03-08 12:06:19 -080020#include "frc971/actions/action_client.h"
21#include "frc971/actions/catch_action.q.h"
22#include "frc971/actions/shoot_action.h"
Brian Silverman756f9ff2014-01-17 23:40:23 -080023
24using ::frc971::control_loops::drivetrain;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -080025using ::frc971::sensors::gyro_reading;
Brian Silverman756f9ff2014-01-17 23:40:23 -080026
27using ::aos::input::driver_station::ButtonLocation;
28using ::aos::input::driver_station::JoystickAxis;
29using ::aos::input::driver_station::ControlBit;
30
31namespace frc971 {
32namespace input {
33namespace joysticks {
34
35const ButtonLocation kDriveControlLoopEnable1(1, 7),
36 kDriveControlLoopEnable2(1, 11);
37const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
38const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
39const ButtonLocation kQuickTurn(1, 5);
Austin Schuh58d23682014-02-23 01:39:50 -080040
Austin Schuh80ff2e12014-03-08 12:06:19 -080041const ButtonLocation kCatch(3, 10);
42
43const ButtonLocation kFire(3, 9);
Brian6faa7822014-03-31 18:00:28 -070044const ButtonLocation kUnload(1, 4);
45const ButtonLocation kReload(1, 2);
Austin Schuh58d23682014-02-23 01:39:50 -080046
Austin Schuh80ff2e12014-03-08 12:06:19 -080047const ButtonLocation kRollersOut(3, 8);
48const ButtonLocation kRollersIn(3, 3);
Austin Schuh58d23682014-02-23 01:39:50 -080049
Austin Schuh80ff2e12014-03-08 12:06:19 -080050const ButtonLocation kTuck(3, 4);
51const ButtonLocation kIntakePosition(3, 5);
52const ButtonLocation kIntakeOpenPosition(3, 11);
Brian6faa7822014-03-31 18:00:28 -070053const ButtonLocation kVerticalTuck(2, 6);
Austin Schuh80ff2e12014-03-08 12:06:19 -080054const JoystickAxis kFlipRobot(3, 3);
Austin Schuh58d23682014-02-23 01:39:50 -080055
Austin Schuh80ff2e12014-03-08 12:06:19 -080056const ButtonLocation kLongShot(3, 7);
Brian Silverman4051c6f2014-07-19 16:57:37 -070057const ButtonLocation kCloseShot(3, 6);
58const ButtonLocation kFenderShot(3, 2);
Brian6faa7822014-03-31 18:00:28 -070059const ButtonLocation kTrussShot(2, 11);
60const ButtonLocation kHumanPlayerShot(3, 1);
61
62const ButtonLocation kUserLeft(2, 7);
63const ButtonLocation kUserRight(2, 10);
Austin Schuh58d23682014-02-23 01:39:50 -080064
Brian Silverman18f6e642014-03-13 18:52:47 -070065const JoystickAxis kAdjustClawGoal(3, 2);
66const JoystickAxis kAdjustClawSeparation(3, 1);
67
Austin Schuh58d23682014-02-23 01:39:50 -080068struct ClawGoal {
69 double angle;
70 double separation;
71};
72
Austin Schuh5d8c5e72014-03-07 20:24:34 -080073struct ShotGoal {
74 ClawGoal claw;
75 double shot_power;
Brian Silvermana379f002014-03-22 19:34:53 -070076 double velocity_compensation;
Austin Schuh5d8c5e72014-03-07 20:24:34 -080077 double intake_power;
78};
79
Brian Silverman545f2ad2014-03-14 12:31:42 -070080const double kIntakePower = 4.0;
Brian Silverman255b5c12014-04-25 09:21:40 -050081// In case we have to quickly adjust it.
Brian Silvermanb3cf0ef2014-03-22 12:45:55 -070082const double kGrabSeparation = 0;
Brian Silverman545f2ad2014-03-14 12:31:42 -070083const double kShootSeparation = 0.11 + kGrabSeparation;
84
Austin Schuh58d23682014-02-23 01:39:50 -080085const ClawGoal kTuckGoal = {-2.273474, -0.749484};
Brian6faa7822014-03-31 18:00:28 -070086const ClawGoal kVerticalTuckGoal = {0, kGrabSeparation};
Brian Silverman63ec7502014-03-30 18:09:10 -070087const ClawGoal kIntakeGoal = {-2.24, kGrabSeparation};
88const ClawGoal kIntakeOpenGoal = {-2.0, 1.1};
Austin Schuh58d23682014-02-23 01:39:50 -080089
Austin Schuh80ff2e12014-03-08 12:06:19 -080090// TODO(austin): Tune these by hand...
91const ClawGoal kFlippedTuckGoal = {2.733474, -0.75};
Brian Silverman545f2ad2014-03-14 12:31:42 -070092const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation};
Austin Schuh80ff2e12014-03-08 12:06:19 -080093const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0};
94
Brian Silverman255b5c12014-04-25 09:21:40 -050095// 34" between near edge of colored line and rear edge of bumper.
96// Only works running?
Austin Schuh5d8c5e72014-03-07 20:24:34 -080097const ShotGoal kLongShotGoal = {
Brian Silverman55d7f572014-04-17 14:33:54 -070098 {-1.08, kShootSeparation}, 145, 0.04, kIntakePower};
Brian Silverman255b5c12014-04-25 09:21:40 -050099// old 34" {-1.06, kShootSeparation}, 140, 0.04, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -0800100const ShotGoal kFlippedLongShotGoal = {
Brian Silverman55d7f572014-04-17 14:33:54 -0700101 {0.96, kShootSeparation}, 145, 0.09, kIntakePower};
Brian Silverman255b5c12014-04-25 09:21:40 -0500102// old 34" {0.96, kShootSeparation}, 140, 0.09, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700103
Brian Silverman255b5c12014-04-25 09:21:40 -0500104// 78" between near edge of colored line and rear edge of bumper.
Brian Silverman4051c6f2014-07-19 16:57:37 -0700105const ShotGoal kCloseShotGoal = {
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700106 {-0.95, kShootSeparation}, 105, 0.2, kIntakePower};
107// 3/4" plunger {-0.90, kShootSeparation}, 105, 0.2, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -0800108const ShotGoal kFlippedMediumShotGoal = {
Brian Silverman255b5c12014-04-25 09:21:40 -0500109 {0.865, kShootSeparation}, 120, 0.2, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700110// 3/4" plunger {0.80, kShootSeparation}, 105, 0.2, kIntakePower};
111
Brian Silverman255b5c12014-04-25 09:21:40 -0500112// Shot from the fender.
Brian Silverman4051c6f2014-07-19 16:57:37 -0700113const ShotGoal kFenderShotGoal = {
Brian Silverman55d7f572014-04-17 14:33:54 -0700114 {-0.68, kShootSeparation}, 115.0, 0.4, kIntakePower};
115const ShotGoal kFlippedShortShotGoal = {
Brian Silverman255b5c12014-04-25 09:21:40 -0500116 {0.63, kShootSeparation}, 115.0, 0.4, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700117
Brian Silverman63ec7502014-03-30 18:09:10 -0700118const ShotGoal kHumanShotGoal = {
119 {-0.90, kShootSeparation}, 140, 0.04, kIntakePower};
Brian Silverman73c48392014-04-05 07:07:41 -0700120const ShotGoal kFlippedHumanShotGoal = {
121 {0.90, kShootSeparation}, 140, 0, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700122const ShotGoal kTrussShotGoal = {
Brian Silverman8d5dda42014-04-06 15:59:49 -0700123 {-0.68, kShootSeparation}, 83.0, 0.4, kIntakePower};
Brian Silverman73c48392014-04-05 07:07:41 -0700124const ShotGoal kFlippedTrussShotGoal = {
Brian Silverman8d5dda42014-04-06 15:59:49 -0700125 {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower};
126
Brian Silverman8cd51202014-07-19 16:58:08 -0700127const ShotGoal kFlippedDemoShotGoal = {
128 {1.0, kShootSeparation}, 65.0, 0.0, kIntakePower};
129const ShotGoal kDemoShotGoal = {
130 {-1.0, kShootSeparation}, 50.0, 0.0, kIntakePower};
131
Brian Silverman8d5dda42014-04-06 15:59:49 -0700132const ClawGoal k254PassGoal = {-1.95, kGrabSeparation};
133const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation};
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800134
135// Makes a new ShootAction action.
Austin Schuh80ff2e12014-03-08 12:06:19 -0800136::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>
137MakeCatchAction() {
138 return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>(
139 new TypedAction< ::frc971::actions::CatchActionGroup>(
140 &::frc971::actions::catch_action));
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800141}
142
143// A queue which queues Actions and cancels them.
144class ActionQueue {
145 public:
146 // Queues up an action for sending.
147 void QueueAction(::std::unique_ptr<Action> action) {
148 if (current_action_) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800149 LOG(INFO, "Queueing action, canceling prior\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800150 current_action_->Cancel();
151 next_action_ = ::std::move(action);
152 } else {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800153 LOG(INFO, "Queueing action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800154 current_action_ = ::std::move(action);
155 current_action_->Start();
156 }
157 }
158
159 // Cancels the current action, and runs the next one when the current one has
160 // finished.
161 void CancelCurrentAction() {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800162 LOG(INFO, "Canceling current action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800163 if (current_action_) {
164 current_action_->Cancel();
165 }
166 }
167
168 // Cancels all running actions.
169 void CancelAllActions() {
Brian Silverman101b9642014-03-08 12:45:16 -0800170 LOG(DEBUG, "Cancelling all actions\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800171 if (current_action_) {
172 current_action_->Cancel();
173 }
174 next_action_.reset();
175 }
176
177 // Runs the next action when the current one is finished running.
178 void Tick() {
179 if (current_action_) {
180 if (!current_action_->Running()) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800181 LOG(INFO, "Action is done.\n");
182 current_action_ = ::std::move(next_action_);
183 if (current_action_) {
184 LOG(INFO, "Running next action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800185 current_action_->Start();
186 }
187 }
188 }
189 }
190
191 // Returns true if any action is running or could be running.
192 // For a one cycle faster response, call Tick before running this.
Brian Silverman2c1e0342014-04-11 16:15:01 -0700193 bool Running() { return static_cast<bool>(current_action_); }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800194
195 private:
196 ::std::unique_ptr<Action> current_action_;
197 ::std::unique_ptr<Action> next_action_;
198};
199
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800200
Brian Silverman756f9ff2014-01-17 23:40:23 -0800201class Reader : public ::aos::input::JoystickInput {
202 public:
Austin Schuh58d23682014-02-23 01:39:50 -0800203 Reader()
Brian Silvermane3a277a2014-04-13 14:56:57 -0700204 : is_high_gear_(false),
Austin Schuh9cb836e2014-02-23 19:25:55 -0800205 shot_power_(80.0),
Austin Schuh58d23682014-02-23 01:39:50 -0800206 goal_angle_(0.0),
Brian Silverman545f2ad2014-03-14 12:31:42 -0700207 separation_angle_(kGrabSeparation),
Brian Silvermana379f002014-03-22 19:34:53 -0700208 velocity_compensation_(0.0),
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800209 intake_power_(0.0),
210 was_running_(false) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -0800211
212 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Brian Silverman756f9ff2014-01-17 23:40:23 -0800213 if (data.GetControlBit(ControlBit::kAutonomous)) {
214 if (data.PosEdge(ControlBit::kEnabled)){
215 LOG(INFO, "Starting auto mode\n");
216 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800217 .run_auto(true)
218 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800219 } else if (data.NegEdge(ControlBit::kEnabled)) {
220 LOG(INFO, "Stopping auto mode\n");
221 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800222 .run_auto(false)
223 .Send();
Brian Silverman2c764f02014-04-25 09:21:21 -0500224 } else if (!data.GetControlBit(ControlBit::kEnabled)) {
225 {
226 auto goal = drivetrain.goal.MakeMessage();
227 goal->Zero();
228 goal->control_loop_driving = false;
229 goal->left_goal = goal->right_goal = 0;
230 goal->left_velocity_goal = goal->right_velocity_goal = 0;
231 if (!goal.Send()) {
232 LOG(WARNING, "sending 0 drivetrain goal failed\n");
233 }
234 }
235 {
236 // TODO(brians): Make sure this doesn't make it unbrake and not move
237 // or something weird.
238 auto goal = control_loops::shooter_queue_group.goal.MakeMessage();
239 goal->Zero();
240 if (!goal.Send()) {
241 LOG(WARNING, "sending 0 shooter goal failed\n");
242 }
243 }
Brian Silverman756f9ff2014-01-17 23:40:23 -0800244 }
Austin Schuh58d23682014-02-23 01:39:50 -0800245 } else {
246 HandleTeleop(data);
Brian Silverman756f9ff2014-01-17 23:40:23 -0800247 }
248 }
Austin Schuh58d23682014-02-23 01:39:50 -0800249
250 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
251 bool is_control_loop_driving = false;
252 double left_goal = 0.0;
253 double right_goal = 0.0;
254 const double wheel = -data.GetAxis(kSteeringWheel);
255 const double throttle = -data.GetAxis(kDriveThrottle);
256 const double kThrottleGain = 1.0 / 2.5;
257 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
258 data.IsPressed(kDriveControlLoopEnable2))) {
259 // TODO(austin): Static sucks!
260 static double distance = 0.0;
261 static double angle = 0.0;
262 static double filtered_goal_distance = 0.0;
263 if (data.PosEdge(kDriveControlLoopEnable1) ||
264 data.PosEdge(kDriveControlLoopEnable2)) {
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800265 if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) {
Austin Schuh58d23682014-02-23 01:39:50 -0800266 distance = (drivetrain.position->left_encoder +
267 drivetrain.position->right_encoder) /
268 2.0 -
269 throttle * kThrottleGain / 2.0;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800270 angle = gyro_reading->angle;
Austin Schuh58d23682014-02-23 01:39:50 -0800271 filtered_goal_distance = distance;
272 }
273 }
274 is_control_loop_driving = true;
275
276 // const double gyro_angle = Gyro.View().angle;
277 const double goal_theta = angle - wheel * 0.27;
278 const double goal_distance = distance + throttle * kThrottleGain;
279 const double robot_width = 22.0 / 100.0 * 2.54;
280 const double kMaxVelocity = 0.6;
281 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
282 filtered_goal_distance += kMaxVelocity * 0.02;
283 } else if (goal_distance <
284 -kMaxVelocity * 0.02 + filtered_goal_distance) {
285 filtered_goal_distance -= kMaxVelocity * 0.02;
286 } else {
287 filtered_goal_distance = goal_distance;
288 }
289 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
290 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
291 is_high_gear_ = false;
292
293 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
294 }
295 if (!drivetrain.goal.MakeWithBuilder()
296 .steering(wheel)
297 .throttle(throttle)
298 .highgear(is_high_gear_)
Brian Silvermand48804e2014-07-20 16:37:25 -0700299 .quickturn(false)
Austin Schuh58d23682014-02-23 01:39:50 -0800300 .control_loop_driving(is_control_loop_driving)
301 .left_goal(left_goal)
302 .right_goal(right_goal)
Brian Silverman2c764f02014-04-25 09:21:21 -0500303 .left_velocity_goal(0)
304 .right_velocity_goal(0)
Austin Schuh58d23682014-02-23 01:39:50 -0800305 .Send()) {
306 LOG(WARNING, "sending stick values failed\n");
307 }
308 if (data.PosEdge(kShiftHigh)) {
309 is_high_gear_ = false;
310 }
311 if (data.PosEdge(kShiftLow)) {
312 is_high_gear_ = true;
313 }
314 }
315
316 void SetGoal(ClawGoal goal) {
317 goal_angle_ = goal.angle;
318 separation_angle_ = goal.separation;
Brian7e294392014-03-31 12:39:13 -0700319 moving_for_shot_ = false;
Brian Silvermana379f002014-03-22 19:34:53 -0700320 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800321 intake_power_ = 0.0;
322 }
323
324 void SetGoal(ShotGoal goal) {
325 goal_angle_ = goal.claw.angle;
Brian7e294392014-03-31 12:39:13 -0700326 shot_separation_angle_ = goal.claw.separation;
327 separation_angle_ = kGrabSeparation;
328 moving_for_shot_ = true;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800329 shot_power_ = goal.shot_power;
330 velocity_compensation_ = goal.velocity_compensation;
331 intake_power_ = goal.intake_power;
Austin Schuh58d23682014-02-23 01:39:50 -0800332 }
333
334 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
335 HandleDrivetrain(data);
Austin Schuhc95c2b72014-03-02 11:56:49 -0800336 if (!data.GetControlBit(ControlBit::kEnabled)) {
337 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800338 LOG(DEBUG, "Canceling\n");
Austin Schuhc95c2b72014-03-02 11:56:49 -0800339 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800340 if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) {
341 intake_power_ = 0.0;
Brian Silverman545f2ad2014-03-14 12:31:42 -0700342 separation_angle_ = kGrabSeparation;
Brian7e294392014-03-31 12:39:13 -0700343 moving_for_shot_ = false;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800344 }
Austin Schuh58d23682014-02-23 01:39:50 -0800345
Brian Silverman18f6e642014-03-13 18:52:47 -0700346 static const double kAdjustClawGoalDeadband = 0.08;
347 double claw_goal_adjust = data.GetAxis(kAdjustClawGoal);
348 if (::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) {
349 claw_goal_adjust = 0;
350 } else {
351 claw_goal_adjust = (claw_goal_adjust -
352 ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband
353 : kAdjustClawGoalDeadband)) *
354 0.035;
355 }
356 double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation);
357 if (::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) {
358 claw_separation_adjust = 0;
359 } else {
360 claw_separation_adjust =
361 (claw_separation_adjust -
362 ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband
363 : kAdjustClawGoalDeadband)) *
364 -0.035;
365 }
366
Brian6faa7822014-03-31 18:00:28 -0700367 if (data.GetAxis(kFlipRobot) > 0.9) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700368 claw_goal_adjust += claw_separation_adjust;
369 claw_goal_adjust *= -1;
370
Austin Schuh80ff2e12014-03-08 12:06:19 -0800371 if (data.IsPressed(kIntakeOpenPosition)) {
372 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800373 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800374 SetGoal(kFlippedIntakeOpenGoal);
375 } else if (data.IsPressed(kIntakePosition)) {
376 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800377 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800378 SetGoal(kFlippedIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700379 } else if (data.IsPressed(kVerticalTuck)) {
380 action_queue_.CancelAllActions();
381 LOG(DEBUG, "Canceling\n");
382 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800383 } else if (data.IsPressed(kTuck)) {
384 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800385 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800386 SetGoal(kFlippedTuckGoal);
387 } else if (data.PosEdge(kLongShot)) {
388 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800389 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800390 SetGoal(kFlippedLongShotGoal);
Brian Silverman4051c6f2014-07-19 16:57:37 -0700391 } else if (data.PosEdge(kCloseShot)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800392 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800393 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800394 SetGoal(kFlippedMediumShotGoal);
Brian Silverman4051c6f2014-07-19 16:57:37 -0700395 } else if (data.PosEdge(kFenderShot)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800396 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800397 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800398 SetGoal(kFlippedShortShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700399 } else if (data.PosEdge(kHumanPlayerShot)) {
400 action_queue_.CancelAllActions();
401 LOG(DEBUG, "Canceling\n");
Brian Silverman73c48392014-04-05 07:07:41 -0700402 SetGoal(kFlippedHumanShotGoal);
Brian Silverman8d5dda42014-04-06 15:59:49 -0700403 } else if (data.PosEdge(kUserLeft)) {
404 action_queue_.CancelAllActions();
405 LOG(DEBUG, "Canceling\n");
406 SetGoal(kFlipped254PassGoal);
Brian Silverman8cd51202014-07-19 16:58:08 -0700407 } else if (data.PosEdge(kUserRight)) {
408 action_queue_.CancelAllActions();
409 LOG(DEBUG, "Canceling\n");
410 SetGoal(kFlippedDemoShotGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800411 } else if (data.PosEdge(kTrussShot)) {
412 action_queue_.CancelAllActions();
413 LOG(DEBUG, "Canceling\n");
Brian Silverman73c48392014-04-05 07:07:41 -0700414 SetGoal(kFlippedTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800415 }
416 } else {
417 if (data.IsPressed(kIntakeOpenPosition)) {
418 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800419 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800420 SetGoal(kIntakeOpenGoal);
421 } else if (data.IsPressed(kIntakePosition)) {
422 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800423 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800424 SetGoal(kIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700425 } else if (data.IsPressed(kVerticalTuck)) {
426 action_queue_.CancelAllActions();
427 LOG(DEBUG, "Canceling\n");
428 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800429 } else if (data.IsPressed(kTuck)) {
430 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800431 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800432 SetGoal(kTuckGoal);
433 } else if (data.PosEdge(kLongShot)) {
434 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800435 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800436 SetGoal(kLongShotGoal);
Brian Silverman4051c6f2014-07-19 16:57:37 -0700437 } else if (data.PosEdge(kCloseShot)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800438 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800439 LOG(DEBUG, "Canceling\n");
Brian Silverman4051c6f2014-07-19 16:57:37 -0700440 SetGoal(kCloseShotGoal);
441 } else if (data.PosEdge(kFenderShot)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800442 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800443 LOG(DEBUG, "Canceling\n");
Brian Silverman4051c6f2014-07-19 16:57:37 -0700444 SetGoal(kFenderShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700445 } else if (data.PosEdge(kHumanPlayerShot)) {
446 action_queue_.CancelAllActions();
447 LOG(DEBUG, "Canceling\n");
448 SetGoal(kHumanShotGoal);
Brian Silverman8d5dda42014-04-06 15:59:49 -0700449 } else if (data.PosEdge(kUserLeft)) {
450 action_queue_.CancelAllActions();
451 LOG(DEBUG, "Canceling\n");
452 SetGoal(k254PassGoal);
Brian Silverman8cd51202014-07-19 16:58:08 -0700453 } else if (data.PosEdge(kUserRight)) {
454 action_queue_.CancelAllActions();
455 LOG(DEBUG, "Canceling\n");
456 SetGoal(kDemoShotGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800457 } else if (data.PosEdge(kTrussShot)) {
458 action_queue_.CancelAllActions();
459 LOG(DEBUG, "Canceling\n");
460 SetGoal(kTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800461 }
Austin Schuh58d23682014-02-23 01:39:50 -0800462 }
463
Austin Schuhade6d082014-03-09 00:53:06 -0800464 /*
Austin Schuh80ff2e12014-03-08 12:06:19 -0800465 if (data.PosEdge(kCatch)) {
466 auto catch_action = MakeCatchAction();
467 catch_action->GetGoal()->catch_angle = goal_angle_;
468 action_queue_.QueueAction(::std::move(catch_action));
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800469 }
Austin Schuhade6d082014-03-09 00:53:06 -0800470 */
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800471
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800472 if (data.PosEdge(kFire)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800473 action_queue_.QueueAction(actions::MakeShootAction());
Brian Silverman9f863a02014-03-29 17:42:36 -0700474 } else if (data.NegEdge(kFire)) {
475 action_queue_.CancelCurrentAction();
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800476 }
477
478 action_queue_.Tick();
Austin Schuhc95c2b72014-03-02 11:56:49 -0800479 if (data.IsPressed(kUnload) || data.IsPressed(kReload)) {
480 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800481 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800482 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700483 velocity_compensation_ = 0.0;
Austin Schuhc95c2b72014-03-02 11:56:49 -0800484 }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800485
486 // Send out the claw and shooter goals if no actions are running.
487 if (!action_queue_.Running()) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700488 goal_angle_ += claw_goal_adjust;
489 separation_angle_ += claw_separation_adjust;
490
Austin Schuh80ff2e12014-03-08 12:06:19 -0800491 // If the action just ended, turn the intake off and stop velocity
492 // compensating.
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800493 if (was_running_) {
494 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700495 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800496 }
497
498 control_loops::drivetrain.status.FetchLatest();
Brian Silvermana379f002014-03-22 19:34:53 -0700499 double goal_angle = goal_angle_;
500 if (control_loops::drivetrain.status.get()) {
501 goal_angle +=
502 SpeedToAngleOffset(control_loops::drivetrain.status->robot_speed);
503 } else {
504 LOG_INTERVAL(no_drivetrain_status_);
505 }
Brian7e294392014-03-31 12:39:13 -0700506
507 if (moving_for_shot_) {
508 auto &claw_status = control_loops::claw_queue_group.status;
509 claw_status.FetchLatest();
510 if (claw_status.get()) {
Brian6faa7822014-03-31 18:00:28 -0700511 if (::std::abs(claw_status->bottom - goal_angle) < 0.2) {
Brian7e294392014-03-31 12:39:13 -0700512 moving_for_shot_ = false;
513 separation_angle_ = shot_separation_angle_;
514 }
515 }
516 }
517
Austin Schuhade6d082014-03-09 00:53:06 -0800518 double separation_angle = separation_angle_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800519
Austin Schuhade6d082014-03-09 00:53:06 -0800520 if (data.IsPressed(kCatch)) {
521 const double kCatchSeparation = 1.0;
522 goal_angle -= kCatchSeparation / 2.0;
523 separation_angle = kCatchSeparation;
524 }
525
526 bool intaking =
527 data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) ||
528 data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch);
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800529 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800530 .bottom_angle(goal_angle)
Austin Schuhade6d082014-03-09 00:53:06 -0800531 .separation_angle(separation_angle)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800532 .intake(intaking ? 12.0
533 : (data.IsPressed(kRollersOut) ? -12.0
534 : intake_power_))
535 .centering(intaking ? 12.0 : 0.0)
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800536 .Send()) {
537 LOG(WARNING, "sending claw goal failed\n");
538 }
539
540 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
541 .shot_power(shot_power_)
542 .shot_requested(data.IsPressed(kFire))
543 .unload_requested(data.IsPressed(kUnload))
544 .load_requested(data.IsPressed(kReload))
545 .Send()) {
546 LOG(WARNING, "sending shooter goal failed\n");
547 }
Austin Schuh58d23682014-02-23 01:39:50 -0800548 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800549 was_running_ = action_queue_.Running();
550 }
551
552 double SpeedToAngleOffset(double speed) {
553 const frc971::constants::Values &values = frc971::constants::GetValues();
554 // scale speed to a [0.0-1.0] on something close to the max
555 // TODO(austin): Change the scale factor for different shots.
Brian Silvermana379f002014-03-22 19:34:53 -0700556 return (speed / values.drivetrain_max_speed) * velocity_compensation_;
Austin Schuh58d23682014-02-23 01:39:50 -0800557 }
558
Austin Schuh01c652b2014-02-21 23:13:42 -0800559 private:
Austin Schuh58d23682014-02-23 01:39:50 -0800560 bool is_high_gear_;
561 double shot_power_;
562 double goal_angle_;
Brian7e294392014-03-31 12:39:13 -0700563 double separation_angle_, shot_separation_angle_;
Brian Silvermana379f002014-03-22 19:34:53 -0700564 double velocity_compensation_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800565 double intake_power_;
566 bool was_running_;
Brian7e294392014-03-31 12:39:13 -0700567 bool moving_for_shot_ = false;
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800568
569 ActionQueue action_queue_;
Brian Silvermana379f002014-03-22 19:34:53 -0700570
571 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
572 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
573 "no drivetrain status");
Brian Silverman756f9ff2014-01-17 23:40:23 -0800574};
575
576} // namespace joysticks
577} // namespace input
578} // namespace frc971
579
580int main() {
581 ::aos::Init();
582 ::frc971::input::joysticks::Reader reader;
583 reader.Run();
584 ::aos::Cleanup();
585}