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" |
| 6 | #include "frc971/zeroing/wrap.h" |
| 7 | |
| 8 | DEFINE_bool(ignore_distance, false, |
| 9 | "If true, ignore distance when shooting and obay joystick_reader"); |
| 10 | |
| 11 | namespace y2023 { |
| 12 | namespace control_loops { |
| 13 | namespace superstructure { |
| 14 | |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 15 | using ::aos::monotonic_clock; |
| 16 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 17 | using frc971::control_loops::AbsoluteEncoderProfiledJointStatus; |
| 18 | using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus; |
| 19 | using frc971::control_loops::RelativeEncoderProfiledJointStatus; |
| 20 | |
| 21 | Superstructure::Superstructure(::aos::EventLoop *event_loop, |
| 22 | std::shared_ptr<const constants::Values> values, |
| 23 | const ::std::string &name) |
| 24 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
| 25 | name), |
| 26 | values_(values), |
| 27 | drivetrain_status_fetcher_( |
| 28 | event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>( |
| 29 | "/drivetrain")), |
| 30 | joystick_state_fetcher_( |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 31 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
| 32 | arm_(values_) {} |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 33 | |
| 34 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
| 35 | const Position *position, |
| 36 | aos::Sender<Output>::Builder *output, |
| 37 | aos::Sender<Status>::Builder *status) { |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 38 | const monotonic_clock::time_point timestamp = |
| 39 | event_loop()->context().monotonic_event_time; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 40 | |
| 41 | if (WasReset()) { |
| 42 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 43 | arm_.Reset(); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | OutputT output_struct; |
| 47 | if (joystick_state_fetcher_.Fetch() && |
| 48 | joystick_state_fetcher_->has_alliance()) { |
| 49 | alliance_ = joystick_state_fetcher_->alliance(); |
| 50 | } |
| 51 | drivetrain_status_fetcher_.Fetch(); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 52 | |
| 53 | const uint32_t arm_goal_position = |
| 54 | unsafe_goal != nullptr ? unsafe_goal->arm_goal_position() : 0u; |
| 55 | |
| 56 | flatbuffers::Offset<superstructure::ArmStatus> arm_status_offset = |
| 57 | arm_.Iterate( |
| 58 | timestamp, unsafe_goal != nullptr ? &(arm_goal_position) : nullptr, |
| 59 | position->arm(), |
| 60 | unsafe_goal != nullptr ? unsafe_goal->trajectory_override() : false, |
| 61 | output != nullptr ? &output_struct.proximal_voltage : nullptr, |
| 62 | output != nullptr ? &output_struct.distal_voltage : nullptr, |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame^] | 63 | output != nullptr ? &output_struct.roll_joint_voltage : nullptr, |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 64 | unsafe_goal != nullptr ? unsafe_goal->intake() : false, |
| 65 | unsafe_goal != nullptr ? unsafe_goal->spit() : false, |
| 66 | |
| 67 | status->fbb()); |
| 68 | |
| 69 | if (output) { |
| 70 | output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct))); |
| 71 | } |
| 72 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 73 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
| 74 | status_builder.add_zeroed(true); |
| 75 | status_builder.add_estopped(false); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 76 | status_builder.add_arm(arm_status_offset); |
| 77 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 78 | (void)status->Send(status_builder.Finish()); |
| 79 | } |
| 80 | |
| 81 | double Superstructure::robot_velocity() const { |
| 82 | return (drivetrain_status_fetcher_.get() != nullptr |
| 83 | ? drivetrain_status_fetcher_->robot_speed() |
| 84 | : 0.0); |
| 85 | } |
| 86 | |
| 87 | } // namespace superstructure |
| 88 | } // namespace control_loops |
| 89 | } // namespace y2023 |