blob: a2e92fd32321765f1c534daaf0dd71acd42fcddd [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),
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 Kujawa1286c012024-03-31 22:53:27 -070021 interpolation_table_shuttle_(
22 y2024::constants::Values::InterpolationTableFromFlatbuffer(
23 robot_constants_->common()
24 ->shooter_shuttle_interpolation_table())),
James Kuszmaul73d68882024-04-07 21:26:17 -070025 note_interpolation_table_(
26 y2024::constants::Values::NoteInterpolationTableFromFlatbuffer(
27 robot_constants_->common()->note_interpolation_table())),
Niko Sohmersc4d2c502024-02-19 19:35:35 -080028 joystick_state_fetcher_(
James Kuszmaul73d68882024-04-07 21:26:17 -070029 event_loop_->MakeFetcher<aos::JoystickState>("/aos")) {
30 event_loop_->MakeWatcher(
31 "/superstructure/rio",
32 [this](const y2024::control_loops::superstructure::CANPosition &msg) {
33 if (latch_current_) return;
34
35 if (msg.has_retention_roller()) {
36 note_current_average_.AddData(
37 msg.retention_roller()->torque_current());
38 }
39 });
40}
Niko Sohmersc4d2c502024-02-19 19:35:35 -080041
42void Aimer::Update(
43 const frc971::control_loops::drivetrain::Status *status,
44 frc971::control_loops::aiming::ShotMode shot_mode,
45 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoalStatic
Filip Kujawa1286c012024-03-31 22:53:27 -070046 *turret_goal,
47 AutoAimMode auto_aim_mode) {
48 // Default to aiming at the speaker in the absence of any other targets.
49 if (auto_aim_mode == AutoAimMode::NONE) {
50 auto_aim_mode = AutoAimMode::SPEAKER;
51 }
James Kuszmaul4bd2b4b2024-03-16 11:51:55 -070052 if (status == nullptr) {
53 return;
54 }
Niko Sohmersc4d2c502024-02-19 19:35:35 -080055 const frc971::control_loops::Pose robot_pose({status->x(), status->y(), 0},
56 status->theta());
James Kuszmaul4bd2b4b2024-03-16 11:51:55 -070057 aos::Alliance alliance = aos::Alliance::kRed;
58 if (!joystick_state_fetcher_.Fetch() && !received_joystick_state_) {
59 received_joystick_state_ = false;
60 } else {
61 received_joystick_state_ = true;
Niko Sohmersc4d2c502024-02-19 19:35:35 -080062
James Kuszmaul4bd2b4b2024-03-16 11:51:55 -070063 CHECK_NOTNULL(joystick_state_fetcher_.get());
64 alliance = joystick_state_fetcher_->alliance();
65 }
Niko Sohmersc4d2c502024-02-19 19:35:35 -080066
Filip Kujawa1286c012024-03-31 22:53:27 -070067 frc971::shooter_interpolation::InterpolationTable<
68 y2024::constants::Values::ShotParams> *current_interpolation_table =
69 interpolation_tables_.at(auto_aim_mode);
Niko Sohmersc4d2c502024-02-19 19:35:35 -080070
71 const frc971::control_loops::Pose goal =
Filip Kujawa1286c012024-03-31 22:53:27 -070072 alliance == aos::Alliance::kRed ? red_alliance_goals_.at(auto_aim_mode)
73 : blue_alliance_goals_.at(auto_aim_mode);
Niko Sohmersc4d2c502024-02-19 19:35:35 -080074
75 const Eigen::Vector2d linear_angular =
76 drivetrain_config_.Tlr_to_la() *
77 Eigen::Vector2d(status->estimated_left_velocity(),
78 status->estimated_right_velocity());
79 const double xdot = linear_angular(0) * std::cos(status->theta());
80 const double ydot = linear_angular(0) * std::sin(status->theta());
81
82 // Use the previous shot distance to estimate the speed-over-ground of the
83 // note.
84 current_goal_ = frc971::control_loops::aiming::AimerGoal(
85 ShotConfig{goal, shot_mode,
86 frc971::constants::Range::FromFlatbuffer(
87 robot_constants_->common()->turret()->range()),
Filip Kujawa1286c012024-03-31 22:53:27 -070088 current_interpolation_table->Get(current_goal_.target_distance)
Niko Sohmersc4d2c502024-02-19 19:35:35 -080089 .shot_speed_over_ground,
James Kuszmaul73d68882024-04-07 21:26:17 -070090 /*wrap_mode=*/0.15,
91 // If we don't have any retention roller data, the averager
92 // will return 0. If we get a current that is out-of-range of
93 // the interpolation table, we will use the terminal values of
94 // the interpolation table.
95 M_PI - note_interpolation_table_
96 .Get(note_current_average_.GetAverage()(0))
97 .turret_offset},
Niko Sohmersc4d2c502024-02-19 19:35:35 -080098 RobotState{
99 robot_pose, {xdot, ydot}, linear_angular(1), current_goal_.position});
100
101 turret_goal->set_unsafe_goal(current_goal_.position);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800102}
103
104flatbuffers::Offset<AimerStatus> Aimer::PopulateStatus(
105 flatbuffers::FlatBufferBuilder *fbb) const {
106 AimerStatus::Builder builder(*fbb);
107 builder.add_turret_position(current_goal_.position);
108 builder.add_turret_velocity(current_goal_.velocity);
109 builder.add_target_distance(current_goal_.target_distance);
James Kuszmaul73d68882024-04-07 21:26:17 -0700110 builder.add_note_current(note_current_average_.GetAverage()(0));
111 builder.add_current_turret_offset(
112 note_interpolation_table_.Get(note_current_average_.GetAverage()(0))
113 .turret_offset);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800114 return builder.Finish();
115}