blob: 3b4d2b0a483b62749ade2bfd15e8d2386efbf44c [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();
Maxwell Hendersonad312342023-01-10 12:07:47 -080047 }
48
49 OutputT output_struct;
50 if (joystick_state_fetcher_.Fetch() &&
51 joystick_state_fetcher_->has_alliance()) {
52 alliance_ = joystick_state_fetcher_->alliance();
53 }
54 drivetrain_status_fetcher_.Fetch();
milind-u01bbcf22023-02-20 18:00:28 -080055
56 const uint32_t arm_goal_position =
57 unsafe_goal != nullptr ? unsafe_goal->arm_goal_position() : 0u;
58
59 flatbuffers::Offset<superstructure::ArmStatus> arm_status_offset =
60 arm_.Iterate(
61 timestamp, unsafe_goal != nullptr ? &(arm_goal_position) : nullptr,
62 position->arm(),
63 unsafe_goal != nullptr ? unsafe_goal->trajectory_override() : false,
64 output != nullptr ? &output_struct.proximal_voltage : nullptr,
65 output != nullptr ? &output_struct.distal_voltage : nullptr,
milind-u18a901d2023-02-17 21:51:55 -080066 output != nullptr ? &output_struct.roll_joint_voltage : nullptr,
milind-u01bbcf22023-02-20 18:00:28 -080067 status->fbb());
68
Maxwell Henderson8ca44562023-02-23 13:11:51 -080069 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> wrist_offset =
70 wrist_.Iterate(unsafe_goal != nullptr ? unsafe_goal->wrist() : nullptr,
71 position->wrist(),
72 output != nullptr ? &output_struct.wrist_voltage : nullptr,
73 status->fbb());
74
Maxwell Henderson589cf272023-02-22 15:56:40 -080075 EndEffectorState end_effector_state = end_effector_.RunIteration(
Maxwell Henderson5938a832023-02-23 09:33:15 -080076 timestamp,
77 unsafe_goal != nullptr ? unsafe_goal->roller_goal() : RollerGoal::IDLE,
Maxwell Henderson589cf272023-02-22 15:56:40 -080078 position->end_effector_cone_beam_break(),
79 position->end_effector_cube_beam_break(), &output_struct.roller_voltage);
80
milind-u01bbcf22023-02-20 18:00:28 -080081 if (output) {
82 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
83 }
84
Maxwell Hendersonad312342023-01-10 12:07:47 -080085 Status::Builder status_builder = status->MakeBuilder<Status>();
86 status_builder.add_zeroed(true);
87 status_builder.add_estopped(false);
milind-u01bbcf22023-02-20 18:00:28 -080088 status_builder.add_arm(arm_status_offset);
Maxwell Henderson8ca44562023-02-23 13:11:51 -080089 status_builder.add_wrist(wrist_offset);
Maxwell Henderson589cf272023-02-22 15:56:40 -080090 status_builder.add_end_effector_state(end_effector_state);
milind-u01bbcf22023-02-20 18:00:28 -080091
Maxwell Hendersonad312342023-01-10 12:07:47 -080092 (void)status->Send(status_builder.Finish());
93}
94
95double Superstructure::robot_velocity() const {
96 return (drivetrain_status_fetcher_.get() != nullptr
97 ? drivetrain_status_fetcher_->robot_speed()
98 : 0.0);
99}
100
101} // namespace superstructure
102} // namespace control_loops
103} // namespace y2023