blob: 7b97bbdc32efe4ecbed19e4f0f878a3a21ce25f9 [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);
Austin Schuh9cb836e2014-02-23 19:25:55 -080044const ButtonLocation kUnload(2, 11);
45const ButtonLocation kReload(2, 6);
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);
Austin Schuh80ff2e12014-03-08 12:06:19 -080053const JoystickAxis kFlipRobot(3, 3);
Austin Schuh58d23682014-02-23 01:39:50 -080054
Austin Schuh80ff2e12014-03-08 12:06:19 -080055const ButtonLocation kLongShot(3, 7);
56const ButtonLocation kMediumShot(3, 6);
57const ButtonLocation kShortShot(3, 2);
Austin Schuhade6d082014-03-09 00:53:06 -080058const ButtonLocation kTrussShot(3, 1);
Austin Schuh58d23682014-02-23 01:39:50 -080059
Brian Silverman18f6e642014-03-13 18:52:47 -070060const JoystickAxis kAdjustClawGoal(3, 2);
61const JoystickAxis kAdjustClawSeparation(3, 1);
62
Austin Schuh58d23682014-02-23 01:39:50 -080063struct ClawGoal {
64 double angle;
65 double separation;
66};
67
Austin Schuh5d8c5e72014-03-07 20:24:34 -080068struct ShotGoal {
69 ClawGoal claw;
70 double shot_power;
Brian Silvermana379f002014-03-22 19:34:53 -070071 double velocity_compensation;
Austin Schuh5d8c5e72014-03-07 20:24:34 -080072 double intake_power;
73};
74
Brian Silverman545f2ad2014-03-14 12:31:42 -070075const double kIntakePower = 4.0;
Brian Silvermanb3cf0ef2014-03-22 12:45:55 -070076// TODO(brians): This wants to be -0.04 on the comp bot. Make them both the
77// same.
78const double kGrabSeparation = 0;
Brian Silverman545f2ad2014-03-14 12:31:42 -070079const double kShootSeparation = 0.11 + kGrabSeparation;
80
Austin Schuh58d23682014-02-23 01:39:50 -080081const ClawGoal kTuckGoal = {-2.273474, -0.749484};
Brian Silverman545f2ad2014-03-14 12:31:42 -070082const ClawGoal kIntakeGoal = {-2.273474, kGrabSeparation};
Austin Schuh9cb836e2014-02-23 19:25:55 -080083const ClawGoal kIntakeOpenGoal = {-2.0, 1.2};
Austin Schuh58d23682014-02-23 01:39:50 -080084
Austin Schuh80ff2e12014-03-08 12:06:19 -080085// TODO(austin): Tune these by hand...
86const ClawGoal kFlippedTuckGoal = {2.733474, -0.75};
Brian Silverman545f2ad2014-03-14 12:31:42 -070087const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation};
Austin Schuh80ff2e12014-03-08 12:06:19 -080088const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0};
89
Austin Schuhade6d082014-03-09 00:53:06 -080090//const ShotGoal kLongShotGoal = {
91 //{-M_PI / 2.0 + 0.46, kShootSeparation}, 120, false, kIntakePower};
Austin Schuh5d8c5e72014-03-07 20:24:34 -080092const ShotGoal kLongShotGoal = {
Brian Silvermana379f002014-03-22 19:34:53 -070093 {-1.04, kShootSeparation}, 140, 0.04, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -080094const ShotGoal kMediumShotGoal = {
Brian Silvermana379f002014-03-22 19:34:53 -070095 {-0.90, kShootSeparation}, 105, 0.2, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -080096const ShotGoal kShortShotGoal = {
Brian Silvermana379f002014-03-22 19:34:53 -070097 {-0.670, kShootSeparation}, 71.0, 0, kIntakePower};
Austin Schuhade6d082014-03-09 00:53:06 -080098const ShotGoal kTrussShotGoal = {
Brian Silvermana379f002014-03-22 19:34:53 -070099 {-0.05, kShootSeparation}, 61.0, 0, kIntakePower};
Brian Silverman756f9ff2014-01-17 23:40:23 -0800100
Austin Schuh80ff2e12014-03-08 12:06:19 -0800101const ShotGoal kFlippedLongShotGoal = {
Brian Silvermana379f002014-03-22 19:34:53 -0700102 {0.97, kShootSeparation}, 140, 0.08, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -0800103const ShotGoal kFlippedMediumShotGoal = {
Brian Silvermana379f002014-03-22 19:34:53 -0700104 {0.80, kShootSeparation}, 105, 0.2, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -0800105const ShotGoal kFlippedShortShotGoal = {
Brian Silvermana379f002014-03-22 19:34:53 -0700106 {0.57, kShootSeparation}, 80.0, 0, kIntakePower};
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800107
108// Makes a new ShootAction action.
Austin Schuh80ff2e12014-03-08 12:06:19 -0800109::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>
110MakeCatchAction() {
111 return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>(
112 new TypedAction< ::frc971::actions::CatchActionGroup>(
113 &::frc971::actions::catch_action));
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800114}
115
116// A queue which queues Actions and cancels them.
117class ActionQueue {
118 public:
119 // Queues up an action for sending.
120 void QueueAction(::std::unique_ptr<Action> action) {
121 if (current_action_) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800122 LOG(INFO, "Queueing action, canceling prior\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800123 current_action_->Cancel();
124 next_action_ = ::std::move(action);
125 } else {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800126 LOG(INFO, "Queueing action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800127 current_action_ = ::std::move(action);
128 current_action_->Start();
129 }
130 }
131
132 // Cancels the current action, and runs the next one when the current one has
133 // finished.
134 void CancelCurrentAction() {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800135 LOG(INFO, "Canceling current action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800136 if (current_action_) {
137 current_action_->Cancel();
138 }
139 }
140
141 // Cancels all running actions.
142 void CancelAllActions() {
Brian Silverman101b9642014-03-08 12:45:16 -0800143 LOG(DEBUG, "Cancelling all actions\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800144 if (current_action_) {
145 current_action_->Cancel();
146 }
147 next_action_.reset();
148 }
149
150 // Runs the next action when the current one is finished running.
151 void Tick() {
152 if (current_action_) {
153 if (!current_action_->Running()) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800154 LOG(INFO, "Action is done.\n");
155 current_action_ = ::std::move(next_action_);
156 if (current_action_) {
157 LOG(INFO, "Running next action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800158 current_action_->Start();
159 }
160 }
161 }
162 }
163
164 // Returns true if any action is running or could be running.
165 // For a one cycle faster response, call Tick before running this.
166 bool Running() { return (bool)current_action_; }
167
168 private:
169 ::std::unique_ptr<Action> current_action_;
170 ::std::unique_ptr<Action> next_action_;
171};
172
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800173
Brian Silverman756f9ff2014-01-17 23:40:23 -0800174class Reader : public ::aos::input::JoystickInput {
175 public:
Austin Schuh58d23682014-02-23 01:39:50 -0800176 Reader()
177 : is_high_gear_(false),
Austin Schuh9cb836e2014-02-23 19:25:55 -0800178 shot_power_(80.0),
Austin Schuh58d23682014-02-23 01:39:50 -0800179 goal_angle_(0.0),
Brian Silverman545f2ad2014-03-14 12:31:42 -0700180 separation_angle_(kGrabSeparation),
Brian Silvermana379f002014-03-22 19:34:53 -0700181 velocity_compensation_(0.0),
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800182 intake_power_(0.0),
183 was_running_(false) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -0800184
185 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Brian Silverman756f9ff2014-01-17 23:40:23 -0800186 if (data.GetControlBit(ControlBit::kAutonomous)) {
187 if (data.PosEdge(ControlBit::kEnabled)){
188 LOG(INFO, "Starting auto mode\n");
189 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800190 .run_auto(true)
191 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800192 } else if (data.NegEdge(ControlBit::kEnabled)) {
193 LOG(INFO, "Stopping auto mode\n");
194 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800195 .run_auto(false)
196 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800197 }
Austin Schuh58d23682014-02-23 01:39:50 -0800198 } else {
199 HandleTeleop(data);
Brian Silverman756f9ff2014-01-17 23:40:23 -0800200 }
201 }
Austin Schuh58d23682014-02-23 01:39:50 -0800202
203 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
204 bool is_control_loop_driving = false;
205 double left_goal = 0.0;
206 double right_goal = 0.0;
207 const double wheel = -data.GetAxis(kSteeringWheel);
208 const double throttle = -data.GetAxis(kDriveThrottle);
209 const double kThrottleGain = 1.0 / 2.5;
210 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
211 data.IsPressed(kDriveControlLoopEnable2))) {
212 // TODO(austin): Static sucks!
213 static double distance = 0.0;
214 static double angle = 0.0;
215 static double filtered_goal_distance = 0.0;
216 if (data.PosEdge(kDriveControlLoopEnable1) ||
217 data.PosEdge(kDriveControlLoopEnable2)) {
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800218 if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) {
Austin Schuh58d23682014-02-23 01:39:50 -0800219 distance = (drivetrain.position->left_encoder +
220 drivetrain.position->right_encoder) /
221 2.0 -
222 throttle * kThrottleGain / 2.0;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800223 angle = gyro_reading->angle;
Austin Schuh58d23682014-02-23 01:39:50 -0800224 filtered_goal_distance = distance;
225 }
226 }
227 is_control_loop_driving = true;
228
229 // const double gyro_angle = Gyro.View().angle;
230 const double goal_theta = angle - wheel * 0.27;
231 const double goal_distance = distance + throttle * kThrottleGain;
232 const double robot_width = 22.0 / 100.0 * 2.54;
233 const double kMaxVelocity = 0.6;
234 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
235 filtered_goal_distance += kMaxVelocity * 0.02;
236 } else if (goal_distance <
237 -kMaxVelocity * 0.02 + filtered_goal_distance) {
238 filtered_goal_distance -= kMaxVelocity * 0.02;
239 } else {
240 filtered_goal_distance = goal_distance;
241 }
242 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
243 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
244 is_high_gear_ = false;
245
246 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
247 }
248 if (!drivetrain.goal.MakeWithBuilder()
249 .steering(wheel)
250 .throttle(throttle)
251 .highgear(is_high_gear_)
252 .quickturn(data.IsPressed(kQuickTurn))
253 .control_loop_driving(is_control_loop_driving)
254 .left_goal(left_goal)
255 .right_goal(right_goal)
256 .Send()) {
257 LOG(WARNING, "sending stick values failed\n");
258 }
259 if (data.PosEdge(kShiftHigh)) {
260 is_high_gear_ = false;
261 }
262 if (data.PosEdge(kShiftLow)) {
263 is_high_gear_ = true;
264 }
265 }
266
267 void SetGoal(ClawGoal goal) {
268 goal_angle_ = goal.angle;
269 separation_angle_ = goal.separation;
Brian Silvermana379f002014-03-22 19:34:53 -0700270 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800271 intake_power_ = 0.0;
272 }
273
274 void SetGoal(ShotGoal goal) {
275 goal_angle_ = goal.claw.angle;
276 separation_angle_ = goal.claw.separation;
277 shot_power_ = goal.shot_power;
278 velocity_compensation_ = goal.velocity_compensation;
279 intake_power_ = goal.intake_power;
Austin Schuh58d23682014-02-23 01:39:50 -0800280 }
281
282 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
283 HandleDrivetrain(data);
Austin Schuhc95c2b72014-03-02 11:56:49 -0800284 if (!data.GetControlBit(ControlBit::kEnabled)) {
285 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800286 LOG(DEBUG, "Canceling\n");
Austin Schuhc95c2b72014-03-02 11:56:49 -0800287 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800288 if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) {
289 intake_power_ = 0.0;
Brian Silverman545f2ad2014-03-14 12:31:42 -0700290 separation_angle_ = kGrabSeparation;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800291 }
Austin Schuh58d23682014-02-23 01:39:50 -0800292
Brian Silverman18f6e642014-03-13 18:52:47 -0700293 static const double kAdjustClawGoalDeadband = 0.08;
294 double claw_goal_adjust = data.GetAxis(kAdjustClawGoal);
295 if (::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) {
296 claw_goal_adjust = 0;
297 } else {
298 claw_goal_adjust = (claw_goal_adjust -
299 ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband
300 : kAdjustClawGoalDeadband)) *
301 0.035;
302 }
303 double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation);
304 if (::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) {
305 claw_separation_adjust = 0;
306 } else {
307 claw_separation_adjust =
308 (claw_separation_adjust -
309 ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband
310 : kAdjustClawGoalDeadband)) *
311 -0.035;
312 }
313
Brian Silverman4d1795d2014-03-13 15:53:40 -0700314 if (data.GetAxis(kFlipRobot) > 0.5) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700315 claw_goal_adjust += claw_separation_adjust;
316 claw_goal_adjust *= -1;
317
Austin Schuh80ff2e12014-03-08 12:06:19 -0800318 if (data.IsPressed(kIntakeOpenPosition)) {
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(kFlippedIntakeOpenGoal);
322 } else if (data.IsPressed(kIntakePosition)) {
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(kFlippedIntakeGoal);
326 } else if (data.IsPressed(kTuck)) {
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(kFlippedTuckGoal);
330 } else if (data.PosEdge(kLongShot)) {
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(kFlippedLongShotGoal);
334 } else if (data.PosEdge(kMediumShot)) {
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(kFlippedMediumShotGoal);
338 } else if (data.PosEdge(kShortShot)) {
339 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800340 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800341 SetGoal(kFlippedShortShotGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800342 } else if (data.PosEdge(kTrussShot)) {
343 action_queue_.CancelAllActions();
344 LOG(DEBUG, "Canceling\n");
345 SetGoal(kTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800346 }
347 } else {
348 if (data.IsPressed(kIntakeOpenPosition)) {
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(kIntakeOpenGoal);
352 } else if (data.IsPressed(kIntakePosition)) {
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(kIntakeGoal);
356 } else if (data.IsPressed(kTuck)) {
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(kTuckGoal);
360 } else if (data.PosEdge(kLongShot)) {
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(kLongShotGoal);
364 } else if (data.PosEdge(kMediumShot)) {
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(kMediumShotGoal);
368 } else if (data.PosEdge(kShortShot)) {
369 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800370 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800371 SetGoal(kShortShotGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800372 } else if (data.PosEdge(kTrussShot)) {
373 action_queue_.CancelAllActions();
374 LOG(DEBUG, "Canceling\n");
375 SetGoal(kTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800376 }
Austin Schuh58d23682014-02-23 01:39:50 -0800377 }
378
Austin Schuhade6d082014-03-09 00:53:06 -0800379 /*
Austin Schuh80ff2e12014-03-08 12:06:19 -0800380 if (data.PosEdge(kCatch)) {
381 auto catch_action = MakeCatchAction();
382 catch_action->GetGoal()->catch_angle = goal_angle_;
383 action_queue_.QueueAction(::std::move(catch_action));
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800384 }
Austin Schuhade6d082014-03-09 00:53:06 -0800385 */
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800386
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800387 if (data.PosEdge(kFire)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800388 action_queue_.QueueAction(actions::MakeShootAction());
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800389 }
390
391 action_queue_.Tick();
Austin Schuhc95c2b72014-03-02 11:56:49 -0800392 if (data.IsPressed(kUnload) || data.IsPressed(kReload)) {
393 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800394 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800395 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700396 velocity_compensation_ = 0.0;
Austin Schuhc95c2b72014-03-02 11:56:49 -0800397 }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800398
399 // Send out the claw and shooter goals if no actions are running.
400 if (!action_queue_.Running()) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700401 goal_angle_ += claw_goal_adjust;
402 separation_angle_ += claw_separation_adjust;
403
Austin Schuh80ff2e12014-03-08 12:06:19 -0800404 // If the action just ended, turn the intake off and stop velocity
405 // compensating.
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800406 if (was_running_) {
407 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700408 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800409 }
410
411 control_loops::drivetrain.status.FetchLatest();
Brian Silvermana379f002014-03-22 19:34:53 -0700412 double goal_angle = goal_angle_;
413 if (control_loops::drivetrain.status.get()) {
414 goal_angle +=
415 SpeedToAngleOffset(control_loops::drivetrain.status->robot_speed);
416 } else {
417 LOG_INTERVAL(no_drivetrain_status_);
418 }
Austin Schuhade6d082014-03-09 00:53:06 -0800419 double separation_angle = separation_angle_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800420
Austin Schuhade6d082014-03-09 00:53:06 -0800421 if (data.IsPressed(kCatch)) {
422 const double kCatchSeparation = 1.0;
423 goal_angle -= kCatchSeparation / 2.0;
424 separation_angle = kCatchSeparation;
425 }
426
427 bool intaking =
428 data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) ||
429 data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch);
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800430 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800431 .bottom_angle(goal_angle)
Austin Schuhade6d082014-03-09 00:53:06 -0800432 .separation_angle(separation_angle)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800433 .intake(intaking ? 12.0
434 : (data.IsPressed(kRollersOut) ? -12.0
435 : intake_power_))
436 .centering(intaking ? 12.0 : 0.0)
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800437 .Send()) {
438 LOG(WARNING, "sending claw goal failed\n");
439 }
440
441 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
442 .shot_power(shot_power_)
443 .shot_requested(data.IsPressed(kFire))
444 .unload_requested(data.IsPressed(kUnload))
445 .load_requested(data.IsPressed(kReload))
446 .Send()) {
447 LOG(WARNING, "sending shooter goal failed\n");
448 }
Austin Schuh58d23682014-02-23 01:39:50 -0800449 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800450 was_running_ = action_queue_.Running();
451 }
452
453 double SpeedToAngleOffset(double speed) {
454 const frc971::constants::Values &values = frc971::constants::GetValues();
455 // scale speed to a [0.0-1.0] on something close to the max
456 // TODO(austin): Change the scale factor for different shots.
Brian Silvermana379f002014-03-22 19:34:53 -0700457 return (speed / values.drivetrain_max_speed) * velocity_compensation_;
Austin Schuh58d23682014-02-23 01:39:50 -0800458 }
459
Austin Schuh01c652b2014-02-21 23:13:42 -0800460 private:
Austin Schuh58d23682014-02-23 01:39:50 -0800461 bool is_high_gear_;
462 double shot_power_;
463 double goal_angle_;
464 double separation_angle_;
Brian Silvermana379f002014-03-22 19:34:53 -0700465 double velocity_compensation_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800466 double intake_power_;
467 bool was_running_;
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800468
469 ActionQueue action_queue_;
Brian Silvermana379f002014-03-22 19:34:53 -0700470
471 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
472 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
473 "no drivetrain status");
Brian Silverman756f9ff2014-01-17 23:40:23 -0800474};
475
476} // namespace joysticks
477} // namespace input
478} // namespace frc971
479
480int main() {
481 ::aos::Init();
482 ::frc971::input::joysticks::Reader reader;
483 reader.Run();
484 ::aos::Cleanup();
485}