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() { |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 17 | // Set claw angle. |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 18 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder() |
| 19 | .bottom_angle(action_q_->goal->catch_angle) |
| 20 | .separation_angle(kCatchSeparation) |
| 21 | .intake(kCatchIntake) |
| 22 | .centering(kCatchCentering) |
| 23 | .Send()) { |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 24 | LOG(WARNING, "sending claw goal failed\n"); |
| 25 | return; |
| 26 | } |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 27 | LOG(INFO, "Waiting for the claw to be ready\n"); |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 28 | |
| 29 | // wait for claw to be ready |
| 30 | if (WaitUntil(::std::bind(&CatchAction::DoneSetupCatch, this))) return; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 31 | LOG(INFO, "Waiting for the sonar\n"); |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 32 | |
| 33 | // wait for the sonar to trigger |
| 34 | if (WaitUntil(::std::bind(&CatchAction::DoneFoundSonar, this))) return; |
| 35 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 36 | LOG(INFO, "Closing the claw\n"); |
| 37 | |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 38 | // close the claw |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 39 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder() |
| 40 | .bottom_angle(action_q_->goal->catch_angle + kCatchSeparation / 2.0) |
| 41 | .separation_angle(0.0) |
| 42 | .intake(kCatchIntake) |
| 43 | .centering(kCatchCentering) |
| 44 | .Send()) { |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 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; |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 51 | |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 52 | return; |
| 53 | } |
| 54 | |
| 55 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 56 | /*bool CatchAction::DoneBallIn() { |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 57 | if (!sensors::othersensors.FetchLatest()) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 58 | sensors::othersensors.FetchNextBlocking(); |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 59 | } |
| 60 | if (sensors::othersensors->travis_hall_effect_distance > 0.005) { |
| 61 | LOG(INFO, "Ball in at %.2f.\n", |
| 62 | sensors::othersensors->travis_hall_effect_distance); |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 63 | return true; |
| 64 | } |
| 65 | return false; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 66 | }*/ |
| 67 | |
| 68 | bool CatchAction::DoneClawWithBall() { |
| 69 | if (!control_loops::claw_queue_group.status.FetchLatest()) { |
| 70 | control_loops::claw_queue_group.status.FetchNextBlocking(); |
| 71 | } |
| 72 | |
| 73 | bool ans = |
| 74 | control_loops::claw_queue_group.status->zeroed && |
| 75 | (::std::abs(control_loops::claw_queue_group.status->bottom_velocity) < |
| 76 | 0.5) && |
| 77 | (::std::abs(control_loops::claw_queue_group.status->bottom - |
| 78 | control_loops::claw_queue_group.goal->bottom_angle) < 0.10) && |
| 79 | (::std::abs(control_loops::claw_queue_group.status->separation - |
| 80 | control_loops::claw_queue_group.goal->separation_angle) < |
| 81 | 0.4); |
| 82 | |
| 83 | if (!ans) { |
| 84 | LOG(INFO, |
| 85 | "Claw is ready %d zeroed %d bottom_velocity %f bottom %f sep %f\n", ans, |
| 86 | control_loops::claw_queue_group.status->zeroed, |
| 87 | ::std::abs(control_loops::claw_queue_group.status->bottom_velocity), |
| 88 | ::std::abs(control_loops::claw_queue_group.status->bottom - |
| 89 | control_loops::claw_queue_group.goal->bottom_angle), |
| 90 | ::std::abs(control_loops::claw_queue_group.status->separation - |
| 91 | control_loops::claw_queue_group.goal->separation_angle)); |
| 92 | } |
| 93 | return ans; |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | bool CatchAction::DoneFoundSonar() { |
| 97 | if (!sensors::othersensors.FetchLatest()) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 98 | sensors::othersensors.FetchNextBlocking(); |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 99 | } |
Brian Silverman | 101b964 | 2014-03-08 12:45:16 -0800 | [diff] [blame^] | 100 | LOG(DEBUG, "Sonar at %.2f.\n", sensors::othersensors->sonar_distance); |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 101 | if (sensors::othersensors->sonar_distance > 0.3 && |
| 102 | sensors::othersensors->sonar_distance < kSonarTriggerDist) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 103 | return true; |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | bool CatchAction::DoneSetupCatch() { |
| 109 | if (!control_loops::claw_queue_group.status.FetchLatest()) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 110 | control_loops::claw_queue_group.status.FetchNextBlocking(); |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 111 | } |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 112 | |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 113 | // Make sure that the shooter and claw has reached the necessary state. |
| 114 | // Check the current positions of the various mechanisms to make sure that we |
| 115 | // avoid race conditions where we send it a new goal but it still thinks that |
| 116 | // it has the old goal and thinks that it is already done. |
| 117 | bool claw_angle_correct = |
| 118 | ::std::abs(control_loops::claw_queue_group.status->bottom - |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 119 | action_q_->goal->catch_angle) < 0.15; |
| 120 | bool open_enough = |
| 121 | control_loops::claw_queue_group.status->separation > kCatchMinSeparation; |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 122 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 123 | if (claw_angle_correct && open_enough) { |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 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 | |