blob: d134aa814208f1a2707ac8495ae860c7f8f16fd9 [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
Brian Silverman5e1eb642014-07-20 16:37:35 -070031#define OLD_DS 0
32
Brian Silverman756f9ff2014-01-17 23:40:23 -080033namespace frc971 {
34namespace input {
35namespace joysticks {
36
37const ButtonLocation kDriveControlLoopEnable1(1, 7),
38 kDriveControlLoopEnable2(1, 11);
39const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
40const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
41const ButtonLocation kQuickTurn(1, 5);
Austin Schuh58d23682014-02-23 01:39:50 -080042
Austin Schuh80ff2e12014-03-08 12:06:19 -080043const ButtonLocation kCatch(3, 10);
44
Brian Silverman5e1eb642014-07-20 16:37:35 -070045#if OLD_DS
46const ButtonLocation kFire(3, 11);
47const ButtonLocation kUnload(1, 4);
48const ButtonLocation kReload(1, 2);
49
50const ButtonLocation kRollersOut(3, 12);
51const ButtonLocation kRollersIn(3, 7);
52
53const ButtonLocation kTuck(3, 9);
54const ButtonLocation kIntakePosition(3, 8);
55const ButtonLocation kIntakeOpenPosition(3, 10);
56const ButtonLocation kVerticalTuck(3, 1);
57const JoystickAxis kFlipRobot(3, 3);
58
59const ButtonLocation kLongShot(3, 5);
60const ButtonLocation kCloseShot(3, 2);
61const ButtonLocation kFenderShot(3, 6);
62const ButtonLocation kTrussShot(2, 11);
63const ButtonLocation kHumanPlayerShot(3, 2);
64#else
Austin Schuh80ff2e12014-03-08 12:06:19 -080065const ButtonLocation kFire(3, 9);
Brian6faa7822014-03-31 18:00:28 -070066const ButtonLocation kUnload(1, 4);
67const ButtonLocation kReload(1, 2);
Austin Schuh58d23682014-02-23 01:39:50 -080068
Austin Schuh80ff2e12014-03-08 12:06:19 -080069const ButtonLocation kRollersOut(3, 8);
70const ButtonLocation kRollersIn(3, 3);
Austin Schuh58d23682014-02-23 01:39:50 -080071
Austin Schuh80ff2e12014-03-08 12:06:19 -080072const ButtonLocation kTuck(3, 4);
73const ButtonLocation kIntakePosition(3, 5);
74const ButtonLocation kIntakeOpenPosition(3, 11);
Brian6faa7822014-03-31 18:00:28 -070075const ButtonLocation kVerticalTuck(2, 6);
Austin Schuh80ff2e12014-03-08 12:06:19 -080076const JoystickAxis kFlipRobot(3, 3);
Austin Schuh58d23682014-02-23 01:39:50 -080077
Austin Schuh80ff2e12014-03-08 12:06:19 -080078const ButtonLocation kLongShot(3, 7);
Brian Silverman4051c6f2014-07-19 16:57:37 -070079const ButtonLocation kCloseShot(3, 6);
80const ButtonLocation kFenderShot(3, 2);
Brian6faa7822014-03-31 18:00:28 -070081const ButtonLocation kTrussShot(2, 11);
82const ButtonLocation kHumanPlayerShot(3, 1);
Brian Silverman5e1eb642014-07-20 16:37:35 -070083#endif
Brian6faa7822014-03-31 18:00:28 -070084
85const ButtonLocation kUserLeft(2, 7);
86const ButtonLocation kUserRight(2, 10);
Austin Schuh58d23682014-02-23 01:39:50 -080087
Brian Silverman18f6e642014-03-13 18:52:47 -070088const JoystickAxis kAdjustClawGoal(3, 2);
89const JoystickAxis kAdjustClawSeparation(3, 1);
90
Austin Schuh58d23682014-02-23 01:39:50 -080091struct ClawGoal {
92 double angle;
93 double separation;
94};
95
Austin Schuh5d8c5e72014-03-07 20:24:34 -080096struct ShotGoal {
97 ClawGoal claw;
98 double shot_power;
Brian Silvermana379f002014-03-22 19:34:53 -070099 double velocity_compensation;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800100 double intake_power;
101};
102
Brian Silverman545f2ad2014-03-14 12:31:42 -0700103const double kIntakePower = 4.0;
Brian Silverman255b5c12014-04-25 09:21:40 -0500104// In case we have to quickly adjust it.
Brian Silvermanb3cf0ef2014-03-22 12:45:55 -0700105const double kGrabSeparation = 0;
Brian Silverman545f2ad2014-03-14 12:31:42 -0700106const double kShootSeparation = 0.11 + kGrabSeparation;
107
Austin Schuh58d23682014-02-23 01:39:50 -0800108const ClawGoal kTuckGoal = {-2.273474, -0.749484};
Brian6faa7822014-03-31 18:00:28 -0700109const ClawGoal kVerticalTuckGoal = {0, kGrabSeparation};
Brian Silverman63ec7502014-03-30 18:09:10 -0700110const ClawGoal kIntakeGoal = {-2.24, kGrabSeparation};
111const ClawGoal kIntakeOpenGoal = {-2.0, 1.1};
Austin Schuh58d23682014-02-23 01:39:50 -0800112
Austin Schuh80ff2e12014-03-08 12:06:19 -0800113// TODO(austin): Tune these by hand...
114const ClawGoal kFlippedTuckGoal = {2.733474, -0.75};
Brian Silverman545f2ad2014-03-14 12:31:42 -0700115const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation};
Austin Schuh80ff2e12014-03-08 12:06:19 -0800116const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0};
117
Brian Silverman255b5c12014-04-25 09:21:40 -0500118// 34" between near edge of colored line and rear edge of bumper.
119// Only works running?
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800120const ShotGoal kLongShotGoal = {
Brian Silverman55d7f572014-04-17 14:33:54 -0700121 {-1.08, kShootSeparation}, 145, 0.04, kIntakePower};
Brian Silverman255b5c12014-04-25 09:21:40 -0500122// old 34" {-1.06, kShootSeparation}, 140, 0.04, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -0800123const ShotGoal kFlippedLongShotGoal = {
Brian Silverman55d7f572014-04-17 14:33:54 -0700124 {0.96, kShootSeparation}, 145, 0.09, kIntakePower};
Brian Silverman255b5c12014-04-25 09:21:40 -0500125// old 34" {0.96, kShootSeparation}, 140, 0.09, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700126
Brian Silverman255b5c12014-04-25 09:21:40 -0500127// 78" between near edge of colored line and rear edge of bumper.
Brian Silverman4051c6f2014-07-19 16:57:37 -0700128const ShotGoal kCloseShotGoal = {
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700129 {-0.95, kShootSeparation}, 105, 0.2, kIntakePower};
130// 3/4" plunger {-0.90, kShootSeparation}, 105, 0.2, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -0800131const ShotGoal kFlippedMediumShotGoal = {
Brian Silverman255b5c12014-04-25 09:21:40 -0500132 {0.865, kShootSeparation}, 120, 0.2, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700133// 3/4" plunger {0.80, kShootSeparation}, 105, 0.2, kIntakePower};
134
Brian Silverman255b5c12014-04-25 09:21:40 -0500135// Shot from the fender.
Brian Silverman4051c6f2014-07-19 16:57:37 -0700136const ShotGoal kFenderShotGoal = {
Brian Silverman55d7f572014-04-17 14:33:54 -0700137 {-0.68, kShootSeparation}, 115.0, 0.4, kIntakePower};
138const ShotGoal kFlippedShortShotGoal = {
Brian Silverman255b5c12014-04-25 09:21:40 -0500139 {0.63, kShootSeparation}, 115.0, 0.4, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700140
Brian Silverman63ec7502014-03-30 18:09:10 -0700141const ShotGoal kHumanShotGoal = {
142 {-0.90, kShootSeparation}, 140, 0.04, kIntakePower};
Brian Silverman73c48392014-04-05 07:07:41 -0700143const ShotGoal kFlippedHumanShotGoal = {
144 {0.90, kShootSeparation}, 140, 0, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700145const ShotGoal kTrussShotGoal = {
Brian Silverman8d5dda42014-04-06 15:59:49 -0700146 {-0.68, kShootSeparation}, 83.0, 0.4, kIntakePower};
Brian Silverman73c48392014-04-05 07:07:41 -0700147const ShotGoal kFlippedTrussShotGoal = {
Brian Silverman8d5dda42014-04-06 15:59:49 -0700148 {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower};
149
Brian Silverman8cd51202014-07-19 16:58:08 -0700150const ShotGoal kFlippedDemoShotGoal = {
151 {1.0, kShootSeparation}, 65.0, 0.0, kIntakePower};
152const ShotGoal kDemoShotGoal = {
153 {-1.0, kShootSeparation}, 50.0, 0.0, kIntakePower};
154
Brian Silverman8d5dda42014-04-06 15:59:49 -0700155const ClawGoal k254PassGoal = {-1.95, kGrabSeparation};
156const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation};
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800157
158// Makes a new ShootAction action.
Austin Schuh80ff2e12014-03-08 12:06:19 -0800159::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>
160MakeCatchAction() {
161 return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>(
162 new TypedAction< ::frc971::actions::CatchActionGroup>(
163 &::frc971::actions::catch_action));
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800164}
165
166// A queue which queues Actions and cancels them.
167class ActionQueue {
168 public:
169 // Queues up an action for sending.
170 void QueueAction(::std::unique_ptr<Action> action) {
171 if (current_action_) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800172 LOG(INFO, "Queueing action, canceling prior\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800173 current_action_->Cancel();
174 next_action_ = ::std::move(action);
175 } else {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800176 LOG(INFO, "Queueing action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800177 current_action_ = ::std::move(action);
178 current_action_->Start();
179 }
180 }
181
182 // Cancels the current action, and runs the next one when the current one has
183 // finished.
184 void CancelCurrentAction() {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800185 LOG(INFO, "Canceling current action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800186 if (current_action_) {
187 current_action_->Cancel();
188 }
189 }
190
191 // Cancels all running actions.
192 void CancelAllActions() {
Brian Silverman101b9642014-03-08 12:45:16 -0800193 LOG(DEBUG, "Cancelling all actions\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800194 if (current_action_) {
195 current_action_->Cancel();
196 }
197 next_action_.reset();
198 }
199
200 // Runs the next action when the current one is finished running.
201 void Tick() {
202 if (current_action_) {
203 if (!current_action_->Running()) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800204 LOG(INFO, "Action is done.\n");
205 current_action_ = ::std::move(next_action_);
206 if (current_action_) {
207 LOG(INFO, "Running next action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800208 current_action_->Start();
209 }
210 }
211 }
212 }
213
214 // Returns true if any action is running or could be running.
215 // For a one cycle faster response, call Tick before running this.
Brian Silverman2c1e0342014-04-11 16:15:01 -0700216 bool Running() { return static_cast<bool>(current_action_); }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800217
218 private:
219 ::std::unique_ptr<Action> current_action_;
220 ::std::unique_ptr<Action> next_action_;
221};
222
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800223
Brian Silverman756f9ff2014-01-17 23:40:23 -0800224class Reader : public ::aos::input::JoystickInput {
225 public:
Austin Schuh58d23682014-02-23 01:39:50 -0800226 Reader()
Brian Silvermane3a277a2014-04-13 14:56:57 -0700227 : is_high_gear_(false),
Austin Schuh9cb836e2014-02-23 19:25:55 -0800228 shot_power_(80.0),
Austin Schuh58d23682014-02-23 01:39:50 -0800229 goal_angle_(0.0),
Brian Silverman545f2ad2014-03-14 12:31:42 -0700230 separation_angle_(kGrabSeparation),
Brian Silvermana379f002014-03-22 19:34:53 -0700231 velocity_compensation_(0.0),
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800232 intake_power_(0.0),
233 was_running_(false) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -0800234
235 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Brian Silverman756f9ff2014-01-17 23:40:23 -0800236 if (data.GetControlBit(ControlBit::kAutonomous)) {
237 if (data.PosEdge(ControlBit::kEnabled)){
238 LOG(INFO, "Starting auto mode\n");
239 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800240 .run_auto(true)
241 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800242 } else if (data.NegEdge(ControlBit::kEnabled)) {
243 LOG(INFO, "Stopping auto mode\n");
244 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800245 .run_auto(false)
246 .Send();
Brian Silverman2c764f02014-04-25 09:21:21 -0500247 } else if (!data.GetControlBit(ControlBit::kEnabled)) {
248 {
249 auto goal = drivetrain.goal.MakeMessage();
250 goal->Zero();
251 goal->control_loop_driving = false;
252 goal->left_goal = goal->right_goal = 0;
253 goal->left_velocity_goal = goal->right_velocity_goal = 0;
254 if (!goal.Send()) {
255 LOG(WARNING, "sending 0 drivetrain goal failed\n");
256 }
257 }
258 {
259 // TODO(brians): Make sure this doesn't make it unbrake and not move
260 // or something weird.
261 auto goal = control_loops::shooter_queue_group.goal.MakeMessage();
262 goal->Zero();
263 if (!goal.Send()) {
264 LOG(WARNING, "sending 0 shooter goal failed\n");
265 }
266 }
Brian Silverman756f9ff2014-01-17 23:40:23 -0800267 }
Austin Schuh58d23682014-02-23 01:39:50 -0800268 } else {
269 HandleTeleop(data);
Brian Silverman756f9ff2014-01-17 23:40:23 -0800270 }
271 }
Austin Schuh58d23682014-02-23 01:39:50 -0800272
273 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
274 bool is_control_loop_driving = false;
275 double left_goal = 0.0;
276 double right_goal = 0.0;
277 const double wheel = -data.GetAxis(kSteeringWheel);
278 const double throttle = -data.GetAxis(kDriveThrottle);
279 const double kThrottleGain = 1.0 / 2.5;
280 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
281 data.IsPressed(kDriveControlLoopEnable2))) {
282 // TODO(austin): Static sucks!
283 static double distance = 0.0;
284 static double angle = 0.0;
285 static double filtered_goal_distance = 0.0;
286 if (data.PosEdge(kDriveControlLoopEnable1) ||
287 data.PosEdge(kDriveControlLoopEnable2)) {
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800288 if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) {
Austin Schuh58d23682014-02-23 01:39:50 -0800289 distance = (drivetrain.position->left_encoder +
290 drivetrain.position->right_encoder) /
291 2.0 -
292 throttle * kThrottleGain / 2.0;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800293 angle = gyro_reading->angle;
Austin Schuh58d23682014-02-23 01:39:50 -0800294 filtered_goal_distance = distance;
295 }
296 }
297 is_control_loop_driving = true;
298
299 // const double gyro_angle = Gyro.View().angle;
300 const double goal_theta = angle - wheel * 0.27;
301 const double goal_distance = distance + throttle * kThrottleGain;
302 const double robot_width = 22.0 / 100.0 * 2.54;
303 const double kMaxVelocity = 0.6;
304 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
305 filtered_goal_distance += kMaxVelocity * 0.02;
306 } else if (goal_distance <
307 -kMaxVelocity * 0.02 + filtered_goal_distance) {
308 filtered_goal_distance -= kMaxVelocity * 0.02;
309 } else {
310 filtered_goal_distance = goal_distance;
311 }
312 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
313 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
314 is_high_gear_ = false;
315
316 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
317 }
318 if (!drivetrain.goal.MakeWithBuilder()
319 .steering(wheel)
320 .throttle(throttle)
321 .highgear(is_high_gear_)
Austin Schuhd21c10d2014-10-25 10:32:04 -0700322 .quickturn(data.IsPressed(kQuickTurn))
Austin Schuh58d23682014-02-23 01:39:50 -0800323 .control_loop_driving(is_control_loop_driving)
324 .left_goal(left_goal)
325 .right_goal(right_goal)
Brian Silverman2c764f02014-04-25 09:21:21 -0500326 .left_velocity_goal(0)
327 .right_velocity_goal(0)
Austin Schuh58d23682014-02-23 01:39:50 -0800328 .Send()) {
329 LOG(WARNING, "sending stick values failed\n");
330 }
331 if (data.PosEdge(kShiftHigh)) {
332 is_high_gear_ = false;
333 }
334 if (data.PosEdge(kShiftLow)) {
335 is_high_gear_ = true;
336 }
337 }
338
339 void SetGoal(ClawGoal goal) {
340 goal_angle_ = goal.angle;
341 separation_angle_ = goal.separation;
Brian7e294392014-03-31 12:39:13 -0700342 moving_for_shot_ = false;
Brian Silvermana379f002014-03-22 19:34:53 -0700343 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800344 intake_power_ = 0.0;
345 }
346
347 void SetGoal(ShotGoal goal) {
348 goal_angle_ = goal.claw.angle;
Brian7e294392014-03-31 12:39:13 -0700349 shot_separation_angle_ = goal.claw.separation;
350 separation_angle_ = kGrabSeparation;
351 moving_for_shot_ = true;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800352 shot_power_ = goal.shot_power;
353 velocity_compensation_ = goal.velocity_compensation;
354 intake_power_ = goal.intake_power;
Austin Schuh58d23682014-02-23 01:39:50 -0800355 }
356
357 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
358 HandleDrivetrain(data);
Austin Schuhc95c2b72014-03-02 11:56:49 -0800359 if (!data.GetControlBit(ControlBit::kEnabled)) {
360 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800361 LOG(DEBUG, "Canceling\n");
Austin Schuhc95c2b72014-03-02 11:56:49 -0800362 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800363 if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) {
364 intake_power_ = 0.0;
Brian Silverman545f2ad2014-03-14 12:31:42 -0700365 separation_angle_ = kGrabSeparation;
Brian7e294392014-03-31 12:39:13 -0700366 moving_for_shot_ = false;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800367 }
Austin Schuh58d23682014-02-23 01:39:50 -0800368
Brian Silverman18f6e642014-03-13 18:52:47 -0700369 static const double kAdjustClawGoalDeadband = 0.08;
370 double claw_goal_adjust = data.GetAxis(kAdjustClawGoal);
Brian Silverman5e1eb642014-07-20 16:37:35 -0700371 if (OLD_DS || ::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700372 claw_goal_adjust = 0;
373 } else {
374 claw_goal_adjust = (claw_goal_adjust -
375 ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband
376 : kAdjustClawGoalDeadband)) *
377 0.035;
378 }
379 double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation);
Brian Silverman5e1eb642014-07-20 16:37:35 -0700380 if (OLD_DS ||
381 ::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700382 claw_separation_adjust = 0;
383 } else {
384 claw_separation_adjust =
385 (claw_separation_adjust -
386 ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband
387 : kAdjustClawGoalDeadband)) *
388 -0.035;
389 }
390
Brian6faa7822014-03-31 18:00:28 -0700391 if (data.GetAxis(kFlipRobot) > 0.9) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700392 claw_goal_adjust += claw_separation_adjust;
393 claw_goal_adjust *= -1;
394
Austin Schuh80ff2e12014-03-08 12:06:19 -0800395 if (data.IsPressed(kIntakeOpenPosition)) {
396 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800397 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800398 SetGoal(kFlippedIntakeOpenGoal);
399 } else if (data.IsPressed(kIntakePosition)) {
400 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800401 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800402 SetGoal(kFlippedIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700403 } else if (data.IsPressed(kVerticalTuck)) {
404 action_queue_.CancelAllActions();
405 LOG(DEBUG, "Canceling\n");
406 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800407 } else if (data.IsPressed(kTuck)) {
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(kFlippedTuckGoal);
411 } else if (data.PosEdge(kLongShot)) {
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(kFlippedLongShotGoal);
Brian Silverman4051c6f2014-07-19 16:57:37 -0700415 } else if (data.PosEdge(kCloseShot)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800416 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800417 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800418 SetGoal(kFlippedMediumShotGoal);
Brian Silverman4051c6f2014-07-19 16:57:37 -0700419 } else if (data.PosEdge(kFenderShot)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800420 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800421 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800422 SetGoal(kFlippedShortShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700423 } else if (data.PosEdge(kHumanPlayerShot)) {
424 action_queue_.CancelAllActions();
425 LOG(DEBUG, "Canceling\n");
Brian Silverman73c48392014-04-05 07:07:41 -0700426 SetGoal(kFlippedHumanShotGoal);
Brian Silverman8d5dda42014-04-06 15:59:49 -0700427 } else if (data.PosEdge(kUserLeft)) {
428 action_queue_.CancelAllActions();
429 LOG(DEBUG, "Canceling\n");
430 SetGoal(kFlipped254PassGoal);
Brian Silverman8cd51202014-07-19 16:58:08 -0700431 } else if (data.PosEdge(kUserRight)) {
432 action_queue_.CancelAllActions();
433 LOG(DEBUG, "Canceling\n");
434 SetGoal(kFlippedDemoShotGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800435 } else if (data.PosEdge(kTrussShot)) {
436 action_queue_.CancelAllActions();
437 LOG(DEBUG, "Canceling\n");
Brian Silverman73c48392014-04-05 07:07:41 -0700438 SetGoal(kFlippedTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800439 }
440 } else {
441 if (data.IsPressed(kIntakeOpenPosition)) {
442 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800443 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800444 SetGoal(kIntakeOpenGoal);
445 } else if (data.IsPressed(kIntakePosition)) {
446 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800447 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800448 SetGoal(kIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700449 } else if (data.IsPressed(kVerticalTuck)) {
450 action_queue_.CancelAllActions();
451 LOG(DEBUG, "Canceling\n");
452 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800453 } else if (data.IsPressed(kTuck)) {
454 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800455 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800456 SetGoal(kTuckGoal);
457 } else if (data.PosEdge(kLongShot)) {
458 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800459 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800460 SetGoal(kLongShotGoal);
Brian Silverman4051c6f2014-07-19 16:57:37 -0700461 } else if (data.PosEdge(kCloseShot)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800462 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800463 LOG(DEBUG, "Canceling\n");
Brian Silverman4051c6f2014-07-19 16:57:37 -0700464 SetGoal(kCloseShotGoal);
465 } else if (data.PosEdge(kFenderShot)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800466 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800467 LOG(DEBUG, "Canceling\n");
Brian Silverman4051c6f2014-07-19 16:57:37 -0700468 SetGoal(kFenderShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700469 } else if (data.PosEdge(kHumanPlayerShot)) {
470 action_queue_.CancelAllActions();
471 LOG(DEBUG, "Canceling\n");
472 SetGoal(kHumanShotGoal);
Brian Silverman8d5dda42014-04-06 15:59:49 -0700473 } else if (data.PosEdge(kUserLeft)) {
474 action_queue_.CancelAllActions();
475 LOG(DEBUG, "Canceling\n");
476 SetGoal(k254PassGoal);
Brian Silverman8cd51202014-07-19 16:58:08 -0700477 } else if (data.PosEdge(kUserRight)) {
478 action_queue_.CancelAllActions();
479 LOG(DEBUG, "Canceling\n");
480 SetGoal(kDemoShotGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800481 } else if (data.PosEdge(kTrussShot)) {
482 action_queue_.CancelAllActions();
483 LOG(DEBUG, "Canceling\n");
484 SetGoal(kTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800485 }
Austin Schuh58d23682014-02-23 01:39:50 -0800486 }
487
Austin Schuhade6d082014-03-09 00:53:06 -0800488 /*
Austin Schuh80ff2e12014-03-08 12:06:19 -0800489 if (data.PosEdge(kCatch)) {
490 auto catch_action = MakeCatchAction();
491 catch_action->GetGoal()->catch_angle = goal_angle_;
492 action_queue_.QueueAction(::std::move(catch_action));
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800493 }
Austin Schuhade6d082014-03-09 00:53:06 -0800494 */
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800495
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800496 if (data.PosEdge(kFire)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800497 action_queue_.QueueAction(actions::MakeShootAction());
Brian Silverman9f863a02014-03-29 17:42:36 -0700498 } else if (data.NegEdge(kFire)) {
499 action_queue_.CancelCurrentAction();
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800500 }
501
502 action_queue_.Tick();
Austin Schuhc95c2b72014-03-02 11:56:49 -0800503 if (data.IsPressed(kUnload) || data.IsPressed(kReload)) {
504 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800505 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800506 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700507 velocity_compensation_ = 0.0;
Austin Schuhc95c2b72014-03-02 11:56:49 -0800508 }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800509
510 // Send out the claw and shooter goals if no actions are running.
511 if (!action_queue_.Running()) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700512 goal_angle_ += claw_goal_adjust;
513 separation_angle_ += claw_separation_adjust;
514
Austin Schuh80ff2e12014-03-08 12:06:19 -0800515 // If the action just ended, turn the intake off and stop velocity
516 // compensating.
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800517 if (was_running_) {
518 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700519 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800520 }
521
522 control_loops::drivetrain.status.FetchLatest();
Brian Silvermana379f002014-03-22 19:34:53 -0700523 double goal_angle = goal_angle_;
524 if (control_loops::drivetrain.status.get()) {
525 goal_angle +=
526 SpeedToAngleOffset(control_loops::drivetrain.status->robot_speed);
527 } else {
528 LOG_INTERVAL(no_drivetrain_status_);
529 }
Brian7e294392014-03-31 12:39:13 -0700530
531 if (moving_for_shot_) {
532 auto &claw_status = control_loops::claw_queue_group.status;
533 claw_status.FetchLatest();
534 if (claw_status.get()) {
Brian6faa7822014-03-31 18:00:28 -0700535 if (::std::abs(claw_status->bottom - goal_angle) < 0.2) {
Brian7e294392014-03-31 12:39:13 -0700536 moving_for_shot_ = false;
537 separation_angle_ = shot_separation_angle_;
538 }
539 }
540 }
541
Austin Schuhade6d082014-03-09 00:53:06 -0800542 double separation_angle = separation_angle_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800543
Austin Schuhade6d082014-03-09 00:53:06 -0800544 if (data.IsPressed(kCatch)) {
545 const double kCatchSeparation = 1.0;
546 goal_angle -= kCatchSeparation / 2.0;
547 separation_angle = kCatchSeparation;
548 }
549
550 bool intaking =
551 data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) ||
552 data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch);
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800553 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800554 .bottom_angle(goal_angle)
Austin Schuhade6d082014-03-09 00:53:06 -0800555 .separation_angle(separation_angle)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800556 .intake(intaking ? 12.0
557 : (data.IsPressed(kRollersOut) ? -12.0
558 : intake_power_))
559 .centering(intaking ? 12.0 : 0.0)
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800560 .Send()) {
561 LOG(WARNING, "sending claw goal failed\n");
562 }
563
564 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
565 .shot_power(shot_power_)
566 .shot_requested(data.IsPressed(kFire))
567 .unload_requested(data.IsPressed(kUnload))
568 .load_requested(data.IsPressed(kReload))
569 .Send()) {
570 LOG(WARNING, "sending shooter goal failed\n");
571 }
Austin Schuh58d23682014-02-23 01:39:50 -0800572 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800573 was_running_ = action_queue_.Running();
574 }
575
576 double SpeedToAngleOffset(double speed) {
577 const frc971::constants::Values &values = frc971::constants::GetValues();
578 // scale speed to a [0.0-1.0] on something close to the max
579 // TODO(austin): Change the scale factor for different shots.
Brian Silvermana379f002014-03-22 19:34:53 -0700580 return (speed / values.drivetrain_max_speed) * velocity_compensation_;
Austin Schuh58d23682014-02-23 01:39:50 -0800581 }
582
Austin Schuh01c652b2014-02-21 23:13:42 -0800583 private:
Austin Schuh58d23682014-02-23 01:39:50 -0800584 bool is_high_gear_;
585 double shot_power_;
586 double goal_angle_;
Brian7e294392014-03-31 12:39:13 -0700587 double separation_angle_, shot_separation_angle_;
Brian Silvermana379f002014-03-22 19:34:53 -0700588 double velocity_compensation_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800589 double intake_power_;
590 bool was_running_;
Brian7e294392014-03-31 12:39:13 -0700591 bool moving_for_shot_ = false;
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800592
593 ActionQueue action_queue_;
Brian Silvermana379f002014-03-22 19:34:53 -0700594
595 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
596 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
597 "no drivetrain status");
Brian Silverman756f9ff2014-01-17 23:40:23 -0800598};
599
600} // namespace joysticks
601} // namespace input
602} // namespace frc971
603
604int main() {
605 ::aos::Init();
606 ::frc971::input::joysticks::Reader reader;
607 reader.Run();
608 ::aos::Cleanup();
609}