blob: 9fc11dcaaf3a2529f829ad52abcb796659df70ab [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);
57const ButtonLocation kMediumShot(3, 6);
58const ButtonLocation kShortShot(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 Silvermanb3cf0ef2014-03-22 12:45:55 -070081// TODO(brians): This wants to be -0.04 on the comp bot. Make them both the
82// same.
83const double kGrabSeparation = 0;
Brian Silverman545f2ad2014-03-14 12:31:42 -070084const double kShootSeparation = 0.11 + kGrabSeparation;
85
Austin Schuh58d23682014-02-23 01:39:50 -080086const ClawGoal kTuckGoal = {-2.273474, -0.749484};
Brian6faa7822014-03-31 18:00:28 -070087const ClawGoal kVerticalTuckGoal = {0, kGrabSeparation};
Brian Silverman63ec7502014-03-30 18:09:10 -070088const ClawGoal kIntakeGoal = {-2.24, kGrabSeparation};
89const ClawGoal kIntakeOpenGoal = {-2.0, 1.1};
Austin Schuh58d23682014-02-23 01:39:50 -080090
Austin Schuh80ff2e12014-03-08 12:06:19 -080091// TODO(austin): Tune these by hand...
92const ClawGoal kFlippedTuckGoal = {2.733474, -0.75};
Brian Silverman545f2ad2014-03-14 12:31:42 -070093const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation};
Austin Schuh80ff2e12014-03-08 12:06:19 -080094const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0};
95
Brian Silverman0d09e3b2014-04-10 21:15:31 -070096// 8" between near edge of colored line and rear edge of bumper
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 Silverman0d09e3b2014-04-10 21:15:31 -070099// 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 Silverman0d09e3b2014-04-10 21:15:31 -0700102// 34" {0.96, kShootSeparation}, 140, 0.09, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700103
104// 78" between near edge of colored line and rear edge of bumper
105const ShotGoal kMediumShotGoal = {
106 {-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 Silverman63ec7502014-03-30 18:09:10 -0700109 {0.905, kShootSeparation}, 120, 0.2, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700110// 3/4" plunger {0.80, kShootSeparation}, 105, 0.2, kIntakePower};
111
112const ShotGoal kShortShotGoal = {
Brian Silverman55d7f572014-04-17 14:33:54 -0700113 {-0.68, kShootSeparation}, 115.0, 0.4, kIntakePower};
114const ShotGoal kFlippedShortShotGoal = {
Brian Silverman63ec7502014-03-30 18:09:10 -0700115 {0.67, kShootSeparation}, 115.0, 0.4, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700116
Brian Silverman63ec7502014-03-30 18:09:10 -0700117const ShotGoal kHumanShotGoal = {
118 {-0.90, kShootSeparation}, 140, 0.04, kIntakePower};
Brian Silverman73c48392014-04-05 07:07:41 -0700119const ShotGoal kFlippedHumanShotGoal = {
120 {0.90, kShootSeparation}, 140, 0, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700121const ShotGoal kTrussShotGoal = {
Brian Silverman8d5dda42014-04-06 15:59:49 -0700122 {-0.68, kShootSeparation}, 83.0, 0.4, kIntakePower};
Brian Silverman73c48392014-04-05 07:07:41 -0700123const ShotGoal kFlippedTrussShotGoal = {
Brian Silverman8d5dda42014-04-06 15:59:49 -0700124 {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower};
125
126const ClawGoal k254PassGoal = {-1.95, kGrabSeparation};
127const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation};
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800128
129// Makes a new ShootAction action.
Austin Schuh80ff2e12014-03-08 12:06:19 -0800130::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>
131MakeCatchAction() {
132 return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>(
133 new TypedAction< ::frc971::actions::CatchActionGroup>(
134 &::frc971::actions::catch_action));
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800135}
136
137// A queue which queues Actions and cancels them.
138class ActionQueue {
139 public:
140 // Queues up an action for sending.
141 void QueueAction(::std::unique_ptr<Action> action) {
142 if (current_action_) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800143 LOG(INFO, "Queueing action, canceling prior\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800144 current_action_->Cancel();
145 next_action_ = ::std::move(action);
146 } else {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800147 LOG(INFO, "Queueing action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800148 current_action_ = ::std::move(action);
149 current_action_->Start();
150 }
151 }
152
153 // Cancels the current action, and runs the next one when the current one has
154 // finished.
155 void CancelCurrentAction() {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800156 LOG(INFO, "Canceling current action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800157 if (current_action_) {
158 current_action_->Cancel();
159 }
160 }
161
162 // Cancels all running actions.
163 void CancelAllActions() {
Brian Silverman101b9642014-03-08 12:45:16 -0800164 LOG(DEBUG, "Cancelling all actions\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800165 if (current_action_) {
166 current_action_->Cancel();
167 }
168 next_action_.reset();
169 }
170
171 // Runs the next action when the current one is finished running.
172 void Tick() {
173 if (current_action_) {
174 if (!current_action_->Running()) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800175 LOG(INFO, "Action is done.\n");
176 current_action_ = ::std::move(next_action_);
177 if (current_action_) {
178 LOG(INFO, "Running next action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800179 current_action_->Start();
180 }
181 }
182 }
183 }
184
185 // Returns true if any action is running or could be running.
186 // For a one cycle faster response, call Tick before running this.
Brian Silverman2c1e0342014-04-11 16:15:01 -0700187 bool Running() { return static_cast<bool>(current_action_); }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800188
189 private:
190 ::std::unique_ptr<Action> current_action_;
191 ::std::unique_ptr<Action> next_action_;
192};
193
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800194
Brian Silverman756f9ff2014-01-17 23:40:23 -0800195class Reader : public ::aos::input::JoystickInput {
196 public:
Austin Schuh58d23682014-02-23 01:39:50 -0800197 Reader()
Brian Silvermane3a277a2014-04-13 14:56:57 -0700198 : is_high_gear_(false),
Austin Schuh9cb836e2014-02-23 19:25:55 -0800199 shot_power_(80.0),
Austin Schuh58d23682014-02-23 01:39:50 -0800200 goal_angle_(0.0),
Brian Silverman545f2ad2014-03-14 12:31:42 -0700201 separation_angle_(kGrabSeparation),
Brian Silvermana379f002014-03-22 19:34:53 -0700202 velocity_compensation_(0.0),
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800203 intake_power_(0.0),
204 was_running_(false) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -0800205
206 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Brian Silverman756f9ff2014-01-17 23:40:23 -0800207 if (data.GetControlBit(ControlBit::kAutonomous)) {
208 if (data.PosEdge(ControlBit::kEnabled)){
209 LOG(INFO, "Starting auto mode\n");
210 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800211 .run_auto(true)
212 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800213 } else if (data.NegEdge(ControlBit::kEnabled)) {
214 LOG(INFO, "Stopping auto mode\n");
215 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800216 .run_auto(false)
217 .Send();
Brian Silverman2c764f02014-04-25 09:21:21 -0500218 } else if (!data.GetControlBit(ControlBit::kEnabled)) {
219 {
220 auto goal = drivetrain.goal.MakeMessage();
221 goal->Zero();
222 goal->control_loop_driving = false;
223 goal->left_goal = goal->right_goal = 0;
224 goal->left_velocity_goal = goal->right_velocity_goal = 0;
225 if (!goal.Send()) {
226 LOG(WARNING, "sending 0 drivetrain goal failed\n");
227 }
228 }
229 {
230 // TODO(brians): Make sure this doesn't make it unbrake and not move
231 // or something weird.
232 auto goal = control_loops::shooter_queue_group.goal.MakeMessage();
233 goal->Zero();
234 if (!goal.Send()) {
235 LOG(WARNING, "sending 0 shooter goal failed\n");
236 }
237 }
Brian Silverman756f9ff2014-01-17 23:40:23 -0800238 }
Austin Schuh58d23682014-02-23 01:39:50 -0800239 } else {
240 HandleTeleop(data);
Brian Silverman756f9ff2014-01-17 23:40:23 -0800241 }
242 }
Austin Schuh58d23682014-02-23 01:39:50 -0800243
244 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
245 bool is_control_loop_driving = false;
246 double left_goal = 0.0;
247 double right_goal = 0.0;
248 const double wheel = -data.GetAxis(kSteeringWheel);
249 const double throttle = -data.GetAxis(kDriveThrottle);
250 const double kThrottleGain = 1.0 / 2.5;
251 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
252 data.IsPressed(kDriveControlLoopEnable2))) {
253 // TODO(austin): Static sucks!
254 static double distance = 0.0;
255 static double angle = 0.0;
256 static double filtered_goal_distance = 0.0;
257 if (data.PosEdge(kDriveControlLoopEnable1) ||
258 data.PosEdge(kDriveControlLoopEnable2)) {
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800259 if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) {
Austin Schuh58d23682014-02-23 01:39:50 -0800260 distance = (drivetrain.position->left_encoder +
261 drivetrain.position->right_encoder) /
262 2.0 -
263 throttle * kThrottleGain / 2.0;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800264 angle = gyro_reading->angle;
Austin Schuh58d23682014-02-23 01:39:50 -0800265 filtered_goal_distance = distance;
266 }
267 }
268 is_control_loop_driving = true;
269
270 // const double gyro_angle = Gyro.View().angle;
271 const double goal_theta = angle - wheel * 0.27;
272 const double goal_distance = distance + throttle * kThrottleGain;
273 const double robot_width = 22.0 / 100.0 * 2.54;
274 const double kMaxVelocity = 0.6;
275 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
276 filtered_goal_distance += kMaxVelocity * 0.02;
277 } else if (goal_distance <
278 -kMaxVelocity * 0.02 + filtered_goal_distance) {
279 filtered_goal_distance -= kMaxVelocity * 0.02;
280 } else {
281 filtered_goal_distance = goal_distance;
282 }
283 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
284 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
285 is_high_gear_ = false;
286
287 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
288 }
289 if (!drivetrain.goal.MakeWithBuilder()
290 .steering(wheel)
291 .throttle(throttle)
292 .highgear(is_high_gear_)
293 .quickturn(data.IsPressed(kQuickTurn))
294 .control_loop_driving(is_control_loop_driving)
295 .left_goal(left_goal)
296 .right_goal(right_goal)
Brian Silverman2c764f02014-04-25 09:21:21 -0500297 .left_velocity_goal(0)
298 .right_velocity_goal(0)
Austin Schuh58d23682014-02-23 01:39:50 -0800299 .Send()) {
300 LOG(WARNING, "sending stick values failed\n");
301 }
302 if (data.PosEdge(kShiftHigh)) {
303 is_high_gear_ = false;
304 }
305 if (data.PosEdge(kShiftLow)) {
306 is_high_gear_ = true;
307 }
308 }
309
310 void SetGoal(ClawGoal goal) {
311 goal_angle_ = goal.angle;
312 separation_angle_ = goal.separation;
Brian7e294392014-03-31 12:39:13 -0700313 moving_for_shot_ = false;
Brian Silvermana379f002014-03-22 19:34:53 -0700314 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800315 intake_power_ = 0.0;
316 }
317
318 void SetGoal(ShotGoal goal) {
319 goal_angle_ = goal.claw.angle;
Brian7e294392014-03-31 12:39:13 -0700320 shot_separation_angle_ = goal.claw.separation;
321 separation_angle_ = kGrabSeparation;
322 moving_for_shot_ = true;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800323 shot_power_ = goal.shot_power;
324 velocity_compensation_ = goal.velocity_compensation;
325 intake_power_ = goal.intake_power;
Austin Schuh58d23682014-02-23 01:39:50 -0800326 }
327
328 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
329 HandleDrivetrain(data);
Austin Schuhc95c2b72014-03-02 11:56:49 -0800330 if (!data.GetControlBit(ControlBit::kEnabled)) {
331 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800332 LOG(DEBUG, "Canceling\n");
Austin Schuhc95c2b72014-03-02 11:56:49 -0800333 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800334 if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) {
335 intake_power_ = 0.0;
Brian Silverman545f2ad2014-03-14 12:31:42 -0700336 separation_angle_ = kGrabSeparation;
Brian7e294392014-03-31 12:39:13 -0700337 moving_for_shot_ = false;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800338 }
Austin Schuh58d23682014-02-23 01:39:50 -0800339
Brian Silverman18f6e642014-03-13 18:52:47 -0700340 static const double kAdjustClawGoalDeadband = 0.08;
341 double claw_goal_adjust = data.GetAxis(kAdjustClawGoal);
342 if (::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) {
343 claw_goal_adjust = 0;
344 } else {
345 claw_goal_adjust = (claw_goal_adjust -
346 ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband
347 : kAdjustClawGoalDeadband)) *
348 0.035;
349 }
350 double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation);
351 if (::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) {
352 claw_separation_adjust = 0;
353 } else {
354 claw_separation_adjust =
355 (claw_separation_adjust -
356 ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband
357 : kAdjustClawGoalDeadband)) *
358 -0.035;
359 }
360
Brian6faa7822014-03-31 18:00:28 -0700361 if (data.GetAxis(kFlipRobot) > 0.9) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700362 claw_goal_adjust += claw_separation_adjust;
363 claw_goal_adjust *= -1;
364
Austin Schuh80ff2e12014-03-08 12:06:19 -0800365 if (data.IsPressed(kIntakeOpenPosition)) {
366 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800367 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800368 SetGoal(kFlippedIntakeOpenGoal);
369 } else if (data.IsPressed(kIntakePosition)) {
370 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800371 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800372 SetGoal(kFlippedIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700373 } else if (data.IsPressed(kVerticalTuck)) {
374 action_queue_.CancelAllActions();
375 LOG(DEBUG, "Canceling\n");
376 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800377 } else if (data.IsPressed(kTuck)) {
378 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800379 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800380 SetGoal(kFlippedTuckGoal);
381 } else if (data.PosEdge(kLongShot)) {
382 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800383 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800384 SetGoal(kFlippedLongShotGoal);
385 } else if (data.PosEdge(kMediumShot)) {
386 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800387 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800388 SetGoal(kFlippedMediumShotGoal);
389 } else if (data.PosEdge(kShortShot)) {
390 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800391 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800392 SetGoal(kFlippedShortShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700393 } else if (data.PosEdge(kHumanPlayerShot)) {
394 action_queue_.CancelAllActions();
395 LOG(DEBUG, "Canceling\n");
Brian Silverman73c48392014-04-05 07:07:41 -0700396 SetGoal(kFlippedHumanShotGoal);
Brian Silverman8d5dda42014-04-06 15:59:49 -0700397 } else if (data.PosEdge(kUserLeft)) {
398 action_queue_.CancelAllActions();
399 LOG(DEBUG, "Canceling\n");
400 SetGoal(kFlipped254PassGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800401 } else if (data.PosEdge(kTrussShot)) {
402 action_queue_.CancelAllActions();
403 LOG(DEBUG, "Canceling\n");
Brian Silverman73c48392014-04-05 07:07:41 -0700404 SetGoal(kFlippedTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800405 }
406 } else {
407 if (data.IsPressed(kIntakeOpenPosition)) {
408 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800409 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800410 SetGoal(kIntakeOpenGoal);
411 } else if (data.IsPressed(kIntakePosition)) {
412 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800413 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800414 SetGoal(kIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700415 } else if (data.IsPressed(kVerticalTuck)) {
416 action_queue_.CancelAllActions();
417 LOG(DEBUG, "Canceling\n");
418 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800419 } else if (data.IsPressed(kTuck)) {
420 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800421 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800422 SetGoal(kTuckGoal);
423 } else if (data.PosEdge(kLongShot)) {
424 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800425 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800426 SetGoal(kLongShotGoal);
427 } else if (data.PosEdge(kMediumShot)) {
428 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800429 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800430 SetGoal(kMediumShotGoal);
431 } else if (data.PosEdge(kShortShot)) {
432 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800433 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800434 SetGoal(kShortShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700435 } else if (data.PosEdge(kHumanPlayerShot)) {
436 action_queue_.CancelAllActions();
437 LOG(DEBUG, "Canceling\n");
438 SetGoal(kHumanShotGoal);
Brian Silverman8d5dda42014-04-06 15:59:49 -0700439 } else if (data.PosEdge(kUserLeft)) {
440 action_queue_.CancelAllActions();
441 LOG(DEBUG, "Canceling\n");
442 SetGoal(k254PassGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800443 } else if (data.PosEdge(kTrussShot)) {
444 action_queue_.CancelAllActions();
445 LOG(DEBUG, "Canceling\n");
446 SetGoal(kTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800447 }
Austin Schuh58d23682014-02-23 01:39:50 -0800448 }
449
Austin Schuhade6d082014-03-09 00:53:06 -0800450 /*
Austin Schuh80ff2e12014-03-08 12:06:19 -0800451 if (data.PosEdge(kCatch)) {
452 auto catch_action = MakeCatchAction();
453 catch_action->GetGoal()->catch_angle = goal_angle_;
454 action_queue_.QueueAction(::std::move(catch_action));
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800455 }
Austin Schuhade6d082014-03-09 00:53:06 -0800456 */
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800457
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800458 if (data.PosEdge(kFire)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800459 action_queue_.QueueAction(actions::MakeShootAction());
Brian Silverman9f863a02014-03-29 17:42:36 -0700460 } else if (data.NegEdge(kFire)) {
461 action_queue_.CancelCurrentAction();
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800462 }
463
464 action_queue_.Tick();
Austin Schuhc95c2b72014-03-02 11:56:49 -0800465 if (data.IsPressed(kUnload) || data.IsPressed(kReload)) {
466 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800467 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800468 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700469 velocity_compensation_ = 0.0;
Austin Schuhc95c2b72014-03-02 11:56:49 -0800470 }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800471
472 // Send out the claw and shooter goals if no actions are running.
473 if (!action_queue_.Running()) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700474 goal_angle_ += claw_goal_adjust;
475 separation_angle_ += claw_separation_adjust;
476
Austin Schuh80ff2e12014-03-08 12:06:19 -0800477 // If the action just ended, turn the intake off and stop velocity
478 // compensating.
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800479 if (was_running_) {
480 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700481 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800482 }
483
484 control_loops::drivetrain.status.FetchLatest();
Brian Silvermana379f002014-03-22 19:34:53 -0700485 double goal_angle = goal_angle_;
486 if (control_loops::drivetrain.status.get()) {
487 goal_angle +=
488 SpeedToAngleOffset(control_loops::drivetrain.status->robot_speed);
489 } else {
490 LOG_INTERVAL(no_drivetrain_status_);
491 }
Brian7e294392014-03-31 12:39:13 -0700492
493 if (moving_for_shot_) {
494 auto &claw_status = control_loops::claw_queue_group.status;
495 claw_status.FetchLatest();
496 if (claw_status.get()) {
Brian6faa7822014-03-31 18:00:28 -0700497 if (::std::abs(claw_status->bottom - goal_angle) < 0.2) {
Brian7e294392014-03-31 12:39:13 -0700498 moving_for_shot_ = false;
499 separation_angle_ = shot_separation_angle_;
500 }
501 }
502 }
503
Austin Schuhade6d082014-03-09 00:53:06 -0800504 double separation_angle = separation_angle_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800505
Austin Schuhade6d082014-03-09 00:53:06 -0800506 if (data.IsPressed(kCatch)) {
507 const double kCatchSeparation = 1.0;
508 goal_angle -= kCatchSeparation / 2.0;
509 separation_angle = kCatchSeparation;
510 }
511
512 bool intaking =
513 data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) ||
514 data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch);
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800515 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800516 .bottom_angle(goal_angle)
Austin Schuhade6d082014-03-09 00:53:06 -0800517 .separation_angle(separation_angle)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800518 .intake(intaking ? 12.0
519 : (data.IsPressed(kRollersOut) ? -12.0
520 : intake_power_))
521 .centering(intaking ? 12.0 : 0.0)
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800522 .Send()) {
523 LOG(WARNING, "sending claw goal failed\n");
524 }
525
526 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
527 .shot_power(shot_power_)
528 .shot_requested(data.IsPressed(kFire))
529 .unload_requested(data.IsPressed(kUnload))
530 .load_requested(data.IsPressed(kReload))
531 .Send()) {
532 LOG(WARNING, "sending shooter goal failed\n");
533 }
Austin Schuh58d23682014-02-23 01:39:50 -0800534 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800535 was_running_ = action_queue_.Running();
536 }
537
538 double SpeedToAngleOffset(double speed) {
539 const frc971::constants::Values &values = frc971::constants::GetValues();
540 // scale speed to a [0.0-1.0] on something close to the max
541 // TODO(austin): Change the scale factor for different shots.
Brian Silvermana379f002014-03-22 19:34:53 -0700542 return (speed / values.drivetrain_max_speed) * velocity_compensation_;
Austin Schuh58d23682014-02-23 01:39:50 -0800543 }
544
Austin Schuh01c652b2014-02-21 23:13:42 -0800545 private:
Austin Schuh58d23682014-02-23 01:39:50 -0800546 bool is_high_gear_;
547 double shot_power_;
548 double goal_angle_;
Brian7e294392014-03-31 12:39:13 -0700549 double separation_angle_, shot_separation_angle_;
Brian Silvermana379f002014-03-22 19:34:53 -0700550 double velocity_compensation_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800551 double intake_power_;
552 bool was_running_;
Brian7e294392014-03-31 12:39:13 -0700553 bool moving_for_shot_ = false;
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800554
555 ActionQueue action_queue_;
Brian Silvermana379f002014-03-22 19:34:53 -0700556
557 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
558 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
559 "no drivetrain status");
Brian Silverman756f9ff2014-01-17 23:40:23 -0800560};
561
562} // namespace joysticks
563} // namespace input
564} // namespace frc971
565
566int main() {
567 ::aos::Init();
568 ::frc971::input::joysticks::Reader reader;
569 reader.Run();
570 ::aos::Cleanup();
571}