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