Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 1 | #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 Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 8 | #include "aos/common/input/driver_station_data.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 9 | #include "aos/common/logging/logging.h" |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 10 | #include "aos/common/util/log_interval.h" |
| 11 | #include "aos/common/time.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 12 | |
| 13 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 14 | #include "frc971/constants.h" |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 15 | #include "frc971/queues/other_sensors.q.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 16 | #include "frc971/autonomous/auto.q.h" |
Brian Silverman | fac5c29 | 2014-02-17 15:26:57 -0800 | [diff] [blame] | 17 | #include "frc971/control_loops/claw/claw.q.h" |
| 18 | #include "frc971/control_loops/shooter/shooter.q.h" |
Ben Fredrickson | aa45045 | 2014-03-01 09:41:18 +0000 | [diff] [blame] | 19 | #include "frc971/actions/shoot_action.q.h" |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 20 | #include "frc971/actions/action_client.h" |
| 21 | #include "frc971/actions/catch_action.q.h" |
| 22 | #include "frc971/actions/shoot_action.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 23 | |
| 24 | using ::frc971::control_loops::drivetrain; |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 25 | using ::frc971::sensors::gyro_reading; |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 26 | |
| 27 | using ::aos::input::driver_station::ButtonLocation; |
| 28 | using ::aos::input::driver_station::JoystickAxis; |
| 29 | using ::aos::input::driver_station::ControlBit; |
| 30 | |
| 31 | namespace frc971 { |
| 32 | namespace input { |
| 33 | namespace joysticks { |
| 34 | |
| 35 | const ButtonLocation kDriveControlLoopEnable1(1, 7), |
| 36 | kDriveControlLoopEnable2(1, 11); |
| 37 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 38 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 39 | const ButtonLocation kQuickTurn(1, 5); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 40 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 41 | const ButtonLocation kCatch(3, 10); |
| 42 | |
| 43 | const ButtonLocation kFire(3, 9); |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 44 | const ButtonLocation kUnload(1, 4); |
| 45 | const ButtonLocation kReload(1, 2); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 46 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 47 | const ButtonLocation kRollersOut(3, 8); |
| 48 | const ButtonLocation kRollersIn(3, 3); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 49 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 50 | const ButtonLocation kTuck(3, 4); |
| 51 | const ButtonLocation kIntakePosition(3, 5); |
| 52 | const ButtonLocation kIntakeOpenPosition(3, 11); |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 53 | const ButtonLocation kVerticalTuck(2, 6); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 54 | const JoystickAxis kFlipRobot(3, 3); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 55 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 56 | const ButtonLocation kLongShot(3, 7); |
| 57 | const ButtonLocation kMediumShot(3, 6); |
| 58 | const ButtonLocation kShortShot(3, 2); |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 59 | const ButtonLocation kTrussShot(2, 11); |
| 60 | const ButtonLocation kHumanPlayerShot(3, 1); |
| 61 | |
| 62 | const ButtonLocation kUserLeft(2, 7); |
| 63 | const ButtonLocation kUserRight(2, 10); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 64 | |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 65 | const JoystickAxis kAdjustClawGoal(3, 2); |
| 66 | const JoystickAxis kAdjustClawSeparation(3, 1); |
| 67 | |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 68 | struct ClawGoal { |
| 69 | double angle; |
| 70 | double separation; |
| 71 | }; |
| 72 | |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 73 | struct ShotGoal { |
| 74 | ClawGoal claw; |
| 75 | double shot_power; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 76 | double velocity_compensation; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 77 | double intake_power; |
| 78 | }; |
| 79 | |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 80 | const double kIntakePower = 4.0; |
Brian Silverman | b3cf0ef | 2014-03-22 12:45:55 -0700 | [diff] [blame] | 81 | // TODO(brians): This wants to be -0.04 on the comp bot. Make them both the |
| 82 | // same. |
| 83 | const double kGrabSeparation = 0; |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 84 | const double kShootSeparation = 0.11 + kGrabSeparation; |
| 85 | |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 86 | const ClawGoal kTuckGoal = {-2.273474, -0.749484}; |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 87 | const ClawGoal kVerticalTuckGoal = {0, kGrabSeparation}; |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame] | 88 | const ClawGoal kIntakeGoal = {-2.24, kGrabSeparation}; |
| 89 | const ClawGoal kIntakeOpenGoal = {-2.0, 1.1}; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 90 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 91 | // TODO(austin): Tune these by hand... |
| 92 | const ClawGoal kFlippedTuckGoal = {2.733474, -0.75}; |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 93 | const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 94 | const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0}; |
| 95 | |
Brian Silverman | 0d09e3b | 2014-04-10 21:15:31 -0700 | [diff] [blame] | 96 | // 8" between near edge of colored line and rear edge of bumper |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 97 | const ShotGoal kLongShotGoal = { |
Brian Silverman | 55d7f57 | 2014-04-17 14:33:54 -0700 | [diff] [blame] | 98 | {-1.08, kShootSeparation}, 145, 0.04, kIntakePower}; |
Brian Silverman | 0d09e3b | 2014-04-10 21:15:31 -0700 | [diff] [blame] | 99 | // 34" {-1.06, kShootSeparation}, 140, 0.04, kIntakePower}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 100 | const ShotGoal kFlippedLongShotGoal = { |
Brian Silverman | 55d7f57 | 2014-04-17 14:33:54 -0700 | [diff] [blame] | 101 | {0.96, kShootSeparation}, 145, 0.09, kIntakePower}; |
Brian Silverman | 0d09e3b | 2014-04-10 21:15:31 -0700 | [diff] [blame] | 102 | // 34" {0.96, kShootSeparation}, 140, 0.09, kIntakePower}; |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 103 | |
| 104 | // 78" between near edge of colored line and rear edge of bumper |
| 105 | const ShotGoal kMediumShotGoal = { |
| 106 | {-0.95, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 107 | // 3/4" plunger {-0.90, kShootSeparation}, 105, 0.2, kIntakePower}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 108 | const ShotGoal kFlippedMediumShotGoal = { |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame] | 109 | {0.905, kShootSeparation}, 120, 0.2, kIntakePower}; |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 110 | // 3/4" plunger {0.80, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 111 | |
| 112 | const ShotGoal kShortShotGoal = { |
Brian Silverman | 55d7f57 | 2014-04-17 14:33:54 -0700 | [diff] [blame] | 113 | {-0.68, kShootSeparation}, 115.0, 0.4, kIntakePower}; |
| 114 | const ShotGoal kFlippedShortShotGoal = { |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame] | 115 | {0.67, kShootSeparation}, 115.0, 0.4, kIntakePower}; |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 116 | |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame] | 117 | const ShotGoal kHumanShotGoal = { |
| 118 | {-0.90, kShootSeparation}, 140, 0.04, kIntakePower}; |
Brian Silverman | 73c4839 | 2014-04-05 07:07:41 -0700 | [diff] [blame] | 119 | const ShotGoal kFlippedHumanShotGoal = { |
| 120 | {0.90, kShootSeparation}, 140, 0, kIntakePower}; |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 121 | const ShotGoal kTrussShotGoal = { |
Brian Silverman | 8d5dda4 | 2014-04-06 15:59:49 -0700 | [diff] [blame] | 122 | {-0.68, kShootSeparation}, 83.0, 0.4, kIntakePower}; |
Brian Silverman | 73c4839 | 2014-04-05 07:07:41 -0700 | [diff] [blame] | 123 | const ShotGoal kFlippedTrussShotGoal = { |
Brian Silverman | 8d5dda4 | 2014-04-06 15:59:49 -0700 | [diff] [blame] | 124 | {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower}; |
| 125 | |
| 126 | const ClawGoal k254PassGoal = {-1.95, kGrabSeparation}; |
| 127 | const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation}; |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 128 | |
| 129 | // Makes a new ShootAction action. |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 130 | ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>> |
| 131 | MakeCatchAction() { |
| 132 | return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>( |
| 133 | new TypedAction< ::frc971::actions::CatchActionGroup>( |
| 134 | &::frc971::actions::catch_action)); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // A queue which queues Actions and cancels them. |
| 138 | class ActionQueue { |
| 139 | public: |
| 140 | // Queues up an action for sending. |
| 141 | void QueueAction(::std::unique_ptr<Action> action) { |
| 142 | if (current_action_) { |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 143 | LOG(INFO, "Queueing action, canceling prior\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 144 | current_action_->Cancel(); |
| 145 | next_action_ = ::std::move(action); |
| 146 | } else { |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 147 | LOG(INFO, "Queueing action\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 148 | 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 Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 156 | LOG(INFO, "Canceling current action\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 157 | if (current_action_) { |
| 158 | current_action_->Cancel(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Cancels all running actions. |
| 163 | void CancelAllActions() { |
Brian Silverman | 101b964 | 2014-03-08 12:45:16 -0800 | [diff] [blame] | 164 | LOG(DEBUG, "Cancelling all actions\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 165 | 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 Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 175 | 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 Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 179 | 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 Silverman | 2c1e034 | 2014-04-11 16:15:01 -0700 | [diff] [blame] | 187 | bool Running() { return static_cast<bool>(current_action_); } |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 188 | |
| 189 | private: |
| 190 | ::std::unique_ptr<Action> current_action_; |
| 191 | ::std::unique_ptr<Action> next_action_; |
| 192 | }; |
| 193 | |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 194 | |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 195 | class Reader : public ::aos::input::JoystickInput { |
| 196 | public: |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 197 | Reader() |
Brian Silverman | e3a277a | 2014-04-13 14:56:57 -0700 | [diff] [blame] | 198 | : is_high_gear_(false), |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 199 | shot_power_(80.0), |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 200 | goal_angle_(0.0), |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 201 | separation_angle_(kGrabSeparation), |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 202 | velocity_compensation_(0.0), |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 203 | intake_power_(0.0), |
| 204 | was_running_(false) {} |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 205 | |
| 206 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 207 | if (data.GetControlBit(ControlBit::kAutonomous)) { |
| 208 | if (data.PosEdge(ControlBit::kEnabled)){ |
| 209 | LOG(INFO, "Starting auto mode\n"); |
| 210 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 211 | .run_auto(true) |
| 212 | .Send(); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 213 | } else if (data.NegEdge(ControlBit::kEnabled)) { |
| 214 | LOG(INFO, "Stopping auto mode\n"); |
| 215 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 216 | .run_auto(false) |
| 217 | .Send(); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 218 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 219 | } else { |
| 220 | HandleTeleop(data); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 221 | } |
| 222 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 223 | |
| 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 Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 239 | if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) { |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 240 | distance = (drivetrain.position->left_encoder + |
| 241 | drivetrain.position->right_encoder) / |
| 242 | 2.0 - |
| 243 | throttle * kThrottleGain / 2.0; |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 244 | angle = gyro_reading->angle; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 245 | 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; |
Brian | 7e29439 | 2014-03-31 12:39:13 -0700 | [diff] [blame] | 291 | moving_for_shot_ = false; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 292 | velocity_compensation_ = 0.0; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 293 | intake_power_ = 0.0; |
| 294 | } |
| 295 | |
| 296 | void SetGoal(ShotGoal goal) { |
| 297 | goal_angle_ = goal.claw.angle; |
Brian | 7e29439 | 2014-03-31 12:39:13 -0700 | [diff] [blame] | 298 | shot_separation_angle_ = goal.claw.separation; |
| 299 | separation_angle_ = kGrabSeparation; |
| 300 | moving_for_shot_ = true; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 301 | shot_power_ = goal.shot_power; |
| 302 | velocity_compensation_ = goal.velocity_compensation; |
| 303 | intake_power_ = goal.intake_power; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 307 | HandleDrivetrain(data); |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 308 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 309 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 310 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 311 | } |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 312 | if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) { |
| 313 | intake_power_ = 0.0; |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 314 | separation_angle_ = kGrabSeparation; |
Brian | 7e29439 | 2014-03-31 12:39:13 -0700 | [diff] [blame] | 315 | moving_for_shot_ = false; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 316 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 317 | |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 318 | 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 | |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 339 | if (data.GetAxis(kFlipRobot) > 0.9) { |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 340 | claw_goal_adjust += claw_separation_adjust; |
| 341 | claw_goal_adjust *= -1; |
| 342 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 343 | if (data.IsPressed(kIntakeOpenPosition)) { |
| 344 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 345 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 346 | SetGoal(kFlippedIntakeOpenGoal); |
| 347 | } else if (data.IsPressed(kIntakePosition)) { |
| 348 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 349 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 350 | SetGoal(kFlippedIntakeGoal); |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 351 | } else if (data.IsPressed(kVerticalTuck)) { |
| 352 | action_queue_.CancelAllActions(); |
| 353 | LOG(DEBUG, "Canceling\n"); |
| 354 | SetGoal(kVerticalTuckGoal); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 355 | } else if (data.IsPressed(kTuck)) { |
| 356 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 357 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 358 | SetGoal(kFlippedTuckGoal); |
| 359 | } else if (data.PosEdge(kLongShot)) { |
| 360 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 361 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 362 | SetGoal(kFlippedLongShotGoal); |
| 363 | } else if (data.PosEdge(kMediumShot)) { |
| 364 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 365 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 366 | SetGoal(kFlippedMediumShotGoal); |
| 367 | } else if (data.PosEdge(kShortShot)) { |
| 368 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 369 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 370 | SetGoal(kFlippedShortShotGoal); |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 371 | } else if (data.PosEdge(kHumanPlayerShot)) { |
| 372 | action_queue_.CancelAllActions(); |
| 373 | LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 73c4839 | 2014-04-05 07:07:41 -0700 | [diff] [blame] | 374 | SetGoal(kFlippedHumanShotGoal); |
Brian Silverman | 8d5dda4 | 2014-04-06 15:59:49 -0700 | [diff] [blame] | 375 | } else if (data.PosEdge(kUserLeft)) { |
| 376 | action_queue_.CancelAllActions(); |
| 377 | LOG(DEBUG, "Canceling\n"); |
| 378 | SetGoal(kFlipped254PassGoal); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 379 | } else if (data.PosEdge(kTrussShot)) { |
| 380 | action_queue_.CancelAllActions(); |
| 381 | LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 73c4839 | 2014-04-05 07:07:41 -0700 | [diff] [blame] | 382 | SetGoal(kFlippedTrussShotGoal); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 383 | } |
| 384 | } else { |
| 385 | if (data.IsPressed(kIntakeOpenPosition)) { |
| 386 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 387 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 388 | SetGoal(kIntakeOpenGoal); |
| 389 | } else if (data.IsPressed(kIntakePosition)) { |
| 390 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 391 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 392 | SetGoal(kIntakeGoal); |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 393 | } else if (data.IsPressed(kVerticalTuck)) { |
| 394 | action_queue_.CancelAllActions(); |
| 395 | LOG(DEBUG, "Canceling\n"); |
| 396 | SetGoal(kVerticalTuckGoal); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 397 | } else if (data.IsPressed(kTuck)) { |
| 398 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 399 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 400 | SetGoal(kTuckGoal); |
| 401 | } else if (data.PosEdge(kLongShot)) { |
| 402 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 403 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 404 | SetGoal(kLongShotGoal); |
| 405 | } else if (data.PosEdge(kMediumShot)) { |
| 406 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 407 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 408 | SetGoal(kMediumShotGoal); |
| 409 | } else if (data.PosEdge(kShortShot)) { |
| 410 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 411 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 412 | SetGoal(kShortShotGoal); |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 413 | } else if (data.PosEdge(kHumanPlayerShot)) { |
| 414 | action_queue_.CancelAllActions(); |
| 415 | LOG(DEBUG, "Canceling\n"); |
| 416 | SetGoal(kHumanShotGoal); |
Brian Silverman | 8d5dda4 | 2014-04-06 15:59:49 -0700 | [diff] [blame] | 417 | } else if (data.PosEdge(kUserLeft)) { |
| 418 | action_queue_.CancelAllActions(); |
| 419 | LOG(DEBUG, "Canceling\n"); |
| 420 | SetGoal(k254PassGoal); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 421 | } else if (data.PosEdge(kTrussShot)) { |
| 422 | action_queue_.CancelAllActions(); |
| 423 | LOG(DEBUG, "Canceling\n"); |
| 424 | SetGoal(kTrussShotGoal); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 425 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 428 | /* |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 429 | 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 Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 433 | } |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 434 | */ |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 435 | |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 436 | if (data.PosEdge(kFire)) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 437 | action_queue_.QueueAction(actions::MakeShootAction()); |
Brian Silverman | 9f863a0 | 2014-03-29 17:42:36 -0700 | [diff] [blame] | 438 | } else if (data.NegEdge(kFire)) { |
| 439 | action_queue_.CancelCurrentAction(); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | action_queue_.Tick(); |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 443 | if (data.IsPressed(kUnload) || data.IsPressed(kReload)) { |
| 444 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 445 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 446 | intake_power_ = 0.0; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 447 | velocity_compensation_ = 0.0; |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 448 | } |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 449 | |
| 450 | // Send out the claw and shooter goals if no actions are running. |
| 451 | if (!action_queue_.Running()) { |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 452 | goal_angle_ += claw_goal_adjust; |
| 453 | separation_angle_ += claw_separation_adjust; |
| 454 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 455 | // If the action just ended, turn the intake off and stop velocity |
| 456 | // compensating. |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 457 | if (was_running_) { |
| 458 | intake_power_ = 0.0; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 459 | velocity_compensation_ = 0.0; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | control_loops::drivetrain.status.FetchLatest(); |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 463 | 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 | } |
Brian | 7e29439 | 2014-03-31 12:39:13 -0700 | [diff] [blame] | 470 | |
| 471 | if (moving_for_shot_) { |
| 472 | auto &claw_status = control_loops::claw_queue_group.status; |
| 473 | claw_status.FetchLatest(); |
| 474 | if (claw_status.get()) { |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 475 | if (::std::abs(claw_status->bottom - goal_angle) < 0.2) { |
Brian | 7e29439 | 2014-03-31 12:39:13 -0700 | [diff] [blame] | 476 | moving_for_shot_ = false; |
| 477 | separation_angle_ = shot_separation_angle_; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 482 | double separation_angle = separation_angle_; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 483 | |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 484 | 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 Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 493 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder() |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 494 | .bottom_angle(goal_angle) |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 495 | .separation_angle(separation_angle) |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 496 | .intake(intaking ? 12.0 |
| 497 | : (data.IsPressed(kRollersOut) ? -12.0 |
| 498 | : intake_power_)) |
| 499 | .centering(intaking ? 12.0 : 0.0) |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 500 | .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 Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 512 | } |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 513 | 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 Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 520 | return (speed / values.drivetrain_max_speed) * velocity_compensation_; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 523 | private: |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 524 | bool is_high_gear_; |
| 525 | double shot_power_; |
| 526 | double goal_angle_; |
Brian | 7e29439 | 2014-03-31 12:39:13 -0700 | [diff] [blame] | 527 | double separation_angle_, shot_separation_angle_; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 528 | double velocity_compensation_; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 529 | double intake_power_; |
| 530 | bool was_running_; |
Brian | 7e29439 | 2014-03-31 12:39:13 -0700 | [diff] [blame] | 531 | bool moving_for_shot_ = false; |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 532 | |
| 533 | ActionQueue action_queue_; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 534 | |
| 535 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 536 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 537 | "no drivetrain status"); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 538 | }; |
| 539 | |
| 540 | } // namespace joysticks |
| 541 | } // namespace input |
| 542 | } // namespace frc971 |
| 543 | |
| 544 | int main() { |
| 545 | ::aos::Init(); |
| 546 | ::frc971::input::joysticks::Reader reader; |
| 547 | reader.Run(); |
| 548 | ::aos::Cleanup(); |
| 549 | } |