blob: 91a75861f14ff91657c428a5e4d3ddc866c80264 [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include "y2020/control_loops/superstructure/superstructure.h"
2
3#include "aos/events/event_loop.h"
4
5namespace y2020 {
6namespace control_loops {
7namespace superstructure {
8
9using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
10using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
11
12Superstructure::Superstructure(::aos::EventLoop *event_loop,
13 const ::std::string &name)
14 : aos::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
Sabina Daviscf08b152020-01-31 22:12:09 -080015 name),
16 hood_(constants::GetValues().hood) {
17 event_loop->SetRuntimeRealtimePriority(30);
Stephan Massaltd021f972020-01-05 20:41:23 -080018}
19
Sabina Daviscf08b152020-01-31 22:12:09 -080020void Superstructure::RunIteration(const Goal *unsafe_goal,
21 const Position *position,
Stephan Massaltd021f972020-01-05 20:41:23 -080022 aos::Sender<Output>::Builder *output,
23 aos::Sender<Status>::Builder *status) {
24 if (WasReset()) {
25 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Sabina Daviscf08b152020-01-31 22:12:09 -080026 hood_.Reset();
Stephan Massaltd021f972020-01-05 20:41:23 -080027 }
28
Sabina Daviscf08b152020-01-31 22:12:09 -080029 OutputT output_struct;
30
31 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> hood_status_offset =
32 hood_.Iterate(unsafe_goal != nullptr ? unsafe_goal->hood() : nullptr,
33 position->hood(),
34 output != nullptr ? &(output_struct.hood_voltage) : nullptr,
35 status->fbb());
36
37 bool zeroed;
38 bool estopped;
39
40 const AbsoluteEncoderProfiledJointStatus *hood_status =
41 GetMutableTemporaryPointer(*status->fbb(), hood_status_offset);
42 zeroed = hood_status->zeroed();
43 estopped = hood_status->estopped();
Stephan Massaltd021f972020-01-05 20:41:23 -080044
45 if (output != nullptr) {
Stephan Massaltd021f972020-01-05 20:41:23 -080046 output->Send(Output::Pack(*output->fbb(), &output_struct));
47 }
48
49 Status::Builder status_builder = status->MakeBuilder<Status>();
50
Sabina Daviscf08b152020-01-31 22:12:09 -080051 status_builder.add_zeroed(zeroed);
52 status_builder.add_estopped(estopped);
53
54 status_builder.add_hood(hood_status_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -080055
56 status->Send(status_builder.Finish());
57}
58
Sabina Daviscf08b152020-01-31 22:12:09 -080059} // namespace superstructure
Stephan Massaltd021f972020-01-05 20:41:23 -080060} // namespace control_loops
61} // namespace y2020