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