Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 1 | #include "y2024/autonomous/autonomous_actor.h" |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <cinttypes> |
| 5 | #include <cmath> |
| 6 | |
| 7 | #include "aos/logging/logging.h" |
| 8 | #include "aos/util/math.h" |
| 9 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
| 10 | #include "y2024/autonomous/auto_splines.h" |
| 11 | #include "y2024/constants.h" |
| 12 | #include "y2024/control_loops/drivetrain/drivetrain_base.h" |
| 13 | |
| 14 | DEFINE_bool(spline_auto, false, "Run simple test S-spline auto mode."); |
| 15 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 16 | namespace y2024::autonomous { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 17 | |
| 18 | using ::frc971::ProfileParametersT; |
| 19 | |
| 20 | ProfileParametersT MakeProfileParameters(float max_velocity, |
| 21 | float max_acceleration) { |
| 22 | ProfileParametersT result; |
| 23 | result.max_velocity = max_velocity; |
| 24 | result.max_acceleration = max_acceleration; |
| 25 | return result; |
| 26 | } |
| 27 | |
| 28 | using ::aos::monotonic_clock; |
| 29 | using frc971::CreateProfileParameters; |
| 30 | using ::frc971::ProfileParametersT; |
| 31 | using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal; |
| 32 | using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal; |
| 33 | using frc971::control_loops::drivetrain::LocalizerControl; |
| 34 | namespace chrono = ::std::chrono; |
| 35 | |
| 36 | AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop) |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame^] | 37 | : frc971::autonomous::UserButtonLocalizedAutonomousActor( |
James Kuszmaul | 2549e75 | 2024-01-20 17:42:51 -0800 | [diff] [blame] | 38 | event_loop, |
| 39 | control_loops::drivetrain::GetDrivetrainConfig(event_loop)), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 40 | localizer_control_sender_( |
| 41 | event_loop->MakeSender< |
| 42 | ::frc971::control_loops::drivetrain::LocalizerControl>( |
| 43 | "/drivetrain")), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 44 | superstructure_goal_sender_( |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame^] | 45 | event_loop |
| 46 | ->MakeSender<::y2024::control_loops::superstructure::GoalStatic>( |
| 47 | "/superstructure")), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 48 | superstructure_status_fetcher_( |
| 49 | event_loop |
| 50 | ->MakeFetcher<::y2024::control_loops::superstructure::Status>( |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame^] | 51 | "/superstructure")), |
| 52 | auto_splines_() {} |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 53 | |
| 54 | void AutonomousActor::Replan() { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 55 | if (FLAGS_spline_auto) { |
| 56 | test_spline_ = |
| 57 | PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_, |
| 58 | std::placeholders::_1, alliance_), |
| 59 | SplineDirection::kForward); |
| 60 | |
| 61 | starting_position_ = test_spline_->starting_position(); |
| 62 | } |
| 63 | |
| 64 | is_planned_ = true; |
| 65 | |
| 66 | MaybeSendStartingPosition(); |
| 67 | } |
| 68 | |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame^] | 69 | bool AutonomousActor::Run( |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 70 | const ::frc971::autonomous::AutonomousActionParams *params) { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 71 | AOS_LOG(INFO, "Params are %d\n", params->mode()); |
| 72 | if (alliance_ == aos::Alliance::kInvalid) { |
| 73 | AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection."); |
| 74 | return false; |
| 75 | } |
| 76 | if (FLAGS_spline_auto) { |
| 77 | SplineAuto(); |
| 78 | } else { |
| 79 | AOS_LOG(WARNING, "No auto mode selected."); |
| 80 | } |
| 81 | return true; |
| 82 | } |
| 83 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 84 | void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) { |
| 85 | // Set up the starting position for the blue alliance. |
| 86 | |
| 87 | auto builder = localizer_control_sender_.MakeBuilder(); |
| 88 | |
| 89 | LocalizerControl::Builder localizer_control_builder = |
| 90 | builder.MakeBuilder<LocalizerControl>(); |
| 91 | localizer_control_builder.add_x(start(0)); |
| 92 | localizer_control_builder.add_y(start(1)); |
| 93 | localizer_control_builder.add_theta(start(2)); |
| 94 | localizer_control_builder.add_theta_uncertainty(0.00001); |
| 95 | AOS_LOG(INFO, "User button pressed, x: %f y: %f theta: %f", start(0), |
| 96 | start(1), start(2)); |
| 97 | if (builder.Send(localizer_control_builder.Finish()) != |
| 98 | aos::RawSender::Error::kOk) { |
| 99 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
| 100 | } |
| 101 | } |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame^] | 102 | |
| 103 | void AutonomousActor::Reset() { |
| 104 | set_intake_goal(control_loops::superstructure::IntakeGoal::NONE); |
| 105 | set_note_goal(control_loops::superstructure::NoteGoal::NONE); |
| 106 | set_auto_aim(false); |
| 107 | set_fire(false); |
| 108 | set_preloaded(false); |
| 109 | SendSuperstructureGoal(); |
| 110 | } |
| 111 | |
| 112 | void AutonomousActor::SplineAuto() { |
| 113 | CHECK(test_spline_); |
| 114 | |
| 115 | if (!test_spline_->WaitForPlan()) return; |
| 116 | test_spline_->Start(); |
| 117 | |
| 118 | if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return; |
| 119 | } |
| 120 | |
| 121 | void AutonomousActor::SendSuperstructureGoal() { |
| 122 | aos::Sender<control_loops::superstructure::GoalStatic>::StaticBuilder |
| 123 | goal_builder = superstructure_goal_sender_.MakeStaticBuilder(); |
| 124 | |
| 125 | goal_builder->set_intake_goal(intake_goal_); |
| 126 | goal_builder->set_note_goal(note_goal_); |
| 127 | goal_builder->set_fire(fire_); |
| 128 | |
| 129 | control_loops::superstructure::ShooterGoalStatic *shooter_goal = |
| 130 | goal_builder->add_shooter_goal(); |
| 131 | |
| 132 | shooter_goal->set_auto_aim(auto_aim_); |
| 133 | shooter_goal->set_preloaded(preloaded_); |
| 134 | |
| 135 | goal_builder.CheckOk(goal_builder.Send()); |
| 136 | } |
| 137 | |
| 138 | void AutonomousActor::Intake() { |
| 139 | set_intake_goal(control_loops::superstructure::IntakeGoal::INTAKE); |
| 140 | set_note_goal(control_loops::superstructure::NoteGoal::CATAPULT); |
| 141 | SendSuperstructureGoal(); |
| 142 | } |
| 143 | |
| 144 | void AutonomousActor::Aim() { |
| 145 | set_auto_aim(true); |
| 146 | SendSuperstructureGoal(); |
| 147 | } |
| 148 | |
| 149 | void AutonomousActor::Shoot() { |
| 150 | set_fire(true); |
| 151 | SendSuperstructureGoal(); |
| 152 | } |
| 153 | |
| 154 | [[nodiscard]] bool AutonomousActor::WaitForPreloaded() { |
| 155 | set_preloaded(true); |
| 156 | SendSuperstructureGoal(); |
| 157 | |
| 158 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 159 | event_loop()->monotonic_now(), |
| 160 | aos::common::actions::kLoopOffset); |
| 161 | |
| 162 | bool loaded = false; |
| 163 | while (!loaded) { |
| 164 | if (ShouldCancel()) { |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | phased_loop.SleepUntilNext(); |
| 169 | superstructure_status_fetcher_.Fetch(); |
| 170 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
| 171 | |
| 172 | loaded = (superstructure_status_fetcher_->shooter()->catapult_state() == |
| 173 | control_loops::superstructure::CatapultState::LOADED); |
| 174 | } |
| 175 | |
| 176 | set_preloaded(false); |
| 177 | SendSuperstructureGoal(); |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 182 | } // namespace y2024::autonomous |