blob: 5520c497b73c9e3ef0fd3973b00b9515051d4ac1 [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
7#include "y2014/control_loops/shooter/shooter.q.h"
8#include "y2014/control_loops/claw/claw.q.h"
9#include "y2014/constants.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000010#include "frc971/control_loops/drivetrain/drivetrain.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 {
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
Austin Schuh1bf8a212019-05-26 22:13:14 -070043ShootActor::ShootActor(::aos::EventLoop *event_loop)
44 : ::aos::common::actions::ActorBase<actors::ShootActionQueueGroup>(
45 event_loop, ".y2014.actors.shoot_action") {}
Brian Silverman17f503e2015-08-02 18:17:18 -070046
47double ShootActor::SpeedToAngleOffset(double speed) {
Brian Silvermanb601d892015-12-20 18:24:38 -050048 const constants::Values& values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -070049 // scale speed to a [0.0-1.0] on something close to the max
50 return (speed / values.drivetrain_max_speed) * kOffsetRadians;
51}
52
53bool ShootActor::RunAction(const double&) {
54 InnerRunAction();
55
56 // Now do our 'finally' block and make sure that we aren't requesting shots
57 // continually.
58 control_loops::shooter_queue.goal.FetchLatest();
59 if (control_loops::shooter_queue.goal.get() == nullptr) {
60 return true;
61 }
62 if (!control_loops::shooter_queue.goal.MakeWithBuilder()
63 .shot_power(control_loops::shooter_queue.goal->shot_power)
64 .shot_requested(false)
65 .unload_requested(false)
66 .load_requested(false)
67 .Send()) {
68 LOG(WARNING, "sending shooter goal failed\n");
69 return false;
70 }
71
72 LOG(INFO, "finished\n");
73 return true;
74}
75
76void ShootActor::InnerRunAction() {
77 LOG(INFO, "Shooting at the current angle and power.\n");
78
79 // wait for claw to be ready
80 if (WaitUntil(::std::bind(&ShootActor::DoneSetupShot, this))) {
81 return;
82 }
83
84 if (!IntakeOff()) return;
85
86 // Make sure that we have the latest shooter status.
87 control_loops::shooter_queue.status.FetchLatest();
88 // Get the number of shots fired up to this point. This should not be updated
89 // again for another few cycles.
90 previous_shots_ = control_loops::shooter_queue.status->shots;
91 // Shoot!
92 if (!control_loops::shooter_queue.goal.MakeWithBuilder()
93 .shot_power(control_loops::shooter_queue.goal->shot_power)
94 .shot_requested(true)
95 .unload_requested(false)
96 .load_requested(false)
97 .Send()) {
98 LOG(WARNING, "sending shooter goal failed\n");
99 return;
100 }
101
102 // wait for record of shot having been fired
103 if (WaitUntil(::std::bind(&ShootActor::DoneShot, this))) return;
104
105 if (!IntakeOff()) return;
106}
107
108bool ClawIsReady() {
109 control_loops::claw_queue.goal.FetchLatest();
110
111 bool ans =
112 control_loops::claw_queue.status->zeroed &&
113 (::std::abs(control_loops::claw_queue.status->bottom_velocity) < 0.5) &&
114 (::std::abs(control_loops::claw_queue.status->bottom -
115 control_loops::claw_queue.goal->bottom_angle) < 0.10) &&
116 (::std::abs(control_loops::claw_queue.status->separation -
117 control_loops::claw_queue.goal->separation_angle) < 0.4);
118 LOG(DEBUG, "Claw is %sready zeroed %d bottom_velocity %f bottom %f sep %f\n",
119 ans ? "" : "not ", control_loops::claw_queue.status->zeroed,
120 ::std::abs(control_loops::claw_queue.status->bottom_velocity),
121 ::std::abs(control_loops::claw_queue.status->bottom -
122 control_loops::claw_queue.goal->bottom_angle),
123 ::std::abs(control_loops::claw_queue.status->separation -
124 control_loops::claw_queue.goal->separation_angle));
125 return ans;
126}
127
128bool ShooterIsReady() {
129 control_loops::shooter_queue.goal.FetchLatest();
130
131 LOG(DEBUG, "Power error is %f - %f -> %f, ready %d\n",
132 control_loops::shooter_queue.status->hard_stop_power,
133 control_loops::shooter_queue.goal->shot_power,
134 ::std::abs(control_loops::shooter_queue.status->hard_stop_power -
135 control_loops::shooter_queue.goal->shot_power),
136 control_loops::shooter_queue.status->ready);
137 return (::std::abs(control_loops::shooter_queue.status->hard_stop_power -
138 control_loops::shooter_queue.goal->shot_power) < 1.0) &&
139 control_loops::shooter_queue.status->ready;
140}
141
142bool ShootActor::DoneSetupShot() {
143 control_loops::shooter_queue.status.FetchAnother();
144 control_loops::claw_queue.status.FetchAnother();
145 // Make sure that both the shooter and claw have reached the necessary
146 // states.
147 if (ShooterIsReady() && ClawIsReady()) {
148 LOG(INFO, "Claw and Shooter ready for shooting.\n");
149 return true;
150 }
151
152 return false;
153}
154
155bool ShootActor::DonePreShotOpen() {
156 control_loops::claw_queue.status.FetchAnother();
157 if (control_loops::claw_queue.status->separation > kClawShootingSeparation) {
158 LOG(INFO, "Opened up enough to shoot.\n");
159 return true;
160 }
161 return false;
162}
163
164bool ShootActor::DoneShot() {
165 control_loops::shooter_queue.status.FetchAnother();
166 if (control_loops::shooter_queue.status->shots > previous_shots_) {
167 LOG(INFO, "Shot succeeded!\n");
168 return true;
169 }
170 return false;
171}
172
Brian Silverman17f503e2015-08-02 18:17:18 -0700173} // namespace actors
Brian Silvermanb601d892015-12-20 18:24:38 -0500174} // namespace y2014