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 | |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame^] | 31 | #define ENABLE_HUMAN 0 |
| 32 | |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 33 | namespace frc971 { |
| 34 | namespace input { |
| 35 | namespace joysticks { |
| 36 | |
| 37 | const ButtonLocation kDriveControlLoopEnable1(1, 7), |
| 38 | kDriveControlLoopEnable2(1, 11); |
| 39 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 40 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 41 | const ButtonLocation kQuickTurn(1, 5); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 42 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 43 | const ButtonLocation kCatch(3, 10); |
| 44 | |
| 45 | const ButtonLocation kFire(3, 9); |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 46 | const ButtonLocation kUnload(2, 11); |
| 47 | const ButtonLocation kReload(2, 6); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 48 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 49 | const ButtonLocation kRollersOut(3, 8); |
| 50 | const ButtonLocation kRollersIn(3, 3); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 51 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 52 | const ButtonLocation kTuck(3, 4); |
| 53 | const ButtonLocation kIntakePosition(3, 5); |
| 54 | const ButtonLocation kIntakeOpenPosition(3, 11); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 55 | const JoystickAxis kFlipRobot(3, 3); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 56 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 57 | const ButtonLocation kLongShot(3, 7); |
| 58 | const ButtonLocation kMediumShot(3, 6); |
| 59 | const ButtonLocation kShortShot(3, 2); |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame^] | 60 | #if ENABLE_HUMAN |
| 61 | // Currently human player shot. |
| 62 | #endif |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 63 | const ButtonLocation kTrussShot(3, 1); |
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 Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame^] | 87 | const ClawGoal kIntakeGoal = {-2.24, kGrabSeparation}; |
| 88 | const ClawGoal kIntakeOpenGoal = {-2.0, 1.1}; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 89 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 90 | // TODO(austin): Tune these by hand... |
| 91 | const ClawGoal kFlippedTuckGoal = {2.733474, -0.75}; |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 92 | const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 93 | const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0}; |
| 94 | |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 95 | // 34" between near edge of colored line and rear edge of bumper |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 96 | const ShotGoal kLongShotGoal = { |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame^] | 97 | {-1.04, kShootSeparation}, 140, 0.04, kIntakePower}; |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 98 | // 3/4" plunger {-1.04, kShootSeparation}, 140, 0.04, kIntakePower}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 99 | const ShotGoal kFlippedLongShotGoal = { |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame^] | 100 | {0.96, kShootSeparation}, 140, 0.09, kIntakePower}; |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 101 | // 3/4 " plunger {0.97, kShootSeparation}, 140, 0.08, kIntakePower}; |
| 102 | |
| 103 | // 78" between near edge of colored line and rear edge of bumper |
| 104 | const ShotGoal kMediumShotGoal = { |
| 105 | {-0.95, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 106 | // 3/4" plunger {-0.90, kShootSeparation}, 105, 0.2, kIntakePower}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 107 | const ShotGoal kFlippedMediumShotGoal = { |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame^] | 108 | {0.905, kShootSeparation}, 120, 0.2, kIntakePower}; |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 109 | // 3/4" plunger {0.80, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 110 | |
| 111 | const ShotGoal kShortShotGoal = { |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame^] | 112 | {-0.670, kShootSeparation}, 77.0, 0.4, kIntakePower}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 113 | const ShotGoal kFlippedShortShotGoal = { |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame^] | 114 | {0.67, kShootSeparation}, 115.0, 0.4, kIntakePower}; |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 115 | |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame^] | 116 | #if ENABLE_HUMAN |
| 117 | const ShotGoal kHumanShotGoal = { |
| 118 | {-0.90, kShootSeparation}, 140, 0.04, kIntakePower}; |
| 119 | #else |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 120 | const ShotGoal kTrussShotGoal = { |
| 121 | {-0.05, kShootSeparation}, 73.0, 0, kIntakePower}; |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame^] | 122 | #endif |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 123 | |
| 124 | // Makes a new ShootAction action. |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 125 | ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>> |
| 126 | MakeCatchAction() { |
| 127 | return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>( |
| 128 | new TypedAction< ::frc971::actions::CatchActionGroup>( |
| 129 | &::frc971::actions::catch_action)); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // A queue which queues Actions and cancels them. |
| 133 | class ActionQueue { |
| 134 | public: |
| 135 | // Queues up an action for sending. |
| 136 | void QueueAction(::std::unique_ptr<Action> action) { |
| 137 | if (current_action_) { |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 138 | LOG(INFO, "Queueing action, canceling prior\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 139 | current_action_->Cancel(); |
| 140 | next_action_ = ::std::move(action); |
| 141 | } else { |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 142 | LOG(INFO, "Queueing action\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 143 | current_action_ = ::std::move(action); |
| 144 | current_action_->Start(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // Cancels the current action, and runs the next one when the current one has |
| 149 | // finished. |
| 150 | void CancelCurrentAction() { |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 151 | LOG(INFO, "Canceling current action\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 152 | if (current_action_) { |
| 153 | current_action_->Cancel(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Cancels all running actions. |
| 158 | void CancelAllActions() { |
Brian Silverman | 101b964 | 2014-03-08 12:45:16 -0800 | [diff] [blame] | 159 | LOG(DEBUG, "Cancelling all actions\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 160 | if (current_action_) { |
| 161 | current_action_->Cancel(); |
| 162 | } |
| 163 | next_action_.reset(); |
| 164 | } |
| 165 | |
| 166 | // Runs the next action when the current one is finished running. |
| 167 | void Tick() { |
| 168 | if (current_action_) { |
| 169 | if (!current_action_->Running()) { |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 170 | LOG(INFO, "Action is done.\n"); |
| 171 | current_action_ = ::std::move(next_action_); |
| 172 | if (current_action_) { |
| 173 | LOG(INFO, "Running next action\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 174 | current_action_->Start(); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Returns true if any action is running or could be running. |
| 181 | // For a one cycle faster response, call Tick before running this. |
| 182 | bool Running() { return (bool)current_action_; } |
| 183 | |
| 184 | private: |
| 185 | ::std::unique_ptr<Action> current_action_; |
| 186 | ::std::unique_ptr<Action> next_action_; |
| 187 | }; |
| 188 | |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 189 | |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 190 | class Reader : public ::aos::input::JoystickInput { |
| 191 | public: |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 192 | Reader() |
| 193 | : is_high_gear_(false), |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 194 | shot_power_(80.0), |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 195 | goal_angle_(0.0), |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 196 | separation_angle_(kGrabSeparation), |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 197 | velocity_compensation_(0.0), |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 198 | intake_power_(0.0), |
| 199 | was_running_(false) {} |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 200 | |
| 201 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 202 | if (data.GetControlBit(ControlBit::kAutonomous)) { |
| 203 | if (data.PosEdge(ControlBit::kEnabled)){ |
| 204 | LOG(INFO, "Starting auto mode\n"); |
| 205 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 206 | .run_auto(true) |
| 207 | .Send(); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 208 | } else if (data.NegEdge(ControlBit::kEnabled)) { |
| 209 | LOG(INFO, "Stopping auto mode\n"); |
| 210 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 211 | .run_auto(false) |
| 212 | .Send(); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 213 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 214 | } else { |
| 215 | HandleTeleop(data); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 216 | } |
| 217 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 218 | |
| 219 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 220 | bool is_control_loop_driving = false; |
| 221 | double left_goal = 0.0; |
| 222 | double right_goal = 0.0; |
| 223 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 224 | const double throttle = -data.GetAxis(kDriveThrottle); |
| 225 | const double kThrottleGain = 1.0 / 2.5; |
| 226 | if (false && (data.IsPressed(kDriveControlLoopEnable1) || |
| 227 | data.IsPressed(kDriveControlLoopEnable2))) { |
| 228 | // TODO(austin): Static sucks! |
| 229 | static double distance = 0.0; |
| 230 | static double angle = 0.0; |
| 231 | static double filtered_goal_distance = 0.0; |
| 232 | if (data.PosEdge(kDriveControlLoopEnable1) || |
| 233 | data.PosEdge(kDriveControlLoopEnable2)) { |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 234 | if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) { |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 235 | distance = (drivetrain.position->left_encoder + |
| 236 | drivetrain.position->right_encoder) / |
| 237 | 2.0 - |
| 238 | throttle * kThrottleGain / 2.0; |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 239 | angle = gyro_reading->angle; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 240 | filtered_goal_distance = distance; |
| 241 | } |
| 242 | } |
| 243 | is_control_loop_driving = true; |
| 244 | |
| 245 | // const double gyro_angle = Gyro.View().angle; |
| 246 | const double goal_theta = angle - wheel * 0.27; |
| 247 | const double goal_distance = distance + throttle * kThrottleGain; |
| 248 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 249 | const double kMaxVelocity = 0.6; |
| 250 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 251 | filtered_goal_distance += kMaxVelocity * 0.02; |
| 252 | } else if (goal_distance < |
| 253 | -kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 254 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 255 | } else { |
| 256 | filtered_goal_distance = goal_distance; |
| 257 | } |
| 258 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 259 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 260 | is_high_gear_ = false; |
| 261 | |
| 262 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 263 | } |
| 264 | if (!drivetrain.goal.MakeWithBuilder() |
| 265 | .steering(wheel) |
| 266 | .throttle(throttle) |
| 267 | .highgear(is_high_gear_) |
| 268 | .quickturn(data.IsPressed(kQuickTurn)) |
| 269 | .control_loop_driving(is_control_loop_driving) |
| 270 | .left_goal(left_goal) |
| 271 | .right_goal(right_goal) |
| 272 | .Send()) { |
| 273 | LOG(WARNING, "sending stick values failed\n"); |
| 274 | } |
| 275 | if (data.PosEdge(kShiftHigh)) { |
| 276 | is_high_gear_ = false; |
| 277 | } |
| 278 | if (data.PosEdge(kShiftLow)) { |
| 279 | is_high_gear_ = true; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void SetGoal(ClawGoal goal) { |
| 284 | goal_angle_ = goal.angle; |
| 285 | separation_angle_ = goal.separation; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 286 | velocity_compensation_ = 0.0; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 287 | intake_power_ = 0.0; |
| 288 | } |
| 289 | |
| 290 | void SetGoal(ShotGoal goal) { |
| 291 | goal_angle_ = goal.claw.angle; |
| 292 | separation_angle_ = goal.claw.separation; |
| 293 | shot_power_ = goal.shot_power; |
| 294 | velocity_compensation_ = goal.velocity_compensation; |
| 295 | intake_power_ = goal.intake_power; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 299 | HandleDrivetrain(data); |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 300 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 301 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 302 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 303 | } |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 304 | if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) { |
| 305 | intake_power_ = 0.0; |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 306 | separation_angle_ = kGrabSeparation; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 307 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 308 | |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 309 | static const double kAdjustClawGoalDeadband = 0.08; |
| 310 | double claw_goal_adjust = data.GetAxis(kAdjustClawGoal); |
| 311 | if (::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) { |
| 312 | claw_goal_adjust = 0; |
| 313 | } else { |
| 314 | claw_goal_adjust = (claw_goal_adjust - |
| 315 | ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband |
| 316 | : kAdjustClawGoalDeadband)) * |
| 317 | 0.035; |
| 318 | } |
| 319 | double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation); |
| 320 | if (::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) { |
| 321 | claw_separation_adjust = 0; |
| 322 | } else { |
| 323 | claw_separation_adjust = |
| 324 | (claw_separation_adjust - |
| 325 | ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband |
| 326 | : kAdjustClawGoalDeadband)) * |
| 327 | -0.035; |
| 328 | } |
| 329 | |
Brian Silverman | 4d1795d | 2014-03-13 15:53:40 -0700 | [diff] [blame] | 330 | if (data.GetAxis(kFlipRobot) > 0.5) { |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 331 | claw_goal_adjust += claw_separation_adjust; |
| 332 | claw_goal_adjust *= -1; |
| 333 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 334 | if (data.IsPressed(kIntakeOpenPosition)) { |
| 335 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 336 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 337 | SetGoal(kFlippedIntakeOpenGoal); |
| 338 | } else if (data.IsPressed(kIntakePosition)) { |
| 339 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 340 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 341 | SetGoal(kFlippedIntakeGoal); |
| 342 | } else if (data.IsPressed(kTuck)) { |
| 343 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 344 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 345 | SetGoal(kFlippedTuckGoal); |
| 346 | } else if (data.PosEdge(kLongShot)) { |
| 347 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 348 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 349 | SetGoal(kFlippedLongShotGoal); |
| 350 | } else if (data.PosEdge(kMediumShot)) { |
| 351 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 352 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 353 | SetGoal(kFlippedMediumShotGoal); |
| 354 | } else if (data.PosEdge(kShortShot)) { |
| 355 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 356 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 357 | SetGoal(kFlippedShortShotGoal); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 358 | } else if (data.PosEdge(kTrussShot)) { |
| 359 | action_queue_.CancelAllActions(); |
| 360 | LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame^] | 361 | #if ENABLE_HUMAN |
| 362 | SetGoal(kHumanShotGoal); |
| 363 | #else |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 364 | SetGoal(kTrussShotGoal); |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame^] | 365 | #endif |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 366 | } |
| 367 | } else { |
| 368 | if (data.IsPressed(kIntakeOpenPosition)) { |
| 369 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 370 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 371 | SetGoal(kIntakeOpenGoal); |
| 372 | } else if (data.IsPressed(kIntakePosition)) { |
| 373 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 374 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 375 | SetGoal(kIntakeGoal); |
| 376 | } else if (data.IsPressed(kTuck)) { |
| 377 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 378 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 379 | SetGoal(kTuckGoal); |
| 380 | } else if (data.PosEdge(kLongShot)) { |
| 381 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 382 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 383 | SetGoal(kLongShotGoal); |
| 384 | } else if (data.PosEdge(kMediumShot)) { |
| 385 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 386 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 387 | SetGoal(kMediumShotGoal); |
| 388 | } else if (data.PosEdge(kShortShot)) { |
| 389 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 390 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 391 | SetGoal(kShortShotGoal); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 392 | } else if (data.PosEdge(kTrussShot)) { |
| 393 | action_queue_.CancelAllActions(); |
| 394 | LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame^] | 395 | #if ENABLE_HUMAN |
| 396 | SetGoal(kHumanShotGoal); |
| 397 | #else |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 398 | SetGoal(kTrussShotGoal); |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame^] | 399 | #endif |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 400 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 403 | /* |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 404 | if (data.PosEdge(kCatch)) { |
| 405 | auto catch_action = MakeCatchAction(); |
| 406 | catch_action->GetGoal()->catch_angle = goal_angle_; |
| 407 | action_queue_.QueueAction(::std::move(catch_action)); |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 408 | } |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 409 | */ |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 410 | |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 411 | if (data.PosEdge(kFire)) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 412 | action_queue_.QueueAction(actions::MakeShootAction()); |
Brian Silverman | 9f863a0 | 2014-03-29 17:42:36 -0700 | [diff] [blame] | 413 | } else if (data.NegEdge(kFire)) { |
| 414 | action_queue_.CancelCurrentAction(); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | action_queue_.Tick(); |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 418 | if (data.IsPressed(kUnload) || data.IsPressed(kReload)) { |
| 419 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 420 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 421 | intake_power_ = 0.0; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 422 | velocity_compensation_ = 0.0; |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 423 | } |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 424 | |
| 425 | // Send out the claw and shooter goals if no actions are running. |
| 426 | if (!action_queue_.Running()) { |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 427 | goal_angle_ += claw_goal_adjust; |
| 428 | separation_angle_ += claw_separation_adjust; |
| 429 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 430 | // If the action just ended, turn the intake off and stop velocity |
| 431 | // compensating. |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 432 | if (was_running_) { |
| 433 | intake_power_ = 0.0; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 434 | velocity_compensation_ = 0.0; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | control_loops::drivetrain.status.FetchLatest(); |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 438 | double goal_angle = goal_angle_; |
| 439 | if (control_loops::drivetrain.status.get()) { |
| 440 | goal_angle += |
| 441 | SpeedToAngleOffset(control_loops::drivetrain.status->robot_speed); |
| 442 | } else { |
| 443 | LOG_INTERVAL(no_drivetrain_status_); |
| 444 | } |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 445 | double separation_angle = separation_angle_; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 446 | |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 447 | if (data.IsPressed(kCatch)) { |
| 448 | const double kCatchSeparation = 1.0; |
| 449 | goal_angle -= kCatchSeparation / 2.0; |
| 450 | separation_angle = kCatchSeparation; |
| 451 | } |
| 452 | |
| 453 | bool intaking = |
| 454 | data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) || |
| 455 | data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 456 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder() |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 457 | .bottom_angle(goal_angle) |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 458 | .separation_angle(separation_angle) |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 459 | .intake(intaking ? 12.0 |
| 460 | : (data.IsPressed(kRollersOut) ? -12.0 |
| 461 | : intake_power_)) |
| 462 | .centering(intaking ? 12.0 : 0.0) |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 463 | .Send()) { |
| 464 | LOG(WARNING, "sending claw goal failed\n"); |
| 465 | } |
| 466 | |
| 467 | if (!control_loops::shooter_queue_group.goal.MakeWithBuilder() |
| 468 | .shot_power(shot_power_) |
| 469 | .shot_requested(data.IsPressed(kFire)) |
| 470 | .unload_requested(data.IsPressed(kUnload)) |
| 471 | .load_requested(data.IsPressed(kReload)) |
| 472 | .Send()) { |
| 473 | LOG(WARNING, "sending shooter goal failed\n"); |
| 474 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 475 | } |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 476 | was_running_ = action_queue_.Running(); |
| 477 | } |
| 478 | |
| 479 | double SpeedToAngleOffset(double speed) { |
| 480 | const frc971::constants::Values &values = frc971::constants::GetValues(); |
| 481 | // scale speed to a [0.0-1.0] on something close to the max |
| 482 | // TODO(austin): Change the scale factor for different shots. |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 483 | return (speed / values.drivetrain_max_speed) * velocity_compensation_; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 486 | private: |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 487 | bool is_high_gear_; |
| 488 | double shot_power_; |
| 489 | double goal_angle_; |
| 490 | double separation_angle_; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 491 | double velocity_compensation_; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 492 | double intake_power_; |
| 493 | bool was_running_; |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 494 | |
| 495 | ActionQueue action_queue_; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 496 | |
| 497 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 498 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 499 | "no drivetrain status"); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 500 | }; |
| 501 | |
| 502 | } // namespace joysticks |
| 503 | } // namespace input |
| 504 | } // namespace frc971 |
| 505 | |
| 506 | int main() { |
| 507 | ::aos::Init(); |
| 508 | ::frc971::input::joysticks::Reader reader; |
| 509 | reader.Run(); |
| 510 | ::aos::Cleanup(); |
| 511 | } |