blob: d1731e191a723cd7782b4a4e22e536b6b6d934d2 [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),
Sabina Davis0f31d3f2020-02-20 20:41:00 -080018 turret_(constants::GetValues().turret.subsystem_params),
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080019 drivetrain_status_fetcher_(
20 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
21 "/drivetrain")) {
Sabina Daviscf08b152020-01-31 22:12:09 -080022 event_loop->SetRuntimeRealtimePriority(30);
Stephan Massaltd021f972020-01-05 20:41:23 -080023}
24
Sabina Daviscf08b152020-01-31 22:12:09 -080025void Superstructure::RunIteration(const Goal *unsafe_goal,
26 const Position *position,
Stephan Massaltd021f972020-01-05 20:41:23 -080027 aos::Sender<Output>::Builder *output,
28 aos::Sender<Status>::Builder *status) {
29 if (WasReset()) {
30 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Sabina Daviscf08b152020-01-31 22:12:09 -080031 hood_.Reset();
Sabina Davis0f2d38c2020-02-08 17:01:21 -080032 intake_joint_.Reset();
Kai Tinkessfb460372020-02-08 14:05:48 -080033 turret_.Reset();
Stephan Massaltd021f972020-01-05 20:41:23 -080034 }
35
Sabina Davis0f31d3f2020-02-20 20:41:00 -080036 const aos::monotonic_clock::time_point position_timestamp =
37 event_loop()->context().monotonic_event_time;
38
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080039 if (drivetrain_status_fetcher_.Fetch()) {
40 aimer_.Update(drivetrain_status_fetcher_.get());
41 }
42
43 const flatbuffers::Offset<AimerStatus> aimer_status_offset =
44 aimer_.PopulateStatus(status->fbb());
45
Sabina Daviscf08b152020-01-31 22:12:09 -080046 OutputT output_struct;
47
48 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> hood_status_offset =
49 hood_.Iterate(unsafe_goal != nullptr ? unsafe_goal->hood() : nullptr,
50 position->hood(),
51 output != nullptr ? &(output_struct.hood_voltage) : nullptr,
52 status->fbb());
53
Sabina Davis0f2d38c2020-02-08 17:01:21 -080054 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> intake_status_offset =
55 intake_joint_.Iterate(
56 unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr,
57 position->intake_joint(),
58 output != nullptr ? &(output_struct.intake_joint_voltage) : nullptr,
59 status->fbb());
60
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080061 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
62 *turret_goal = unsafe_goal != nullptr ? (unsafe_goal->turret_tracking()
63 ? aimer_.TurretGoal()
64 : unsafe_goal->turret())
65 : nullptr;
Kai Tinkessfb460372020-02-08 14:05:48 -080066 flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
67 turret_status_offset = turret_.Iterate(
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080068 turret_goal, position->turret(),
Kai Tinkessfb460372020-02-08 14:05:48 -080069 output != nullptr ? &(output_struct.turret_voltage) : nullptr,
70 status->fbb());
71
Sabina Davis0f31d3f2020-02-20 20:41:00 -080072 flatbuffers::Offset<ShooterStatus> shooter_status_offset =
73 shooter_.RunIteration(
74 unsafe_goal != nullptr ? unsafe_goal->shooter() : nullptr,
75 position->shooter(), status->fbb(),
76 output != nullptr ? &(output_struct) : nullptr, position_timestamp);
77
John Park0a245a02020-02-02 14:10:15 -080078 climber_.Iterate(unsafe_goal, output != nullptr ? &(output_struct) : nullptr);
79
Sabina Daviscf08b152020-01-31 22:12:09 -080080 bool zeroed;
81 bool estopped;
82
Sabina Davis0f2d38c2020-02-08 17:01:21 -080083 {
Kai Tinkessfb460372020-02-08 14:05:48 -080084 const AbsoluteEncoderProfiledJointStatus *const hood_status =
Sabina Davis0f2d38c2020-02-08 17:01:21 -080085 GetMutableTemporaryPointer(*status->fbb(), hood_status_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -080086
Kai Tinkessfb460372020-02-08 14:05:48 -080087 const AbsoluteEncoderProfiledJointStatus *const intake_status =
Sabina Davis0f2d38c2020-02-08 17:01:21 -080088 GetMutableTemporaryPointer(*status->fbb(), intake_status_offset);
89
Kai Tinkessfb460372020-02-08 14:05:48 -080090 const PotAndAbsoluteEncoderProfiledJointStatus *const turret_status =
91 GetMutableTemporaryPointer(*status->fbb(), turret_status_offset);
92
93 zeroed = hood_status->zeroed() && intake_status->zeroed() &&
94 turret_status->zeroed();
95 estopped = hood_status->estopped() || intake_status->estopped() ||
96 turret_status->estopped();
Stephan Massaltd021f972020-01-05 20:41:23 -080097 }
98
99 Status::Builder status_builder = status->MakeBuilder<Status>();
100
Sabina Daviscf08b152020-01-31 22:12:09 -0800101 status_builder.add_zeroed(zeroed);
102 status_builder.add_estopped(estopped);
103
104 status_builder.add_hood(hood_status_offset);
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800105 status_builder.add_intake(intake_status_offset);
Kai Tinkessfb460372020-02-08 14:05:48 -0800106 status_builder.add_turret(turret_status_offset);
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800107 status_builder.add_shooter(shooter_status_offset);
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800108 status_builder.add_aimer(aimer_status_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -0800109
110 status->Send(status_builder.Finish());
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800111
112 if (output != nullptr) {
113 if (unsafe_goal) {
Austin Schuh43a220f2020-02-26 22:02:34 -0800114 output_struct.washing_machine_spinner_voltage = 6.0;
115 if (unsafe_goal->shooting()) {
116 output_struct.feeder_voltage = 6.0;
117 } else {
118 output_struct.feeder_voltage = 0.0;
119 }
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800120 output_struct.intake_roller_voltage = unsafe_goal->roller_voltage();
121 } else {
122 output_struct.intake_roller_voltage = 0.0;
123 }
124 output->Send(Output::Pack(*output->fbb(), &output_struct));
125 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800126}
127
Sabina Daviscf08b152020-01-31 22:12:09 -0800128} // namespace superstructure
Stephan Massaltd021f972020-01-05 20:41:23 -0800129} // namespace control_loops
130} // namespace y2020