blob: eb88e93f9b0e77cc9eeecc259908c7854e2783f0 [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),
Kai Tinkessfb460372020-02-08 14:05:48 -080017 intake_joint_(constants::GetValues().intake),
18 turret_(constants::GetValues().turret.subsystem_params) {
Sabina Daviscf08b152020-01-31 22:12:09 -080019 event_loop->SetRuntimeRealtimePriority(30);
Stephan Massaltd021f972020-01-05 20:41:23 -080020}
21
Sabina Daviscf08b152020-01-31 22:12:09 -080022void Superstructure::RunIteration(const Goal *unsafe_goal,
23 const Position *position,
Stephan Massaltd021f972020-01-05 20:41:23 -080024 aos::Sender<Output>::Builder *output,
25 aos::Sender<Status>::Builder *status) {
26 if (WasReset()) {
27 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Sabina Daviscf08b152020-01-31 22:12:09 -080028 hood_.Reset();
Sabina Davis0f2d38c2020-02-08 17:01:21 -080029 intake_joint_.Reset();
Kai Tinkessfb460372020-02-08 14:05:48 -080030 turret_.Reset();
Stephan Massaltd021f972020-01-05 20:41:23 -080031 }
32
Sabina Daviscf08b152020-01-31 22:12:09 -080033 OutputT output_struct;
34
35 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> hood_status_offset =
36 hood_.Iterate(unsafe_goal != nullptr ? unsafe_goal->hood() : nullptr,
37 position->hood(),
38 output != nullptr ? &(output_struct.hood_voltage) : nullptr,
39 status->fbb());
40
Sabina Davis0f2d38c2020-02-08 17:01:21 -080041 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> intake_status_offset =
42 intake_joint_.Iterate(
43 unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr,
44 position->intake_joint(),
45 output != nullptr ? &(output_struct.intake_joint_voltage) : nullptr,
46 status->fbb());
47
Kai Tinkessfb460372020-02-08 14:05:48 -080048 flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
49 turret_status_offset = turret_.Iterate(
50 unsafe_goal != nullptr ? unsafe_goal->turret() : nullptr,
51 position->turret(),
52 output != nullptr ? &(output_struct.turret_voltage) : nullptr,
53 status->fbb());
54
Sabina Daviscf08b152020-01-31 22:12:09 -080055 bool zeroed;
56 bool estopped;
57
Sabina Davis0f2d38c2020-02-08 17:01:21 -080058 {
Kai Tinkessfb460372020-02-08 14:05:48 -080059 const AbsoluteEncoderProfiledJointStatus *const hood_status =
Sabina Davis0f2d38c2020-02-08 17:01:21 -080060 GetMutableTemporaryPointer(*status->fbb(), hood_status_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -080061
Kai Tinkessfb460372020-02-08 14:05:48 -080062 const AbsoluteEncoderProfiledJointStatus *const intake_status =
Sabina Davis0f2d38c2020-02-08 17:01:21 -080063 GetMutableTemporaryPointer(*status->fbb(), intake_status_offset);
64
Kai Tinkessfb460372020-02-08 14:05:48 -080065 const PotAndAbsoluteEncoderProfiledJointStatus *const turret_status =
66 GetMutableTemporaryPointer(*status->fbb(), turret_status_offset);
67
68 zeroed = hood_status->zeroed() && intake_status->zeroed() &&
69 turret_status->zeroed();
70 estopped = hood_status->estopped() || intake_status->estopped() ||
71 turret_status->estopped();
Stephan Massaltd021f972020-01-05 20:41:23 -080072 }
73
74 Status::Builder status_builder = status->MakeBuilder<Status>();
75
Sabina Daviscf08b152020-01-31 22:12:09 -080076 status_builder.add_zeroed(zeroed);
77 status_builder.add_estopped(estopped);
78
79 status_builder.add_hood(hood_status_offset);
Sabina Davis0f2d38c2020-02-08 17:01:21 -080080 status_builder.add_intake(intake_status_offset);
Kai Tinkessfb460372020-02-08 14:05:48 -080081 status_builder.add_turret(turret_status_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -080082
83 status->Send(status_builder.Finish());
Sabina Davis0f2d38c2020-02-08 17:01:21 -080084
85 if (output != nullptr) {
86 if (unsafe_goal) {
87 output_struct.intake_roller_voltage = unsafe_goal->roller_voltage();
88 } else {
89 output_struct.intake_roller_voltage = 0.0;
90 }
91 output->Send(Output::Pack(*output->fbb(), &output_struct));
92 }
Stephan Massaltd021f972020-01-05 20:41:23 -080093}
94
Sabina Daviscf08b152020-01-31 22:12:09 -080095} // namespace superstructure
Stephan Massaltd021f972020-01-05 20:41:23 -080096} // namespace control_loops
97} // namespace y2020