Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 1 | #include "y2023/control_loops/superstructure/superstructure.h" |
| 2 | |
| 3 | #include "aos/events/event_loop.h" |
| 4 | #include "aos/flatbuffer_merge.h" |
| 5 | #include "aos/network/team_number.h" |
James Kuszmaul | cf451fb | 2023-03-10 20:42:36 -0800 | [diff] [blame] | 6 | #include "frc971/shooter_interpolation/interpolation.h" |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 7 | #include "frc971/zeroing/wrap.h" |
Maxwell Henderson | b392b74 | 2023-03-05 07:53:51 -0800 | [diff] [blame] | 8 | #include "y2023/control_loops/superstructure/arm/arm_trajectories_generated.h" |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 9 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 10 | ABSL_FLAG(bool, ignore_distance, false, |
| 11 | "If true, ignore distance when shooting and obay joystick_reader"); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 12 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 13 | namespace y2023::control_loops::superstructure { |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 14 | |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 15 | using ::aos::monotonic_clock; |
| 16 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 17 | using frc971::control_loops::AbsoluteEncoderProfiledJointStatus; |
| 18 | using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus; |
| 19 | using frc971::control_loops::RelativeEncoderProfiledJointStatus; |
| 20 | |
Maxwell Henderson | b392b74 | 2023-03-05 07:53:51 -0800 | [diff] [blame] | 21 | Superstructure::Superstructure( |
| 22 | ::aos::EventLoop *event_loop, |
| 23 | std::shared_ptr<const constants::Values> values, |
| 24 | const aos::FlatbufferVector<ArmTrajectories> &arm_trajectories, |
| 25 | const ::std::string &name) |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 26 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
| 27 | name), |
| 28 | values_(values), |
James Kuszmaul | cf451fb | 2023-03-10 20:42:36 -0800 | [diff] [blame] | 29 | constants_fetcher_(event_loop), |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 30 | drivetrain_status_fetcher_( |
| 31 | event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>( |
| 32 | "/drivetrain")), |
| 33 | joystick_state_fetcher_( |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 34 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
Maxwell Henderson | b392b74 | 2023-03-05 07:53:51 -0800 | [diff] [blame] | 35 | arm_(values_, arm_trajectories.message()), |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 36 | end_effector_(), |
James Kuszmaul | 630ab1d | 2024-01-09 16:38:57 -0800 | [diff] [blame] | 37 | wrist_(constants_fetcher_.constants().wrist(), |
| 38 | constants_fetcher_.constants().robot()->wrist_zero()) { |
Austin Schuh | 595ffc7 | 2023-02-24 16:24:37 -0800 | [diff] [blame] | 39 | event_loop->SetRuntimeRealtimePriority(30); |
| 40 | } |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 41 | |
| 42 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
| 43 | const Position *position, |
| 44 | aos::Sender<Output>::Builder *output, |
| 45 | aos::Sender<Status>::Builder *status) { |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 46 | const monotonic_clock::time_point timestamp = |
| 47 | event_loop()->context().monotonic_event_time; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 48 | |
| 49 | if (WasReset()) { |
| 50 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 51 | arm_.Reset(); |
Maxwell Henderson | 589cf27 | 2023-02-22 15:56:40 -0800 | [diff] [blame] | 52 | end_effector_.Reset(); |
Austin Schuh | bdabc70 | 2023-02-24 16:21:07 -0800 | [diff] [blame] | 53 | wrist_.Reset(); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | OutputT output_struct; |
| 57 | if (joystick_state_fetcher_.Fetch() && |
| 58 | joystick_state_fetcher_->has_alliance()) { |
| 59 | alliance_ = joystick_state_fetcher_->alliance(); |
| 60 | } |
| 61 | drivetrain_status_fetcher_.Fetch(); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 62 | |
| 63 | const uint32_t arm_goal_position = |
| 64 | unsafe_goal != nullptr ? unsafe_goal->arm_goal_position() : 0u; |
| 65 | |
| 66 | flatbuffers::Offset<superstructure::ArmStatus> arm_status_offset = |
| 67 | arm_.Iterate( |
| 68 | timestamp, unsafe_goal != nullptr ? &(arm_goal_position) : nullptr, |
| 69 | position->arm(), |
| 70 | unsafe_goal != nullptr ? unsafe_goal->trajectory_override() : false, |
| 71 | output != nullptr ? &output_struct.proximal_voltage : nullptr, |
| 72 | output != nullptr ? &output_struct.distal_voltage : nullptr, |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 73 | output != nullptr ? &output_struct.roll_joint_voltage : nullptr, |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 74 | status->fbb()); |
| 75 | |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 76 | flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> wrist_offset = |
Austin Schuh | bdabc70 | 2023-02-24 16:21:07 -0800 | [diff] [blame] | 77 | wrist_.Iterate( |
| 78 | unsafe_goal != nullptr ? unsafe_goal->wrist() : nullptr, |
| 79 | position->wrist(), |
| 80 | output != nullptr ? &(output_struct.wrist_voltage) : nullptr, |
| 81 | status->fbb()); |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 82 | |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 83 | end_effector_.RunIteration( |
Maxwell Henderson | 5938a83 | 2023-02-23 09:33:15 -0800 | [diff] [blame] | 84 | timestamp, |
| 85 | unsafe_goal != nullptr ? unsafe_goal->roller_goal() : RollerGoal::IDLE, |
milind-u | 6ff6d9e | 2023-02-24 20:15:06 -0800 | [diff] [blame] | 86 | position->has_roller_falcon() |
| 87 | ? position->roller_falcon()->torque_current() |
| 88 | : 0.0, |
| 89 | position->cone_position(), position->end_effector_cube_beam_break(), |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 90 | &output_struct.roller_voltage, |
| 91 | unsafe_goal != nullptr ? unsafe_goal->preloaded_with_cone() : false); |
Maxwell Henderson | 589cf27 | 2023-02-22 15:56:40 -0800 | [diff] [blame] | 92 | |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 93 | if (output) { |
| 94 | output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct))); |
| 95 | } |
| 96 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 97 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
Austin Schuh | bdabc70 | 2023-02-24 16:21:07 -0800 | [diff] [blame] | 98 | status_builder.add_zeroed(wrist_.zeroed() && arm_.zeroed()); |
| 99 | status_builder.add_estopped(wrist_.estopped() || arm_.estopped()); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 100 | status_builder.add_arm(arm_status_offset); |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 101 | status_builder.add_wrist(wrist_offset); |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 102 | status_builder.add_end_effector_state(end_effector_.state()); |
| 103 | // TODO(milind): integrate this with ML game piece detection somehow |
| 104 | status_builder.add_game_piece(end_effector_.game_piece()); |
James Kuszmaul | cf451fb | 2023-03-10 20:42:36 -0800 | [diff] [blame] | 105 | const std::optional<double> game_piece_position = |
| 106 | LateralOffsetForTimeOfFlight(position->cone_position()); |
| 107 | if (game_piece_position.has_value()) { |
| 108 | status_builder.add_game_piece_position(game_piece_position.value()); |
| 109 | } |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 110 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 111 | (void)status->Send(status_builder.Finish()); |
| 112 | } |
| 113 | |
| 114 | double Superstructure::robot_velocity() const { |
| 115 | return (drivetrain_status_fetcher_.get() != nullptr |
| 116 | ? drivetrain_status_fetcher_->robot_speed() |
| 117 | : 0.0); |
| 118 | } |
| 119 | |
James Kuszmaul | cf451fb | 2023-03-10 20:42:36 -0800 | [diff] [blame] | 120 | std::optional<double> Superstructure::LateralOffsetForTimeOfFlight( |
| 121 | double reading) { |
| 122 | switch (end_effector_.game_piece()) { |
| 123 | case vision::Class::NONE: |
| 124 | return std::nullopt; |
| 125 | case vision::Class::CUBE: |
| 126 | // Cubes are definitionally centered. |
| 127 | return 0.0; |
| 128 | case vision::Class::CONE_UP: |
| 129 | case vision::Class::CONE_DOWN: |
| 130 | // execute logic below. |
| 131 | break; |
| 132 | } |
| 133 | constexpr double kInvalidReading = 0.93; |
James Kuszmaul | 4fe5a77 | 2023-04-05 20:17:43 -0700 | [diff] [blame] | 134 | if (reading > kInvalidReading || !std::isfinite(reading)) { |
James Kuszmaul | cf451fb | 2023-03-10 20:42:36 -0800 | [diff] [blame] | 135 | return std::nullopt; |
| 136 | } |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 137 | auto robot = constants_fetcher_.constants().robot(); |
| 138 | CHECK(robot != nullptr); |
| 139 | const TimeOfFlight *calibration = |
| 140 | constants_fetcher_.constants().robot()->tof(); |
| 141 | CHECK(calibration != nullptr); |
James Kuszmaul | cf451fb | 2023-03-10 20:42:36 -0800 | [diff] [blame] | 142 | // TODO(james): Use a generic interpolation table class. |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 143 | auto table = calibration->interpolation_table(); |
| 144 | CHECK(table != nullptr); |
James Kuszmaul | cf451fb | 2023-03-10 20:42:36 -0800 | [diff] [blame] | 145 | CHECK_EQ(2u, table->size()); |
| 146 | double x1 = table->Get(0)->tof_reading(); |
| 147 | double x2 = table->Get(1)->tof_reading(); |
| 148 | double y1 = table->Get(0)->lateral_position(); |
| 149 | double y2 = table->Get(1)->lateral_position(); |
| 150 | return frc971::shooter_interpolation::Blend((reading - x1) / (x2 - x1), y1, |
| 151 | y2); |
| 152 | } |
| 153 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 154 | } // namespace y2023::control_loops::superstructure |