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