blob: 10738ee5f557808f90032659d9660f4aed370fd9 [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
milind-u0a178a82021-09-28 18:42:09 -070084 shooter_goal.Finish(CreateShooterGoal(
milind-uf7fadbf2021-11-07 14:10:54 -080085 *shooter_goal.fbb(), 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 Schuh13e55522020-02-29 23:11:17 -0800108 if (unsafe_goal != nullptr) {
109 if (unsafe_goal->shooting() &&
110 shooting_start_time_ == aos::monotonic_clock::min_time) {
111 shooting_start_time_ = position_timestamp;
112 }
113
114 if (unsafe_goal->shooting()) {
Austin Schuh93ddcb42021-10-25 21:54:11 -0700115 intake_joint_.set_max_acceleration(30.0);
Austin Schuh13e55522020-02-29 23:11:17 -0800116 constexpr std::chrono::milliseconds kPeriod =
117 std::chrono::milliseconds(250);
118 if ((position_timestamp - shooting_start_time_) % (kPeriod * 2) <
119 kPeriod) {
120 intake_joint_.set_min_position(-0.25);
121 } else {
122 intake_joint_.set_min_position(-0.75);
123 }
124 } else {
Austin Schuh93ddcb42021-10-25 21:54:11 -0700125 intake_joint_.clear_max_acceleration();
Austin Schuh13e55522020-02-29 23:11:17 -0800126 intake_joint_.clear_min_position();
127 }
128
129 if (!unsafe_goal->shooting()) {
130 shooting_start_time_ = aos::monotonic_clock::min_time;
131 }
132 }
133
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800134 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> intake_status_offset =
135 intake_joint_.Iterate(
136 unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr,
137 position->intake_joint(),
138 output != nullptr ? &(output_struct.intake_joint_voltage) : nullptr,
139 status->fbb());
140
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800141 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
142 *turret_goal = unsafe_goal != nullptr ? (unsafe_goal->turret_tracking()
143 ? aimer_.TurretGoal()
144 : unsafe_goal->turret())
145 : nullptr;
James Kuszmaul98154a22021-04-03 16:09:29 -0700146
Kai Tinkessfb460372020-02-08 14:05:48 -0800147 flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Austin Schuhdce3f872021-11-12 11:55:14 -0800148 turret_status_offset;
149 if (has_turret_) {
150 turret_status_offset = turret_.Iterate(
151 turret_goal, position->turret(),
152 output != nullptr ? &(output_struct.turret_voltage) : nullptr,
153 status->fbb());
154 } else {
155 PotAndAbsoluteEncoderProfiledJointStatus::Builder turret_builder(
156 *status->fbb());
157 turret_builder.add_position(M_PI);
158 turret_builder.add_velocity(0.0);
159 turret_status_offset = turret_builder.Finish();
160 }
Kai Tinkessfb460372020-02-08 14:05:48 -0800161
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800162 flatbuffers::Offset<ShooterStatus> shooter_status_offset =
163 shooter_.RunIteration(
James Kuszmaul98154a22021-04-03 16:09:29 -0700164 unsafe_goal != nullptr
165 ? (unsafe_goal->shooter_tracking() ? &shooter_goal.message()
166 : unsafe_goal->shooter())
167 : nullptr,
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800168 position->shooter(), status->fbb(),
169 output != nullptr ? &(output_struct) : nullptr, position_timestamp);
170
Ravago Jones937587c2020-12-26 17:21:09 -0800171 const AbsoluteAndAbsoluteEncoderProfiledJointStatus *const hood_status =
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800172 GetMutableTemporaryPointer(*status->fbb(), hood_status_offset);
173
Austin Schuh2fb23642020-02-29 15:10:51 -0800174 const PotAndAbsoluteEncoderProfiledJointStatus *const turret_status =
175 GetMutableTemporaryPointer(*status->fbb(), turret_status_offset);
176
177 if (output != nullptr) {
178 // Friction is a pain and putting a really high burden on the integrator.
James Kuszmaul9cbdb022021-09-19 17:42:29 -0700179 // TODO(james): I'm not sure how helpful this gain is.
James Kuszmaul519585d2020-03-08 22:32:48 -0700180 const double turret_velocity_sign =
181 turret_status->velocity() * kTurretFrictionGain;
Austin Schuh2fb23642020-02-29 15:10:51 -0800182 output_struct.turret_voltage +=
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800183 std::clamp(turret_velocity_sign, -kTurretFrictionVoltageLimit,
Austin Schuh2fb23642020-02-29 15:10:51 -0800184 kTurretFrictionVoltageLimit);
James Kuszmaul9cbdb022021-09-19 17:42:29 -0700185 const double time_sec =
186 aos::time::DurationInSeconds(position_timestamp.time_since_epoch());
187 output_struct.turret_voltage +=
188 kTurretDitherGain * std::sin(2.0 * M_PI * time_sec * 30.0);
James Kuszmaulb7fe49e2020-03-05 08:24:44 -0800189 output_struct.turret_voltage =
190 std::clamp(output_struct.turret_voltage, -turret_.operating_voltage(),
191 turret_.operating_voltage());
Austin Schuh2fb23642020-02-29 15:10:51 -0800192 }
193
Sabina Daviscf08b152020-01-31 22:12:09 -0800194 bool zeroed;
195 bool estopped;
196
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800197 {
Kai Tinkessfb460372020-02-08 14:05:48 -0800198 const AbsoluteEncoderProfiledJointStatus *const intake_status =
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800199 GetMutableTemporaryPointer(*status->fbb(), intake_status_offset);
200
Kai Tinkessfb460372020-02-08 14:05:48 -0800201 zeroed = hood_status->zeroed() && intake_status->zeroed() &&
202 turret_status->zeroed();
203 estopped = hood_status->estopped() || intake_status->estopped() ||
204 turret_status->estopped();
Stephan Massaltd021f972020-01-05 20:41:23 -0800205 }
206
milind-u0beb7dc2021-10-16 19:31:33 -0700207 flatbuffers::Offset<flatbuffers::Vector<Subsystem>>
208 subsystems_not_ready_offset;
209 const bool turret_ready =
Austin Schuh01d81c32021-11-06 22:59:56 -0700210 (std::abs(turret_.goal(0) - turret_.position()) < 0.025) || !has_turret_;
milind-u0beb7dc2021-10-16 19:31:33 -0700211 if (unsafe_goal && unsafe_goal->shooting() &&
212 (!shooter_.ready() || !turret_ready)) {
213 aos::SizedArray<Subsystem, 3> subsystems_not_ready;
214 if (!shooter_.finisher_ready()) {
215 subsystems_not_ready.push_back(Subsystem::FINISHER);
216 }
217 if (!shooter_.accelerator_ready()) {
218 subsystems_not_ready.push_back(Subsystem::ACCELERATOR);
219 }
220 if (!turret_ready) {
221 subsystems_not_ready.push_back(Subsystem::TURRET);
222 }
223
224 subsystems_not_ready_offset =
225 status->fbb()->CreateVector(subsystems_not_ready.backing_array().data(),
226 subsystems_not_ready.size());
227 }
228
Stephan Massaltd021f972020-01-05 20:41:23 -0800229 Status::Builder status_builder = status->MakeBuilder<Status>();
230
Sabina Daviscf08b152020-01-31 22:12:09 -0800231 status_builder.add_zeroed(zeroed);
232 status_builder.add_estopped(estopped);
233
234 status_builder.add_hood(hood_status_offset);
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800235 status_builder.add_intake(intake_status_offset);
Kai Tinkessfb460372020-02-08 14:05:48 -0800236 status_builder.add_turret(turret_status_offset);
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800237 status_builder.add_shooter(shooter_status_offset);
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800238 status_builder.add_aimer(aimer_status_offset);
milind-u0beb7dc2021-10-16 19:31:33 -0700239 status_builder.add_subsystems_not_ready(subsystems_not_ready_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -0800240
241 status->Send(status_builder.Finish());
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800242
243 if (output != nullptr) {
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700244 output_struct.washing_machine_spinner_voltage = 0.0;
245 output_struct.feeder_voltage = 0.0;
246 output_struct.intake_roller_voltage = 0.0;
Austin Schuh46712f52021-10-24 22:24:45 -0700247 output_struct.climber_voltage = 0.0;
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800248 if (unsafe_goal) {
Austin Schuh46712f52021-10-24 22:24:45 -0700249 if (unsafe_goal->has_turret()) {
250 output_struct.climber_voltage =
251 std::clamp(unsafe_goal->climber_voltage(), -12.0f, 12.0f);
Ravago Jonese92ff112021-10-23 17:27:44 -0700252
Austin Schuh46712f52021-10-24 22:24:45 -0700253 // Make sure the turret is relatively close to the goal before turning
254 // the climber on.
255 CHECK(unsafe_goal->has_turret());
256 if (std::abs(unsafe_goal->turret()->unsafe_goal() -
Austin Schuh77813e02021-11-07 23:30:46 -0800257 turret_.position()) > 0.1 &&
258 has_turret_) {
Austin Schuh46712f52021-10-24 22:24:45 -0700259 output_struct.climber_voltage = 0;
260 }
milind-ud6534142021-10-24 17:42:58 -0700261 }
262
Ravago Jonesac850da2021-10-13 20:38:29 -0700263 if (unsafe_goal->shooting() || unsafe_goal->intake_preloading()) {
264 preloading_timeout_ = position_timestamp + kPreloadingTimeout;
265 }
266
267 if (position_timestamp <= preloading_timeout_ &&
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700268 !position->intake_beambreak_triggered()) {
269 output_struct.washing_machine_spinner_voltage = 5.0;
270 output_struct.feeder_voltage = 12.0;
Ravago Jonesac850da2021-10-13 20:38:29 -0700271
272 preloading_backpower_timeout_ =
273 position_timestamp + kPreloadingBackpowerDuration;
274 }
275
276 if (position->intake_beambreak_triggered() &&
277 position_timestamp <= preloading_backpower_timeout_) {
278 output_struct.feeder_voltage = -12.0;
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700279 }
280
Austin Schuhb187ddb2021-11-13 16:16:16 -0800281 if (unsafe_goal->has_feed_voltage_override()) {
282 output_struct.feeder_voltage = unsafe_goal->feed_voltage_override();
283 output_struct.washing_machine_spinner_voltage = -5.0;
284 preloading_timeout_ = position_timestamp;
285 }
286
Austin Schuh43a220f2020-02-26 22:02:34 -0800287 if (unsafe_goal->shooting()) {
Austin Schuh01d81c32021-11-06 22:59:56 -0700288 if ((shooter_.ready() ||
289 (!has_turret_ && shooter_.accelerator_ready())) &&
290 turret_ready) {
Austin Schuh93109a52020-03-04 21:37:33 -0800291 output_struct.feeder_voltage = 12.0;
Austin Schuh13e55522020-02-29 23:11:17 -0800292 }
293 output_struct.washing_machine_spinner_voltage = 5.0;
294 output_struct.intake_roller_voltage = 3.0;
Austin Schuh43a220f2020-02-26 22:02:34 -0800295 } else {
milind upadhyayaec1aee2020-10-13 13:44:33 -0700296 output_struct.intake_roller_voltage =
297 unsafe_goal->roller_voltage() +
298 std::max(velocity * unsafe_goal->roller_speed_compensation(), 0.0f);
Austin Schuh43a220f2020-02-26 22:02:34 -0800299 }
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800300 }
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700301
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800302 output->Send(Output::Pack(*output->fbb(), &output_struct));
303 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800304}
305
Sabina Daviscf08b152020-01-31 22:12:09 -0800306} // namespace superstructure
Stephan Massaltd021f972020-01-05 20:41:23 -0800307} // namespace control_loops
308} // namespace y2020