Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [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" |
| 8 | #include "aos/common/input/driver_station_data.h" |
| 9 | #include "aos/common/logging/logging.h" |
| 10 | #include "aos/common/util/log_interval.h" |
| 11 | #include "aos/common/time.h" |
| 12 | |
| 13 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 14 | #include "frc971/constants.h" |
| 15 | #include "frc971/queues/other_sensors.q.h" |
| 16 | #include "frc971/autonomous/auto.q.h" |
| 17 | #include "frc971/control_loops/claw/claw.q.h" |
| 18 | #include "frc971/control_loops/shooter/shooter.q.h" |
| 19 | #include "frc971/actions/shoot_action.q.h" |
| 20 | #include "frc971/actions/action_client.h" |
| 21 | #include "frc971/actions/catch_action.q.h" |
| 22 | #include "frc971/actions/shoot_action.h" |
| 23 | |
| 24 | using ::frc971::control_loops::drivetrain; |
| 25 | using ::frc971::sensors::gyro_reading; |
| 26 | |
| 27 | using ::aos::input::driver_station::ButtonLocation; |
| 28 | using ::aos::input::driver_station::JoystickAxis; |
| 29 | using ::aos::input::driver_station::ControlBit; |
| 30 | |
| 31 | #define OLD_DS 0 |
| 32 | |
| 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); |
| 42 | |
| 43 | const ButtonLocation kCatch(3, 10); |
| 44 | |
| 45 | #if OLD_DS |
| 46 | const ButtonLocation kFire(3, 11); |
| 47 | const ButtonLocation kUnload(1, 4); |
| 48 | const ButtonLocation kReload(1, 2); |
| 49 | |
| 50 | const ButtonLocation kRollersOut(3, 12); |
| 51 | const ButtonLocation kRollersIn(3, 7); |
| 52 | |
| 53 | const ButtonLocation kTuck(3, 9); |
| 54 | const ButtonLocation kIntakePosition(3, 8); |
| 55 | const ButtonLocation kIntakeOpenPosition(3, 10); |
| 56 | const ButtonLocation kVerticalTuck(3, 1); |
| 57 | const JoystickAxis kFlipRobot(3, 3); |
| 58 | |
| 59 | const ButtonLocation kLongShot(3, 5); |
| 60 | const ButtonLocation kCloseShot(3, 2); |
| 61 | const ButtonLocation kFenderShot(3, 3); |
| 62 | const ButtonLocation kTrussShot(2, 11); |
| 63 | const ButtonLocation kHumanPlayerShot(3, 2); |
| 64 | #else |
| 65 | const ButtonLocation kFire(3, 9); |
| 66 | const ButtonLocation kUnload(1, 4); |
| 67 | const ButtonLocation kReload(1, 2); |
| 68 | |
| 69 | const ButtonLocation kRollersOut(3, 8); |
| 70 | const ButtonLocation kRollersIn(3, 3); |
| 71 | |
| 72 | const ButtonLocation kTuck(3, 4); |
| 73 | const ButtonLocation kIntakePosition(3, 5); |
| 74 | const ButtonLocation kIntakeOpenPosition(3, 11); |
| 75 | const ButtonLocation kVerticalTuck(2, 6); |
| 76 | const JoystickAxis kFlipRobot(3, 3); |
| 77 | |
| 78 | const ButtonLocation kLongShot(3, 7); |
| 79 | const ButtonLocation kCloseShot(3, 6); |
| 80 | const ButtonLocation kFenderShot(3, 2); |
| 81 | const ButtonLocation kTrussShot(2, 11); |
| 82 | const ButtonLocation kHumanPlayerShot(3, 1); |
| 83 | #endif |
| 84 | |
| 85 | const ButtonLocation kUserLeft(2, 7); |
| 86 | const ButtonLocation kUserRight(2, 10); |
| 87 | |
| 88 | const JoystickAxis kAdjustClawGoal(3, 2); |
| 89 | const JoystickAxis kAdjustClawSeparation(3, 1); |
| 90 | |
| 91 | struct ClawGoal { |
| 92 | double angle; |
| 93 | double separation; |
| 94 | }; |
| 95 | |
| 96 | struct ShotGoal { |
| 97 | ClawGoal claw; |
| 98 | double shot_power; |
| 99 | double velocity_compensation; |
| 100 | double intake_power; |
| 101 | }; |
| 102 | |
| 103 | const double kIntakePower = 4.0; |
| 104 | // In case we have to quickly adjust it. |
| 105 | const double kGrabSeparation = 0; |
| 106 | const double kShootSeparation = 0.11 + kGrabSeparation; |
| 107 | |
| 108 | const ClawGoal kTuckGoal = {-2.273474, -0.749484}; |
| 109 | const ClawGoal kVerticalTuckGoal = {0, kGrabSeparation}; |
| 110 | const ClawGoal kIntakeGoal = {-2.24, kGrabSeparation}; |
| 111 | const ClawGoal kIntakeOpenGoal = {-2.0, 1.1}; |
| 112 | |
| 113 | // TODO(austin): Tune these by hand... |
| 114 | const ClawGoal kFlippedTuckGoal = {2.733474, -0.75}; |
| 115 | const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation}; |
| 116 | const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0}; |
| 117 | |
| 118 | // 34" between near edge of colored line and rear edge of bumper. |
| 119 | // Only works running? |
| 120 | const ShotGoal kLongShotGoal = { |
| 121 | {-1.08, kShootSeparation}, 145, 0.04, kIntakePower}; |
| 122 | // old 34" {-1.06, kShootSeparation}, 140, 0.04, kIntakePower}; |
| 123 | const ShotGoal kFlippedLongShotGoal = { |
| 124 | {0.96, kShootSeparation}, 145, 0.09, kIntakePower}; |
| 125 | // old 34" {0.96, kShootSeparation}, 140, 0.09, kIntakePower}; |
| 126 | |
| 127 | // 78" between near edge of colored line and rear edge of bumper. |
| 128 | const ShotGoal kCloseShotGoal = { |
| 129 | {-0.95, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 130 | // 3/4" plunger {-0.90, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 131 | const ShotGoal kFlippedMediumShotGoal = { |
| 132 | {0.865, kShootSeparation}, 120, 0.2, kIntakePower}; |
| 133 | // 3/4" plunger {0.80, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 134 | |
| 135 | // Shot from the fender. |
| 136 | const ShotGoal kFenderShotGoal = { |
| 137 | {-0.68, kShootSeparation}, 115.0, 0.0, kIntakePower}; |
| 138 | const ShotGoal kFlippedShortShotGoal = { |
| 139 | {0.63, kShootSeparation}, 115.0, 0.0, kIntakePower}; |
| 140 | |
| 141 | const ShotGoal kHumanShotGoal = { |
| 142 | {-0.90, kShootSeparation}, 140, 0.04, kIntakePower}; |
| 143 | const ShotGoal kFlippedHumanShotGoal = { |
| 144 | {0.90, kShootSeparation}, 140, 0, kIntakePower}; |
| 145 | const ShotGoal kTrussShotGoal = { |
| 146 | {-0.68, kShootSeparation}, 88.0, 0.4, kIntakePower}; |
| 147 | const ShotGoal kFlippedTrussShotGoal = { |
| 148 | {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower}; |
| 149 | |
| 150 | const ShotGoal kFlippedDemoShotGoal = { |
| 151 | {1.0, kShootSeparation}, 65.0, 0.0, kIntakePower}; |
| 152 | const ShotGoal kDemoShotGoal = { |
| 153 | {-1.0, kShootSeparation}, 50.0, 0.0, kIntakePower}; |
| 154 | |
| 155 | const ClawGoal k254PassGoal = {-1.95, kGrabSeparation}; |
| 156 | const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation}; |
| 157 | |
| 158 | // Makes a new ShootAction action. |
| 159 | ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>> |
| 160 | MakeCatchAction() { |
| 161 | return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>( |
| 162 | new TypedAction< ::frc971::actions::CatchActionGroup>( |
| 163 | &::frc971::actions::catch_action)); |
| 164 | } |
| 165 | |
| 166 | // A queue which queues Actions and cancels them. |
| 167 | class ActionQueue { |
| 168 | public: |
| 169 | // Queues up an action for sending. |
| 170 | void QueueAction(::std::unique_ptr<Action> action) { |
| 171 | if (current_action_) { |
| 172 | LOG(INFO, "Queueing action, canceling prior\n"); |
| 173 | current_action_->Cancel(); |
| 174 | next_action_ = ::std::move(action); |
| 175 | } else { |
| 176 | LOG(INFO, "Queueing action\n"); |
| 177 | current_action_ = ::std::move(action); |
| 178 | current_action_->Start(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Cancels the current action, and runs the next one when the current one has |
| 183 | // finished. |
| 184 | void CancelCurrentAction() { |
| 185 | LOG(INFO, "Canceling current action\n"); |
| 186 | if (current_action_) { |
| 187 | current_action_->Cancel(); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Cancels all running actions. |
| 192 | void CancelAllActions() { |
| 193 | LOG(DEBUG, "Cancelling all actions\n"); |
| 194 | if (current_action_) { |
| 195 | current_action_->Cancel(); |
| 196 | } |
| 197 | next_action_.reset(); |
| 198 | } |
| 199 | |
| 200 | // Runs the next action when the current one is finished running. |
| 201 | void Tick() { |
| 202 | if (current_action_) { |
| 203 | if (!current_action_->Running()) { |
| 204 | LOG(INFO, "Action is done.\n"); |
| 205 | current_action_ = ::std::move(next_action_); |
| 206 | if (current_action_) { |
| 207 | LOG(INFO, "Running next action\n"); |
| 208 | current_action_->Start(); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Returns true if any action is running or could be running. |
| 215 | // For a one cycle faster response, call Tick before running this. |
| 216 | bool Running() { return static_cast<bool>(current_action_); } |
| 217 | |
| 218 | private: |
| 219 | ::std::unique_ptr<Action> current_action_; |
| 220 | ::std::unique_ptr<Action> next_action_; |
| 221 | }; |
| 222 | |
| 223 | |
| 224 | class Reader : public ::aos::input::JoystickInput { |
| 225 | public: |
| 226 | Reader() |
| 227 | : is_high_gear_(false), |
| 228 | shot_power_(80.0), |
| 229 | goal_angle_(0.0), |
| 230 | separation_angle_(kGrabSeparation), |
| 231 | velocity_compensation_(0.0), |
| 232 | intake_power_(0.0), |
| 233 | was_running_(false) {} |
| 234 | |
| 235 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
| 236 | if (data.GetControlBit(ControlBit::kAutonomous)) { |
| 237 | if (data.PosEdge(ControlBit::kEnabled)){ |
| 238 | LOG(INFO, "Starting auto mode\n"); |
| 239 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
| 240 | .run_auto(true) |
| 241 | .Send(); |
| 242 | } else if (data.NegEdge(ControlBit::kEnabled)) { |
| 243 | LOG(INFO, "Stopping auto mode\n"); |
| 244 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
| 245 | .run_auto(false) |
| 246 | .Send(); |
| 247 | } else if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 248 | { |
| 249 | auto goal = drivetrain.goal.MakeMessage(); |
| 250 | goal->Zero(); |
| 251 | goal->control_loop_driving = false; |
| 252 | goal->left_goal = goal->right_goal = 0; |
| 253 | goal->left_velocity_goal = goal->right_velocity_goal = 0; |
| 254 | if (!goal.Send()) { |
| 255 | LOG(WARNING, "sending 0 drivetrain goal failed\n"); |
| 256 | } |
| 257 | } |
| 258 | { |
| 259 | // TODO(brians): Make sure this doesn't make it unbrake and not move |
| 260 | // or something weird. |
| 261 | auto goal = control_loops::shooter_queue_group.goal.MakeMessage(); |
| 262 | goal->Zero(); |
| 263 | if (!goal.Send()) { |
| 264 | LOG(WARNING, "sending 0 shooter goal failed\n"); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } else { |
| 269 | HandleTeleop(data); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 274 | bool is_control_loop_driving = false; |
| 275 | double left_goal = 0.0; |
| 276 | double right_goal = 0.0; |
| 277 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 278 | const double throttle = -data.GetAxis(kDriveThrottle); |
| 279 | const double kThrottleGain = 1.0 / 2.5; |
| 280 | if (false && (data.IsPressed(kDriveControlLoopEnable1) || |
| 281 | data.IsPressed(kDriveControlLoopEnable2))) { |
| 282 | // TODO(austin): Static sucks! |
| 283 | static double distance = 0.0; |
| 284 | static double angle = 0.0; |
| 285 | static double filtered_goal_distance = 0.0; |
| 286 | if (data.PosEdge(kDriveControlLoopEnable1) || |
| 287 | data.PosEdge(kDriveControlLoopEnable2)) { |
| 288 | if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) { |
| 289 | distance = (drivetrain.position->left_encoder + |
| 290 | drivetrain.position->right_encoder) / |
| 291 | 2.0 - |
| 292 | throttle * kThrottleGain / 2.0; |
| 293 | angle = gyro_reading->angle; |
| 294 | filtered_goal_distance = distance; |
| 295 | } |
| 296 | } |
| 297 | is_control_loop_driving = true; |
| 298 | |
| 299 | // const double gyro_angle = Gyro.View().angle; |
| 300 | const double goal_theta = angle - wheel * 0.27; |
| 301 | const double goal_distance = distance + throttle * kThrottleGain; |
| 302 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 303 | const double kMaxVelocity = 0.6; |
| 304 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 305 | filtered_goal_distance += kMaxVelocity * 0.02; |
| 306 | } else if (goal_distance < |
| 307 | -kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 308 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 309 | } else { |
| 310 | filtered_goal_distance = goal_distance; |
| 311 | } |
| 312 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 313 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 314 | is_high_gear_ = false; |
| 315 | |
| 316 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 317 | } |
| 318 | if (!drivetrain.goal.MakeWithBuilder() |
| 319 | .steering(wheel) |
| 320 | .throttle(throttle) |
| 321 | .highgear(is_high_gear_) |
| 322 | .quickturn(data.IsPressed(kQuickTurn)) |
| 323 | .control_loop_driving(is_control_loop_driving) |
| 324 | .left_goal(left_goal) |
| 325 | .right_goal(right_goal) |
| 326 | .left_velocity_goal(0) |
| 327 | .right_velocity_goal(0) |
| 328 | .Send()) { |
| 329 | LOG(WARNING, "sending stick values failed\n"); |
| 330 | } |
| 331 | if (data.PosEdge(kShiftHigh)) { |
| 332 | is_high_gear_ = false; |
| 333 | } |
| 334 | if (data.PosEdge(kShiftLow)) { |
| 335 | is_high_gear_ = true; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | void SetGoal(ClawGoal goal) { |
| 340 | goal_angle_ = goal.angle; |
| 341 | separation_angle_ = goal.separation; |
| 342 | moving_for_shot_ = false; |
| 343 | velocity_compensation_ = 0.0; |
| 344 | intake_power_ = 0.0; |
| 345 | } |
| 346 | |
| 347 | void SetGoal(ShotGoal goal) { |
| 348 | goal_angle_ = goal.claw.angle; |
| 349 | shot_separation_angle_ = goal.claw.separation; |
| 350 | separation_angle_ = kGrabSeparation; |
| 351 | moving_for_shot_ = true; |
| 352 | shot_power_ = goal.shot_power; |
| 353 | velocity_compensation_ = goal.velocity_compensation; |
| 354 | intake_power_ = goal.intake_power; |
| 355 | } |
| 356 | |
| 357 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 358 | HandleDrivetrain(data); |
| 359 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 360 | action_queue_.CancelAllActions(); |
| 361 | LOG(DEBUG, "Canceling\n"); |
| 362 | } |
| 363 | if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) { |
| 364 | intake_power_ = 0.0; |
| 365 | separation_angle_ = kGrabSeparation; |
| 366 | moving_for_shot_ = false; |
| 367 | } |
| 368 | |
| 369 | static const double kAdjustClawGoalDeadband = 0.08; |
| 370 | double claw_goal_adjust = data.GetAxis(kAdjustClawGoal); |
| 371 | if (OLD_DS || ::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) { |
| 372 | claw_goal_adjust = 0; |
| 373 | } else { |
| 374 | claw_goal_adjust = (claw_goal_adjust - |
| 375 | ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband |
| 376 | : kAdjustClawGoalDeadband)) * |
| 377 | 0.035; |
| 378 | } |
| 379 | double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation); |
| 380 | if (OLD_DS || |
| 381 | ::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) { |
| 382 | claw_separation_adjust = 0; |
| 383 | } else { |
| 384 | claw_separation_adjust = |
| 385 | (claw_separation_adjust - |
| 386 | ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband |
| 387 | : kAdjustClawGoalDeadband)) * |
| 388 | -0.035; |
| 389 | } |
| 390 | |
| 391 | #if OLD_DS |
| 392 | if (data.IsPressed(kFenderShot)) { |
| 393 | #else |
| 394 | if (data.GetAxis(kFlipRobot) > 0.9) { |
| 395 | #endif |
| 396 | claw_goal_adjust += claw_separation_adjust; |
| 397 | claw_goal_adjust *= -1; |
| 398 | |
| 399 | if (data.IsPressed(kIntakeOpenPosition)) { |
| 400 | action_queue_.CancelAllActions(); |
| 401 | LOG(DEBUG, "Canceling\n"); |
| 402 | SetGoal(kFlippedIntakeOpenGoal); |
| 403 | } else if (data.IsPressed(kIntakePosition)) { |
| 404 | action_queue_.CancelAllActions(); |
| 405 | LOG(DEBUG, "Canceling\n"); |
| 406 | SetGoal(kFlippedIntakeGoal); |
| 407 | } else if (data.IsPressed(kVerticalTuck)) { |
| 408 | action_queue_.CancelAllActions(); |
| 409 | LOG(DEBUG, "Canceling\n"); |
| 410 | SetGoal(kVerticalTuckGoal); |
| 411 | } else if (data.IsPressed(kTuck)) { |
| 412 | action_queue_.CancelAllActions(); |
| 413 | LOG(DEBUG, "Canceling\n"); |
| 414 | SetGoal(kFlippedTuckGoal); |
| 415 | } else if (data.PosEdge(kLongShot)) { |
| 416 | action_queue_.CancelAllActions(); |
| 417 | LOG(DEBUG, "Canceling\n"); |
| 418 | SetGoal(kFlippedLongShotGoal); |
| 419 | } else if (data.PosEdge(kCloseShot)) { |
| 420 | action_queue_.CancelAllActions(); |
| 421 | LOG(DEBUG, "Canceling\n"); |
| 422 | SetGoal(kFlippedMediumShotGoal); |
| 423 | } else if (data.PosEdge(kFenderShot)) { |
| 424 | action_queue_.CancelAllActions(); |
| 425 | LOG(DEBUG, "Canceling\n"); |
| 426 | SetGoal(kFlippedShortShotGoal); |
| 427 | } else if (data.PosEdge(kHumanPlayerShot)) { |
| 428 | action_queue_.CancelAllActions(); |
| 429 | LOG(DEBUG, "Canceling\n"); |
| 430 | SetGoal(kFlippedHumanShotGoal); |
| 431 | } else if (data.PosEdge(kUserLeft)) { |
| 432 | action_queue_.CancelAllActions(); |
| 433 | LOG(DEBUG, "Canceling\n"); |
| 434 | SetGoal(kFlipped254PassGoal); |
| 435 | } else if (data.PosEdge(kUserRight)) { |
| 436 | action_queue_.CancelAllActions(); |
| 437 | LOG(DEBUG, "Canceling\n"); |
| 438 | SetGoal(kFlippedDemoShotGoal); |
| 439 | } else if (data.PosEdge(kTrussShot)) { |
| 440 | action_queue_.CancelAllActions(); |
| 441 | LOG(DEBUG, "Canceling\n"); |
| 442 | SetGoal(kFlippedTrussShotGoal); |
| 443 | } |
| 444 | } else { |
| 445 | if (data.IsPressed(kIntakeOpenPosition)) { |
| 446 | action_queue_.CancelAllActions(); |
| 447 | LOG(DEBUG, "Canceling\n"); |
| 448 | SetGoal(kIntakeOpenGoal); |
| 449 | } else if (data.IsPressed(kIntakePosition)) { |
| 450 | action_queue_.CancelAllActions(); |
| 451 | LOG(DEBUG, "Canceling\n"); |
| 452 | SetGoal(kIntakeGoal); |
| 453 | } else if (data.IsPressed(kVerticalTuck)) { |
| 454 | action_queue_.CancelAllActions(); |
| 455 | LOG(DEBUG, "Canceling\n"); |
| 456 | SetGoal(kVerticalTuckGoal); |
| 457 | } else if (data.IsPressed(kTuck)) { |
| 458 | action_queue_.CancelAllActions(); |
| 459 | LOG(DEBUG, "Canceling\n"); |
| 460 | SetGoal(kTuckGoal); |
| 461 | } else if (data.PosEdge(kLongShot)) { |
| 462 | action_queue_.CancelAllActions(); |
| 463 | LOG(DEBUG, "Canceling\n"); |
| 464 | SetGoal(kLongShotGoal); |
| 465 | } else if (data.PosEdge(kCloseShot)) { |
| 466 | action_queue_.CancelAllActions(); |
| 467 | LOG(DEBUG, "Canceling\n"); |
| 468 | SetGoal(kCloseShotGoal); |
| 469 | } else if (data.PosEdge(kFenderShot)) { |
| 470 | action_queue_.CancelAllActions(); |
| 471 | LOG(DEBUG, "Canceling\n"); |
| 472 | SetGoal(kFenderShotGoal); |
| 473 | } else if (data.PosEdge(kHumanPlayerShot)) { |
| 474 | action_queue_.CancelAllActions(); |
| 475 | LOG(DEBUG, "Canceling\n"); |
| 476 | SetGoal(kHumanShotGoal); |
| 477 | } else if (data.PosEdge(kUserLeft)) { |
| 478 | action_queue_.CancelAllActions(); |
| 479 | LOG(DEBUG, "Canceling\n"); |
| 480 | SetGoal(k254PassGoal); |
| 481 | } else if (data.PosEdge(kUserRight)) { |
| 482 | action_queue_.CancelAllActions(); |
| 483 | LOG(DEBUG, "Canceling\n"); |
| 484 | SetGoal(kDemoShotGoal); |
| 485 | } else if (data.PosEdge(kTrussShot)) { |
| 486 | action_queue_.CancelAllActions(); |
| 487 | LOG(DEBUG, "Canceling\n"); |
| 488 | SetGoal(kTrussShotGoal); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | if (data.PosEdge(kCatch)) { |
| 494 | auto catch_action = MakeCatchAction(); |
| 495 | catch_action->GetGoal()->catch_angle = goal_angle_; |
| 496 | action_queue_.QueueAction(::std::move(catch_action)); |
| 497 | } |
| 498 | */ |
| 499 | |
| 500 | if (data.PosEdge(kFire)) { |
| 501 | action_queue_.QueueAction(actions::MakeShootAction()); |
| 502 | } else if (data.NegEdge(kFire)) { |
| 503 | action_queue_.CancelCurrentAction(); |
| 504 | } |
| 505 | |
| 506 | action_queue_.Tick(); |
| 507 | if (data.IsPressed(kUnload) || data.IsPressed(kReload)) { |
| 508 | action_queue_.CancelAllActions(); |
| 509 | LOG(DEBUG, "Canceling\n"); |
| 510 | intake_power_ = 0.0; |
| 511 | velocity_compensation_ = 0.0; |
| 512 | } |
| 513 | |
| 514 | // Send out the claw and shooter goals if no actions are running. |
| 515 | if (!action_queue_.Running()) { |
| 516 | goal_angle_ += claw_goal_adjust; |
| 517 | separation_angle_ += claw_separation_adjust; |
| 518 | |
| 519 | // If the action just ended, turn the intake off and stop velocity |
| 520 | // compensating. |
| 521 | if (was_running_) { |
| 522 | intake_power_ = 0.0; |
| 523 | velocity_compensation_ = 0.0; |
| 524 | } |
| 525 | |
| 526 | control_loops::drivetrain.status.FetchLatest(); |
| 527 | double goal_angle = goal_angle_; |
| 528 | if (control_loops::drivetrain.status.get()) { |
| 529 | goal_angle += |
| 530 | SpeedToAngleOffset(control_loops::drivetrain.status->robot_speed); |
| 531 | } else { |
| 532 | LOG_INTERVAL(no_drivetrain_status_); |
| 533 | } |
| 534 | |
| 535 | if (moving_for_shot_) { |
| 536 | auto &claw_status = control_loops::claw_queue_group.status; |
| 537 | claw_status.FetchLatest(); |
| 538 | if (claw_status.get()) { |
| 539 | if (::std::abs(claw_status->bottom - goal_angle) < 0.2) { |
| 540 | moving_for_shot_ = false; |
| 541 | separation_angle_ = shot_separation_angle_; |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | double separation_angle = separation_angle_; |
| 547 | |
| 548 | if (data.IsPressed(kCatch)) { |
| 549 | const double kCatchSeparation = 1.0; |
| 550 | goal_angle -= kCatchSeparation / 2.0; |
| 551 | separation_angle = kCatchSeparation; |
| 552 | } |
| 553 | |
| 554 | bool intaking = |
| 555 | data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) || |
| 556 | data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch); |
| 557 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder() |
| 558 | .bottom_angle(goal_angle) |
| 559 | .separation_angle(separation_angle) |
| 560 | .intake(intaking ? 12.0 |
| 561 | : (data.IsPressed(kRollersOut) ? -12.0 |
| 562 | : intake_power_)) |
| 563 | .centering(intaking ? 12.0 : 0.0) |
| 564 | .Send()) { |
| 565 | LOG(WARNING, "sending claw goal failed\n"); |
| 566 | } |
| 567 | |
| 568 | if (!control_loops::shooter_queue_group.goal.MakeWithBuilder() |
| 569 | .shot_power(shot_power_) |
| 570 | .shot_requested(data.IsPressed(kFire)) |
| 571 | .unload_requested(data.IsPressed(kUnload)) |
| 572 | .load_requested(data.IsPressed(kReload)) |
| 573 | .Send()) { |
| 574 | LOG(WARNING, "sending shooter goal failed\n"); |
| 575 | } |
| 576 | } |
| 577 | was_running_ = action_queue_.Running(); |
| 578 | } |
| 579 | |
| 580 | double SpeedToAngleOffset(double speed) { |
| 581 | const frc971::constants::Values &values = frc971::constants::GetValues(); |
| 582 | // scale speed to a [0.0-1.0] on something close to the max |
| 583 | // TODO(austin): Change the scale factor for different shots. |
| 584 | return (speed / values.drivetrain_max_speed) * velocity_compensation_; |
| 585 | } |
| 586 | |
| 587 | private: |
| 588 | bool is_high_gear_; |
| 589 | double shot_power_; |
| 590 | double goal_angle_; |
| 591 | double separation_angle_, shot_separation_angle_; |
| 592 | double velocity_compensation_; |
| 593 | double intake_power_; |
| 594 | bool was_running_; |
| 595 | bool moving_for_shot_ = false; |
| 596 | |
| 597 | ActionQueue action_queue_; |
| 598 | |
| 599 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 600 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 601 | "no drivetrain status"); |
| 602 | }; |
| 603 | |
| 604 | } // namespace joysticks |
| 605 | } // namespace input |
| 606 | } // namespace frc971 |
| 607 | |
| 608 | int main() { |
| 609 | ::aos::Init(); |
| 610 | ::frc971::input::joysticks::Reader reader; |
| 611 | reader.Run(); |
| 612 | ::aos::Cleanup(); |
| 613 | } |