blob: 03c6269d534f39c89a710d37b81ec1cfe70f5c3a [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_(),
34 wrist_(values->wrist.subsystem_params) {}
Maxwell Hendersonad312342023-01-10 12:07:47 -080035
36void Superstructure::RunIteration(const Goal *unsafe_goal,
37 const Position *position,
38 aos::Sender<Output>::Builder *output,
39 aos::Sender<Status>::Builder *status) {
milind-u01bbcf22023-02-20 18:00:28 -080040 const monotonic_clock::time_point timestamp =
41 event_loop()->context().monotonic_event_time;
Maxwell Hendersonad312342023-01-10 12:07:47 -080042
43 if (WasReset()) {
44 AOS_LOG(ERROR, "WPILib reset, restarting\n");
milind-u01bbcf22023-02-20 18:00:28 -080045 arm_.Reset();
Maxwell Henderson589cf272023-02-22 15:56:40 -080046 end_effector_.Reset();
Austin Schuhbdabc702023-02-24 16:21:07 -080047 wrist_.Reset();
Maxwell Hendersonad312342023-01-10 12:07:47 -080048 }
49
50 OutputT output_struct;
51 if (joystick_state_fetcher_.Fetch() &&
52 joystick_state_fetcher_->has_alliance()) {
53 alliance_ = joystick_state_fetcher_->alliance();
54 }
55 drivetrain_status_fetcher_.Fetch();
milind-u01bbcf22023-02-20 18:00:28 -080056
57 const uint32_t arm_goal_position =
58 unsafe_goal != nullptr ? unsafe_goal->arm_goal_position() : 0u;
59
60 flatbuffers::Offset<superstructure::ArmStatus> arm_status_offset =
61 arm_.Iterate(
62 timestamp, unsafe_goal != nullptr ? &(arm_goal_position) : nullptr,
63 position->arm(),
64 unsafe_goal != nullptr ? unsafe_goal->trajectory_override() : false,
65 output != nullptr ? &output_struct.proximal_voltage : nullptr,
66 output != nullptr ? &output_struct.distal_voltage : nullptr,
milind-u18a901d2023-02-17 21:51:55 -080067 output != nullptr ? &output_struct.roll_joint_voltage : nullptr,
milind-u01bbcf22023-02-20 18:00:28 -080068 status->fbb());
69
Maxwell Henderson8ca44562023-02-23 13:11:51 -080070 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> wrist_offset =
Austin Schuhbdabc702023-02-24 16:21:07 -080071 wrist_.Iterate(
72 unsafe_goal != nullptr ? unsafe_goal->wrist() : nullptr,
73 position->wrist(),
74 output != nullptr ? &(output_struct.wrist_voltage) : nullptr,
75 status->fbb());
Maxwell Henderson8ca44562023-02-23 13:11:51 -080076
Maxwell Henderson589cf272023-02-22 15:56:40 -080077 EndEffectorState end_effector_state = end_effector_.RunIteration(
Maxwell Henderson5938a832023-02-23 09:33:15 -080078 timestamp,
79 unsafe_goal != nullptr ? unsafe_goal->roller_goal() : RollerGoal::IDLE,
Maxwell Henderson589cf272023-02-22 15:56:40 -080080 position->end_effector_cone_beam_break(),
81 position->end_effector_cube_beam_break(), &output_struct.roller_voltage);
82
milind-u01bbcf22023-02-20 18:00:28 -080083 if (output) {
84 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
85 }
86
Maxwell Hendersonad312342023-01-10 12:07:47 -080087 Status::Builder status_builder = status->MakeBuilder<Status>();
Austin Schuhbdabc702023-02-24 16:21:07 -080088 status_builder.add_zeroed(wrist_.zeroed() && arm_.zeroed());
89 status_builder.add_estopped(wrist_.estopped() || arm_.estopped());
milind-u01bbcf22023-02-20 18:00:28 -080090 status_builder.add_arm(arm_status_offset);
Maxwell Henderson8ca44562023-02-23 13:11:51 -080091 status_builder.add_wrist(wrist_offset);
Maxwell Henderson589cf272023-02-22 15:56:40 -080092 status_builder.add_end_effector_state(end_effector_state);
milind-u01bbcf22023-02-20 18:00:28 -080093
Maxwell Hendersonad312342023-01-10 12:07:47 -080094 (void)status->Send(status_builder.Finish());
95}
96
97double Superstructure::robot_velocity() const {
98 return (drivetrain_status_fetcher_.get() != nullptr
99 ? drivetrain_status_fetcher_->robot_speed()
100 : 0.0);
101}
102
103} // namespace superstructure
104} // namespace control_loops
105} // namespace y2023