Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame^] | 1 | #include "y2017/actors/autonomous_actor.h" |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | |
| 5 | #include <chrono> |
| 6 | #include <cmath> |
| 7 | |
| 8 | #include "aos/common/util/phased_loop.h" |
| 9 | #include "aos/common/logging/logging.h" |
| 10 | |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 12 | #include "y2017/actors/autonomous_action.q.h" |
| 13 | |
| 14 | namespace y2017 { |
| 15 | namespace actors { |
| 16 | using ::frc971::control_loops::drivetrain_queue; |
| 17 | using ::aos::monotonic_clock; |
| 18 | namespace chrono = ::std::chrono; |
| 19 | namespace this_thread = ::std::this_thread; |
| 20 | |
| 21 | namespace { |
| 22 | double DoubleSeconds(monotonic_clock::duration duration) { |
| 23 | return ::std::chrono::duration_cast<::std::chrono::duration<double>>(duration) |
| 24 | .count(); |
| 25 | } |
| 26 | } // namespace |
| 27 | |
| 28 | AutonomousActor::AutonomousActor(actors::AutonomousActionQueueGroup *s) |
| 29 | : aos::common::actions::ActorBase<actors::AutonomousActionQueueGroup>(s) {} |
| 30 | |
| 31 | void AutonomousActor::WaitUntilDoneOrCanceled( |
| 32 | ::std::unique_ptr<aos::common::actions::Action> action) { |
| 33 | if (!action) { |
| 34 | LOG(ERROR, "No action, not waiting\n"); |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 39 | ::std::chrono::milliseconds(5) / 2); |
| 40 | while (true) { |
| 41 | // Poll the running bit and see if we should cancel. |
| 42 | phased_loop.SleepUntilNext(); |
| 43 | if (!action->Running() || ShouldCancel()) { |
| 44 | return; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | bool AutonomousActor::RunAction(const actors::AutonomousActionParams ¶ms) { |
| 50 | monotonic_clock::time_point start_time = monotonic_clock::now(); |
| 51 | LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode); |
| 52 | |
| 53 | switch (params.mode) { |
| 54 | case 0: |
| 55 | break; |
| 56 | |
| 57 | default: |
| 58 | LOG(ERROR, "Invalid auto mode %d\n", params.mode); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time)); |
| 63 | |
| 64 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 65 | ::std::chrono::milliseconds(5) / 2); |
| 66 | |
| 67 | while (!ShouldCancel()) { |
| 68 | phased_loop.SleepUntilNext(); |
| 69 | } |
| 70 | LOG(DEBUG, "Done running\n"); |
| 71 | |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | ::std::unique_ptr<AutonomousAction> MakeAutonomousAction( |
| 76 | const ::y2017::actors::AutonomousActionParams ¶ms) { |
| 77 | return ::std::unique_ptr<AutonomousAction>( |
| 78 | new AutonomousAction(&::y2017::actors::autonomous_action, params)); |
| 79 | } |
| 80 | |
| 81 | } // namespace actors |
| 82 | } // namespace y2017 |