blob: b7d57b41a2c910a507665be05d6526b3cf49734c [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include "y2020/control_loops/superstructure/superstructure.h"
2
milind-u0beb7dc2021-10-16 19:31:33 -07003#include "aos/containers/sized_array.h"
Stephan Massaltd021f972020-01-05 20:41:23 -08004#include "aos/events/event_loop.h"
Austin Schuh01d81c32021-11-06 22:59:56 -07005#include "aos/network/team_number.h"
Stephan Massaltd021f972020-01-05 20:41:23 -08006
7namespace y2020 {
8namespace control_loops {
9namespace superstructure {
10
Ravago Jones937587c2020-12-26 17:21:09 -080011using frc971::control_loops::AbsoluteAndAbsoluteEncoderProfiledJointStatus;
Stephan Massaltd021f972020-01-05 20:41:23 -080012using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
13using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
14
15Superstructure::Superstructure(::aos::EventLoop *event_loop,
16 const ::std::string &name)
James Kuszmaul61750662021-06-21 21:32:33 -070017 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
18 name),
Sabina Davis0f2d38c2020-02-08 17:01:21 -080019 hood_(constants::GetValues().hood),
Kai Tinkessfb460372020-02-08 14:05:48 -080020 intake_joint_(constants::GetValues().intake),
Sabina Davis0f31d3f2020-02-20 20:41:00 -080021 turret_(constants::GetValues().turret.subsystem_params),
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080022 drivetrain_status_fetcher_(
23 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
James Kuszmaula53c3ac2020-02-22 19:36:01 -080024 "/drivetrain")),
25 joystick_state_fetcher_(
Austin Schuh01d81c32021-11-06 22:59:56 -070026 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
27 has_turret_(::aos::network::GetTeamNumber() != 9971) {
Sabina Daviscf08b152020-01-31 22:12:09 -080028 event_loop->SetRuntimeRealtimePriority(30);
Stephan Massaltd021f972020-01-05 20:41:23 -080029}
30
milind upadhyayaec1aee2020-10-13 13:44:33 -070031double Superstructure::robot_speed() const {
32 return (drivetrain_status_fetcher_.get() != nullptr
33 ? drivetrain_status_fetcher_->robot_speed()
34 : 0.0);
35}
36
Sabina Daviscf08b152020-01-31 22:12:09 -080037void Superstructure::RunIteration(const Goal *unsafe_goal,
38 const Position *position,
Stephan Massaltd021f972020-01-05 20:41:23 -080039 aos::Sender<Output>::Builder *output,
40 aos::Sender<Status>::Builder *status) {
41 if (WasReset()) {
42 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Sabina Daviscf08b152020-01-31 22:12:09 -080043 hood_.Reset();
Sabina Davis0f2d38c2020-02-08 17:01:21 -080044 intake_joint_.Reset();
Kai Tinkessfb460372020-02-08 14:05:48 -080045 turret_.Reset();
Stephan Massaltd021f972020-01-05 20:41:23 -080046 }
47
Sabina Davis0f31d3f2020-02-20 20:41:00 -080048 const aos::monotonic_clock::time_point position_timestamp =
49 event_loop()->context().monotonic_event_time;
50
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080051 if (drivetrain_status_fetcher_.Fetch()) {
James Kuszmaula53c3ac2020-02-22 19:36:01 -080052 aos::Alliance alliance = aos::Alliance::kInvalid;
James Kuszmaul519585d2020-03-08 22:32:48 -070053 joystick_state_fetcher_.Fetch();
54 if (joystick_state_fetcher_.get() != nullptr) {
James Kuszmaula53c3ac2020-02-22 19:36:01 -080055 alliance = joystick_state_fetcher_->alliance();
56 }
James Kuszmaul3b393d72020-02-26 19:43:51 -080057 const turret::Aimer::WrapMode mode =
James Kuszmaulb83d6e12020-02-22 20:44:48 -080058 (unsafe_goal != nullptr && unsafe_goal->shooting())
James Kuszmaul3b393d72020-02-26 19:43:51 -080059 ? turret::Aimer::WrapMode::kAvoidWrapping
60 : turret::Aimer::WrapMode::kAvoidEdges;
61 aimer_.Update(drivetrain_status_fetcher_.get(), alliance, mode,
62 turret::Aimer::ShotMode::kShootOnTheFly);
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080063 }
64
milind upadhyayaec1aee2020-10-13 13:44:33 -070065 const float velocity = robot_speed();
66
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080067 const flatbuffers::Offset<AimerStatus> aimer_status_offset =
68 aimer_.PopulateStatus(status->fbb());
69
James Kuszmaul98154a22021-04-03 16:09:29 -070070 const double distance_to_goal = aimer_.DistanceToGoal();
71
72 aos::FlatbufferFixedAllocatorArray<
73 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 64>
74 hood_goal;
75 aos::FlatbufferFixedAllocatorArray<ShooterGoal, 64> shooter_goal;
76
77 constants::Values::ShotParams shot_params;
78 if (constants::GetValues().shot_interpolation_table.GetInRange(
milind-uf7fadbf2021-11-07 14:10:54 -080079 distance_to_goal, &shot_params)) {
James Kuszmaul98154a22021-04-03 16:09:29 -070080 hood_goal.Finish(frc971::control_loops::
81 CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
82 *hood_goal.fbb(), shot_params.hood_angle));
83
milind1f1dca32021-07-03 13:50:07 -070084 shooter_goal.Finish(CreateShooterGoal(*shooter_goal.fbb(),
85 shot_params.velocity_accelerator,
86 shot_params.velocity_finisher));
James Kuszmaul98154a22021-04-03 16:09:29 -070087 } else {
88 hood_goal.Finish(
89 frc971::control_loops::
90 CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
91 *hood_goal.fbb(), constants::GetValues().hood.range.upper));
92
93 shooter_goal.Finish(CreateShooterGoal(*shooter_goal.fbb(), 0.0, 0.0));
94 }
95
Sabina Daviscf08b152020-01-31 22:12:09 -080096 OutputT output_struct;
97
Ravago Jones937587c2020-12-26 17:21:09 -080098 flatbuffers::Offset<AbsoluteAndAbsoluteEncoderProfiledJointStatus>
99 hood_status_offset = hood_.Iterate(
James Kuszmaul98154a22021-04-03 16:09:29 -0700100 unsafe_goal != nullptr
101 ? (unsafe_goal->hood_tracking() ? &hood_goal.message()
102 : unsafe_goal->hood())
103 : nullptr,
Ravago Jones937587c2020-12-26 17:21:09 -0800104 position->hood(),
105 output != nullptr ? &(output_struct.hood_voltage) : nullptr,
106 status->fbb());
Sabina Daviscf08b152020-01-31 22:12:09 -0800107
Austin Schuh346cdb72021-11-14 21:03:07 -0800108 bool intake_out_jostle = false;
109
Austin Schuh13e55522020-02-29 23:11:17 -0800110 if (unsafe_goal != nullptr) {
111 if (unsafe_goal->shooting() &&
112 shooting_start_time_ == aos::monotonic_clock::min_time) {
113 shooting_start_time_ = position_timestamp;
114 }
115
116 if (unsafe_goal->shooting()) {
Austin Schuh93ddcb42021-10-25 21:54:11 -0700117 intake_joint_.set_max_acceleration(30.0);
Austin Schuh13e55522020-02-29 23:11:17 -0800118 constexpr std::chrono::milliseconds kPeriod =
119 std::chrono::milliseconds(250);
120 if ((position_timestamp - shooting_start_time_) % (kPeriod * 2) <
121 kPeriod) {
122 intake_joint_.set_min_position(-0.25);
Austin Schuh346cdb72021-11-14 21:03:07 -0800123 intake_out_jostle = false;
Austin Schuh13e55522020-02-29 23:11:17 -0800124 } else {
Austin Schuh346cdb72021-11-14 21:03:07 -0800125 intake_out_jostle = true;
Austin Schuh13e55522020-02-29 23:11:17 -0800126 intake_joint_.set_min_position(-0.75);
127 }
128 } else {
Austin Schuh93ddcb42021-10-25 21:54:11 -0700129 intake_joint_.clear_max_acceleration();
Austin Schuh13e55522020-02-29 23:11:17 -0800130 intake_joint_.clear_min_position();
131 }
132
133 if (!unsafe_goal->shooting()) {
134 shooting_start_time_ = aos::monotonic_clock::min_time;
135 }
136 }
137
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800138 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> intake_status_offset =
139 intake_joint_.Iterate(
140 unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr,
141 position->intake_joint(),
142 output != nullptr ? &(output_struct.intake_joint_voltage) : nullptr,
143 status->fbb());
144
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800145 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
146 *turret_goal = unsafe_goal != nullptr ? (unsafe_goal->turret_tracking()
147 ? aimer_.TurretGoal()
148 : unsafe_goal->turret())
149 : nullptr;
James Kuszmaul98154a22021-04-03 16:09:29 -0700150
Kai Tinkessfb460372020-02-08 14:05:48 -0800151 flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Austin Schuhdce3f872021-11-12 11:55:14 -0800152 turret_status_offset;
153 if (has_turret_) {
154 turret_status_offset = turret_.Iterate(
155 turret_goal, position->turret(),
156 output != nullptr ? &(output_struct.turret_voltage) : nullptr,
157 status->fbb());
158 } else {
159 PotAndAbsoluteEncoderProfiledJointStatus::Builder turret_builder(
160 *status->fbb());
161 turret_builder.add_position(M_PI);
162 turret_builder.add_velocity(0.0);
163 turret_status_offset = turret_builder.Finish();
164 }
Kai Tinkessfb460372020-02-08 14:05:48 -0800165
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800166 flatbuffers::Offset<ShooterStatus> shooter_status_offset =
167 shooter_.RunIteration(
James Kuszmaul98154a22021-04-03 16:09:29 -0700168 unsafe_goal != nullptr
169 ? (unsafe_goal->shooter_tracking() ? &shooter_goal.message()
170 : unsafe_goal->shooter())
171 : nullptr,
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800172 position->shooter(), status->fbb(),
173 output != nullptr ? &(output_struct) : nullptr, position_timestamp);
174
Ravago Jones937587c2020-12-26 17:21:09 -0800175 const AbsoluteAndAbsoluteEncoderProfiledJointStatus *const hood_status =
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800176 GetMutableTemporaryPointer(*status->fbb(), hood_status_offset);
177
Austin Schuh2fb23642020-02-29 15:10:51 -0800178 const PotAndAbsoluteEncoderProfiledJointStatus *const turret_status =
179 GetMutableTemporaryPointer(*status->fbb(), turret_status_offset);
180
181 if (output != nullptr) {
182 // Friction is a pain and putting a really high burden on the integrator.
James Kuszmaul9cbdb022021-09-19 17:42:29 -0700183 // TODO(james): I'm not sure how helpful this gain is.
James Kuszmaul519585d2020-03-08 22:32:48 -0700184 const double turret_velocity_sign =
185 turret_status->velocity() * kTurretFrictionGain;
Austin Schuh2fb23642020-02-29 15:10:51 -0800186 output_struct.turret_voltage +=
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800187 std::clamp(turret_velocity_sign, -kTurretFrictionVoltageLimit,
Austin Schuh2fb23642020-02-29 15:10:51 -0800188 kTurretFrictionVoltageLimit);
James Kuszmaul9cbdb022021-09-19 17:42:29 -0700189 const double time_sec =
190 aos::time::DurationInSeconds(position_timestamp.time_since_epoch());
191 output_struct.turret_voltage +=
192 kTurretDitherGain * std::sin(2.0 * M_PI * time_sec * 30.0);
James Kuszmaulb7fe49e2020-03-05 08:24:44 -0800193 output_struct.turret_voltage =
194 std::clamp(output_struct.turret_voltage, -turret_.operating_voltage(),
195 turret_.operating_voltage());
Austin Schuh2fb23642020-02-29 15:10:51 -0800196 }
197
Sabina Daviscf08b152020-01-31 22:12:09 -0800198 bool zeroed;
199 bool estopped;
200
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800201 {
Kai Tinkessfb460372020-02-08 14:05:48 -0800202 const AbsoluteEncoderProfiledJointStatus *const intake_status =
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800203 GetMutableTemporaryPointer(*status->fbb(), intake_status_offset);
204
Kai Tinkessfb460372020-02-08 14:05:48 -0800205 zeroed = hood_status->zeroed() && intake_status->zeroed() &&
206 turret_status->zeroed();
207 estopped = hood_status->estopped() || intake_status->estopped() ||
208 turret_status->estopped();
Stephan Massaltd021f972020-01-05 20:41:23 -0800209 }
210
milind-u0beb7dc2021-10-16 19:31:33 -0700211 flatbuffers::Offset<flatbuffers::Vector<Subsystem>>
212 subsystems_not_ready_offset;
213 const bool turret_ready =
Austin Schuh01d81c32021-11-06 22:59:56 -0700214 (std::abs(turret_.goal(0) - turret_.position()) < 0.025) || !has_turret_;
milind-u0beb7dc2021-10-16 19:31:33 -0700215 if (unsafe_goal && unsafe_goal->shooting() &&
216 (!shooter_.ready() || !turret_ready)) {
217 aos::SizedArray<Subsystem, 3> subsystems_not_ready;
218 if (!shooter_.finisher_ready()) {
219 subsystems_not_ready.push_back(Subsystem::FINISHER);
220 }
221 if (!shooter_.accelerator_ready()) {
222 subsystems_not_ready.push_back(Subsystem::ACCELERATOR);
223 }
224 if (!turret_ready) {
225 subsystems_not_ready.push_back(Subsystem::TURRET);
226 }
227
Tyler Chatowd0a49742022-02-25 22:06:19 -0800228 subsystems_not_ready_offset = status->fbb()->CreateVector(
229 subsystems_not_ready.data(), subsystems_not_ready.size());
milind-u0beb7dc2021-10-16 19:31:33 -0700230 }
231
Stephan Massaltd021f972020-01-05 20:41:23 -0800232 Status::Builder status_builder = status->MakeBuilder<Status>();
233
Sabina Daviscf08b152020-01-31 22:12:09 -0800234 status_builder.add_zeroed(zeroed);
235 status_builder.add_estopped(estopped);
236
237 status_builder.add_hood(hood_status_offset);
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800238 status_builder.add_intake(intake_status_offset);
Kai Tinkessfb460372020-02-08 14:05:48 -0800239 status_builder.add_turret(turret_status_offset);
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800240 status_builder.add_shooter(shooter_status_offset);
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800241 status_builder.add_aimer(aimer_status_offset);
milind-u0beb7dc2021-10-16 19:31:33 -0700242 status_builder.add_subsystems_not_ready(subsystems_not_ready_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -0800243
milind1f1dca32021-07-03 13:50:07 -0700244 status_builder.add_send_failures(status_failure_counter_.failures());
245
246 status_failure_counter_.Count(status->Send(status_builder.Finish()));
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800247
248 if (output != nullptr) {
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700249 output_struct.washing_machine_spinner_voltage = 0.0;
250 output_struct.feeder_voltage = 0.0;
251 output_struct.intake_roller_voltage = 0.0;
Austin Schuh46712f52021-10-24 22:24:45 -0700252 output_struct.climber_voltage = 0.0;
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800253 if (unsafe_goal) {
Austin Schuh46712f52021-10-24 22:24:45 -0700254 if (unsafe_goal->has_turret()) {
255 output_struct.climber_voltage =
256 std::clamp(unsafe_goal->climber_voltage(), -12.0f, 12.0f);
Ravago Jonese92ff112021-10-23 17:27:44 -0700257
Austin Schuh46712f52021-10-24 22:24:45 -0700258 // Make sure the turret is relatively close to the goal before turning
259 // the climber on.
260 CHECK(unsafe_goal->has_turret());
261 if (std::abs(unsafe_goal->turret()->unsafe_goal() -
Austin Schuh77813e02021-11-07 23:30:46 -0800262 turret_.position()) > 0.1 &&
263 has_turret_) {
Austin Schuh46712f52021-10-24 22:24:45 -0700264 output_struct.climber_voltage = 0;
265 }
milind-ud6534142021-10-24 17:42:58 -0700266 }
267
Ravago Jonesac850da2021-10-13 20:38:29 -0700268 if (unsafe_goal->shooting() || unsafe_goal->intake_preloading()) {
269 preloading_timeout_ = position_timestamp + kPreloadingTimeout;
270 }
271
272 if (position_timestamp <= preloading_timeout_ &&
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700273 !position->intake_beambreak_triggered()) {
274 output_struct.washing_machine_spinner_voltage = 5.0;
275 output_struct.feeder_voltage = 12.0;
Ravago Jonesac850da2021-10-13 20:38:29 -0700276
277 preloading_backpower_timeout_ =
278 position_timestamp + kPreloadingBackpowerDuration;
279 }
280
281 if (position->intake_beambreak_triggered() &&
282 position_timestamp <= preloading_backpower_timeout_) {
283 output_struct.feeder_voltage = -12.0;
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700284 }
285
Austin Schuhb187ddb2021-11-13 16:16:16 -0800286 if (unsafe_goal->has_feed_voltage_override()) {
287 output_struct.feeder_voltage = unsafe_goal->feed_voltage_override();
288 output_struct.washing_machine_spinner_voltage = -5.0;
289 preloading_timeout_ = position_timestamp;
290 }
291
Austin Schuh43a220f2020-02-26 22:02:34 -0800292 if (unsafe_goal->shooting()) {
Austin Schuh01d81c32021-11-06 22:59:56 -0700293 if ((shooter_.ready() ||
294 (!has_turret_ && shooter_.accelerator_ready())) &&
295 turret_ready) {
Austin Schuh93109a52020-03-04 21:37:33 -0800296 output_struct.feeder_voltage = 12.0;
Austin Schuh13e55522020-02-29 23:11:17 -0800297 }
Austin Schuh346cdb72021-11-14 21:03:07 -0800298
299 if (!intake_out_jostle) {
300 output_struct.washing_machine_spinner_voltage = 5.0;
301 } else {
302 output_struct.washing_machine_spinner_voltage = -5.0;
303 }
Austin Schuh13e55522020-02-29 23:11:17 -0800304 output_struct.intake_roller_voltage = 3.0;
Austin Schuh43a220f2020-02-26 22:02:34 -0800305 } else {
milind upadhyayaec1aee2020-10-13 13:44:33 -0700306 output_struct.intake_roller_voltage =
307 unsafe_goal->roller_voltage() +
308 std::max(velocity * unsafe_goal->roller_speed_compensation(), 0.0f);
Austin Schuh43a220f2020-02-26 22:02:34 -0800309 }
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800310 }
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700311
milind1f1dca32021-07-03 13:50:07 -0700312 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800313 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800314}
315
Sabina Daviscf08b152020-01-31 22:12:09 -0800316} // namespace superstructure
Stephan Massaltd021f972020-01-05 20:41:23 -0800317} // namespace control_loops
318} // namespace y2020