blob: a7af922f316daee711c2370f88fc898fde5a76e4 [file] [log] [blame]
milind-u086d7262022-01-19 20:44:18 -08001#include "y2022/control_loops/superstructure/superstructure.h"
2
3#include "aos/events/event_loop.h"
4
5namespace y2022 {
6namespace control_loops {
7namespace superstructure {
8
9using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
10using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080011using frc971::control_loops:: RelativeEncoderProfiledJointStatus;
milind-u086d7262022-01-19 20:44:18 -080012
13Superstructure::Superstructure(::aos::EventLoop *event_loop,
14 const ::std::string &name)
15 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080016 name),
17 climber_(constants::GetValues().climber.subsystem_params) {
milind-u086d7262022-01-19 20:44:18 -080018 event_loop->SetRuntimeRealtimePriority(30);
19}
20
21void Superstructure::RunIteration(const Goal *unsafe_goal,
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080022 const Position *position,
milind-u086d7262022-01-19 20:44:18 -080023 aos::Sender<Output>::Builder *output,
24 aos::Sender<Status>::Builder *status) {
25 if (WasReset()) {
26 AOS_LOG(ERROR, "WPILib reset, restarting\n");
27 }
28
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080029 OutputT output_struct;
30
31 flatbuffers::Offset<RelativeEncoderProfiledJointStatus>
32 climber_status_offset = climber_.Iterate(
33 unsafe_goal != nullptr ? unsafe_goal->climber() : nullptr,
34 position->climber(),
35 output != nullptr ? &(output_struct.climber_voltage) : nullptr,
36 status->fbb());
37
38 if (output != nullptr) {
milind-u086d7262022-01-19 20:44:18 -080039 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
40 }
41
42 Status::Builder status_builder = status->MakeBuilder<Status>();
43
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080044 // Climber is always zeroed; only has a pot
45 status_builder.add_zeroed(climber_.zeroed());
46 status_builder.add_estopped(climber_.estopped());
47 status_builder.add_climber(climber_status_offset);
milind-u086d7262022-01-19 20:44:18 -080048
milind-u086d7262022-01-19 20:44:18 -080049 (void)status->Send(status_builder.Finish());
50}
51
52} // namespace superstructure
53} // namespace control_loops
54} // namespace y2022