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); |
Austin Schuh | 76af51e | 2017-04-09 18:32:38 -0700 | [diff] [blame^] | 57 | set_turret_goal(0.0); |
| 58 | // Values good for blue: |
| 59 | // TODO(austin): Drive these off the auto switch. |
| 60 | //set_hood_goal(0.355 + 0.01 + 0.005); |
| 61 | //set_shooter_velocity(355.0); |
| 62 | set_hood_goal(0.355 + 0.01 + 0.005 + 0.01 + 0.01); |
| 63 | set_shooter_velocity(351.0); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 64 | SendSuperstructureGoal(); |
| 65 | StartDrive(0.0, -M_PI / 4.0 * kDriveDirection, kSlowDrive, kSlowTurn); |
| 66 | if (!WaitForDriveNear(0.05, 0.0)) return true; |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 67 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 68 | this_thread::sleep_for(chrono::milliseconds(100)); |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 69 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 70 | StartDrive(0.0, (M_PI / 4.0 + 0.20) * kDriveDirection, kSlowDrive, |
| 71 | kSmashTurn); |
| 72 | if (!WaitForDriveNear(0.05, 0.2)) return true; |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 73 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 74 | set_vision_track(true); |
Austin Schuh | 76af51e | 2017-04-09 18:32:38 -0700 | [diff] [blame^] | 75 | |
| 76 | set_indexer_angular_velocity(-1.8 * M_PI); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 77 | SendSuperstructureGoal(); |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 78 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 79 | this_thread::sleep_for(chrono::milliseconds(200)); |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 80 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 81 | StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn); |
| 82 | if (!WaitForDriveNear(0.05, 0.02)) return true; |
Austin Schuh | 76af51e | 2017-04-09 18:32:38 -0700 | [diff] [blame^] | 83 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 84 | LOG(INFO, "Started shooting at %f\n", |
| 85 | DoubleSeconds(monotonic_clock::now() - start_time)); |
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 | this_thread::sleep_for(start_time + chrono::seconds(9) - |
| 88 | monotonic_clock::now()); |
| 89 | StartDrive(0.0, (-0.05) * kDriveDirection, kSlowDrive, kSlowTurn); |
| 90 | if (ShouldCancel()) return true; |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 91 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 92 | set_intake_max_velocity(0.05); |
| 93 | set_intake_goal(0.08); |
| 94 | SendSuperstructureGoal(); |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 95 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 96 | this_thread::sleep_for(start_time + chrono::seconds(15) - |
| 97 | monotonic_clock::now()); |
| 98 | if (ShouldCancel()) return true; |
| 99 | |
| 100 | set_shooter_velocity(0.0); |
| 101 | set_indexer_angular_velocity(0.0); |
| 102 | SendSuperstructureGoal(); |
| 103 | |
| 104 | } break; |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time)); |
| 108 | |
| 109 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 110 | ::std::chrono::milliseconds(5) / 2); |
| 111 | |
| 112 | while (!ShouldCancel()) { |
| 113 | phased_loop.SleepUntilNext(); |
| 114 | } |
| 115 | LOG(DEBUG, "Done running\n"); |
| 116 | |
| 117 | return true; |
| 118 | } |
| 119 | |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 120 | } // namespace actors |
| 121 | } // namespace y2017 |