blob: c2512dbbc9dbee3dc40f7b070059a95fbf423617 [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
John Park0a245a02020-02-02 14:10:15 -080055 climber_.Iterate(unsafe_goal, output != nullptr ? &(output_struct) : nullptr);
56
Sabina Daviscf08b152020-01-31 22:12:09 -080057 bool zeroed;
58 bool estopped;
59
Sabina Davis0f2d38c2020-02-08 17:01:21 -080060 {
Kai Tinkessfb460372020-02-08 14:05:48 -080061 const AbsoluteEncoderProfiledJointStatus *const hood_status =
Sabina Davis0f2d38c2020-02-08 17:01:21 -080062 GetMutableTemporaryPointer(*status->fbb(), hood_status_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -080063
Kai Tinkessfb460372020-02-08 14:05:48 -080064 const AbsoluteEncoderProfiledJointStatus *const intake_status =
Sabina Davis0f2d38c2020-02-08 17:01:21 -080065 GetMutableTemporaryPointer(*status->fbb(), intake_status_offset);
66
Kai Tinkessfb460372020-02-08 14:05:48 -080067 const PotAndAbsoluteEncoderProfiledJointStatus *const turret_status =
68 GetMutableTemporaryPointer(*status->fbb(), turret_status_offset);
69
70 zeroed = hood_status->zeroed() && intake_status->zeroed() &&
71 turret_status->zeroed();
72 estopped = hood_status->estopped() || intake_status->estopped() ||
73 turret_status->estopped();
Stephan Massaltd021f972020-01-05 20:41:23 -080074 }
75
76 Status::Builder status_builder = status->MakeBuilder<Status>();
77
Sabina Daviscf08b152020-01-31 22:12:09 -080078 status_builder.add_zeroed(zeroed);
79 status_builder.add_estopped(estopped);
80
81 status_builder.add_hood(hood_status_offset);
Sabina Davis0f2d38c2020-02-08 17:01:21 -080082 status_builder.add_intake(intake_status_offset);
Kai Tinkessfb460372020-02-08 14:05:48 -080083 status_builder.add_turret(turret_status_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -080084
85 status->Send(status_builder.Finish());
Sabina Davis0f2d38c2020-02-08 17:01:21 -080086
87 if (output != nullptr) {
88 if (unsafe_goal) {
89 output_struct.intake_roller_voltage = unsafe_goal->roller_voltage();
90 } else {
91 output_struct.intake_roller_voltage = 0.0;
92 }
93 output->Send(Output::Pack(*output->fbb(), &output_struct));
94 }
Stephan Massaltd021f972020-01-05 20:41:23 -080095}
96
Sabina Daviscf08b152020-01-31 22:12:09 -080097} // namespace superstructure
Stephan Massaltd021f972020-01-05 20:41:23 -080098} // namespace control_loops
99} // namespace y2020