blob: a70d6928360367401e9280c5515d0d1d08bdf207 [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#include "y2014/actors/shoot_actor.h"
2
3#include <functional>
4
5#include "aos/common/logging/logging.h"
6
7#include "y2014/control_loops/shooter/shooter.q.h"
8#include "y2014/control_loops/claw/claw.q.h"
9#include "y2014/constants.h"
10#include "y2014/control_loops/drivetrain/drivetrain.q.h"
11
Brian Silvermanb601d892015-12-20 18:24:38 -050012namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070013namespace actors {
14namespace {
15
16bool IntakeOff() {
17 control_loops::claw_queue.goal.FetchLatest();
18 if (!control_loops::claw_queue.goal.get()) {
19 LOG(WARNING, "no claw goal\n");
20 // If it doesn't have a goal, then the intake isn't on so we don't have to
21 // turn it off.
22 return true;
23 } else {
24 if (!control_loops::claw_queue.goal.MakeWithBuilder()
25 .bottom_angle(control_loops::claw_queue.goal->bottom_angle)
26 .separation_angle(control_loops::claw_queue.goal->separation_angle)
27 .intake(0.0)
28 .centering(0.0)
29 .Send()) {
30 LOG(WARNING, "sending claw goal failed\n");
31 return false;
32 }
33 }
34 return true;
35}
36
37} // namespace
38
39constexpr double ShootActor::kOffsetRadians;
40constexpr double ShootActor::kClawShootingSeparation;
41constexpr double ShootActor::kClawShootingSeparationGoal;
42
43ShootActor::ShootActor(actors::ShootActionQueueGroup* s)
44 : ::aos::common::actions::ActorBase<actors::ShootActionQueueGroup>(s) {}
45
46double ShootActor::SpeedToAngleOffset(double speed) {
Brian Silvermanb601d892015-12-20 18:24:38 -050047 const constants::Values& values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -070048 // scale speed to a [0.0-1.0] on something close to the max
49 return (speed / values.drivetrain_max_speed) * kOffsetRadians;
50}
51
52bool ShootActor::RunAction(const double&) {
53 InnerRunAction();
54
55 // Now do our 'finally' block and make sure that we aren't requesting shots
56 // continually.
57 control_loops::shooter_queue.goal.FetchLatest();
58 if (control_loops::shooter_queue.goal.get() == nullptr) {
59 return true;
60 }
61 if (!control_loops::shooter_queue.goal.MakeWithBuilder()
62 .shot_power(control_loops::shooter_queue.goal->shot_power)
63 .shot_requested(false)
64 .unload_requested(false)
65 .load_requested(false)
66 .Send()) {
67 LOG(WARNING, "sending shooter goal failed\n");
68 return false;
69 }
70
71 LOG(INFO, "finished\n");
72 return true;
73}
74
75void ShootActor::InnerRunAction() {
76 LOG(INFO, "Shooting at the current angle and power.\n");
77
78 // wait for claw to be ready
79 if (WaitUntil(::std::bind(&ShootActor::DoneSetupShot, this))) {
80 return;
81 }
82
83 if (!IntakeOff()) return;
84
85 // Make sure that we have the latest shooter status.
86 control_loops::shooter_queue.status.FetchLatest();
87 // Get the number of shots fired up to this point. This should not be updated
88 // again for another few cycles.
89 previous_shots_ = control_loops::shooter_queue.status->shots;
90 // Shoot!
91 if (!control_loops::shooter_queue.goal.MakeWithBuilder()
92 .shot_power(control_loops::shooter_queue.goal->shot_power)
93 .shot_requested(true)
94 .unload_requested(false)
95 .load_requested(false)
96 .Send()) {
97 LOG(WARNING, "sending shooter goal failed\n");
98 return;
99 }
100
101 // wait for record of shot having been fired
102 if (WaitUntil(::std::bind(&ShootActor::DoneShot, this))) return;
103
104 if (!IntakeOff()) return;
105}
106
107bool ClawIsReady() {
108 control_loops::claw_queue.goal.FetchLatest();
109
110 bool ans =
111 control_loops::claw_queue.status->zeroed &&
112 (::std::abs(control_loops::claw_queue.status->bottom_velocity) < 0.5) &&
113 (::std::abs(control_loops::claw_queue.status->bottom -
114 control_loops::claw_queue.goal->bottom_angle) < 0.10) &&
115 (::std::abs(control_loops::claw_queue.status->separation -
116 control_loops::claw_queue.goal->separation_angle) < 0.4);
117 LOG(DEBUG, "Claw is %sready zeroed %d bottom_velocity %f bottom %f sep %f\n",
118 ans ? "" : "not ", control_loops::claw_queue.status->zeroed,
119 ::std::abs(control_loops::claw_queue.status->bottom_velocity),
120 ::std::abs(control_loops::claw_queue.status->bottom -
121 control_loops::claw_queue.goal->bottom_angle),
122 ::std::abs(control_loops::claw_queue.status->separation -
123 control_loops::claw_queue.goal->separation_angle));
124 return ans;
125}
126
127bool ShooterIsReady() {
128 control_loops::shooter_queue.goal.FetchLatest();
129
130 LOG(DEBUG, "Power error is %f - %f -> %f, ready %d\n",
131 control_loops::shooter_queue.status->hard_stop_power,
132 control_loops::shooter_queue.goal->shot_power,
133 ::std::abs(control_loops::shooter_queue.status->hard_stop_power -
134 control_loops::shooter_queue.goal->shot_power),
135 control_loops::shooter_queue.status->ready);
136 return (::std::abs(control_loops::shooter_queue.status->hard_stop_power -
137 control_loops::shooter_queue.goal->shot_power) < 1.0) &&
138 control_loops::shooter_queue.status->ready;
139}
140
141bool ShootActor::DoneSetupShot() {
142 control_loops::shooter_queue.status.FetchAnother();
143 control_loops::claw_queue.status.FetchAnother();
144 // Make sure that both the shooter and claw have reached the necessary
145 // states.
146 if (ShooterIsReady() && ClawIsReady()) {
147 LOG(INFO, "Claw and Shooter ready for shooting.\n");
148 return true;
149 }
150
151 return false;
152}
153
154bool ShootActor::DonePreShotOpen() {
155 control_loops::claw_queue.status.FetchAnother();
156 if (control_loops::claw_queue.status->separation > kClawShootingSeparation) {
157 LOG(INFO, "Opened up enough to shoot.\n");
158 return true;
159 }
160 return false;
161}
162
163bool ShootActor::DoneShot() {
164 control_loops::shooter_queue.status.FetchAnother();
165 if (control_loops::shooter_queue.status->shots > previous_shots_) {
166 LOG(INFO, "Shot succeeded!\n");
167 return true;
168 }
169 return false;
170}
171
172::std::unique_ptr<ShootAction> MakeShootAction() {
173 return ::std::unique_ptr<ShootAction>(
Brian Silvermanb601d892015-12-20 18:24:38 -0500174 new ShootAction(&::y2014::actors::shoot_action, 0.0));
Brian Silverman17f503e2015-08-02 18:17:18 -0700175}
176
177} // namespace actors
Brian Silvermanb601d892015-12-20 18:24:38 -0500178} // namespace y2014