Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 1 | #include "y2020/actors/autonomous_actor.h" |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | |
| 5 | #include <chrono> |
| 6 | #include <cmath> |
| 7 | |
| 8 | #include "aos/logging/logging.h" |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 9 | #include "aos/util/math.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/drivetrain/spline.h" |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 12 | #include "y2020/actors/auto_splines.h" |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 13 | #include "y2020/control_loops/drivetrain/drivetrain_base.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 14 | |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 15 | DEFINE_bool(spline_auto, true, "If true, define a spline autonomous mode"); |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 16 | DEFINE_bool(galactic_search, false, |
| 17 | "If true, do the galactic search autonomous"); |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 18 | DEFINE_bool(bounce, false, "If true, run the AutoNav Bounce autonomous"); |
| 19 | DEFINE_bool(barrel, false, "If true, run the AutoNav Barrel autonomous"); |
| 20 | DEFINE_bool(slalom, false, "If true, run the AutoNav Slalom autonomous"); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 21 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 22 | namespace y2020 { |
| 23 | namespace actors { |
| 24 | |
| 25 | using ::aos::monotonic_clock; |
| 26 | using ::frc971::ProfileParametersT; |
| 27 | using frc971::control_loops::drivetrain::LocalizerControl; |
| 28 | namespace chrono = ::std::chrono; |
| 29 | |
| 30 | AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop) |
| 31 | : frc971::autonomous::BaseAutonomousActor( |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 32 | event_loop, control_loops::drivetrain::GetDrivetrainConfig()), |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 33 | localizer_control_sender_( |
| 34 | event_loop->MakeSender< |
| 35 | ::frc971::control_loops::drivetrain::LocalizerControl>( |
| 36 | "/drivetrain")), |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 37 | joystick_state_fetcher_( |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 38 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 39 | path_fetcher_(event_loop->MakeFetcher<y2020::vision::GalacticSearchPath>( |
| 40 | "/pi1/camera")), |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 41 | auto_splines_() { |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 42 | set_max_drivetrain_voltage(2.0); |
| 43 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 44 | |
| 45 | void AutonomousActor::Reset() { |
| 46 | InitializeEncoders(); |
| 47 | ResetDrivetrain(); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 48 | |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 49 | joystick_state_fetcher_.Fetch(); |
| 50 | CHECK(joystick_state_fetcher_.get() != nullptr) |
| 51 | << "Expect at least one JoystickState message before running auto..."; |
| 52 | alliance_ = joystick_state_fetcher_->alliance(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | bool AutonomousActor::RunAction( |
Austin Schuh | 6fb0a6d | 2021-01-23 15:43:17 -0800 | [diff] [blame] | 56 | const ::frc971::autonomous::AutonomousActionParams *params) { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 57 | Reset(); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 58 | AOS_LOG(INFO, "Params are %d\n", params->mode()); |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 59 | if (alliance_ == aos::Alliance::kInvalid) { |
| 60 | AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection."); |
| 61 | return false; |
| 62 | } |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 63 | if (FLAGS_galactic_search) { |
| 64 | GalacticSearch(); |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 65 | } else if (FLAGS_bounce) { |
| 66 | AutoNavBounce(); |
| 67 | } else if (FLAGS_barrel) { |
| 68 | AutoNavBarrel(); |
| 69 | } else if (FLAGS_slalom) { |
| 70 | AutoNavSlalom(); |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 71 | } else if (FLAGS_spline_auto) { |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 72 | SplineAuto(); |
| 73 | } else { |
| 74 | return DriveFwd(); |
| 75 | } |
| 76 | return true; |
| 77 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 78 | |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 79 | void AutonomousActor::SendStartingPosition(double x, double y, double theta) { |
| 80 | // Set up the starting position for the blue alliance. |
| 81 | double starting_heading = theta; |
| 82 | |
| 83 | // TODO(james): Resetting the localizer breaks the left/right statespace |
| 84 | // controller. That is a bug, but we can fix that later by not resetting. |
| 85 | auto builder = localizer_control_sender_.MakeBuilder(); |
| 86 | |
| 87 | LocalizerControl::Builder localizer_control_builder = |
| 88 | builder.MakeBuilder<LocalizerControl>(); |
| 89 | localizer_control_builder.add_x(x); |
| 90 | localizer_control_builder.add_y(y); |
| 91 | localizer_control_builder.add_theta(starting_heading); |
| 92 | localizer_control_builder.add_theta_uncertainty(0.00001); |
| 93 | if (!builder.Send(localizer_control_builder.Finish())) { |
| 94 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void AutonomousActor::GalacticSearch() { |
| 99 | path_fetcher_.Fetch(); |
| 100 | CHECK(path_fetcher_.get() != nullptr) |
| 101 | << "Expect at least one GalacticSearchPath message before running " |
| 102 | "auto..."; |
| 103 | if (path_fetcher_->alliance() == y2020::vision::Alliance::kUnknown) { |
| 104 | AOS_LOG(ERROR, "The galactic search path is unknown, doing nothing."); |
| 105 | } else { |
| 106 | SplineHandle spline1 = PlanSpline( |
| 107 | [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder |
| 108 | *builder) { |
| 109 | flatbuffers::Offset<frc971::MultiSpline> target_spline; |
| 110 | if (path_fetcher_->alliance() == y2020::vision::Alliance::kRed) { |
| 111 | if (path_fetcher_->letter() == y2020::vision::Letter::kA) { |
| 112 | target_spline = auto_splines_.SplineRedA(builder); |
| 113 | } else { |
| 114 | CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB); |
| 115 | target_spline = auto_splines_.SplineRedB(builder); |
| 116 | } |
| 117 | } else { |
| 118 | if (path_fetcher_->letter() == y2020::vision::Letter::kA) { |
| 119 | target_spline = auto_splines_.SplineBlueA(builder); |
| 120 | } else { |
| 121 | CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB); |
| 122 | target_spline = auto_splines_.SplineBlueB(builder); |
| 123 | } |
| 124 | } |
| 125 | const frc971::MultiSpline *const spline = |
| 126 | flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline); |
| 127 | |
| 128 | SendStartingPosition(CHECK_NOTNULL(spline)); |
| 129 | |
| 130 | return target_spline; |
| 131 | }, |
| 132 | SplineDirection::kForward); |
| 133 | |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 134 | set_intake_goal(1.2); |
| 135 | set_roller_voltage(7.0); |
| 136 | SendSuperstructureGoal(); |
| 137 | |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 138 | if (!spline1.WaitForPlan()) return; |
| 139 | spline1.Start(); |
| 140 | |
| 141 | if (!spline1.WaitForSplineDistanceRemaining(0.02)) return; |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 142 | set_intake_goal(-0.89); |
| 143 | set_roller_voltage(0.0); |
| 144 | SendSuperstructureGoal(); |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 148 | void AutonomousActor::AutoNavBounce() { |
| 149 | SplineHandle spline1 = PlanSpline( |
| 150 | [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder |
| 151 | *builder) { |
| 152 | flatbuffers::Offset<frc971::MultiSpline> target_spline = |
| 153 | auto_splines_.AutoNavBounce1(builder); |
| 154 | const frc971::MultiSpline *const spline = |
| 155 | flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline); |
| 156 | SendStartingPosition(CHECK_NOTNULL(spline)); |
| 157 | return target_spline; |
| 158 | }, |
| 159 | SplineDirection::kForward); |
| 160 | |
| 161 | if (!spline1.WaitForPlan()) return; |
| 162 | spline1.Start(); |
| 163 | |
| 164 | SplineHandle spline2 = PlanSpline( |
| 165 | [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder |
| 166 | *builder) { return auto_splines_.AutoNavBounce2(builder); }, |
| 167 | SplineDirection::kBackward); |
| 168 | |
| 169 | if (!spline1.WaitForSplineDistanceRemaining(0.02)) return; |
| 170 | |
| 171 | if (!spline2.WaitForPlan()) return; |
| 172 | spline2.Start(); |
| 173 | |
| 174 | SplineHandle spline3 = PlanSpline( |
| 175 | [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder |
| 176 | *builder) { return auto_splines_.AutoNavBounce3(builder); }, |
| 177 | SplineDirection::kForward); |
| 178 | |
| 179 | if (!spline2.WaitForSplineDistanceRemaining(0.02)) return; |
| 180 | |
| 181 | if (!spline3.WaitForPlan()) return; |
| 182 | spline3.Start(); |
| 183 | |
| 184 | SplineHandle spline4 = PlanSpline( |
| 185 | [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder |
| 186 | *builder) { return auto_splines_.AutoNavBounce4(builder); }, |
| 187 | SplineDirection::kBackward); |
| 188 | |
| 189 | if (!spline3.WaitForSplineDistanceRemaining(0.02)) return; |
| 190 | |
| 191 | if (!spline4.WaitForPlan()) return; |
| 192 | spline4.Start(); |
| 193 | |
| 194 | if (!spline4.WaitForSplineDistanceRemaining(0.02)) return; |
| 195 | } |
| 196 | |
| 197 | void AutonomousActor::AutoNavBarrel() { |
| 198 | SplineHandle spline1 = PlanSpline( |
| 199 | [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder |
| 200 | *builder) { |
| 201 | flatbuffers::Offset<frc971::MultiSpline> target_spline; |
| 202 | target_spline = auto_splines_.AutoNavBarrel(builder); |
| 203 | const frc971::MultiSpline *const spline = |
| 204 | flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline); |
| 205 | SendStartingPosition(CHECK_NOTNULL(spline)); |
| 206 | return target_spline; |
| 207 | }, |
| 208 | SplineDirection::kForward); |
| 209 | |
| 210 | if (!spline1.WaitForPlan()) return; |
| 211 | spline1.Start(); |
| 212 | |
| 213 | if (!spline1.WaitForSplineDistanceRemaining(0.02)) return; |
| 214 | } |
| 215 | |
| 216 | void AutonomousActor::AutoNavSlalom() { |
| 217 | SplineHandle spline1 = PlanSpline( |
| 218 | [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder |
| 219 | *builder) { |
| 220 | flatbuffers::Offset<frc971::MultiSpline> target_spline; |
| 221 | target_spline = auto_splines_.AutoNavSlalom(builder); |
| 222 | const frc971::MultiSpline *const spline = |
| 223 | flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline); |
| 224 | SendStartingPosition(CHECK_NOTNULL(spline)); |
| 225 | return target_spline; |
| 226 | }, |
| 227 | SplineDirection::kForward); |
| 228 | |
| 229 | if (!spline1.WaitForPlan()) return; |
| 230 | spline1.Start(); |
| 231 | |
| 232 | if (!spline1.WaitForSplineDistanceRemaining(0.02)) return; |
| 233 | } |
| 234 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 235 | void AutonomousActor::SplineAuto() { |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 236 | SplineHandle spline1 = PlanSpline( |
| 237 | [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 238 | *builder) { |
| 239 | flatbuffers::Offset<frc971::MultiSpline> target_spline; |
| 240 | target_spline = auto_splines_.TestSpline(builder); |
| 241 | const frc971::MultiSpline *const spline = |
| 242 | flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline); |
| 243 | SendStartingPosition(CHECK_NOTNULL(spline)); |
| 244 | return target_spline; |
| 245 | }, |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 246 | SplineDirection::kForward); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 247 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 248 | if (!spline1.WaitForPlan()) return; |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 249 | spline1.Start(); |
| 250 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 251 | if (!spline1.WaitForSplineDistanceRemaining(0.02)) return; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 252 | } |
| 253 | |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 254 | void AutonomousActor::SendStartingPosition( |
| 255 | const frc971::MultiSpline *const spline) { |
| 256 | float x = spline->spline_x()->Get(0); |
| 257 | float y = spline->spline_y()->Get(0); |
| 258 | |
| 259 | Eigen::Matrix<double, 2, 6> control_points; |
| 260 | for (size_t ii = 0; ii < 6; ++ii) { |
| 261 | control_points(0, ii) = spline->spline_x()->Get(ii); |
| 262 | control_points(1, ii) = spline->spline_y()->Get(ii); |
| 263 | } |
| 264 | |
| 265 | frc971::control_loops::drivetrain::Spline spline_object(control_points); |
| 266 | |
| 267 | SendStartingPosition(x, y, spline_object.Theta(0)); |
| 268 | } |
| 269 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 270 | ProfileParametersT MakeProfileParametersT(const float max_velocity, |
| 271 | const float max_acceleration) { |
| 272 | ProfileParametersT params; |
| 273 | params.max_velocity = max_velocity; |
| 274 | params.max_acceleration = max_acceleration; |
| 275 | return params; |
| 276 | } |
| 277 | |
| 278 | bool AutonomousActor::DriveFwd() { |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 279 | SendStartingPosition(0, 0, 0); |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 280 | const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 281 | const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f); |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 282 | StartDrive(1.0, 0.0, kDrive, kTurn); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 283 | return WaitForDriveDone(); |
| 284 | } |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 285 | |
| 286 | void AutonomousActor::SendSuperstructureGoal() { |
| 287 | |
| 288 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 289 | |
| 290 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 291 | intake_offset; |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 292 | |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 293 | { |
| 294 | StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder = |
| 295 | builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>(); |
| 296 | |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 297 | frc971::ProfileParameters::Builder profile_params_builder = |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 298 | builder.MakeBuilder<frc971::ProfileParameters>(); |
| 299 | profile_params_builder.add_max_velocity(0.0); |
| 300 | profile_params_builder.add_max_acceleration(0.0); |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 301 | flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset = |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 302 | profile_params_builder.Finish(); |
| 303 | intake_builder.add_unsafe_goal(intake_goal_); |
| 304 | intake_builder.add_profile_params(profile_params_offset); |
| 305 | intake_offset = intake_builder.Finish(); |
| 306 | } |
| 307 | |
| 308 | superstructure::Goal::Builder superstructure_builder = |
| 309 | builder.MakeBuilder<superstructure::Goal>(); |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 310 | |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 311 | superstructure_builder.add_intake(intake_offset); |
| 312 | superstructure_builder.add_roller_voltage(roller_voltage_); |
| 313 | superstructure_builder.add_roller_speed_compensation(kRollerSpeedCompensation); |
| 314 | |
| 315 | if (!builder.Send(superstructure_builder.Finish())) { |
| 316 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 317 | } |
| 318 | |
| 319 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 320 | } // namespace actors |
| 321 | } // namespace y2020 |