blob: 55274f81617c6f5f1fefc98a32a9e7f6dcf90e71 [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"
10
11#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh5d8c5e72014-03-07 20:24:34 -080012#include "frc971/constants.h"
Brian Silverman6bf0d3c2014-03-08 12:52:54 -080013#include "frc971/queues/other_sensors.q.h"
Brian Silverman756f9ff2014-01-17 23:40:23 -080014#include "frc971/autonomous/auto.q.h"
Brian Silvermanfac5c292014-02-17 15:26:57 -080015#include "frc971/control_loops/claw/claw.q.h"
16#include "frc971/control_loops/shooter/shooter.q.h"
Ben Fredricksonaa450452014-03-01 09:41:18 +000017#include "frc971/actions/shoot_action.q.h"
Austin Schuh80ff2e12014-03-08 12:06:19 -080018#include "frc971/actions/action_client.h"
19#include "frc971/actions/catch_action.q.h"
20#include "frc971/actions/shoot_action.h"
Brian Silverman756f9ff2014-01-17 23:40:23 -080021
22using ::frc971::control_loops::drivetrain;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -080023using ::frc971::sensors::gyro_reading;
Brian Silverman756f9ff2014-01-17 23:40:23 -080024
25using ::aos::input::driver_station::ButtonLocation;
26using ::aos::input::driver_station::JoystickAxis;
27using ::aos::input::driver_station::ControlBit;
28
29namespace frc971 {
30namespace input {
31namespace joysticks {
32
33const ButtonLocation kDriveControlLoopEnable1(1, 7),
34 kDriveControlLoopEnable2(1, 11);
35const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
36const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
37const ButtonLocation kQuickTurn(1, 5);
Austin Schuh58d23682014-02-23 01:39:50 -080038
Austin Schuh80ff2e12014-03-08 12:06:19 -080039const ButtonLocation kCatch(3, 10);
40
41const ButtonLocation kFire(3, 9);
Austin Schuh9cb836e2014-02-23 19:25:55 -080042const ButtonLocation kUnload(2, 11);
43const ButtonLocation kReload(2, 6);
Austin Schuh58d23682014-02-23 01:39:50 -080044
Austin Schuh80ff2e12014-03-08 12:06:19 -080045const ButtonLocation kRollersOut(3, 8);
46const ButtonLocation kRollersIn(3, 3);
Austin Schuh58d23682014-02-23 01:39:50 -080047
Austin Schuh80ff2e12014-03-08 12:06:19 -080048const ButtonLocation kTuck(3, 4);
49const ButtonLocation kIntakePosition(3, 5);
50const ButtonLocation kIntakeOpenPosition(3, 11);
Austin Schuh80ff2e12014-03-08 12:06:19 -080051const JoystickAxis kFlipRobot(3, 3);
Austin Schuh58d23682014-02-23 01:39:50 -080052
Austin Schuh80ff2e12014-03-08 12:06:19 -080053const ButtonLocation kLongShot(3, 7);
54const ButtonLocation kMediumShot(3, 6);
55const ButtonLocation kShortShot(3, 2);
Austin Schuhade6d082014-03-09 00:53:06 -080056const ButtonLocation kTrussShot(3, 1);
Austin Schuh58d23682014-02-23 01:39:50 -080057
Brian Silverman18f6e642014-03-13 18:52:47 -070058const JoystickAxis kAdjustClawGoal(3, 2);
59const JoystickAxis kAdjustClawSeparation(3, 1);
60
Austin Schuh58d23682014-02-23 01:39:50 -080061struct ClawGoal {
62 double angle;
63 double separation;
64};
65
Austin Schuh5d8c5e72014-03-07 20:24:34 -080066struct ShotGoal {
67 ClawGoal claw;
68 double shot_power;
69 bool velocity_compensation;
70 double intake_power;
71};
72
Brian Silverman545f2ad2014-03-14 12:31:42 -070073const double kIntakePower = 4.0;
74const double kGrabSeparation = -0.04;
75const double kShootSeparation = 0.11 + kGrabSeparation;
76
Austin Schuh58d23682014-02-23 01:39:50 -080077const ClawGoal kTuckGoal = {-2.273474, -0.749484};
Brian Silverman545f2ad2014-03-14 12:31:42 -070078const ClawGoal kIntakeGoal = {-2.273474, kGrabSeparation};
Austin Schuh9cb836e2014-02-23 19:25:55 -080079const ClawGoal kIntakeOpenGoal = {-2.0, 1.2};
Austin Schuh58d23682014-02-23 01:39:50 -080080
Austin Schuh80ff2e12014-03-08 12:06:19 -080081// TODO(austin): Tune these by hand...
82const ClawGoal kFlippedTuckGoal = {2.733474, -0.75};
Brian Silverman545f2ad2014-03-14 12:31:42 -070083const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation};
Austin Schuh80ff2e12014-03-08 12:06:19 -080084const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0};
85
Austin Schuhade6d082014-03-09 00:53:06 -080086//const ShotGoal kLongShotGoal = {
87 //{-M_PI / 2.0 + 0.46, kShootSeparation}, 120, false, kIntakePower};
Austin Schuh5d8c5e72014-03-07 20:24:34 -080088const ShotGoal kLongShotGoal = {
Austin Schuh5f4944e2014-03-09 16:40:39 -070089 {-0.60, kShootSeparation}, 80, false, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -080090const ShotGoal kMediumShotGoal = {
Austin Schuh5f4944e2014-03-09 16:40:39 -070091 {-0.90, kShootSeparation}, 105, true, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -080092const ShotGoal kShortShotGoal = {
Austin Schuhade6d082014-03-09 00:53:06 -080093 {-0.670, kShootSeparation}, 71.0, false, kIntakePower};
94const ShotGoal kTrussShotGoal = {
95 {-0.05, kShootSeparation}, 61.0, false, kIntakePower};
Brian Silverman756f9ff2014-01-17 23:40:23 -080096
Austin Schuh80ff2e12014-03-08 12:06:19 -080097const ShotGoal kFlippedLongShotGoal = {
Austin Schuh5f4944e2014-03-09 16:40:39 -070098 {0.55, kShootSeparation}, 80, false, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -080099const ShotGoal kFlippedMediumShotGoal = {
Austin Schuhade6d082014-03-09 00:53:06 -0800100 {0.85, kShootSeparation}, 105, true, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -0800101const ShotGoal kFlippedShortShotGoal = {
Austin Schuhade6d082014-03-09 00:53:06 -0800102 {0.57, kShootSeparation}, 80.0, false, kIntakePower};
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800103
104// Makes a new ShootAction action.
Austin Schuh80ff2e12014-03-08 12:06:19 -0800105::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>
106MakeCatchAction() {
107 return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>(
108 new TypedAction< ::frc971::actions::CatchActionGroup>(
109 &::frc971::actions::catch_action));
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800110}
111
112// A queue which queues Actions and cancels them.
113class ActionQueue {
114 public:
115 // Queues up an action for sending.
116 void QueueAction(::std::unique_ptr<Action> action) {
117 if (current_action_) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800118 LOG(INFO, "Queueing action, canceling prior\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800119 current_action_->Cancel();
120 next_action_ = ::std::move(action);
121 } else {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800122 LOG(INFO, "Queueing action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800123 current_action_ = ::std::move(action);
124 current_action_->Start();
125 }
126 }
127
128 // Cancels the current action, and runs the next one when the current one has
129 // finished.
130 void CancelCurrentAction() {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800131 LOG(INFO, "Canceling current action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800132 if (current_action_) {
133 current_action_->Cancel();
134 }
135 }
136
137 // Cancels all running actions.
138 void CancelAllActions() {
Brian Silverman101b9642014-03-08 12:45:16 -0800139 LOG(DEBUG, "Cancelling all actions\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800140 if (current_action_) {
141 current_action_->Cancel();
142 }
143 next_action_.reset();
144 }
145
146 // Runs the next action when the current one is finished running.
147 void Tick() {
148 if (current_action_) {
149 if (!current_action_->Running()) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800150 LOG(INFO, "Action is done.\n");
151 current_action_ = ::std::move(next_action_);
152 if (current_action_) {
153 LOG(INFO, "Running next action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800154 current_action_->Start();
155 }
156 }
157 }
158 }
159
160 // Returns true if any action is running or could be running.
161 // For a one cycle faster response, call Tick before running this.
162 bool Running() { return (bool)current_action_; }
163
164 private:
165 ::std::unique_ptr<Action> current_action_;
166 ::std::unique_ptr<Action> next_action_;
167};
168
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800169
Brian Silverman756f9ff2014-01-17 23:40:23 -0800170class Reader : public ::aos::input::JoystickInput {
171 public:
Austin Schuh58d23682014-02-23 01:39:50 -0800172 Reader()
173 : is_high_gear_(false),
Austin Schuh9cb836e2014-02-23 19:25:55 -0800174 shot_power_(80.0),
Austin Schuh58d23682014-02-23 01:39:50 -0800175 goal_angle_(0.0),
Brian Silverman545f2ad2014-03-14 12:31:42 -0700176 separation_angle_(kGrabSeparation),
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800177 velocity_compensation_(false),
178 intake_power_(0.0),
179 was_running_(false) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -0800180
181 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Brian Silverman756f9ff2014-01-17 23:40:23 -0800182 if (data.GetControlBit(ControlBit::kAutonomous)) {
183 if (data.PosEdge(ControlBit::kEnabled)){
184 LOG(INFO, "Starting auto mode\n");
185 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800186 .run_auto(true)
187 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800188 } else if (data.NegEdge(ControlBit::kEnabled)) {
189 LOG(INFO, "Stopping auto mode\n");
190 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800191 .run_auto(false)
192 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800193 }
Austin Schuh58d23682014-02-23 01:39:50 -0800194 } else {
195 HandleTeleop(data);
Brian Silverman756f9ff2014-01-17 23:40:23 -0800196 }
197 }
Austin Schuh58d23682014-02-23 01:39:50 -0800198
199 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
200 bool is_control_loop_driving = false;
201 double left_goal = 0.0;
202 double right_goal = 0.0;
203 const double wheel = -data.GetAxis(kSteeringWheel);
204 const double throttle = -data.GetAxis(kDriveThrottle);
205 const double kThrottleGain = 1.0 / 2.5;
206 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
207 data.IsPressed(kDriveControlLoopEnable2))) {
208 // TODO(austin): Static sucks!
209 static double distance = 0.0;
210 static double angle = 0.0;
211 static double filtered_goal_distance = 0.0;
212 if (data.PosEdge(kDriveControlLoopEnable1) ||
213 data.PosEdge(kDriveControlLoopEnable2)) {
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800214 if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) {
Austin Schuh58d23682014-02-23 01:39:50 -0800215 distance = (drivetrain.position->left_encoder +
216 drivetrain.position->right_encoder) /
217 2.0 -
218 throttle * kThrottleGain / 2.0;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800219 angle = gyro_reading->angle;
Austin Schuh58d23682014-02-23 01:39:50 -0800220 filtered_goal_distance = distance;
221 }
222 }
223 is_control_loop_driving = true;
224
225 // const double gyro_angle = Gyro.View().angle;
226 const double goal_theta = angle - wheel * 0.27;
227 const double goal_distance = distance + throttle * kThrottleGain;
228 const double robot_width = 22.0 / 100.0 * 2.54;
229 const double kMaxVelocity = 0.6;
230 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
231 filtered_goal_distance += kMaxVelocity * 0.02;
232 } else if (goal_distance <
233 -kMaxVelocity * 0.02 + filtered_goal_distance) {
234 filtered_goal_distance -= kMaxVelocity * 0.02;
235 } else {
236 filtered_goal_distance = goal_distance;
237 }
238 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
239 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
240 is_high_gear_ = false;
241
242 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
243 }
244 if (!drivetrain.goal.MakeWithBuilder()
245 .steering(wheel)
246 .throttle(throttle)
247 .highgear(is_high_gear_)
248 .quickturn(data.IsPressed(kQuickTurn))
249 .control_loop_driving(is_control_loop_driving)
250 .left_goal(left_goal)
251 .right_goal(right_goal)
252 .Send()) {
253 LOG(WARNING, "sending stick values failed\n");
254 }
255 if (data.PosEdge(kShiftHigh)) {
256 is_high_gear_ = false;
257 }
258 if (data.PosEdge(kShiftLow)) {
259 is_high_gear_ = true;
260 }
261 }
262
263 void SetGoal(ClawGoal goal) {
264 goal_angle_ = goal.angle;
265 separation_angle_ = goal.separation;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800266 velocity_compensation_ = false;
267 intake_power_ = 0.0;
268 }
269
270 void SetGoal(ShotGoal goal) {
271 goal_angle_ = goal.claw.angle;
272 separation_angle_ = goal.claw.separation;
273 shot_power_ = goal.shot_power;
274 velocity_compensation_ = goal.velocity_compensation;
275 intake_power_ = goal.intake_power;
Austin Schuh58d23682014-02-23 01:39:50 -0800276 }
277
278 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
279 HandleDrivetrain(data);
Austin Schuhc95c2b72014-03-02 11:56:49 -0800280 if (!data.GetControlBit(ControlBit::kEnabled)) {
281 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800282 LOG(DEBUG, "Canceling\n");
Austin Schuhc95c2b72014-03-02 11:56:49 -0800283 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800284 if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) {
285 intake_power_ = 0.0;
Brian Silverman545f2ad2014-03-14 12:31:42 -0700286 separation_angle_ = kGrabSeparation;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800287 }
Austin Schuh58d23682014-02-23 01:39:50 -0800288
Brian Silverman18f6e642014-03-13 18:52:47 -0700289 static const double kAdjustClawGoalDeadband = 0.08;
290 double claw_goal_adjust = data.GetAxis(kAdjustClawGoal);
291 if (::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) {
292 claw_goal_adjust = 0;
293 } else {
294 claw_goal_adjust = (claw_goal_adjust -
295 ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband
296 : kAdjustClawGoalDeadband)) *
297 0.035;
298 }
299 double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation);
300 if (::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) {
301 claw_separation_adjust = 0;
302 } else {
303 claw_separation_adjust =
304 (claw_separation_adjust -
305 ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband
306 : kAdjustClawGoalDeadband)) *
307 -0.035;
308 }
309
Brian Silverman4d1795d2014-03-13 15:53:40 -0700310 if (data.GetAxis(kFlipRobot) > 0.5) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700311 claw_goal_adjust += claw_separation_adjust;
312 claw_goal_adjust *= -1;
313
Austin Schuh80ff2e12014-03-08 12:06:19 -0800314 if (data.IsPressed(kIntakeOpenPosition)) {
315 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800316 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800317 SetGoal(kFlippedIntakeOpenGoal);
318 } else if (data.IsPressed(kIntakePosition)) {
319 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800320 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800321 SetGoal(kFlippedIntakeGoal);
322 } else if (data.IsPressed(kTuck)) {
323 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800324 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800325 SetGoal(kFlippedTuckGoal);
326 } else if (data.PosEdge(kLongShot)) {
327 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800328 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800329 SetGoal(kFlippedLongShotGoal);
330 } else if (data.PosEdge(kMediumShot)) {
331 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800332 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800333 SetGoal(kFlippedMediumShotGoal);
334 } else if (data.PosEdge(kShortShot)) {
335 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800336 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800337 SetGoal(kFlippedShortShotGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800338 } else if (data.PosEdge(kTrussShot)) {
339 action_queue_.CancelAllActions();
340 LOG(DEBUG, "Canceling\n");
341 SetGoal(kTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800342 }
343 } else {
344 if (data.IsPressed(kIntakeOpenPosition)) {
345 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800346 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800347 SetGoal(kIntakeOpenGoal);
348 } else if (data.IsPressed(kIntakePosition)) {
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(kIntakeGoal);
352 } else if (data.IsPressed(kTuck)) {
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(kTuckGoal);
356 } else if (data.PosEdge(kLongShot)) {
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(kLongShotGoal);
360 } else if (data.PosEdge(kMediumShot)) {
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(kMediumShotGoal);
364 } else if (data.PosEdge(kShortShot)) {
365 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800366 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800367 SetGoal(kShortShotGoal);
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 }
Austin Schuh58d23682014-02-23 01:39:50 -0800373 }
374
Austin Schuhade6d082014-03-09 00:53:06 -0800375 /*
Austin Schuh80ff2e12014-03-08 12:06:19 -0800376 if (data.PosEdge(kCatch)) {
377 auto catch_action = MakeCatchAction();
378 catch_action->GetGoal()->catch_angle = goal_angle_;
379 action_queue_.QueueAction(::std::move(catch_action));
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800380 }
Austin Schuhade6d082014-03-09 00:53:06 -0800381 */
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800382
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800383 if (data.PosEdge(kFire)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800384 action_queue_.QueueAction(actions::MakeShootAction());
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800385 }
386
387 action_queue_.Tick();
Austin Schuhc95c2b72014-03-02 11:56:49 -0800388 if (data.IsPressed(kUnload) || data.IsPressed(kReload)) {
389 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800390 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800391 intake_power_ = 0.0;
392 velocity_compensation_ = false;
Austin Schuhc95c2b72014-03-02 11:56:49 -0800393 }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800394
395 // Send out the claw and shooter goals if no actions are running.
396 if (!action_queue_.Running()) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700397 goal_angle_ += claw_goal_adjust;
398 separation_angle_ += claw_separation_adjust;
399
Austin Schuh80ff2e12014-03-08 12:06:19 -0800400 // If the action just ended, turn the intake off and stop velocity
401 // compensating.
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800402 if (was_running_) {
403 intake_power_ = 0.0;
404 velocity_compensation_ = false;
405 }
406
407 control_loops::drivetrain.status.FetchLatest();
Austin Schuhade6d082014-03-09 00:53:06 -0800408 double goal_angle =
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800409 goal_angle_ +
410 (velocity_compensation_
411 ? SpeedToAngleOffset(
412 control_loops::drivetrain.status->robot_speed)
413 : 0.0);
Austin Schuhade6d082014-03-09 00:53:06 -0800414 double separation_angle = separation_angle_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800415
Austin Schuhade6d082014-03-09 00:53:06 -0800416 if (data.IsPressed(kCatch)) {
417 const double kCatchSeparation = 1.0;
418 goal_angle -= kCatchSeparation / 2.0;
419 separation_angle = kCatchSeparation;
420 }
421
422 bool intaking =
423 data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) ||
424 data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch);
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800425 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800426 .bottom_angle(goal_angle)
Austin Schuhade6d082014-03-09 00:53:06 -0800427 .separation_angle(separation_angle)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800428 .intake(intaking ? 12.0
429 : (data.IsPressed(kRollersOut) ? -12.0
430 : intake_power_))
431 .centering(intaking ? 12.0 : 0.0)
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800432 .Send()) {
433 LOG(WARNING, "sending claw goal failed\n");
434 }
435
436 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
437 .shot_power(shot_power_)
438 .shot_requested(data.IsPressed(kFire))
439 .unload_requested(data.IsPressed(kUnload))
440 .load_requested(data.IsPressed(kReload))
441 .Send()) {
442 LOG(WARNING, "sending shooter goal failed\n");
443 }
Austin Schuh58d23682014-02-23 01:39:50 -0800444 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800445 was_running_ = action_queue_.Running();
446 }
447
448 double SpeedToAngleOffset(double speed) {
449 const frc971::constants::Values &values = frc971::constants::GetValues();
450 // scale speed to a [0.0-1.0] on something close to the max
451 // TODO(austin): Change the scale factor for different shots.
Austin Schuhade6d082014-03-09 00:53:06 -0800452 return (speed / values.drivetrain_max_speed) * 0.2;
Austin Schuh58d23682014-02-23 01:39:50 -0800453 }
454
Austin Schuh01c652b2014-02-21 23:13:42 -0800455 private:
Austin Schuh58d23682014-02-23 01:39:50 -0800456 bool is_high_gear_;
457 double shot_power_;
458 double goal_angle_;
459 double separation_angle_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800460 bool velocity_compensation_;
461 double intake_power_;
462 bool was_running_;
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800463
464 ActionQueue action_queue_;
Brian Silverman756f9ff2014-01-17 23:40:23 -0800465};
466
467} // namespace joysticks
468} // namespace input
469} // namespace frc971
470
471int main() {
472 ::aos::Init();
473 ::frc971::input::joysticks::Reader reader;
474 reader.Run();
475 ::aos::Cleanup();
476}