blob: 9d5d564c4547362f0b7b9fc785cdf853ce379e7e [file] [log] [blame]
Maxwell Hendersonad312342023-01-10 12:07:47 -08001#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
8DEFINE_bool(ignore_distance, false,
9 "If true, ignore distance when shooting and obay joystick_reader");
10
11namespace y2023 {
12namespace control_loops {
13namespace superstructure {
14
milind-u01bbcf22023-02-20 18:00:28 -080015using ::aos::monotonic_clock;
16
Maxwell Hendersonad312342023-01-10 12:07:47 -080017using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
18using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
19using frc971::control_loops::RelativeEncoderProfiledJointStatus;
20
21Superstructure::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-u01bbcf22023-02-20 18:00:28 -080031 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
Maxwell Henderson589cf272023-02-22 15:56:40 -080032 arm_(values_),
Maxwell Henderson8ca44562023-02-23 13:11:51 -080033 end_effector_(),
Austin Schuh595ffc72023-02-24 16:24:37 -080034 wrist_(values->wrist.subsystem_params) {
35 event_loop->SetRuntimeRealtimePriority(30);
36}
Maxwell Hendersonad312342023-01-10 12:07:47 -080037
38void Superstructure::RunIteration(const Goal *unsafe_goal,
39 const Position *position,
40 aos::Sender<Output>::Builder *output,
41 aos::Sender<Status>::Builder *status) {
milind-u01bbcf22023-02-20 18:00:28 -080042 const monotonic_clock::time_point timestamp =
43 event_loop()->context().monotonic_event_time;
Maxwell Hendersonad312342023-01-10 12:07:47 -080044
45 if (WasReset()) {
46 AOS_LOG(ERROR, "WPILib reset, restarting\n");
milind-u01bbcf22023-02-20 18:00:28 -080047 arm_.Reset();
Maxwell Henderson589cf272023-02-22 15:56:40 -080048 end_effector_.Reset();
Austin Schuhbdabc702023-02-24 16:21:07 -080049 wrist_.Reset();
Maxwell Hendersonad312342023-01-10 12:07:47 -080050 }
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-u01bbcf22023-02-20 18:00:28 -080058
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-u18a901d2023-02-17 21:51:55 -080069 output != nullptr ? &output_struct.roll_joint_voltage : nullptr,
milind-u01bbcf22023-02-20 18:00:28 -080070 status->fbb());
71
Maxwell Henderson8ca44562023-02-23 13:11:51 -080072 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> wrist_offset =
Austin Schuhbdabc702023-02-24 16:21:07 -080073 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 Henderson8ca44562023-02-23 13:11:51 -080078
Maxwell Henderson589cf272023-02-22 15:56:40 -080079 EndEffectorState end_effector_state = end_effector_.RunIteration(
Maxwell Henderson5938a832023-02-23 09:33:15 -080080 timestamp,
81 unsafe_goal != nullptr ? unsafe_goal->roller_goal() : RollerGoal::IDLE,
Maxwell Henderson589cf272023-02-22 15:56:40 -080082 position->end_effector_cone_beam_break(),
83 position->end_effector_cube_beam_break(), &output_struct.roller_voltage);
84
milind-u01bbcf22023-02-20 18:00:28 -080085 if (output) {
86 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
87 }
88
Maxwell Hendersonad312342023-01-10 12:07:47 -080089 Status::Builder status_builder = status->MakeBuilder<Status>();
Austin Schuhbdabc702023-02-24 16:21:07 -080090 status_builder.add_zeroed(wrist_.zeroed() && arm_.zeroed());
91 status_builder.add_estopped(wrist_.estopped() || arm_.estopped());
milind-u01bbcf22023-02-20 18:00:28 -080092 status_builder.add_arm(arm_status_offset);
Maxwell Henderson8ca44562023-02-23 13:11:51 -080093 status_builder.add_wrist(wrist_offset);
Maxwell Henderson589cf272023-02-22 15:56:40 -080094 status_builder.add_end_effector_state(end_effector_state);
milind-u01bbcf22023-02-20 18:00:28 -080095
Maxwell Hendersonad312342023-01-10 12:07:47 -080096 (void)status->Send(status_builder.Finish());
97}
98
99double 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