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())), |
| 21 | joystick_state_fetcher_( |
| 22 | event_loop_->MakeFetcher<aos::JoystickState>("/aos")) {} |
| 23 | |
| 24 | void Aimer::Update( |
| 25 | const frc971::control_loops::drivetrain::Status *status, |
| 26 | frc971::control_loops::aiming::ShotMode shot_mode, |
| 27 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoalStatic |
| 28 | *turret_goal) { |
James Kuszmaul | 4bd2b4b | 2024-03-16 11:51:55 -0700 | [diff] [blame] | 29 | if (status == nullptr) { |
| 30 | return; |
| 31 | } |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 32 | const frc971::control_loops::Pose robot_pose({status->x(), status->y(), 0}, |
| 33 | status->theta()); |
James Kuszmaul | 4bd2b4b | 2024-03-16 11:51:55 -0700 | [diff] [blame] | 34 | aos::Alliance alliance = aos::Alliance::kRed; |
| 35 | if (!joystick_state_fetcher_.Fetch() && !received_joystick_state_) { |
| 36 | received_joystick_state_ = false; |
| 37 | } else { |
| 38 | received_joystick_state_ = true; |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 39 | |
James Kuszmaul | 4bd2b4b | 2024-03-16 11:51:55 -0700 | [diff] [blame] | 40 | CHECK_NOTNULL(joystick_state_fetcher_.get()); |
| 41 | alliance = joystick_state_fetcher_->alliance(); |
| 42 | } |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 43 | |
| 44 | const frc971::control_loops::Pose red_alliance_goal( |
| 45 | frc971::ToEigenOrDie<3, 1>(*robot_constants_->common() |
| 46 | ->shooter_targets() |
| 47 | ->red_alliance() |
| 48 | ->pos()), |
| 49 | robot_constants_->common()->shooter_targets()->red_alliance()->theta()); |
| 50 | |
| 51 | const frc971::control_loops::Pose blue_alliance_goal( |
| 52 | frc971::ToEigenOrDie<3, 1>(*robot_constants_->common() |
| 53 | ->shooter_targets() |
| 54 | ->blue_alliance() |
| 55 | ->pos()), |
| 56 | robot_constants_->common()->shooter_targets()->blue_alliance()->theta()); |
| 57 | |
| 58 | const frc971::control_loops::Pose goal = |
| 59 | alliance == aos::Alliance::kRed ? red_alliance_goal : blue_alliance_goal; |
| 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()), |
| 74 | interpolation_table_.Get(current_goal_.target_distance) |
| 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 | } |