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