blob: 07190628e9ec3abd5e34266beb2d43d116b15707 [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#include "y2014/actors/shoot_actor.h"
2
3#include <functional>
4
John Park33858a32018-09-28 23:05:48 -07005#include "aos/logging/logging.h"
Austin Schuhb2461f42019-06-29 18:17:06 -07006#include "y2014/constants.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#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 Silverman17f503e2015-08-02 18:17:18 -070011
Brian Silvermanb601d892015-12-20 18:24:38 -050012namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070013namespace actors {
Brian Silverman17f503e2015-08-02 18:17:18 -070014
15constexpr double ShootActor::kOffsetRadians;
16constexpr double ShootActor::kClawShootingSeparation;
17constexpr double ShootActor::kClawShootingSeparationGoal;
18
Austin Schuh1bf8a212019-05-26 22:13:14 -070019ShootActor::ShootActor(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -070020 : ::aos::common::actions::ActorBase<aos::common::actions::Goal>(
21 event_loop, "/shoot_action"),
Austin Schuhb2461f42019-06-29 18:17:06 -070022 claw_goal_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070023 event_loop->MakeFetcher<::y2014::control_loops::claw::Goal>("/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070024 claw_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070025 event_loop->MakeFetcher<::y2014::control_loops::claw::Status>(
26 "/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070027 claw_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070028 event_loop->MakeSender<::y2014::control_loops::claw::Goal>("/claw")),
Austin Schuh493f7af2019-06-29 18:42:12 -070029 shooter_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070030 event_loop->MakeFetcher<::y2014::control_loops::shooter::Status>(
31 "/shooter")),
Austin Schuh493f7af2019-06-29 18:42:12 -070032 shooter_goal_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070033 event_loop->MakeFetcher<::y2014::control_loops::shooter::Goal>(
34 "/shooter")),
Austin Schuh493f7af2019-06-29 18:42:12 -070035 shooter_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070036 event_loop->MakeSender<::y2014::control_loops::shooter::Goal>(
Austin Schuh094d09b2020-11-20 23:26:52 -080037 "/shooter")) {
38 event_loop->SetRuntimeRealtimePriority(29);
39}
Brian Silverman17f503e2015-08-02 18:17:18 -070040
41double ShootActor::SpeedToAngleOffset(double speed) {
Austin Schuhb2461f42019-06-29 18:17:06 -070042 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -070043 // 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 Schuhb2461f42019-06-29 18:17:06 -070047bool ShootActor::IntakeOff() {
48 claw_goal_fetcher_.Fetch();
49 if (!claw_goal_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070050 AOS_LOG(WARNING, "no claw goal\n");
Austin Schuhb2461f42019-06-29 18:17:06 -070051 // 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 Perrycb7da4b2019-08-28 19:35:56 -070055 auto builder = claw_goal_sender_.MakeBuilder();
Austin Schuhb2461f42019-06-29 18:17:06 -070056
Alex Perrycb7da4b2019-08-28 19:35:56 -070057 control_loops::claw::Goal::Builder claw_builder =
58 builder.MakeBuilder<control_loops::claw::Goal>();
Austin Schuhb2461f42019-06-29 18:17:06 -070059
Alex Perrycb7da4b2019-08-28 19:35:56 -070060 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 Schrader790cb542023-07-05 21:06:52 -070065 if (builder.Send(claw_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070066 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhb2461f42019-06-29 18:17:06 -070067 return false;
68 }
69 }
70 return true;
71}
72
Alex Perrycb7da4b2019-08-28 19:35:56 -070073bool ShootActor::RunAction(const aos::common::actions::DoubleParam *) {
Brian Silverman17f503e2015-08-02 18:17:18 -070074 InnerRunAction();
75
76 // Now do our 'finally' block and make sure that we aren't requesting shots
77 // continually.
Austin Schuh493f7af2019-06-29 18:42:12 -070078 shooter_goal_fetcher_.Fetch();
79 if (shooter_goal_fetcher_.get() == nullptr) {
Brian Silverman17f503e2015-08-02 18:17:18 -070080 return true;
81 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070082 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 Schrader790cb542023-07-05 21:06:52 -070090 if (builder.Send(shooter_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070091 AOS_LOG(WARNING, "sending shooter goal failed\n");
Brian Silverman17f503e2015-08-02 18:17:18 -070092 return false;
93 }
94
Austin Schuhf257f3c2019-10-27 21:00:43 -070095 AOS_LOG(INFO, "finished\n");
Brian Silverman17f503e2015-08-02 18:17:18 -070096 return true;
97}
98
99void ShootActor::InnerRunAction() {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700100 AOS_LOG(INFO, "Shooting at the current angle and power.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700101
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 Schuh493f7af2019-06-29 18:42:12 -0700110 shooter_status_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700111 // Get the number of shots fired up to this point. This should not be updated
112 // again for another few cycles.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700113 previous_shots_ = shooter_status_fetcher_->shots();
Brian Silverman17f503e2015-08-02 18:17:18 -0700114 // Shoot!
Austin Schuh493f7af2019-06-29 18:42:12 -0700115 shooter_goal_fetcher_.Fetch();
116 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700117 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 Schrader790cb542023-07-05 21:06:52 -0700124 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700125 AOS_LOG(WARNING, "sending shooter goal failed\n");
Austin Schuh493f7af2019-06-29 18:42:12 -0700126 return;
127 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700128 }
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 Schuhb2461f42019-06-29 18:17:06 -0700136bool ShootActor::ClawIsReady() {
137 claw_goal_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700138
Alex Perrycb7da4b2019-08-28 19:35:56 -0700139 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 Schuhf257f3c2019-10-27 21:00:43 -0700145 AOS_LOG(DEBUG,
146 "Claw is %sready zeroed %d bottom_velocity %f bottom %f sep %f\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700147 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 Silverman17f503e2015-08-02 18:17:18 -0700153 return ans;
154}
155
Austin Schuh493f7af2019-06-29 18:42:12 -0700156bool ShootActor::ShooterIsReady() {
157 shooter_goal_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700158
Austin Schuhf257f3c2019-10-27 21:00:43 -0700159 AOS_LOG(DEBUG, "Power error is %f - %f -> %f, ready %d\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700160 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 Silverman17f503e2015-08-02 18:17:18 -0700168}
169
170bool ShootActor::DoneSetupShot() {
Austin Schuh493f7af2019-06-29 18:42:12 -0700171 shooter_status_fetcher_.Fetch();
Austin Schuhb2461f42019-06-29 18:17:06 -0700172 claw_status_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700173 // Make sure that both the shooter and claw have reached the necessary
174 // states.
175 if (ShooterIsReady() && ClawIsReady()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700176 AOS_LOG(INFO, "Claw and Shooter ready for shooting.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700177 return true;
178 }
179
180 return false;
181}
182
183bool ShootActor::DonePreShotOpen() {
Austin Schuhb2461f42019-06-29 18:17:06 -0700184 claw_status_fetcher_.Fetch();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700185 if (claw_status_fetcher_->separation() > kClawShootingSeparation) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700186 AOS_LOG(INFO, "Opened up enough to shoot.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700187 return true;
188 }
189 return false;
190}
191
192bool ShootActor::DoneShot() {
Austin Schuh493f7af2019-06-29 18:42:12 -0700193 shooter_status_fetcher_.Fetch();
194 if (shooter_status_fetcher_.get() &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700195 shooter_status_fetcher_->shots() > previous_shots_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700196 AOS_LOG(INFO, "Shot succeeded!\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700197 return true;
198 }
199 return false;
200}
201
Brian Silverman17f503e2015-08-02 18:17:18 -0700202} // namespace actors
Brian Silvermanb601d892015-12-20 18:24:38 -0500203} // namespace y2014