blob: aad64e83d7a38c0f8b27bad2a90454a146932515 [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"
Brian Silverman17f503e2015-08-02 18:17:18 -07006
Austin Schuhb2461f42019-06-29 18:17:06 -07007#include "y2014/constants.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#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 Silverman17f503e2015-08-02 18:17:18 -070012
Brian Silvermanb601d892015-12-20 18:24:38 -050013namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070014namespace actors {
Brian Silverman17f503e2015-08-02 18:17:18 -070015
16constexpr double ShootActor::kOffsetRadians;
17constexpr double ShootActor::kClawShootingSeparation;
18constexpr double ShootActor::kClawShootingSeparationGoal;
19
Austin Schuh1bf8a212019-05-26 22:13:14 -070020ShootActor::ShootActor(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -070021 : ::aos::common::actions::ActorBase<aos::common::actions::Goal>(
22 event_loop, "/shoot_action"),
Austin Schuhb2461f42019-06-29 18:17:06 -070023 claw_goal_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070024 event_loop->MakeFetcher<::y2014::control_loops::claw::Goal>("/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070025 claw_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070026 event_loop->MakeFetcher<::y2014::control_loops::claw::Status>(
27 "/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070028 claw_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070029 event_loop->MakeSender<::y2014::control_loops::claw::Goal>("/claw")),
Austin Schuh493f7af2019-06-29 18:42:12 -070030 shooter_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 event_loop->MakeFetcher<::y2014::control_loops::shooter::Status>(
32 "/shooter")),
Austin Schuh493f7af2019-06-29 18:42:12 -070033 shooter_goal_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070034 event_loop->MakeFetcher<::y2014::control_loops::shooter::Goal>(
35 "/shooter")),
Austin Schuh493f7af2019-06-29 18:42:12 -070036 shooter_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070037 event_loop->MakeSender<::y2014::control_loops::shooter::Goal>(
Austin Schuh094d09b2020-11-20 23:26:52 -080038 "/shooter")) {
39 event_loop->SetRuntimeRealtimePriority(29);
40}
Brian Silverman17f503e2015-08-02 18:17:18 -070041
42double ShootActor::SpeedToAngleOffset(double speed) {
Austin Schuhb2461f42019-06-29 18:17:06 -070043 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -070044 // 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 Schuhb2461f42019-06-29 18:17:06 -070048bool ShootActor::IntakeOff() {
49 claw_goal_fetcher_.Fetch();
50 if (!claw_goal_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070051 AOS_LOG(WARNING, "no claw goal\n");
Austin Schuhb2461f42019-06-29 18:17:06 -070052 // 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 Perrycb7da4b2019-08-28 19:35:56 -070056 auto builder = claw_goal_sender_.MakeBuilder();
Austin Schuhb2461f42019-06-29 18:17:06 -070057
Alex Perrycb7da4b2019-08-28 19:35:56 -070058 control_loops::claw::Goal::Builder claw_builder =
59 builder.MakeBuilder<control_loops::claw::Goal>();
Austin Schuhb2461f42019-06-29 18:17:06 -070060
Alex Perrycb7da4b2019-08-28 19:35:56 -070061 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
milind1f1dca32021-07-03 13:50:07 -070066 if (builder.Send(claw_builder.Finish()) !=
67 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070068 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhb2461f42019-06-29 18:17:06 -070069 return false;
70 }
71 }
72 return true;
73}
74
Alex Perrycb7da4b2019-08-28 19:35:56 -070075bool ShootActor::RunAction(const aos::common::actions::DoubleParam *) {
Brian Silverman17f503e2015-08-02 18:17:18 -070076 InnerRunAction();
77
78 // Now do our 'finally' block and make sure that we aren't requesting shots
79 // continually.
Austin Schuh493f7af2019-06-29 18:42:12 -070080 shooter_goal_fetcher_.Fetch();
81 if (shooter_goal_fetcher_.get() == nullptr) {
Brian Silverman17f503e2015-08-02 18:17:18 -070082 return true;
83 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070084 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);
milind1f1dca32021-07-03 13:50:07 -070092 if (builder.Send(shooter_builder.Finish()) !=
93 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070094 AOS_LOG(WARNING, "sending shooter goal failed\n");
Brian Silverman17f503e2015-08-02 18:17:18 -070095 return false;
96 }
97
Austin Schuhf257f3c2019-10-27 21:00:43 -070098 AOS_LOG(INFO, "finished\n");
Brian Silverman17f503e2015-08-02 18:17:18 -070099 return true;
100}
101
102void ShootActor::InnerRunAction() {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700103 AOS_LOG(INFO, "Shooting at the current angle and power.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700104
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 Schuh493f7af2019-06-29 18:42:12 -0700113 shooter_status_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700114 // Get the number of shots fired up to this point. This should not be updated
115 // again for another few cycles.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700116 previous_shots_ = shooter_status_fetcher_->shots();
Brian Silverman17f503e2015-08-02 18:17:18 -0700117 // Shoot!
Austin Schuh493f7af2019-06-29 18:42:12 -0700118 shooter_goal_fetcher_.Fetch();
119 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700120 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);
milind1f1dca32021-07-03 13:50:07 -0700127 if (builder.Send(goal_builder.Finish()) !=
128 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700129 AOS_LOG(WARNING, "sending shooter goal failed\n");
Austin Schuh493f7af2019-06-29 18:42:12 -0700130 return;
131 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700132 }
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 Schuhb2461f42019-06-29 18:17:06 -0700140bool ShootActor::ClawIsReady() {
141 claw_goal_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700142
Alex Perrycb7da4b2019-08-28 19:35:56 -0700143 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 Schuhf257f3c2019-10-27 21:00:43 -0700149 AOS_LOG(DEBUG,
150 "Claw is %sready zeroed %d bottom_velocity %f bottom %f sep %f\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700151 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 Silverman17f503e2015-08-02 18:17:18 -0700157 return ans;
158}
159
Austin Schuh493f7af2019-06-29 18:42:12 -0700160bool ShootActor::ShooterIsReady() {
161 shooter_goal_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700162
Austin Schuhf257f3c2019-10-27 21:00:43 -0700163 AOS_LOG(DEBUG, "Power error is %f - %f -> %f, ready %d\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700164 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 Silverman17f503e2015-08-02 18:17:18 -0700172}
173
174bool ShootActor::DoneSetupShot() {
Austin Schuh493f7af2019-06-29 18:42:12 -0700175 shooter_status_fetcher_.Fetch();
Austin Schuhb2461f42019-06-29 18:17:06 -0700176 claw_status_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700177 // Make sure that both the shooter and claw have reached the necessary
178 // states.
179 if (ShooterIsReady() && ClawIsReady()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700180 AOS_LOG(INFO, "Claw and Shooter ready for shooting.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700181 return true;
182 }
183
184 return false;
185}
186
187bool ShootActor::DonePreShotOpen() {
Austin Schuhb2461f42019-06-29 18:17:06 -0700188 claw_status_fetcher_.Fetch();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700189 if (claw_status_fetcher_->separation() > kClawShootingSeparation) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700190 AOS_LOG(INFO, "Opened up enough to shoot.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700191 return true;
192 }
193 return false;
194}
195
196bool ShootActor::DoneShot() {
Austin Schuh493f7af2019-06-29 18:42:12 -0700197 shooter_status_fetcher_.Fetch();
198 if (shooter_status_fetcher_.get() &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700199 shooter_status_fetcher_->shots() > previous_shots_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700200 AOS_LOG(INFO, "Shot succeeded!\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700201 return true;
202 }
203 return false;
204}
205
Brian Silverman17f503e2015-08-02 18:17:18 -0700206} // namespace actors
Brian Silvermanb601d892015-12-20 18:24:38 -0500207} // namespace y2014