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" |
Maxwell Henderson | b392b74 | 2023-03-05 07:53:51 -0800 | [diff] [blame^] | 7 | #include "y2023/control_loops/superstructure/arm/arm_trajectories_generated.h" |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 8 | |
| 9 | DEFINE_bool(ignore_distance, false, |
| 10 | "If true, ignore distance when shooting and obay joystick_reader"); |
| 11 | |
| 12 | namespace y2023 { |
| 13 | namespace control_loops { |
| 14 | namespace superstructure { |
| 15 | |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 16 | using ::aos::monotonic_clock; |
| 17 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 18 | using frc971::control_loops::AbsoluteEncoderProfiledJointStatus; |
| 19 | using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus; |
| 20 | using frc971::control_loops::RelativeEncoderProfiledJointStatus; |
| 21 | |
Maxwell Henderson | b392b74 | 2023-03-05 07:53:51 -0800 | [diff] [blame^] | 22 | Superstructure::Superstructure( |
| 23 | ::aos::EventLoop *event_loop, |
| 24 | std::shared_ptr<const constants::Values> values, |
| 25 | const aos::FlatbufferVector<ArmTrajectories> &arm_trajectories, |
| 26 | const ::std::string &name) |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 27 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
| 28 | name), |
| 29 | values_(values), |
| 30 | drivetrain_status_fetcher_( |
| 31 | event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>( |
| 32 | "/drivetrain")), |
| 33 | joystick_state_fetcher_( |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 34 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
Maxwell Henderson | b392b74 | 2023-03-05 07:53:51 -0800 | [diff] [blame^] | 35 | arm_(values_, arm_trajectories.message()), |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 36 | end_effector_(), |
Austin Schuh | 595ffc7 | 2023-02-24 16:24:37 -0800 | [diff] [blame] | 37 | wrist_(values->wrist.subsystem_params) { |
| 38 | event_loop->SetRuntimeRealtimePriority(30); |
| 39 | } |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 40 | |
| 41 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
| 42 | const Position *position, |
| 43 | aos::Sender<Output>::Builder *output, |
| 44 | aos::Sender<Status>::Builder *status) { |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 45 | const monotonic_clock::time_point timestamp = |
| 46 | event_loop()->context().monotonic_event_time; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 47 | |
| 48 | if (WasReset()) { |
| 49 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 50 | arm_.Reset(); |
Maxwell Henderson | 589cf27 | 2023-02-22 15:56:40 -0800 | [diff] [blame] | 51 | end_effector_.Reset(); |
Austin Schuh | bdabc70 | 2023-02-24 16:21:07 -0800 | [diff] [blame] | 52 | wrist_.Reset(); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | OutputT output_struct; |
| 56 | if (joystick_state_fetcher_.Fetch() && |
| 57 | joystick_state_fetcher_->has_alliance()) { |
| 58 | alliance_ = joystick_state_fetcher_->alliance(); |
| 59 | } |
| 60 | drivetrain_status_fetcher_.Fetch(); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 61 | |
| 62 | const uint32_t arm_goal_position = |
| 63 | unsafe_goal != nullptr ? unsafe_goal->arm_goal_position() : 0u; |
| 64 | |
| 65 | flatbuffers::Offset<superstructure::ArmStatus> arm_status_offset = |
| 66 | arm_.Iterate( |
| 67 | timestamp, unsafe_goal != nullptr ? &(arm_goal_position) : nullptr, |
| 68 | position->arm(), |
| 69 | unsafe_goal != nullptr ? unsafe_goal->trajectory_override() : false, |
| 70 | output != nullptr ? &output_struct.proximal_voltage : nullptr, |
| 71 | output != nullptr ? &output_struct.distal_voltage : nullptr, |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 72 | output != nullptr ? &output_struct.roll_joint_voltage : nullptr, |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 73 | status->fbb()); |
| 74 | |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 75 | flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> wrist_offset = |
Austin Schuh | bdabc70 | 2023-02-24 16:21:07 -0800 | [diff] [blame] | 76 | wrist_.Iterate( |
| 77 | unsafe_goal != nullptr ? unsafe_goal->wrist() : nullptr, |
| 78 | position->wrist(), |
| 79 | output != nullptr ? &(output_struct.wrist_voltage) : nullptr, |
| 80 | status->fbb()); |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 81 | |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 82 | end_effector_.RunIteration( |
Maxwell Henderson | 5938a83 | 2023-02-23 09:33:15 -0800 | [diff] [blame] | 83 | timestamp, |
| 84 | unsafe_goal != nullptr ? unsafe_goal->roller_goal() : RollerGoal::IDLE, |
milind-u | 6ff6d9e | 2023-02-24 20:15:06 -0800 | [diff] [blame] | 85 | position->has_roller_falcon() |
| 86 | ? position->roller_falcon()->torque_current() |
| 87 | : 0.0, |
| 88 | position->cone_position(), position->end_effector_cube_beam_break(), |
| 89 | &output_struct.roller_voltage); |
Maxwell Henderson | 589cf27 | 2023-02-22 15:56:40 -0800 | [diff] [blame] | 90 | |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 91 | if (output) { |
| 92 | output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct))); |
| 93 | } |
| 94 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 95 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
Austin Schuh | bdabc70 | 2023-02-24 16:21:07 -0800 | [diff] [blame] | 96 | status_builder.add_zeroed(wrist_.zeroed() && arm_.zeroed()); |
| 97 | status_builder.add_estopped(wrist_.estopped() || arm_.estopped()); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 98 | status_builder.add_arm(arm_status_offset); |
Maxwell Henderson | 8ca4456 | 2023-02-23 13:11:51 -0800 | [diff] [blame] | 99 | status_builder.add_wrist(wrist_offset); |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 100 | status_builder.add_end_effector_state(end_effector_.state()); |
| 101 | // TODO(milind): integrate this with ML game piece detection somehow |
| 102 | status_builder.add_game_piece(end_effector_.game_piece()); |
milind-u | 01bbcf2 | 2023-02-20 18:00:28 -0800 | [diff] [blame] | 103 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 104 | (void)status->Send(status_builder.Finish()); |
| 105 | } |
| 106 | |
| 107 | double Superstructure::robot_velocity() const { |
| 108 | return (drivetrain_status_fetcher_.get() != nullptr |
| 109 | ? drivetrain_status_fetcher_->robot_speed() |
| 110 | : 0.0); |
| 111 | } |
| 112 | |
| 113 | } // namespace superstructure |
| 114 | } // namespace control_loops |
| 115 | } // namespace y2023 |