blob: 736632d42170cec2b15e0293a17ef415f26740b0 [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
Comran Morshed5323ecb2015-12-26 20:50:55 +00007#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuhb2461f42019-06-29 18:17:06 -07008#include "y2014/constants.h"
9#include "y2014/control_loops/claw/claw.q.h"
10#include "y2014/control_loops/shooter/shooter.q.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)
20 : ::aos::common::actions::ActorBase<actors::ShootActionQueueGroup>(
Austin Schuhb2461f42019-06-29 18:17:06 -070021 event_loop, ".y2014.actors.shoot_action"),
22 claw_goal_fetcher_(
23 event_loop->MakeFetcher<::y2014::control_loops::ClawQueue::Goal>(
24 ".y2014.control_loops.claw_queue.goal")),
25 claw_status_fetcher_(
26 event_loop->MakeFetcher<::y2014::control_loops::ClawQueue::Status>(
27 ".y2014.control_loops.claw_queue.status")),
28 claw_goal_sender_(
29 event_loop->MakeSender<::y2014::control_loops::ClawQueue::Goal>(
Austin Schuh493f7af2019-06-29 18:42:12 -070030 ".y2014.control_loops.claw_queue.goal")),
31 shooter_status_fetcher_(
32 event_loop->MakeFetcher<::y2014::control_loops::ShooterQueue::Status>(
33 ".y2014.control_loops.shooter_queue.status")),
34 shooter_goal_fetcher_(
35 event_loop->MakeFetcher<::y2014::control_loops::ShooterQueue::Goal>(
36 ".y2014.control_loops.shooter_queue.goal")),
37 shooter_goal_sender_(
38 event_loop->MakeSender<::y2014::control_loops::ShooterQueue::Goal>(
39 ".y2014.control_loops.shooter_queue.goal")) {}
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()) {
50 LOG(WARNING, "no claw goal\n");
51 // 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 {
55 auto goal_message = claw_goal_sender_.MakeMessage();
56
57 goal_message->bottom_angle = claw_goal_fetcher_->bottom_angle;
58 goal_message->separation_angle = claw_goal_fetcher_->separation_angle;
59 goal_message->intake = 0.0;
60 goal_message->centering = 0.0;
61
62 if (!goal_message.Send()) {
63 LOG(WARNING, "sending claw goal failed\n");
64 return false;
65 }
66 }
67 return true;
68}
69
Brian Silverman17f503e2015-08-02 18:17:18 -070070bool ShootActor::RunAction(const double&) {
71 InnerRunAction();
72
73 // Now do our 'finally' block and make sure that we aren't requesting shots
74 // continually.
Austin Schuh493f7af2019-06-29 18:42:12 -070075 shooter_goal_fetcher_.Fetch();
76 if (shooter_goal_fetcher_.get() == nullptr) {
Brian Silverman17f503e2015-08-02 18:17:18 -070077 return true;
78 }
Austin Schuh493f7af2019-06-29 18:42:12 -070079 auto goal_message = shooter_goal_sender_.MakeMessage();
80 goal_message->shot_power = shooter_goal_fetcher_->shot_power;
81 goal_message->shot_requested = false;
82 goal_message->unload_requested = false;
83 goal_message->load_requested = false;
84 if (!goal_message.Send()) {
Brian Silverman17f503e2015-08-02 18:17:18 -070085 LOG(WARNING, "sending shooter goal failed\n");
86 return false;
87 }
88
89 LOG(INFO, "finished\n");
90 return true;
91}
92
93void ShootActor::InnerRunAction() {
94 LOG(INFO, "Shooting at the current angle and power.\n");
95
96 // wait for claw to be ready
97 if (WaitUntil(::std::bind(&ShootActor::DoneSetupShot, this))) {
98 return;
99 }
100
101 if (!IntakeOff()) return;
102
103 // Make sure that we have the latest shooter status.
Austin Schuh493f7af2019-06-29 18:42:12 -0700104 shooter_status_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700105 // Get the number of shots fired up to this point. This should not be updated
106 // again for another few cycles.
Austin Schuh493f7af2019-06-29 18:42:12 -0700107 previous_shots_ = shooter_status_fetcher_->shots;
Brian Silverman17f503e2015-08-02 18:17:18 -0700108 // Shoot!
Austin Schuh493f7af2019-06-29 18:42:12 -0700109 shooter_goal_fetcher_.Fetch();
110 {
111 auto goal_message = shooter_goal_sender_.MakeMessage();
112 goal_message->shot_power = shooter_goal_fetcher_->shot_power;
113 goal_message->shot_requested = true;
114 goal_message->unload_requested = false;
115 goal_message->load_requested = false;
116 if (!goal_message.Send()) {
117 LOG(WARNING, "sending shooter goal failed\n");
118 return;
119 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700120 }
121
122 // wait for record of shot having been fired
123 if (WaitUntil(::std::bind(&ShootActor::DoneShot, this))) return;
124
125 if (!IntakeOff()) return;
126}
127
Austin Schuhb2461f42019-06-29 18:17:06 -0700128bool ShootActor::ClawIsReady() {
129 claw_goal_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700130
Austin Schuhb2461f42019-06-29 18:17:06 -0700131 bool ans = claw_status_fetcher_->zeroed &&
132 (::std::abs(claw_status_fetcher_->bottom_velocity) < 0.5) &&
133 (::std::abs(claw_status_fetcher_->bottom -
134 claw_goal_fetcher_->bottom_angle) < 0.10) &&
135 (::std::abs(claw_status_fetcher_->separation -
136 claw_goal_fetcher_->separation_angle) < 0.4);
Brian Silverman17f503e2015-08-02 18:17:18 -0700137 LOG(DEBUG, "Claw is %sready zeroed %d bottom_velocity %f bottom %f sep %f\n",
Austin Schuhb2461f42019-06-29 18:17:06 -0700138 ans ? "" : "not ", claw_status_fetcher_->zeroed,
139 ::std::abs(claw_status_fetcher_->bottom_velocity),
140 ::std::abs(claw_status_fetcher_->bottom -
141 claw_goal_fetcher_->bottom_angle),
142 ::std::abs(claw_status_fetcher_->separation -
143 claw_goal_fetcher_->separation_angle));
Brian Silverman17f503e2015-08-02 18:17:18 -0700144 return ans;
145}
146
Austin Schuh493f7af2019-06-29 18:42:12 -0700147bool ShootActor::ShooterIsReady() {
148 shooter_goal_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700149
150 LOG(DEBUG, "Power error is %f - %f -> %f, ready %d\n",
Austin Schuh493f7af2019-06-29 18:42:12 -0700151 shooter_status_fetcher_->hard_stop_power,
152 shooter_goal_fetcher_->shot_power,
153 ::std::abs(shooter_status_fetcher_->hard_stop_power -
154 shooter_goal_fetcher_->shot_power),
155 shooter_status_fetcher_->ready);
156 return (::std::abs(shooter_status_fetcher_->hard_stop_power -
157 shooter_goal_fetcher_->shot_power) < 1.0) &&
158 shooter_status_fetcher_->ready;
Brian Silverman17f503e2015-08-02 18:17:18 -0700159}
160
161bool ShootActor::DoneSetupShot() {
Austin Schuh493f7af2019-06-29 18:42:12 -0700162 shooter_status_fetcher_.Fetch();
Austin Schuhb2461f42019-06-29 18:17:06 -0700163 claw_status_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700164 // Make sure that both the shooter and claw have reached the necessary
165 // states.
166 if (ShooterIsReady() && ClawIsReady()) {
167 LOG(INFO, "Claw and Shooter ready for shooting.\n");
168 return true;
169 }
170
171 return false;
172}
173
174bool ShootActor::DonePreShotOpen() {
Austin Schuhb2461f42019-06-29 18:17:06 -0700175 claw_status_fetcher_.Fetch();
176 if (claw_status_fetcher_->separation > kClawShootingSeparation) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700177 LOG(INFO, "Opened up enough to shoot.\n");
178 return true;
179 }
180 return false;
181}
182
183bool ShootActor::DoneShot() {
Austin Schuh493f7af2019-06-29 18:42:12 -0700184 shooter_status_fetcher_.Fetch();
185 if (shooter_status_fetcher_.get() &&
186 shooter_status_fetcher_->shots > previous_shots_) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700187 LOG(INFO, "Shot succeeded!\n");
188 return true;
189 }
190 return false;
191}
192
Brian Silverman17f503e2015-08-02 18:17:18 -0700193} // namespace actors
Brian Silvermanb601d892015-12-20 18:24:38 -0500194} // namespace y2014