blob: 0d740e91a23def417d6ac59ecef21f5acee4b0bb [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>(
James Kuszmaula53c3ac2020-02-22 19:36:01 -080021 "/drivetrain")),
22 joystick_state_fetcher_(
23 event_loop->MakeFetcher<aos::JoystickState>("/aos")) {
Sabina Daviscf08b152020-01-31 22:12:09 -080024 event_loop->SetRuntimeRealtimePriority(30);
Stephan Massaltd021f972020-01-05 20:41:23 -080025}
26
Sabina Daviscf08b152020-01-31 22:12:09 -080027void Superstructure::RunIteration(const Goal *unsafe_goal,
28 const Position *position,
Stephan Massaltd021f972020-01-05 20:41:23 -080029 aos::Sender<Output>::Builder *output,
30 aos::Sender<Status>::Builder *status) {
31 if (WasReset()) {
32 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Sabina Daviscf08b152020-01-31 22:12:09 -080033 hood_.Reset();
Sabina Davis0f2d38c2020-02-08 17:01:21 -080034 intake_joint_.Reset();
Kai Tinkessfb460372020-02-08 14:05:48 -080035 turret_.Reset();
Stephan Massaltd021f972020-01-05 20:41:23 -080036 }
37
Sabina Davis0f31d3f2020-02-20 20:41:00 -080038 const aos::monotonic_clock::time_point position_timestamp =
39 event_loop()->context().monotonic_event_time;
40
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080041 if (drivetrain_status_fetcher_.Fetch()) {
James Kuszmaula53c3ac2020-02-22 19:36:01 -080042 aos::Alliance alliance = aos::Alliance::kInvalid;
James Kuszmaul519585d2020-03-08 22:32:48 -070043 joystick_state_fetcher_.Fetch();
44 if (joystick_state_fetcher_.get() != nullptr) {
James Kuszmaula53c3ac2020-02-22 19:36:01 -080045 alliance = joystick_state_fetcher_->alliance();
46 }
James Kuszmaul3b393d72020-02-26 19:43:51 -080047 const turret::Aimer::WrapMode mode =
James Kuszmaulb83d6e12020-02-22 20:44:48 -080048 (unsafe_goal != nullptr && unsafe_goal->shooting())
James Kuszmaul3b393d72020-02-26 19:43:51 -080049 ? turret::Aimer::WrapMode::kAvoidWrapping
50 : turret::Aimer::WrapMode::kAvoidEdges;
51 aimer_.Update(drivetrain_status_fetcher_.get(), alliance, mode,
52 turret::Aimer::ShotMode::kShootOnTheFly);
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080053 }
54
55 const flatbuffers::Offset<AimerStatus> aimer_status_offset =
56 aimer_.PopulateStatus(status->fbb());
57
Sabina Daviscf08b152020-01-31 22:12:09 -080058 OutputT output_struct;
59
60 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> hood_status_offset =
61 hood_.Iterate(unsafe_goal != nullptr ? unsafe_goal->hood() : nullptr,
62 position->hood(),
63 output != nullptr ? &(output_struct.hood_voltage) : nullptr,
64 status->fbb());
65
Austin Schuh13e55522020-02-29 23:11:17 -080066 if (unsafe_goal != nullptr) {
67 if (unsafe_goal->shooting() &&
68 shooting_start_time_ == aos::monotonic_clock::min_time) {
69 shooting_start_time_ = position_timestamp;
70 }
71
72 if (unsafe_goal->shooting()) {
73 constexpr std::chrono::milliseconds kPeriod =
74 std::chrono::milliseconds(250);
75 if ((position_timestamp - shooting_start_time_) % (kPeriod * 2) <
76 kPeriod) {
77 intake_joint_.set_min_position(-0.25);
78 } else {
79 intake_joint_.set_min_position(-0.75);
80 }
81 } else {
82 intake_joint_.clear_min_position();
83 }
84
85 if (!unsafe_goal->shooting()) {
86 shooting_start_time_ = aos::monotonic_clock::min_time;
87 }
88 }
89
Sabina Davis0f2d38c2020-02-08 17:01:21 -080090 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> intake_status_offset =
91 intake_joint_.Iterate(
92 unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr,
93 position->intake_joint(),
94 output != nullptr ? &(output_struct.intake_joint_voltage) : nullptr,
95 status->fbb());
96
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080097 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
98 *turret_goal = unsafe_goal != nullptr ? (unsafe_goal->turret_tracking()
99 ? aimer_.TurretGoal()
100 : unsafe_goal->turret())
101 : nullptr;
Kai Tinkessfb460372020-02-08 14:05:48 -0800102 flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
103 turret_status_offset = turret_.Iterate(
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800104 turret_goal, position->turret(),
Kai Tinkessfb460372020-02-08 14:05:48 -0800105 output != nullptr ? &(output_struct.turret_voltage) : nullptr,
106 status->fbb());
107
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800108 flatbuffers::Offset<ShooterStatus> shooter_status_offset =
109 shooter_.RunIteration(
110 unsafe_goal != nullptr ? unsafe_goal->shooter() : nullptr,
111 position->shooter(), status->fbb(),
112 output != nullptr ? &(output_struct) : nullptr, position_timestamp);
113
John Park0a245a02020-02-02 14:10:15 -0800114 climber_.Iterate(unsafe_goal, output != nullptr ? &(output_struct) : nullptr);
115
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800116 const AbsoluteEncoderProfiledJointStatus *const hood_status =
117 GetMutableTemporaryPointer(*status->fbb(), hood_status_offset);
118
Austin Schuh2fb23642020-02-29 15:10:51 -0800119 const PotAndAbsoluteEncoderProfiledJointStatus *const turret_status =
120 GetMutableTemporaryPointer(*status->fbb(), turret_status_offset);
121
122 if (output != nullptr) {
123 // Friction is a pain and putting a really high burden on the integrator.
James Kuszmaul519585d2020-03-08 22:32:48 -0700124 const double turret_velocity_sign =
125 turret_status->velocity() * kTurretFrictionGain;
Austin Schuh2fb23642020-02-29 15:10:51 -0800126 output_struct.turret_voltage +=
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800127 std::clamp(turret_velocity_sign, -kTurretFrictionVoltageLimit,
Austin Schuh2fb23642020-02-29 15:10:51 -0800128 kTurretFrictionVoltageLimit);
James Kuszmaulb7fe49e2020-03-05 08:24:44 -0800129 output_struct.turret_voltage =
130 std::clamp(output_struct.turret_voltage, -turret_.operating_voltage(),
131 turret_.operating_voltage());
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800132
133 // Friction is a pain and putting a really high burden on the integrator.
134 const double hood_velocity_sign = hood_status->velocity() * kHoodFrictionGain;
135 output_struct.hood_voltage +=
136 std::clamp(hood_velocity_sign, -kHoodFrictionVoltageLimit,
137 kHoodFrictionVoltageLimit);
138
139 // And dither the output.
140 time_ += 0.00505;
141 output_struct.hood_voltage += 1.3 * std::sin(time_ * 2.0 * M_PI * 30.0);
Austin Schuh2fb23642020-02-29 15:10:51 -0800142 }
143
Sabina Daviscf08b152020-01-31 22:12:09 -0800144 bool zeroed;
145 bool estopped;
146
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800147 {
Kai Tinkessfb460372020-02-08 14:05:48 -0800148 const AbsoluteEncoderProfiledJointStatus *const intake_status =
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800149 GetMutableTemporaryPointer(*status->fbb(), intake_status_offset);
150
Kai Tinkessfb460372020-02-08 14:05:48 -0800151 zeroed = hood_status->zeroed() && intake_status->zeroed() &&
152 turret_status->zeroed();
153 estopped = hood_status->estopped() || intake_status->estopped() ||
154 turret_status->estopped();
Stephan Massaltd021f972020-01-05 20:41:23 -0800155 }
156
157 Status::Builder status_builder = status->MakeBuilder<Status>();
158
Sabina Daviscf08b152020-01-31 22:12:09 -0800159 status_builder.add_zeroed(zeroed);
160 status_builder.add_estopped(estopped);
161
162 status_builder.add_hood(hood_status_offset);
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800163 status_builder.add_intake(intake_status_offset);
Kai Tinkessfb460372020-02-08 14:05:48 -0800164 status_builder.add_turret(turret_status_offset);
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800165 status_builder.add_shooter(shooter_status_offset);
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800166 status_builder.add_aimer(aimer_status_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -0800167
168 status->Send(status_builder.Finish());
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800169
170 if (output != nullptr) {
171 if (unsafe_goal) {
Austin Schuh13e55522020-02-29 23:11:17 -0800172 output_struct.washing_machine_spinner_voltage = 0.0;
Austin Schuh43a220f2020-02-26 22:02:34 -0800173 if (unsafe_goal->shooting()) {
Austin Schuh13e55522020-02-29 23:11:17 -0800174 if (shooter_.ready() &&
175 unsafe_goal->shooter()->velocity_accelerator() > 10.0 &&
176 unsafe_goal->shooter()->velocity_finisher() > 10.0) {
Austin Schuh93109a52020-03-04 21:37:33 -0800177 output_struct.feeder_voltage = 12.0;
Austin Schuh13e55522020-02-29 23:11:17 -0800178 } else {
179 output_struct.feeder_voltage = 0.0;
180 }
181 output_struct.washing_machine_spinner_voltage = 5.0;
182 output_struct.intake_roller_voltage = 3.0;
Austin Schuh43a220f2020-02-26 22:02:34 -0800183 } else {
184 output_struct.feeder_voltage = 0.0;
Austin Schuh13e55522020-02-29 23:11:17 -0800185 output_struct.intake_roller_voltage = unsafe_goal->roller_voltage();
Austin Schuh43a220f2020-02-26 22:02:34 -0800186 }
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800187 } else {
188 output_struct.intake_roller_voltage = 0.0;
189 }
190 output->Send(Output::Pack(*output->fbb(), &output_struct));
191 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800192}
193
Sabina Daviscf08b152020-01-31 22:12:09 -0800194} // namespace superstructure
Stephan Massaltd021f972020-01-05 20:41:23 -0800195} // namespace control_loops
196} // namespace y2020