blob: 3b4835c6056fa38a05cd5a706abf81792c4ba7db [file] [log] [blame]
Ariv Diggi0af59c02023-10-07 13:15:39 -07001#include "y2023_bot3/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
12namespace y2023_bot3 {
13namespace control_loops {
14namespace superstructure {
15
16using ::aos::monotonic_clock;
17
Ariv Diggi0af59c02023-10-07 13:15:39 -070018using 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),
Maxwell Henderson0d220772023-11-06 11:09:58 -080026 values_(values),
27 end_effector_(),
28 pivot_joint_(values) {
Ariv Diggi0af59c02023-10-07 13:15:39 -070029 event_loop->SetRuntimeRealtimePriority(30);
30}
31
32void Superstructure::RunIteration(const Goal *unsafe_goal,
33 const Position *position,
34 aos::Sender<Output>::Builder *output,
35 aos::Sender<Status>::Builder *status) {
Ariv Diggi0af59c02023-10-07 13:15:39 -070036 const monotonic_clock::time_point timestamp =
37 event_loop()->context().monotonic_event_time;
Ariv Diggi0af59c02023-10-07 13:15:39 -070038
39 if (WasReset()) {
40 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Maxwell Henderson3772d282023-11-06 11:07:49 -080041 end_effector_.Reset();
Ariv Diggi0af59c02023-10-07 13:15:39 -070042 }
43
44 OutputT output_struct;
45
Maxwell Henderson3772d282023-11-06 11:07:49 -080046 end_effector_.RunIteration(
47 timestamp,
48 unsafe_goal != nullptr ? unsafe_goal->roller_goal() : RollerGoal::IDLE,
49 position->end_effector_cube_beam_break(), &output_struct.roller_voltage,
50 unsafe_goal != nullptr ? unsafe_goal->preloaded_with_cube() : false);
51
Maxwell Henderson0d220772023-11-06 11:09:58 -080052 flatbuffers::Offset<
53 frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus>
54 pivot_joint_offset = pivot_joint_.RunIteration(
55 unsafe_goal != nullptr ? unsafe_goal->pivot_goal()
56 : PivotGoal::NEUTRAL,
57 &(output_struct.pivot_joint_voltage),
58 position->pivot_joint_position(), status->fbb());
Ariv Diggi0af59c02023-10-07 13:15:39 -070059
60 Status::Builder status_builder = status->MakeBuilder<Status>();
Maxwell Henderson43684fa2023-11-06 11:08:06 -080061
Maxwell Henderson0d220772023-11-06 11:09:58 -080062 status_builder.add_zeroed(pivot_joint_.zeroed());
63 status_builder.add_estopped(pivot_joint_.estopped());
64 status_builder.add_pivot_joint(pivot_joint_offset);
Maxwell Henderson3772d282023-11-06 11:07:49 -080065 status_builder.add_end_effector_state(end_effector_.state());
Ariv Diggi0af59c02023-10-07 13:15:39 -070066
Maxwell Henderson0d220772023-11-06 11:09:58 -080067 if (output) {
68 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
69 }
70
Ariv Diggi0af59c02023-10-07 13:15:39 -070071 (void)status->Send(status_builder.Finish());
72}
73
74} // namespace superstructure
75} // namespace control_loops
76} // namespace y2023_bot3