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")), |
Maxwell Henderson | 589cf27 | 2023-02-22 15:56:40 -0800 | [diff] [blame] | 32 | arm_(values_), |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 33 | end_effector_(), |
Austin Schuh | 595ffc7 | 2023-02-24 16:24:37 -0800 | [diff] [blame^] | 34 | wrist_(values->wrist.subsystem_params) { |
| 35 | event_loop->SetRuntimeRealtimePriority(30); |
| 36 | } |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 37 | |
| 38 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
| 39 | const Position *position, |
| 40 | aos::Sender<Output>::Builder *output, |
| 41 | aos::Sender<Status>::Builder *status) { |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 42 | const monotonic_clock::time_point timestamp = |
| 43 | event_loop()->context().monotonic_event_time; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 44 | |
| 45 | if (WasReset()) { |
| 46 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 47 | arm_.Reset(); |
Maxwell Henderson | 589cf27 | 2023-02-22 15:56:40 -0800 | [diff] [blame] | 48 | end_effector_.Reset(); |
Austin Schuh | bdabc70 | 2023-02-24 16:21:07 -0800 | [diff] [blame] | 49 | wrist_.Reset(); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 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(); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 58 | |
| 59 | const uint32_t arm_goal_position = |
| 60 | unsafe_goal != nullptr ? unsafe_goal->arm_goal_position() : 0u; |
| 61 | |
| 62 | flatbuffers::Offset<superstructure::ArmStatus> arm_status_offset = |
| 63 | arm_.Iterate( |
| 64 | timestamp, unsafe_goal != nullptr ? &(arm_goal_position) : nullptr, |
| 65 | position->arm(), |
| 66 | unsafe_goal != nullptr ? unsafe_goal->trajectory_override() : false, |
| 67 | output != nullptr ? &output_struct.proximal_voltage : nullptr, |
| 68 | output != nullptr ? &output_struct.distal_voltage : nullptr, |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 69 | output != nullptr ? &output_struct.roll_joint_voltage : nullptr, |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 70 | status->fbb()); |
| 71 | |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 72 | flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> wrist_offset = |
Austin Schuh | bdabc70 | 2023-02-24 16:21:07 -0800 | [diff] [blame] | 73 | wrist_.Iterate( |
| 74 | unsafe_goal != nullptr ? unsafe_goal->wrist() : nullptr, |
| 75 | position->wrist(), |
| 76 | output != nullptr ? &(output_struct.wrist_voltage) : nullptr, |
| 77 | status->fbb()); |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 78 | |
Maxwell Henderson | 589cf27 | 2023-02-22 15:56:40 -0800 | [diff] [blame] | 79 | EndEffectorState end_effector_state = end_effector_.RunIteration( |
Maxwell Henderson | 5938a83 | 2023-02-23 09:33:15 -0800 | [diff] [blame] | 80 | timestamp, |
| 81 | unsafe_goal != nullptr ? unsafe_goal->roller_goal() : RollerGoal::IDLE, |
Maxwell Henderson | 589cf27 | 2023-02-22 15:56:40 -0800 | [diff] [blame] | 82 | position->end_effector_cone_beam_break(), |
| 83 | position->end_effector_cube_beam_break(), &output_struct.roller_voltage); |
| 84 | |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 85 | if (output) { |
| 86 | output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct))); |
| 87 | } |
| 88 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 89 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
Austin Schuh | bdabc70 | 2023-02-24 16:21:07 -0800 | [diff] [blame] | 90 | status_builder.add_zeroed(wrist_.zeroed() && arm_.zeroed()); |
| 91 | status_builder.add_estopped(wrist_.estopped() || arm_.estopped()); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 92 | status_builder.add_arm(arm_status_offset); |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 93 | status_builder.add_wrist(wrist_offset); |
Maxwell Henderson | 589cf27 | 2023-02-22 15:56:40 -0800 | [diff] [blame] | 94 | status_builder.add_end_effector_state(end_effector_state); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 95 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 96 | (void)status->Send(status_builder.Finish()); |
| 97 | } |
| 98 | |
| 99 | double Superstructure::robot_velocity() const { |
| 100 | return (drivetrain_status_fetcher_.get() != nullptr |
| 101 | ? drivetrain_status_fetcher_->robot_speed() |
| 102 | : 0.0); |
| 103 | } |
| 104 | |
| 105 | } // namespace superstructure |
| 106 | } // namespace control_loops |
| 107 | } // namespace y2023 |