James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 1 | #include <functional> |
| 2 | |
| 3 | #include "aos/common/control_loop/Timing.h" |
| 4 | #include "aos/common/logging/logging.h" |
| 5 | |
| 6 | #include "frc971/actions/catch_action.h" |
| 7 | #include "frc971/control_loops/claw/claw.q.h" |
| 8 | #include "frc971/queues/othersensors.q.h" |
| 9 | |
| 10 | namespace frc971 { |
| 11 | namespace actions { |
| 12 | |
| 13 | CatchAction::CatchAction(actions::CatchActionGroup* s) |
| 14 | : actions::ActionBase<actions::CatchActionGroup>(s) {} |
| 15 | |
| 16 | void CatchAction::RunAction() { |
| 17 | |
| 18 | // Set claw angle. |
| 19 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder().bottom_angle( |
| 20 | catch_action.goal->catch_angle) |
| 21 | .separation_angle(kCatchSeparation).intake(kCatchIntake) |
| 22 | .centering(kCatchCentering).Send()) { |
| 23 | LOG(WARNING, "sending claw goal failed\n"); |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | // wait for claw to be ready |
| 28 | if (WaitUntil(::std::bind(&CatchAction::DoneSetupCatch, this))) return; |
| 29 | |
| 30 | // Set claw angle. |
| 31 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder().bottom_angle( |
| 32 | catch_action.goal->catch_angle).separation_angle(kCatchSeparation) |
| 33 | .intake(kCatchIntake).centering(kCatchCentering).Send()) { |
| 34 | LOG(WARNING, "sending claw goal failed\n"); |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | // wait for the sonar to trigger |
| 39 | if (WaitUntil(::std::bind(&CatchAction::DoneFoundSonar, this))) return; |
| 40 | |
| 41 | // close the claw |
| 42 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder().bottom_angle( |
| 43 | kFinishAngle).separation_angle(0.0).intake(kCatchIntake) |
| 44 | .centering(kCatchCentering).Send()) { |
| 45 | LOG(WARNING, "sending claw goal failed\n"); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // claw now closed |
| 50 | if (WaitUntil(::std::bind(&CatchAction::DoneClawWithBall, this))) return; |
| 51 | // ball is fully in |
| 52 | if (WaitUntil(::std::bind(&CatchAction::DoneBallIn, this))) return; |
| 53 | |
| 54 | // head to a finshed pose |
| 55 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder().bottom_angle( |
| 56 | kFinishAngle) |
James Kuszmaul | 9d9b798 | 2014-03-05 21:15:13 -0800 | [diff] [blame] | 57 | .separation_angle(0.0).intake(0.0).centering(0.0).Send()) { |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 58 | LOG(WARNING, "sending claw goal failed\n"); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | // thats it |
| 63 | if (WaitUntil(::std::bind(&CatchAction::DoneClawWithBall, this))) return; |
| 64 | |
| 65 | // done with action |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | bool CatchAction::DoneBallIn() { |
| 71 | if (!sensors::othersensors.FetchLatest()) { |
| 72 | sensors::othersensors.FetchNextBlocking(); |
| 73 | } |
| 74 | if (sensors::othersensors->travis_hall_effect_distance > 0.005) { |
| 75 | LOG(INFO, "Ball in at %.2f.\n", |
| 76 | sensors::othersensors->travis_hall_effect_distance); |
| 77 | return true; |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | bool CatchAction::DoneClawWithBall() { |
| 83 | if (!control_loops::claw_queue_group.status.FetchLatest()) { |
| 84 | control_loops::claw_queue_group.status.FetchNextBlocking(); |
| 85 | } |
| 86 | // Make sure that both the shooter and claw have reached the necessary |
| 87 | // states. |
| 88 | if (control_loops::claw_queue_group.status->done_with_ball) { |
| 89 | LOG(INFO, "Claw at goal.\n"); |
| 90 | return true; |
| 91 | } |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | bool CatchAction::DoneFoundSonar() { |
| 96 | if (!sensors::othersensors.FetchLatest()) { |
| 97 | sensors::othersensors.FetchNextBlocking(); |
| 98 | } |
| 99 | if (sensors::othersensors->sonar_distance > 0.3 && |
| 100 | sensors::othersensors->sonar_distance < kSonarTriggerDist) { |
| 101 | LOG(INFO, "Hit Sonar at %.2f.\n", sensors::othersensors->sonar_distance); |
| 102 | return true; |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | bool CatchAction::DoneSetupCatch() { |
| 108 | if (!control_loops::claw_queue_group.status.FetchLatest()) { |
| 109 | control_loops::claw_queue_group.status.FetchNextBlocking(); |
| 110 | } |
| 111 | if (!control_loops::claw_queue_group.goal.FetchLatest()) { |
| 112 | LOG(ERROR, "Failed to fetch claw goal.\n"); |
| 113 | } |
| 114 | // Make sure that the shooter and claw has reached the necessary state. |
| 115 | // Check the current positions of the various mechanisms to make sure that we |
| 116 | // avoid race conditions where we send it a new goal but it still thinks that |
| 117 | // it has the old goal and thinks that it is already done. |
| 118 | bool claw_angle_correct = |
| 119 | ::std::abs(control_loops::claw_queue_group.status->bottom - |
| 120 | control_loops::claw_queue_group.goal->bottom_angle) < |
| 121 | 0.005; |
| 122 | |
| 123 | if (control_loops::claw_queue_group.status->done && claw_angle_correct) { |
| 124 | LOG(INFO, "Claw ready for catching.\n"); |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | } // namespace actions |
| 132 | } // namespace frc971 |
| 133 | |