James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 1 | #include "aos/common/control_loop/Timing.h" |
| 2 | #include "aos/common/logging/logging.h" |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 3 | |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 4 | #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 Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 7 | #include "frc971/constants.h" |
| 8 | |
| 9 | namespace frc971 { |
| 10 | namespace actions { |
| 11 | |
| 12 | class ShootAction { |
| 13 | public: |
| 14 | void Run() { |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 15 | ::frc971::actions::shoot_action.goal.FetchLatest(); |
| 16 | while (!::frc971::actions::shoot_action.goal.get()) { |
| 17 | ::frc971::actions::shoot_action.goal.FetchNextBlocking(); |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 18 | } |
| 19 | |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 20 | if (!::frc971::actions::shoot_action.status.MakeWithBuilder().running(false) |
| 21 | .Send()) { |
| 22 | LOG(ERROR, "Failed to send the status.\n"); |
| 23 | } |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 24 | while (true) { |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 25 | while (!::frc971::actions::shoot_action.goal->run) { |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 26 | LOG(INFO, "Waiting for an action request.\n"); |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 27 | ::frc971::actions::shoot_action.goal.FetchNextBlocking(); |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 28 | } |
| 29 | LOG(INFO, "Starting action\n"); |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 30 | if (!::frc971::actions::shoot_action.status.MakeWithBuilder().running( |
| 31 | true).Send()) { |
| 32 | LOG(ERROR, "Failed to send the status.\n"); |
| 33 | } |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 34 | RunAction(); |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 35 | if (!::frc971::actions::shoot_action.status.MakeWithBuilder().running( |
| 36 | false).Send()) { |
| 37 | LOG(ERROR, "Failed to send the status.\n"); |
| 38 | } |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 39 | |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 40 | while (::frc971::actions::shoot_action.goal->run) { |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 41 | LOG(INFO, "Waiting for the action to be stopped.\n"); |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 42 | ::frc971::actions::shoot_action.goal.FetchNextBlocking(); |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 47 | // Actually execute the action of moving the claw and shooter into position |
| 48 | // and actually firing them. |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 49 | void RunAction(); |
| 50 | |
| 51 | protected: |
| 52 | // Returns true if the action should be canceled. |
| 53 | bool ShouldCancel() { |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 54 | shoot_action.goal.FetchLatest(); |
| 55 | bool ans = !::frc971::actions::shoot_action.goal->run; |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 56 | if (ans) { |
| 57 | LOG(INFO, "Time to exit auto mode\n"); |
| 58 | } |
| 59 | return ans; |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | void ShootAction::RunAction() { |
| 64 | if (!control_loops::shooter_queue_group.goal.MakeWithBuilder().shot_power( |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 65 | shoot_action.goal->shot_power).shot_requested(false) |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 66 | .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 Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 72 | shoot_action.goal->shot_angle).separation_angle(0.0).intake(2.0) |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 73 | .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 Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 82 | // Make sure that both the shooter and claw have reached the necessary |
| 83 | // states. |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 84 | 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 Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 87 | // TODO(james): Get realer numbers for shooter_action. |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 88 | 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 Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 98 | const frc971::constants::Values &values = frc971::constants::GetValues(); |
| 99 | |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 100 | // Open up the claw in preparation for shooting. |
| 101 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder().bottom_angle( |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 102 | shoot_action.goal->shot_angle) |
| 103 | .separation_angle(values.shooter_action.claw_separation_goal) |
| 104 | .intake(2.0).centering(1.0).Send()) { |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 105 | LOG(WARNING, "sending claw goal failed\n"); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | if (ShouldCancel()) return; |
| 110 | |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 111 | // Make sure we have the latest status. |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 112 | control_loops::claw_queue_group.status.FetchLatest(); |
| 113 | while (true) { |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 114 | if (control_loops::claw_queue_group.status->separation > values.shooter_action.claw_shooting_separation) { |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 115 | 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 Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame^] | 124 | |
| 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 Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } // namespace actions |
| 156 | } // namespace frc971 |