blob: 822d9f9cc78144653fedb8c54f7dd1d71486e54a [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 Silverman0d09e3b2014-04-10 21:15:31 -070096// 8" between near edge of colored line and rear edge of bumper
Austin Schuh5d8c5e72014-03-07 20:24:34 -080097const ShotGoal kLongShotGoal = {
Brian Silverman55d7f572014-04-17 14:33:54 -070098 {-1.08, kShootSeparation}, 145, 0.04, kIntakePower};
Brian Silverman0d09e3b2014-04-10 21:15:31 -070099// 34" {-1.06, kShootSeparation}, 140, 0.04, kIntakePower};
Austin Schuh80ff2e12014-03-08 12:06:19 -0800100const ShotGoal kFlippedLongShotGoal = {
Brian Silverman55d7f572014-04-17 14:33:54 -0700101 {0.96, kShootSeparation}, 145, 0.09, kIntakePower};
Brian Silverman0d09e3b2014-04-10 21:15:31 -0700102// 34" {0.96, kShootSeparation}, 140, 0.09, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700103
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 Silverman55d7f572014-04-17 14:33:54 -0700113 {-0.68, kShootSeparation}, 115.0, 0.4, kIntakePower};
114const 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 Silverman73c48392014-04-05 07:07:41 -0700119const ShotGoal kFlippedHumanShotGoal = {
120 {0.90, kShootSeparation}, 140, 0, kIntakePower};
Brian Silverman0e7c03e2014-03-23 17:06:24 -0700121const ShotGoal kTrussShotGoal = {
Brian Silverman8d5dda42014-04-06 15:59:49 -0700122 {-0.68, kShootSeparation}, 83.0, 0.4, kIntakePower};
Brian Silverman73c48392014-04-05 07:07:41 -0700123const ShotGoal kFlippedTrussShotGoal = {
Brian Silverman8d5dda42014-04-06 15:59:49 -0700124 {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower};
125
126const ClawGoal k254PassGoal = {-1.95, kGrabSeparation};
127const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation};
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800128
129// Makes a new ShootAction action.
Austin Schuh80ff2e12014-03-08 12:06:19 -0800130::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>
131MakeCatchAction() {
132 return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>(
133 new TypedAction< ::frc971::actions::CatchActionGroup>(
134 &::frc971::actions::catch_action));
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800135}
136
137// A queue which queues Actions and cancels them.
138class ActionQueue {
139 public:
140 // Queues up an action for sending.
141 void QueueAction(::std::unique_ptr<Action> action) {
142 if (current_action_) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800143 LOG(INFO, "Queueing action, canceling prior\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800144 current_action_->Cancel();
145 next_action_ = ::std::move(action);
146 } else {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800147 LOG(INFO, "Queueing action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800148 current_action_ = ::std::move(action);
149 current_action_->Start();
150 }
151 }
152
153 // Cancels the current action, and runs the next one when the current one has
154 // finished.
155 void CancelCurrentAction() {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800156 LOG(INFO, "Canceling current action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800157 if (current_action_) {
158 current_action_->Cancel();
159 }
160 }
161
162 // Cancels all running actions.
163 void CancelAllActions() {
Brian Silverman101b9642014-03-08 12:45:16 -0800164 LOG(DEBUG, "Cancelling all actions\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800165 if (current_action_) {
166 current_action_->Cancel();
167 }
168 next_action_.reset();
169 }
170
171 // Runs the next action when the current one is finished running.
172 void Tick() {
173 if (current_action_) {
174 if (!current_action_->Running()) {
Austin Schuhc95c2b72014-03-02 11:56:49 -0800175 LOG(INFO, "Action is done.\n");
176 current_action_ = ::std::move(next_action_);
177 if (current_action_) {
178 LOG(INFO, "Running next action\n");
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800179 current_action_->Start();
180 }
181 }
182 }
183 }
184
185 // Returns true if any action is running or could be running.
186 // For a one cycle faster response, call Tick before running this.
Brian Silverman2c1e0342014-04-11 16:15:01 -0700187 bool Running() { return static_cast<bool>(current_action_); }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800188
189 private:
190 ::std::unique_ptr<Action> current_action_;
191 ::std::unique_ptr<Action> next_action_;
192};
193
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800194
Brian Silverman756f9ff2014-01-17 23:40:23 -0800195class Reader : public ::aos::input::JoystickInput {
196 public:
Austin Schuh58d23682014-02-23 01:39:50 -0800197 Reader()
Brian Silvermane3a277a2014-04-13 14:56:57 -0700198 : is_high_gear_(false),
Austin Schuh9cb836e2014-02-23 19:25:55 -0800199 shot_power_(80.0),
Austin Schuh58d23682014-02-23 01:39:50 -0800200 goal_angle_(0.0),
Brian Silverman545f2ad2014-03-14 12:31:42 -0700201 separation_angle_(kGrabSeparation),
Brian Silvermana379f002014-03-22 19:34:53 -0700202 velocity_compensation_(0.0),
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800203 intake_power_(0.0),
204 was_running_(false) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -0800205
206 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Brian Silverman756f9ff2014-01-17 23:40:23 -0800207 if (data.GetControlBit(ControlBit::kAutonomous)) {
208 if (data.PosEdge(ControlBit::kEnabled)){
209 LOG(INFO, "Starting auto mode\n");
210 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800211 .run_auto(true)
212 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800213 } else if (data.NegEdge(ControlBit::kEnabled)) {
214 LOG(INFO, "Stopping auto mode\n");
215 ::frc971::autonomous::autonomous.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800216 .run_auto(false)
217 .Send();
Brian Silverman756f9ff2014-01-17 23:40:23 -0800218 }
Austin Schuh58d23682014-02-23 01:39:50 -0800219 } else {
220 HandleTeleop(data);
Brian Silverman756f9ff2014-01-17 23:40:23 -0800221 }
222 }
Austin Schuh58d23682014-02-23 01:39:50 -0800223
224 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
225 bool is_control_loop_driving = false;
226 double left_goal = 0.0;
227 double right_goal = 0.0;
228 const double wheel = -data.GetAxis(kSteeringWheel);
229 const double throttle = -data.GetAxis(kDriveThrottle);
230 const double kThrottleGain = 1.0 / 2.5;
231 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
232 data.IsPressed(kDriveControlLoopEnable2))) {
233 // TODO(austin): Static sucks!
234 static double distance = 0.0;
235 static double angle = 0.0;
236 static double filtered_goal_distance = 0.0;
237 if (data.PosEdge(kDriveControlLoopEnable1) ||
238 data.PosEdge(kDriveControlLoopEnable2)) {
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800239 if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) {
Austin Schuh58d23682014-02-23 01:39:50 -0800240 distance = (drivetrain.position->left_encoder +
241 drivetrain.position->right_encoder) /
242 2.0 -
243 throttle * kThrottleGain / 2.0;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800244 angle = gyro_reading->angle;
Austin Schuh58d23682014-02-23 01:39:50 -0800245 filtered_goal_distance = distance;
246 }
247 }
248 is_control_loop_driving = true;
249
250 // const double gyro_angle = Gyro.View().angle;
251 const double goal_theta = angle - wheel * 0.27;
252 const double goal_distance = distance + throttle * kThrottleGain;
253 const double robot_width = 22.0 / 100.0 * 2.54;
254 const double kMaxVelocity = 0.6;
255 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
256 filtered_goal_distance += kMaxVelocity * 0.02;
257 } else if (goal_distance <
258 -kMaxVelocity * 0.02 + filtered_goal_distance) {
259 filtered_goal_distance -= kMaxVelocity * 0.02;
260 } else {
261 filtered_goal_distance = goal_distance;
262 }
263 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
264 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
265 is_high_gear_ = false;
266
267 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
268 }
269 if (!drivetrain.goal.MakeWithBuilder()
270 .steering(wheel)
271 .throttle(throttle)
272 .highgear(is_high_gear_)
273 .quickturn(data.IsPressed(kQuickTurn))
274 .control_loop_driving(is_control_loop_driving)
275 .left_goal(left_goal)
276 .right_goal(right_goal)
277 .Send()) {
278 LOG(WARNING, "sending stick values failed\n");
279 }
280 if (data.PosEdge(kShiftHigh)) {
281 is_high_gear_ = false;
282 }
283 if (data.PosEdge(kShiftLow)) {
284 is_high_gear_ = true;
285 }
286 }
287
288 void SetGoal(ClawGoal goal) {
289 goal_angle_ = goal.angle;
290 separation_angle_ = goal.separation;
Brian7e294392014-03-31 12:39:13 -0700291 moving_for_shot_ = false;
Brian Silvermana379f002014-03-22 19:34:53 -0700292 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800293 intake_power_ = 0.0;
294 }
295
296 void SetGoal(ShotGoal goal) {
297 goal_angle_ = goal.claw.angle;
Brian7e294392014-03-31 12:39:13 -0700298 shot_separation_angle_ = goal.claw.separation;
299 separation_angle_ = kGrabSeparation;
300 moving_for_shot_ = true;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800301 shot_power_ = goal.shot_power;
302 velocity_compensation_ = goal.velocity_compensation;
303 intake_power_ = goal.intake_power;
Austin Schuh58d23682014-02-23 01:39:50 -0800304 }
305
306 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
307 HandleDrivetrain(data);
Austin Schuhc95c2b72014-03-02 11:56:49 -0800308 if (!data.GetControlBit(ControlBit::kEnabled)) {
309 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800310 LOG(DEBUG, "Canceling\n");
Austin Schuhc95c2b72014-03-02 11:56:49 -0800311 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800312 if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) {
313 intake_power_ = 0.0;
Brian Silverman545f2ad2014-03-14 12:31:42 -0700314 separation_angle_ = kGrabSeparation;
Brian7e294392014-03-31 12:39:13 -0700315 moving_for_shot_ = false;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800316 }
Austin Schuh58d23682014-02-23 01:39:50 -0800317
Brian Silverman18f6e642014-03-13 18:52:47 -0700318 static const double kAdjustClawGoalDeadband = 0.08;
319 double claw_goal_adjust = data.GetAxis(kAdjustClawGoal);
320 if (::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) {
321 claw_goal_adjust = 0;
322 } else {
323 claw_goal_adjust = (claw_goal_adjust -
324 ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband
325 : kAdjustClawGoalDeadband)) *
326 0.035;
327 }
328 double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation);
329 if (::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) {
330 claw_separation_adjust = 0;
331 } else {
332 claw_separation_adjust =
333 (claw_separation_adjust -
334 ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband
335 : kAdjustClawGoalDeadband)) *
336 -0.035;
337 }
338
Brian6faa7822014-03-31 18:00:28 -0700339 if (data.GetAxis(kFlipRobot) > 0.9) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700340 claw_goal_adjust += claw_separation_adjust;
341 claw_goal_adjust *= -1;
342
Austin Schuh80ff2e12014-03-08 12:06:19 -0800343 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(kFlippedIntakeOpenGoal);
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(kFlippedIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700351 } else if (data.IsPressed(kVerticalTuck)) {
352 action_queue_.CancelAllActions();
353 LOG(DEBUG, "Canceling\n");
354 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800355 } else if (data.IsPressed(kTuck)) {
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(kFlippedTuckGoal);
359 } else if (data.PosEdge(kLongShot)) {
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(kFlippedLongShotGoal);
363 } else if (data.PosEdge(kMediumShot)) {
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(kFlippedMediumShotGoal);
367 } else if (data.PosEdge(kShortShot)) {
368 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800369 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800370 SetGoal(kFlippedShortShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700371 } else if (data.PosEdge(kHumanPlayerShot)) {
372 action_queue_.CancelAllActions();
373 LOG(DEBUG, "Canceling\n");
Brian Silverman73c48392014-04-05 07:07:41 -0700374 SetGoal(kFlippedHumanShotGoal);
Brian Silverman8d5dda42014-04-06 15:59:49 -0700375 } else if (data.PosEdge(kUserLeft)) {
376 action_queue_.CancelAllActions();
377 LOG(DEBUG, "Canceling\n");
378 SetGoal(kFlipped254PassGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800379 } else if (data.PosEdge(kTrussShot)) {
380 action_queue_.CancelAllActions();
381 LOG(DEBUG, "Canceling\n");
Brian Silverman73c48392014-04-05 07:07:41 -0700382 SetGoal(kFlippedTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800383 }
384 } else {
385 if (data.IsPressed(kIntakeOpenPosition)) {
386 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800387 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800388 SetGoal(kIntakeOpenGoal);
389 } else if (data.IsPressed(kIntakePosition)) {
390 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800391 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800392 SetGoal(kIntakeGoal);
Brian6faa7822014-03-31 18:00:28 -0700393 } else if (data.IsPressed(kVerticalTuck)) {
394 action_queue_.CancelAllActions();
395 LOG(DEBUG, "Canceling\n");
396 SetGoal(kVerticalTuckGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800397 } else if (data.IsPressed(kTuck)) {
398 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800399 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800400 SetGoal(kTuckGoal);
401 } else if (data.PosEdge(kLongShot)) {
402 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800403 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800404 SetGoal(kLongShotGoal);
405 } else if (data.PosEdge(kMediumShot)) {
406 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800407 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800408 SetGoal(kMediumShotGoal);
409 } else if (data.PosEdge(kShortShot)) {
410 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800411 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800412 SetGoal(kShortShotGoal);
Brian6faa7822014-03-31 18:00:28 -0700413 } else if (data.PosEdge(kHumanPlayerShot)) {
414 action_queue_.CancelAllActions();
415 LOG(DEBUG, "Canceling\n");
416 SetGoal(kHumanShotGoal);
Brian Silverman8d5dda42014-04-06 15:59:49 -0700417 } else if (data.PosEdge(kUserLeft)) {
418 action_queue_.CancelAllActions();
419 LOG(DEBUG, "Canceling\n");
420 SetGoal(k254PassGoal);
Austin Schuhade6d082014-03-09 00:53:06 -0800421 } else if (data.PosEdge(kTrussShot)) {
422 action_queue_.CancelAllActions();
423 LOG(DEBUG, "Canceling\n");
424 SetGoal(kTrussShotGoal);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800425 }
Austin Schuh58d23682014-02-23 01:39:50 -0800426 }
427
Austin Schuhade6d082014-03-09 00:53:06 -0800428 /*
Austin Schuh80ff2e12014-03-08 12:06:19 -0800429 if (data.PosEdge(kCatch)) {
430 auto catch_action = MakeCatchAction();
431 catch_action->GetGoal()->catch_angle = goal_angle_;
432 action_queue_.QueueAction(::std::move(catch_action));
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800433 }
Austin Schuhade6d082014-03-09 00:53:06 -0800434 */
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800435
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800436 if (data.PosEdge(kFire)) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800437 action_queue_.QueueAction(actions::MakeShootAction());
Brian Silverman9f863a02014-03-29 17:42:36 -0700438 } else if (data.NegEdge(kFire)) {
439 action_queue_.CancelCurrentAction();
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800440 }
441
442 action_queue_.Tick();
Austin Schuhc95c2b72014-03-02 11:56:49 -0800443 if (data.IsPressed(kUnload) || data.IsPressed(kReload)) {
444 action_queue_.CancelAllActions();
Austin Schuhade6d082014-03-09 00:53:06 -0800445 LOG(DEBUG, "Canceling\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800446 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700447 velocity_compensation_ = 0.0;
Austin Schuhc95c2b72014-03-02 11:56:49 -0800448 }
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800449
450 // Send out the claw and shooter goals if no actions are running.
451 if (!action_queue_.Running()) {
Brian Silverman18f6e642014-03-13 18:52:47 -0700452 goal_angle_ += claw_goal_adjust;
453 separation_angle_ += claw_separation_adjust;
454
Austin Schuh80ff2e12014-03-08 12:06:19 -0800455 // If the action just ended, turn the intake off and stop velocity
456 // compensating.
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800457 if (was_running_) {
458 intake_power_ = 0.0;
Brian Silvermana379f002014-03-22 19:34:53 -0700459 velocity_compensation_ = 0.0;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800460 }
461
462 control_loops::drivetrain.status.FetchLatest();
Brian Silvermana379f002014-03-22 19:34:53 -0700463 double goal_angle = goal_angle_;
464 if (control_loops::drivetrain.status.get()) {
465 goal_angle +=
466 SpeedToAngleOffset(control_loops::drivetrain.status->robot_speed);
467 } else {
468 LOG_INTERVAL(no_drivetrain_status_);
469 }
Brian7e294392014-03-31 12:39:13 -0700470
471 if (moving_for_shot_) {
472 auto &claw_status = control_loops::claw_queue_group.status;
473 claw_status.FetchLatest();
474 if (claw_status.get()) {
Brian6faa7822014-03-31 18:00:28 -0700475 if (::std::abs(claw_status->bottom - goal_angle) < 0.2) {
Brian7e294392014-03-31 12:39:13 -0700476 moving_for_shot_ = false;
477 separation_angle_ = shot_separation_angle_;
478 }
479 }
480 }
481
Austin Schuhade6d082014-03-09 00:53:06 -0800482 double separation_angle = separation_angle_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800483
Austin Schuhade6d082014-03-09 00:53:06 -0800484 if (data.IsPressed(kCatch)) {
485 const double kCatchSeparation = 1.0;
486 goal_angle -= kCatchSeparation / 2.0;
487 separation_angle = kCatchSeparation;
488 }
489
490 bool intaking =
491 data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) ||
492 data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch);
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800493 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800494 .bottom_angle(goal_angle)
Austin Schuhade6d082014-03-09 00:53:06 -0800495 .separation_angle(separation_angle)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800496 .intake(intaking ? 12.0
497 : (data.IsPressed(kRollersOut) ? -12.0
498 : intake_power_))
499 .centering(intaking ? 12.0 : 0.0)
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800500 .Send()) {
501 LOG(WARNING, "sending claw goal failed\n");
502 }
503
504 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
505 .shot_power(shot_power_)
506 .shot_requested(data.IsPressed(kFire))
507 .unload_requested(data.IsPressed(kUnload))
508 .load_requested(data.IsPressed(kReload))
509 .Send()) {
510 LOG(WARNING, "sending shooter goal failed\n");
511 }
Austin Schuh58d23682014-02-23 01:39:50 -0800512 }
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800513 was_running_ = action_queue_.Running();
514 }
515
516 double SpeedToAngleOffset(double speed) {
517 const frc971::constants::Values &values = frc971::constants::GetValues();
518 // scale speed to a [0.0-1.0] on something close to the max
519 // TODO(austin): Change the scale factor for different shots.
Brian Silvermana379f002014-03-22 19:34:53 -0700520 return (speed / values.drivetrain_max_speed) * velocity_compensation_;
Austin Schuh58d23682014-02-23 01:39:50 -0800521 }
522
Austin Schuh01c652b2014-02-21 23:13:42 -0800523 private:
Austin Schuh58d23682014-02-23 01:39:50 -0800524 bool is_high_gear_;
525 double shot_power_;
526 double goal_angle_;
Brian7e294392014-03-31 12:39:13 -0700527 double separation_angle_, shot_separation_angle_;
Brian Silvermana379f002014-03-22 19:34:53 -0700528 double velocity_compensation_;
Austin Schuh5d8c5e72014-03-07 20:24:34 -0800529 double intake_power_;
530 bool was_running_;
Brian7e294392014-03-31 12:39:13 -0700531 bool moving_for_shot_ = false;
Austin Schuhb7dfabc2014-03-01 18:57:42 -0800532
533 ActionQueue action_queue_;
Brian Silvermana379f002014-03-22 19:34:53 -0700534
535 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
536 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
537 "no drivetrain status");
Brian Silverman756f9ff2014-01-17 23:40:23 -0800538};
539
540} // namespace joysticks
541} // namespace input
542} // namespace frc971
543
544int main() {
545 ::aos::Init();
546 ::frc971::input::joysticks::Reader reader;
547 reader.Run();
548 ::aos::Cleanup();
549}