blob: bb890213c343908dcb4ba020ecacd084b6f0d1b9 [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),
27 drivetrain_status_fetcher_(
28 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
29 "/drivetrain")),
30 joystick_state_fetcher_(
31 event_loop->MakeFetcher<aos::JoystickState>("/aos")) {
32 event_loop->SetRuntimeRealtimePriority(30);
33}
34
35void Superstructure::RunIteration(const Goal *unsafe_goal,
36 const Position *position,
37 aos::Sender<Output>::Builder *output,
38 aos::Sender<Status>::Builder *status) {
39 const monotonic_clock::time_point timestamp =
40 event_loop()->context().monotonic_event_time;
41
42 (void)timestamp;
43 (void)unsafe_goal;
44 (void)position;
45
46 if (WasReset()) {
47 AOS_LOG(ERROR, "WPILib reset, restarting\n");
48 }
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();
56
57 if (output) {
58 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
59 }
60
61 Status::Builder status_builder = status->MakeBuilder<Status>();
62 status_builder.add_zeroed(true);
63 status_builder.add_estopped(false);
64
65 (void)status->Send(status_builder.Finish());
66}
67
68double Superstructure::robot_velocity() const {
69 return (drivetrain_status_fetcher_.get() != nullptr
70 ? drivetrain_status_fetcher_->robot_speed()
71 : 0.0);
72}
73
Stephan Pleinesf63bde82024-01-13 15:59:33 -080074} // namespace y2024::control_loops::superstructure