blob: c570856085886ef383e3345f0d6c9e3525bce7b9 [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
5namespace y2022 {
6namespace control_loops {
7namespace superstructure {
8namespace turret {
9
10using frc971::control_loops::Pose;
James Kuszmaul84083f42022-02-27 17:24:38 -080011using frc971::control_loops::aiming::RobotState;
Milind Upadhyaye9075d12022-04-12 22:45:16 -070012using frc971::control_loops::aiming::ShotConfig;
James Kuszmaul84083f42022-02-27 17:24:38 -080013
14namespace {
James Kuszmaul84083f42022-02-27 17:24:38 -080015// If the turret is at zero, then it will be at this angle at which the shot
16// will leave the robot. I.e., if the turret is at zero, then the shot will go
17// straight out the back of the robot.
18constexpr double kTurretZeroOffset = M_PI;
19
James Kuszmaule3fc87f2022-03-12 21:00:50 -080020constexpr double kMaxProfiledVelocity = 10.0;
Austin Schuh7003a362022-04-17 19:13:00 -070021constexpr double kMaxProfiledAccel = 25.0;
James Kuszmaule3fc87f2022-03-12 21:00:50 -080022
James Kuszmaul84083f42022-02-27 17:24:38 -080023flatbuffers::DetachedBuffer MakePrefilledGoal() {
24 flatbuffers::FlatBufferBuilder fbb;
25 fbb.ForceDefaults(true);
James Kuszmaule3fc87f2022-03-12 21:00:50 -080026 frc971::ProfileParameters::Builder profile_builder(fbb);
27 profile_builder.add_max_velocity(kMaxProfiledVelocity);
28 profile_builder.add_max_acceleration(kMaxProfiledAccel);
29 const flatbuffers::Offset<frc971::ProfileParameters> profile_offset =
30 profile_builder.Finish();
James Kuszmaul84083f42022-02-27 17:24:38 -080031 Aimer::Goal::Builder builder(fbb);
32 builder.add_unsafe_goal(0);
33 builder.add_goal_velocity(0);
James Kuszmaule3fc87f2022-03-12 21:00:50 -080034 builder.add_ignore_profile(false);
35 builder.add_profile_params(profile_offset);
James Kuszmaul84083f42022-02-27 17:24:38 -080036 fbb.Finish(builder.Finish());
37 return fbb.Release();
38}
39} // namespace
40
James Kuszmaulb9ba9a52022-03-31 22:16:01 -070041Aimer::Aimer(std::shared_ptr<const constants::Values> constants)
42 : constants_(constants), goal_(MakePrefilledGoal()) {}
James Kuszmaul84083f42022-02-27 17:24:38 -080043
44void Aimer::Update(const Status *status, ShotMode shot_mode) {
45 const Pose robot_pose({status->x(), status->y(), 0}, status->theta());
46 const Pose goal({0.0, 0.0, 0.0}, 0.0);
47
48 const Eigen::Vector2d linear_angular =
49 drivetrain::GetDrivetrainConfig().Tlr_to_la() *
50 Eigen::Vector2d(status->estimated_left_velocity(),
51 status->estimated_right_velocity());
52 const double xdot = linear_angular(0) * std::cos(status->theta());
53 const double ydot = linear_angular(0) * std::sin(status->theta());
54
James Kuszmaulb9ba9a52022-03-31 22:16:01 -070055 // Use the previous shot distance to estimate the speed-over-ground of the
56 // ball.
57 current_goal_ = frc971::control_loops::aiming::AimerGoal(
Milind Upadhyaye9075d12022-04-12 22:45:16 -070058 ShotConfig{goal, shot_mode, constants_->turret_range,
James Kuszmaulb9ba9a52022-03-31 22:16:01 -070059 constants_->shot_velocity_interpolation_table
60 .Get(current_goal_.target_distance)
61 .shot_speed_over_ground,
62 /*wrap_mode=*/0.0, kTurretZeroOffset},
63 RobotState{robot_pose,
64 {xdot, ydot},
65 linear_angular(1),
66 goal_.message().unsafe_goal()});
James Kuszmaul84083f42022-02-27 17:24:38 -080067
68 goal_.mutable_message()->mutate_unsafe_goal(current_goal_.position);
69 goal_.mutable_message()->mutate_goal_velocity(
Austin Schuh3be7f7a2022-03-13 20:35:13 -070070 std::clamp(current_goal_.velocity, 0.0, 0.0));
James Kuszmaul84083f42022-02-27 17:24:38 -080071}
72
73flatbuffers::Offset<AimerStatus> Aimer::PopulateStatus(
74 flatbuffers::FlatBufferBuilder *fbb) const {
75 AimerStatus::Builder builder(*fbb);
76 builder.add_turret_position(current_goal_.position);
77 builder.add_turret_velocity(current_goal_.velocity);
78 builder.add_target_distance(current_goal_.target_distance);
79 builder.add_shot_distance(DistanceToGoal());
80 return builder.Finish();
81}
82
83} // namespace turret
84} // namespace superstructure
85} // namespace control_loops
86} // namespace y2022