blob: b53dbb50ffa89b0a0e8102e752f0a0801be7df54 [file] [log] [blame]
Niko Sohmersc4d2c502024-02-19 19:35:35 -08001#include "y2024/control_loops/superstructure/aiming.h"
2
3#include "frc971/control_loops/aiming/aiming.h"
4#include "frc971/control_loops/pose.h"
5
6using frc971::control_loops::aiming::RobotState;
7using frc971::control_loops::aiming::ShotConfig;
8using frc971::control_loops::aiming::ShotMode;
9using y2024::control_loops::superstructure::Aimer;
10
11Aimer::Aimer(aos::EventLoop *event_loop,
12 const y2024::Constants *robot_constants)
13 : event_loop_(event_loop),
Austin Schuh6bdcc372024-06-27 14:49:11 -070014 robot_constants_([&]() {
15 CHECK(robot_constants != nullptr);
16 return robot_constants;
17 }()),
Niko Sohmersc4d2c502024-02-19 19:35:35 -080018 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())),
Filip Kujawa1286c012024-03-31 22:53:27 -070024 interpolation_table_shuttle_(
25 y2024::constants::Values::InterpolationTableFromFlatbuffer(
26 robot_constants_->common()
27 ->shooter_shuttle_interpolation_table())),
James Kuszmaul73d68882024-04-07 21:26:17 -070028 note_interpolation_table_(
29 y2024::constants::Values::NoteInterpolationTableFromFlatbuffer(
30 robot_constants_->common()->note_interpolation_table())),
Niko Sohmersc4d2c502024-02-19 19:35:35 -080031 joystick_state_fetcher_(
James Kuszmaul73d68882024-04-07 21:26:17 -070032 event_loop_->MakeFetcher<aos::JoystickState>("/aos")) {
33 event_loop_->MakeWatcher(
34 "/superstructure/rio",
35 [this](const y2024::control_loops::superstructure::CANPosition &msg) {
36 if (latch_current_) return;
37
38 if (msg.has_retention_roller()) {
39 note_current_average_.AddData(
40 msg.retention_roller()->torque_current());
41 }
42 });
43}
Niko Sohmersc4d2c502024-02-19 19:35:35 -080044
45void Aimer::Update(
46 const frc971::control_loops::drivetrain::Status *status,
47 frc971::control_loops::aiming::ShotMode shot_mode,
48 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoalStatic
Filip Kujawa1286c012024-03-31 22:53:27 -070049 *turret_goal,
50 AutoAimMode auto_aim_mode) {
51 // Default to aiming at the speaker in the absence of any other targets.
52 if (auto_aim_mode == AutoAimMode::NONE) {
53 auto_aim_mode = AutoAimMode::SPEAKER;
54 }
James Kuszmaul4bd2b4b2024-03-16 11:51:55 -070055 if (status == nullptr) {
56 return;
57 }
James Kuszmaul4bd2b4b2024-03-16 11:51:55 -070058 aos::Alliance alliance = aos::Alliance::kRed;
59 if (!joystick_state_fetcher_.Fetch() && !received_joystick_state_) {
60 received_joystick_state_ = false;
61 } else {
62 received_joystick_state_ = true;
Niko Sohmersc4d2c502024-02-19 19:35:35 -080063
Austin Schuh6bdcc372024-06-27 14:49:11 -070064 CHECK(joystick_state_fetcher_.get() != nullptr);
James Kuszmaul4bd2b4b2024-03-16 11:51:55 -070065 alliance = joystick_state_fetcher_->alliance();
66 }
Niko Sohmersc4d2c502024-02-19 19:35:35 -080067
Austin Schuh79903e82024-04-07 23:12:00 -070068 const bool ignore_localizer_pos =
69 auto_aim_mode == AutoAimMode::TURRET_SHUTTLE;
70 const Eigen::Vector3d ignore_localizer_position{
71 0.0 * (alliance == aos::Alliance::kRed ? 1.0 : -1.0), -1.0, 0.0};
72 const frc971::control_loops::Pose robot_pose(
73 ignore_localizer_pos ? ignore_localizer_position
74 : Eigen::Vector3d{status->x(), status->y(), 0},
75 status->theta());
76
Filip Kujawa1286c012024-03-31 22:53:27 -070077 frc971::shooter_interpolation::InterpolationTable<
78 y2024::constants::Values::ShotParams> *current_interpolation_table =
79 interpolation_tables_.at(auto_aim_mode);
Niko Sohmersc4d2c502024-02-19 19:35:35 -080080
81 const frc971::control_loops::Pose goal =
Filip Kujawa1286c012024-03-31 22:53:27 -070082 alliance == aos::Alliance::kRed ? red_alliance_goals_.at(auto_aim_mode)
83 : blue_alliance_goals_.at(auto_aim_mode);
Niko Sohmersc4d2c502024-02-19 19:35:35 -080084
85 const Eigen::Vector2d linear_angular =
86 drivetrain_config_.Tlr_to_la() *
87 Eigen::Vector2d(status->estimated_left_velocity(),
88 status->estimated_right_velocity());
89 const double xdot = linear_angular(0) * std::cos(status->theta());
90 const double ydot = linear_angular(0) * std::sin(status->theta());
91
92 // Use the previous shot distance to estimate the speed-over-ground of the
93 // note.
94 current_goal_ = frc971::control_loops::aiming::AimerGoal(
95 ShotConfig{goal, shot_mode,
96 frc971::constants::Range::FromFlatbuffer(
97 robot_constants_->common()->turret()->range()),
Filip Kujawa1286c012024-03-31 22:53:27 -070098 current_interpolation_table->Get(current_goal_.target_distance)
Niko Sohmersc4d2c502024-02-19 19:35:35 -080099 .shot_speed_over_ground,
James Kuszmaul73d68882024-04-07 21:26:17 -0700100 /*wrap_mode=*/0.15,
101 // If we don't have any retention roller data, the averager
102 // will return 0. If we get a current that is out-of-range of
103 // the interpolation table, we will use the terminal values of
104 // the interpolation table.
105 M_PI - note_interpolation_table_
106 .Get(note_current_average_.GetAverage()(0))
107 .turret_offset},
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800108 RobotState{
109 robot_pose, {xdot, ydot}, linear_angular(1), current_goal_.position});
110
111 turret_goal->set_unsafe_goal(current_goal_.position);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800112}
113
114flatbuffers::Offset<AimerStatus> Aimer::PopulateStatus(
115 flatbuffers::FlatBufferBuilder *fbb) const {
116 AimerStatus::Builder builder(*fbb);
117 builder.add_turret_position(current_goal_.position);
118 builder.add_turret_velocity(current_goal_.velocity);
119 builder.add_target_distance(current_goal_.target_distance);
James Kuszmaul73d68882024-04-07 21:26:17 -0700120 builder.add_note_current(note_current_average_.GetAverage()(0));
121 builder.add_current_turret_offset(
122 note_interpolation_table_.Get(note_current_average_.GetAverage()(0))
123 .turret_offset);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800124 return builder.Finish();
125}