blob: 095a9728fab9b0b7110a0c6c0dadce0187953d8e [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
66 if (!builder.Send(claw_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070067 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhb2461f42019-06-29 18:17:06 -070068 return false;
69 }
70 }
71 return true;
72}
73
Alex Perrycb7da4b2019-08-28 19:35:56 -070074bool ShootActor::RunAction(const aos::common::actions::DoubleParam *) {
Brian Silverman17f503e2015-08-02 18:17:18 -070075 InnerRunAction();
76
77 // Now do our 'finally' block and make sure that we aren't requesting shots
78 // continually.
Austin Schuh493f7af2019-06-29 18:42:12 -070079 shooter_goal_fetcher_.Fetch();
80 if (shooter_goal_fetcher_.get() == nullptr) {
Brian Silverman17f503e2015-08-02 18:17:18 -070081 return true;
82 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070083 auto builder = shooter_goal_sender_.MakeBuilder();
84 control_loops::shooter::Goal::Builder shooter_builder =
85 builder.MakeBuilder<control_loops::shooter::Goal>();
86
87 shooter_builder.add_shot_power(shooter_goal_fetcher_->shot_power());
88 shooter_builder.add_shot_requested(false);
89 shooter_builder.add_unload_requested(false);
90 shooter_builder.add_load_requested(false);
91 if (!builder.Send(shooter_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070092 AOS_LOG(WARNING, "sending shooter goal failed\n");
Brian Silverman17f503e2015-08-02 18:17:18 -070093 return false;
94 }
95
Austin Schuhf257f3c2019-10-27 21:00:43 -070096 AOS_LOG(INFO, "finished\n");
Brian Silverman17f503e2015-08-02 18:17:18 -070097 return true;
98}
99
100void ShootActor::InnerRunAction() {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700101 AOS_LOG(INFO, "Shooting at the current angle and power.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700102
103 // wait for claw to be ready
104 if (WaitUntil(::std::bind(&ShootActor::DoneSetupShot, this))) {
105 return;
106 }
107
108 if (!IntakeOff()) return;
109
110 // Make sure that we have the latest shooter status.
Austin Schuh493f7af2019-06-29 18:42:12 -0700111 shooter_status_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700112 // Get the number of shots fired up to this point. This should not be updated
113 // again for another few cycles.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700114 previous_shots_ = shooter_status_fetcher_->shots();
Brian Silverman17f503e2015-08-02 18:17:18 -0700115 // Shoot!
Austin Schuh493f7af2019-06-29 18:42:12 -0700116 shooter_goal_fetcher_.Fetch();
117 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700118 auto builder = shooter_goal_sender_.MakeBuilder();
119 control_loops::shooter::Goal::Builder goal_builder =
120 builder.MakeBuilder<control_loops::shooter::Goal>();
121 goal_builder.add_shot_power(shooter_goal_fetcher_->shot_power());
122 goal_builder.add_shot_requested(true);
123 goal_builder.add_unload_requested(false);
124 goal_builder.add_load_requested(false);
125 if (!builder.Send(goal_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700126 AOS_LOG(WARNING, "sending shooter goal failed\n");
Austin Schuh493f7af2019-06-29 18:42:12 -0700127 return;
128 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700129 }
130
131 // wait for record of shot having been fired
132 if (WaitUntil(::std::bind(&ShootActor::DoneShot, this))) return;
133
134 if (!IntakeOff()) return;
135}
136
Austin Schuhb2461f42019-06-29 18:17:06 -0700137bool ShootActor::ClawIsReady() {
138 claw_goal_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700139
Alex Perrycb7da4b2019-08-28 19:35:56 -0700140 bool ans = claw_status_fetcher_->zeroed() &&
141 (::std::abs(claw_status_fetcher_->bottom_velocity()) < 0.5) &&
142 (::std::abs(claw_status_fetcher_->bottom() -
143 claw_goal_fetcher_->bottom_angle()) < 0.10) &&
144 (::std::abs(claw_status_fetcher_->separation() -
145 claw_goal_fetcher_->separation_angle()) < 0.4);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700146 AOS_LOG(DEBUG,
147 "Claw is %sready zeroed %d bottom_velocity %f bottom %f sep %f\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700148 ans ? "" : "not ", claw_status_fetcher_->zeroed(),
149 ::std::abs(claw_status_fetcher_->bottom_velocity()),
150 ::std::abs(claw_status_fetcher_->bottom() -
151 claw_goal_fetcher_->bottom_angle()),
152 ::std::abs(claw_status_fetcher_->separation() -
153 claw_goal_fetcher_->separation_angle()));
Brian Silverman17f503e2015-08-02 18:17:18 -0700154 return ans;
155}
156
Austin Schuh493f7af2019-06-29 18:42:12 -0700157bool ShootActor::ShooterIsReady() {
158 shooter_goal_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700159
Austin Schuhf257f3c2019-10-27 21:00:43 -0700160 AOS_LOG(DEBUG, "Power error is %f - %f -> %f, ready %d\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161 shooter_status_fetcher_->hard_stop_power(),
162 shooter_goal_fetcher_->shot_power(),
163 ::std::abs(shooter_status_fetcher_->hard_stop_power() -
164 shooter_goal_fetcher_->shot_power()),
165 shooter_status_fetcher_->ready());
166 return (::std::abs(shooter_status_fetcher_->hard_stop_power() -
167 shooter_goal_fetcher_->shot_power()) < 1.0) &&
168 shooter_status_fetcher_->ready();
Brian Silverman17f503e2015-08-02 18:17:18 -0700169}
170
171bool ShootActor::DoneSetupShot() {
Austin Schuh493f7af2019-06-29 18:42:12 -0700172 shooter_status_fetcher_.Fetch();
Austin Schuhb2461f42019-06-29 18:17:06 -0700173 claw_status_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700174 // Make sure that both the shooter and claw have reached the necessary
175 // states.
176 if (ShooterIsReady() && ClawIsReady()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700177 AOS_LOG(INFO, "Claw and Shooter ready for shooting.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700178 return true;
179 }
180
181 return false;
182}
183
184bool ShootActor::DonePreShotOpen() {
Austin Schuhb2461f42019-06-29 18:17:06 -0700185 claw_status_fetcher_.Fetch();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700186 if (claw_status_fetcher_->separation() > kClawShootingSeparation) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700187 AOS_LOG(INFO, "Opened up enough to shoot.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700188 return true;
189 }
190 return false;
191}
192
193bool ShootActor::DoneShot() {
Austin Schuh493f7af2019-06-29 18:42:12 -0700194 shooter_status_fetcher_.Fetch();
195 if (shooter_status_fetcher_.get() &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700196 shooter_status_fetcher_->shots() > previous_shots_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700197 AOS_LOG(INFO, "Shot succeeded!\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700198 return true;
199 }
200 return false;
201}
202
Brian Silverman17f503e2015-08-02 18:17:18 -0700203} // namespace actors
Brian Silvermanb601d892015-12-20 18:24:38 -0500204} // namespace y2014