blob: cfa6d94142f8333b2d251f2897bafedf43192a02 [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),
Sabina Davis0f2d38c2020-02-08 17:01:21 -080016 hood_(constants::GetValues().hood),
17 intake_joint_(constants::GetValues().intake) {
Sabina Daviscf08b152020-01-31 22:12:09 -080018 event_loop->SetRuntimeRealtimePriority(30);
Stephan Massaltd021f972020-01-05 20:41:23 -080019}
20
Sabina Daviscf08b152020-01-31 22:12:09 -080021void Superstructure::RunIteration(const Goal *unsafe_goal,
22 const Position *position,
Stephan Massaltd021f972020-01-05 20:41:23 -080023 aos::Sender<Output>::Builder *output,
24 aos::Sender<Status>::Builder *status) {
25 if (WasReset()) {
26 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Sabina Daviscf08b152020-01-31 22:12:09 -080027 hood_.Reset();
Sabina Davis0f2d38c2020-02-08 17:01:21 -080028 intake_joint_.Reset();
Stephan Massaltd021f972020-01-05 20:41:23 -080029 }
30
Sabina Daviscf08b152020-01-31 22:12:09 -080031 OutputT output_struct;
32
33 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> hood_status_offset =
34 hood_.Iterate(unsafe_goal != nullptr ? unsafe_goal->hood() : nullptr,
35 position->hood(),
36 output != nullptr ? &(output_struct.hood_voltage) : nullptr,
37 status->fbb());
38
Sabina Davis0f2d38c2020-02-08 17:01:21 -080039 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> intake_status_offset =
40 intake_joint_.Iterate(
41 unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr,
42 position->intake_joint(),
43 output != nullptr ? &(output_struct.intake_joint_voltage) : nullptr,
44 status->fbb());
45
Sabina Daviscf08b152020-01-31 22:12:09 -080046 bool zeroed;
47 bool estopped;
48
Sabina Davis0f2d38c2020-02-08 17:01:21 -080049 {
50 AbsoluteEncoderProfiledJointStatus *hood_status =
51 GetMutableTemporaryPointer(*status->fbb(), hood_status_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -080052
Sabina Davis0f2d38c2020-02-08 17:01:21 -080053 AbsoluteEncoderProfiledJointStatus *intake_status =
54 GetMutableTemporaryPointer(*status->fbb(), intake_status_offset);
55
56 zeroed = hood_status->zeroed() && intake_status->zeroed();
57 estopped = hood_status->estopped() || intake_status->estopped();
Stephan Massaltd021f972020-01-05 20:41:23 -080058 }
59
60 Status::Builder status_builder = status->MakeBuilder<Status>();
61
Sabina Daviscf08b152020-01-31 22:12:09 -080062 status_builder.add_zeroed(zeroed);
63 status_builder.add_estopped(estopped);
64
65 status_builder.add_hood(hood_status_offset);
Sabina Davis0f2d38c2020-02-08 17:01:21 -080066 status_builder.add_intake(intake_status_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -080067
68 status->Send(status_builder.Finish());
Sabina Davis0f2d38c2020-02-08 17:01:21 -080069
70 if (output != nullptr) {
71 if (unsafe_goal) {
72 output_struct.intake_roller_voltage = unsafe_goal->roller_voltage();
73 } else {
74 output_struct.intake_roller_voltage = 0.0;
75 }
76 output->Send(Output::Pack(*output->fbb(), &output_struct));
77 }
Stephan Massaltd021f972020-01-05 20:41:23 -080078}
79
Sabina Daviscf08b152020-01-31 22:12:09 -080080} // namespace superstructure
Stephan Massaltd021f972020-01-05 20:41:23 -080081} // namespace control_loops
82} // namespace y2020