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" |
Philipp Schrader | 996a2a2 | 2017-02-22 05:02:48 +0000 | [diff] [blame] | 12 | #include "y2017/control_loops/drivetrain/drivetrain_base.h" |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 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 { |
Philipp Schrader | 85fca55 | 2017-03-05 00:30:50 +0000 | [diff] [blame] | 22 | |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 23 | double DoubleSeconds(monotonic_clock::duration duration) { |
| 24 | return ::std::chrono::duration_cast<::std::chrono::duration<double>>(duration) |
| 25 | .count(); |
| 26 | } |
Philipp Schrader | 85fca55 | 2017-03-05 00:30:50 +0000 | [diff] [blame] | 27 | |
| 28 | const ProfileParameters kSlowDrive = {0.8, 2.5}; |
| 29 | const ProfileParameters kSlowTurn = {0.8, 3.0}; |
| 30 | |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 31 | } // namespace |
| 32 | |
Philipp Schrader | 996a2a2 | 2017-02-22 05:02:48 +0000 | [diff] [blame] | 33 | AutonomousActor::AutonomousActor( |
| 34 | ::frc971::autonomous::AutonomousActionQueueGroup *s) |
| 35 | : frc971::autonomous::BaseAutonomousActor( |
| 36 | s, control_loops::drivetrain::GetDrivetrainConfig()) {} |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 37 | |
Philipp Schrader | 996a2a2 | 2017-02-22 05:02:48 +0000 | [diff] [blame] | 38 | bool AutonomousActor::RunAction( |
| 39 | const ::frc971::autonomous::AutonomousActionParams ¶ms) { |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 40 | monotonic_clock::time_point start_time = monotonic_clock::now(); |
| 41 | LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode); |
| 42 | |
| 43 | switch (params.mode) { |
| 44 | case 0: |
Philipp Schrader | 85fca55 | 2017-03-05 00:30:50 +0000 | [diff] [blame] | 45 | // Test case autonomous mode. |
| 46 | // Drives forward 1.0 meters and then turns 180 degrees. |
| 47 | StartDrive(1.1, 0.0, kSlowDrive, kSlowTurn); |
| 48 | if (!WaitForDriveNear(1.0, 0.0)) return true; |
| 49 | StartDrive(0.0, M_PI / 2, kSlowDrive, kSlowTurn); |
| 50 | if (!WaitForDriveDone()) return true; |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 51 | break; |
| 52 | |
| 53 | default: |
| 54 | LOG(ERROR, "Invalid auto mode %d\n", params.mode); |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time)); |
| 59 | |
| 60 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 61 | ::std::chrono::milliseconds(5) / 2); |
| 62 | |
| 63 | while (!ShouldCancel()) { |
| 64 | phased_loop.SleepUntilNext(); |
| 65 | } |
| 66 | LOG(DEBUG, "Done running\n"); |
| 67 | |
| 68 | return true; |
| 69 | } |
| 70 | |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 71 | } // namespace actors |
| 72 | } // namespace y2017 |