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 | |
| 10 | DEFINE_bool(ignore_distance, false, |
| 11 | "If true, ignore distance when shooting and obay joystick_reader"); |
| 12 | |
| 13 | namespace y2023 { |
| 14 | namespace control_loops { |
| 15 | namespace superstructure { |
| 16 | |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 17 | using ::aos::monotonic_clock; |
| 18 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 19 | using frc971::control_loops::AbsoluteEncoderProfiledJointStatus; |
| 20 | using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus; |
| 21 | using frc971::control_loops::RelativeEncoderProfiledJointStatus; |
| 22 | |
Maxwell Henderson | b392b74 | 2023-03-05 07:53:51 -0800 | [diff] [blame] | 23 | Superstructure::Superstructure( |
| 24 | ::aos::EventLoop *event_loop, |
| 25 | std::shared_ptr<const constants::Values> values, |
| 26 | const aos::FlatbufferVector<ArmTrajectories> &arm_trajectories, |
| 27 | const ::std::string &name) |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 28 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
| 29 | name), |
| 30 | values_(values), |
James Kuszmaul | cf451fb | 2023-03-10 20:42:36 -0800 | [diff] [blame] | 31 | constants_fetcher_(event_loop), |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 32 | drivetrain_status_fetcher_( |
| 33 | event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>( |
| 34 | "/drivetrain")), |
| 35 | joystick_state_fetcher_( |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 36 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
Maxwell Henderson | b392b74 | 2023-03-05 07:53:51 -0800 | [diff] [blame] | 37 | arm_(values_, arm_trajectories.message()), |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 38 | end_effector_(), |
Austin Schuh | 595ffc7 | 2023-02-24 16:24:37 -0800 | [diff] [blame] | 39 | wrist_(values->wrist.subsystem_params) { |
| 40 | event_loop->SetRuntimeRealtimePriority(30); |
| 41 | } |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 42 | |
| 43 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
| 44 | const Position *position, |
| 45 | aos::Sender<Output>::Builder *output, |
| 46 | aos::Sender<Status>::Builder *status) { |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 47 | const monotonic_clock::time_point timestamp = |
| 48 | event_loop()->context().monotonic_event_time; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 49 | |
| 50 | if (WasReset()) { |
| 51 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 52 | arm_.Reset(); |
Maxwell Henderson | 589cf27 | 2023-02-22 15:56:40 -0800 | [diff] [blame] | 53 | end_effector_.Reset(); |
Austin Schuh | bdabc70 | 2023-02-24 16:21:07 -0800 | [diff] [blame] | 54 | wrist_.Reset(); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | OutputT output_struct; |
| 58 | if (joystick_state_fetcher_.Fetch() && |
| 59 | joystick_state_fetcher_->has_alliance()) { |
| 60 | alliance_ = joystick_state_fetcher_->alliance(); |
| 61 | } |
| 62 | drivetrain_status_fetcher_.Fetch(); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 63 | |
| 64 | const uint32_t arm_goal_position = |
| 65 | unsafe_goal != nullptr ? unsafe_goal->arm_goal_position() : 0u; |
| 66 | |
| 67 | flatbuffers::Offset<superstructure::ArmStatus> arm_status_offset = |
| 68 | arm_.Iterate( |
| 69 | timestamp, unsafe_goal != nullptr ? &(arm_goal_position) : nullptr, |
| 70 | position->arm(), |
| 71 | unsafe_goal != nullptr ? unsafe_goal->trajectory_override() : false, |
| 72 | output != nullptr ? &output_struct.proximal_voltage : nullptr, |
| 73 | output != nullptr ? &output_struct.distal_voltage : nullptr, |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 74 | output != nullptr ? &output_struct.roll_joint_voltage : nullptr, |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 75 | status->fbb()); |
| 76 | |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 77 | flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> wrist_offset = |
Austin Schuh | bdabc70 | 2023-02-24 16:21:07 -0800 | [diff] [blame] | 78 | wrist_.Iterate( |
| 79 | unsafe_goal != nullptr ? unsafe_goal->wrist() : nullptr, |
| 80 | position->wrist(), |
| 81 | output != nullptr ? &(output_struct.wrist_voltage) : nullptr, |
| 82 | status->fbb()); |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 83 | |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 84 | end_effector_.RunIteration( |
Maxwell Henderson | 5938a83 | 2023-02-23 09:33:15 -0800 | [diff] [blame] | 85 | timestamp, |
| 86 | unsafe_goal != nullptr ? unsafe_goal->roller_goal() : RollerGoal::IDLE, |
milind-u | 6ff6d9e | 2023-02-24 20:15:06 -0800 | [diff] [blame] | 87 | position->has_roller_falcon() |
| 88 | ? position->roller_falcon()->torque_current() |
| 89 | : 0.0, |
| 90 | position->cone_position(), position->end_effector_cube_beam_break(), |
| 91 | &output_struct.roller_voltage); |
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; |
| 134 | if (reading > kInvalidReading) { |
| 135 | return std::nullopt; |
| 136 | } |
| 137 | const TimeOfFlight *calibration = CHECK_NOTNULL( |
| 138 | CHECK_NOTNULL(constants_fetcher_.constants().robot())->tof()); |
| 139 | // TODO(james): Use a generic interpolation table class. |
| 140 | auto table = CHECK_NOTNULL(calibration->interpolation_table()); |
| 141 | CHECK_EQ(2u, table->size()); |
| 142 | double x1 = table->Get(0)->tof_reading(); |
| 143 | double x2 = table->Get(1)->tof_reading(); |
| 144 | double y1 = table->Get(0)->lateral_position(); |
| 145 | double y2 = table->Get(1)->lateral_position(); |
| 146 | return frc971::shooter_interpolation::Blend((reading - x1) / (x2 - x1), y1, |
| 147 | y2); |
| 148 | } |
| 149 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 150 | } // namespace superstructure |
| 151 | } // namespace control_loops |
| 152 | } // namespace y2023 |