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