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")), |
| 33 | intake_pivot_( |
| 34 | robot_constants_->common()->intake_pivot(), |
| 35 | robot_constants_->robot()->intake_constants()->intake_pivot_zero()) { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 36 | event_loop->SetRuntimeRealtimePriority(30); |
| 37 | } |
| 38 | |
| 39 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
| 40 | const Position *position, |
| 41 | aos::Sender<Output>::Builder *output, |
| 42 | aos::Sender<Status>::Builder *status) { |
| 43 | const monotonic_clock::time_point timestamp = |
| 44 | event_loop()->context().monotonic_event_time; |
| 45 | |
| 46 | (void)timestamp; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 47 | (void)position; |
| 48 | |
| 49 | if (WasReset()) { |
| 50 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame^] | 51 | intake_pivot_.Reset(); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | OutputT output_struct; |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame^] | 55 | |
| 56 | double intake_pivot_position = |
| 57 | robot_constants_->common()->intake_pivot_set_points()->retracted(); |
| 58 | |
| 59 | if (unsafe_goal != nullptr && |
| 60 | unsafe_goal->intake_pivot_goal() == IntakePivotGoal::EXTENDED) { |
| 61 | intake_pivot_position = |
| 62 | robot_constants_->common()->intake_pivot_set_points()->extended(); |
| 63 | } |
| 64 | |
| 65 | IntakeRollerState intake_roller_state = IntakeRollerState::NONE; |
| 66 | |
| 67 | switch (unsafe_goal != nullptr ? unsafe_goal->intake_roller_goal() |
| 68 | : IntakeRollerGoal::NONE) { |
| 69 | case IntakeRollerGoal::NONE: |
| 70 | output_struct.intake_roller_voltage = 0.0; |
| 71 | break; |
| 72 | case IntakeRollerGoal::SPIT: |
| 73 | intake_roller_state = IntakeRollerState::SPITTING; |
| 74 | output_struct.intake_roller_voltage = |
| 75 | robot_constants_->common()->intake_roller_voltages()->spitting(); |
| 76 | break; |
| 77 | case IntakeRollerGoal::INTAKE: |
| 78 | intake_roller_state = IntakeRollerState::INTAKING; |
| 79 | output_struct.intake_roller_voltage = |
| 80 | robot_constants_->common()->intake_roller_voltages()->intaking(); |
| 81 | break; |
| 82 | } |
| 83 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 84 | if (joystick_state_fetcher_.Fetch() && |
| 85 | joystick_state_fetcher_->has_alliance()) { |
| 86 | alliance_ = joystick_state_fetcher_->alliance(); |
| 87 | } |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame^] | 88 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 89 | drivetrain_status_fetcher_.Fetch(); |
| 90 | |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame^] | 91 | aos::FlatbufferFixedAllocatorArray< |
| 92 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512> |
| 93 | intake_pivot_goal_buffer; |
| 94 | |
| 95 | intake_pivot_goal_buffer.Finish( |
| 96 | frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 97 | *intake_pivot_goal_buffer.fbb(), intake_pivot_position)); |
| 98 | |
| 99 | const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal |
| 100 | *intake_pivot_goal = &intake_pivot_goal_buffer.message(); |
| 101 | |
| 102 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
| 103 | intake_pivot_status_offset = intake_pivot_.Iterate( |
| 104 | intake_pivot_goal, position->intake_pivot(), |
| 105 | output != nullptr ? &output_struct.intake_pivot_voltage : nullptr, |
| 106 | status->fbb()); |
| 107 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 108 | if (output) { |
| 109 | output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct))); |
| 110 | } |
| 111 | |
| 112 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame^] | 113 | |
| 114 | const bool zeroed = intake_pivot_.zeroed(); |
| 115 | const bool estopped = intake_pivot_.estopped(); |
| 116 | |
| 117 | status_builder.add_zeroed(zeroed); |
| 118 | status_builder.add_estopped(estopped); |
| 119 | status_builder.add_intake_roller_state(intake_roller_state); |
| 120 | status_builder.add_intake_pivot_state(intake_pivot_status_offset); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 121 | |
| 122 | (void)status->Send(status_builder.Finish()); |
| 123 | } |
| 124 | |
| 125 | double Superstructure::robot_velocity() const { |
| 126 | return (drivetrain_status_fetcher_.get() != nullptr |
| 127 | ? drivetrain_status_fetcher_->robot_speed() |
| 128 | : 0.0); |
| 129 | } |
| 130 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 131 | } // namespace y2024::control_loops::superstructure |