blob: d3c246478d7a0d8e8acf42aac611499288bdd996 [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"
5
6namespace y2020 {
7namespace control_loops {
8namespace superstructure {
9
Ravago Jones937587c2020-12-26 17:21:09 -080010using frc971::control_loops::AbsoluteAndAbsoluteEncoderProfiledJointStatus;
Stephan Massaltd021f972020-01-05 20:41:23 -080011using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
12using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
13
14Superstructure::Superstructure(::aos::EventLoop *event_loop,
15 const ::std::string &name)
James Kuszmaul61750662021-06-21 21:32:33 -070016 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
17 name),
Sabina Davis0f2d38c2020-02-08 17:01:21 -080018 hood_(constants::GetValues().hood),
Kai Tinkessfb460372020-02-08 14:05:48 -080019 intake_joint_(constants::GetValues().intake),
Sabina Davis0f31d3f2020-02-20 20:41:00 -080020 turret_(constants::GetValues().turret.subsystem_params),
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080021 drivetrain_status_fetcher_(
22 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
James Kuszmaula53c3ac2020-02-22 19:36:01 -080023 "/drivetrain")),
24 joystick_state_fetcher_(
25 event_loop->MakeFetcher<aos::JoystickState>("/aos")) {
Sabina Daviscf08b152020-01-31 22:12:09 -080026 event_loop->SetRuntimeRealtimePriority(30);
Stephan Massaltd021f972020-01-05 20:41:23 -080027}
28
milind upadhyayaec1aee2020-10-13 13:44:33 -070029double Superstructure::robot_speed() const {
30 return (drivetrain_status_fetcher_.get() != nullptr
31 ? drivetrain_status_fetcher_->robot_speed()
32 : 0.0);
33}
34
Sabina Daviscf08b152020-01-31 22:12:09 -080035void Superstructure::RunIteration(const Goal *unsafe_goal,
36 const Position *position,
Stephan Massaltd021f972020-01-05 20:41:23 -080037 aos::Sender<Output>::Builder *output,
38 aos::Sender<Status>::Builder *status) {
39 if (WasReset()) {
40 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Sabina Daviscf08b152020-01-31 22:12:09 -080041 hood_.Reset();
Sabina Davis0f2d38c2020-02-08 17:01:21 -080042 intake_joint_.Reset();
Kai Tinkessfb460372020-02-08 14:05:48 -080043 turret_.Reset();
Stephan Massaltd021f972020-01-05 20:41:23 -080044 }
45
Sabina Davis0f31d3f2020-02-20 20:41:00 -080046 const aos::monotonic_clock::time_point position_timestamp =
47 event_loop()->context().monotonic_event_time;
48
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080049 if (drivetrain_status_fetcher_.Fetch()) {
James Kuszmaula53c3ac2020-02-22 19:36:01 -080050 aos::Alliance alliance = aos::Alliance::kInvalid;
James Kuszmaul519585d2020-03-08 22:32:48 -070051 joystick_state_fetcher_.Fetch();
52 if (joystick_state_fetcher_.get() != nullptr) {
James Kuszmaula53c3ac2020-02-22 19:36:01 -080053 alliance = joystick_state_fetcher_->alliance();
54 }
James Kuszmaul3b393d72020-02-26 19:43:51 -080055 const turret::Aimer::WrapMode mode =
James Kuszmaulb83d6e12020-02-22 20:44:48 -080056 (unsafe_goal != nullptr && unsafe_goal->shooting())
James Kuszmaul3b393d72020-02-26 19:43:51 -080057 ? turret::Aimer::WrapMode::kAvoidWrapping
58 : turret::Aimer::WrapMode::kAvoidEdges;
59 aimer_.Update(drivetrain_status_fetcher_.get(), alliance, mode,
60 turret::Aimer::ShotMode::kShootOnTheFly);
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080061 }
62
milind upadhyayaec1aee2020-10-13 13:44:33 -070063 const float velocity = robot_speed();
64
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080065 const flatbuffers::Offset<AimerStatus> aimer_status_offset =
66 aimer_.PopulateStatus(status->fbb());
67
James Kuszmaul98154a22021-04-03 16:09:29 -070068 const double distance_to_goal = aimer_.DistanceToGoal();
69
70 aos::FlatbufferFixedAllocatorArray<
71 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 64>
72 hood_goal;
73 aos::FlatbufferFixedAllocatorArray<ShooterGoal, 64> shooter_goal;
74
75 constants::Values::ShotParams shot_params;
milind-u0a178a82021-09-28 18:42:09 -070076 constants::Values::FlywheelShotParams flywheel_shot_params;
James Kuszmaul98154a22021-04-03 16:09:29 -070077 if (constants::GetValues().shot_interpolation_table.GetInRange(
milind-u0a178a82021-09-28 18:42:09 -070078 distance_to_goal, &shot_params) &&
79 constants::GetValues().flywheel_shot_interpolation_table.GetInRange(
80 shot_params.velocity_ball, &flywheel_shot_params)) {
James Kuszmaul98154a22021-04-03 16:09:29 -070081 hood_goal.Finish(frc971::control_loops::
82 CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
83 *hood_goal.fbb(), shot_params.hood_angle));
84
milind-u0a178a82021-09-28 18:42:09 -070085 shooter_goal.Finish(CreateShooterGoal(
86 *shooter_goal.fbb(), flywheel_shot_params.velocity_accelerator,
87 flywheel_shot_params.velocity_finisher));
James Kuszmaul98154a22021-04-03 16:09:29 -070088 } else {
89 hood_goal.Finish(
90 frc971::control_loops::
91 CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
92 *hood_goal.fbb(), constants::GetValues().hood.range.upper));
93
94 shooter_goal.Finish(CreateShooterGoal(*shooter_goal.fbb(), 0.0, 0.0));
95 }
96
Sabina Daviscf08b152020-01-31 22:12:09 -080097 OutputT output_struct;
98
Ravago Jones937587c2020-12-26 17:21:09 -080099 flatbuffers::Offset<AbsoluteAndAbsoluteEncoderProfiledJointStatus>
100 hood_status_offset = hood_.Iterate(
James Kuszmaul98154a22021-04-03 16:09:29 -0700101 unsafe_goal != nullptr
102 ? (unsafe_goal->hood_tracking() ? &hood_goal.message()
103 : unsafe_goal->hood())
104 : nullptr,
Ravago Jones937587c2020-12-26 17:21:09 -0800105 position->hood(),
106 output != nullptr ? &(output_struct.hood_voltage) : nullptr,
107 status->fbb());
Sabina Daviscf08b152020-01-31 22:12:09 -0800108
Austin Schuh13e55522020-02-29 23:11:17 -0800109 if (unsafe_goal != nullptr) {
110 if (unsafe_goal->shooting() &&
111 shooting_start_time_ == aos::monotonic_clock::min_time) {
112 shooting_start_time_ = position_timestamp;
113 }
114
115 if (unsafe_goal->shooting()) {
Austin Schuh93ddcb42021-10-25 21:54:11 -0700116 intake_joint_.set_max_acceleration(30.0);
Austin Schuh13e55522020-02-29 23:11:17 -0800117 constexpr std::chrono::milliseconds kPeriod =
118 std::chrono::milliseconds(250);
119 if ((position_timestamp - shooting_start_time_) % (kPeriod * 2) <
120 kPeriod) {
121 intake_joint_.set_min_position(-0.25);
122 } else {
123 intake_joint_.set_min_position(-0.75);
124 }
125 } else {
Austin Schuh93ddcb42021-10-25 21:54:11 -0700126 intake_joint_.clear_max_acceleration();
Austin Schuh13e55522020-02-29 23:11:17 -0800127 intake_joint_.clear_min_position();
128 }
129
130 if (!unsafe_goal->shooting()) {
131 shooting_start_time_ = aos::monotonic_clock::min_time;
132 }
133 }
134
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800135 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> intake_status_offset =
136 intake_joint_.Iterate(
137 unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr,
138 position->intake_joint(),
139 output != nullptr ? &(output_struct.intake_joint_voltage) : nullptr,
140 status->fbb());
141
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800142 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
143 *turret_goal = unsafe_goal != nullptr ? (unsafe_goal->turret_tracking()
144 ? aimer_.TurretGoal()
145 : unsafe_goal->turret())
146 : nullptr;
James Kuszmaul98154a22021-04-03 16:09:29 -0700147
Kai Tinkessfb460372020-02-08 14:05:48 -0800148 flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
149 turret_status_offset = turret_.Iterate(
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800150 turret_goal, position->turret(),
Kai Tinkessfb460372020-02-08 14:05:48 -0800151 output != nullptr ? &(output_struct.turret_voltage) : nullptr,
152 status->fbb());
153
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800154 flatbuffers::Offset<ShooterStatus> shooter_status_offset =
155 shooter_.RunIteration(
James Kuszmaul98154a22021-04-03 16:09:29 -0700156 unsafe_goal != nullptr
157 ? (unsafe_goal->shooter_tracking() ? &shooter_goal.message()
158 : unsafe_goal->shooter())
159 : nullptr,
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800160 position->shooter(), status->fbb(),
161 output != nullptr ? &(output_struct) : nullptr, position_timestamp);
162
Ravago Jones937587c2020-12-26 17:21:09 -0800163 const AbsoluteAndAbsoluteEncoderProfiledJointStatus *const hood_status =
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800164 GetMutableTemporaryPointer(*status->fbb(), hood_status_offset);
165
Austin Schuh2fb23642020-02-29 15:10:51 -0800166 const PotAndAbsoluteEncoderProfiledJointStatus *const turret_status =
167 GetMutableTemporaryPointer(*status->fbb(), turret_status_offset);
168
169 if (output != nullptr) {
170 // Friction is a pain and putting a really high burden on the integrator.
James Kuszmaul9cbdb022021-09-19 17:42:29 -0700171 // TODO(james): I'm not sure how helpful this gain is.
James Kuszmaul519585d2020-03-08 22:32:48 -0700172 const double turret_velocity_sign =
173 turret_status->velocity() * kTurretFrictionGain;
Austin Schuh2fb23642020-02-29 15:10:51 -0800174 output_struct.turret_voltage +=
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800175 std::clamp(turret_velocity_sign, -kTurretFrictionVoltageLimit,
Austin Schuh2fb23642020-02-29 15:10:51 -0800176 kTurretFrictionVoltageLimit);
James Kuszmaul9cbdb022021-09-19 17:42:29 -0700177 const double time_sec =
178 aos::time::DurationInSeconds(position_timestamp.time_since_epoch());
179 output_struct.turret_voltage +=
180 kTurretDitherGain * std::sin(2.0 * M_PI * time_sec * 30.0);
James Kuszmaulb7fe49e2020-03-05 08:24:44 -0800181 output_struct.turret_voltage =
182 std::clamp(output_struct.turret_voltage, -turret_.operating_voltage(),
183 turret_.operating_voltage());
Austin Schuh2fb23642020-02-29 15:10:51 -0800184 }
185
Sabina Daviscf08b152020-01-31 22:12:09 -0800186 bool zeroed;
187 bool estopped;
188
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800189 {
Kai Tinkessfb460372020-02-08 14:05:48 -0800190 const AbsoluteEncoderProfiledJointStatus *const intake_status =
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800191 GetMutableTemporaryPointer(*status->fbb(), intake_status_offset);
192
Kai Tinkessfb460372020-02-08 14:05:48 -0800193 zeroed = hood_status->zeroed() && intake_status->zeroed() &&
194 turret_status->zeroed();
195 estopped = hood_status->estopped() || intake_status->estopped() ||
196 turret_status->estopped();
Stephan Massaltd021f972020-01-05 20:41:23 -0800197 }
198
milind-u0beb7dc2021-10-16 19:31:33 -0700199 flatbuffers::Offset<flatbuffers::Vector<Subsystem>>
200 subsystems_not_ready_offset;
201 const bool turret_ready =
202 (std::abs(turret_.goal(0) - turret_.position()) < 0.025);
203 if (unsafe_goal && unsafe_goal->shooting() &&
204 (!shooter_.ready() || !turret_ready)) {
205 aos::SizedArray<Subsystem, 3> subsystems_not_ready;
206 if (!shooter_.finisher_ready()) {
207 subsystems_not_ready.push_back(Subsystem::FINISHER);
208 }
209 if (!shooter_.accelerator_ready()) {
210 subsystems_not_ready.push_back(Subsystem::ACCELERATOR);
211 }
212 if (!turret_ready) {
213 subsystems_not_ready.push_back(Subsystem::TURRET);
214 }
215
216 subsystems_not_ready_offset =
217 status->fbb()->CreateVector(subsystems_not_ready.backing_array().data(),
218 subsystems_not_ready.size());
219 }
220
Stephan Massaltd021f972020-01-05 20:41:23 -0800221 Status::Builder status_builder = status->MakeBuilder<Status>();
222
Sabina Daviscf08b152020-01-31 22:12:09 -0800223 status_builder.add_zeroed(zeroed);
224 status_builder.add_estopped(estopped);
225
226 status_builder.add_hood(hood_status_offset);
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800227 status_builder.add_intake(intake_status_offset);
Kai Tinkessfb460372020-02-08 14:05:48 -0800228 status_builder.add_turret(turret_status_offset);
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800229 status_builder.add_shooter(shooter_status_offset);
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800230 status_builder.add_aimer(aimer_status_offset);
milind-u0beb7dc2021-10-16 19:31:33 -0700231 status_builder.add_subsystems_not_ready(subsystems_not_ready_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -0800232
233 status->Send(status_builder.Finish());
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800234
235 if (output != nullptr) {
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700236 output_struct.washing_machine_spinner_voltage = 0.0;
237 output_struct.feeder_voltage = 0.0;
238 output_struct.intake_roller_voltage = 0.0;
Austin Schuh46712f52021-10-24 22:24:45 -0700239 output_struct.climber_voltage = 0.0;
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800240 if (unsafe_goal) {
Austin Schuh46712f52021-10-24 22:24:45 -0700241 if (unsafe_goal->has_turret()) {
242 output_struct.climber_voltage =
243 std::clamp(unsafe_goal->climber_voltage(), -12.0f, 12.0f);
Ravago Jonese92ff112021-10-23 17:27:44 -0700244
Austin Schuh46712f52021-10-24 22:24:45 -0700245 // Make sure the turret is relatively close to the goal before turning
246 // the climber on.
247 CHECK(unsafe_goal->has_turret());
248 if (std::abs(unsafe_goal->turret()->unsafe_goal() -
249 turret_.position()) > 0.1) {
250 output_struct.climber_voltage = 0;
251 }
milind-ud6534142021-10-24 17:42:58 -0700252 }
253
Ravago Jonesac850da2021-10-13 20:38:29 -0700254 if (unsafe_goal->shooting() || unsafe_goal->intake_preloading()) {
255 preloading_timeout_ = position_timestamp + kPreloadingTimeout;
256 }
257
258 if (position_timestamp <= preloading_timeout_ &&
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700259 !position->intake_beambreak_triggered()) {
260 output_struct.washing_machine_spinner_voltage = 5.0;
261 output_struct.feeder_voltage = 12.0;
Ravago Jonesac850da2021-10-13 20:38:29 -0700262
263 preloading_backpower_timeout_ =
264 position_timestamp + kPreloadingBackpowerDuration;
265 }
266
267 if (position->intake_beambreak_triggered() &&
268 position_timestamp <= preloading_backpower_timeout_) {
269 output_struct.feeder_voltage = -12.0;
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700270 }
271
Austin Schuh43a220f2020-02-26 22:02:34 -0800272 if (unsafe_goal->shooting()) {
milind-u0beb7dc2021-10-16 19:31:33 -0700273 if (shooter_.ready() && turret_ready) {
Austin Schuh93109a52020-03-04 21:37:33 -0800274 output_struct.feeder_voltage = 12.0;
Austin Schuh13e55522020-02-29 23:11:17 -0800275 }
276 output_struct.washing_machine_spinner_voltage = 5.0;
277 output_struct.intake_roller_voltage = 3.0;
Austin Schuh43a220f2020-02-26 22:02:34 -0800278 } else {
milind upadhyayaec1aee2020-10-13 13:44:33 -0700279 output_struct.intake_roller_voltage =
280 unsafe_goal->roller_voltage() +
281 std::max(velocity * unsafe_goal->roller_speed_compensation(), 0.0f);
Austin Schuh43a220f2020-02-26 22:02:34 -0800282 }
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800283 }
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700284
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800285 output->Send(Output::Pack(*output->fbb(), &output_struct));
286 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800287}
288
Sabina Daviscf08b152020-01-31 22:12:09 -0800289} // namespace superstructure
Stephan Massaltd021f972020-01-05 20:41:23 -0800290} // namespace control_loops
291} // namespace y2020