Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 1 | #include "y2014/actors/shoot_actor.h" |
| 2 | |
| 3 | #include <functional> |
| 4 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 5 | #include "aos/logging/logging.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 6 | |
| 7 | #include "y2014/control_loops/shooter/shooter.q.h" |
| 8 | #include "y2014/control_loops/claw/claw.q.h" |
| 9 | #include "y2014/constants.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 11 | |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 12 | namespace y2014 { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 13 | namespace actors { |
| 14 | namespace { |
| 15 | |
| 16 | bool IntakeOff() { |
| 17 | control_loops::claw_queue.goal.FetchLatest(); |
| 18 | if (!control_loops::claw_queue.goal.get()) { |
| 19 | LOG(WARNING, "no claw goal\n"); |
| 20 | // If it doesn't have a goal, then the intake isn't on so we don't have to |
| 21 | // turn it off. |
| 22 | return true; |
| 23 | } else { |
| 24 | if (!control_loops::claw_queue.goal.MakeWithBuilder() |
| 25 | .bottom_angle(control_loops::claw_queue.goal->bottom_angle) |
| 26 | .separation_angle(control_loops::claw_queue.goal->separation_angle) |
| 27 | .intake(0.0) |
| 28 | .centering(0.0) |
| 29 | .Send()) { |
| 30 | LOG(WARNING, "sending claw goal failed\n"); |
| 31 | return false; |
| 32 | } |
| 33 | } |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | } // namespace |
| 38 | |
| 39 | constexpr double ShootActor::kOffsetRadians; |
| 40 | constexpr double ShootActor::kClawShootingSeparation; |
| 41 | constexpr double ShootActor::kClawShootingSeparationGoal; |
| 42 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 43 | ShootActor::ShootActor(::aos::EventLoop *event_loop) |
| 44 | : ::aos::common::actions::ActorBase<actors::ShootActionQueueGroup>( |
| 45 | event_loop, ".y2014.actors.shoot_action") {} |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 46 | |
| 47 | double ShootActor::SpeedToAngleOffset(double speed) { |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 48 | const constants::Values& values = constants::GetValues(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 49 | // scale speed to a [0.0-1.0] on something close to the max |
| 50 | return (speed / values.drivetrain_max_speed) * kOffsetRadians; |
| 51 | } |
| 52 | |
| 53 | bool ShootActor::RunAction(const double&) { |
| 54 | InnerRunAction(); |
| 55 | |
| 56 | // Now do our 'finally' block and make sure that we aren't requesting shots |
| 57 | // continually. |
| 58 | control_loops::shooter_queue.goal.FetchLatest(); |
| 59 | if (control_loops::shooter_queue.goal.get() == nullptr) { |
| 60 | return true; |
| 61 | } |
| 62 | if (!control_loops::shooter_queue.goal.MakeWithBuilder() |
| 63 | .shot_power(control_loops::shooter_queue.goal->shot_power) |
| 64 | .shot_requested(false) |
| 65 | .unload_requested(false) |
| 66 | .load_requested(false) |
| 67 | .Send()) { |
| 68 | LOG(WARNING, "sending shooter goal failed\n"); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | LOG(INFO, "finished\n"); |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | void ShootActor::InnerRunAction() { |
| 77 | LOG(INFO, "Shooting at the current angle and power.\n"); |
| 78 | |
| 79 | // wait for claw to be ready |
| 80 | if (WaitUntil(::std::bind(&ShootActor::DoneSetupShot, this))) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | if (!IntakeOff()) return; |
| 85 | |
| 86 | // Make sure that we have the latest shooter status. |
| 87 | control_loops::shooter_queue.status.FetchLatest(); |
| 88 | // Get the number of shots fired up to this point. This should not be updated |
| 89 | // again for another few cycles. |
| 90 | previous_shots_ = control_loops::shooter_queue.status->shots; |
| 91 | // Shoot! |
| 92 | if (!control_loops::shooter_queue.goal.MakeWithBuilder() |
| 93 | .shot_power(control_loops::shooter_queue.goal->shot_power) |
| 94 | .shot_requested(true) |
| 95 | .unload_requested(false) |
| 96 | .load_requested(false) |
| 97 | .Send()) { |
| 98 | LOG(WARNING, "sending shooter goal failed\n"); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | // wait for record of shot having been fired |
| 103 | if (WaitUntil(::std::bind(&ShootActor::DoneShot, this))) return; |
| 104 | |
| 105 | if (!IntakeOff()) return; |
| 106 | } |
| 107 | |
| 108 | bool ClawIsReady() { |
| 109 | control_loops::claw_queue.goal.FetchLatest(); |
| 110 | |
| 111 | bool ans = |
| 112 | control_loops::claw_queue.status->zeroed && |
| 113 | (::std::abs(control_loops::claw_queue.status->bottom_velocity) < 0.5) && |
| 114 | (::std::abs(control_loops::claw_queue.status->bottom - |
| 115 | control_loops::claw_queue.goal->bottom_angle) < 0.10) && |
| 116 | (::std::abs(control_loops::claw_queue.status->separation - |
| 117 | control_loops::claw_queue.goal->separation_angle) < 0.4); |
| 118 | LOG(DEBUG, "Claw is %sready zeroed %d bottom_velocity %f bottom %f sep %f\n", |
| 119 | ans ? "" : "not ", control_loops::claw_queue.status->zeroed, |
| 120 | ::std::abs(control_loops::claw_queue.status->bottom_velocity), |
| 121 | ::std::abs(control_loops::claw_queue.status->bottom - |
| 122 | control_loops::claw_queue.goal->bottom_angle), |
| 123 | ::std::abs(control_loops::claw_queue.status->separation - |
| 124 | control_loops::claw_queue.goal->separation_angle)); |
| 125 | return ans; |
| 126 | } |
| 127 | |
| 128 | bool ShooterIsReady() { |
| 129 | control_loops::shooter_queue.goal.FetchLatest(); |
| 130 | |
| 131 | LOG(DEBUG, "Power error is %f - %f -> %f, ready %d\n", |
| 132 | control_loops::shooter_queue.status->hard_stop_power, |
| 133 | control_loops::shooter_queue.goal->shot_power, |
| 134 | ::std::abs(control_loops::shooter_queue.status->hard_stop_power - |
| 135 | control_loops::shooter_queue.goal->shot_power), |
| 136 | control_loops::shooter_queue.status->ready); |
| 137 | return (::std::abs(control_loops::shooter_queue.status->hard_stop_power - |
| 138 | control_loops::shooter_queue.goal->shot_power) < 1.0) && |
| 139 | control_loops::shooter_queue.status->ready; |
| 140 | } |
| 141 | |
| 142 | bool ShootActor::DoneSetupShot() { |
| 143 | control_loops::shooter_queue.status.FetchAnother(); |
| 144 | control_loops::claw_queue.status.FetchAnother(); |
| 145 | // Make sure that both the shooter and claw have reached the necessary |
| 146 | // states. |
| 147 | if (ShooterIsReady() && ClawIsReady()) { |
| 148 | LOG(INFO, "Claw and Shooter ready for shooting.\n"); |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | bool ShootActor::DonePreShotOpen() { |
| 156 | control_loops::claw_queue.status.FetchAnother(); |
| 157 | if (control_loops::claw_queue.status->separation > kClawShootingSeparation) { |
| 158 | LOG(INFO, "Opened up enough to shoot.\n"); |
| 159 | return true; |
| 160 | } |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | bool ShootActor::DoneShot() { |
| 165 | control_loops::shooter_queue.status.FetchAnother(); |
| 166 | if (control_loops::shooter_queue.status->shots > previous_shots_) { |
| 167 | LOG(INFO, "Shot succeeded!\n"); |
| 168 | return true; |
| 169 | } |
| 170 | return false; |
| 171 | } |
| 172 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 173 | } // namespace actors |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 174 | } // namespace y2014 |