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