blob: 3fe3b76a3aaaa2a20e8f9f4019ab77094bc56679 [file] [log] [blame]
James Kuszmaul4abaf482014-02-26 21:16:35 -08001#include "aos/common/control_loop/Timing.h"
2#include "aos/common/logging/logging.h"
James Kuszmaul4abaf482014-02-26 21:16:35 -08003
James Kuszmaul9ead1de2014-02-28 21:24:39 -08004#include "frc971/autonomous/shoot_action.q.h"
5#include "frc971/control_loops/shooter/shooter.q.h"
6#include "frc971/control_loops/claw/claw.q.h"
James Kuszmaul4abaf482014-02-26 21:16:35 -08007#include "frc971/constants.h"
8
9namespace frc971 {
10namespace actions {
11
12class ShootAction {
13 public:
14 void Run() {
James Kuszmaul9ead1de2014-02-28 21:24:39 -080015 ::frc971::actions::shoot_action.goal.FetchLatest();
16 while (!::frc971::actions::shoot_action.goal.get()) {
17 ::frc971::actions::shoot_action.goal.FetchNextBlocking();
James Kuszmaul4abaf482014-02-26 21:16:35 -080018 }
19
James Kuszmaul9ead1de2014-02-28 21:24:39 -080020 if (!::frc971::actions::shoot_action.status.MakeWithBuilder().running(false)
21 .Send()) {
22 LOG(ERROR, "Failed to send the status.\n");
23 }
James Kuszmaul4abaf482014-02-26 21:16:35 -080024 while (true) {
James Kuszmaul9ead1de2014-02-28 21:24:39 -080025 while (!::frc971::actions::shoot_action.goal->run) {
James Kuszmaul4abaf482014-02-26 21:16:35 -080026 LOG(INFO, "Waiting for an action request.\n");
James Kuszmaul9ead1de2014-02-28 21:24:39 -080027 ::frc971::actions::shoot_action.goal.FetchNextBlocking();
James Kuszmaul4abaf482014-02-26 21:16:35 -080028 }
29 LOG(INFO, "Starting action\n");
James Kuszmaul9ead1de2014-02-28 21:24:39 -080030 if (!::frc971::actions::shoot_action.status.MakeWithBuilder().running(
31 true).Send()) {
32 LOG(ERROR, "Failed to send the status.\n");
33 }
James Kuszmaul4abaf482014-02-26 21:16:35 -080034 RunAction();
James Kuszmaul9ead1de2014-02-28 21:24:39 -080035 if (!::frc971::actions::shoot_action.status.MakeWithBuilder().running(
36 false).Send()) {
37 LOG(ERROR, "Failed to send the status.\n");
38 }
James Kuszmaul4abaf482014-02-26 21:16:35 -080039
James Kuszmaul9ead1de2014-02-28 21:24:39 -080040 while (::frc971::actions::shoot_action.goal->run) {
James Kuszmaul4abaf482014-02-26 21:16:35 -080041 LOG(INFO, "Waiting for the action to be stopped.\n");
James Kuszmaul9ead1de2014-02-28 21:24:39 -080042 ::frc971::actions::shoot_action.goal.FetchNextBlocking();
James Kuszmaul4abaf482014-02-26 21:16:35 -080043 }
44 }
45 }
46
James Kuszmaul9ead1de2014-02-28 21:24:39 -080047 // Actually execute the action of moving the claw and shooter into position
48 // and actually firing them.
James Kuszmaul4abaf482014-02-26 21:16:35 -080049 void RunAction();
50
51 protected:
52 // Returns true if the action should be canceled.
53 bool ShouldCancel() {
James Kuszmaul9ead1de2014-02-28 21:24:39 -080054 shoot_action.goal.FetchLatest();
55 bool ans = !::frc971::actions::shoot_action.goal->run;
James Kuszmaul4abaf482014-02-26 21:16:35 -080056 if (ans) {
57 LOG(INFO, "Time to exit auto mode\n");
58 }
59 return ans;
60 }
61};
62
63void ShootAction::RunAction() {
64 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder().shot_power(
James Kuszmaul9ead1de2014-02-28 21:24:39 -080065 shoot_action.goal->shot_power).shot_requested(false)
James Kuszmaul4abaf482014-02-26 21:16:35 -080066 .unload_requested(false).load_requested(false).Send()) {
67 LOG(ERROR, "Failed to send the shoot action\n");
68 return;
69 }
70
71 if (!control_loops::claw_queue_group.goal.MakeWithBuilder().bottom_angle(
James Kuszmaul9ead1de2014-02-28 21:24:39 -080072 shoot_action.goal->shot_angle).separation_angle(0.0).intake(2.0)
James Kuszmaul4abaf482014-02-26 21:16:35 -080073 .centering(1.0).Send()) {
74 LOG(WARNING, "sending claw goal failed\n");
75 return;
76 }
77
78 // Make sure we have the latest statuses.
79 control_loops::shooter_queue_group.status.FetchLatest();
80 control_loops::claw_queue_group.status.FetchLatest();
81 while (true) {
James Kuszmaul9ead1de2014-02-28 21:24:39 -080082 // Make sure that both the shooter and claw have reached the necessary
83 // states.
James Kuszmaul4abaf482014-02-26 21:16:35 -080084 if (control_loops::shooter_queue_group.status->ready &&
85 control_loops::claw_queue_group.status->done) {
86 LOG(INFO, "Claw and Shooter ready for shooting.\n");
James Kuszmaul9ead1de2014-02-28 21:24:39 -080087 // TODO(james): Get realer numbers for shooter_action.
James Kuszmaul4abaf482014-02-26 21:16:35 -080088 break;
89 }
90
91 // Wait until we have a new status.
92 control_loops::shooter_queue_group.status.FetchNextBlocking();
93 control_loops::claw_queue_group.status.FetchNextBlocking();
94
95 if (ShouldCancel()) return;
96 }
97
James Kuszmaul9ead1de2014-02-28 21:24:39 -080098 const frc971::constants::Values &values = frc971::constants::GetValues();
99
James Kuszmaul4abaf482014-02-26 21:16:35 -0800100 // Open up the claw in preparation for shooting.
101 if (!control_loops::claw_queue_group.goal.MakeWithBuilder().bottom_angle(
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800102 shoot_action.goal->shot_angle)
103 .separation_angle(values.shooter_action.claw_separation_goal)
104 .intake(2.0).centering(1.0).Send()) {
James Kuszmaul4abaf482014-02-26 21:16:35 -0800105 LOG(WARNING, "sending claw goal failed\n");
106 return;
107 }
108
109 if (ShouldCancel()) return;
110
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800111 // Make sure we have the latest status.
James Kuszmaul4abaf482014-02-26 21:16:35 -0800112 control_loops::claw_queue_group.status.FetchLatest();
113 while (true) {
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800114 if (control_loops::claw_queue_group.status->separation > values.shooter_action.claw_shooting_separation) {
James Kuszmaul4abaf482014-02-26 21:16:35 -0800115 LOG(INFO, "Opened up enough to shoot.\n");
116 break;
117 }
118
119 // Wait until we have a new status.
120 control_loops::claw_queue_group.status.FetchNextBlocking();
121
122 if (ShouldCancel()) return;
123 }
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800124
125 // Shoot!
126 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder().shot_power(
127 shoot_action.goal->shot_power).shot_requested(true)
128 .unload_requested(false).load_requested(false).Send()) {
129 LOG(WARNING, "sending shooter goal failed\n");
130 return;
131 }
132
133 if (ShouldCancel()) return;
134
135 // Make sure that we have the latest shooter status.
136 control_loops::shooter_queue_group.status.FetchLatest();
137 // Get the number of shots fired up to this point. This should not be updated
138 // again for another few cycles.
139 int previous_shots = control_loops::shooter_queue_group.status->shots;
140 while (true) {
141 if (control_loops::shooter_queue_group.status->shots > previous_shots) {
142 LOG(INFO, "Shot succeeded!\n");
143 break;
144 }
145
146 // Wait until we have a new status.
147 control_loops::shooter_queue_group.status.FetchNextBlocking();
148
149 if (ShouldCancel()) return;
150 }
151
152 return;
James Kuszmaul4abaf482014-02-26 21:16:35 -0800153}
154
155} // namespace actions
156} // namespace frc971