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 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 66 | if (builder.Send(claw_builder.Finish()) != |
| 67 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 68 | AOS_LOG(WARNING, "sending claw goal failed\n"); |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 75 | bool ShootActor::RunAction(const aos::common::actions::DoubleParam *) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 76 | InnerRunAction(); |
| 77 | |
| 78 | // Now do our 'finally' block and make sure that we aren't requesting shots |
| 79 | // continually. |
Austin Schuh | 493f7af | 2019-06-29 18:42:12 -0700 | [diff] [blame] | 80 | shooter_goal_fetcher_.Fetch(); |
| 81 | if (shooter_goal_fetcher_.get() == nullptr) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 82 | return true; |
| 83 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 84 | auto builder = shooter_goal_sender_.MakeBuilder(); |
| 85 | control_loops::shooter::Goal::Builder shooter_builder = |
| 86 | builder.MakeBuilder<control_loops::shooter::Goal>(); |
| 87 | |
| 88 | shooter_builder.add_shot_power(shooter_goal_fetcher_->shot_power()); |
| 89 | shooter_builder.add_shot_requested(false); |
| 90 | shooter_builder.add_unload_requested(false); |
| 91 | shooter_builder.add_load_requested(false); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 92 | if (builder.Send(shooter_builder.Finish()) != |
| 93 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 94 | AOS_LOG(WARNING, "sending shooter goal failed\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 98 | AOS_LOG(INFO, "finished\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
| 102 | void ShootActor::InnerRunAction() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 103 | AOS_LOG(INFO, "Shooting at the current angle and power.\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 104 | |
| 105 | // wait for claw to be ready |
| 106 | if (WaitUntil(::std::bind(&ShootActor::DoneSetupShot, this))) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | if (!IntakeOff()) return; |
| 111 | |
| 112 | // Make sure that we have the latest shooter status. |
Austin Schuh | 493f7af | 2019-06-29 18:42:12 -0700 | [diff] [blame] | 113 | shooter_status_fetcher_.Fetch(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 114 | // Get the number of shots fired up to this point. This should not be updated |
| 115 | // again for another few cycles. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 116 | previous_shots_ = shooter_status_fetcher_->shots(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 117 | // Shoot! |
Austin Schuh | 493f7af | 2019-06-29 18:42:12 -0700 | [diff] [blame] | 118 | shooter_goal_fetcher_.Fetch(); |
| 119 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 120 | auto builder = shooter_goal_sender_.MakeBuilder(); |
| 121 | control_loops::shooter::Goal::Builder goal_builder = |
| 122 | builder.MakeBuilder<control_loops::shooter::Goal>(); |
| 123 | goal_builder.add_shot_power(shooter_goal_fetcher_->shot_power()); |
| 124 | goal_builder.add_shot_requested(true); |
| 125 | goal_builder.add_unload_requested(false); |
| 126 | goal_builder.add_load_requested(false); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 127 | if (builder.Send(goal_builder.Finish()) != |
| 128 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 129 | AOS_LOG(WARNING, "sending shooter goal failed\n"); |
Austin Schuh | 493f7af | 2019-06-29 18:42:12 -0700 | [diff] [blame] | 130 | return; |
| 131 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // wait for record of shot having been fired |
| 135 | if (WaitUntil(::std::bind(&ShootActor::DoneShot, this))) return; |
| 136 | |
| 137 | if (!IntakeOff()) return; |
| 138 | } |
| 139 | |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 140 | bool ShootActor::ClawIsReady() { |
| 141 | claw_goal_fetcher_.Fetch(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 142 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 143 | bool ans = claw_status_fetcher_->zeroed() && |
| 144 | (::std::abs(claw_status_fetcher_->bottom_velocity()) < 0.5) && |
| 145 | (::std::abs(claw_status_fetcher_->bottom() - |
| 146 | claw_goal_fetcher_->bottom_angle()) < 0.10) && |
| 147 | (::std::abs(claw_status_fetcher_->separation() - |
| 148 | claw_goal_fetcher_->separation_angle()) < 0.4); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 149 | AOS_LOG(DEBUG, |
| 150 | "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] | 151 | ans ? "" : "not ", claw_status_fetcher_->zeroed(), |
| 152 | ::std::abs(claw_status_fetcher_->bottom_velocity()), |
| 153 | ::std::abs(claw_status_fetcher_->bottom() - |
| 154 | claw_goal_fetcher_->bottom_angle()), |
| 155 | ::std::abs(claw_status_fetcher_->separation() - |
| 156 | claw_goal_fetcher_->separation_angle())); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 157 | return ans; |
| 158 | } |
| 159 | |
Austin Schuh | 493f7af | 2019-06-29 18:42:12 -0700 | [diff] [blame] | 160 | bool ShootActor::ShooterIsReady() { |
| 161 | shooter_goal_fetcher_.Fetch(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 162 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 163 | AOS_LOG(DEBUG, "Power error is %f - %f -> %f, ready %d\n", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 164 | shooter_status_fetcher_->hard_stop_power(), |
| 165 | shooter_goal_fetcher_->shot_power(), |
| 166 | ::std::abs(shooter_status_fetcher_->hard_stop_power() - |
| 167 | shooter_goal_fetcher_->shot_power()), |
| 168 | shooter_status_fetcher_->ready()); |
| 169 | return (::std::abs(shooter_status_fetcher_->hard_stop_power() - |
| 170 | shooter_goal_fetcher_->shot_power()) < 1.0) && |
| 171 | shooter_status_fetcher_->ready(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | bool ShootActor::DoneSetupShot() { |
Austin Schuh | 493f7af | 2019-06-29 18:42:12 -0700 | [diff] [blame] | 175 | shooter_status_fetcher_.Fetch(); |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 176 | claw_status_fetcher_.Fetch(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 177 | // Make sure that both the shooter and claw have reached the necessary |
| 178 | // states. |
| 179 | if (ShooterIsReady() && ClawIsReady()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 180 | AOS_LOG(INFO, "Claw and Shooter ready for shooting.\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 181 | return true; |
| 182 | } |
| 183 | |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | bool ShootActor::DonePreShotOpen() { |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 188 | claw_status_fetcher_.Fetch(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 189 | if (claw_status_fetcher_->separation() > kClawShootingSeparation) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 190 | AOS_LOG(INFO, "Opened up enough to shoot.\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 191 | return true; |
| 192 | } |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | bool ShootActor::DoneShot() { |
Austin Schuh | 493f7af | 2019-06-29 18:42:12 -0700 | [diff] [blame] | 197 | shooter_status_fetcher_.Fetch(); |
| 198 | if (shooter_status_fetcher_.get() && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 199 | shooter_status_fetcher_->shots() > previous_shots_) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 200 | AOS_LOG(INFO, "Shot succeeded!\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 201 | return true; |
| 202 | } |
| 203 | return false; |
| 204 | } |
| 205 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 206 | } // namespace actors |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 207 | } // namespace y2014 |