blob: 013db301ba260594102002fb910c514458a7878a [file] [log] [blame]
milind-u086d7262022-01-19 20:44:18 -08001#include "y2022/control_loops/superstructure/superstructure.h"
2
3#include "aos/events/event_loop.h"
Milind Upadhyay225156b2022-02-25 22:42:12 -08004#include "y2022/control_loops/superstructure/collision_avoidance.h"
milind-u086d7262022-01-19 20:44:18 -08005
6namespace y2022 {
7namespace control_loops {
8namespace superstructure {
9
10using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
11using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
Henry Speiser55aa3ba2022-02-21 23:21:12 -080012using frc971::control_loops::RelativeEncoderProfiledJointStatus;
milind-u086d7262022-01-19 20:44:18 -080013
14Superstructure::Superstructure(::aos::EventLoop *event_loop,
Henry Speiser55aa3ba2022-02-21 23:21:12 -080015 std::shared_ptr<const constants::Values> values,
milind-u086d7262022-01-19 20:44:18 -080016 const ::std::string &name)
17 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080018 name),
Austin Schuh39f26f62022-02-24 21:34:46 -080019 values_(values),
20 climber_(values_->climber.subsystem_params),
21 intake_front_(values_->intake_front.subsystem_params),
22 intake_back_(values_->intake_back.subsystem_params),
23 turret_(values_->turret.subsystem_params),
Ravago Jones5da06352022-03-04 20:26:24 -080024 catapult_(*values_),
Henry Speiser55aa3ba2022-02-21 23:21:12 -080025 drivetrain_status_fetcher_(
26 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
Austin Schuh39f26f62022-02-24 21:34:46 -080027 "/drivetrain")),
Ravago Jones5da06352022-03-04 20:26:24 -080028 can_position_fetcher_(
29 event_loop->MakeFetcher<CANPosition>("/superstructure")) {
milind-u086d7262022-01-19 20:44:18 -080030 event_loop->SetRuntimeRealtimePriority(30);
31}
32
33void Superstructure::RunIteration(const Goal *unsafe_goal,
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080034 const Position *position,
milind-u086d7262022-01-19 20:44:18 -080035 aos::Sender<Output>::Builder *output,
36 aos::Sender<Status>::Builder *status) {
37 if (WasReset()) {
38 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Henry Speiser55aa3ba2022-02-21 23:21:12 -080039 intake_front_.Reset();
40 intake_back_.Reset();
41 turret_.Reset();
42 climber_.Reset();
Austin Schuh39f26f62022-02-24 21:34:46 -080043 catapult_.Reset();
Henry Speiser55aa3ba2022-02-21 23:21:12 -080044 }
45
Ravago Jones5da06352022-03-04 20:26:24 -080046 OutputT output_struct;
Milind Upadhyay225156b2022-02-25 22:42:12 -080047
Ravago Jones5da06352022-03-04 20:26:24 -080048 aos::FlatbufferFixedAllocatorArray<
49 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 64>
50 turret_goal_buffer;
51
52 const aos::monotonic_clock::time_point timestamp =
53 event_loop()->context().monotonic_event_time;
Milind Upadhyay225156b2022-02-25 22:42:12 -080054
Henry Speiser55aa3ba2022-02-21 23:21:12 -080055 drivetrain_status_fetcher_.Fetch();
56 const float velocity = robot_velocity();
57
Ravago Jones5da06352022-03-04 20:26:24 -080058 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
59 *turret_goal = nullptr;
60 double roller_speed_compensated_front = 0.0;
61 double roller_speed_compensated_back = 0.0;
62 double transfer_roller_speed = 0.0;
63 double flipper_arms_voltage = 0.0;
Henry Speiser55aa3ba2022-02-21 23:21:12 -080064
65 if (unsafe_goal != nullptr) {
Ravago Jones5da06352022-03-04 20:26:24 -080066 turret_goal = unsafe_goal->turret();
67
Henry Speiser55aa3ba2022-02-21 23:21:12 -080068 roller_speed_compensated_front =
69 unsafe_goal->roller_speed_front() +
70 std::max(velocity * unsafe_goal->roller_speed_compensation(), 0.0);
71
72 roller_speed_compensated_back =
73 unsafe_goal->roller_speed_back() -
74 std::min(velocity * unsafe_goal->roller_speed_compensation(), 0.0);
75
76 transfer_roller_speed = unsafe_goal->transfer_roller_speed();
milind-u086d7262022-01-19 20:44:18 -080077 }
78
Ravago Jones5da06352022-03-04 20:26:24 -080079 // TODO: Aimer sets turret_goal here
Austin Schuh39f26f62022-02-24 21:34:46 -080080
Ravago Jones5da06352022-03-04 20:26:24 -080081 // Supersturcture state machine:
82 // 1. IDLE: Wait until an intake beambreak is triggerred, meaning that a ball
83 // is being intaked. This means that the transfer rollers have a ball. If
84 // we've been waiting here for too long without any beambreak triggered, the
85 // ball got lost, so reset.
86 // 2. TRANSFERRING: Until the turret reaches the loading position where the
87 // ball can be transferred into the catapult, wiggle the ball in place.
88 // Once the turret reaches the loading position, send the ball forward with
89 // the transfer rollers until the turret beambreak is triggered.
90 // If we have been in this state for too long, the ball probably got lost so
91 // reset back to IDLE.
92 // 3. LOADING: To load the ball into the catapult, put the flippers at the
93 // feeding speed. Wait for a timeout, and then wait until the ball has gone
94 // past the turret beambreak and the flippers have stopped moving, meaning
95 // that the ball is fully loaded in the catapult.
96 // 4. LOADED: Wait until the user asks us to fire to transition to the
97 // shooting stage. If asked to cancel the shot, reset back to the IDLE state.
98 // 5. SHOOTING: Open the flippers to get ready for the shot. If they don't
99 // open quickly enough, try reseating the ball and going back to the LOADING
100 // stage, which moves the flippers in the opposite direction first.
101 // Now, hold the flippers open and wait until the turret has reached its
102 // aiming goal. Once the turret is ready, tell the catapult to fire.
103 // If the flippers move back for some reason now, it could damage the
104 // catapult, so estop it. Otherwise, wait until the catapult shoots a ball and
105 // goes back to its return position. We have now finished the shot, so return
106 // to IDLE.
107
108 const bool is_spitting = ((intake_state_ == IntakeState::INTAKE_FRONT_BALL &&
109 transfer_roller_speed < 0) ||
110 (intake_state_ == IntakeState::INTAKE_BACK_BALL &&
111 transfer_roller_speed > 0));
112
113 // Intake handling should happen regardless of the turret state
114 if (position->intake_beambreak_front() || position->intake_beambreak_back()) {
115 if (intake_state_ == IntakeState::NO_BALL) {
116 if (position->intake_beambreak_front()) {
117 intake_state_ = IntakeState::INTAKE_FRONT_BALL;
118 } else if (position->intake_beambreak_back()) {
119 intake_state_ = IntakeState::INTAKE_BACK_BALL;
120 }
121 }
122
123 intake_beambreak_timer_ = timestamp;
124 }
125
126 if (intake_state_ != IntakeState::NO_BALL) {
127 // Block intaking in
128 roller_speed_compensated_front = 0.0;
129 roller_speed_compensated_back = 0.0;
130
131 const double wiggle_voltage =
132 (intake_state_ == IntakeState::INTAKE_FRONT_BALL
133 ? constants::Values::kTransferRollerFrontWiggleVoltage()
134 : constants::Values::kTransferRollerBackWiggleVoltage());
135 // Wiggle transfer rollers: send the ball back and forth while waiting
136 // for the turret or waiting for another shot to be completed
137 if ((intake_state_ == IntakeState::INTAKE_FRONT_BALL &&
138 position->intake_beambreak_front()) ||
139 (intake_state_ == IntakeState::INTAKE_BACK_BALL &&
140 position->intake_beambreak_back())) {
141 transfer_roller_speed = -wiggle_voltage;
142 } else {
143 transfer_roller_speed = wiggle_voltage;
144 }
145 }
146
147 switch (state_) {
148 case SuperstructureState::IDLE: {
149 if (timestamp >
150 intake_beambreak_timer_ + constants::Values::kBallLostTime()) {
151 intake_state_ = IntakeState::NO_BALL;
152 }
153
154 if (is_spitting) {
155 intake_state_ = IntakeState::NO_BALL;
156 }
157
158 if (intake_state_ == IntakeState::NO_BALL ||
159 !(position->intake_beambreak_front() ||
160 position->intake_beambreak_back())) {
161 break;
162 }
163
164 state_ = SuperstructureState::TRANSFERRING;
Ravago Jones5da06352022-03-04 20:26:24 -0800165 // Save the side the ball is on for later
166
167 break;
168 }
169 case SuperstructureState::TRANSFERRING: {
170 // If we've been transferring for too long, the ball probably got lost
171 if (timestamp >
Milind Upadhyay47e0d032022-03-05 23:06:25 -0800172 intake_beambreak_timer_ + constants::Values::kBallLostTime()) {
Ravago Jones5da06352022-03-04 20:26:24 -0800173 intake_state_ = IntakeState::NO_BALL;
Milind Upadhyay47e0d032022-03-05 23:06:25 -0800174 state_ = SuperstructureState::IDLE;
Ravago Jones5da06352022-03-04 20:26:24 -0800175 break;
176 }
177
178 if (intake_state_ == IntakeState::NO_BALL) {
179 state_ = SuperstructureState::IDLE;
180 break;
181 }
182
183 double turret_loading_position =
184 (intake_state_ == IntakeState::INTAKE_FRONT_BALL
185 ? constants::Values::kTurretFrontIntakePos()
186 : constants::Values::kTurretBackIntakePos());
187
188 turret_goal_buffer.Finish(
189 frc971::control_loops::
190 CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
191 *turret_goal_buffer.fbb(), turret_loading_position));
192 turret_goal = &turret_goal_buffer.message();
193
194 const bool turret_near_goal =
195 std::abs(turret_.estimated_position() - turret_loading_position) <
196 kTurretGoalThreshold;
197 if (!turret_near_goal) {
198 break; // Wait for turret to reach the chosen intake
199 }
200
201 // Transfer rollers and flipper arm belt on
202 transfer_roller_speed =
203 (intake_state_ == IntakeState::INTAKE_FRONT_BALL
204 ? constants::Values::kTransferRollerFrontVoltage()
205 : constants::Values::kTransferRollerBackVoltage());
206 flipper_arms_voltage = constants::Values::kFlipperFeedVoltage();
207
208 // Ball is in catapult
209 if (position->turret_beambreak()) {
210 intake_state_ = IntakeState::NO_BALL;
211 state_ = SuperstructureState::LOADING;
212 loading_timer_ = timestamp;
213 }
214 break;
215 }
216 case SuperstructureState::LOADING: {
217 flipper_arms_voltage = constants::Values::kFlipperFeedVoltage();
218
219 // Keep feeding for kExtraLoadingTime
220
Ravago Jones5da06352022-03-04 20:26:24 -0800221 // The ball should go past the turret beambreak to be loaded.
222 // If we got a CAN reading not too long ago, the flippers should have also
223 // stopped.
Milind Upadhyay47e0d032022-03-05 23:06:25 -0800224 if (position->turret_beambreak()) {
225 loading_timer_ = timestamp;
226 } else if (timestamp >
227 loading_timer_ + constants::Values::kExtraLoadingTime()) {
Ravago Jones5da06352022-03-04 20:26:24 -0800228 state_ = SuperstructureState::LOADED;
229 reseating_in_catapult_ = false;
230 }
231 break;
232 }
233 case SuperstructureState::LOADED: {
234 if (unsafe_goal != nullptr) {
235 if (unsafe_goal->cancel_shot()) {
236 // Cancel the shot process
237 state_ = SuperstructureState::IDLE;
238 } else if (unsafe_goal->fire()) {
239 // Start if we were asked to and the turret is at goal
240 state_ = SuperstructureState::SHOOTING;
241 prev_shot_count_ = catapult_.shot_count();
242
243 // Reset opening timeout
244 flipper_opening_start_time_ = timestamp;
245 }
246 }
247 break;
248 }
249 case SuperstructureState::SHOOTING: {
250 // Opening flipper arms could fail, wait until they are open using their
251 // potentiometers (the member below is just named encoder).
252 // Be a little more lenient if the flippers were already open in case of
253 // noise or collisions.
254 const double flipper_open_position =
255 (flippers_open_ ? constants::Values::kReseatFlipperPosition()
256 : constants::Values::kFlipperOpenPosition());
257 flippers_open_ =
258 position->flipper_arm_left()->encoder() >= flipper_open_position &&
259 position->flipper_arm_right()->encoder() >= flipper_open_position;
260
261 if (flippers_open_) {
262 // Hold at kFlipperHoldVoltage
263 flipper_arms_voltage = constants::Values::kFlipperHoldVoltage();
264 } else {
265 // Open at kFlipperOpenVoltage
266 flipper_arms_voltage = constants::Values::kFlipperOpenVoltage();
267 }
268
269 if (!flippers_open_ &&
270 timestamp >
271 loading_timer_ + constants::Values::kFlipperOpeningTimeout()) {
272 // Reseat the ball and try again
273 state_ = SuperstructureState::LOADING;
274 loading_timer_ = timestamp;
275 reseating_in_catapult_ = true;
276 break;
277 }
278
279 const bool turret_near_goal =
280 turret_goal != nullptr &&
281 std::abs(turret_goal->unsafe_goal() - turret_.position()) <
282 kTurretGoalThreshold;
283
284 // If the turret reached the aiming goal, fire!
285 if (flippers_open_ && turret_near_goal) {
286 fire_ = true;
287 }
288
289 // If we started firing and the flippers closed a bit, estop to prevent
290 // damage
291 if (fire_ && !flippers_open_) {
292 catapult_.Estop();
293 }
294
295 const bool near_return_position =
296 (unsafe_goal != nullptr && unsafe_goal->has_catapult() &&
297 unsafe_goal->catapult()->has_return_position() &&
298 std::abs(unsafe_goal->catapult()->return_position()->unsafe_goal() -
299 catapult_.estimated_position()) < kCatapultGoalThreshold);
300
301 // Once the shot is complete and the catapult is back to its return
302 // position, go back to IDLE
303 if (catapult_.shot_count() > prev_shot_count_ && near_return_position) {
304 prev_shot_count_ = catapult_.shot_count();
305 fire_ = false;
306 state_ = SuperstructureState::IDLE;
307 }
308
309 break;
310 }
311 }
312
313 collision_avoidance_.UpdateGoal(
314 {.intake_front_position = intake_front_.estimated_position(),
315 .intake_back_position = intake_back_.estimated_position(),
316 .turret_position = turret_.estimated_position()},
317 turret_goal);
318
319 turret_.set_min_position(collision_avoidance_.min_turret_goal());
320 turret_.set_max_position(collision_avoidance_.max_turret_goal());
321 intake_front_.set_min_position(collision_avoidance_.min_intake_front_goal());
322 intake_front_.set_max_position(collision_avoidance_.max_intake_front_goal());
323 intake_back_.set_min_position(collision_avoidance_.min_intake_back_goal());
324 intake_back_.set_max_position(collision_avoidance_.max_intake_back_goal());
325
326 // Disable the catapult if we want to restart to prevent damage with flippers
Austin Schuh39f26f62022-02-24 21:34:46 -0800327 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Ravago Jones5da06352022-03-04 20:26:24 -0800328 catapult_status_offset =
329 catapult_.Iterate(unsafe_goal, position,
330 output != nullptr && !catapult_.estopped()
331 ? &(output_struct.catapult_voltage)
332 : nullptr,
333 fire_, status->fbb());
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800334
Ravago Jones5da06352022-03-04 20:26:24 -0800335 const flatbuffers::Offset<RelativeEncoderProfiledJointStatus>
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800336 climber_status_offset = climber_.Iterate(
337 unsafe_goal != nullptr ? unsafe_goal->climber() : nullptr,
Austin Schuh7f120362022-03-05 17:10:11 -0800338 position->climber(),
339 output != nullptr ? &output_struct.climber_voltage : nullptr,
340 status->fbb());
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800341
Ravago Jones5da06352022-03-04 20:26:24 -0800342 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800343 intake_status_offset_front = intake_front_.Iterate(
344 unsafe_goal != nullptr ? unsafe_goal->intake_front() : nullptr,
Austin Schuh7f120362022-03-05 17:10:11 -0800345 position->intake_front(),
346 output != nullptr ? &output_struct.intake_voltage_front : nullptr,
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800347 status->fbb());
348
Ravago Jones5da06352022-03-04 20:26:24 -0800349 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800350 intake_status_offset_back = intake_back_.Iterate(
351 unsafe_goal != nullptr ? unsafe_goal->intake_back() : nullptr,
Austin Schuh7f120362022-03-05 17:10:11 -0800352 position->intake_back(),
353 output != nullptr ? &output_struct.intake_voltage_back : nullptr,
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800354 status->fbb());
355
Ravago Jones5da06352022-03-04 20:26:24 -0800356 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Austin Schuh7f120362022-03-05 17:10:11 -0800357 turret_status_offset = turret_.Iterate(
358 turret_goal, position->turret(),
359 output != nullptr ? &output_struct.turret_voltage : nullptr,
360 status->fbb());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800361
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800362 if (output != nullptr) {
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800363 output_struct.roller_voltage_front = roller_speed_compensated_front;
364 output_struct.roller_voltage_back = roller_speed_compensated_back;
365 output_struct.transfer_roller_voltage = transfer_roller_speed;
Ravago Jones5da06352022-03-04 20:26:24 -0800366 output_struct.flipper_arms_voltage = flipper_arms_voltage;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800367
milind-u086d7262022-01-19 20:44:18 -0800368 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
369 }
370
371 Status::Builder status_builder = status->MakeBuilder<Status>();
372
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800373 const bool zeroed = intake_front_.zeroed() && intake_back_.zeroed() &&
Ravago Jones5da06352022-03-04 20:26:24 -0800374 turret_.zeroed() && climber_.zeroed() &&
375 catapult_.zeroed();
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800376 const bool estopped = intake_front_.estopped() || intake_back_.estopped() ||
Ravago Jones5da06352022-03-04 20:26:24 -0800377 turret_.estopped() || climber_.estopped() ||
378 catapult_.estopped();
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800379
380 status_builder.add_zeroed(zeroed);
381 status_builder.add_estopped(estopped);
382
383 status_builder.add_intake_front(intake_status_offset_front);
384 status_builder.add_intake_back(intake_status_offset_back);
385 status_builder.add_turret(turret_status_offset);
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800386 status_builder.add_climber(climber_status_offset);
Ravago Jones5da06352022-03-04 20:26:24 -0800387
Austin Schuh39f26f62022-02-24 21:34:46 -0800388 status_builder.add_catapult(catapult_status_offset);
389 status_builder.add_solve_time(catapult_.solve_time());
Austin Schuh80fc2752022-02-25 13:33:56 -0800390 status_builder.add_shot_count(catapult_.shot_count());
Ravago Jones5da06352022-03-04 20:26:24 -0800391 status_builder.add_mpc_active(catapult_.mpc_active());
392
393 status_builder.add_flippers_open(flippers_open_);
394 status_builder.add_reseating_in_catapult(reseating_in_catapult_);
395 status_builder.add_fire(fire_);
396 status_builder.add_state(state_);
397 status_builder.add_intake_state(intake_state_);
milind-u086d7262022-01-19 20:44:18 -0800398
milind-u086d7262022-01-19 20:44:18 -0800399 (void)status->Send(status_builder.Finish());
400}
401
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800402double Superstructure::robot_velocity() const {
403 return (drivetrain_status_fetcher_.get() != nullptr
404 ? drivetrain_status_fetcher_->robot_speed()
405 : 0.0);
406}
407
milind-u086d7262022-01-19 20:44:18 -0800408} // namespace superstructure
409} // namespace control_loops
410} // namespace y2022