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 | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame^] | 28 | const ProfileParameters kSlowDrive = {3.0, 2.0}; |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 29 | const ProfileParameters kSlowTurn = {3.0, 3.0}; |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame^] | 30 | const ProfileParameters kSmashTurn = {3.0, 5.0}; |
Philipp Schrader | 85fca55 | 2017-03-05 00:30:50 +0000 | [diff] [blame] | 31 | |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 32 | } // namespace |
| 33 | |
Philipp Schrader | 996a2a2 | 2017-02-22 05:02:48 +0000 | [diff] [blame] | 34 | AutonomousActor::AutonomousActor( |
| 35 | ::frc971::autonomous::AutonomousActionQueueGroup *s) |
| 36 | : frc971::autonomous::BaseAutonomousActor( |
| 37 | s, control_loops::drivetrain::GetDrivetrainConfig()) {} |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 38 | |
Philipp Schrader | 996a2a2 | 2017-02-22 05:02:48 +0000 | [diff] [blame] | 39 | bool AutonomousActor::RunAction( |
| 40 | const ::frc971::autonomous::AutonomousActionParams ¶ms) { |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 41 | monotonic_clock::time_point start_time = monotonic_clock::now(); |
| 42 | LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame^] | 43 | Reset(); |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 44 | |
| 45 | switch (params.mode) { |
| 46 | case 0: |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame^] | 47 | default: { |
| 48 | constexpr double kDriveDirection = 1.0; |
| 49 | // Test case autonomous mode. |
| 50 | // Drives forward 1.0 meters and then turns 180 degrees. |
| 51 | set_intake_goal(0.07); |
| 52 | SendSuperstructureGoal(); |
| 53 | StartDrive(-3.7, 0.0, kSlowDrive, kSlowTurn); |
| 54 | if (!WaitForDriveNear(2.75, 0.0)) return true; |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 55 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame^] | 56 | set_intake_goal(0.23); |
| 57 | SendSuperstructureGoal(); |
| 58 | StartDrive(0.0, -M_PI / 4.0 * kDriveDirection, kSlowDrive, kSlowTurn); |
| 59 | if (!WaitForDriveNear(0.05, 0.0)) return true; |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 60 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame^] | 61 | this_thread::sleep_for(chrono::milliseconds(100)); |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 62 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame^] | 63 | StartDrive(0.0, (M_PI / 4.0 + 0.20) * kDriveDirection, kSlowDrive, |
| 64 | kSmashTurn); |
| 65 | if (!WaitForDriveNear(0.05, 0.2)) return true; |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 66 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame^] | 67 | set_vision_track(true); |
| 68 | set_turret_goal(0.0); |
| 69 | set_hood_goal(0.63); |
| 70 | set_shooter_velocity(371.0); |
| 71 | SendSuperstructureGoal(); |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 72 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame^] | 73 | this_thread::sleep_for(chrono::milliseconds(200)); |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 74 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame^] | 75 | StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn); |
| 76 | if (!WaitForDriveNear(0.05, 0.02)) return true; |
| 77 | set_indexer_angular_velocity(-1.8 * M_PI); |
| 78 | SendSuperstructureGoal(); |
| 79 | LOG(INFO, "Started shooting at %f\n", |
| 80 | DoubleSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 81 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame^] | 82 | this_thread::sleep_for(start_time + chrono::seconds(9) - |
| 83 | monotonic_clock::now()); |
| 84 | StartDrive(0.0, (-0.05) * kDriveDirection, kSlowDrive, kSlowTurn); |
| 85 | if (ShouldCancel()) return true; |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 86 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame^] | 87 | set_intake_max_velocity(0.05); |
| 88 | set_intake_goal(0.08); |
| 89 | SendSuperstructureGoal(); |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 90 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame^] | 91 | this_thread::sleep_for(start_time + chrono::seconds(15) - |
| 92 | monotonic_clock::now()); |
| 93 | if (ShouldCancel()) return true; |
| 94 | |
| 95 | set_shooter_velocity(0.0); |
| 96 | set_indexer_angular_velocity(0.0); |
| 97 | SendSuperstructureGoal(); |
| 98 | |
| 99 | } break; |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time)); |
| 103 | |
| 104 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 105 | ::std::chrono::milliseconds(5) / 2); |
| 106 | |
| 107 | while (!ShouldCancel()) { |
| 108 | phased_loop.SleepUntilNext(); |
| 109 | } |
| 110 | LOG(DEBUG, "Done running\n"); |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 115 | } // namespace actors |
| 116 | } // namespace y2017 |