Ben Fredrickson | 8c6868c | 2014-03-01 07:52:53 +0000 | [diff] [blame] | 1 | #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/constants.h" |
| 8 | |
| 9 | namespace frc971 { |
| 10 | namespace actions { |
| 11 | |
| 12 | template <class T> |
| 13 | class ActionBase { |
| 14 | public: |
| 15 | ActionBase (T* acq) |
| 16 | : action_q_(acq) {} |
| 17 | |
| 18 | virtual void RunAction() = 0; |
| 19 | |
| 20 | void Run () { |
| 21 | action_q_->goal.FetchLatest(); |
| 22 | while (!action_q_->goal.get()) { |
| 23 | action_q_->goal.FetchNextBlocking(); |
| 24 | } |
| 25 | |
| 26 | if (!action_q_->status.MakeWithBuilder().running(false) |
| 27 | .Send()) { |
| 28 | LOG(ERROR, "Failed to send the status.\n"); |
| 29 | } |
| 30 | while (true) { |
| 31 | while (!action_q_->goal->run) { |
| 32 | LOG(INFO, "Waiting for an action request.\n"); |
| 33 | action_q_->goal.FetchNextBlocking(); |
| 34 | } |
| 35 | LOG(INFO, "Starting action\n"); |
| 36 | if (!action_q_->status.MakeWithBuilder().running( |
| 37 | true).Send()) { |
| 38 | LOG(ERROR, "Failed to send the status.\n"); |
| 39 | } |
| 40 | RunAction(); |
| 41 | if (!action_q_->status.MakeWithBuilder().running( |
| 42 | false).Send()) { |
| 43 | LOG(ERROR, "Failed to send the status.\n"); |
| 44 | } |
| 45 | |
| 46 | while (action_q_->goal->run) { |
| 47 | LOG(INFO, "Waiting for the action to be stopped.\n"); |
| 48 | action_q_->goal.FetchNextBlocking(); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | // Returns true if the action should be canceled. |
| 53 | bool ShouldCancel() { |
| 54 | action_q_->goal.FetchLatest(); |
| 55 | bool ans = !action_q_->goal->run; |
| 56 | if (ans) { |
| 57 | LOG(INFO, "Time to exit auto mode\n"); |
| 58 | } |
| 59 | return ans; |
| 60 | } |
| 61 | |
| 62 | protected: |
| 63 | T* action_q_; |
| 64 | }; |
| 65 | |
| 66 | } // namespace actions |
| 67 | } // namespace frc971 |
| 68 | |