blob: 87287b750471194ad5190ca91f1a2a6563974a38 [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
Austin Schuh58d23682014-02-23 01:39:50 -080073const ClawGoal kTuckGoal = {-2.273474, -0.749484};
74const ClawGoal kIntakeGoal = {-2.273474, 0.0};
Austin Schuh9cb836e2014-02-23 19:25:55 -080075const ClawGoal kIntakeOpenGoal = {-2.0, 1.2};
Austin Schuh58d23682014-02-23 01:39:50 -080076
Austin Schuh80ff2e12014-03-08 12:06:19 -080077// TODO(austin): Tune these by hand...
78const ClawGoal kFlippedTuckGoal = {2.733474, -0.75};
79const ClawGoal kFlippedIntakeGoal = {2.0, 0.0};
80const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0};
81
Austin Schuhade6d082014-03-09 00:53:06 -080082const double kIntakePower = 4.0;
83const double kShootSeparation = 0.11;
Austin Schuh5d8c5e72014-03-07 20:24:34 -080084
Austin Schuhade6d082014-03-09 00:53:06 -080085//const ShotGoal kLongShotGoal = {
86 //{-M_PI / 2.0 + 0.46, kShootSeparation}, 120, false, kIntakePower};
Austin Schuh5d8c5e72014-03-07 20:24:34 -080087const ShotGoal kLongShotGoal = {
Austin Schuh5f4944e2014-03-09 16:40:39 -070088 {-0.60, kShootSeparation}, 80, false, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -080089const ShotGoal kMediumShotGoal = {
Austin Schuh5f4944e2014-03-09 16:40:39 -070090 {-0.90, kShootSeparation}, 105, true, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -080091const ShotGoal kShortShotGoal = {
Austin Schuhade6d082014-03-09 00:53:06 -080092 {-0.670, kShootSeparation}, 71.0, false, kIntakePower};
93const ShotGoal kTrussShotGoal = {
94 {-0.05, kShootSeparation}, 61.0, false, kIntakePower};
Brian Silverman756f9ff2014-01-17 23:40:23 -080095
Austin Schuh80ff2e12014-03-08 12:06:19 -080096const ShotGoal kFlippedLongShotGoal = {
Austin Schuh5f4944e2014-03-09 16:40:39 -070097 {0.55, kShootSeparation}, 80, false, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -080098const ShotGoal kFlippedMediumShotGoal = {
Austin Schuhade6d082014-03-09 00:53:06 -080099 {0.85, kShootSeparation}, 105, true, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -0800100const ShotGoal kFlippedShortShotGoal = {
Austin Schuhade6d082014-03-09 00:53:06 -0800101 {0.57, kShootSeparation}, 80.0, false, kIntakePower};
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800102
103// Makes a new ShootAction action.
Austin Schuh80ff2e12014-03-08 12:06:19 -0800104::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>
105MakeCatchAction() {
106 return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>(
107 new TypedAction< ::frc971::actions::CatchActionGroup>(
108 &::frc971::actions::catch_action));
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800109}
110
111// A queue which queues Actions and cancels them.
112class ActionQueue {
113 public:
114 // Queues up an action for sending.
115 void QueueAction(::std::unique_ptr<Action> action) {
116 if (current_action_) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800117 LOG(INFO, "Queueing action, canceling prior\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800118 current_action_->Cancel();
119 next_action_ = ::std::move(action);
120 } else {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800121 LOG(INFO, "Queueing action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800122 current_action_ = ::std::move(action);
123 current_action_->Start();
124 }
125 }
126
127 // Cancels the current action, and runs the next one when the current one has
128 // finished.
129 void CancelCurrentAction() {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800130 LOG(INFO, "Canceling current action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800131 if (current_action_) {
132 current_action_->Cancel();
133 }
134 }
135
136 // Cancels all running actions.
137 void CancelAllActions() {
Brian Silverman101b9642014-03-08 12:45:16 -0800138 LOG(DEBUG, "Cancelling all actions\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800139 if (current_action_) {
140 current_action_->Cancel();
141 }
142 next_action_.reset();
143 }
144
145 // Runs the next action when the current one is finished running.
146 void Tick() {
147 if (current_action_) {
148 if (!current_action_->Running()) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800149 LOG(INFO, "Action is done.\n");
150 current_action_ = ::std::move(next_action_);
151 if (current_action_) {
152 LOG(INFO, "Running next action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800153 current_action_->Start();
154 }
155 }
156 }
157 }
158
159 // Returns true if any action is running or could be running.
160 // For a one cycle faster response, call Tick before running this.
161 bool Running() { return (bool)current_action_; }
162
163 private:
164 ::std::unique_ptr<Action> current_action_;
165 ::std::unique_ptr<Action> next_action_;
166};
167
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800168
Brian Silverman756f9ff2014-01-17 23:40:23 -0800169class Reader : public ::aos::input::JoystickInput {
170 public:
Austin Schuh58d23682014-02-23 01:39:50 -0800171 Reader()
172 : is_high_gear_(false),
Austin Schuh9cb836e2014-02-23 19:25:55 -0800173 shot_power_(80.0),
Austin Schuh58d23682014-02-23 01:39:50 -0800174 goal_angle_(0.0),
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800175 separation_angle_(0.0),
176 velocity_compensation_(false),
177 intake_power_(0.0),
178 was_running_(false) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -0800179
180 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Brian Silverman756f9ff2014-01-17 23:40:23 -0800181 if (data.GetControlBit(ControlBit::kAutonomous)) {
182 if (data.PosEdge(ControlBit::kEnabled)){
183 LOG(INFO, "Starting auto mode\n");
184 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800185 .run_auto(true)
186 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800187 } else if (data.NegEdge(ControlBit::kEnabled)) {
188 LOG(INFO, "Stopping auto mode\n");
189 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800190 .run_auto(false)
191 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800192 }
Austin Schuh58d23682014-02-23 01:39:50 -0800193 } else {
194 HandleTeleop(data);
Brian Silverman756f9ff2014-01-17 23:40:23 -0800195 }
196 }
Austin Schuh58d23682014-02-23 01:39:50 -0800197
198 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
199 bool is_control_loop_driving = false;
200 double left_goal = 0.0;
201 double right_goal = 0.0;
202 const double wheel = -data.GetAxis(kSteeringWheel);
203 const double throttle = -data.GetAxis(kDriveThrottle);
204 const double kThrottleGain = 1.0 / 2.5;
205 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
206 data.IsPressed(kDriveControlLoopEnable2))) {
207 // TODO(austin): Static sucks!
208 static double distance = 0.0;
209 static double angle = 0.0;
210 static double filtered_goal_distance = 0.0;
211 if (data.PosEdge(kDriveControlLoopEnable1) ||
212 data.PosEdge(kDriveControlLoopEnable2)) {
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800213 if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) {
Austin Schuh58d23682014-02-23 01:39:50 -0800214 distance = (drivetrain.position->left_encoder +
215 drivetrain.position->right_encoder) /
216 2.0 -
217 throttle * kThrottleGain / 2.0;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800218 angle = gyro_reading->angle;
Austin Schuh58d23682014-02-23 01:39:50 -0800219 filtered_goal_distance = distance;
220 }
221 }
222 is_control_loop_driving = true;
223
224 // const double gyro_angle = Gyro.View().angle;
225 const double goal_theta = angle - wheel * 0.27;
226 const double goal_distance = distance + throttle * kThrottleGain;
227 const double robot_width = 22.0 / 100.0 * 2.54;
228 const double kMaxVelocity = 0.6;
229 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
230 filtered_goal_distance += kMaxVelocity * 0.02;
231 } else if (goal_distance <
232 -kMaxVelocity * 0.02 + filtered_goal_distance) {
233 filtered_goal_distance -= kMaxVelocity * 0.02;
234 } else {
235 filtered_goal_distance = goal_distance;
236 }
237 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
238 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
239 is_high_gear_ = false;
240
241 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
242 }
243 if (!drivetrain.goal.MakeWithBuilder()
244 .steering(wheel)
245 .throttle(throttle)
246 .highgear(is_high_gear_)
247 .quickturn(data.IsPressed(kQuickTurn))
248 .control_loop_driving(is_control_loop_driving)
249 .left_goal(left_goal)
250 .right_goal(right_goal)
251 .Send()) {
252 LOG(WARNING, "sending stick values failed\n");
253 }
254 if (data.PosEdge(kShiftHigh)) {
255 is_high_gear_ = false;
256 }
257 if (data.PosEdge(kShiftLow)) {
258 is_high_gear_ = true;
259 }
260 }
261
262 void SetGoal(ClawGoal goal) {
263 goal_angle_ = goal.angle;
264 separation_angle_ = goal.separation;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800265 velocity_compensation_ = false;
266 intake_power_ = 0.0;
267 }
268
269 void SetGoal(ShotGoal goal) {
270 goal_angle_ = goal.claw.angle;
271 separation_angle_ = goal.claw.separation;
272 shot_power_ = goal.shot_power;
273 velocity_compensation_ = goal.velocity_compensation;
274 intake_power_ = goal.intake_power;
Austin Schuh58d23682014-02-23 01:39:50 -0800275 }
276
277 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
278 HandleDrivetrain(data);
Austin Schuhc95c2b72014-03-02 11:56:49 -0800279 if (!data.GetControlBit(ControlBit::kEnabled)) {
280 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800281 LOG(DEBUG, "Canceling\n");
Austin Schuhc95c2b72014-03-02 11:56:49 -0800282 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800283 if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) {
284 intake_power_ = 0.0;
Austin Schuh5f4944e2014-03-09 16:40:39 -0700285 separation_angle_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800286 }
Austin Schuh58d23682014-02-23 01:39:50 -0800287
Brian Silverman18f6e642014-03-13 18:52:47 -0700288 static const double kAdjustClawGoalDeadband = 0.08;
289 double claw_goal_adjust = data.GetAxis(kAdjustClawGoal);
290 if (::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) {
291 claw_goal_adjust = 0;
292 } else {
293 claw_goal_adjust = (claw_goal_adjust -
294 ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband
295 : kAdjustClawGoalDeadband)) *
296 0.035;
297 }
298 double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation);
299 if (::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) {
300 claw_separation_adjust = 0;
301 } else {
302 claw_separation_adjust =
303 (claw_separation_adjust -
304 ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband
305 : kAdjustClawGoalDeadband)) *
306 -0.035;
307 }
308
Brian Silverman4d1795d2014-03-13 15:53:40 -0700309 if (data.GetAxis(kFlipRobot) > 0.5) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700310 claw_goal_adjust += claw_separation_adjust;
311 claw_goal_adjust *= -1;
312
Austin Schuh80ff2e12014-03-08 12:06:19 -0800313 if (data.IsPressed(kIntakeOpenPosition)) {
314 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800315 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800316 SetGoal(kFlippedIntakeOpenGoal);
317 } else if (data.IsPressed(kIntakePosition)) {
318 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800319 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800320 SetGoal(kFlippedIntakeGoal);
321 } else if (data.IsPressed(kTuck)) {
322 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800323 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800324 SetGoal(kFlippedTuckGoal);
325 } else if (data.PosEdge(kLongShot)) {
326 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800327 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800328 SetGoal(kFlippedLongShotGoal);
329 } else if (data.PosEdge(kMediumShot)) {
330 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800331 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800332 SetGoal(kFlippedMediumShotGoal);
333 } else if (data.PosEdge(kShortShot)) {
334 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800335 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800336 SetGoal(kFlippedShortShotGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800337 } else if (data.PosEdge(kTrussShot)) {
338 action_queue_.CancelAllActions();
339 LOG(DEBUG, "Canceling\n");
340 SetGoal(kTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800341 }
342 } else {
343 if (data.IsPressed(kIntakeOpenPosition)) {
344 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800345 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800346 SetGoal(kIntakeOpenGoal);
347 } else if (data.IsPressed(kIntakePosition)) {
348 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800349 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800350 SetGoal(kIntakeGoal);
351 } else if (data.IsPressed(kTuck)) {
352 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800353 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800354 SetGoal(kTuckGoal);
355 } else if (data.PosEdge(kLongShot)) {
356 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800357 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800358 SetGoal(kLongShotGoal);
359 } else if (data.PosEdge(kMediumShot)) {
360 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800361 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800362 SetGoal(kMediumShotGoal);
363 } else if (data.PosEdge(kShortShot)) {
364 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800365 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800366 SetGoal(kShortShotGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800367 } else if (data.PosEdge(kTrussShot)) {
368 action_queue_.CancelAllActions();
369 LOG(DEBUG, "Canceling\n");
370 SetGoal(kTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800371 }
Austin Schuh58d23682014-02-23 01:39:50 -0800372 }
373
Austin Schuhade6d082014-03-09 00:53:06 -0800374 /*
Austin Schuh80ff2e12014-03-08 12:06:19 -0800375 if (data.PosEdge(kCatch)) {
376 auto catch_action = MakeCatchAction();
377 catch_action->GetGoal()->catch_angle = goal_angle_;
378 action_queue_.QueueAction(::std::move(catch_action));
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800379 }
Austin Schuhade6d082014-03-09 00:53:06 -0800380 */
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800381
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800382 if (data.PosEdge(kFire)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800383 action_queue_.QueueAction(actions::MakeShootAction());
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800384 }
385
386 action_queue_.Tick();
Austin Schuhc95c2b72014-03-02 11:56:49 -0800387 if (data.IsPressed(kUnload) || data.IsPressed(kReload)) {
388 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800389 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800390 intake_power_ = 0.0;
391 velocity_compensation_ = false;
Austin Schuhc95c2b72014-03-02 11:56:49 -0800392 }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800393
394 // Send out the claw and shooter goals if no actions are running.
395 if (!action_queue_.Running()) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700396 goal_angle_ += claw_goal_adjust;
397 separation_angle_ += claw_separation_adjust;
398
Austin Schuh80ff2e12014-03-08 12:06:19 -0800399 // If the action just ended, turn the intake off and stop velocity
400 // compensating.
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800401 if (was_running_) {
402 intake_power_ = 0.0;
403 velocity_compensation_ = false;
404 }
405
406 control_loops::drivetrain.status.FetchLatest();
Austin Schuhade6d082014-03-09 00:53:06 -0800407 double goal_angle =
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800408 goal_angle_ +
409 (velocity_compensation_
410 ? SpeedToAngleOffset(
411 control_loops::drivetrain.status->robot_speed)
412 : 0.0);
Austin Schuhade6d082014-03-09 00:53:06 -0800413 double separation_angle = separation_angle_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800414
Austin Schuhade6d082014-03-09 00:53:06 -0800415 if (data.IsPressed(kCatch)) {
416 const double kCatchSeparation = 1.0;
417 goal_angle -= kCatchSeparation / 2.0;
418 separation_angle = kCatchSeparation;
419 }
420
421 bool intaking =
422 data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) ||
423 data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch);
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800424 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800425 .bottom_angle(goal_angle)
Austin Schuhade6d082014-03-09 00:53:06 -0800426 .separation_angle(separation_angle)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800427 .intake(intaking ? 12.0
428 : (data.IsPressed(kRollersOut) ? -12.0
429 : intake_power_))
430 .centering(intaking ? 12.0 : 0.0)
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800431 .Send()) {
432 LOG(WARNING, "sending claw goal failed\n");
433 }
434
435 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
436 .shot_power(shot_power_)
437 .shot_requested(data.IsPressed(kFire))
438 .unload_requested(data.IsPressed(kUnload))
439 .load_requested(data.IsPressed(kReload))
440 .Send()) {
441 LOG(WARNING, "sending shooter goal failed\n");
442 }
Austin Schuh58d23682014-02-23 01:39:50 -0800443 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800444 was_running_ = action_queue_.Running();
445 }
446
447 double SpeedToAngleOffset(double speed) {
448 const frc971::constants::Values &values = frc971::constants::GetValues();
449 // scale speed to a [0.0-1.0] on something close to the max
450 // TODO(austin): Change the scale factor for different shots.
Austin Schuhade6d082014-03-09 00:53:06 -0800451 return (speed / values.drivetrain_max_speed) * 0.2;
Austin Schuh58d23682014-02-23 01:39:50 -0800452 }
453
Austin Schuh01c652b2014-02-21 23:13:42 -0800454 private:
Austin Schuh58d23682014-02-23 01:39:50 -0800455 bool is_high_gear_;
456 double shot_power_;
457 double goal_angle_;
458 double separation_angle_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800459 bool velocity_compensation_;
460 double intake_power_;
461 bool was_running_;
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800462
463 ActionQueue action_queue_;
Brian Silverman756f9ff2014-01-17 23:40:23 -0800464};
465
466} // namespace joysticks
467} // namespace input
468} // namespace frc971
469
470int main() {
471 ::aos::Init();
472 ::frc971::input::joysticks::Reader reader;
473 reader.Run();
474 ::aos::Cleanup();
475}