blob: 18ebafac0f2b26474d67d4946a81a85a22f5ec27 [file] [log] [blame]
James Kuszmaul84083f42022-02-27 17:24:38 -08001#include "y2022/control_loops/superstructure/turret/aiming.h"
2
James Kuszmaul84083f42022-02-27 17:24:38 -08003#include "y2022/control_loops/drivetrain/drivetrain_base.h"
4
Stephan Pleinesf63bde82024-01-13 15:59:33 -08005namespace y2022::control_loops::superstructure::turret {
James Kuszmaul84083f42022-02-27 17:24:38 -08006
7using frc971::control_loops::Pose;
James Kuszmaul84083f42022-02-27 17:24:38 -08008using frc971::control_loops::aiming::RobotState;
Milind Upadhyaye9075d12022-04-12 22:45:16 -07009using frc971::control_loops::aiming::ShotConfig;
James Kuszmaul84083f42022-02-27 17:24:38 -080010
11namespace {
James Kuszmaul84083f42022-02-27 17:24:38 -080012// If the turret is at zero, then it will be at this angle at which the shot
13// will leave the robot. I.e., if the turret is at zero, then the shot will go
14// straight out the back of the robot.
15constexpr double kTurretZeroOffset = M_PI;
16
James Kuszmaule3fc87f2022-03-12 21:00:50 -080017constexpr double kMaxProfiledVelocity = 10.0;
Austin Schuh7003a362022-04-17 19:13:00 -070018constexpr double kMaxProfiledAccel = 25.0;
James Kuszmaule3fc87f2022-03-12 21:00:50 -080019
James Kuszmaul84083f42022-02-27 17:24:38 -080020flatbuffers::DetachedBuffer MakePrefilledGoal() {
21 flatbuffers::FlatBufferBuilder fbb;
22 fbb.ForceDefaults(true);
James Kuszmaule3fc87f2022-03-12 21:00:50 -080023 frc971::ProfileParameters::Builder profile_builder(fbb);
24 profile_builder.add_max_velocity(kMaxProfiledVelocity);
25 profile_builder.add_max_acceleration(kMaxProfiledAccel);
26 const flatbuffers::Offset<frc971::ProfileParameters> profile_offset =
27 profile_builder.Finish();
James Kuszmaul84083f42022-02-27 17:24:38 -080028 Aimer::Goal::Builder builder(fbb);
29 builder.add_unsafe_goal(0);
30 builder.add_goal_velocity(0);
James Kuszmaule3fc87f2022-03-12 21:00:50 -080031 builder.add_ignore_profile(false);
32 builder.add_profile_params(profile_offset);
James Kuszmaul84083f42022-02-27 17:24:38 -080033 fbb.Finish(builder.Finish());
34 return fbb.Release();
35}
36} // namespace
37
James Kuszmaulb9ba9a52022-03-31 22:16:01 -070038Aimer::Aimer(std::shared_ptr<const constants::Values> constants)
39 : constants_(constants), goal_(MakePrefilledGoal()) {}
James Kuszmaul84083f42022-02-27 17:24:38 -080040
41void Aimer::Update(const Status *status, ShotMode shot_mode) {
42 const Pose robot_pose({status->x(), status->y(), 0}, status->theta());
43 const Pose goal({0.0, 0.0, 0.0}, 0.0);
44
45 const Eigen::Vector2d linear_angular =
46 drivetrain::GetDrivetrainConfig().Tlr_to_la() *
47 Eigen::Vector2d(status->estimated_left_velocity(),
48 status->estimated_right_velocity());
49 const double xdot = linear_angular(0) * std::cos(status->theta());
50 const double ydot = linear_angular(0) * std::sin(status->theta());
51
James Kuszmaulb9ba9a52022-03-31 22:16:01 -070052 // Use the previous shot distance to estimate the speed-over-ground of the
53 // ball.
54 current_goal_ = frc971::control_loops::aiming::AimerGoal(
Milind Upadhyaye9075d12022-04-12 22:45:16 -070055 ShotConfig{goal, shot_mode, constants_->turret_range,
James Kuszmaulb9ba9a52022-03-31 22:16:01 -070056 constants_->shot_velocity_interpolation_table
57 .Get(current_goal_.target_distance)
58 .shot_speed_over_ground,
59 /*wrap_mode=*/0.0, kTurretZeroOffset},
60 RobotState{robot_pose,
61 {xdot, ydot},
62 linear_angular(1),
63 goal_.message().unsafe_goal()});
James Kuszmaul84083f42022-02-27 17:24:38 -080064
65 goal_.mutable_message()->mutate_unsafe_goal(current_goal_.position);
66 goal_.mutable_message()->mutate_goal_velocity(
Austin Schuh3be7f7a2022-03-13 20:35:13 -070067 std::clamp(current_goal_.velocity, 0.0, 0.0));
James Kuszmaul84083f42022-02-27 17:24:38 -080068}
69
70flatbuffers::Offset<AimerStatus> Aimer::PopulateStatus(
71 flatbuffers::FlatBufferBuilder *fbb) const {
72 AimerStatus::Builder builder(*fbb);
73 builder.add_turret_position(current_goal_.position);
74 builder.add_turret_velocity(current_goal_.velocity);
75 builder.add_target_distance(current_goal_.target_distance);
76 builder.add_shot_distance(DistanceToGoal());
77 return builder.Finish();
78}
79
Stephan Pleinesf63bde82024-01-13 15:59:33 -080080} // namespace y2022::control_loops::superstructure::turret