blob: 8eee134387b26de46d4b6bc9ee2e234697e046e1 [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 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.
105// TODO(brians): kCloseShot
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700106const ShotGoal kMediumShotGoal = {
107 {-0.95, kShootSeparation}, 105, 0.2, kIntakePower};
108// 3/4" plunger {-0.90, kShootSeparation}, 105, 0.2, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -0800109const ShotGoal kFlippedMediumShotGoal = {
Brian Silverman255b5c12014-04-25 09:21:40 -0500110 {0.865, kShootSeparation}, 120, 0.2, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700111// 3/4" plunger {0.80, kShootSeparation}, 105, 0.2, kIntakePower};
112
Brian Silverman255b5c12014-04-25 09:21:40 -0500113// Shot from the fender.
114// TODO(brians): kFenderShot
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700115const ShotGoal kShortShotGoal = {
Brian Silverman55d7f572014-04-17 14:33:54 -0700116 {-0.68, kShootSeparation}, 115.0, 0.4, kIntakePower};
117const ShotGoal kFlippedShortShotGoal = {
Brian Silverman255b5c12014-04-25 09:21:40 -0500118 {0.63, kShootSeparation}, 115.0, 0.4, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700119
Brian Silverman63ec7502014-03-30 18:09:10 -0700120const ShotGoal kHumanShotGoal = {
121 {-0.90, kShootSeparation}, 140, 0.04, kIntakePower};
Brian Silverman73c48392014-04-05 07:07:41 -0700122const ShotGoal kFlippedHumanShotGoal = {
123 {0.90, kShootSeparation}, 140, 0, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700124const ShotGoal kTrussShotGoal = {
Brian Silverman8d5dda42014-04-06 15:59:49 -0700125 {-0.68, kShootSeparation}, 83.0, 0.4, kIntakePower};
Brian Silverman73c48392014-04-05 07:07:41 -0700126const ShotGoal kFlippedTrussShotGoal = {
Brian Silverman8d5dda42014-04-06 15:59:49 -0700127 {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower};
128
129const ClawGoal k254PassGoal = {-1.95, kGrabSeparation};
130const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation};
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800131
132// Makes a new ShootAction action.
Austin Schuh80ff2e12014-03-08 12:06:19 -0800133::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>
134MakeCatchAction() {
135 return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>(
136 new TypedAction< ::frc971::actions::CatchActionGroup>(
137 &::frc971::actions::catch_action));
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800138}
139
140// A queue which queues Actions and cancels them.
141class ActionQueue {
142 public:
143 // Queues up an action for sending.
144 void QueueAction(::std::unique_ptr<Action> action) {
145 if (current_action_) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800146 LOG(INFO, "Queueing action, canceling prior\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800147 current_action_->Cancel();
148 next_action_ = ::std::move(action);
149 } else {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800150 LOG(INFO, "Queueing action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800151 current_action_ = ::std::move(action);
152 current_action_->Start();
153 }
154 }
155
156 // Cancels the current action, and runs the next one when the current one has
157 // finished.
158 void CancelCurrentAction() {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800159 LOG(INFO, "Canceling current action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800160 if (current_action_) {
161 current_action_->Cancel();
162 }
163 }
164
165 // Cancels all running actions.
166 void CancelAllActions() {
Brian Silverman101b9642014-03-08 12:45:16 -0800167 LOG(DEBUG, "Cancelling all actions\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800168 if (current_action_) {
169 current_action_->Cancel();
170 }
171 next_action_.reset();
172 }
173
174 // Runs the next action when the current one is finished running.
175 void Tick() {
176 if (current_action_) {
177 if (!current_action_->Running()) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800178 LOG(INFO, "Action is done.\n");
179 current_action_ = ::std::move(next_action_);
180 if (current_action_) {
181 LOG(INFO, "Running next action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800182 current_action_->Start();
183 }
184 }
185 }
186 }
187
188 // Returns true if any action is running or could be running.
189 // For a one cycle faster response, call Tick before running this.
Brian Silverman2c1e0342014-04-11 16:15:01 -0700190 bool Running() { return static_cast<bool>(current_action_); }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800191
192 private:
193 ::std::unique_ptr<Action> current_action_;
194 ::std::unique_ptr<Action> next_action_;
195};
196
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800197
Brian Silverman756f9ff2014-01-17 23:40:23 -0800198class Reader : public ::aos::input::JoystickInput {
199 public:
Austin Schuh58d23682014-02-23 01:39:50 -0800200 Reader()
Brian Silvermane3a277a2014-04-13 14:56:57 -0700201 : is_high_gear_(false),
Austin Schuh9cb836e2014-02-23 19:25:55 -0800202 shot_power_(80.0),
Austin Schuh58d23682014-02-23 01:39:50 -0800203 goal_angle_(0.0),
Brian Silverman545f2ad2014-03-14 12:31:42 -0700204 separation_angle_(kGrabSeparation),
Brian Silvermana379f002014-03-22 19:34:53 -0700205 velocity_compensation_(0.0),
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800206 intake_power_(0.0),
207 was_running_(false) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -0800208
209 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Brian Silverman756f9ff2014-01-17 23:40:23 -0800210 if (data.GetControlBit(ControlBit::kAutonomous)) {
211 if (data.PosEdge(ControlBit::kEnabled)){
212 LOG(INFO, "Starting auto mode\n");
213 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800214 .run_auto(true)
215 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800216 } else if (data.NegEdge(ControlBit::kEnabled)) {
217 LOG(INFO, "Stopping auto mode\n");
218 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800219 .run_auto(false)
220 .Send();
Brian Silverman2c764f02014-04-25 09:21:21 -0500221 } else if (!data.GetControlBit(ControlBit::kEnabled)) {
222 {
223 auto goal = drivetrain.goal.MakeMessage();
224 goal->Zero();
225 goal->control_loop_driving = false;
226 goal->left_goal = goal->right_goal = 0;
227 goal->left_velocity_goal = goal->right_velocity_goal = 0;
228 if (!goal.Send()) {
229 LOG(WARNING, "sending 0 drivetrain goal failed\n");
230 }
231 }
232 {
233 // TODO(brians): Make sure this doesn't make it unbrake and not move
234 // or something weird.
235 auto goal = control_loops::shooter_queue_group.goal.MakeMessage();
236 goal->Zero();
237 if (!goal.Send()) {
238 LOG(WARNING, "sending 0 shooter goal failed\n");
239 }
240 }
Brian Silverman756f9ff2014-01-17 23:40:23 -0800241 }
Austin Schuh58d23682014-02-23 01:39:50 -0800242 } else {
243 HandleTeleop(data);
Brian Silverman756f9ff2014-01-17 23:40:23 -0800244 }
245 }
Austin Schuh58d23682014-02-23 01:39:50 -0800246
247 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
248 bool is_control_loop_driving = false;
249 double left_goal = 0.0;
250 double right_goal = 0.0;
251 const double wheel = -data.GetAxis(kSteeringWheel);
252 const double throttle = -data.GetAxis(kDriveThrottle);
253 const double kThrottleGain = 1.0 / 2.5;
254 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
255 data.IsPressed(kDriveControlLoopEnable2))) {
256 // TODO(austin): Static sucks!
257 static double distance = 0.0;
258 static double angle = 0.0;
259 static double filtered_goal_distance = 0.0;
260 if (data.PosEdge(kDriveControlLoopEnable1) ||
261 data.PosEdge(kDriveControlLoopEnable2)) {
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800262 if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) {
Austin Schuh58d23682014-02-23 01:39:50 -0800263 distance = (drivetrain.position->left_encoder +
264 drivetrain.position->right_encoder) /
265 2.0 -
266 throttle * kThrottleGain / 2.0;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800267 angle = gyro_reading->angle;
Austin Schuh58d23682014-02-23 01:39:50 -0800268 filtered_goal_distance = distance;
269 }
270 }
271 is_control_loop_driving = true;
272
273 // const double gyro_angle = Gyro.View().angle;
274 const double goal_theta = angle - wheel * 0.27;
275 const double goal_distance = distance + throttle * kThrottleGain;
276 const double robot_width = 22.0 / 100.0 * 2.54;
277 const double kMaxVelocity = 0.6;
278 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
279 filtered_goal_distance += kMaxVelocity * 0.02;
280 } else if (goal_distance <
281 -kMaxVelocity * 0.02 + filtered_goal_distance) {
282 filtered_goal_distance -= kMaxVelocity * 0.02;
283 } else {
284 filtered_goal_distance = goal_distance;
285 }
286 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
287 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
288 is_high_gear_ = false;
289
290 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
291 }
292 if (!drivetrain.goal.MakeWithBuilder()
293 .steering(wheel)
294 .throttle(throttle)
295 .highgear(is_high_gear_)
296 .quickturn(data.IsPressed(kQuickTurn))
297 .control_loop_driving(is_control_loop_driving)
298 .left_goal(left_goal)
299 .right_goal(right_goal)
Brian Silverman2c764f02014-04-25 09:21:21 -0500300 .left_velocity_goal(0)
301 .right_velocity_goal(0)
Austin Schuh58d23682014-02-23 01:39:50 -0800302 .Send()) {
303 LOG(WARNING, "sending stick values failed\n");
304 }
305 if (data.PosEdge(kShiftHigh)) {
306 is_high_gear_ = false;
307 }
308 if (data.PosEdge(kShiftLow)) {
309 is_high_gear_ = true;
310 }
311 }
312
313 void SetGoal(ClawGoal goal) {
314 goal_angle_ = goal.angle;
315 separation_angle_ = goal.separation;
Brian7e294392014-03-31 12:39:13 -0700316 moving_for_shot_ = false;
Brian Silvermana379f002014-03-22 19:34:53 -0700317 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800318 intake_power_ = 0.0;
319 }
320
321 void SetGoal(ShotGoal goal) {
322 goal_angle_ = goal.claw.angle;
Brian7e294392014-03-31 12:39:13 -0700323 shot_separation_angle_ = goal.claw.separation;
324 separation_angle_ = kGrabSeparation;
325 moving_for_shot_ = true;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800326 shot_power_ = goal.shot_power;
327 velocity_compensation_ = goal.velocity_compensation;
328 intake_power_ = goal.intake_power;
Austin Schuh58d23682014-02-23 01:39:50 -0800329 }
330
331 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
332 HandleDrivetrain(data);
Austin Schuhc95c2b72014-03-02 11:56:49 -0800333 if (!data.GetControlBit(ControlBit::kEnabled)) {
334 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800335 LOG(DEBUG, "Canceling\n");
Austin Schuhc95c2b72014-03-02 11:56:49 -0800336 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800337 if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) {
338 intake_power_ = 0.0;
Brian Silverman545f2ad2014-03-14 12:31:42 -0700339 separation_angle_ = kGrabSeparation;
Brian7e294392014-03-31 12:39:13 -0700340 moving_for_shot_ = false;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800341 }
Austin Schuh58d23682014-02-23 01:39:50 -0800342
Brian Silverman18f6e642014-03-13 18:52:47 -0700343 static const double kAdjustClawGoalDeadband = 0.08;
344 double claw_goal_adjust = data.GetAxis(kAdjustClawGoal);
345 if (::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) {
346 claw_goal_adjust = 0;
347 } else {
348 claw_goal_adjust = (claw_goal_adjust -
349 ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband
350 : kAdjustClawGoalDeadband)) *
351 0.035;
352 }
353 double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation);
354 if (::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) {
355 claw_separation_adjust = 0;
356 } else {
357 claw_separation_adjust =
358 (claw_separation_adjust -
359 ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband
360 : kAdjustClawGoalDeadband)) *
361 -0.035;
362 }
363
Brian6faa7822014-03-31 18:00:28 -0700364 if (data.GetAxis(kFlipRobot) > 0.9) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700365 claw_goal_adjust += claw_separation_adjust;
366 claw_goal_adjust *= -1;
367
Austin Schuh80ff2e12014-03-08 12:06:19 -0800368 if (data.IsPressed(kIntakeOpenPosition)) {
369 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800370 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800371 SetGoal(kFlippedIntakeOpenGoal);
372 } else if (data.IsPressed(kIntakePosition)) {
373 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800374 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800375 SetGoal(kFlippedIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700376 } else if (data.IsPressed(kVerticalTuck)) {
377 action_queue_.CancelAllActions();
378 LOG(DEBUG, "Canceling\n");
379 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800380 } else if (data.IsPressed(kTuck)) {
381 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800382 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800383 SetGoal(kFlippedTuckGoal);
384 } else if (data.PosEdge(kLongShot)) {
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(kFlippedLongShotGoal);
388 } else if (data.PosEdge(kMediumShot)) {
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(kFlippedMediumShotGoal);
392 } else if (data.PosEdge(kShortShot)) {
393 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800394 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800395 SetGoal(kFlippedShortShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700396 } else if (data.PosEdge(kHumanPlayerShot)) {
397 action_queue_.CancelAllActions();
398 LOG(DEBUG, "Canceling\n");
Brian Silverman73c48392014-04-05 07:07:41 -0700399 SetGoal(kFlippedHumanShotGoal);
Brian Silverman8d5dda42014-04-06 15:59:49 -0700400 } else if (data.PosEdge(kUserLeft)) {
401 action_queue_.CancelAllActions();
402 LOG(DEBUG, "Canceling\n");
403 SetGoal(kFlipped254PassGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800404 } else if (data.PosEdge(kTrussShot)) {
405 action_queue_.CancelAllActions();
406 LOG(DEBUG, "Canceling\n");
Brian Silverman73c48392014-04-05 07:07:41 -0700407 SetGoal(kFlippedTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800408 }
409 } else {
410 if (data.IsPressed(kIntakeOpenPosition)) {
411 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800412 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800413 SetGoal(kIntakeOpenGoal);
414 } else if (data.IsPressed(kIntakePosition)) {
415 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800416 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800417 SetGoal(kIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700418 } else if (data.IsPressed(kVerticalTuck)) {
419 action_queue_.CancelAllActions();
420 LOG(DEBUG, "Canceling\n");
421 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800422 } else if (data.IsPressed(kTuck)) {
423 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800424 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800425 SetGoal(kTuckGoal);
426 } else if (data.PosEdge(kLongShot)) {
427 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800428 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800429 SetGoal(kLongShotGoal);
430 } else if (data.PosEdge(kMediumShot)) {
431 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800432 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800433 SetGoal(kMediumShotGoal);
434 } else if (data.PosEdge(kShortShot)) {
435 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800436 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800437 SetGoal(kShortShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700438 } else if (data.PosEdge(kHumanPlayerShot)) {
439 action_queue_.CancelAllActions();
440 LOG(DEBUG, "Canceling\n");
441 SetGoal(kHumanShotGoal);
Brian Silverman8d5dda42014-04-06 15:59:49 -0700442 } else if (data.PosEdge(kUserLeft)) {
443 action_queue_.CancelAllActions();
444 LOG(DEBUG, "Canceling\n");
445 SetGoal(k254PassGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800446 } else if (data.PosEdge(kTrussShot)) {
447 action_queue_.CancelAllActions();
448 LOG(DEBUG, "Canceling\n");
449 SetGoal(kTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800450 }
Austin Schuh58d23682014-02-23 01:39:50 -0800451 }
452
Austin Schuhade6d082014-03-09 00:53:06 -0800453 /*
Austin Schuh80ff2e12014-03-08 12:06:19 -0800454 if (data.PosEdge(kCatch)) {
455 auto catch_action = MakeCatchAction();
456 catch_action->GetGoal()->catch_angle = goal_angle_;
457 action_queue_.QueueAction(::std::move(catch_action));
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800458 }
Austin Schuhade6d082014-03-09 00:53:06 -0800459 */
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800460
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800461 if (data.PosEdge(kFire)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800462 action_queue_.QueueAction(actions::MakeShootAction());
Brian Silverman9f863a02014-03-29 17:42:36 -0700463 } else if (data.NegEdge(kFire)) {
464 action_queue_.CancelCurrentAction();
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800465 }
466
467 action_queue_.Tick();
Austin Schuhc95c2b72014-03-02 11:56:49 -0800468 if (data.IsPressed(kUnload) || data.IsPressed(kReload)) {
469 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800470 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800471 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700472 velocity_compensation_ = 0.0;
Austin Schuhc95c2b72014-03-02 11:56:49 -0800473 }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800474
475 // Send out the claw and shooter goals if no actions are running.
476 if (!action_queue_.Running()) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700477 goal_angle_ += claw_goal_adjust;
478 separation_angle_ += claw_separation_adjust;
479
Austin Schuh80ff2e12014-03-08 12:06:19 -0800480 // If the action just ended, turn the intake off and stop velocity
481 // compensating.
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800482 if (was_running_) {
483 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700484 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800485 }
486
487 control_loops::drivetrain.status.FetchLatest();
Brian Silvermana379f002014-03-22 19:34:53 -0700488 double goal_angle = goal_angle_;
489 if (control_loops::drivetrain.status.get()) {
490 goal_angle +=
491 SpeedToAngleOffset(control_loops::drivetrain.status->robot_speed);
492 } else {
493 LOG_INTERVAL(no_drivetrain_status_);
494 }
Brian7e294392014-03-31 12:39:13 -0700495
496 if (moving_for_shot_) {
497 auto &claw_status = control_loops::claw_queue_group.status;
498 claw_status.FetchLatest();
499 if (claw_status.get()) {
Brian6faa7822014-03-31 18:00:28 -0700500 if (::std::abs(claw_status->bottom - goal_angle) < 0.2) {
Brian7e294392014-03-31 12:39:13 -0700501 moving_for_shot_ = false;
502 separation_angle_ = shot_separation_angle_;
503 }
504 }
505 }
506
Austin Schuhade6d082014-03-09 00:53:06 -0800507 double separation_angle = separation_angle_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800508
Austin Schuhade6d082014-03-09 00:53:06 -0800509 if (data.IsPressed(kCatch)) {
510 const double kCatchSeparation = 1.0;
511 goal_angle -= kCatchSeparation / 2.0;
512 separation_angle = kCatchSeparation;
513 }
514
515 bool intaking =
516 data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) ||
517 data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch);
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800518 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800519 .bottom_angle(goal_angle)
Austin Schuhade6d082014-03-09 00:53:06 -0800520 .separation_angle(separation_angle)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800521 .intake(intaking ? 12.0
522 : (data.IsPressed(kRollersOut) ? -12.0
523 : intake_power_))
524 .centering(intaking ? 12.0 : 0.0)
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800525 .Send()) {
526 LOG(WARNING, "sending claw goal failed\n");
527 }
528
529 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
530 .shot_power(shot_power_)
531 .shot_requested(data.IsPressed(kFire))
532 .unload_requested(data.IsPressed(kUnload))
533 .load_requested(data.IsPressed(kReload))
534 .Send()) {
535 LOG(WARNING, "sending shooter goal failed\n");
536 }
Austin Schuh58d23682014-02-23 01:39:50 -0800537 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800538 was_running_ = action_queue_.Running();
539 }
540
541 double SpeedToAngleOffset(double speed) {
542 const frc971::constants::Values &values = frc971::constants::GetValues();
543 // scale speed to a [0.0-1.0] on something close to the max
544 // TODO(austin): Change the scale factor for different shots.
Brian Silvermana379f002014-03-22 19:34:53 -0700545 return (speed / values.drivetrain_max_speed) * velocity_compensation_;
Austin Schuh58d23682014-02-23 01:39:50 -0800546 }
547
Austin Schuh01c652b2014-02-21 23:13:42 -0800548 private:
Austin Schuh58d23682014-02-23 01:39:50 -0800549 bool is_high_gear_;
550 double shot_power_;
551 double goal_angle_;
Brian7e294392014-03-31 12:39:13 -0700552 double separation_angle_, shot_separation_angle_;
Brian Silvermana379f002014-03-22 19:34:53 -0700553 double velocity_compensation_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800554 double intake_power_;
555 bool was_running_;
Brian7e294392014-03-31 12:39:13 -0700556 bool moving_for_shot_ = false;
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800557
558 ActionQueue action_queue_;
Brian Silvermana379f002014-03-22 19:34:53 -0700559
560 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
561 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
562 "no drivetrain status");
Brian Silverman756f9ff2014-01-17 23:40:23 -0800563};
564
565} // namespace joysticks
566} // namespace input
567} // namespace frc971
568
569int main() {
570 ::aos::Init();
571 ::frc971::input::joysticks::Reader reader;
572 reader.Run();
573 ::aos::Cleanup();
574}