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"); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 18 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 19 | namespace y2020 { |
| 20 | namespace actors { |
| 21 | |
| 22 | using ::aos::monotonic_clock; |
| 23 | using ::frc971::ProfileParametersT; |
| 24 | using frc971::control_loops::drivetrain::LocalizerControl; |
| 25 | namespace chrono = ::std::chrono; |
| 26 | |
| 27 | AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop) |
| 28 | : frc971::autonomous::BaseAutonomousActor( |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 29 | event_loop, control_loops::drivetrain::GetDrivetrainConfig()), |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 30 | localizer_control_sender_( |
| 31 | event_loop->MakeSender< |
| 32 | ::frc971::control_loops::drivetrain::LocalizerControl>( |
| 33 | "/drivetrain")), |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 34 | joystick_state_fetcher_( |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 35 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 36 | path_fetcher_(event_loop->MakeFetcher<y2020::vision::GalacticSearchPath>( |
| 37 | "/pi1/camera")), |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 38 | auto_splines_() { |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 39 | set_max_drivetrain_voltage(2.0); |
| 40 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 41 | |
| 42 | void AutonomousActor::Reset() { |
| 43 | InitializeEncoders(); |
| 44 | ResetDrivetrain(); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 45 | |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 46 | joystick_state_fetcher_.Fetch(); |
| 47 | CHECK(joystick_state_fetcher_.get() != nullptr) |
| 48 | << "Expect at least one JoystickState message before running auto..."; |
| 49 | alliance_ = joystick_state_fetcher_->alliance(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool AutonomousActor::RunAction( |
Austin Schuh | 6fb0a6d | 2021-01-23 15:43:17 -0800 | [diff] [blame] | 53 | const ::frc971::autonomous::AutonomousActionParams *params) { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 54 | Reset(); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 55 | AOS_LOG(INFO, "Params are %d\n", params->mode()); |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 56 | if (alliance_ == aos::Alliance::kInvalid) { |
| 57 | AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection."); |
| 58 | return false; |
| 59 | } |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 60 | if (FLAGS_galactic_search) { |
| 61 | GalacticSearch(); |
| 62 | } else if (FLAGS_spline_auto) { |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 63 | SplineAuto(); |
| 64 | } else { |
| 65 | return DriveFwd(); |
| 66 | } |
| 67 | return true; |
| 68 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 69 | |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 70 | void AutonomousActor::SendStartingPosition(double x, double y, double theta) { |
| 71 | // Set up the starting position for the blue alliance. |
| 72 | double starting_heading = theta; |
| 73 | |
| 74 | // TODO(james): Resetting the localizer breaks the left/right statespace |
| 75 | // controller. That is a bug, but we can fix that later by not resetting. |
| 76 | auto builder = localizer_control_sender_.MakeBuilder(); |
| 77 | |
| 78 | LocalizerControl::Builder localizer_control_builder = |
| 79 | builder.MakeBuilder<LocalizerControl>(); |
| 80 | localizer_control_builder.add_x(x); |
| 81 | localizer_control_builder.add_y(y); |
| 82 | localizer_control_builder.add_theta(starting_heading); |
| 83 | localizer_control_builder.add_theta_uncertainty(0.00001); |
| 84 | if (!builder.Send(localizer_control_builder.Finish())) { |
| 85 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void AutonomousActor::GalacticSearch() { |
| 90 | path_fetcher_.Fetch(); |
| 91 | CHECK(path_fetcher_.get() != nullptr) |
| 92 | << "Expect at least one GalacticSearchPath message before running " |
| 93 | "auto..."; |
| 94 | if (path_fetcher_->alliance() == y2020::vision::Alliance::kUnknown) { |
| 95 | AOS_LOG(ERROR, "The galactic search path is unknown, doing nothing."); |
| 96 | } else { |
| 97 | SplineHandle spline1 = PlanSpline( |
| 98 | [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder |
| 99 | *builder) { |
| 100 | flatbuffers::Offset<frc971::MultiSpline> target_spline; |
| 101 | if (path_fetcher_->alliance() == y2020::vision::Alliance::kRed) { |
| 102 | if (path_fetcher_->letter() == y2020::vision::Letter::kA) { |
| 103 | target_spline = auto_splines_.SplineRedA(builder); |
| 104 | } else { |
| 105 | CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB); |
| 106 | target_spline = auto_splines_.SplineRedB(builder); |
| 107 | } |
| 108 | } else { |
| 109 | if (path_fetcher_->letter() == y2020::vision::Letter::kA) { |
| 110 | target_spline = auto_splines_.SplineBlueA(builder); |
| 111 | } else { |
| 112 | CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB); |
| 113 | target_spline = auto_splines_.SplineBlueB(builder); |
| 114 | } |
| 115 | } |
| 116 | const frc971::MultiSpline *const spline = |
| 117 | flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline); |
| 118 | |
| 119 | SendStartingPosition(CHECK_NOTNULL(spline)); |
| 120 | |
| 121 | return target_spline; |
| 122 | }, |
| 123 | SplineDirection::kForward); |
| 124 | |
| 125 | if (!spline1.WaitForPlan()) return; |
| 126 | spline1.Start(); |
| 127 | |
| 128 | if (!spline1.WaitForSplineDistanceRemaining(0.02)) return; |
| 129 | } |
| 130 | } |
| 131 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 132 | void AutonomousActor::SplineAuto() { |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 133 | SplineHandle spline1 = PlanSpline( |
| 134 | [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 135 | *builder) { |
| 136 | flatbuffers::Offset<frc971::MultiSpline> target_spline; |
| 137 | target_spline = auto_splines_.TestSpline(builder); |
| 138 | const frc971::MultiSpline *const spline = |
| 139 | flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline); |
| 140 | SendStartingPosition(CHECK_NOTNULL(spline)); |
| 141 | return target_spline; |
| 142 | }, |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 143 | SplineDirection::kForward); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 144 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 145 | if (!spline1.WaitForPlan()) return; |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 146 | spline1.Start(); |
| 147 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 148 | if (!spline1.WaitForSplineDistanceRemaining(0.02)) return; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 149 | } |
| 150 | |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 151 | void AutonomousActor::SendStartingPosition( |
| 152 | const frc971::MultiSpline *const spline) { |
| 153 | float x = spline->spline_x()->Get(0); |
| 154 | float y = spline->spline_y()->Get(0); |
| 155 | |
| 156 | Eigen::Matrix<double, 2, 6> control_points; |
| 157 | for (size_t ii = 0; ii < 6; ++ii) { |
| 158 | control_points(0, ii) = spline->spline_x()->Get(ii); |
| 159 | control_points(1, ii) = spline->spline_y()->Get(ii); |
| 160 | } |
| 161 | |
| 162 | frc971::control_loops::drivetrain::Spline spline_object(control_points); |
| 163 | |
| 164 | SendStartingPosition(x, y, spline_object.Theta(0)); |
| 165 | } |
| 166 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 167 | ProfileParametersT MakeProfileParametersT(const float max_velocity, |
| 168 | const float max_acceleration) { |
| 169 | ProfileParametersT params; |
| 170 | params.max_velocity = max_velocity; |
| 171 | params.max_acceleration = max_acceleration; |
| 172 | return params; |
| 173 | } |
| 174 | |
| 175 | bool AutonomousActor::DriveFwd() { |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 176 | SendStartingPosition(0, 0, 0); |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 177 | const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 178 | const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f); |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 179 | StartDrive(1.0, 0.0, kDrive, kTurn); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 180 | return WaitForDriveDone(); |
| 181 | } |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 182 | |
| 183 | void AutonomousActor::SendSuperstructureGoal() { |
| 184 | |
| 185 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 186 | |
| 187 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 188 | intake_offset; |
| 189 | |
| 190 | { |
| 191 | StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder = |
| 192 | builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>(); |
| 193 | |
| 194 | frc971::ProfileParameters::Builder profile_params_builder = |
| 195 | builder.MakeBuilder<frc971::ProfileParameters>(); |
| 196 | profile_params_builder.add_max_velocity(0.0); |
| 197 | profile_params_builder.add_max_acceleration(0.0); |
| 198 | flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset = |
| 199 | profile_params_builder.Finish(); |
| 200 | intake_builder.add_unsafe_goal(intake_goal_); |
| 201 | intake_builder.add_profile_params(profile_params_offset); |
| 202 | intake_offset = intake_builder.Finish(); |
| 203 | } |
| 204 | |
| 205 | superstructure::Goal::Builder superstructure_builder = |
| 206 | builder.MakeBuilder<superstructure::Goal>(); |
| 207 | |
| 208 | superstructure_builder.add_intake(intake_offset); |
| 209 | superstructure_builder.add_roller_voltage(roller_voltage_); |
| 210 | superstructure_builder.add_roller_speed_compensation(kRollerSpeedCompensation); |
| 211 | |
| 212 | if (!builder.Send(superstructure_builder.Finish())) { |
| 213 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 214 | } |
| 215 | |
| 216 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 217 | } // namespace actors |
| 218 | } // namespace y2020 |