blob: 15201f73c4fb369468135aba76f1185e1ee32aa8 [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"
Ravago Jones3283ce02022-03-09 19:31:29 -08004#include "aos/flatbuffer_merge.h"
James Kuszmaul40526952022-03-13 15:56:38 -07005#include "frc971/zeroing/wrap.h"
Milind Upadhyay225156b2022-02-25 22:42:12 -08006#include "y2022/control_loops/superstructure/collision_avoidance.h"
milind-u086d7262022-01-19 20:44:18 -08007
8namespace y2022 {
9namespace control_loops {
10namespace superstructure {
11
12using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
13using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
Henry Speiser55aa3ba2022-02-21 23:21:12 -080014using frc971::control_loops::RelativeEncoderProfiledJointStatus;
milind-u086d7262022-01-19 20:44:18 -080015
16Superstructure::Superstructure(::aos::EventLoop *event_loop,
Henry Speiser55aa3ba2022-02-21 23:21:12 -080017 std::shared_ptr<const constants::Values> values,
milind-u086d7262022-01-19 20:44:18 -080018 const ::std::string &name)
19 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080020 name),
Austin Schuh39f26f62022-02-24 21:34:46 -080021 values_(values),
22 climber_(values_->climber.subsystem_params),
23 intake_front_(values_->intake_front.subsystem_params),
24 intake_back_(values_->intake_back.subsystem_params),
25 turret_(values_->turret.subsystem_params),
Ravago Jones5da06352022-03-04 20:26:24 -080026 catapult_(*values_),
Henry Speiser55aa3ba2022-02-21 23:21:12 -080027 drivetrain_status_fetcher_(
28 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
Austin Schuh39f26f62022-02-24 21:34:46 -080029 "/drivetrain")),
Ravago Jones5da06352022-03-04 20:26:24 -080030 can_position_fetcher_(
31 event_loop->MakeFetcher<CANPosition>("/superstructure")) {
milind-u086d7262022-01-19 20:44:18 -080032 event_loop->SetRuntimeRealtimePriority(30);
33}
34
35void Superstructure::RunIteration(const Goal *unsafe_goal,
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080036 const Position *position,
milind-u086d7262022-01-19 20:44:18 -080037 aos::Sender<Output>::Builder *output,
38 aos::Sender<Status>::Builder *status) {
39 if (WasReset()) {
40 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Henry Speiser55aa3ba2022-02-21 23:21:12 -080041 intake_front_.Reset();
42 intake_back_.Reset();
43 turret_.Reset();
44 climber_.Reset();
Austin Schuh39f26f62022-02-24 21:34:46 -080045 catapult_.Reset();
Henry Speiser55aa3ba2022-02-21 23:21:12 -080046 }
47
Ravago Jones5da06352022-03-04 20:26:24 -080048 OutputT output_struct;
Milind Upadhyay225156b2022-02-25 22:42:12 -080049
Ravago Jones5da06352022-03-04 20:26:24 -080050 aos::FlatbufferFixedAllocatorArray<
51 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 64>
James Kuszmaul40526952022-03-13 15:56:38 -070052 turret_loading_goal_buffer;
Ravago Jones3283ce02022-03-09 19:31:29 -080053 aos::FlatbufferFixedAllocatorArray<CatapultGoal, 64> catapult_goal_buffer;
Ravago Jones5da06352022-03-04 20:26:24 -080054
55 const aos::monotonic_clock::time_point timestamp =
56 event_loop()->context().monotonic_event_time;
Milind Upadhyay225156b2022-02-25 22:42:12 -080057
Henry Speiser55aa3ba2022-02-21 23:21:12 -080058 drivetrain_status_fetcher_.Fetch();
59 const float velocity = robot_velocity();
60
James Kuszmaul84083f42022-02-27 17:24:38 -080061 const turret::Aimer::Goal *auto_aim_goal = nullptr;
62 if (drivetrain_status_fetcher_.get() != nullptr) {
63 aimer_.Update(drivetrain_status_fetcher_.get(),
64 turret::Aimer::ShotMode::kShootOnTheFly);
65 auto_aim_goal = aimer_.TurretGoal();
66 }
67
Ravago Jones5da06352022-03-04 20:26:24 -080068 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
69 *turret_goal = nullptr;
Ravago Jones3283ce02022-03-09 19:31:29 -080070 const CatapultGoal *catapult_goal = nullptr;
Ravago Jones5da06352022-03-04 20:26:24 -080071 double roller_speed_compensated_front = 0.0;
72 double roller_speed_compensated_back = 0.0;
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -080073 double transfer_roller_speed_front = 0.0;
74 double transfer_roller_speed_back = 0.0;
Ravago Jones5da06352022-03-04 20:26:24 -080075 double flipper_arms_voltage = 0.0;
James Kuszmaul40526952022-03-13 15:56:38 -070076 bool have_active_intake_request = false;
Henry Speiser55aa3ba2022-02-21 23:21:12 -080077
78 if (unsafe_goal != nullptr) {
79 roller_speed_compensated_front =
80 unsafe_goal->roller_speed_front() +
81 std::max(velocity * unsafe_goal->roller_speed_compensation(), 0.0);
82
83 roller_speed_compensated_back =
84 unsafe_goal->roller_speed_back() -
85 std::min(velocity * unsafe_goal->roller_speed_compensation(), 0.0);
86
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -080087 transfer_roller_speed_front = unsafe_goal->transfer_roller_speed_front();
88 transfer_roller_speed_back = unsafe_goal->transfer_roller_speed_back();
milind-u086d7262022-01-19 20:44:18 -080089
James Kuszmaul84083f42022-02-27 17:24:38 -080090 turret_goal =
91 unsafe_goal->auto_aim() ? auto_aim_goal : unsafe_goal->turret();
Ravago Jones3283ce02022-03-09 19:31:29 -080092
93 catapult_goal = unsafe_goal->catapult();
94
95 constants::Values::ShotParams shot_params;
96 const double distance_to_goal = aimer_.DistanceToGoal();
97 if (unsafe_goal->auto_aim() && values_->shot_interpolation_table.GetInRange(
98 distance_to_goal, &shot_params)) {
99 std::optional<flatbuffers::Offset<
100 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal>>
101 return_position_offset;
102 if (unsafe_goal != nullptr && unsafe_goal->has_catapult() &&
103 unsafe_goal->catapult()->has_return_position()) {
104 return_position_offset = {
105 aos::CopyFlatBuffer(unsafe_goal->catapult()->return_position(),
106 catapult_goal_buffer.fbb())};
107 }
108 CatapultGoal::Builder builder(*catapult_goal_buffer.fbb());
109 builder.add_shot_position(shot_params.shot_angle);
110 builder.add_shot_velocity(shot_params.shot_velocity);
111 if (return_position_offset.has_value()) {
112 builder.add_return_position(return_position_offset.value());
113 }
114 catapult_goal_buffer.Finish(builder.Finish());
115 catapult_goal = &catapult_goal_buffer.message();
116 }
James Kuszmaul40526952022-03-13 15:56:38 -0700117
118 if (unsafe_goal->has_turret_intake()) {
119 have_active_intake_request = true;
120 }
James Kuszmaul84083f42022-02-27 17:24:38 -0800121 }
Austin Schuh39f26f62022-02-24 21:34:46 -0800122
Milind Upadhyay803bbf02022-03-11 17:56:26 -0800123 // Superstructure state machine:
Ravago Jones5da06352022-03-04 20:26:24 -0800124 // 1. IDLE: Wait until an intake beambreak is triggerred, meaning that a ball
125 // is being intaked. This means that the transfer rollers have a ball. If
126 // we've been waiting here for too long without any beambreak triggered, the
127 // ball got lost, so reset.
128 // 2. TRANSFERRING: Until the turret reaches the loading position where the
129 // ball can be transferred into the catapult, wiggle the ball in place.
130 // Once the turret reaches the loading position, send the ball forward with
131 // the transfer rollers until the turret beambreak is triggered.
132 // If we have been in this state for too long, the ball probably got lost so
133 // reset back to IDLE.
134 // 3. LOADING: To load the ball into the catapult, put the flippers at the
135 // feeding speed. Wait for a timeout, and then wait until the ball has gone
136 // past the turret beambreak and the flippers have stopped moving, meaning
137 // that the ball is fully loaded in the catapult.
138 // 4. LOADED: Wait until the user asks us to fire to transition to the
139 // shooting stage. If asked to cancel the shot, reset back to the IDLE state.
140 // 5. SHOOTING: Open the flippers to get ready for the shot. If they don't
141 // open quickly enough, try reseating the ball and going back to the LOADING
142 // stage, which moves the flippers in the opposite direction first.
143 // Now, hold the flippers open and wait until the turret has reached its
144 // aiming goal. Once the turret is ready, tell the catapult to fire.
145 // If the flippers move back for some reason now, it could damage the
146 // catapult, so estop it. Otherwise, wait until the catapult shoots a ball and
147 // goes back to its return position. We have now finished the shot, so return
148 // to IDLE.
149
Milind Upadhyay803bbf02022-03-11 17:56:26 -0800150 // If we started off preloaded, skip to the loaded state.
151 // Make sure we weren't already there just in case.
152 if (unsafe_goal != nullptr && unsafe_goal->preloaded()) {
153 switch (state_) {
154 case SuperstructureState::IDLE:
155 case SuperstructureState::TRANSFERRING:
156 case SuperstructureState::LOADING:
157 state_ = SuperstructureState::LOADED;
158 loading_timer_ = timestamp;
159 break;
160 case SuperstructureState::LOADED:
161 case SuperstructureState::SHOOTING:
162 break;
163 }
164 }
165
James Kuszmaul40526952022-03-13 15:56:38 -0700166 if (position->intake_beambreak_front()) {
167 front_intake_has_ball_ = true;
168 front_intake_beambreak_timer_ = timestamp;
Ravago Jones5da06352022-03-04 20:26:24 -0800169 }
170
James Kuszmaul40526952022-03-13 15:56:38 -0700171 if (position->intake_beambreak_back()) {
172 back_intake_has_ball_ = true;
173 back_intake_beambreak_timer_ = timestamp;
Milind Upadhyay5dfabef2022-03-06 14:08:15 -0800174 }
175
James Kuszmaul40526952022-03-13 15:56:38 -0700176 // Check if we're either spitting of have lost the ball.
177 if (transfer_roller_speed_front < 0.0 ||
178 timestamp >
179 front_intake_beambreak_timer_ + constants::Values::kBallLostTime()) {
180 front_intake_has_ball_ = false;
181 }
182
183 if (transfer_roller_speed_back < 0.0 ||
184 timestamp >
185 back_intake_beambreak_timer_ + constants::Values::kBallLostTime()) {
186 back_intake_has_ball_ = false;
187 }
188
189 // Wiggle transfer rollers: send the ball back and forth while waiting
190 // for the turret or waiting for another shot to be completed
191 const double wiggle_voltage =
192 constants::Values::kTransferRollerWiggleVoltage();
193 if (front_intake_has_ball_) {
Ravago Jones5da06352022-03-04 20:26:24 -0800194 roller_speed_compensated_front = 0.0;
James Kuszmaul40526952022-03-13 15:56:38 -0700195 if (position->intake_beambreak_front()) {
196 transfer_roller_speed_front = -wiggle_voltage;
Ravago Jones5da06352022-03-04 20:26:24 -0800197 } else {
James Kuszmaul40526952022-03-13 15:56:38 -0700198 transfer_roller_speed_front = wiggle_voltage;
Ravago Jones5da06352022-03-04 20:26:24 -0800199 }
200 }
201
James Kuszmaul40526952022-03-13 15:56:38 -0700202 if (back_intake_has_ball_) {
203 roller_speed_compensated_back = 0.0;
204 if (position->intake_beambreak_back()) {
205 transfer_roller_speed_back = -wiggle_voltage;
206 } else {
207 transfer_roller_speed_back = wiggle_voltage;
Milind Upadhyay918ecf02022-03-12 15:55:46 -0800208 }
209 }
210
James Kuszmaul40526952022-03-13 15:56:38 -0700211 // Create the goal message for putting the turret in its loading position.
212 // This will then get used in the state machine for the states (IDLE and
213 // TRANSFERRING) that use it.
214 double turret_loading_position =
215 (turret_intake_state_ == RequestedIntake::kFront
216 ? constants::Values::kTurretFrontIntakePos()
217 : constants::Values::kTurretBackIntakePos());
218 // Turn to the loading position as close to the current position as
219 // possible.
220 turret_loading_position =
221 turret_.estimated_position() +
222 aos::math::NormalizeAngle(turret_loading_position -
223 turret_.estimated_position());
224 // if out of range, reset back to within +/- pi of zero.
225 if (turret_loading_position > constants::Values::kTurretRange().upper ||
226 turret_loading_position < constants::Values::kTurretRange().lower) {
Milind Upadhyay918ecf02022-03-12 15:55:46 -0800227 turret_loading_position =
James Kuszmaul40526952022-03-13 15:56:38 -0700228 frc971::zeroing::Wrap(constants::Values::kTurretRange().middle(),
229 turret_loading_position, 2.0 * M_PI);
Milind Upadhyay918ecf02022-03-12 15:55:46 -0800230 }
231
James Kuszmaul40526952022-03-13 15:56:38 -0700232 turret_loading_goal_buffer.Finish(
233 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
234 *turret_loading_goal_buffer.fbb(), turret_loading_position));
235
Ravago Jones5da06352022-03-04 20:26:24 -0800236 switch (state_) {
237 case SuperstructureState::IDLE: {
James Kuszmaul40526952022-03-13 15:56:38 -0700238 // Only change the turret's goal loading position when idle, to prevent us
239 // spinning the turret around when TRANSFERRING...
240 if (front_intake_has_ball_ != back_intake_has_ball_) {
241 if (have_active_intake_request) {
242 turret_intake_state_ = unsafe_goal->turret_intake();
243 }
244 turret_intake_state_ = front_intake_has_ball_ ? RequestedIntake::kFront
245 : RequestedIntake::kBack;
246 }
247 // When IDLE with no specific intake button pressed, allow the goal
248 // message to override the intaking stuff.
249 if (have_active_intake_request || turret_goal == nullptr) {
250 turret_goal = &turret_loading_goal_buffer.message();
Ravago Jones5da06352022-03-04 20:26:24 -0800251 }
252
James Kuszmaul40526952022-03-13 15:56:38 -0700253 if (!front_intake_has_ball_ && !back_intake_has_ball_) {
Ravago Jones5da06352022-03-04 20:26:24 -0800254 break;
255 }
256
257 state_ = SuperstructureState::TRANSFERRING;
Ravago Jones5da06352022-03-04 20:26:24 -0800258 // Save the side the ball is on for later
259
260 break;
261 }
262 case SuperstructureState::TRANSFERRING: {
James Kuszmaul40526952022-03-13 15:56:38 -0700263 // If we've been transferring for too long, the ball probably got lost.
264 if ((turret_intake_state_ == RequestedIntake::kFront &&
265 !front_intake_has_ball_) ||
266 (turret_intake_state_ == RequestedIntake::kBack &&
267 !back_intake_has_ball_)) {
Ravago Jones5da06352022-03-04 20:26:24 -0800268 state_ = SuperstructureState::IDLE;
269 break;
270 }
271
James Kuszmaul40526952022-03-13 15:56:38 -0700272 turret_goal = &turret_loading_goal_buffer.message();
273
Ravago Jones5da06352022-03-04 20:26:24 -0800274 const bool turret_near_goal =
275 std::abs(turret_.estimated_position() - turret_loading_position) <
276 kTurretGoalThreshold;
277 if (!turret_near_goal) {
278 break; // Wait for turret to reach the chosen intake
279 }
280
281 // Transfer rollers and flipper arm belt on
James Kuszmaul40526952022-03-13 15:56:38 -0700282 if (turret_intake_state_ == RequestedIntake::kFront) {
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800283 transfer_roller_speed_front =
284 constants::Values::kTransferRollerVoltage();
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800285 } else {
286 transfer_roller_speed_back =
287 constants::Values::kTransferRollerVoltage();
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800288 }
Ravago Jones5da06352022-03-04 20:26:24 -0800289 flipper_arms_voltage = constants::Values::kFlipperFeedVoltage();
290
291 // Ball is in catapult
292 if (position->turret_beambreak()) {
James Kuszmaul40526952022-03-13 15:56:38 -0700293 if (turret_intake_state_ == RequestedIntake::kFront) {
294 front_intake_has_ball_ = false;
295 } else {
296 back_intake_has_ball_ = false;
297 }
Ravago Jones5da06352022-03-04 20:26:24 -0800298 state_ = SuperstructureState::LOADING;
299 loading_timer_ = timestamp;
300 }
301 break;
302 }
303 case SuperstructureState::LOADING: {
304 flipper_arms_voltage = constants::Values::kFlipperFeedVoltage();
305
306 // Keep feeding for kExtraLoadingTime
307
Ravago Jones5da06352022-03-04 20:26:24 -0800308 // The ball should go past the turret beambreak to be loaded.
Milind Upadhyay5dfabef2022-03-06 14:08:15 -0800309 // If we got a CAN reading not too long ago, the flippers should have
310 // also stopped.
Milind Upadhyay47e0d032022-03-05 23:06:25 -0800311 if (position->turret_beambreak()) {
312 loading_timer_ = timestamp;
313 } else if (timestamp >
314 loading_timer_ + constants::Values::kExtraLoadingTime()) {
Ravago Jones5da06352022-03-04 20:26:24 -0800315 state_ = SuperstructureState::LOADED;
316 reseating_in_catapult_ = false;
317 }
318 break;
319 }
320 case SuperstructureState::LOADED: {
321 if (unsafe_goal != nullptr) {
322 if (unsafe_goal->cancel_shot()) {
323 // Cancel the shot process
324 state_ = SuperstructureState::IDLE;
325 } else if (unsafe_goal->fire()) {
326 // Start if we were asked to and the turret is at goal
327 state_ = SuperstructureState::SHOOTING;
328 prev_shot_count_ = catapult_.shot_count();
329
330 // Reset opening timeout
331 flipper_opening_start_time_ = timestamp;
332 }
333 }
334 break;
335 }
336 case SuperstructureState::SHOOTING: {
337 // Opening flipper arms could fail, wait until they are open using their
338 // potentiometers (the member below is just named encoder).
339 // Be a little more lenient if the flippers were already open in case of
340 // noise or collisions.
341 const double flipper_open_position =
342 (flippers_open_ ? constants::Values::kReseatFlipperPosition()
343 : constants::Values::kFlipperOpenPosition());
milind-u37dc40b2022-03-08 19:51:27 -0800344
345 // TODO(milind): add left arm back once it's fixed
Ravago Jones5da06352022-03-04 20:26:24 -0800346 flippers_open_ =
Ravago Jones5da06352022-03-04 20:26:24 -0800347 position->flipper_arm_right()->encoder() >= flipper_open_position;
348
349 if (flippers_open_) {
350 // Hold at kFlipperHoldVoltage
351 flipper_arms_voltage = constants::Values::kFlipperHoldVoltage();
352 } else {
353 // Open at kFlipperOpenVoltage
354 flipper_arms_voltage = constants::Values::kFlipperOpenVoltage();
355 }
356
357 if (!flippers_open_ &&
358 timestamp >
359 loading_timer_ + constants::Values::kFlipperOpeningTimeout()) {
360 // Reseat the ball and try again
361 state_ = SuperstructureState::LOADING;
362 loading_timer_ = timestamp;
363 reseating_in_catapult_ = true;
364 break;
365 }
366
367 const bool turret_near_goal =
368 turret_goal != nullptr &&
369 std::abs(turret_goal->unsafe_goal() - turret_.position()) <
370 kTurretGoalThreshold;
Henry Speiser28288cc2022-03-09 22:59:24 -0800371 const bool collided = collision_avoidance_.IsCollided(
372 {.intake_front_position = intake_front_.estimated_position(),
373 .intake_back_position = intake_back_.estimated_position(),
374 .turret_position = turret_.estimated_position(),
375 .shooting = true});
Ravago Jones5da06352022-03-04 20:26:24 -0800376
Henry Speiser28288cc2022-03-09 22:59:24 -0800377 // If the turret reached the aiming goal and the catapult is safe to move
378 // up, fire!
379 if (flippers_open_ && turret_near_goal && !collided) {
Ravago Jones5da06352022-03-04 20:26:24 -0800380 fire_ = true;
381 }
382
383 // If we started firing and the flippers closed a bit, estop to prevent
384 // damage
385 if (fire_ && !flippers_open_) {
386 catapult_.Estop();
387 }
388
389 const bool near_return_position =
390 (unsafe_goal != nullptr && unsafe_goal->has_catapult() &&
391 unsafe_goal->catapult()->has_return_position() &&
392 std::abs(unsafe_goal->catapult()->return_position()->unsafe_goal() -
393 catapult_.estimated_position()) < kCatapultGoalThreshold);
394
395 // Once the shot is complete and the catapult is back to its return
396 // position, go back to IDLE
397 if (catapult_.shot_count() > prev_shot_count_ && near_return_position) {
398 prev_shot_count_ = catapult_.shot_count();
399 fire_ = false;
400 state_ = SuperstructureState::IDLE;
401 }
402
403 break;
404 }
405 }
406
407 collision_avoidance_.UpdateGoal(
408 {.intake_front_position = intake_front_.estimated_position(),
409 .intake_back_position = intake_back_.estimated_position(),
Henry Speiser28288cc2022-03-09 22:59:24 -0800410 .turret_position = turret_.estimated_position(),
411 .shooting = state_ == SuperstructureState::SHOOTING},
Ravago Jones5da06352022-03-04 20:26:24 -0800412 turret_goal);
413
414 turret_.set_min_position(collision_avoidance_.min_turret_goal());
415 turret_.set_max_position(collision_avoidance_.max_turret_goal());
416 intake_front_.set_min_position(collision_avoidance_.min_intake_front_goal());
417 intake_front_.set_max_position(collision_avoidance_.max_intake_front_goal());
418 intake_back_.set_min_position(collision_avoidance_.min_intake_back_goal());
419 intake_back_.set_max_position(collision_avoidance_.max_intake_back_goal());
420
James Kuszmaul84083f42022-02-27 17:24:38 -0800421 const flatbuffers::Offset<AimerStatus> aimer_offset =
422 aimer_.PopulateStatus(status->fbb());
423
Milind Upadhyay5dfabef2022-03-06 14:08:15 -0800424 // Disable the catapult if we want to restart to prevent damage with
425 // flippers
Austin Schuh39f26f62022-02-24 21:34:46 -0800426 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Austin Schuh97410d72022-03-12 15:37:23 -0800427 catapult_status_offset = catapult_.Iterate(
Ravago Jones3283ce02022-03-09 19:31:29 -0800428 catapult_goal, position, robot_state().voltage_battery(),
Austin Schuh97410d72022-03-12 15:37:23 -0800429 output != nullptr && !catapult_.estopped()
430 ? &(output_struct.catapult_voltage)
431 : nullptr,
432 fire_, status->fbb());
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800433
Ravago Jones5da06352022-03-04 20:26:24 -0800434 const flatbuffers::Offset<RelativeEncoderProfiledJointStatus>
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800435 climber_status_offset = climber_.Iterate(
436 unsafe_goal != nullptr ? unsafe_goal->climber() : nullptr,
Austin Schuh7f120362022-03-05 17:10:11 -0800437 position->climber(),
438 output != nullptr ? &output_struct.climber_voltage : nullptr,
439 status->fbb());
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800440
Ravago Jones5da06352022-03-04 20:26:24 -0800441 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800442 intake_status_offset_front = intake_front_.Iterate(
443 unsafe_goal != nullptr ? unsafe_goal->intake_front() : nullptr,
Austin Schuh7f120362022-03-05 17:10:11 -0800444 position->intake_front(),
445 output != nullptr ? &output_struct.intake_voltage_front : nullptr,
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800446 status->fbb());
447
Ravago Jones5da06352022-03-04 20:26:24 -0800448 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800449 intake_status_offset_back = intake_back_.Iterate(
450 unsafe_goal != nullptr ? unsafe_goal->intake_back() : nullptr,
Austin Schuh7f120362022-03-05 17:10:11 -0800451 position->intake_back(),
452 output != nullptr ? &output_struct.intake_voltage_back : nullptr,
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800453 status->fbb());
454
Ravago Jones5da06352022-03-04 20:26:24 -0800455 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Austin Schuh7f120362022-03-05 17:10:11 -0800456 turret_status_offset = turret_.Iterate(
457 turret_goal, position->turret(),
458 output != nullptr ? &output_struct.turret_voltage : nullptr,
459 status->fbb());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800460
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800461 if (output != nullptr) {
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800462 output_struct.roller_voltage_front = roller_speed_compensated_front;
463 output_struct.roller_voltage_back = roller_speed_compensated_back;
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800464 output_struct.transfer_roller_voltage_front = transfer_roller_speed_front;
465 output_struct.transfer_roller_voltage_back = transfer_roller_speed_back;
Ravago Jones5da06352022-03-04 20:26:24 -0800466 output_struct.flipper_arms_voltage = flipper_arms_voltage;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800467
milind-u086d7262022-01-19 20:44:18 -0800468 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
469 }
470
471 Status::Builder status_builder = status->MakeBuilder<Status>();
472
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800473 const bool zeroed = intake_front_.zeroed() && intake_back_.zeroed() &&
Ravago Jones5da06352022-03-04 20:26:24 -0800474 turret_.zeroed() && climber_.zeroed() &&
475 catapult_.zeroed();
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800476 const bool estopped = intake_front_.estopped() || intake_back_.estopped() ||
Ravago Jones5da06352022-03-04 20:26:24 -0800477 turret_.estopped() || climber_.estopped() ||
478 catapult_.estopped();
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800479
480 status_builder.add_zeroed(zeroed);
481 status_builder.add_estopped(estopped);
482
483 status_builder.add_intake_front(intake_status_offset_front);
484 status_builder.add_intake_back(intake_status_offset_back);
485 status_builder.add_turret(turret_status_offset);
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800486 status_builder.add_climber(climber_status_offset);
Ravago Jones5da06352022-03-04 20:26:24 -0800487
Austin Schuh39f26f62022-02-24 21:34:46 -0800488 status_builder.add_catapult(catapult_status_offset);
489 status_builder.add_solve_time(catapult_.solve_time());
Austin Schuh80fc2752022-02-25 13:33:56 -0800490 status_builder.add_shot_count(catapult_.shot_count());
Austin Schuh41472552022-03-13 18:09:41 -0700491 status_builder.add_mpc_horizon(catapult_.mpc_horizon());
Ravago Jones3283ce02022-03-09 19:31:29 -0800492 if (catapult_goal != nullptr) {
493 status_builder.add_shot_position(catapult_goal->shot_position());
494 status_builder.add_shot_velocity(catapult_goal->shot_velocity());
495 }
Ravago Jones5da06352022-03-04 20:26:24 -0800496
497 status_builder.add_flippers_open(flippers_open_);
498 status_builder.add_reseating_in_catapult(reseating_in_catapult_);
499 status_builder.add_fire(fire_);
500 status_builder.add_state(state_);
James Kuszmaul40526952022-03-13 15:56:38 -0700501 if (!front_intake_has_ball_ && !back_intake_has_ball_) {
502 status_builder.add_intake_state(IntakeState::NO_BALL);
503 } else if (front_intake_has_ball_ && back_intake_has_ball_) {
504 status_builder.add_intake_state(turret_intake_state_ ==
505 RequestedIntake::kFront
506 ? IntakeState::INTAKE_FRONT_BALL
507 : IntakeState::INTAKE_BACK_BALL);
508 } else {
509 status_builder.add_intake_state(front_intake_has_ball_
510 ? IntakeState::INTAKE_FRONT_BALL
511 : IntakeState::INTAKE_BACK_BALL);
512 }
513 status_builder.add_front_intake_has_ball(front_intake_has_ball_);
514 status_builder.add_back_intake_has_ball(back_intake_has_ball_);
milind-u086d7262022-01-19 20:44:18 -0800515
James Kuszmaul84083f42022-02-27 17:24:38 -0800516 status_builder.add_aimer(aimer_offset);
517
milind-u086d7262022-01-19 20:44:18 -0800518 (void)status->Send(status_builder.Finish());
519}
520
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800521double Superstructure::robot_velocity() const {
522 return (drivetrain_status_fetcher_.get() != nullptr
523 ? drivetrain_status_fetcher_->robot_speed()
524 : 0.0);
525}
526
milind-u086d7262022-01-19 20:44:18 -0800527} // namespace superstructure
528} // namespace control_loops
529} // namespace y2022