Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 1 | #include "y2024/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" |
| 6 | #include "frc971/shooter_interpolation/interpolation.h" |
| 7 | #include "frc971/zeroing/wrap.h" |
| 8 | |
| 9 | DEFINE_bool(ignore_distance, false, |
| 10 | "If true, ignore distance when shooting and obay joystick_reader"); |
| 11 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 12 | namespace y2024::control_loops::superstructure { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 13 | |
| 14 | using ::aos::monotonic_clock; |
| 15 | |
| 16 | using frc971::control_loops::AbsoluteEncoderProfiledJointStatus; |
| 17 | using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus; |
| 18 | using frc971::control_loops::RelativeEncoderProfiledJointStatus; |
| 19 | |
| 20 | Superstructure::Superstructure(::aos::EventLoop *event_loop, |
| 21 | std::shared_ptr<const constants::Values> values, |
| 22 | const ::std::string &name) |
| 23 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
| 24 | name), |
| 25 | values_(values), |
| 26 | constants_fetcher_(event_loop), |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 27 | robot_constants_(CHECK_NOTNULL(&constants_fetcher_.constants())), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 28 | drivetrain_status_fetcher_( |
| 29 | event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>( |
| 30 | "/drivetrain")), |
| 31 | joystick_state_fetcher_( |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 32 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
Niko Sohmers | e735fa8 | 2024-02-02 16:49:02 -0800 | [diff] [blame] | 33 | transfer_goal_(TransferRollerGoal::NONE), |
Niko Sohmers | 74b0ad5 | 2024-02-03 18:00:31 -0800 | [diff] [blame] | 34 | intake_pivot_(robot_constants_->common()->intake_pivot(), |
| 35 | robot_constants_->robot()->intake_constants()), |
Filip Kujawa | 6d71763 | 2024-02-01 11:40:55 -0800 | [diff] [blame] | 36 | climber_( |
| 37 | robot_constants_->common()->climber(), |
| 38 | robot_constants_->robot()->climber_constants()->zeroing_constants()) { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 39 | event_loop->SetRuntimeRealtimePriority(30); |
| 40 | } |
| 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) { |
| 46 | const monotonic_clock::time_point timestamp = |
| 47 | event_loop()->context().monotonic_event_time; |
| 48 | |
| 49 | (void)timestamp; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 50 | (void)position; |
| 51 | |
| 52 | if (WasReset()) { |
| 53 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 54 | intake_pivot_.Reset(); |
Filip Kujawa | 6d71763 | 2024-02-01 11:40:55 -0800 | [diff] [blame] | 55 | climber_.Reset(); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | OutputT output_struct; |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 59 | |
| 60 | double intake_pivot_position = |
| 61 | robot_constants_->common()->intake_pivot_set_points()->retracted(); |
| 62 | |
| 63 | if (unsafe_goal != nullptr && |
| 64 | unsafe_goal->intake_pivot_goal() == IntakePivotGoal::EXTENDED) { |
| 65 | intake_pivot_position = |
| 66 | robot_constants_->common()->intake_pivot_set_points()->extended(); |
| 67 | } |
| 68 | |
Filip Kujawa | 102a9b2 | 2024-02-18 09:40:23 -0800 | [diff] [blame^] | 69 | IntakeRollerStatus intake_roller_state = IntakeRollerStatus::NONE; |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 70 | |
| 71 | switch (unsafe_goal != nullptr ? unsafe_goal->intake_roller_goal() |
| 72 | : IntakeRollerGoal::NONE) { |
| 73 | case IntakeRollerGoal::NONE: |
| 74 | output_struct.intake_roller_voltage = 0.0; |
| 75 | break; |
| 76 | case IntakeRollerGoal::SPIT: |
Niko Sohmers | e735fa8 | 2024-02-02 16:49:02 -0800 | [diff] [blame] | 77 | transfer_goal_ = TransferRollerGoal::TRANSFER_OUT; |
Filip Kujawa | 102a9b2 | 2024-02-18 09:40:23 -0800 | [diff] [blame^] | 78 | intake_roller_state = IntakeRollerStatus::SPITTING; |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 79 | output_struct.intake_roller_voltage = |
| 80 | robot_constants_->common()->intake_roller_voltages()->spitting(); |
| 81 | break; |
| 82 | case IntakeRollerGoal::INTAKE: |
Niko Sohmers | e735fa8 | 2024-02-02 16:49:02 -0800 | [diff] [blame] | 83 | transfer_goal_ = TransferRollerGoal::TRANSFER_IN; |
Filip Kujawa | 102a9b2 | 2024-02-18 09:40:23 -0800 | [diff] [blame^] | 84 | intake_roller_state = IntakeRollerStatus::INTAKING; |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 85 | output_struct.intake_roller_voltage = |
| 86 | robot_constants_->common()->intake_roller_voltages()->intaking(); |
| 87 | break; |
| 88 | } |
| 89 | |
Filip Kujawa | 102a9b2 | 2024-02-18 09:40:23 -0800 | [diff] [blame^] | 90 | TransferRollerStatus transfer_roller_state = TransferRollerStatus::NONE; |
Niko Sohmers | e735fa8 | 2024-02-02 16:49:02 -0800 | [diff] [blame] | 91 | |
| 92 | switch (unsafe_goal != nullptr ? transfer_goal_ : TransferRollerGoal::NONE) { |
| 93 | case TransferRollerGoal::NONE: |
| 94 | output_struct.transfer_roller_voltage = 0.0; |
| 95 | break; |
| 96 | case TransferRollerGoal::TRANSFER_IN: |
| 97 | if (position->transfer_beambreak()) { |
| 98 | transfer_goal_ = TransferRollerGoal::NONE; |
Filip Kujawa | 102a9b2 | 2024-02-18 09:40:23 -0800 | [diff] [blame^] | 99 | transfer_roller_state = TransferRollerStatus::NONE; |
Niko Sohmers | e735fa8 | 2024-02-02 16:49:02 -0800 | [diff] [blame] | 100 | output_struct.transfer_roller_voltage = 0.0; |
| 101 | break; |
| 102 | } |
Filip Kujawa | 102a9b2 | 2024-02-18 09:40:23 -0800 | [diff] [blame^] | 103 | transfer_roller_state = TransferRollerStatus::TRANSFERING_IN; |
Niko Sohmers | e735fa8 | 2024-02-02 16:49:02 -0800 | [diff] [blame] | 104 | output_struct.transfer_roller_voltage = |
| 105 | robot_constants_->common()->transfer_roller_voltages()->transfer_in(); |
| 106 | break; |
| 107 | case TransferRollerGoal::TRANSFER_OUT: |
Filip Kujawa | 102a9b2 | 2024-02-18 09:40:23 -0800 | [diff] [blame^] | 108 | transfer_roller_state = TransferRollerStatus::TRANSFERING_OUT; |
Niko Sohmers | e735fa8 | 2024-02-02 16:49:02 -0800 | [diff] [blame] | 109 | output_struct.transfer_roller_voltage = robot_constants_->common() |
| 110 | ->transfer_roller_voltages() |
| 111 | ->transfer_out(); |
| 112 | break; |
| 113 | } |
| 114 | |
Filip Kujawa | 6d71763 | 2024-02-01 11:40:55 -0800 | [diff] [blame] | 115 | double climber_position = |
| 116 | robot_constants_->common()->climber_set_points()->retract(); |
| 117 | |
| 118 | if (unsafe_goal != nullptr) { |
| 119 | switch (unsafe_goal->climber_goal()) { |
| 120 | case ClimberGoal::FULL_EXTEND: |
| 121 | climber_position = |
| 122 | robot_constants_->common()->climber_set_points()->full_extend(); |
| 123 | break; |
| 124 | case ClimberGoal::HALF_EXTEND: |
| 125 | climber_position = |
| 126 | robot_constants_->common()->climber_set_points()->half_extend(); |
| 127 | break; |
| 128 | case ClimberGoal::RETRACT: |
| 129 | climber_position = |
| 130 | robot_constants_->common()->climber_set_points()->retract(); |
| 131 | break; |
| 132 | default: |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 137 | if (joystick_state_fetcher_.Fetch() && |
| 138 | joystick_state_fetcher_->has_alliance()) { |
| 139 | alliance_ = joystick_state_fetcher_->alliance(); |
| 140 | } |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 141 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 142 | drivetrain_status_fetcher_.Fetch(); |
| 143 | |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 144 | aos::FlatbufferFixedAllocatorArray< |
| 145 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512> |
| 146 | intake_pivot_goal_buffer; |
| 147 | |
| 148 | intake_pivot_goal_buffer.Finish( |
| 149 | frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 150 | *intake_pivot_goal_buffer.fbb(), intake_pivot_position)); |
| 151 | |
| 152 | const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal |
| 153 | *intake_pivot_goal = &intake_pivot_goal_buffer.message(); |
| 154 | |
Niko Sohmers | 74b0ad5 | 2024-02-03 18:00:31 -0800 | [diff] [blame] | 155 | const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 156 | intake_pivot_status_offset = intake_pivot_.Iterate( |
| 157 | intake_pivot_goal, position->intake_pivot(), |
| 158 | output != nullptr ? &output_struct.intake_pivot_voltage : nullptr, |
| 159 | status->fbb()); |
| 160 | |
Filip Kujawa | 6d71763 | 2024-02-01 11:40:55 -0800 | [diff] [blame] | 161 | aos::FlatbufferFixedAllocatorArray< |
| 162 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512> |
| 163 | climber_goal_buffer; |
| 164 | |
| 165 | climber_goal_buffer.Finish( |
| 166 | frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 167 | *climber_goal_buffer.fbb(), climber_position)); |
| 168 | |
| 169 | const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal |
| 170 | *climber_goal = &climber_goal_buffer.message(); |
| 171 | |
| 172 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
| 173 | climber_status_offset = climber_.Iterate( |
| 174 | climber_goal, position->climber(), |
| 175 | output != nullptr ? &output_struct.climber_voltage : nullptr, |
| 176 | status->fbb()); |
| 177 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 178 | if (output) { |
| 179 | output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct))); |
| 180 | } |
| 181 | |
| 182 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 183 | |
Filip Kujawa | 6d71763 | 2024-02-01 11:40:55 -0800 | [diff] [blame] | 184 | const bool zeroed = intake_pivot_.zeroed() && climber_.zeroed(); |
| 185 | const bool estopped = intake_pivot_.estopped() || climber_.estopped(); |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 186 | |
| 187 | status_builder.add_zeroed(zeroed); |
| 188 | status_builder.add_estopped(estopped); |
Filip Kujawa | 102a9b2 | 2024-02-18 09:40:23 -0800 | [diff] [blame^] | 189 | status_builder.add_intake_roller(intake_roller_state); |
| 190 | status_builder.add_intake_pivot(intake_pivot_status_offset); |
| 191 | status_builder.add_transfer_roller(transfer_roller_state); |
| 192 | status_builder.add_climber(climber_status_offset); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 193 | |
| 194 | (void)status->Send(status_builder.Finish()); |
| 195 | } |
| 196 | |
| 197 | double Superstructure::robot_velocity() const { |
| 198 | return (drivetrain_status_fetcher_.get() != nullptr |
| 199 | ? drivetrain_status_fetcher_->robot_speed() |
| 200 | : 0.0); |
| 201 | } |
| 202 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 203 | } // namespace y2024::control_loops::superstructure |