James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 1 | #include <functional> |
| 2 | |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 3 | #include "aos/common/logging/logging.h" |
| 4 | |
| 5 | #include "frc971/actions/catch_action.h" |
| 6 | #include "frc971/control_loops/claw/claw.q.h" |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 7 | #include "frc971/queues/other_sensors.q.h" |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 8 | |
| 9 | namespace frc971 { |
| 10 | namespace actions { |
| 11 | |
| 12 | CatchAction::CatchAction(actions::CatchActionGroup* s) |
| 13 | : actions::ActionBase<actions::CatchActionGroup>(s) {} |
| 14 | |
| 15 | void CatchAction::RunAction() { |
Austin Schuh | a719bf1 | 2014-03-09 00:45:02 -0800 | [diff] [blame] | 16 | control_loops::claw_queue_group.goal.FetchLatest(); |
| 17 | if (control_loops::claw_queue_group.goal.get() == nullptr) { |
| 18 | LOG(WARNING, "no claw goal\n"); |
| 19 | return; |
| 20 | } |
| 21 | |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 22 | // Set claw angle. |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 23 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder() |
Austin Schuh | a719bf1 | 2014-03-09 00:45:02 -0800 | [diff] [blame] | 24 | .bottom_angle(action_q_->goal->catch_angle - kCatchSeparation / 2.0) |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 25 | .separation_angle(kCatchSeparation) |
| 26 | .intake(kCatchIntake) |
| 27 | .centering(kCatchCentering) |
| 28 | .Send()) { |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 29 | LOG(WARNING, "sending claw goal failed\n"); |
| 30 | return; |
| 31 | } |
Austin Schuh | a719bf1 | 2014-03-09 00:45:02 -0800 | [diff] [blame] | 32 | |
| 33 | control_loops::claw_queue_group.goal.FetchLatest(); |
| 34 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 35 | LOG(INFO, "Waiting for the claw to be ready\n"); |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 36 | |
| 37 | // wait for claw to be ready |
| 38 | if (WaitUntil(::std::bind(&CatchAction::DoneSetupCatch, this))) return; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 39 | LOG(INFO, "Waiting for the sonar\n"); |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 40 | |
Austin Schuh | a719bf1 | 2014-03-09 00:45:02 -0800 | [diff] [blame] | 41 | close_count_ = 0; |
| 42 | |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 43 | // wait for the sonar to trigger |
| 44 | if (WaitUntil(::std::bind(&CatchAction::DoneFoundSonar, this))) return; |
| 45 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 46 | LOG(INFO, "Closing the claw\n"); |
| 47 | |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 48 | // close the claw |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 49 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder() |
Austin Schuh | a719bf1 | 2014-03-09 00:45:02 -0800 | [diff] [blame] | 50 | .bottom_angle(action_q_->goal->catch_angle) |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 51 | .separation_angle(0.0) |
| 52 | .intake(kCatchIntake) |
| 53 | .centering(kCatchCentering) |
| 54 | .Send()) { |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 55 | LOG(WARNING, "sending claw goal failed\n"); |
| 56 | return; |
| 57 | } |
| 58 | |
Austin Schuh | a719bf1 | 2014-03-09 00:45:02 -0800 | [diff] [blame] | 59 | control_loops::claw_queue_group.goal.FetchLatest(); |
| 60 | |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 61 | // claw now closed |
| 62 | if (WaitUntil(::std::bind(&CatchAction::DoneClawWithBall, this))) return; |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 63 | |
Austin Schuh | a719bf1 | 2014-03-09 00:45:02 -0800 | [diff] [blame] | 64 | for (int i = 0; i < 5; ++i) { |
| 65 | aos::time::SleepFor(aos::time::Time::InSeconds(0.05)); |
| 66 | if (ShouldCancel()) return; |
| 67 | } |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 71 | /*bool CatchAction::DoneBallIn() { |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 72 | if (!sensors::othersensors.FetchLatest()) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 73 | sensors::othersensors.FetchNextBlocking(); |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 74 | } |
| 75 | if (sensors::othersensors->travis_hall_effect_distance > 0.005) { |
| 76 | LOG(INFO, "Ball in at %.2f.\n", |
| 77 | sensors::othersensors->travis_hall_effect_distance); |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 78 | return true; |
| 79 | } |
| 80 | return false; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 81 | }*/ |
| 82 | |
| 83 | bool CatchAction::DoneClawWithBall() { |
| 84 | if (!control_loops::claw_queue_group.status.FetchLatest()) { |
| 85 | control_loops::claw_queue_group.status.FetchNextBlocking(); |
| 86 | } |
| 87 | |
| 88 | bool ans = |
| 89 | control_loops::claw_queue_group.status->zeroed && |
| 90 | (::std::abs(control_loops::claw_queue_group.status->bottom_velocity) < |
Austin Schuh | a719bf1 | 2014-03-09 00:45:02 -0800 | [diff] [blame] | 91 | 1.0) && |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 92 | (::std::abs(control_loops::claw_queue_group.status->bottom - |
| 93 | control_loops::claw_queue_group.goal->bottom_angle) < 0.10) && |
| 94 | (::std::abs(control_loops::claw_queue_group.status->separation - |
| 95 | control_loops::claw_queue_group.goal->separation_angle) < |
| 96 | 0.4); |
| 97 | |
| 98 | if (!ans) { |
| 99 | LOG(INFO, |
| 100 | "Claw is ready %d zeroed %d bottom_velocity %f bottom %f sep %f\n", ans, |
| 101 | control_loops::claw_queue_group.status->zeroed, |
| 102 | ::std::abs(control_loops::claw_queue_group.status->bottom_velocity), |
| 103 | ::std::abs(control_loops::claw_queue_group.status->bottom - |
| 104 | control_loops::claw_queue_group.goal->bottom_angle), |
| 105 | ::std::abs(control_loops::claw_queue_group.status->separation - |
| 106 | control_loops::claw_queue_group.goal->separation_angle)); |
| 107 | } |
| 108 | return ans; |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool CatchAction::DoneFoundSonar() { |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 112 | if (!sensors::other_sensors.FetchLatest()) { |
| 113 | sensors::other_sensors.FetchNextBlocking(); |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 114 | } |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 115 | LOG(DEBUG, "Sonar at %.2f.\n", sensors::other_sensors->sonar_distance); |
Austin Schuh | a719bf1 | 2014-03-09 00:45:02 -0800 | [diff] [blame] | 116 | if (sensors::other_sensors->sonar_distance > 0.1 && |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 117 | sensors::other_sensors->sonar_distance < kSonarTriggerDist) { |
Austin Schuh | a719bf1 | 2014-03-09 00:45:02 -0800 | [diff] [blame] | 118 | ++close_count_; |
| 119 | } else { |
| 120 | close_count_ = 0; |
| 121 | } |
| 122 | if (close_count_ > 50) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 123 | return true; |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | bool CatchAction::DoneSetupCatch() { |
| 129 | if (!control_loops::claw_queue_group.status.FetchLatest()) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 130 | control_loops::claw_queue_group.status.FetchNextBlocking(); |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 131 | } |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 132 | |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 133 | // Make sure that the shooter and claw has reached the necessary state. |
| 134 | // Check the current positions of the various mechanisms to make sure that we |
| 135 | // avoid race conditions where we send it a new goal but it still thinks that |
| 136 | // it has the old goal and thinks that it is already done. |
| 137 | bool claw_angle_correct = |
| 138 | ::std::abs(control_loops::claw_queue_group.status->bottom - |
Austin Schuh | a719bf1 | 2014-03-09 00:45:02 -0800 | [diff] [blame] | 139 | control_loops::claw_queue_group.goal->bottom_angle) < 0.15; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 140 | bool open_enough = |
| 141 | control_loops::claw_queue_group.status->separation > kCatchMinSeparation; |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 142 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 143 | if (claw_angle_correct && open_enough) { |
James Kuszmaul | c0568f2 | 2014-03-05 20:33:00 -0800 | [diff] [blame] | 144 | LOG(INFO, "Claw ready for catching.\n"); |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | } // namespace actions |
| 152 | } // namespace frc971 |
| 153 | |