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