Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 1 | #include "y2019/actors/autonomous_actor.h" |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | |
| 5 | #include <chrono> |
| 6 | #include <cmath> |
| 7 | |
| 8 | #include "aos/logging/logging.h" |
| 9 | #include "aos/util/phased_loop.h" |
| 10 | |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 12 | #include "frc971/control_loops/drivetrain/localizer.q.h" |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 13 | #include "y2019/actors/auto_splines.h" |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 14 | #include "y2019/control_loops/drivetrain/drivetrain_base.h" |
| 15 | |
| 16 | namespace y2019 { |
| 17 | namespace actors { |
| 18 | using ::frc971::control_loops::drivetrain_queue; |
| 19 | using ::frc971::control_loops::drivetrain::localizer_control; |
| 20 | using ::aos::monotonic_clock; |
| 21 | namespace chrono = ::std::chrono; |
| 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 | } // namespace |
| 31 | |
| 32 | AutonomousActor::AutonomousActor( |
| 33 | ::frc971::autonomous::AutonomousActionQueueGroup *s) |
| 34 | : frc971::autonomous::BaseAutonomousActor( |
| 35 | s, control_loops::drivetrain::GetDrivetrainConfig()) {} |
| 36 | |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 37 | void AutonomousActor::Reset(bool is_left) { |
| 38 | const double turn_scalar = is_left ? 1.0 : -1.0; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 39 | elevator_goal_ = 0.01; |
| 40 | wrist_goal_ = -M_PI / 2.0; |
| 41 | intake_goal_ = -1.2; |
| 42 | |
| 43 | suction_on_ = false; |
| 44 | suction_gamepiece_ = 1; |
| 45 | |
| 46 | elevator_max_velocity_ = 0.0; |
| 47 | elevator_max_acceleration_ = 0.0; |
| 48 | wrist_max_velocity_ = 0.0; |
| 49 | wrist_max_acceleration_ = 0.0; |
| 50 | |
| 51 | InitializeEncoders(); |
| 52 | SendSuperstructureGoal(); |
| 53 | |
| 54 | { |
| 55 | auto localizer_resetter = localizer_control.MakeMessage(); |
| 56 | // Start on the left l2. |
| 57 | localizer_resetter->x = 1.0; |
Austin Schuh | da0ac23 | 2019-03-17 18:13:18 -0700 | [diff] [blame] | 58 | localizer_resetter->y = 1.5 * turn_scalar; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 59 | localizer_resetter->theta = M_PI; |
Austin Schuh | 4b3a005 | 2019-03-24 19:19:10 -0700 | [diff] [blame] | 60 | localizer_resetter->theta_uncertainty = 0.0000001; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 61 | if (!localizer_resetter.Send()) { |
| 62 | LOG(ERROR, "Failed to reset localizer.\n"); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Wait for the drivetrain to run so it has time to reset the heading. |
| 67 | // Otherwise our drivetrain reset will do a 180 right at the start. |
| 68 | drivetrain_queue.status.FetchAnother(); |
| 69 | LOG(INFO, "Heading is %f\n", drivetrain_queue.status->estimated_heading); |
| 70 | InitializeEncoders(); |
| 71 | ResetDrivetrain(); |
| 72 | drivetrain_queue.status.FetchAnother(); |
| 73 | LOG(INFO, "Heading is %f\n", drivetrain_queue.status->estimated_heading); |
| 74 | |
| 75 | ResetDrivetrain(); |
| 76 | InitializeEncoders(); |
| 77 | } |
| 78 | |
| 79 | const ProfileParameters kJumpDrive = {2.0, 3.0}; |
| 80 | const ProfileParameters kDrive = {4.0, 3.0}; |
| 81 | const ProfileParameters kTurn = {5.0, 15.0}; |
| 82 | |
| 83 | bool AutonomousActor::RunAction( |
| 84 | const ::frc971::autonomous::AutonomousActionParams ¶ms) { |
| 85 | monotonic_clock::time_point start_time = monotonic_clock::now(); |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 86 | const bool is_left = params.mode == 0; |
| 87 | |
| 88 | { |
| 89 | LOG(INFO, "Starting autonomous action with mode %" PRId32 " %s\n", |
| 90 | params.mode, is_left ? "left" : "right"); |
| 91 | } |
| 92 | const double turn_scalar = is_left ? 1.0 : -1.0; |
| 93 | |
| 94 | Reset(is_left); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 95 | |
| 96 | // Grab the disk, wait until we have vacuum, then jump |
| 97 | set_elevator_goal(0.01); |
| 98 | set_wrist_goal(-M_PI / 2.0); |
| 99 | set_intake_goal(-1.2); |
| 100 | set_suction_goal(true, 1); |
| 101 | SendSuperstructureGoal(); |
| 102 | |
| 103 | if (!WaitForGamePiece()) return true; |
| 104 | LOG(INFO, "Has game piece\n"); |
| 105 | |
Austin Schuh | a78857f | 2019-03-13 22:43:41 -0700 | [diff] [blame] | 106 | StartDrive(-4.0, 0.0, kJumpDrive, kTurn); |
| 107 | if (!WaitForDriveNear(3.3, 10.0)) return true; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 108 | LOG(INFO, "Lifting\n"); |
| 109 | set_elevator_goal(0.30); |
| 110 | SendSuperstructureGoal(); |
| 111 | |
Austin Schuh | a78857f | 2019-03-13 22:43:41 -0700 | [diff] [blame] | 112 | if (!WaitForDriveNear(2.8, 10.0)) return true; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 113 | LOG(INFO, "Off the platform\n"); |
| 114 | |
Austin Schuh | da0ac23 | 2019-03-17 18:13:18 -0700 | [diff] [blame] | 115 | StartDrive(0.0, 1.00 * turn_scalar, kDrive, kTurn); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 116 | LOG(INFO, "Turn started\n"); |
| 117 | if (!WaitForSuperstructureDone()) return true; |
| 118 | LOG(INFO, "Superstructure done\n"); |
| 119 | |
Austin Schuh | a78857f | 2019-03-13 22:43:41 -0700 | [diff] [blame] | 120 | if (!WaitForDriveNear(0.7, 10.0)) return true; |
Austin Schuh | da0ac23 | 2019-03-17 18:13:18 -0700 | [diff] [blame] | 121 | StartDrive(0.0, -0.35 * turn_scalar, kDrive, kTurn); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 122 | |
| 123 | LOG(INFO, "Elevator up\n"); |
Austin Schuh | 291ffe9 | 2019-03-24 16:03:59 -0700 | [diff] [blame] | 124 | set_elevator_goal(0.78); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 125 | SendSuperstructureGoal(); |
| 126 | |
| 127 | if (!WaitForDriveDone()) return true; |
| 128 | LOG(INFO, "Done driving\n"); |
| 129 | |
| 130 | if (!WaitForSuperstructureDone()) return true; |
| 131 | |
| 132 | LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time)); |
| 133 | |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | |
| 137 | } // namespace actors |
| 138 | } // namespace y2019 |