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