Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame^] | 1 | #include "y2018/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 "y2018/control_loops/drivetrain/drivetrain_base.h" |
| 13 | |
| 14 | namespace y2018 { |
| 15 | namespace actors { |
| 16 | using ::frc971::ProfileParameters; |
| 17 | |
| 18 | using ::frc971::control_loops::drivetrain_queue; |
| 19 | using ::aos::monotonic_clock; |
| 20 | namespace chrono = ::std::chrono; |
| 21 | namespace this_thread = ::std::this_thread; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | double DoubleSeconds(monotonic_clock::duration duration) { |
| 26 | return ::std::chrono::duration_cast<::std::chrono::duration<double>>(duration) |
| 27 | .count(); |
| 28 | } |
| 29 | |
| 30 | const ProfileParameters kDrive = {1.5, 2.0}; |
| 31 | const ProfileParameters kTurn = {3.0, 3.0}; |
| 32 | |
| 33 | } // namespace |
| 34 | |
| 35 | AutonomousActor::AutonomousActor( |
| 36 | ::frc971::autonomous::AutonomousActionQueueGroup *s) |
| 37 | : frc971::autonomous::BaseAutonomousActor( |
| 38 | s, control_loops::drivetrain::GetDrivetrainConfig()) {} |
| 39 | |
| 40 | bool AutonomousActor::RunAction( |
| 41 | const ::frc971::autonomous::AutonomousActionParams ¶ms) { |
| 42 | monotonic_clock::time_point start_time = monotonic_clock::now(); |
| 43 | LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode); |
| 44 | Reset(); |
| 45 | |
| 46 | for (int i = 0; i < 4; ++i) { |
| 47 | StartDrive(0.0, M_PI / 2.0, kDrive, kTurn); |
| 48 | if (!WaitForTurnProfileDone()) return true; |
| 49 | |
| 50 | // Drive, but get within 0.3 meters |
| 51 | StartDrive(1.0, 0.0, kDrive, kTurn); |
| 52 | if (!WaitForDriveProfileDone()) return true; |
| 53 | } |
| 54 | |
| 55 | LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time)); |
| 56 | |
| 57 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 58 | ::std::chrono::milliseconds(5) / 2); |
| 59 | |
| 60 | while (!ShouldCancel()) { |
| 61 | phased_loop.SleepUntilNext(); |
| 62 | } |
| 63 | LOG(DEBUG, "Done running\n"); |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | } // namespace actors |
| 69 | } // namespace y2018 |