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