blob: e1e5139bba21a87ea7ce70f01081e1f0cb49bc28 [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 Silverman0e7c03e2014-03-23 17:06:24 -070096// 34" between near edge of colored line and rear edge of bumper
Austin Schuh5d8c5e72014-03-07 20:24:34 -080097const ShotGoal kLongShotGoal = {
Brian Silverman63ec7502014-03-30 18:09:10 -070098 {-1.04, kShootSeparation}, 140, 0.04, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -070099// 3/4" plunger {-1.04, kShootSeparation}, 140, 0.04, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -0800100const ShotGoal kFlippedLongShotGoal = {
Brian Silverman63ec7502014-03-30 18:09:10 -0700101 {0.96, kShootSeparation}, 140, 0.09, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700102// 3/4 " plunger {0.97, kShootSeparation}, 140, 0.08, kIntakePower};
103
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.670, kShootSeparation}, 77.0, 0.4, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -0800114const 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 Silverman0e7c03e2014-03-23 17:06:24 -0700119const ShotGoal kTrussShotGoal = {
120 {-0.05, kShootSeparation}, 73.0, 0, kIntakePower};
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800121
122// Makes a new ShootAction action.
Austin Schuh80ff2e12014-03-08 12:06:19 -0800123::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>
124MakeCatchAction() {
125 return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>(
126 new TypedAction< ::frc971::actions::CatchActionGroup>(
127 &::frc971::actions::catch_action));
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800128}
129
130// A queue which queues Actions and cancels them.
131class ActionQueue {
132 public:
133 // Queues up an action for sending.
134 void QueueAction(::std::unique_ptr<Action> action) {
135 if (current_action_) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800136 LOG(INFO, "Queueing action, canceling prior\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800137 current_action_->Cancel();
138 next_action_ = ::std::move(action);
139 } else {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800140 LOG(INFO, "Queueing action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800141 current_action_ = ::std::move(action);
142 current_action_->Start();
143 }
144 }
145
146 // Cancels the current action, and runs the next one when the current one has
147 // finished.
148 void CancelCurrentAction() {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800149 LOG(INFO, "Canceling current action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800150 if (current_action_) {
151 current_action_->Cancel();
152 }
153 }
154
155 // Cancels all running actions.
156 void CancelAllActions() {
Brian Silverman101b9642014-03-08 12:45:16 -0800157 LOG(DEBUG, "Cancelling all actions\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800158 if (current_action_) {
159 current_action_->Cancel();
160 }
161 next_action_.reset();
162 }
163
164 // Runs the next action when the current one is finished running.
165 void Tick() {
166 if (current_action_) {
167 if (!current_action_->Running()) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800168 LOG(INFO, "Action is done.\n");
169 current_action_ = ::std::move(next_action_);
170 if (current_action_) {
171 LOG(INFO, "Running next action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800172 current_action_->Start();
173 }
174 }
175 }
176 }
177
178 // Returns true if any action is running or could be running.
179 // For a one cycle faster response, call Tick before running this.
180 bool Running() { return (bool)current_action_; }
181
182 private:
183 ::std::unique_ptr<Action> current_action_;
184 ::std::unique_ptr<Action> next_action_;
185};
186
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800187
Brian Silverman756f9ff2014-01-17 23:40:23 -0800188class Reader : public ::aos::input::JoystickInput {
189 public:
Austin Schuh58d23682014-02-23 01:39:50 -0800190 Reader()
191 : is_high_gear_(false),
Austin Schuh9cb836e2014-02-23 19:25:55 -0800192 shot_power_(80.0),
Austin Schuh58d23682014-02-23 01:39:50 -0800193 goal_angle_(0.0),
Brian Silverman545f2ad2014-03-14 12:31:42 -0700194 separation_angle_(kGrabSeparation),
Brian Silvermana379f002014-03-22 19:34:53 -0700195 velocity_compensation_(0.0),
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800196 intake_power_(0.0),
197 was_running_(false) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -0800198
199 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Brian Silverman756f9ff2014-01-17 23:40:23 -0800200 if (data.GetControlBit(ControlBit::kAutonomous)) {
201 if (data.PosEdge(ControlBit::kEnabled)){
202 LOG(INFO, "Starting auto mode\n");
203 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800204 .run_auto(true)
205 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800206 } else if (data.NegEdge(ControlBit::kEnabled)) {
207 LOG(INFO, "Stopping auto mode\n");
208 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800209 .run_auto(false)
210 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800211 }
Austin Schuh58d23682014-02-23 01:39:50 -0800212 } else {
213 HandleTeleop(data);
Brian Silverman756f9ff2014-01-17 23:40:23 -0800214 }
215 }
Austin Schuh58d23682014-02-23 01:39:50 -0800216
217 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
218 bool is_control_loop_driving = false;
219 double left_goal = 0.0;
220 double right_goal = 0.0;
221 const double wheel = -data.GetAxis(kSteeringWheel);
222 const double throttle = -data.GetAxis(kDriveThrottle);
223 const double kThrottleGain = 1.0 / 2.5;
224 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
225 data.IsPressed(kDriveControlLoopEnable2))) {
226 // TODO(austin): Static sucks!
227 static double distance = 0.0;
228 static double angle = 0.0;
229 static double filtered_goal_distance = 0.0;
230 if (data.PosEdge(kDriveControlLoopEnable1) ||
231 data.PosEdge(kDriveControlLoopEnable2)) {
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800232 if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) {
Austin Schuh58d23682014-02-23 01:39:50 -0800233 distance = (drivetrain.position->left_encoder +
234 drivetrain.position->right_encoder) /
235 2.0 -
236 throttle * kThrottleGain / 2.0;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800237 angle = gyro_reading->angle;
Austin Schuh58d23682014-02-23 01:39:50 -0800238 filtered_goal_distance = distance;
239 }
240 }
241 is_control_loop_driving = true;
242
243 // const double gyro_angle = Gyro.View().angle;
244 const double goal_theta = angle - wheel * 0.27;
245 const double goal_distance = distance + throttle * kThrottleGain;
246 const double robot_width = 22.0 / 100.0 * 2.54;
247 const double kMaxVelocity = 0.6;
248 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
249 filtered_goal_distance += kMaxVelocity * 0.02;
250 } else if (goal_distance <
251 -kMaxVelocity * 0.02 + filtered_goal_distance) {
252 filtered_goal_distance -= kMaxVelocity * 0.02;
253 } else {
254 filtered_goal_distance = goal_distance;
255 }
256 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
257 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
258 is_high_gear_ = false;
259
260 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
261 }
262 if (!drivetrain.goal.MakeWithBuilder()
263 .steering(wheel)
264 .throttle(throttle)
265 .highgear(is_high_gear_)
266 .quickturn(data.IsPressed(kQuickTurn))
267 .control_loop_driving(is_control_loop_driving)
268 .left_goal(left_goal)
269 .right_goal(right_goal)
270 .Send()) {
271 LOG(WARNING, "sending stick values failed\n");
272 }
273 if (data.PosEdge(kShiftHigh)) {
274 is_high_gear_ = false;
275 }
276 if (data.PosEdge(kShiftLow)) {
277 is_high_gear_ = true;
278 }
279 }
280
281 void SetGoal(ClawGoal goal) {
282 goal_angle_ = goal.angle;
283 separation_angle_ = goal.separation;
Brian7e294392014-03-31 12:39:13 -0700284 moving_for_shot_ = false;
Brian Silvermana379f002014-03-22 19:34:53 -0700285 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800286 intake_power_ = 0.0;
287 }
288
289 void SetGoal(ShotGoal goal) {
290 goal_angle_ = goal.claw.angle;
Brian7e294392014-03-31 12:39:13 -0700291 shot_separation_angle_ = goal.claw.separation;
292 separation_angle_ = kGrabSeparation;
293 moving_for_shot_ = true;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800294 shot_power_ = goal.shot_power;
295 velocity_compensation_ = goal.velocity_compensation;
296 intake_power_ = goal.intake_power;
Austin Schuh58d23682014-02-23 01:39:50 -0800297 }
298
299 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
300 HandleDrivetrain(data);
Austin Schuhc95c2b72014-03-02 11:56:49 -0800301 if (!data.GetControlBit(ControlBit::kEnabled)) {
302 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800303 LOG(DEBUG, "Canceling\n");
Austin Schuhc95c2b72014-03-02 11:56:49 -0800304 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800305 if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) {
306 intake_power_ = 0.0;
Brian Silverman545f2ad2014-03-14 12:31:42 -0700307 separation_angle_ = kGrabSeparation;
Brian7e294392014-03-31 12:39:13 -0700308 moving_for_shot_ = false;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800309 }
Austin Schuh58d23682014-02-23 01:39:50 -0800310
Brian Silverman18f6e642014-03-13 18:52:47 -0700311 static const double kAdjustClawGoalDeadband = 0.08;
312 double claw_goal_adjust = data.GetAxis(kAdjustClawGoal);
313 if (::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) {
314 claw_goal_adjust = 0;
315 } else {
316 claw_goal_adjust = (claw_goal_adjust -
317 ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband
318 : kAdjustClawGoalDeadband)) *
319 0.035;
320 }
321 double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation);
322 if (::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) {
323 claw_separation_adjust = 0;
324 } else {
325 claw_separation_adjust =
326 (claw_separation_adjust -
327 ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband
328 : kAdjustClawGoalDeadband)) *
329 -0.035;
330 }
331
Brian6faa7822014-03-31 18:00:28 -0700332 if (data.GetAxis(kFlipRobot) > 0.9) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700333 claw_goal_adjust += claw_separation_adjust;
334 claw_goal_adjust *= -1;
335
Austin Schuh80ff2e12014-03-08 12:06:19 -0800336 if (data.IsPressed(kIntakeOpenPosition)) {
337 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800338 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800339 SetGoal(kFlippedIntakeOpenGoal);
340 } else if (data.IsPressed(kIntakePosition)) {
341 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800342 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800343 SetGoal(kFlippedIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700344 } else if (data.IsPressed(kVerticalTuck)) {
345 action_queue_.CancelAllActions();
346 LOG(DEBUG, "Canceling\n");
347 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800348 } else if (data.IsPressed(kTuck)) {
349 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800350 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800351 SetGoal(kFlippedTuckGoal);
352 } else if (data.PosEdge(kLongShot)) {
353 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800354 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800355 SetGoal(kFlippedLongShotGoal);
356 } else if (data.PosEdge(kMediumShot)) {
357 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800358 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800359 SetGoal(kFlippedMediumShotGoal);
360 } else if (data.PosEdge(kShortShot)) {
361 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800362 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800363 SetGoal(kFlippedShortShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700364 } else if (data.PosEdge(kHumanPlayerShot)) {
365 action_queue_.CancelAllActions();
366 LOG(DEBUG, "Canceling\n");
367 SetGoal(kHumanShotGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800368 } else if (data.PosEdge(kTrussShot)) {
369 action_queue_.CancelAllActions();
370 LOG(DEBUG, "Canceling\n");
371 SetGoal(kTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800372 }
373 } else {
374 if (data.IsPressed(kIntakeOpenPosition)) {
375 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800376 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800377 SetGoal(kIntakeOpenGoal);
378 } else if (data.IsPressed(kIntakePosition)) {
379 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800380 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800381 SetGoal(kIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700382 } else if (data.IsPressed(kVerticalTuck)) {
383 action_queue_.CancelAllActions();
384 LOG(DEBUG, "Canceling\n");
385 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800386 } else if (data.IsPressed(kTuck)) {
387 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800388 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800389 SetGoal(kTuckGoal);
390 } else if (data.PosEdge(kLongShot)) {
391 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800392 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800393 SetGoal(kLongShotGoal);
394 } else if (data.PosEdge(kMediumShot)) {
395 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800396 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800397 SetGoal(kMediumShotGoal);
398 } else if (data.PosEdge(kShortShot)) {
399 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800400 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800401 SetGoal(kShortShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700402 } else if (data.PosEdge(kHumanPlayerShot)) {
403 action_queue_.CancelAllActions();
404 LOG(DEBUG, "Canceling\n");
405 SetGoal(kHumanShotGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800406 } else if (data.PosEdge(kTrussShot)) {
407 action_queue_.CancelAllActions();
408 LOG(DEBUG, "Canceling\n");
409 SetGoal(kTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800410 }
Austin Schuh58d23682014-02-23 01:39:50 -0800411 }
412
Austin Schuhade6d082014-03-09 00:53:06 -0800413 /*
Austin Schuh80ff2e12014-03-08 12:06:19 -0800414 if (data.PosEdge(kCatch)) {
415 auto catch_action = MakeCatchAction();
416 catch_action->GetGoal()->catch_angle = goal_angle_;
417 action_queue_.QueueAction(::std::move(catch_action));
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800418 }
Austin Schuhade6d082014-03-09 00:53:06 -0800419 */
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800420
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800421 if (data.PosEdge(kFire)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800422 action_queue_.QueueAction(actions::MakeShootAction());
Brian Silverman9f863a02014-03-29 17:42:36 -0700423 } else if (data.NegEdge(kFire)) {
424 action_queue_.CancelCurrentAction();
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800425 }
426
427 action_queue_.Tick();
Austin Schuhc95c2b72014-03-02 11:56:49 -0800428 if (data.IsPressed(kUnload) || data.IsPressed(kReload)) {
429 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800430 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800431 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700432 velocity_compensation_ = 0.0;
Austin Schuhc95c2b72014-03-02 11:56:49 -0800433 }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800434
435 // Send out the claw and shooter goals if no actions are running.
436 if (!action_queue_.Running()) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700437 goal_angle_ += claw_goal_adjust;
438 separation_angle_ += claw_separation_adjust;
439
Austin Schuh80ff2e12014-03-08 12:06:19 -0800440 // If the action just ended, turn the intake off and stop velocity
441 // compensating.
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800442 if (was_running_) {
443 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700444 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800445 }
446
447 control_loops::drivetrain.status.FetchLatest();
Brian Silvermana379f002014-03-22 19:34:53 -0700448 double goal_angle = goal_angle_;
449 if (control_loops::drivetrain.status.get()) {
450 goal_angle +=
451 SpeedToAngleOffset(control_loops::drivetrain.status->robot_speed);
452 } else {
453 LOG_INTERVAL(no_drivetrain_status_);
454 }
Brian7e294392014-03-31 12:39:13 -0700455
456 if (moving_for_shot_) {
457 auto &claw_status = control_loops::claw_queue_group.status;
458 claw_status.FetchLatest();
459 if (claw_status.get()) {
Brian6faa7822014-03-31 18:00:28 -0700460 if (::std::abs(claw_status->bottom - goal_angle) < 0.2) {
Brian7e294392014-03-31 12:39:13 -0700461 moving_for_shot_ = false;
462 separation_angle_ = shot_separation_angle_;
463 }
464 }
465 }
466
Austin Schuhade6d082014-03-09 00:53:06 -0800467 double separation_angle = separation_angle_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800468
Austin Schuhade6d082014-03-09 00:53:06 -0800469 if (data.IsPressed(kCatch)) {
470 const double kCatchSeparation = 1.0;
471 goal_angle -= kCatchSeparation / 2.0;
472 separation_angle = kCatchSeparation;
473 }
474
475 bool intaking =
476 data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) ||
477 data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch);
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800478 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800479 .bottom_angle(goal_angle)
Austin Schuhade6d082014-03-09 00:53:06 -0800480 .separation_angle(separation_angle)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800481 .intake(intaking ? 12.0
482 : (data.IsPressed(kRollersOut) ? -12.0
483 : intake_power_))
484 .centering(intaking ? 12.0 : 0.0)
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800485 .Send()) {
486 LOG(WARNING, "sending claw goal failed\n");
487 }
488
489 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
490 .shot_power(shot_power_)
491 .shot_requested(data.IsPressed(kFire))
492 .unload_requested(data.IsPressed(kUnload))
493 .load_requested(data.IsPressed(kReload))
494 .Send()) {
495 LOG(WARNING, "sending shooter goal failed\n");
496 }
Austin Schuh58d23682014-02-23 01:39:50 -0800497 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800498 was_running_ = action_queue_.Running();
499 }
500
501 double SpeedToAngleOffset(double speed) {
502 const frc971::constants::Values &values = frc971::constants::GetValues();
503 // scale speed to a [0.0-1.0] on something close to the max
504 // TODO(austin): Change the scale factor for different shots.
Brian Silvermana379f002014-03-22 19:34:53 -0700505 return (speed / values.drivetrain_max_speed) * velocity_compensation_;
Austin Schuh58d23682014-02-23 01:39:50 -0800506 }
507
Austin Schuh01c652b2014-02-21 23:13:42 -0800508 private:
Austin Schuh58d23682014-02-23 01:39:50 -0800509 bool is_high_gear_;
510 double shot_power_;
511 double goal_angle_;
Brian7e294392014-03-31 12:39:13 -0700512 double separation_angle_, shot_separation_angle_;
Brian Silvermana379f002014-03-22 19:34:53 -0700513 double velocity_compensation_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800514 double intake_power_;
515 bool was_running_;
Brian7e294392014-03-31 12:39:13 -0700516 bool moving_for_shot_ = false;
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800517
518 ActionQueue action_queue_;
Brian Silvermana379f002014-03-22 19:34:53 -0700519
520 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
521 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
522 "no drivetrain status");
Brian Silverman756f9ff2014-01-17 23:40:23 -0800523};
524
525} // namespace joysticks
526} // namespace input
527} // namespace frc971
528
529int main() {
530 ::aos::Init();
531 ::frc971::input::joysticks::Reader reader;
532 reader.Run();
533 ::aos::Cleanup();
534}