blob: 118736b5063eddce1069c0be676db6d808a62623 [file] [log] [blame]
James Kuszmaul4abaf482014-02-26 21:16:35 -08001#include "stdio.h"
2
3#include "aos/common/control_loop/Timing.h"
4#include "aos/common/logging/logging.h"
5#include "aos/common/network/team_number.h"
6
7#include "frc971/actions/shoot_action.q.h"
8#include "frc971/constants.h"
9
10namespace frc971 {
11namespace actions {
12
13class ShootAction {
14 public:
15 void Run() {
16 ::frc971::actions::shoot_action.FetchLatest();
17 while (!::frc971::actions::shoot_action.get()) {
18 ::frc971::actions::shoot_action.FetchNextBlocking();
19 }
20
21 while (true) {
22 while (!::frc971::actions::shoot_action->run) {
23 LOG(INFO, "Waiting for an action request.\n");
24 ::frc971::actions::shoot_action.FetchNextBlocking();
25 }
26 LOG(INFO, "Starting action\n");
27 RunAction();
28
29 while (::frc971::actions::shoot_action->run) {
30 LOG(INFO, "Waiting for the action to be stopped.\n");
31 ::frc971::actions::shoot_action.FetchNextBlocking();
32 }
33 }
34 }
35
36 void RunAction();
37
38 protected:
39 // Returns true if the action should be canceled.
40 bool ShouldCancel() {
41 shoot_action.FetchLatest();
42 bool ans = !::frc971::actions::shoot_action->run;
43 if (ans) {
44 LOG(INFO, "Time to exit auto mode\n");
45 }
46 return ans;
47 }
48};
49
50void ShootAction::RunAction() {
51 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder().shot_power(
52 shoot_action->shot_power).shot_requested(false)
53 .unload_requested(false).load_requested(false).Send()) {
54 LOG(ERROR, "Failed to send the shoot action\n");
55 return;
56 }
57
58 if (!control_loops::claw_queue_group.goal.MakeWithBuilder().bottom_angle(
59 shoot_action->shot_angle).separation_angle(0.0).intake(2.0)
60 .centering(1.0).Send()) {
61 LOG(WARNING, "sending claw goal failed\n");
62 return;
63 }
64
65 // Make sure we have the latest statuses.
66 control_loops::shooter_queue_group.status.FetchLatest();
67 control_loops::claw_queue_group.status.FetchLatest();
68 while (true) {
69 if (control_loops::shooter_queue_group.status->ready &&
70 control_loops::claw_queue_group.status->done) {
71 LOG(INFO, "Claw and Shooter ready for shooting.\n");
72 break;
73 }
74
75 // Wait until we have a new status.
76 control_loops::shooter_queue_group.status.FetchNextBlocking();
77 control_loops::claw_queue_group.status.FetchNextBlocking();
78
79 if (ShouldCancel()) return;
80 }
81
82 // Open up the claw in preparation for shooting.
83 if (!control_loops::claw_queue_group.goal.MakeWithBuilder().bottom_angle(
84 shoot_action->shot_angle).separation_angle(k2).intake(2.0)
85 .centering(1.0).Send()) {
86 LOG(WARNING, "sending claw goal failed\n");
87 return;
88 }
89
90 if (ShouldCancel()) return;
91
92 // Make sure we have the latest statuses.
93 control_loops::claw_queue_group.status.FetchLatest();
94 while (true) {
95 if (control_loops::claw_queue_group.status->separation > k1) {
96 LOG(INFO, "Opened up enough to shoot.\n");
97 break;
98 }
99
100 // Wait until we have a new status.
101 control_loops::claw_queue_group.status.FetchNextBlocking();
102
103 if (ShouldCancel()) return;
104 }
105}
106
107} // namespace actions
108} // namespace frc971