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 | |
| 12 | namespace y2024 { |
| 13 | namespace control_loops { |
| 14 | namespace superstructure { |
| 15 | |
| 16 | using ::aos::monotonic_clock; |
| 17 | |
| 18 | using frc971::control_loops::AbsoluteEncoderProfiledJointStatus; |
| 19 | using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus; |
| 20 | using frc971::control_loops::RelativeEncoderProfiledJointStatus; |
| 21 | |
| 22 | Superstructure::Superstructure(::aos::EventLoop *event_loop, |
| 23 | std::shared_ptr<const constants::Values> values, |
| 24 | const ::std::string &name) |
| 25 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
| 26 | name), |
| 27 | values_(values), |
| 28 | constants_fetcher_(event_loop), |
| 29 | drivetrain_status_fetcher_( |
| 30 | event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>( |
| 31 | "/drivetrain")), |
| 32 | joystick_state_fetcher_( |
| 33 | event_loop->MakeFetcher<aos::JoystickState>("/aos")) { |
| 34 | event_loop->SetRuntimeRealtimePriority(30); |
| 35 | } |
| 36 | |
| 37 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
| 38 | const Position *position, |
| 39 | aos::Sender<Output>::Builder *output, |
| 40 | aos::Sender<Status>::Builder *status) { |
| 41 | const monotonic_clock::time_point timestamp = |
| 42 | event_loop()->context().monotonic_event_time; |
| 43 | |
| 44 | (void)timestamp; |
| 45 | (void)unsafe_goal; |
| 46 | (void)position; |
| 47 | |
| 48 | if (WasReset()) { |
| 49 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
| 50 | } |
| 51 | |
| 52 | OutputT output_struct; |
| 53 | if (joystick_state_fetcher_.Fetch() && |
| 54 | joystick_state_fetcher_->has_alliance()) { |
| 55 | alliance_ = joystick_state_fetcher_->alliance(); |
| 56 | } |
| 57 | drivetrain_status_fetcher_.Fetch(); |
| 58 | |
| 59 | if (output) { |
| 60 | output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct))); |
| 61 | } |
| 62 | |
| 63 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
| 64 | status_builder.add_zeroed(true); |
| 65 | status_builder.add_estopped(false); |
| 66 | |
| 67 | (void)status->Send(status_builder.Finish()); |
| 68 | } |
| 69 | |
| 70 | double Superstructure::robot_velocity() const { |
| 71 | return (drivetrain_status_fetcher_.get() != nullptr |
| 72 | ? drivetrain_status_fetcher_->robot_speed() |
| 73 | : 0.0); |
| 74 | } |
| 75 | |
| 76 | } // namespace superstructure |
| 77 | } // namespace control_loops |
| 78 | } // namespace y2024 |