blob: 2c510adea18d280c5967054c8c4989a6a5b3f02d [file] [log] [blame]
James (Peilun) Lia70e5752024-09-18 20:43:00 -07001#include "y2024_bot3/control_loops/superstructure/superstructure.h"
2
3#include <chrono>
4
5#include "aos/events/event_loop.h"
6#include "aos/flatbuffer_merge.h"
7#include "aos/network/team_number.h"
8#include "aos/time/time.h"
9#include "frc971/zeroing/wrap.h"
10
11ABSL_FLAG(bool, ignore_distance, false,
12 "If true, ignore distance when shooting and obey joystick_reader");
13
14namespace y2024_bot3::control_loops::superstructure {
15
16using ::aos::monotonic_clock;
17
18using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
19using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
20using frc971::control_loops::RelativeEncoderProfiledJointStatus;
21
22Superstructure::Superstructure(::aos::EventLoop *event_loop,
23 const ::std::string &name)
24 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
25 name),
26 constants_fetcher_(event_loop),
27 robot_constants_(&constants_fetcher_.constants()),
28 joystick_state_fetcher_(
James (Peilun) Lie9ee3e62024-11-03 21:24:21 -080029 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
30 arm_(robot_constants_->common()->arm(),
31 robot_constants_->robot()->arm_constants()->zeroing_constants()) {
James (Peilun) Lia70e5752024-09-18 20:43:00 -070032 event_loop->SetRuntimeRealtimePriority(30);
33}
34
35bool PositionNear(double position, double goal, double threshold) {
36 return std::abs(position - goal) < threshold;
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) {
James (Peilun) Lia70e5752024-09-18 20:43:00 -070043 if (WasReset()) {
44 AOS_LOG(ERROR, "WPILib reset, restarting\n");
45 }
46
47 OutputT output_struct;
48
James (Peilun) Lia70e5752024-09-18 20:43:00 -070049 if (joystick_state_fetcher_.Fetch() &&
50 joystick_state_fetcher_->has_alliance()) {
51 alliance_ = joystick_state_fetcher_->alliance();
52 }
53
James (Peilun) Lie9ee3e62024-11-03 21:24:21 -080054 aos::FlatbufferFixedAllocatorArray<
55 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
56 arm_goal_buffer;
57
58 ArmStatus arm_status = ArmStatus::IDLE;
59 double arm_position =
60 robot_constants_->robot()->arm_constants()->arm_positions()->idle();
61 if (unsafe_goal != nullptr) {
62 switch (unsafe_goal->arm_position()) {
63 case PivotGoal::INTAKE:
64 arm_status = ArmStatus::INTAKE;
65 arm_position = robot_constants_->robot()
66 ->arm_constants()
67 ->arm_positions()
68 ->intake();
69 break;
70 case PivotGoal::AMP:
71 arm_status = ArmStatus::AMP;
72 arm_position =
73 robot_constants_->robot()->arm_constants()->arm_positions()->amp();
74 break;
75 default:
76 arm_position =
77 robot_constants_->robot()->arm_constants()->arm_positions()->idle();
78 }
79 }
80
81 arm_goal_buffer.Finish(
82 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
83 *arm_goal_buffer.fbb(), arm_position));
84
85 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
86 *arm_goal = &arm_goal_buffer.message();
87
88 // static_zeroing_single_dof_profiled_subsystem.h
89 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
90 arm_offset =
91 arm_.Iterate(arm_goal, position->arm(),
92 output != nullptr ? &output_struct.arm_voltage : nullptr,
93 status->fbb());
94
James (Peilun) Lia70e5752024-09-18 20:43:00 -070095 if (output) {
96 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
97 }
98
99 Status::Builder status_builder = status->MakeBuilder<Status>();
100
James (Peilun) Lie9ee3e62024-11-03 21:24:21 -0800101 const bool zeroed = arm_.zeroed();
102 const bool estopped = arm_.estopped();
James (Peilun) Lia70e5752024-09-18 20:43:00 -0700103
104 status_builder.add_zeroed(zeroed);
105 status_builder.add_estopped(estopped);
James (Peilun) Lie9ee3e62024-11-03 21:24:21 -0800106 status_builder.add_arm(arm_offset);
107 status_builder.add_arm_status(arm_status);
James (Peilun) Lia70e5752024-09-18 20:43:00 -0700108
109 (void)status->Send(status_builder.Finish());
110}
111} // namespace y2024_bot3::control_loops::superstructure