James (Peilun) Li | a70e575 | 2024-09-18 20:43:00 -0700 | [diff] [blame] | 1 | #include "y2024_bot3/control_loops/superstructure/superstructure.h" |
| 2 | |
| 3 | #include <chrono> |
| 4 | |
| 5 | #include "aos/events/event_loop.h" |
| 6 | #include "aos/flatbuffer_merge.h" |
| 7 | #include "aos/network/team_number.h" |
| 8 | #include "aos/time/time.h" |
| 9 | #include "frc971/zeroing/wrap.h" |
| 10 | |
| 11 | ABSL_FLAG(bool, ignore_distance, false, |
| 12 | "If true, ignore distance when shooting and obey joystick_reader"); |
| 13 | |
| 14 | namespace y2024_bot3::control_loops::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 | const ::std::string &name) |
| 24 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
| 25 | name), |
| 26 | constants_fetcher_(event_loop), |
| 27 | robot_constants_(&constants_fetcher_.constants()), |
| 28 | joystick_state_fetcher_( |
James (Peilun) Li | e9ee3e6 | 2024-11-03 21:24:21 -0800 | [diff] [blame] | 29 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
| 30 | arm_(robot_constants_->common()->arm(), |
| 31 | robot_constants_->robot()->arm_constants()->zeroing_constants()) { |
James (Peilun) Li | a70e575 | 2024-09-18 20:43:00 -0700 | [diff] [blame] | 32 | event_loop->SetRuntimeRealtimePriority(30); |
| 33 | } |
| 34 | |
| 35 | bool PositionNear(double position, double goal, double threshold) { |
| 36 | return std::abs(position - goal) < threshold; |
| 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) { |
James (Peilun) Li | a70e575 | 2024-09-18 20:43:00 -0700 | [diff] [blame] | 43 | if (WasReset()) { |
| 44 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
| 45 | } |
| 46 | |
| 47 | OutputT output_struct; |
| 48 | |
James (Peilun) Li | a70e575 | 2024-09-18 20:43:00 -0700 | [diff] [blame] | 49 | if (joystick_state_fetcher_.Fetch() && |
| 50 | joystick_state_fetcher_->has_alliance()) { |
| 51 | alliance_ = joystick_state_fetcher_->alliance(); |
| 52 | } |
| 53 | |
James (Peilun) Li | e9ee3e6 | 2024-11-03 21:24:21 -0800 | [diff] [blame] | 54 | aos::FlatbufferFixedAllocatorArray< |
| 55 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512> |
| 56 | arm_goal_buffer; |
| 57 | |
| 58 | ArmStatus arm_status = ArmStatus::IDLE; |
| 59 | double arm_position = |
| 60 | robot_constants_->robot()->arm_constants()->arm_positions()->idle(); |
| 61 | if (unsafe_goal != nullptr) { |
| 62 | switch (unsafe_goal->arm_position()) { |
| 63 | case PivotGoal::INTAKE: |
| 64 | arm_status = ArmStatus::INTAKE; |
| 65 | arm_position = robot_constants_->robot() |
| 66 | ->arm_constants() |
| 67 | ->arm_positions() |
| 68 | ->intake(); |
| 69 | break; |
| 70 | case PivotGoal::AMP: |
| 71 | arm_status = ArmStatus::AMP; |
| 72 | arm_position = |
| 73 | robot_constants_->robot()->arm_constants()->arm_positions()->amp(); |
| 74 | break; |
| 75 | default: |
| 76 | arm_position = |
| 77 | robot_constants_->robot()->arm_constants()->arm_positions()->idle(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | arm_goal_buffer.Finish( |
| 82 | frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 83 | *arm_goal_buffer.fbb(), arm_position)); |
| 84 | |
| 85 | const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal |
| 86 | *arm_goal = &arm_goal_buffer.message(); |
| 87 | |
| 88 | // static_zeroing_single_dof_profiled_subsystem.h |
| 89 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
| 90 | arm_offset = |
| 91 | arm_.Iterate(arm_goal, position->arm(), |
| 92 | output != nullptr ? &output_struct.arm_voltage : nullptr, |
| 93 | status->fbb()); |
| 94 | |
James (Peilun) Li | a70e575 | 2024-09-18 20:43:00 -0700 | [diff] [blame] | 95 | if (output) { |
| 96 | output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct))); |
| 97 | } |
| 98 | |
| 99 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
| 100 | |
James (Peilun) Li | e9ee3e6 | 2024-11-03 21:24:21 -0800 | [diff] [blame] | 101 | const bool zeroed = arm_.zeroed(); |
| 102 | const bool estopped = arm_.estopped(); |
James (Peilun) Li | a70e575 | 2024-09-18 20:43:00 -0700 | [diff] [blame] | 103 | |
| 104 | status_builder.add_zeroed(zeroed); |
| 105 | status_builder.add_estopped(estopped); |
James (Peilun) Li | e9ee3e6 | 2024-11-03 21:24:21 -0800 | [diff] [blame] | 106 | status_builder.add_arm(arm_offset); |
| 107 | status_builder.add_arm_status(arm_status); |
James (Peilun) Li | a70e575 | 2024-09-18 20:43:00 -0700 | [diff] [blame] | 108 | |
| 109 | (void)status->Send(status_builder.Finish()); |
| 110 | } |
| 111 | } // namespace y2024_bot3::control_loops::superstructure |