Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 1 | #include "y2024/control_loops/superstructure/aiming.h" |
| 2 | |
| 3 | #include "frc971/control_loops/aiming/aiming.h" |
| 4 | #include "frc971/control_loops/pose.h" |
| 5 | |
| 6 | using frc971::control_loops::aiming::RobotState; |
| 7 | using frc971::control_loops::aiming::ShotConfig; |
| 8 | using frc971::control_loops::aiming::ShotMode; |
| 9 | using y2024::control_loops::superstructure::Aimer; |
| 10 | |
| 11 | Aimer::Aimer(aos::EventLoop *event_loop, |
| 12 | const y2024::Constants *robot_constants) |
| 13 | : event_loop_(event_loop), |
| 14 | robot_constants_(CHECK_NOTNULL(robot_constants)), |
| 15 | drivetrain_config_( |
| 16 | frc971::control_loops::drivetrain::DrivetrainConfig<double>:: |
| 17 | FromFlatbuffer(*robot_constants_->common()->drivetrain())), |
| 18 | interpolation_table_( |
| 19 | y2024::constants::Values::InterpolationTableFromFlatbuffer( |
| 20 | robot_constants_->common()->shooter_interpolation_table())), |
Filip Kujawa | 1286c01 | 2024-03-31 22:53:27 -0700 | [diff] [blame^] | 21 | interpolation_table_shuttle_( |
| 22 | y2024::constants::Values::InterpolationTableFromFlatbuffer( |
| 23 | robot_constants_->common() |
| 24 | ->shooter_shuttle_interpolation_table())), |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 25 | joystick_state_fetcher_( |
| 26 | event_loop_->MakeFetcher<aos::JoystickState>("/aos")) {} |
| 27 | |
| 28 | void Aimer::Update( |
| 29 | const frc971::control_loops::drivetrain::Status *status, |
| 30 | frc971::control_loops::aiming::ShotMode shot_mode, |
| 31 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoalStatic |
Filip Kujawa | 1286c01 | 2024-03-31 22:53:27 -0700 | [diff] [blame^] | 32 | *turret_goal, |
| 33 | AutoAimMode auto_aim_mode) { |
| 34 | // Default to aiming at the speaker in the absence of any other targets. |
| 35 | if (auto_aim_mode == AutoAimMode::NONE) { |
| 36 | auto_aim_mode = AutoAimMode::SPEAKER; |
| 37 | } |
James Kuszmaul | 4bd2b4b | 2024-03-16 11:51:55 -0700 | [diff] [blame] | 38 | if (status == nullptr) { |
| 39 | return; |
| 40 | } |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 41 | const frc971::control_loops::Pose robot_pose({status->x(), status->y(), 0}, |
| 42 | status->theta()); |
James Kuszmaul | 4bd2b4b | 2024-03-16 11:51:55 -0700 | [diff] [blame] | 43 | aos::Alliance alliance = aos::Alliance::kRed; |
| 44 | if (!joystick_state_fetcher_.Fetch() && !received_joystick_state_) { |
| 45 | received_joystick_state_ = false; |
| 46 | } else { |
| 47 | received_joystick_state_ = true; |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 48 | |
James Kuszmaul | 4bd2b4b | 2024-03-16 11:51:55 -0700 | [diff] [blame] | 49 | CHECK_NOTNULL(joystick_state_fetcher_.get()); |
| 50 | alliance = joystick_state_fetcher_->alliance(); |
| 51 | } |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 52 | |
Filip Kujawa | 1286c01 | 2024-03-31 22:53:27 -0700 | [diff] [blame^] | 53 | frc971::shooter_interpolation::InterpolationTable< |
| 54 | y2024::constants::Values::ShotParams> *current_interpolation_table = |
| 55 | interpolation_tables_.at(auto_aim_mode); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 56 | |
| 57 | const frc971::control_loops::Pose goal = |
Filip Kujawa | 1286c01 | 2024-03-31 22:53:27 -0700 | [diff] [blame^] | 58 | alliance == aos::Alliance::kRed ? red_alliance_goals_.at(auto_aim_mode) |
| 59 | : blue_alliance_goals_.at(auto_aim_mode); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 60 | |
| 61 | const Eigen::Vector2d linear_angular = |
| 62 | drivetrain_config_.Tlr_to_la() * |
| 63 | Eigen::Vector2d(status->estimated_left_velocity(), |
| 64 | status->estimated_right_velocity()); |
| 65 | const double xdot = linear_angular(0) * std::cos(status->theta()); |
| 66 | const double ydot = linear_angular(0) * std::sin(status->theta()); |
| 67 | |
| 68 | // Use the previous shot distance to estimate the speed-over-ground of the |
| 69 | // note. |
| 70 | current_goal_ = frc971::control_loops::aiming::AimerGoal( |
| 71 | ShotConfig{goal, shot_mode, |
| 72 | frc971::constants::Range::FromFlatbuffer( |
| 73 | robot_constants_->common()->turret()->range()), |
Filip Kujawa | 1286c01 | 2024-03-31 22:53:27 -0700 | [diff] [blame^] | 74 | current_interpolation_table->Get(current_goal_.target_distance) |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 75 | .shot_speed_over_ground, |
Austin Schuh | 7cea29d | 2024-03-17 18:20:14 -0700 | [diff] [blame] | 76 | /*wrap_mode=*/0.15, M_PI - kTurretZeroOffset}, |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 77 | RobotState{ |
| 78 | robot_pose, {xdot, ydot}, linear_angular(1), current_goal_.position}); |
| 79 | |
| 80 | turret_goal->set_unsafe_goal(current_goal_.position); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | flatbuffers::Offset<AimerStatus> Aimer::PopulateStatus( |
| 84 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 85 | AimerStatus::Builder builder(*fbb); |
| 86 | builder.add_turret_position(current_goal_.position); |
| 87 | builder.add_turret_velocity(current_goal_.velocity); |
| 88 | builder.add_target_distance(current_goal_.target_distance); |
| 89 | builder.add_shot_distance(DistanceToGoal()); |
| 90 | return builder.Finish(); |
| 91 | } |