blob: ecb93f21afe7dbb935a3d1c7cc480bd1be05b25d [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 Silverman0d09e3b2014-04-10 21:15:31 -070098 {-1.08, kShootSeparation}, 140, 0.04, kIntakePower};
99// 34" {-1.06, kShootSeparation}, 140, 0.04, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -0800100const ShotGoal kFlippedLongShotGoal = {
Brian Silverman73c48392014-04-05 07:07:41 -0700101 {0.96, kShootSeparation}, 140, 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 Silverman63ec7502014-03-30 18:09:10 -0700113 {0.67, kShootSeparation}, 115.0, 0.4, kIntakePower};
Brian Silverman73c48392014-04-05 07:07:41 -0700114const ShotGoal kFlippedShortShotGoal = kShortShotGoal;
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700115
Brian Silverman63ec7502014-03-30 18:09:10 -0700116const ShotGoal kHumanShotGoal = {
117 {-0.90, kShootSeparation}, 140, 0.04, kIntakePower};
Brian Silverman73c48392014-04-05 07:07:41 -0700118const ShotGoal kFlippedHumanShotGoal = {
119 {0.90, kShootSeparation}, 140, 0, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700120const ShotGoal kTrussShotGoal = {
Brian Silverman8d5dda42014-04-06 15:59:49 -0700121 {-0.68, kShootSeparation}, 83.0, 0.4, kIntakePower};
Brian Silverman73c48392014-04-05 07:07:41 -0700122const ShotGoal kFlippedTrussShotGoal = {
Brian Silverman8d5dda42014-04-06 15:59:49 -0700123 {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower};
124
125const ClawGoal k254PassGoal = {-1.95, kGrabSeparation};
126const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation};
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800127
128// Makes a new ShootAction action.
Austin Schuh80ff2e12014-03-08 12:06:19 -0800129::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>
130MakeCatchAction() {
131 return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>(
132 new TypedAction< ::frc971::actions::CatchActionGroup>(
133 &::frc971::actions::catch_action));
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800134}
135
136// A queue which queues Actions and cancels them.
137class ActionQueue {
138 public:
139 // Queues up an action for sending.
140 void QueueAction(::std::unique_ptr<Action> action) {
141 if (current_action_) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800142 LOG(INFO, "Queueing action, canceling prior\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800143 current_action_->Cancel();
144 next_action_ = ::std::move(action);
145 } else {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800146 LOG(INFO, "Queueing action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800147 current_action_ = ::std::move(action);
148 current_action_->Start();
149 }
150 }
151
152 // Cancels the current action, and runs the next one when the current one has
153 // finished.
154 void CancelCurrentAction() {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800155 LOG(INFO, "Canceling current action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800156 if (current_action_) {
157 current_action_->Cancel();
158 }
159 }
160
161 // Cancels all running actions.
162 void CancelAllActions() {
Brian Silverman101b9642014-03-08 12:45:16 -0800163 LOG(DEBUG, "Cancelling all actions\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800164 if (current_action_) {
165 current_action_->Cancel();
166 }
167 next_action_.reset();
168 }
169
170 // Runs the next action when the current one is finished running.
171 void Tick() {
172 if (current_action_) {
173 if (!current_action_->Running()) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800174 LOG(INFO, "Action is done.\n");
175 current_action_ = ::std::move(next_action_);
176 if (current_action_) {
177 LOG(INFO, "Running next action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800178 current_action_->Start();
179 }
180 }
181 }
182 }
183
184 // Returns true if any action is running or could be running.
185 // For a one cycle faster response, call Tick before running this.
186 bool Running() { return (bool)current_action_; }
187
188 private:
189 ::std::unique_ptr<Action> current_action_;
190 ::std::unique_ptr<Action> next_action_;
191};
192
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800193
Brian Silverman756f9ff2014-01-17 23:40:23 -0800194class Reader : public ::aos::input::JoystickInput {
195 public:
Austin Schuh58d23682014-02-23 01:39:50 -0800196 Reader()
197 : is_high_gear_(false),
Austin Schuh9cb836e2014-02-23 19:25:55 -0800198 shot_power_(80.0),
Austin Schuh58d23682014-02-23 01:39:50 -0800199 goal_angle_(0.0),
Brian Silverman545f2ad2014-03-14 12:31:42 -0700200 separation_angle_(kGrabSeparation),
Brian Silvermana379f002014-03-22 19:34:53 -0700201 velocity_compensation_(0.0),
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800202 intake_power_(0.0),
203 was_running_(false) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -0800204
205 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Brian Silverman756f9ff2014-01-17 23:40:23 -0800206 if (data.GetControlBit(ControlBit::kAutonomous)) {
207 if (data.PosEdge(ControlBit::kEnabled)){
208 LOG(INFO, "Starting auto mode\n");
209 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800210 .run_auto(true)
211 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800212 } else if (data.NegEdge(ControlBit::kEnabled)) {
213 LOG(INFO, "Stopping auto mode\n");
214 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800215 .run_auto(false)
216 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800217 }
Austin Schuh58d23682014-02-23 01:39:50 -0800218 } else {
219 HandleTeleop(data);
Brian Silverman756f9ff2014-01-17 23:40:23 -0800220 }
221 }
Austin Schuh58d23682014-02-23 01:39:50 -0800222
223 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
224 bool is_control_loop_driving = false;
225 double left_goal = 0.0;
226 double right_goal = 0.0;
227 const double wheel = -data.GetAxis(kSteeringWheel);
228 const double throttle = -data.GetAxis(kDriveThrottle);
229 const double kThrottleGain = 1.0 / 2.5;
230 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
231 data.IsPressed(kDriveControlLoopEnable2))) {
232 // TODO(austin): Static sucks!
233 static double distance = 0.0;
234 static double angle = 0.0;
235 static double filtered_goal_distance = 0.0;
236 if (data.PosEdge(kDriveControlLoopEnable1) ||
237 data.PosEdge(kDriveControlLoopEnable2)) {
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800238 if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) {
Austin Schuh58d23682014-02-23 01:39:50 -0800239 distance = (drivetrain.position->left_encoder +
240 drivetrain.position->right_encoder) /
241 2.0 -
242 throttle * kThrottleGain / 2.0;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800243 angle = gyro_reading->angle;
Austin Schuh58d23682014-02-23 01:39:50 -0800244 filtered_goal_distance = distance;
245 }
246 }
247 is_control_loop_driving = true;
248
249 // const double gyro_angle = Gyro.View().angle;
250 const double goal_theta = angle - wheel * 0.27;
251 const double goal_distance = distance + throttle * kThrottleGain;
252 const double robot_width = 22.0 / 100.0 * 2.54;
253 const double kMaxVelocity = 0.6;
254 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
255 filtered_goal_distance += kMaxVelocity * 0.02;
256 } else if (goal_distance <
257 -kMaxVelocity * 0.02 + filtered_goal_distance) {
258 filtered_goal_distance -= kMaxVelocity * 0.02;
259 } else {
260 filtered_goal_distance = goal_distance;
261 }
262 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
263 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
264 is_high_gear_ = false;
265
266 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
267 }
268 if (!drivetrain.goal.MakeWithBuilder()
269 .steering(wheel)
270 .throttle(throttle)
271 .highgear(is_high_gear_)
272 .quickturn(data.IsPressed(kQuickTurn))
273 .control_loop_driving(is_control_loop_driving)
274 .left_goal(left_goal)
275 .right_goal(right_goal)
276 .Send()) {
277 LOG(WARNING, "sending stick values failed\n");
278 }
279 if (data.PosEdge(kShiftHigh)) {
280 is_high_gear_ = false;
281 }
282 if (data.PosEdge(kShiftLow)) {
283 is_high_gear_ = true;
284 }
285 }
286
287 void SetGoal(ClawGoal goal) {
288 goal_angle_ = goal.angle;
289 separation_angle_ = goal.separation;
Brian7e294392014-03-31 12:39:13 -0700290 moving_for_shot_ = false;
Brian Silvermana379f002014-03-22 19:34:53 -0700291 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800292 intake_power_ = 0.0;
293 }
294
295 void SetGoal(ShotGoal goal) {
296 goal_angle_ = goal.claw.angle;
Brian7e294392014-03-31 12:39:13 -0700297 shot_separation_angle_ = goal.claw.separation;
298 separation_angle_ = kGrabSeparation;
299 moving_for_shot_ = true;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800300 shot_power_ = goal.shot_power;
301 velocity_compensation_ = goal.velocity_compensation;
302 intake_power_ = goal.intake_power;
Austin Schuh58d23682014-02-23 01:39:50 -0800303 }
304
305 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
306 HandleDrivetrain(data);
Austin Schuhc95c2b72014-03-02 11:56:49 -0800307 if (!data.GetControlBit(ControlBit::kEnabled)) {
308 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800309 LOG(DEBUG, "Canceling\n");
Austin Schuhc95c2b72014-03-02 11:56:49 -0800310 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800311 if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) {
312 intake_power_ = 0.0;
Brian Silverman545f2ad2014-03-14 12:31:42 -0700313 separation_angle_ = kGrabSeparation;
Brian7e294392014-03-31 12:39:13 -0700314 moving_for_shot_ = false;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800315 }
Austin Schuh58d23682014-02-23 01:39:50 -0800316
Brian Silverman18f6e642014-03-13 18:52:47 -0700317 static const double kAdjustClawGoalDeadband = 0.08;
318 double claw_goal_adjust = data.GetAxis(kAdjustClawGoal);
319 if (::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) {
320 claw_goal_adjust = 0;
321 } else {
322 claw_goal_adjust = (claw_goal_adjust -
323 ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband
324 : kAdjustClawGoalDeadband)) *
325 0.035;
326 }
327 double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation);
328 if (::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) {
329 claw_separation_adjust = 0;
330 } else {
331 claw_separation_adjust =
332 (claw_separation_adjust -
333 ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband
334 : kAdjustClawGoalDeadband)) *
335 -0.035;
336 }
337
Brian6faa7822014-03-31 18:00:28 -0700338 if (data.GetAxis(kFlipRobot) > 0.9) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700339 claw_goal_adjust += claw_separation_adjust;
340 claw_goal_adjust *= -1;
341
Austin Schuh80ff2e12014-03-08 12:06:19 -0800342 if (data.IsPressed(kIntakeOpenPosition)) {
343 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800344 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800345 SetGoal(kFlippedIntakeOpenGoal);
346 } else if (data.IsPressed(kIntakePosition)) {
347 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800348 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800349 SetGoal(kFlippedIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700350 } else if (data.IsPressed(kVerticalTuck)) {
351 action_queue_.CancelAllActions();
352 LOG(DEBUG, "Canceling\n");
353 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800354 } else if (data.IsPressed(kTuck)) {
355 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800356 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800357 SetGoal(kFlippedTuckGoal);
358 } else if (data.PosEdge(kLongShot)) {
359 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800360 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800361 SetGoal(kFlippedLongShotGoal);
362 } else if (data.PosEdge(kMediumShot)) {
363 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800364 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800365 SetGoal(kFlippedMediumShotGoal);
366 } else if (data.PosEdge(kShortShot)) {
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(kFlippedShortShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700370 } else if (data.PosEdge(kHumanPlayerShot)) {
371 action_queue_.CancelAllActions();
372 LOG(DEBUG, "Canceling\n");
Brian Silverman73c48392014-04-05 07:07:41 -0700373 SetGoal(kFlippedHumanShotGoal);
Brian Silverman8d5dda42014-04-06 15:59:49 -0700374 } else if (data.PosEdge(kUserLeft)) {
375 action_queue_.CancelAllActions();
376 LOG(DEBUG, "Canceling\n");
377 SetGoal(kFlipped254PassGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800378 } else if (data.PosEdge(kTrussShot)) {
379 action_queue_.CancelAllActions();
380 LOG(DEBUG, "Canceling\n");
Brian Silverman73c48392014-04-05 07:07:41 -0700381 SetGoal(kFlippedTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800382 }
383 } else {
384 if (data.IsPressed(kIntakeOpenPosition)) {
385 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800386 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800387 SetGoal(kIntakeOpenGoal);
388 } else if (data.IsPressed(kIntakePosition)) {
389 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800390 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800391 SetGoal(kIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700392 } else if (data.IsPressed(kVerticalTuck)) {
393 action_queue_.CancelAllActions();
394 LOG(DEBUG, "Canceling\n");
395 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800396 } else if (data.IsPressed(kTuck)) {
397 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800398 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800399 SetGoal(kTuckGoal);
400 } else if (data.PosEdge(kLongShot)) {
401 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800402 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800403 SetGoal(kLongShotGoal);
404 } else if (data.PosEdge(kMediumShot)) {
405 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800406 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800407 SetGoal(kMediumShotGoal);
408 } else if (data.PosEdge(kShortShot)) {
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(kShortShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700412 } else if (data.PosEdge(kHumanPlayerShot)) {
413 action_queue_.CancelAllActions();
414 LOG(DEBUG, "Canceling\n");
415 SetGoal(kHumanShotGoal);
Brian Silverman8d5dda42014-04-06 15:59:49 -0700416 } else if (data.PosEdge(kUserLeft)) {
417 action_queue_.CancelAllActions();
418 LOG(DEBUG, "Canceling\n");
419 SetGoal(k254PassGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800420 } else if (data.PosEdge(kTrussShot)) {
421 action_queue_.CancelAllActions();
422 LOG(DEBUG, "Canceling\n");
423 SetGoal(kTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800424 }
Austin Schuh58d23682014-02-23 01:39:50 -0800425 }
426
Austin Schuhade6d082014-03-09 00:53:06 -0800427 /*
Austin Schuh80ff2e12014-03-08 12:06:19 -0800428 if (data.PosEdge(kCatch)) {
429 auto catch_action = MakeCatchAction();
430 catch_action->GetGoal()->catch_angle = goal_angle_;
431 action_queue_.QueueAction(::std::move(catch_action));
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800432 }
Austin Schuhade6d082014-03-09 00:53:06 -0800433 */
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800434
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800435 if (data.PosEdge(kFire)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800436 action_queue_.QueueAction(actions::MakeShootAction());
Brian Silverman9f863a02014-03-29 17:42:36 -0700437 } else if (data.NegEdge(kFire)) {
438 action_queue_.CancelCurrentAction();
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800439 }
440
441 action_queue_.Tick();
Austin Schuhc95c2b72014-03-02 11:56:49 -0800442 if (data.IsPressed(kUnload) || data.IsPressed(kReload)) {
443 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800444 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800445 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700446 velocity_compensation_ = 0.0;
Austin Schuhc95c2b72014-03-02 11:56:49 -0800447 }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800448
449 // Send out the claw and shooter goals if no actions are running.
450 if (!action_queue_.Running()) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700451 goal_angle_ += claw_goal_adjust;
452 separation_angle_ += claw_separation_adjust;
453
Austin Schuh80ff2e12014-03-08 12:06:19 -0800454 // If the action just ended, turn the intake off and stop velocity
455 // compensating.
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800456 if (was_running_) {
457 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700458 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800459 }
460
461 control_loops::drivetrain.status.FetchLatest();
Brian Silvermana379f002014-03-22 19:34:53 -0700462 double goal_angle = goal_angle_;
463 if (control_loops::drivetrain.status.get()) {
464 goal_angle +=
465 SpeedToAngleOffset(control_loops::drivetrain.status->robot_speed);
466 } else {
467 LOG_INTERVAL(no_drivetrain_status_);
468 }
Brian7e294392014-03-31 12:39:13 -0700469
470 if (moving_for_shot_) {
471 auto &claw_status = control_loops::claw_queue_group.status;
472 claw_status.FetchLatest();
473 if (claw_status.get()) {
Brian6faa7822014-03-31 18:00:28 -0700474 if (::std::abs(claw_status->bottom - goal_angle) < 0.2) {
Brian7e294392014-03-31 12:39:13 -0700475 moving_for_shot_ = false;
476 separation_angle_ = shot_separation_angle_;
477 }
478 }
479 }
480
Austin Schuhade6d082014-03-09 00:53:06 -0800481 double separation_angle = separation_angle_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800482
Austin Schuhade6d082014-03-09 00:53:06 -0800483 if (data.IsPressed(kCatch)) {
484 const double kCatchSeparation = 1.0;
485 goal_angle -= kCatchSeparation / 2.0;
486 separation_angle = kCatchSeparation;
487 }
488
489 bool intaking =
490 data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) ||
491 data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch);
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800492 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800493 .bottom_angle(goal_angle)
Austin Schuhade6d082014-03-09 00:53:06 -0800494 .separation_angle(separation_angle)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800495 .intake(intaking ? 12.0
496 : (data.IsPressed(kRollersOut) ? -12.0
497 : intake_power_))
498 .centering(intaking ? 12.0 : 0.0)
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800499 .Send()) {
500 LOG(WARNING, "sending claw goal failed\n");
501 }
502
503 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
504 .shot_power(shot_power_)
505 .shot_requested(data.IsPressed(kFire))
506 .unload_requested(data.IsPressed(kUnload))
507 .load_requested(data.IsPressed(kReload))
508 .Send()) {
509 LOG(WARNING, "sending shooter goal failed\n");
510 }
Austin Schuh58d23682014-02-23 01:39:50 -0800511 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800512 was_running_ = action_queue_.Running();
513 }
514
515 double SpeedToAngleOffset(double speed) {
516 const frc971::constants::Values &values = frc971::constants::GetValues();
517 // scale speed to a [0.0-1.0] on something close to the max
518 // TODO(austin): Change the scale factor for different shots.
Brian Silvermana379f002014-03-22 19:34:53 -0700519 return (speed / values.drivetrain_max_speed) * velocity_compensation_;
Austin Schuh58d23682014-02-23 01:39:50 -0800520 }
521
Austin Schuh01c652b2014-02-21 23:13:42 -0800522 private:
Austin Schuh58d23682014-02-23 01:39:50 -0800523 bool is_high_gear_;
524 double shot_power_;
525 double goal_angle_;
Brian7e294392014-03-31 12:39:13 -0700526 double separation_angle_, shot_separation_angle_;
Brian Silvermana379f002014-03-22 19:34:53 -0700527 double velocity_compensation_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800528 double intake_power_;
529 bool was_running_;
Brian7e294392014-03-31 12:39:13 -0700530 bool moving_for_shot_ = false;
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800531
532 ActionQueue action_queue_;
Brian Silvermana379f002014-03-22 19:34:53 -0700533
534 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
535 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
536 "no drivetrain status");
Brian Silverman756f9ff2014-01-17 23:40:23 -0800537};
538
539} // namespace joysticks
540} // namespace input
541} // namespace frc971
542
543int main() {
544 ::aos::Init();
545 ::frc971::input::joysticks::Reader reader;
546 reader.Run();
547 ::aos::Cleanup();
548}