blob: 366a8dbeba52367535def7966a2463915f2bd531 [file] [log] [blame]
Niko Sohmers3860f8a2024-01-12 21:05:19 -08001#include "y2024/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/shooter_interpolation/interpolation.h"
7#include "frc971/zeroing/wrap.h"
8
9DEFINE_bool(ignore_distance, false,
10 "If true, ignore distance when shooting and obay joystick_reader");
11
Stephan Pleinesf63bde82024-01-13 15:59:33 -080012namespace y2024::control_loops::superstructure {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080013
14using ::aos::monotonic_clock;
15
16using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
17using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
18using frc971::control_loops::RelativeEncoderProfiledJointStatus;
19
20Superstructure::Superstructure(::aos::EventLoop *event_loop,
21 std::shared_ptr<const constants::Values> values,
22 const ::std::string &name)
23 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
24 name),
25 values_(values),
26 constants_fetcher_(event_loop),
Niko Sohmersafc51fe2024-01-29 17:48:35 -080027 robot_constants_(CHECK_NOTNULL(&constants_fetcher_.constants())),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080028 drivetrain_status_fetcher_(
29 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
30 "/drivetrain")),
31 joystick_state_fetcher_(
Niko Sohmersafc51fe2024-01-29 17:48:35 -080032 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
33 intake_pivot_(
34 robot_constants_->common()->intake_pivot(),
35 robot_constants_->robot()->intake_constants()->intake_pivot_zero()) {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080036 event_loop->SetRuntimeRealtimePriority(30);
37}
38
39void Superstructure::RunIteration(const Goal *unsafe_goal,
40 const Position *position,
41 aos::Sender<Output>::Builder *output,
42 aos::Sender<Status>::Builder *status) {
43 const monotonic_clock::time_point timestamp =
44 event_loop()->context().monotonic_event_time;
45
46 (void)timestamp;
Niko Sohmers3860f8a2024-01-12 21:05:19 -080047 (void)position;
48
49 if (WasReset()) {
50 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Niko Sohmersafc51fe2024-01-29 17:48:35 -080051 intake_pivot_.Reset();
Niko Sohmers3860f8a2024-01-12 21:05:19 -080052 }
53
54 OutputT output_struct;
Niko Sohmersafc51fe2024-01-29 17:48:35 -080055
56 double intake_pivot_position =
57 robot_constants_->common()->intake_pivot_set_points()->retracted();
58
59 if (unsafe_goal != nullptr &&
60 unsafe_goal->intake_pivot_goal() == IntakePivotGoal::EXTENDED) {
61 intake_pivot_position =
62 robot_constants_->common()->intake_pivot_set_points()->extended();
63 }
64
65 IntakeRollerState intake_roller_state = IntakeRollerState::NONE;
66
67 switch (unsafe_goal != nullptr ? unsafe_goal->intake_roller_goal()
68 : IntakeRollerGoal::NONE) {
69 case IntakeRollerGoal::NONE:
70 output_struct.intake_roller_voltage = 0.0;
71 break;
72 case IntakeRollerGoal::SPIT:
73 intake_roller_state = IntakeRollerState::SPITTING;
74 output_struct.intake_roller_voltage =
75 robot_constants_->common()->intake_roller_voltages()->spitting();
76 break;
77 case IntakeRollerGoal::INTAKE:
78 intake_roller_state = IntakeRollerState::INTAKING;
79 output_struct.intake_roller_voltage =
80 robot_constants_->common()->intake_roller_voltages()->intaking();
81 break;
82 }
83
Niko Sohmers3860f8a2024-01-12 21:05:19 -080084 if (joystick_state_fetcher_.Fetch() &&
85 joystick_state_fetcher_->has_alliance()) {
86 alliance_ = joystick_state_fetcher_->alliance();
87 }
Niko Sohmersafc51fe2024-01-29 17:48:35 -080088
Niko Sohmers3860f8a2024-01-12 21:05:19 -080089 drivetrain_status_fetcher_.Fetch();
90
Niko Sohmersafc51fe2024-01-29 17:48:35 -080091 aos::FlatbufferFixedAllocatorArray<
92 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
93 intake_pivot_goal_buffer;
94
95 intake_pivot_goal_buffer.Finish(
96 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
97 *intake_pivot_goal_buffer.fbb(), intake_pivot_position));
98
99 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
100 *intake_pivot_goal = &intake_pivot_goal_buffer.message();
101
102 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
103 intake_pivot_status_offset = intake_pivot_.Iterate(
104 intake_pivot_goal, position->intake_pivot(),
105 output != nullptr ? &output_struct.intake_pivot_voltage : nullptr,
106 status->fbb());
107
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800108 if (output) {
109 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
110 }
111
112 Status::Builder status_builder = status->MakeBuilder<Status>();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800113
114 const bool zeroed = intake_pivot_.zeroed();
115 const bool estopped = intake_pivot_.estopped();
116
117 status_builder.add_zeroed(zeroed);
118 status_builder.add_estopped(estopped);
119 status_builder.add_intake_roller_state(intake_roller_state);
120 status_builder.add_intake_pivot_state(intake_pivot_status_offset);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800121
122 (void)status->Send(status_builder.Finish());
123}
124
125double Superstructure::robot_velocity() const {
126 return (drivetrain_status_fetcher_.get() != nullptr
127 ? drivetrain_status_fetcher_->robot_speed()
128 : 0.0);
129}
130
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800131} // namespace y2024::control_loops::superstructure