blob: 7fb4c05408df8f0ba87b221051e69a54d5cda490 [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()) {
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 {
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()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070063 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhb2461f42019-06-29 18:17:06 -070064 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()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070085 AOS_LOG(WARNING, "sending shooter goal failed\n");
Brian Silverman17f503e2015-08-02 18:17:18 -070086 return false;
87 }
88
Austin Schuhf257f3c2019-10-27 21:00:43 -070089 AOS_LOG(INFO, "finished\n");
Brian Silverman17f503e2015-08-02 18:17:18 -070090 return true;
91}
92
93void ShootActor::InnerRunAction() {
Austin Schuhf257f3c2019-10-27 21:00:43 -070094 AOS_LOG(INFO, "Shooting at the current angle and power.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -070095
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()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700117 AOS_LOG(WARNING, "sending shooter goal failed\n");
Austin Schuh493f7af2019-06-29 18:42:12 -0700118 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);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700137 AOS_LOG(DEBUG,
138 "Claw is %sready zeroed %d bottom_velocity %f bottom %f sep %f\n",
139 ans ? "" : "not ", claw_status_fetcher_->zeroed,
140 ::std::abs(claw_status_fetcher_->bottom_velocity),
141 ::std::abs(claw_status_fetcher_->bottom -
142 claw_goal_fetcher_->bottom_angle),
143 ::std::abs(claw_status_fetcher_->separation -
144 claw_goal_fetcher_->separation_angle));
Brian Silverman17f503e2015-08-02 18:17:18 -0700145 return ans;
146}
147
Austin Schuh493f7af2019-06-29 18:42:12 -0700148bool ShootActor::ShooterIsReady() {
149 shooter_goal_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700150
Austin Schuhf257f3c2019-10-27 21:00:43 -0700151 AOS_LOG(DEBUG, "Power error is %f - %f -> %f, ready %d\n",
152 shooter_status_fetcher_->hard_stop_power,
153 shooter_goal_fetcher_->shot_power,
154 ::std::abs(shooter_status_fetcher_->hard_stop_power -
155 shooter_goal_fetcher_->shot_power),
156 shooter_status_fetcher_->ready);
Austin Schuh493f7af2019-06-29 18:42:12 -0700157 return (::std::abs(shooter_status_fetcher_->hard_stop_power -
158 shooter_goal_fetcher_->shot_power) < 1.0) &&
159 shooter_status_fetcher_->ready;
Brian Silverman17f503e2015-08-02 18:17:18 -0700160}
161
162bool ShootActor::DoneSetupShot() {
Austin Schuh493f7af2019-06-29 18:42:12 -0700163 shooter_status_fetcher_.Fetch();
Austin Schuhb2461f42019-06-29 18:17:06 -0700164 claw_status_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700165 // Make sure that both the shooter and claw have reached the necessary
166 // states.
167 if (ShooterIsReady() && ClawIsReady()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700168 AOS_LOG(INFO, "Claw and Shooter ready for shooting.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700169 return true;
170 }
171
172 return false;
173}
174
175bool ShootActor::DonePreShotOpen() {
Austin Schuhb2461f42019-06-29 18:17:06 -0700176 claw_status_fetcher_.Fetch();
177 if (claw_status_fetcher_->separation > kClawShootingSeparation) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700178 AOS_LOG(INFO, "Opened up enough to shoot.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700179 return true;
180 }
181 return false;
182}
183
184bool ShootActor::DoneShot() {
Austin Schuh493f7af2019-06-29 18:42:12 -0700185 shooter_status_fetcher_.Fetch();
186 if (shooter_status_fetcher_.get() &&
187 shooter_status_fetcher_->shots > previous_shots_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700188 AOS_LOG(INFO, "Shot succeeded!\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700189 return true;
190 }
191 return false;
192}
193
Brian Silverman17f503e2015-08-02 18:17:18 -0700194} // namespace actors
Brian Silvermanb601d892015-12-20 18:24:38 -0500195} // namespace y2014