blob: d16c692c49e3986a9a157d1ec07f4e6aca4d3d95 [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),
19 shooter_() {
Sabina Daviscf08b152020-01-31 22:12:09 -080020 event_loop->SetRuntimeRealtimePriority(30);
Stephan Massaltd021f972020-01-05 20:41:23 -080021}
22
Sabina Daviscf08b152020-01-31 22:12:09 -080023void Superstructure::RunIteration(const Goal *unsafe_goal,
24 const Position *position,
Stephan Massaltd021f972020-01-05 20:41:23 -080025 aos::Sender<Output>::Builder *output,
26 aos::Sender<Status>::Builder *status) {
27 if (WasReset()) {
28 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Sabina Daviscf08b152020-01-31 22:12:09 -080029 hood_.Reset();
Sabina Davis0f2d38c2020-02-08 17:01:21 -080030 intake_joint_.Reset();
Kai Tinkessfb460372020-02-08 14:05:48 -080031 turret_.Reset();
Stephan Massaltd021f972020-01-05 20:41:23 -080032 }
33
Sabina Davis0f31d3f2020-02-20 20:41:00 -080034 const aos::monotonic_clock::time_point position_timestamp =
35 event_loop()->context().monotonic_event_time;
36
Sabina Daviscf08b152020-01-31 22:12:09 -080037 OutputT output_struct;
38
39 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> hood_status_offset =
40 hood_.Iterate(unsafe_goal != nullptr ? unsafe_goal->hood() : nullptr,
41 position->hood(),
42 output != nullptr ? &(output_struct.hood_voltage) : nullptr,
43 status->fbb());
44
Sabina Davis0f2d38c2020-02-08 17:01:21 -080045 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> intake_status_offset =
46 intake_joint_.Iterate(
47 unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr,
48 position->intake_joint(),
49 output != nullptr ? &(output_struct.intake_joint_voltage) : nullptr,
50 status->fbb());
51
Kai Tinkessfb460372020-02-08 14:05:48 -080052 flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
53 turret_status_offset = turret_.Iterate(
54 unsafe_goal != nullptr ? unsafe_goal->turret() : nullptr,
55 position->turret(),
56 output != nullptr ? &(output_struct.turret_voltage) : nullptr,
57 status->fbb());
58
Sabina Davis0f31d3f2020-02-20 20:41:00 -080059 flatbuffers::Offset<ShooterStatus> shooter_status_offset =
60 shooter_.RunIteration(
61 unsafe_goal != nullptr ? unsafe_goal->shooter() : nullptr,
62 position->shooter(), status->fbb(),
63 output != nullptr ? &(output_struct) : nullptr, position_timestamp);
64
John Park0a245a02020-02-02 14:10:15 -080065 climber_.Iterate(unsafe_goal, output != nullptr ? &(output_struct) : nullptr);
66
Austin Schuh78f0bfd2020-02-29 23:04:21 -080067 const AbsoluteEncoderProfiledJointStatus *const hood_status =
68 GetMutableTemporaryPointer(*status->fbb(), hood_status_offset);
69
Austin Schuh2fb23642020-02-29 15:10:51 -080070 const PotAndAbsoluteEncoderProfiledJointStatus *const turret_status =
71 GetMutableTemporaryPointer(*status->fbb(), turret_status_offset);
72
73 if (output != nullptr) {
74 // Friction is a pain and putting a really high burden on the integrator.
Austin Schuh78f0bfd2020-02-29 23:04:21 -080075 const double turret_velocity_sign = turret_status->velocity() * kTurretFrictionGain;
Austin Schuh2fb23642020-02-29 15:10:51 -080076 output_struct.turret_voltage +=
Austin Schuh78f0bfd2020-02-29 23:04:21 -080077 std::clamp(turret_velocity_sign, -kTurretFrictionVoltageLimit,
Austin Schuh2fb23642020-02-29 15:10:51 -080078 kTurretFrictionVoltageLimit);
Austin Schuh78f0bfd2020-02-29 23:04:21 -080079
80 // Friction is a pain and putting a really high burden on the integrator.
81 const double hood_velocity_sign = hood_status->velocity() * kHoodFrictionGain;
82 output_struct.hood_voltage +=
83 std::clamp(hood_velocity_sign, -kHoodFrictionVoltageLimit,
84 kHoodFrictionVoltageLimit);
85
86 // And dither the output.
87 time_ += 0.00505;
88 output_struct.hood_voltage += 1.3 * std::sin(time_ * 2.0 * M_PI * 30.0);
Austin Schuh2fb23642020-02-29 15:10:51 -080089 }
90
Sabina Daviscf08b152020-01-31 22:12:09 -080091 bool zeroed;
92 bool estopped;
93
Sabina Davis0f2d38c2020-02-08 17:01:21 -080094 {
Kai Tinkessfb460372020-02-08 14:05:48 -080095 const AbsoluteEncoderProfiledJointStatus *const intake_status =
Sabina Davis0f2d38c2020-02-08 17:01:21 -080096 GetMutableTemporaryPointer(*status->fbb(), intake_status_offset);
97
Kai Tinkessfb460372020-02-08 14:05:48 -080098 zeroed = hood_status->zeroed() && intake_status->zeroed() &&
99 turret_status->zeroed();
100 estopped = hood_status->estopped() || intake_status->estopped() ||
101 turret_status->estopped();
Stephan Massaltd021f972020-01-05 20:41:23 -0800102 }
103
104 Status::Builder status_builder = status->MakeBuilder<Status>();
105
Sabina Daviscf08b152020-01-31 22:12:09 -0800106 status_builder.add_zeroed(zeroed);
107 status_builder.add_estopped(estopped);
108
109 status_builder.add_hood(hood_status_offset);
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800110 status_builder.add_intake(intake_status_offset);
Kai Tinkessfb460372020-02-08 14:05:48 -0800111 status_builder.add_turret(turret_status_offset);
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800112 status_builder.add_shooter(shooter_status_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -0800113
114 status->Send(status_builder.Finish());
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800115
116 if (output != nullptr) {
117 if (unsafe_goal) {
Austin Schuh43a220f2020-02-26 22:02:34 -0800118 output_struct.washing_machine_spinner_voltage = 6.0;
119 if (unsafe_goal->shooting()) {
120 output_struct.feeder_voltage = 6.0;
121 } else {
122 output_struct.feeder_voltage = 0.0;
123 }
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800124 output_struct.intake_roller_voltage = unsafe_goal->roller_voltage();
125 } else {
126 output_struct.intake_roller_voltage = 0.0;
127 }
128 output->Send(Output::Pack(*output->fbb(), &output_struct));
129 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800130}
131
Sabina Daviscf08b152020-01-31 22:12:09 -0800132} // namespace superstructure
Stephan Massaltd021f972020-01-05 20:41:23 -0800133} // namespace control_loops
134} // namespace y2020