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 | |
Maxwell Henderson | ed8bf81 | 2024-02-24 16:41:44 -0800 | [diff] [blame] | 11 | // When the turret is at 0 the note will be leaving the robot at PI. |
| 12 | static constexpr double kTurretZeroOffset = M_PI; |
| 13 | |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 14 | Aimer::Aimer(aos::EventLoop *event_loop, |
| 15 | const y2024::Constants *robot_constants) |
| 16 | : event_loop_(event_loop), |
| 17 | robot_constants_(CHECK_NOTNULL(robot_constants)), |
| 18 | drivetrain_config_( |
| 19 | frc971::control_loops::drivetrain::DrivetrainConfig<double>:: |
| 20 | FromFlatbuffer(*robot_constants_->common()->drivetrain())), |
| 21 | interpolation_table_( |
| 22 | y2024::constants::Values::InterpolationTableFromFlatbuffer( |
| 23 | robot_constants_->common()->shooter_interpolation_table())), |
| 24 | joystick_state_fetcher_( |
| 25 | event_loop_->MakeFetcher<aos::JoystickState>("/aos")) {} |
| 26 | |
| 27 | void Aimer::Update( |
| 28 | const frc971::control_loops::drivetrain::Status *status, |
| 29 | frc971::control_loops::aiming::ShotMode shot_mode, |
| 30 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoalStatic |
| 31 | *turret_goal) { |
| 32 | const frc971::control_loops::Pose robot_pose({status->x(), status->y(), 0}, |
| 33 | status->theta()); |
| 34 | joystick_state_fetcher_.Fetch(); |
| 35 | CHECK_NOTNULL(joystick_state_fetcher_.get()); |
| 36 | |
| 37 | aos::Alliance alliance = joystick_state_fetcher_->alliance(); |
| 38 | |
| 39 | const frc971::control_loops::Pose red_alliance_goal( |
| 40 | frc971::ToEigenOrDie<3, 1>(*robot_constants_->common() |
| 41 | ->shooter_targets() |
| 42 | ->red_alliance() |
| 43 | ->pos()), |
| 44 | robot_constants_->common()->shooter_targets()->red_alliance()->theta()); |
| 45 | |
| 46 | const frc971::control_loops::Pose blue_alliance_goal( |
| 47 | frc971::ToEigenOrDie<3, 1>(*robot_constants_->common() |
| 48 | ->shooter_targets() |
| 49 | ->blue_alliance() |
| 50 | ->pos()), |
| 51 | robot_constants_->common()->shooter_targets()->blue_alliance()->theta()); |
| 52 | |
| 53 | const frc971::control_loops::Pose goal = |
| 54 | alliance == aos::Alliance::kRed ? red_alliance_goal : blue_alliance_goal; |
| 55 | |
| 56 | const Eigen::Vector2d linear_angular = |
| 57 | drivetrain_config_.Tlr_to_la() * |
| 58 | Eigen::Vector2d(status->estimated_left_velocity(), |
| 59 | status->estimated_right_velocity()); |
| 60 | const double xdot = linear_angular(0) * std::cos(status->theta()); |
| 61 | const double ydot = linear_angular(0) * std::sin(status->theta()); |
| 62 | |
| 63 | // Use the previous shot distance to estimate the speed-over-ground of the |
| 64 | // note. |
| 65 | current_goal_ = frc971::control_loops::aiming::AimerGoal( |
| 66 | ShotConfig{goal, shot_mode, |
| 67 | frc971::constants::Range::FromFlatbuffer( |
| 68 | robot_constants_->common()->turret()->range()), |
| 69 | interpolation_table_.Get(current_goal_.target_distance) |
| 70 | .shot_speed_over_ground, |
Maxwell Henderson | ed8bf81 | 2024-02-24 16:41:44 -0800 | [diff] [blame] | 71 | /*wrap_mode=*/0.0, kTurretZeroOffset}, |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 72 | RobotState{ |
| 73 | robot_pose, {xdot, ydot}, linear_angular(1), current_goal_.position}); |
| 74 | |
| 75 | turret_goal->set_unsafe_goal(current_goal_.position); |
| 76 | turret_goal->set_goal_velocity(current_goal_.velocity); |
| 77 | } |
| 78 | |
| 79 | flatbuffers::Offset<AimerStatus> Aimer::PopulateStatus( |
| 80 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 81 | AimerStatus::Builder builder(*fbb); |
| 82 | builder.add_turret_position(current_goal_.position); |
| 83 | builder.add_turret_velocity(current_goal_.velocity); |
| 84 | builder.add_target_distance(current_goal_.target_distance); |
| 85 | builder.add_shot_distance(DistanceToGoal()); |
| 86 | return builder.Finish(); |
| 87 | } |