blob: a639c88b7b285bda3adb162d574f9ce8b796e1e7 [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
127const ClawGoal k254PassGoal = {-1.95, kGrabSeparation};
128const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation};
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800129
130// Makes a new ShootAction action.
Austin Schuh80ff2e12014-03-08 12:06:19 -0800131::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>
132MakeCatchAction() {
133 return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>(
134 new TypedAction< ::frc971::actions::CatchActionGroup>(
135 &::frc971::actions::catch_action));
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800136}
137
138// A queue which queues Actions and cancels them.
139class ActionQueue {
140 public:
141 // Queues up an action for sending.
142 void QueueAction(::std::unique_ptr<Action> action) {
143 if (current_action_) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800144 LOG(INFO, "Queueing action, canceling prior\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800145 current_action_->Cancel();
146 next_action_ = ::std::move(action);
147 } else {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800148 LOG(INFO, "Queueing action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800149 current_action_ = ::std::move(action);
150 current_action_->Start();
151 }
152 }
153
154 // Cancels the current action, and runs the next one when the current one has
155 // finished.
156 void CancelCurrentAction() {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800157 LOG(INFO, "Canceling current action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800158 if (current_action_) {
159 current_action_->Cancel();
160 }
161 }
162
163 // Cancels all running actions.
164 void CancelAllActions() {
Brian Silverman101b9642014-03-08 12:45:16 -0800165 LOG(DEBUG, "Cancelling all actions\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800166 if (current_action_) {
167 current_action_->Cancel();
168 }
169 next_action_.reset();
170 }
171
172 // Runs the next action when the current one is finished running.
173 void Tick() {
174 if (current_action_) {
175 if (!current_action_->Running()) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800176 LOG(INFO, "Action is done.\n");
177 current_action_ = ::std::move(next_action_);
178 if (current_action_) {
179 LOG(INFO, "Running next action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800180 current_action_->Start();
181 }
182 }
183 }
184 }
185
186 // Returns true if any action is running or could be running.
187 // For a one cycle faster response, call Tick before running this.
Brian Silverman2c1e0342014-04-11 16:15:01 -0700188 bool Running() { return static_cast<bool>(current_action_); }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800189
190 private:
191 ::std::unique_ptr<Action> current_action_;
192 ::std::unique_ptr<Action> next_action_;
193};
194
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800195
Brian Silverman756f9ff2014-01-17 23:40:23 -0800196class Reader : public ::aos::input::JoystickInput {
197 public:
Austin Schuh58d23682014-02-23 01:39:50 -0800198 Reader()
Brian Silvermane3a277a2014-04-13 14:56:57 -0700199 : is_high_gear_(false),
Austin Schuh9cb836e2014-02-23 19:25:55 -0800200 shot_power_(80.0),
Austin Schuh58d23682014-02-23 01:39:50 -0800201 goal_angle_(0.0),
Brian Silverman545f2ad2014-03-14 12:31:42 -0700202 separation_angle_(kGrabSeparation),
Brian Silvermana379f002014-03-22 19:34:53 -0700203 velocity_compensation_(0.0),
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800204 intake_power_(0.0),
205 was_running_(false) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -0800206
207 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Brian Silverman756f9ff2014-01-17 23:40:23 -0800208 if (data.GetControlBit(ControlBit::kAutonomous)) {
209 if (data.PosEdge(ControlBit::kEnabled)){
210 LOG(INFO, "Starting auto mode\n");
211 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800212 .run_auto(true)
213 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800214 } else if (data.NegEdge(ControlBit::kEnabled)) {
215 LOG(INFO, "Stopping auto mode\n");
216 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800217 .run_auto(false)
218 .Send();
Brian Silverman2c764f02014-04-25 09:21:21 -0500219 } else if (!data.GetControlBit(ControlBit::kEnabled)) {
220 {
221 auto goal = drivetrain.goal.MakeMessage();
222 goal->Zero();
223 goal->control_loop_driving = false;
224 goal->left_goal = goal->right_goal = 0;
225 goal->left_velocity_goal = goal->right_velocity_goal = 0;
226 if (!goal.Send()) {
227 LOG(WARNING, "sending 0 drivetrain goal failed\n");
228 }
229 }
230 {
231 // TODO(brians): Make sure this doesn't make it unbrake and not move
232 // or something weird.
233 auto goal = control_loops::shooter_queue_group.goal.MakeMessage();
234 goal->Zero();
235 if (!goal.Send()) {
236 LOG(WARNING, "sending 0 shooter goal failed\n");
237 }
238 }
Brian Silverman756f9ff2014-01-17 23:40:23 -0800239 }
Austin Schuh58d23682014-02-23 01:39:50 -0800240 } else {
241 HandleTeleop(data);
Brian Silverman756f9ff2014-01-17 23:40:23 -0800242 }
243 }
Austin Schuh58d23682014-02-23 01:39:50 -0800244
245 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
246 bool is_control_loop_driving = false;
247 double left_goal = 0.0;
248 double right_goal = 0.0;
249 const double wheel = -data.GetAxis(kSteeringWheel);
250 const double throttle = -data.GetAxis(kDriveThrottle);
251 const double kThrottleGain = 1.0 / 2.5;
252 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
253 data.IsPressed(kDriveControlLoopEnable2))) {
254 // TODO(austin): Static sucks!
255 static double distance = 0.0;
256 static double angle = 0.0;
257 static double filtered_goal_distance = 0.0;
258 if (data.PosEdge(kDriveControlLoopEnable1) ||
259 data.PosEdge(kDriveControlLoopEnable2)) {
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800260 if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) {
Austin Schuh58d23682014-02-23 01:39:50 -0800261 distance = (drivetrain.position->left_encoder +
262 drivetrain.position->right_encoder) /
263 2.0 -
264 throttle * kThrottleGain / 2.0;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800265 angle = gyro_reading->angle;
Austin Schuh58d23682014-02-23 01:39:50 -0800266 filtered_goal_distance = distance;
267 }
268 }
269 is_control_loop_driving = true;
270
271 // const double gyro_angle = Gyro.View().angle;
272 const double goal_theta = angle - wheel * 0.27;
273 const double goal_distance = distance + throttle * kThrottleGain;
274 const double robot_width = 22.0 / 100.0 * 2.54;
275 const double kMaxVelocity = 0.6;
276 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
277 filtered_goal_distance += kMaxVelocity * 0.02;
278 } else if (goal_distance <
279 -kMaxVelocity * 0.02 + filtered_goal_distance) {
280 filtered_goal_distance -= kMaxVelocity * 0.02;
281 } else {
282 filtered_goal_distance = goal_distance;
283 }
284 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
285 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
286 is_high_gear_ = false;
287
288 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
289 }
290 if (!drivetrain.goal.MakeWithBuilder()
291 .steering(wheel)
292 .throttle(throttle)
293 .highgear(is_high_gear_)
294 .quickturn(data.IsPressed(kQuickTurn))
295 .control_loop_driving(is_control_loop_driving)
296 .left_goal(left_goal)
297 .right_goal(right_goal)
Brian Silverman2c764f02014-04-25 09:21:21 -0500298 .left_velocity_goal(0)
299 .right_velocity_goal(0)
Austin Schuh58d23682014-02-23 01:39:50 -0800300 .Send()) {
301 LOG(WARNING, "sending stick values failed\n");
302 }
303 if (data.PosEdge(kShiftHigh)) {
304 is_high_gear_ = false;
305 }
306 if (data.PosEdge(kShiftLow)) {
307 is_high_gear_ = true;
308 }
309 }
310
311 void SetGoal(ClawGoal goal) {
312 goal_angle_ = goal.angle;
313 separation_angle_ = goal.separation;
Brian7e294392014-03-31 12:39:13 -0700314 moving_for_shot_ = false;
Brian Silvermana379f002014-03-22 19:34:53 -0700315 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800316 intake_power_ = 0.0;
317 }
318
319 void SetGoal(ShotGoal goal) {
320 goal_angle_ = goal.claw.angle;
Brian7e294392014-03-31 12:39:13 -0700321 shot_separation_angle_ = goal.claw.separation;
322 separation_angle_ = kGrabSeparation;
323 moving_for_shot_ = true;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800324 shot_power_ = goal.shot_power;
325 velocity_compensation_ = goal.velocity_compensation;
326 intake_power_ = goal.intake_power;
Austin Schuh58d23682014-02-23 01:39:50 -0800327 }
328
329 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
330 HandleDrivetrain(data);
Austin Schuhc95c2b72014-03-02 11:56:49 -0800331 if (!data.GetControlBit(ControlBit::kEnabled)) {
332 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800333 LOG(DEBUG, "Canceling\n");
Austin Schuhc95c2b72014-03-02 11:56:49 -0800334 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800335 if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) {
336 intake_power_ = 0.0;
Brian Silverman545f2ad2014-03-14 12:31:42 -0700337 separation_angle_ = kGrabSeparation;
Brian7e294392014-03-31 12:39:13 -0700338 moving_for_shot_ = false;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800339 }
Austin Schuh58d23682014-02-23 01:39:50 -0800340
Brian Silverman18f6e642014-03-13 18:52:47 -0700341 static const double kAdjustClawGoalDeadband = 0.08;
342 double claw_goal_adjust = data.GetAxis(kAdjustClawGoal);
343 if (::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) {
344 claw_goal_adjust = 0;
345 } else {
346 claw_goal_adjust = (claw_goal_adjust -
347 ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband
348 : kAdjustClawGoalDeadband)) *
349 0.035;
350 }
351 double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation);
352 if (::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) {
353 claw_separation_adjust = 0;
354 } else {
355 claw_separation_adjust =
356 (claw_separation_adjust -
357 ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband
358 : kAdjustClawGoalDeadband)) *
359 -0.035;
360 }
361
Brian6faa7822014-03-31 18:00:28 -0700362 if (data.GetAxis(kFlipRobot) > 0.9) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700363 claw_goal_adjust += claw_separation_adjust;
364 claw_goal_adjust *= -1;
365
Austin Schuh80ff2e12014-03-08 12:06:19 -0800366 if (data.IsPressed(kIntakeOpenPosition)) {
367 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800368 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800369 SetGoal(kFlippedIntakeOpenGoal);
370 } else if (data.IsPressed(kIntakePosition)) {
371 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800372 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800373 SetGoal(kFlippedIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700374 } else if (data.IsPressed(kVerticalTuck)) {
375 action_queue_.CancelAllActions();
376 LOG(DEBUG, "Canceling\n");
377 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800378 } else if (data.IsPressed(kTuck)) {
379 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800380 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800381 SetGoal(kFlippedTuckGoal);
382 } else if (data.PosEdge(kLongShot)) {
383 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800384 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800385 SetGoal(kFlippedLongShotGoal);
Brian Silverman4051c6f2014-07-19 16:57:37 -0700386 } else if (data.PosEdge(kCloseShot)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800387 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800388 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800389 SetGoal(kFlippedMediumShotGoal);
Brian Silverman4051c6f2014-07-19 16:57:37 -0700390 } else if (data.PosEdge(kFenderShot)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800391 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800392 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800393 SetGoal(kFlippedShortShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700394 } else if (data.PosEdge(kHumanPlayerShot)) {
395 action_queue_.CancelAllActions();
396 LOG(DEBUG, "Canceling\n");
Brian Silverman73c48392014-04-05 07:07:41 -0700397 SetGoal(kFlippedHumanShotGoal);
Brian Silverman8d5dda42014-04-06 15:59:49 -0700398 } else if (data.PosEdge(kUserLeft)) {
399 action_queue_.CancelAllActions();
400 LOG(DEBUG, "Canceling\n");
401 SetGoal(kFlipped254PassGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800402 } else if (data.PosEdge(kTrussShot)) {
403 action_queue_.CancelAllActions();
404 LOG(DEBUG, "Canceling\n");
Brian Silverman73c48392014-04-05 07:07:41 -0700405 SetGoal(kFlippedTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800406 }
407 } else {
408 if (data.IsPressed(kIntakeOpenPosition)) {
409 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800410 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800411 SetGoal(kIntakeOpenGoal);
412 } else if (data.IsPressed(kIntakePosition)) {
413 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800414 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800415 SetGoal(kIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700416 } else if (data.IsPressed(kVerticalTuck)) {
417 action_queue_.CancelAllActions();
418 LOG(DEBUG, "Canceling\n");
419 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800420 } else if (data.IsPressed(kTuck)) {
421 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800422 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800423 SetGoal(kTuckGoal);
424 } else if (data.PosEdge(kLongShot)) {
425 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800426 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800427 SetGoal(kLongShotGoal);
Brian Silverman4051c6f2014-07-19 16:57:37 -0700428 } else if (data.PosEdge(kCloseShot)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800429 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800430 LOG(DEBUG, "Canceling\n");
Brian Silverman4051c6f2014-07-19 16:57:37 -0700431 SetGoal(kCloseShotGoal);
432 } else if (data.PosEdge(kFenderShot)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800433 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800434 LOG(DEBUG, "Canceling\n");
Brian Silverman4051c6f2014-07-19 16:57:37 -0700435 SetGoal(kFenderShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700436 } else if (data.PosEdge(kHumanPlayerShot)) {
437 action_queue_.CancelAllActions();
438 LOG(DEBUG, "Canceling\n");
439 SetGoal(kHumanShotGoal);
Brian Silverman8d5dda42014-04-06 15:59:49 -0700440 } else if (data.PosEdge(kUserLeft)) {
441 action_queue_.CancelAllActions();
442 LOG(DEBUG, "Canceling\n");
443 SetGoal(k254PassGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800444 } else if (data.PosEdge(kTrussShot)) {
445 action_queue_.CancelAllActions();
446 LOG(DEBUG, "Canceling\n");
447 SetGoal(kTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800448 }
Austin Schuh58d23682014-02-23 01:39:50 -0800449 }
450
Austin Schuhade6d082014-03-09 00:53:06 -0800451 /*
Austin Schuh80ff2e12014-03-08 12:06:19 -0800452 if (data.PosEdge(kCatch)) {
453 auto catch_action = MakeCatchAction();
454 catch_action->GetGoal()->catch_angle = goal_angle_;
455 action_queue_.QueueAction(::std::move(catch_action));
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800456 }
Austin Schuhade6d082014-03-09 00:53:06 -0800457 */
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800458
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800459 if (data.PosEdge(kFire)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800460 action_queue_.QueueAction(actions::MakeShootAction());
Brian Silverman9f863a02014-03-29 17:42:36 -0700461 } else if (data.NegEdge(kFire)) {
462 action_queue_.CancelCurrentAction();
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800463 }
464
465 action_queue_.Tick();
Austin Schuhc95c2b72014-03-02 11:56:49 -0800466 if (data.IsPressed(kUnload) || data.IsPressed(kReload)) {
467 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800468 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800469 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700470 velocity_compensation_ = 0.0;
Austin Schuhc95c2b72014-03-02 11:56:49 -0800471 }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800472
473 // Send out the claw and shooter goals if no actions are running.
474 if (!action_queue_.Running()) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700475 goal_angle_ += claw_goal_adjust;
476 separation_angle_ += claw_separation_adjust;
477
Austin Schuh80ff2e12014-03-08 12:06:19 -0800478 // If the action just ended, turn the intake off and stop velocity
479 // compensating.
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800480 if (was_running_) {
481 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700482 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800483 }
484
485 control_loops::drivetrain.status.FetchLatest();
Brian Silvermana379f002014-03-22 19:34:53 -0700486 double goal_angle = goal_angle_;
487 if (control_loops::drivetrain.status.get()) {
488 goal_angle +=
489 SpeedToAngleOffset(control_loops::drivetrain.status->robot_speed);
490 } else {
491 LOG_INTERVAL(no_drivetrain_status_);
492 }
Brian7e294392014-03-31 12:39:13 -0700493
494 if (moving_for_shot_) {
495 auto &claw_status = control_loops::claw_queue_group.status;
496 claw_status.FetchLatest();
497 if (claw_status.get()) {
Brian6faa7822014-03-31 18:00:28 -0700498 if (::std::abs(claw_status->bottom - goal_angle) < 0.2) {
Brian7e294392014-03-31 12:39:13 -0700499 moving_for_shot_ = false;
500 separation_angle_ = shot_separation_angle_;
501 }
502 }
503 }
504
Austin Schuhade6d082014-03-09 00:53:06 -0800505 double separation_angle = separation_angle_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800506
Austin Schuhade6d082014-03-09 00:53:06 -0800507 if (data.IsPressed(kCatch)) {
508 const double kCatchSeparation = 1.0;
509 goal_angle -= kCatchSeparation / 2.0;
510 separation_angle = kCatchSeparation;
511 }
512
513 bool intaking =
514 data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) ||
515 data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch);
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800516 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800517 .bottom_angle(goal_angle)
Austin Schuhade6d082014-03-09 00:53:06 -0800518 .separation_angle(separation_angle)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800519 .intake(intaking ? 12.0
520 : (data.IsPressed(kRollersOut) ? -12.0
521 : intake_power_))
522 .centering(intaking ? 12.0 : 0.0)
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800523 .Send()) {
524 LOG(WARNING, "sending claw goal failed\n");
525 }
526
527 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
528 .shot_power(shot_power_)
529 .shot_requested(data.IsPressed(kFire))
530 .unload_requested(data.IsPressed(kUnload))
531 .load_requested(data.IsPressed(kReload))
532 .Send()) {
533 LOG(WARNING, "sending shooter goal failed\n");
534 }
Austin Schuh58d23682014-02-23 01:39:50 -0800535 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800536 was_running_ = action_queue_.Running();
537 }
538
539 double SpeedToAngleOffset(double speed) {
540 const frc971::constants::Values &values = frc971::constants::GetValues();
541 // scale speed to a [0.0-1.0] on something close to the max
542 // TODO(austin): Change the scale factor for different shots.
Brian Silvermana379f002014-03-22 19:34:53 -0700543 return (speed / values.drivetrain_max_speed) * velocity_compensation_;
Austin Schuh58d23682014-02-23 01:39:50 -0800544 }
545
Austin Schuh01c652b2014-02-21 23:13:42 -0800546 private:
Austin Schuh58d23682014-02-23 01:39:50 -0800547 bool is_high_gear_;
548 double shot_power_;
549 double goal_angle_;
Brian7e294392014-03-31 12:39:13 -0700550 double separation_angle_, shot_separation_angle_;
Brian Silvermana379f002014-03-22 19:34:53 -0700551 double velocity_compensation_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800552 double intake_power_;
553 bool was_running_;
Brian7e294392014-03-31 12:39:13 -0700554 bool moving_for_shot_ = false;
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800555
556 ActionQueue action_queue_;
Brian Silvermana379f002014-03-22 19:34:53 -0700557
558 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
559 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
560 "no drivetrain status");
Brian Silverman756f9ff2014-01-17 23:40:23 -0800561};
562
563} // namespace joysticks
564} // namespace input
565} // namespace frc971
566
567int main() {
568 ::aos::Init();
569 ::frc971::input::joysticks::Reader reader;
570 reader.Run();
571 ::aos::Cleanup();
572}