Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 1 | #include "y2019/actors/autonomous_actor.h" |
| 2 | |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 3 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame^] | 4 | #include <cinttypes> |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 5 | #include <cmath> |
| 6 | |
| 7 | #include "aos/logging/logging.h" |
| 8 | #include "aos/util/phased_loop.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 10 | #include "y2019/actors/auto_splines.h" |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 11 | #include "y2019/control_loops/drivetrain/drivetrain_base.h" |
| 12 | |
| 13 | namespace y2019 { |
| 14 | namespace actors { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 15 | |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 16 | using ::aos::monotonic_clock; |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame^] | 17 | using ::frc971::ProfileParametersT; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 18 | using frc971::control_loops::drivetrain::LocalizerControl; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 19 | namespace chrono = ::std::chrono; |
| 20 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 21 | AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop) |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 22 | : frc971::autonomous::BaseAutonomousActor( |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 23 | event_loop, control_loops::drivetrain::GetDrivetrainConfig()), |
Austin Schuh | eb99d07 | 2019-05-12 21:03:38 -0700 | [diff] [blame] | 24 | localizer_control_sender_( |
| 25 | event_loop->MakeSender< |
| 26 | ::frc971::control_loops::drivetrain::LocalizerControl>( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 27 | "/drivetrain")), |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 28 | superstructure_goal_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 29 | event_loop->MakeSender<::y2019::control_loops::superstructure::Goal>( |
| 30 | "/superstructure")), |
| 31 | superstructure_status_fetcher_( |
| 32 | event_loop |
| 33 | ->MakeFetcher<::y2019::control_loops::superstructure::Status>( |
| 34 | "/superstructure")) {} |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 35 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 36 | bool AutonomousActor::WaitForDriveXGreater(double x) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 37 | AOS_LOG(INFO, "Waiting until x > %f\n", x); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 38 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 39 | event_loop()->monotonic_now(), |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 40 | ::std::chrono::milliseconds(5) / 2); |
| 41 | |
| 42 | while (true) { |
| 43 | if (ShouldCancel()) { |
| 44 | return false; |
| 45 | } |
| 46 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 47 | drivetrain_status_fetcher_.Fetch(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 48 | if (drivetrain_status_fetcher_->x() > x) { |
| 49 | AOS_LOG(INFO, "X at %f\n", drivetrain_status_fetcher_->x()); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 50 | return true; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | bool AutonomousActor::WaitForDriveYCloseToZero(double y) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 56 | AOS_LOG(INFO, "Waiting until |y| < %f\n", y); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 57 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 58 | event_loop()->monotonic_now(), |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 59 | ::std::chrono::milliseconds(5) / 2); |
| 60 | |
| 61 | while (true) { |
| 62 | if (ShouldCancel()) { |
| 63 | return false; |
| 64 | } |
| 65 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 66 | drivetrain_status_fetcher_.Fetch(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 67 | if (::std::abs(drivetrain_status_fetcher_->y()) < y) { |
| 68 | AOS_LOG(INFO, "Y at %f\n", drivetrain_status_fetcher_->y()); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 69 | return true; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 74 | void AutonomousActor::Reset(bool is_left) { |
| 75 | const double turn_scalar = is_left ? 1.0 : -1.0; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 76 | elevator_goal_ = 0.01; |
| 77 | wrist_goal_ = -M_PI / 2.0; |
| 78 | intake_goal_ = -1.2; |
| 79 | |
| 80 | suction_on_ = false; |
| 81 | suction_gamepiece_ = 1; |
| 82 | |
| 83 | elevator_max_velocity_ = 0.0; |
| 84 | elevator_max_acceleration_ = 0.0; |
| 85 | wrist_max_velocity_ = 0.0; |
| 86 | wrist_max_acceleration_ = 0.0; |
| 87 | |
| 88 | InitializeEncoders(); |
| 89 | SendSuperstructureGoal(); |
| 90 | |
| 91 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 92 | auto builder = localizer_control_sender_.MakeBuilder(); |
| 93 | |
| 94 | LocalizerControl::Builder localizer_control_builder = |
| 95 | builder.MakeBuilder<LocalizerControl>(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 96 | // Start on the left l2. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 97 | localizer_control_builder.add_x(1.0); |
| 98 | localizer_control_builder.add_y(1.35 * turn_scalar); |
| 99 | localizer_control_builder.add_theta(M_PI); |
| 100 | localizer_control_builder.add_theta_uncertainty(0.00001); |
| 101 | if (!builder.Send(localizer_control_builder.Finish())) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 102 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | // Wait for the drivetrain to run so it has time to reset the heading. |
| 107 | // Otherwise our drivetrain reset will do a 180 right at the start. |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 108 | WaitUntil([this]() { return drivetrain_status_fetcher_.Fetch(); }); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 109 | AOS_LOG(INFO, "Heading is %f\n", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 110 | drivetrain_status_fetcher_->estimated_heading()); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 111 | InitializeEncoders(); |
| 112 | ResetDrivetrain(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 113 | WaitUntil([this]() { return drivetrain_status_fetcher_.Fetch(); }); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 114 | AOS_LOG(INFO, "Heading is %f\n", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 115 | drivetrain_status_fetcher_->estimated_heading()); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 116 | |
| 117 | ResetDrivetrain(); |
| 118 | InitializeEncoders(); |
| 119 | } |
| 120 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 121 | ProfileParametersT MakeProfileParameters(float max_velocity, |
| 122 | float max_acceleration) { |
| 123 | ProfileParametersT result; |
| 124 | result.max_velocity = max_velocity; |
| 125 | result.max_acceleration = max_acceleration; |
| 126 | return result; |
| 127 | } |
| 128 | |
| 129 | const ProfileParametersT kJumpDrive = MakeProfileParameters(2.0, 3.0); |
| 130 | const ProfileParametersT kDrive = MakeProfileParameters(4.0, 3.0); |
| 131 | const ProfileParametersT kTurn = MakeProfileParameters(5.0, 15.0); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 132 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 133 | const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.01, M_PI / 2.0}; |
| 134 | const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.015, -M_PI / 2.0}; |
| 135 | |
| 136 | const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0}; |
| 137 | const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0}; |
| 138 | |
| 139 | const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0}; |
| 140 | |
| 141 | const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0}; |
| 142 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 143 | template <typename Functor> |
| 144 | std::function<flatbuffers::Offset<frc971::MultiSpline>( |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 145 | aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder |
| 146 | *builder)> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 147 | BindIsLeft(Functor f, bool is_left) { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 148 | return [is_left, |
| 149 | f](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder |
| 150 | *builder) { return f(builder, is_left); }; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 153 | bool AutonomousActor::RunAction( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 154 | const ::frc971::autonomous::AutonomousActionParams *params) { |
Austin Schuh | 77ed543 | 2019-07-07 20:41:36 -0700 | [diff] [blame] | 155 | const monotonic_clock::time_point start_time = monotonic_now(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 156 | const bool is_left = params->mode() == 0; |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 157 | |
| 158 | { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 159 | AOS_LOG(INFO, "Starting autonomous action with mode %" PRId32 " %s\n", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 160 | params->mode(), is_left ? "left" : "right"); |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 161 | } |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 162 | |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 163 | const double turn_scalar = is_left ? 1.0 : -1.0; |
| 164 | |
| 165 | Reset(is_left); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 166 | enum class Mode { kTesting, kRocket, kCargoship }; |
| 167 | Mode mode = Mode::kCargoship; |
| 168 | if (mode == Mode::kRocket) { |
| 169 | SplineHandle spline1 = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 170 | PlanSpline(BindIsLeft(AutonomousSplines::HabToFarRocketTest, is_left), |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 171 | SplineDirection::kBackward); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 172 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 173 | // Grab the disk, jump, wait until we have vacuum, then raise the elevator |
| 174 | set_elevator_goal(0.010); |
| 175 | set_wrist_goal(-M_PI / 2.0); |
| 176 | set_intake_goal(-1.2); |
| 177 | set_suction_goal(true, 1); |
| 178 | SendSuperstructureGoal(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 179 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 180 | // if planned start the spline and plan the next |
| 181 | if (!spline1.WaitForPlan()) return true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 182 | AOS_LOG(INFO, "Planned\n"); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 183 | spline1.Start(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 184 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 185 | // If suction, move the superstructure to score |
| 186 | if (!WaitForGamePiece()) return true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 187 | AOS_LOG(INFO, "Has game piece\n"); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 188 | if (!spline1.WaitForSplineDistanceRemaining(3.5)) return true; |
| 189 | set_elevator_wrist_goal(kPanelBackwardMiddlePos); |
| 190 | SendSuperstructureGoal(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 191 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 192 | if (!spline1.WaitForSplineDistanceRemaining(2.0)) return true; |
| 193 | set_elevator_wrist_goal(kPanelForwardMiddlePos); |
| 194 | SendSuperstructureGoal(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 195 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 196 | // END SPLINE 1 |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 197 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 198 | if (!spline1.WaitForSplineDistanceRemaining(0.2)) return true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 199 | LineFollowAtVelocity(1.3, |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 200 | control_loops::drivetrain::SelectionHint::FAR_ROCKET); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 201 | if (!WaitForMilliseconds(::std::chrono::milliseconds(1200))) return true; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 202 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 203 | set_suction_goal(false, 1); |
| 204 | SendSuperstructureGoal(); |
| 205 | if (!WaitForMilliseconds(::std::chrono::milliseconds(200))) return true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 206 | LineFollowAtVelocity(-1.0, |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 207 | control_loops::drivetrain::SelectionHint::FAR_ROCKET); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 208 | SplineHandle spline2 = |
| 209 | PlanSpline(BindIsLeft(AutonomousSplines::FarRocketToHP, is_left), |
| 210 | SplineDirection::kBackward); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 211 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 212 | if (!WaitForMilliseconds(::std::chrono::milliseconds(150))) return true; |
| 213 | if (!spline2.WaitForPlan()) return true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 214 | AOS_LOG(INFO, "Planned\n"); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 215 | // Drive back to hp and set the superstructure accordingly |
| 216 | spline2.Start(); |
| 217 | if (!WaitForMilliseconds(::std::chrono::milliseconds(500))) return true; |
| 218 | set_elevator_wrist_goal(kPanelHPIntakeBackwardPos); |
| 219 | SendSuperstructureGoal(); |
| 220 | if (!WaitForMilliseconds(::std::chrono::milliseconds(1000))) return true; |
| 221 | set_suction_goal(true, 1); |
| 222 | SendSuperstructureGoal(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 223 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 224 | if (!spline2.WaitForSplineDistanceRemaining(1.6)) return true; |
| 225 | LineFollowAtVelocity(-1.6); |
| 226 | |
| 227 | // As soon as we pick up Panel 2 go score on the back rocket |
| 228 | if (!WaitForGamePiece()) return true; |
| 229 | LineFollowAtVelocity(1.5); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 230 | SplineHandle spline3 = |
| 231 | PlanSpline(BindIsLeft(AutonomousSplines::HPToFarRocket, is_left), |
| 232 | SplineDirection::kForward); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 233 | if (!WaitForDriveXGreater(0.50)) return true; |
| 234 | if (!spline3.WaitForPlan()) return true; |
| 235 | spline3.Start(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 236 | AOS_LOG(INFO, "Has game piece\n"); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 237 | if (!WaitForMilliseconds(::std::chrono::milliseconds(1000))) return true; |
| 238 | set_elevator_wrist_goal(kPanelBackwardMiddlePos); |
| 239 | SendSuperstructureGoal(); |
| 240 | if (!WaitForDriveXGreater(7.1)) return true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 241 | LineFollowAtVelocity(-1.5, |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 242 | control_loops::drivetrain::SelectionHint::FAR_ROCKET); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 243 | if (!WaitForMilliseconds(::std::chrono::milliseconds(1000))) return true; |
| 244 | set_elevator_wrist_goal(kPanelBackwardUpperPos); |
| 245 | SendSuperstructureGoal(); |
| 246 | if (!WaitForMilliseconds(::std::chrono::milliseconds(1500))) return true; |
| 247 | set_suction_goal(false, 1); |
| 248 | SendSuperstructureGoal(); |
| 249 | if (!WaitForMilliseconds(::std::chrono::milliseconds(400))) return true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 250 | LineFollowAtVelocity(1.0, |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 251 | control_loops::drivetrain::SelectionHint::FAR_ROCKET); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 252 | SendSuperstructureGoal(); |
| 253 | if (!WaitForMilliseconds(::std::chrono::milliseconds(200))) return true; |
| 254 | } else if (mode == Mode::kCargoship) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 255 | SplineHandle spline1 = PlanSpline( |
| 256 | BindIsLeft(AutonomousSplines::HABToSecondCargoShipBay, is_left), |
| 257 | SplineDirection::kBackward); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 258 | set_elevator_goal(0.01); |
| 259 | set_wrist_goal(-M_PI / 2.0); |
| 260 | set_intake_goal(-1.2); |
| 261 | set_suction_goal(true, 1); |
| 262 | SendSuperstructureGoal(); |
| 263 | |
| 264 | // if planned start the spline and plan the next |
| 265 | if (!spline1.WaitForPlan()) return true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 266 | AOS_LOG(INFO, "Planned\n"); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 267 | spline1.Start(); |
| 268 | |
| 269 | // If suction, move the superstructure to score |
| 270 | if (!WaitForGamePiece()) return true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 271 | AOS_LOG(INFO, "Has game piece\n"); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 272 | // unstick the hatch panel |
| 273 | if (!WaitForMilliseconds(::std::chrono::milliseconds(500))) return true; |
| 274 | set_elevator_goal(0.5); |
| 275 | set_wrist_goal(-M_PI / 2.0); |
| 276 | SendSuperstructureGoal(); |
| 277 | if (!WaitForMilliseconds(::std::chrono::milliseconds(500))) return true; |
| 278 | set_elevator_wrist_goal(kPanelCargoBackwardPos); |
| 279 | SendSuperstructureGoal(); |
| 280 | |
| 281 | if (!spline1.WaitForSplineDistanceRemaining(0.8)) return true; |
| 282 | // Line follow in to the first disc. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 283 | LineFollowAtVelocity(-0.9, |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 284 | control_loops::drivetrain::SelectionHint::MID_SHIP); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 285 | if (!WaitForDriveYCloseToZero(1.2)) return true; |
| 286 | |
| 287 | set_suction_goal(false, 1); |
| 288 | SendSuperstructureGoal(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 289 | AOS_LOG(INFO, "Dropping disc 1 %f\n", |
| 290 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 291 | |
| 292 | if (!WaitForDriveYCloseToZero(1.13)) return true; |
| 293 | if (!WaitForMilliseconds(::std::chrono::milliseconds(300))) return true; |
| 294 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 295 | LineFollowAtVelocity(0.9, |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 296 | control_loops::drivetrain::SelectionHint::MID_SHIP); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 297 | SplineHandle spline2 = PlanSpline( |
| 298 | BindIsLeft(AutonomousSplines::SecondCargoShipBayToHP, is_left), |
| 299 | SplineDirection::kForward); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 300 | if (!WaitForMilliseconds(::std::chrono::milliseconds(400))) return true; |
| 301 | if (!spline2.WaitForPlan()) return true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 302 | AOS_LOG(INFO, "Planned\n"); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 303 | // Drive back to hp and set the superstructure accordingly |
| 304 | spline2.Start(); |
| 305 | if (!WaitForMilliseconds(::std::chrono::milliseconds(200))) return true; |
| 306 | set_elevator_wrist_goal(kPanelHPIntakeForwrdPos); |
| 307 | SendSuperstructureGoal(); |
| 308 | if (!WaitForMilliseconds(::std::chrono::milliseconds(1000))) return true; |
| 309 | set_suction_goal(true, 1); |
| 310 | SendSuperstructureGoal(); |
| 311 | |
| 312 | if (!spline2.WaitForSplineDistanceRemaining(1.75)) return true; |
| 313 | LineFollowAtVelocity(1.5); |
| 314 | // As soon as we pick up Panel 2 go score on the rocket |
| 315 | if (!WaitForGamePiece()) return true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 316 | AOS_LOG(INFO, "Got gamepiece %f\n", |
| 317 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 318 | LineFollowAtVelocity(-4.0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 319 | SplineHandle spline3 = PlanSpline( |
| 320 | BindIsLeft(AutonomousSplines::HPToThirdCargoShipBay, is_left), |
| 321 | SplineDirection::kBackward); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 322 | if (!WaitForDriveXGreater(0.55)) return true; |
| 323 | if (!spline3.WaitForPlan()) return true; |
| 324 | spline3.Start(); |
| 325 | // Wait until we are a bit out to lift. |
| 326 | if (!WaitForMilliseconds(::std::chrono::milliseconds(1000))) return true; |
| 327 | set_elevator_wrist_goal(kPanelCargoBackwardPos); |
| 328 | SendSuperstructureGoal(); |
| 329 | |
| 330 | if (!spline3.WaitForSplineDistanceRemaining(0.7)) return true; |
| 331 | // Line follow in to the second disc. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 332 | LineFollowAtVelocity(-0.7, |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 333 | control_loops::drivetrain::SelectionHint::FAR_SHIP); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 334 | AOS_LOG(INFO, "Drawing in disc 2 %f\n", |
| 335 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 336 | if (!WaitForDriveYCloseToZero(1.2)) return true; |
| 337 | |
| 338 | set_suction_goal(false, 1); |
| 339 | SendSuperstructureGoal(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 340 | AOS_LOG(INFO, "Dropping disc 2 %f\n", |
| 341 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 342 | |
| 343 | if (!WaitForDriveYCloseToZero(1.13)) return true; |
| 344 | if (!WaitForMilliseconds(::std::chrono::milliseconds(200))) return true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 345 | AOS_LOG(INFO, "Backing up %f\n", |
| 346 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 347 | LineFollowAtVelocity(0.9, |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 348 | control_loops::drivetrain::SelectionHint::FAR_SHIP); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 349 | if (!WaitForMilliseconds(::std::chrono::milliseconds(400))) return true; |
| 350 | } else { |
| 351 | // Grab the disk, wait until we have vacuum, then jump |
| 352 | set_elevator_goal(0.01); |
| 353 | set_wrist_goal(-M_PI / 2.0); |
| 354 | set_intake_goal(-1.2); |
| 355 | set_suction_goal(true, 1); |
| 356 | SendSuperstructureGoal(); |
| 357 | |
| 358 | if (!WaitForGamePiece()) return true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 359 | AOS_LOG(INFO, "Has game piece\n"); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 360 | |
| 361 | StartDrive(-4.0, 0.0, kJumpDrive, kTurn); |
| 362 | if (!WaitForDriveNear(3.3, 10.0)) return true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 363 | AOS_LOG(INFO, "Lifting\n"); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 364 | set_elevator_goal(0.30); |
| 365 | SendSuperstructureGoal(); |
| 366 | |
| 367 | if (!WaitForDriveNear(2.8, 10.0)) return true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 368 | AOS_LOG(INFO, "Off the platform\n"); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 369 | |
| 370 | StartDrive(0.0, 1.00 * turn_scalar, kDrive, kTurn); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 371 | AOS_LOG(INFO, "Turn started\n"); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 372 | if (!WaitForSuperstructureDone()) return true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 373 | AOS_LOG(INFO, "Superstructure done\n"); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 374 | |
| 375 | if (!WaitForDriveNear(0.7, 10.0)) return true; |
| 376 | StartDrive(0.0, -0.35 * turn_scalar, kDrive, kTurn); |
| 377 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 378 | AOS_LOG(INFO, "Elevator up\n"); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 379 | set_elevator_goal(0.78); |
| 380 | SendSuperstructureGoal(); |
| 381 | |
| 382 | if (!WaitForDriveDone()) return true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 383 | AOS_LOG(INFO, "Done driving\n"); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 384 | |
| 385 | if (!WaitForSuperstructureDone()) return true; |
| 386 | } |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 387 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 388 | AOS_LOG(INFO, "Done %f\n", |
| 389 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 390 | |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 391 | return true; |
| 392 | } |
| 393 | |
| 394 | } // namespace actors |
| 395 | } // namespace y2019 |