blob: 691e3de7932c3fd1d7548e748a11066acec8d503 [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080012namespace y2014::actors {
Brian Silverman17f503e2015-08-02 18:17:18 -070013
14constexpr double ShootActor::kOffsetRadians;
15constexpr double ShootActor::kClawShootingSeparation;
16constexpr double ShootActor::kClawShootingSeparationGoal;
17
Austin Schuh1bf8a212019-05-26 22:13:14 -070018ShootActor::ShootActor(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -070019 : ::aos::common::actions::ActorBase<aos::common::actions::Goal>(
20 event_loop, "/shoot_action"),
Austin Schuhb2461f42019-06-29 18:17:06 -070021 claw_goal_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070022 event_loop->MakeFetcher<::y2014::control_loops::claw::Goal>("/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070023 claw_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070024 event_loop->MakeFetcher<::y2014::control_loops::claw::Status>(
25 "/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070026 claw_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070027 event_loop->MakeSender<::y2014::control_loops::claw::Goal>("/claw")),
Austin Schuh493f7af2019-06-29 18:42:12 -070028 shooter_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070029 event_loop->MakeFetcher<::y2014::control_loops::shooter::Status>(
30 "/shooter")),
Austin Schuh493f7af2019-06-29 18:42:12 -070031 shooter_goal_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070032 event_loop->MakeFetcher<::y2014::control_loops::shooter::Goal>(
33 "/shooter")),
Austin Schuh493f7af2019-06-29 18:42:12 -070034 shooter_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070035 event_loop->MakeSender<::y2014::control_loops::shooter::Goal>(
Austin Schuh094d09b2020-11-20 23:26:52 -080036 "/shooter")) {
37 event_loop->SetRuntimeRealtimePriority(29);
38}
Brian Silverman17f503e2015-08-02 18:17:18 -070039
40double ShootActor::SpeedToAngleOffset(double speed) {
Austin Schuhb2461f42019-06-29 18:17:06 -070041 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -070042 // 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 Schuhb2461f42019-06-29 18:17:06 -070046bool ShootActor::IntakeOff() {
47 claw_goal_fetcher_.Fetch();
48 if (!claw_goal_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070049 AOS_LOG(WARNING, "no claw goal\n");
Austin Schuhb2461f42019-06-29 18:17:06 -070050 // 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 Perrycb7da4b2019-08-28 19:35:56 -070054 auto builder = claw_goal_sender_.MakeBuilder();
Austin Schuhb2461f42019-06-29 18:17:06 -070055
Alex Perrycb7da4b2019-08-28 19:35:56 -070056 control_loops::claw::Goal::Builder claw_builder =
57 builder.MakeBuilder<control_loops::claw::Goal>();
Austin Schuhb2461f42019-06-29 18:17:06 -070058
Alex Perrycb7da4b2019-08-28 19:35:56 -070059 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
Philipp Schrader790cb542023-07-05 21:06:52 -070064 if (builder.Send(claw_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070065 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhb2461f42019-06-29 18:17:06 -070066 return false;
67 }
68 }
69 return true;
70}
71
Alex Perrycb7da4b2019-08-28 19:35:56 -070072bool ShootActor::RunAction(const aos::common::actions::DoubleParam *) {
Brian Silverman17f503e2015-08-02 18:17:18 -070073 InnerRunAction();
74
75 // Now do our 'finally' block and make sure that we aren't requesting shots
76 // continually.
Austin Schuh493f7af2019-06-29 18:42:12 -070077 shooter_goal_fetcher_.Fetch();
78 if (shooter_goal_fetcher_.get() == nullptr) {
Brian Silverman17f503e2015-08-02 18:17:18 -070079 return true;
80 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070081 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);
Philipp Schrader790cb542023-07-05 21:06:52 -070089 if (builder.Send(shooter_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070090 AOS_LOG(WARNING, "sending shooter goal failed\n");
Brian Silverman17f503e2015-08-02 18:17:18 -070091 return false;
92 }
93
Austin Schuhf257f3c2019-10-27 21:00:43 -070094 AOS_LOG(INFO, "finished\n");
Brian Silverman17f503e2015-08-02 18:17:18 -070095 return true;
96}
97
98void ShootActor::InnerRunAction() {
Austin Schuhf257f3c2019-10-27 21:00:43 -070099 AOS_LOG(INFO, "Shooting at the current angle and power.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700100
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 Schuh493f7af2019-06-29 18:42:12 -0700109 shooter_status_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700110 // Get the number of shots fired up to this point. This should not be updated
111 // again for another few cycles.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700112 previous_shots_ = shooter_status_fetcher_->shots();
Brian Silverman17f503e2015-08-02 18:17:18 -0700113 // Shoot!
Austin Schuh493f7af2019-06-29 18:42:12 -0700114 shooter_goal_fetcher_.Fetch();
115 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700116 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);
Philipp Schrader790cb542023-07-05 21:06:52 -0700123 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700124 AOS_LOG(WARNING, "sending shooter goal failed\n");
Austin Schuh493f7af2019-06-29 18:42:12 -0700125 return;
126 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700127 }
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 Schuhb2461f42019-06-29 18:17:06 -0700135bool ShootActor::ClawIsReady() {
136 claw_goal_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700137
Alex Perrycb7da4b2019-08-28 19:35:56 -0700138 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 Schuhf257f3c2019-10-27 21:00:43 -0700144 AOS_LOG(DEBUG,
145 "Claw is %sready zeroed %d bottom_velocity %f bottom %f sep %f\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700146 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 Silverman17f503e2015-08-02 18:17:18 -0700152 return ans;
153}
154
Austin Schuh493f7af2019-06-29 18:42:12 -0700155bool ShootActor::ShooterIsReady() {
156 shooter_goal_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700157
Austin Schuhf257f3c2019-10-27 21:00:43 -0700158 AOS_LOG(DEBUG, "Power error is %f - %f -> %f, ready %d\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700159 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 Silverman17f503e2015-08-02 18:17:18 -0700167}
168
169bool ShootActor::DoneSetupShot() {
Austin Schuh493f7af2019-06-29 18:42:12 -0700170 shooter_status_fetcher_.Fetch();
Austin Schuhb2461f42019-06-29 18:17:06 -0700171 claw_status_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700172 // Make sure that both the shooter and claw have reached the necessary
173 // states.
174 if (ShooterIsReady() && ClawIsReady()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700175 AOS_LOG(INFO, "Claw and Shooter ready for shooting.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700176 return true;
177 }
178
179 return false;
180}
181
182bool ShootActor::DonePreShotOpen() {
Austin Schuhb2461f42019-06-29 18:17:06 -0700183 claw_status_fetcher_.Fetch();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700184 if (claw_status_fetcher_->separation() > kClawShootingSeparation) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700185 AOS_LOG(INFO, "Opened up enough to shoot.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700186 return true;
187 }
188 return false;
189}
190
191bool ShootActor::DoneShot() {
Austin Schuh493f7af2019-06-29 18:42:12 -0700192 shooter_status_fetcher_.Fetch();
193 if (shooter_status_fetcher_.get() &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700194 shooter_status_fetcher_->shots() > previous_shots_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700195 AOS_LOG(INFO, "Shot succeeded!\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700196 return true;
197 }
198 return false;
199}
200
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800201} // namespace y2014::actors