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 | |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame^] | 28 | const ProfileParameters kSlowDrive = {2.0, 2.0}; |
| 29 | const ProfileParameters kSlowTurn = {3.0, 3.0}; |
Philipp Schrader | 85fca55 | 2017-03-05 00:30:50 +0000 | [diff] [blame] | 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); |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame^] | 42 | InitializeEncoders(); |
| 43 | ResetDrivetrain(); |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 44 | |
| 45 | switch (params.mode) { |
| 46 | case 0: |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame^] | 47 | default: |
| 48 | while (true) { |
| 49 | constexpr auto kDelayTime = chrono::milliseconds(1); |
| 50 | // Test case autonomous mode. |
| 51 | // Drives forward 1.0 meters and then turns 180 degrees. |
| 52 | StartDrive(1.0, 0.0, kSlowDrive, kSlowTurn); |
| 53 | if (!WaitForDriveDone()) return true; |
| 54 | |
| 55 | this_thread::sleep_for(kDelayTime); |
| 56 | if (ShouldCancel()) return true; |
| 57 | |
| 58 | StartDrive(0.0, M_PI, kSlowDrive, kSlowTurn); |
| 59 | if (!WaitForDriveDone()) return true; |
| 60 | |
| 61 | this_thread::sleep_for(kDelayTime); |
| 62 | if (ShouldCancel()) return true; |
| 63 | |
| 64 | StartDrive(1.0, 0.0, kSlowDrive, kSlowTurn); |
| 65 | if (!WaitForDriveDone()) return true; |
| 66 | |
| 67 | this_thread::sleep_for(kDelayTime); |
| 68 | if (ShouldCancel()) return true; |
| 69 | |
| 70 | StartDrive(0.0, M_PI, kSlowDrive, kSlowTurn); |
| 71 | if (!WaitForDriveDone()) return true; |
| 72 | |
| 73 | this_thread::sleep_for(kDelayTime); |
| 74 | if (ShouldCancel()) return true; |
| 75 | } |
| 76 | |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 77 | break; |
| 78 | |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time)); |
| 82 | |
| 83 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 84 | ::std::chrono::milliseconds(5) / 2); |
| 85 | |
| 86 | while (!ShouldCancel()) { |
| 87 | phased_loop.SleepUntilNext(); |
| 88 | } |
| 89 | LOG(DEBUG, "Done running\n"); |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 94 | } // namespace actors |
| 95 | } // namespace y2017 |