blob: d23e5895b7e49a01f5d08a809f9b5f442538e517 [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
James Kuszmaulb9ba9a52022-03-31 22:16:01 -07008DEFINE_bool(ignore_distance, false,
9 "If true, ignore distance when shooting and obay joystick_reader");
10
milind-u086d7262022-01-19 20:44:18 -080011namespace y2022 {
12namespace control_loops {
13namespace superstructure {
14
15using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
16using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
Henry Speiser55aa3ba2022-02-21 23:21:12 -080017using frc971::control_loops::RelativeEncoderProfiledJointStatus;
milind-u086d7262022-01-19 20:44:18 -080018
19Superstructure::Superstructure(::aos::EventLoop *event_loop,
Henry Speiser55aa3ba2022-02-21 23:21:12 -080020 std::shared_ptr<const constants::Values> values,
milind-u086d7262022-01-19 20:44:18 -080021 const ::std::string &name)
22 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080023 name),
Austin Schuh39f26f62022-02-24 21:34:46 -080024 values_(values),
25 climber_(values_->climber.subsystem_params),
26 intake_front_(values_->intake_front.subsystem_params),
27 intake_back_(values_->intake_back.subsystem_params),
28 turret_(values_->turret.subsystem_params),
Ravago Jones5da06352022-03-04 20:26:24 -080029 catapult_(*values_),
Henry Speiser55aa3ba2022-02-21 23:21:12 -080030 drivetrain_status_fetcher_(
31 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
Austin Schuh39f26f62022-02-24 21:34:46 -080032 "/drivetrain")),
Ravago Jones5da06352022-03-04 20:26:24 -080033 can_position_fetcher_(
James Kuszmaulb9ba9a52022-03-31 22:16:01 -070034 event_loop->MakeFetcher<CANPosition>("/superstructure")),
35 aimer_(values) {
milind-u086d7262022-01-19 20:44:18 -080036 event_loop->SetRuntimeRealtimePriority(30);
37}
38
39void Superstructure::RunIteration(const Goal *unsafe_goal,
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080040 const Position *position,
milind-u086d7262022-01-19 20:44:18 -080041 aos::Sender<Output>::Builder *output,
42 aos::Sender<Status>::Builder *status) {
43 if (WasReset()) {
44 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Henry Speiser55aa3ba2022-02-21 23:21:12 -080045 intake_front_.Reset();
46 intake_back_.Reset();
47 turret_.Reset();
48 climber_.Reset();
Austin Schuh39f26f62022-02-24 21:34:46 -080049 catapult_.Reset();
Henry Speiser55aa3ba2022-02-21 23:21:12 -080050 }
51
Ravago Jones5da06352022-03-04 20:26:24 -080052 OutputT output_struct;
Milind Upadhyay225156b2022-02-25 22:42:12 -080053
Ravago Jones5da06352022-03-04 20:26:24 -080054 aos::FlatbufferFixedAllocatorArray<
Austin Schuhfce2b602022-03-13 20:05:01 -070055 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
James Kuszmaul40526952022-03-13 15:56:38 -070056 turret_loading_goal_buffer;
Austin Schuhfce2b602022-03-13 20:05:01 -070057 aos::FlatbufferFixedAllocatorArray<CatapultGoal, 512> catapult_goal_buffer;
Ravago Jones5da06352022-03-04 20:26:24 -080058
59 const aos::monotonic_clock::time_point timestamp =
60 event_loop()->context().monotonic_event_time;
Milind Upadhyay225156b2022-02-25 22:42:12 -080061
Henry Speiser55aa3ba2022-02-21 23:21:12 -080062 drivetrain_status_fetcher_.Fetch();
63 const float velocity = robot_velocity();
64
James Kuszmaul84083f42022-02-27 17:24:38 -080065 const turret::Aimer::Goal *auto_aim_goal = nullptr;
66 if (drivetrain_status_fetcher_.get() != nullptr) {
67 aimer_.Update(drivetrain_status_fetcher_.get(),
68 turret::Aimer::ShotMode::kShootOnTheFly);
69 auto_aim_goal = aimer_.TurretGoal();
70 }
71
Ravago Jones5da06352022-03-04 20:26:24 -080072 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
73 *turret_goal = nullptr;
Ravago Jones3283ce02022-03-09 19:31:29 -080074 const CatapultGoal *catapult_goal = nullptr;
Ravago Jones5da06352022-03-04 20:26:24 -080075 double roller_speed_compensated_front = 0.0;
76 double roller_speed_compensated_back = 0.0;
Milind Upadhyay29dcc172022-04-02 19:21:30 -070077 double transfer_roller_speed = 0.0;
Ravago Jones5da06352022-03-04 20:26:24 -080078 double flipper_arms_voltage = 0.0;
James Kuszmaul40526952022-03-13 15:56:38 -070079 bool have_active_intake_request = false;
Henry Speiser55aa3ba2022-02-21 23:21:12 -080080
81 if (unsafe_goal != nullptr) {
82 roller_speed_compensated_front =
83 unsafe_goal->roller_speed_front() +
84 std::max(velocity * unsafe_goal->roller_speed_compensation(), 0.0);
85
86 roller_speed_compensated_back =
87 unsafe_goal->roller_speed_back() -
88 std::min(velocity * unsafe_goal->roller_speed_compensation(), 0.0);
89
Milind Upadhyay29dcc172022-04-02 19:21:30 -070090 transfer_roller_speed = unsafe_goal->transfer_roller_speed();
milind-u086d7262022-01-19 20:44:18 -080091
James Kuszmaul84083f42022-02-27 17:24:38 -080092 turret_goal =
93 unsafe_goal->auto_aim() ? auto_aim_goal : unsafe_goal->turret();
Ravago Jones3283ce02022-03-09 19:31:29 -080094
95 catapult_goal = unsafe_goal->catapult();
96
97 constants::Values::ShotParams shot_params;
98 const double distance_to_goal = aimer_.DistanceToGoal();
James Kuszmaulb9ba9a52022-03-31 22:16:01 -070099 if (!FLAGS_ignore_distance && unsafe_goal->auto_aim() &&
100 values_->shot_interpolation_table.GetInRange(distance_to_goal,
101 &shot_params)) {
Austin Schuhfce2b602022-03-13 20:05:01 -0700102 flatbuffers::FlatBufferBuilder *catapult_goal_fbb =
103 catapult_goal_buffer.fbb();
Ravago Jones3283ce02022-03-09 19:31:29 -0800104 std::optional<flatbuffers::Offset<
105 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal>>
106 return_position_offset;
107 if (unsafe_goal != nullptr && unsafe_goal->has_catapult() &&
108 unsafe_goal->catapult()->has_return_position()) {
Austin Schuhfce2b602022-03-13 20:05:01 -0700109 return_position_offset = {aos::CopyFlatBuffer(
110 unsafe_goal->catapult()->return_position(), catapult_goal_fbb)};
Ravago Jones3283ce02022-03-09 19:31:29 -0800111 }
Austin Schuhfce2b602022-03-13 20:05:01 -0700112 CatapultGoal::Builder builder(*catapult_goal_fbb);
Ravago Jones3283ce02022-03-09 19:31:29 -0800113 builder.add_shot_position(shot_params.shot_angle);
114 builder.add_shot_velocity(shot_params.shot_velocity);
115 if (return_position_offset.has_value()) {
116 builder.add_return_position(return_position_offset.value());
117 }
118 catapult_goal_buffer.Finish(builder.Finish());
119 catapult_goal = &catapult_goal_buffer.message();
120 }
James Kuszmaul40526952022-03-13 15:56:38 -0700121
122 if (unsafe_goal->has_turret_intake()) {
123 have_active_intake_request = true;
124 }
James Kuszmaul84083f42022-02-27 17:24:38 -0800125 }
Austin Schuh39f26f62022-02-24 21:34:46 -0800126
Milind Upadhyay803bbf02022-03-11 17:56:26 -0800127 // Superstructure state machine:
Ravago Jones5da06352022-03-04 20:26:24 -0800128 // 1. IDLE: Wait until an intake beambreak is triggerred, meaning that a ball
129 // is being intaked. This means that the transfer rollers have a ball. If
130 // we've been waiting here for too long without any beambreak triggered, the
131 // ball got lost, so reset.
132 // 2. TRANSFERRING: Until the turret reaches the loading position where the
133 // ball can be transferred into the catapult, wiggle the ball in place.
134 // Once the turret reaches the loading position, send the ball forward with
135 // the transfer rollers until the turret beambreak is triggered.
136 // If we have been in this state for too long, the ball probably got lost so
137 // reset back to IDLE.
138 // 3. LOADING: To load the ball into the catapult, put the flippers at the
139 // feeding speed. Wait for a timeout, and then wait until the ball has gone
140 // past the turret beambreak and the flippers have stopped moving, meaning
141 // that the ball is fully loaded in the catapult.
142 // 4. LOADED: Wait until the user asks us to fire to transition to the
143 // shooting stage. If asked to cancel the shot, reset back to the IDLE state.
144 // 5. SHOOTING: Open the flippers to get ready for the shot. If they don't
145 // open quickly enough, try reseating the ball and going back to the LOADING
146 // stage, which moves the flippers in the opposite direction first.
147 // Now, hold the flippers open and wait until the turret has reached its
148 // aiming goal. Once the turret is ready, tell the catapult to fire.
149 // If the flippers move back for some reason now, it could damage the
150 // catapult, so estop it. Otherwise, wait until the catapult shoots a ball and
151 // goes back to its return position. We have now finished the shot, so return
152 // to IDLE.
153
Milind Upadhyay803bbf02022-03-11 17:56:26 -0800154 // If we started off preloaded, skip to the loaded state.
155 // Make sure we weren't already there just in case.
156 if (unsafe_goal != nullptr && unsafe_goal->preloaded()) {
157 switch (state_) {
158 case SuperstructureState::IDLE:
159 case SuperstructureState::TRANSFERRING:
160 case SuperstructureState::LOADING:
161 state_ = SuperstructureState::LOADED;
162 loading_timer_ = timestamp;
163 break;
164 case SuperstructureState::LOADED:
165 case SuperstructureState::SHOOTING:
166 break;
167 }
168 }
169
James Kuszmaul40526952022-03-13 15:56:38 -0700170 if (position->intake_beambreak_front()) {
171 front_intake_has_ball_ = true;
172 front_intake_beambreak_timer_ = timestamp;
Ravago Jones5da06352022-03-04 20:26:24 -0800173 }
174
James Kuszmaul40526952022-03-13 15:56:38 -0700175 if (position->intake_beambreak_back()) {
176 back_intake_has_ball_ = true;
177 back_intake_beambreak_timer_ = timestamp;
Milind Upadhyay5dfabef2022-03-06 14:08:15 -0800178 }
179
James Kuszmaul40526952022-03-13 15:56:38 -0700180 // Check if we're either spitting of have lost the ball.
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700181 if ((transfer_roller_speed < 0.0 && front_intake_has_ball_) ||
James Kuszmaul40526952022-03-13 15:56:38 -0700182 timestamp >
183 front_intake_beambreak_timer_ + constants::Values::kBallLostTime()) {
184 front_intake_has_ball_ = false;
185 }
186
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700187 if ((transfer_roller_speed > 0.0 && back_intake_has_ball_) ||
James Kuszmaul40526952022-03-13 15:56:38 -0700188 timestamp >
189 back_intake_beambreak_timer_ + constants::Values::kBallLostTime()) {
190 back_intake_has_ball_ = false;
191 }
192
193 // Wiggle transfer rollers: send the ball back and forth while waiting
194 // for the turret or waiting for another shot to be completed
195 const double wiggle_voltage =
196 constants::Values::kTransferRollerWiggleVoltage();
197 if (front_intake_has_ball_) {
Ravago Jones5da06352022-03-04 20:26:24 -0800198 roller_speed_compensated_front = 0.0;
James Kuszmaul40526952022-03-13 15:56:38 -0700199 if (position->intake_beambreak_front()) {
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700200 transfer_roller_speed = -wiggle_voltage;
Ravago Jones5da06352022-03-04 20:26:24 -0800201 } else {
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700202 transfer_roller_speed = wiggle_voltage;
Ravago Jones5da06352022-03-04 20:26:24 -0800203 }
204 }
205
James Kuszmaul40526952022-03-13 15:56:38 -0700206 if (back_intake_has_ball_) {
207 roller_speed_compensated_back = 0.0;
208 if (position->intake_beambreak_back()) {
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700209 transfer_roller_speed = wiggle_voltage;
James Kuszmaul40526952022-03-13 15:56:38 -0700210 } else {
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700211 transfer_roller_speed = -wiggle_voltage;
Milind Upadhyay918ecf02022-03-12 15:55:46 -0800212 }
213 }
214
James Kuszmaul40526952022-03-13 15:56:38 -0700215 // Create the goal message for putting the turret in its loading position.
216 // This will then get used in the state machine for the states (IDLE and
217 // TRANSFERRING) that use it.
218 double turret_loading_position =
219 (turret_intake_state_ == RequestedIntake::kFront
220 ? constants::Values::kTurretFrontIntakePos()
221 : constants::Values::kTurretBackIntakePos());
222 // Turn to the loading position as close to the current position as
223 // possible.
224 turret_loading_position =
225 turret_.estimated_position() +
226 aos::math::NormalizeAngle(turret_loading_position -
227 turret_.estimated_position());
228 // if out of range, reset back to within +/- pi of zero.
229 if (turret_loading_position > constants::Values::kTurretRange().upper ||
230 turret_loading_position < constants::Values::kTurretRange().lower) {
Milind Upadhyay918ecf02022-03-12 15:55:46 -0800231 turret_loading_position =
James Kuszmaul2a59cf02022-03-17 11:02:02 -0700232 frc971::zeroing::Wrap(constants::Values::kTurretRange().middle_soft(),
James Kuszmaul40526952022-03-13 15:56:38 -0700233 turret_loading_position, 2.0 * M_PI);
Milind Upadhyay918ecf02022-03-12 15:55:46 -0800234 }
235
James Kuszmaul40526952022-03-13 15:56:38 -0700236 turret_loading_goal_buffer.Finish(
237 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
238 *turret_loading_goal_buffer.fbb(), turret_loading_position));
239
Henry Speiser77747b72022-03-06 17:18:29 -0800240 const bool turret_near_goal =
241 turret_goal != nullptr &&
242 std::abs(turret_goal->unsafe_goal() - turret_.position()) <
243 kTurretGoalThreshold;
244 const bool collided = collision_avoidance_.IsCollided(
245 {.intake_front_position = intake_front_.estimated_position(),
246 .intake_back_position = intake_back_.estimated_position(),
247 .turret_position = turret_.estimated_position(),
248 .shooting = true});
249
Henry Speisera9c49a22022-04-01 15:22:35 -0700250 // Dont shoot if the robot is moving faster than this
James Kuszmaulb9ba9a52022-03-31 22:16:01 -0700251 constexpr double kMaxShootSpeed = 2.7;
Henry Speisera9c49a22022-04-01 15:22:35 -0700252 const bool moving_too_fast = std::abs(robot_velocity()) > kMaxShootSpeed;
253
Ravago Jones5da06352022-03-04 20:26:24 -0800254 switch (state_) {
255 case SuperstructureState::IDLE: {
James Kuszmaul40526952022-03-13 15:56:38 -0700256 // Only change the turret's goal loading position when idle, to prevent us
257 // spinning the turret around when TRANSFERRING...
Milind Upadhyay90648532022-03-13 20:37:02 -0700258 if (have_active_intake_request) {
259 turret_intake_state_ = unsafe_goal->turret_intake();
260 }
James Kuszmaul40526952022-03-13 15:56:38 -0700261 if (front_intake_has_ball_ != back_intake_has_ball_) {
James Kuszmaul40526952022-03-13 15:56:38 -0700262 turret_intake_state_ = front_intake_has_ball_ ? RequestedIntake::kFront
263 : RequestedIntake::kBack;
264 }
265 // When IDLE with no specific intake button pressed, allow the goal
266 // message to override the intaking stuff.
Austin Schuh92908812022-03-27 14:14:05 -0700267 if (have_active_intake_request || (turret_goal == nullptr)) {
James Kuszmaul40526952022-03-13 15:56:38 -0700268 turret_goal = &turret_loading_goal_buffer.message();
Ravago Jones5da06352022-03-04 20:26:24 -0800269 }
270
James Kuszmaul40526952022-03-13 15:56:38 -0700271 if (!front_intake_has_ball_ && !back_intake_has_ball_) {
Austin Schuh92908812022-03-27 14:14:05 -0700272 last_shot_angle_ = std::nullopt;
Ravago Jones5da06352022-03-04 20:26:24 -0800273 break;
274 }
275
276 state_ = SuperstructureState::TRANSFERRING;
Ravago Jones5da06352022-03-04 20:26:24 -0800277 // Save the side the ball is on for later
278
279 break;
280 }
281 case SuperstructureState::TRANSFERRING: {
James Kuszmaul40526952022-03-13 15:56:38 -0700282 // If we've been transferring for too long, the ball probably got lost.
283 if ((turret_intake_state_ == RequestedIntake::kFront &&
284 !front_intake_has_ball_) ||
285 (turret_intake_state_ == RequestedIntake::kBack &&
286 !back_intake_has_ball_)) {
Ravago Jones5da06352022-03-04 20:26:24 -0800287 state_ = SuperstructureState::IDLE;
288 break;
289 }
290
James Kuszmaul40526952022-03-13 15:56:38 -0700291 turret_goal = &turret_loading_goal_buffer.message();
292
Ravago Jones5da06352022-03-04 20:26:24 -0800293 const bool turret_near_goal =
294 std::abs(turret_.estimated_position() - turret_loading_position) <
295 kTurretGoalThreshold;
296 if (!turret_near_goal) {
297 break; // Wait for turret to reach the chosen intake
298 }
299
300 // Transfer rollers and flipper arm belt on
James Kuszmaul40526952022-03-13 15:56:38 -0700301 if (turret_intake_state_ == RequestedIntake::kFront) {
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700302 transfer_roller_speed = constants::Values::kTransferRollerVoltage();
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800303 } else {
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700304 transfer_roller_speed = -constants::Values::kTransferRollerVoltage();
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800305 }
Ravago Jones5da06352022-03-04 20:26:24 -0800306 flipper_arms_voltage = constants::Values::kFlipperFeedVoltage();
307
308 // Ball is in catapult
309 if (position->turret_beambreak()) {
James Kuszmaul40526952022-03-13 15:56:38 -0700310 if (turret_intake_state_ == RequestedIntake::kFront) {
311 front_intake_has_ball_ = false;
312 } else {
313 back_intake_has_ball_ = false;
314 }
Ravago Jones5da06352022-03-04 20:26:24 -0800315 state_ = SuperstructureState::LOADING;
316 loading_timer_ = timestamp;
317 }
318 break;
319 }
320 case SuperstructureState::LOADING: {
321 flipper_arms_voltage = constants::Values::kFlipperFeedVoltage();
322
323 // Keep feeding for kExtraLoadingTime
324
Ravago Jones5da06352022-03-04 20:26:24 -0800325 // The ball should go past the turret beambreak to be loaded.
Milind Upadhyay5dfabef2022-03-06 14:08:15 -0800326 // If we got a CAN reading not too long ago, the flippers should have
327 // also stopped.
Milind Upadhyay47e0d032022-03-05 23:06:25 -0800328 if (position->turret_beambreak()) {
329 loading_timer_ = timestamp;
330 } else if (timestamp >
331 loading_timer_ + constants::Values::kExtraLoadingTime()) {
Ravago Jones5da06352022-03-04 20:26:24 -0800332 state_ = SuperstructureState::LOADED;
333 reseating_in_catapult_ = false;
334 }
335 break;
336 }
337 case SuperstructureState::LOADED: {
338 if (unsafe_goal != nullptr) {
Austin Schuh92908812022-03-27 14:14:05 -0700339 if (turret_goal == nullptr) {
340 if (last_shot_angle_) {
341 turret_loading_goal_buffer.mutable_message()->mutate_unsafe_goal(
342 *last_shot_angle_);
343 }
344 turret_goal = &turret_loading_goal_buffer.message();
345 }
Ravago Jones5da06352022-03-04 20:26:24 -0800346 if (unsafe_goal->cancel_shot()) {
347 // Cancel the shot process
348 state_ = SuperstructureState::IDLE;
349 } else if (unsafe_goal->fire()) {
350 // Start if we were asked to and the turret is at goal
351 state_ = SuperstructureState::SHOOTING;
352 prev_shot_count_ = catapult_.shot_count();
353
354 // Reset opening timeout
355 flipper_opening_start_time_ = timestamp;
356 }
357 }
358 break;
359 }
360 case SuperstructureState::SHOOTING: {
Austin Schuh92908812022-03-27 14:14:05 -0700361 if (turret_goal == nullptr) {
362 if (last_shot_angle_) {
363 turret_loading_goal_buffer.mutable_message()->mutate_unsafe_goal(
364 *last_shot_angle_);
365 }
366 turret_goal = &turret_loading_goal_buffer.message();
367 last_shot_angle_ = turret_goal->unsafe_goal();
368 } else {
369 last_shot_angle_ = std::nullopt;
370 }
371 const bool turret_near_goal =
372 turret_goal != nullptr &&
373 std::abs(turret_goal->unsafe_goal() - turret_.position()) <
374 kTurretGoalThreshold;
375
milind-uee3b0012022-03-14 21:19:57 -0700376 // Don't open the flippers until the turret's ready: give them as little
Henry Speisera9c49a22022-04-01 15:22:35 -0700377 // time to get bumped as possible. Or moving to fast.
378 if (!turret_near_goal || collided || moving_too_fast) {
milind-uee3b0012022-03-14 21:19:57 -0700379 break;
380 }
381
Ravago Jones5da06352022-03-04 20:26:24 -0800382 // Opening flipper arms could fail, wait until they are open using their
383 // potentiometers (the member below is just named encoder).
384 // Be a little more lenient if the flippers were already open in case of
385 // noise or collisions.
386 const double flipper_open_position =
387 (flippers_open_ ? constants::Values::kReseatFlipperPosition()
388 : constants::Values::kFlipperOpenPosition());
milind-u37dc40b2022-03-08 19:51:27 -0800389
390 // TODO(milind): add left arm back once it's fixed
Ravago Jones5da06352022-03-04 20:26:24 -0800391 flippers_open_ =
Ravago Jones5da06352022-03-04 20:26:24 -0800392 position->flipper_arm_right()->encoder() >= flipper_open_position;
393
394 if (flippers_open_) {
395 // Hold at kFlipperHoldVoltage
396 flipper_arms_voltage = constants::Values::kFlipperHoldVoltage();
397 } else {
398 // Open at kFlipperOpenVoltage
399 flipper_arms_voltage = constants::Values::kFlipperOpenVoltage();
400 }
401
milind-uee3b0012022-03-14 21:19:57 -0700402 // There are two possible failures for the flippers:
403 // 1. They never open on time
404 // 2. They opened and we started firing, but we got bumped or something
405 // and they went back.
406 // If the flippers didn't open in a reasonable amount of time, try
407 // reseating the ball and reversing them.
408 if (!fire_ && !flippers_open_ &&
Ravago Jones5da06352022-03-04 20:26:24 -0800409 timestamp >
410 loading_timer_ + constants::Values::kFlipperOpeningTimeout()) {
411 // Reseat the ball and try again
412 state_ = SuperstructureState::LOADING;
413 loading_timer_ = timestamp;
414 reseating_in_catapult_ = true;
415 break;
416 }
Ravago Jones5da06352022-03-04 20:26:24 -0800417
Henry Speiser28288cc2022-03-09 22:59:24 -0800418 // If the turret reached the aiming goal and the catapult is safe to move
419 // up, fire!
420 if (flippers_open_ && turret_near_goal && !collided) {
Ravago Jones5da06352022-03-04 20:26:24 -0800421 fire_ = true;
422 }
423
Ravago Jones5da06352022-03-04 20:26:24 -0800424 const bool near_return_position =
425 (unsafe_goal != nullptr && unsafe_goal->has_catapult() &&
426 unsafe_goal->catapult()->has_return_position() &&
427 std::abs(unsafe_goal->catapult()->return_position()->unsafe_goal() -
428 catapult_.estimated_position()) < kCatapultGoalThreshold);
429
430 // Once the shot is complete and the catapult is back to its return
431 // position, go back to IDLE
432 if (catapult_.shot_count() > prev_shot_count_ && near_return_position) {
433 prev_shot_count_ = catapult_.shot_count();
434 fire_ = false;
435 state_ = SuperstructureState::IDLE;
436 }
437
438 break;
439 }
440 }
441
442 collision_avoidance_.UpdateGoal(
443 {.intake_front_position = intake_front_.estimated_position(),
444 .intake_back_position = intake_back_.estimated_position(),
Henry Speiser28288cc2022-03-09 22:59:24 -0800445 .turret_position = turret_.estimated_position(),
446 .shooting = state_ == SuperstructureState::SHOOTING},
Ravago Jones5da06352022-03-04 20:26:24 -0800447 turret_goal);
448
449 turret_.set_min_position(collision_avoidance_.min_turret_goal());
450 turret_.set_max_position(collision_avoidance_.max_turret_goal());
451 intake_front_.set_min_position(collision_avoidance_.min_intake_front_goal());
452 intake_front_.set_max_position(collision_avoidance_.max_intake_front_goal());
453 intake_back_.set_min_position(collision_avoidance_.min_intake_back_goal());
454 intake_back_.set_max_position(collision_avoidance_.max_intake_back_goal());
455
James Kuszmaul84083f42022-02-27 17:24:38 -0800456 const flatbuffers::Offset<AimerStatus> aimer_offset =
457 aimer_.PopulateStatus(status->fbb());
458
Milind Upadhyay5dfabef2022-03-06 14:08:15 -0800459 // Disable the catapult if we want to restart to prevent damage with
460 // flippers
Austin Schuh39f26f62022-02-24 21:34:46 -0800461 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Austin Schuh97410d72022-03-12 15:37:23 -0800462 catapult_status_offset = catapult_.Iterate(
Ravago Jones3283ce02022-03-09 19:31:29 -0800463 catapult_goal, position, robot_state().voltage_battery(),
Austin Schuh97410d72022-03-12 15:37:23 -0800464 output != nullptr && !catapult_.estopped()
465 ? &(output_struct.catapult_voltage)
466 : nullptr,
467 fire_, status->fbb());
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800468
Ravago Jones5da06352022-03-04 20:26:24 -0800469 const flatbuffers::Offset<RelativeEncoderProfiledJointStatus>
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800470 climber_status_offset = climber_.Iterate(
471 unsafe_goal != nullptr ? unsafe_goal->climber() : nullptr,
Austin Schuh7f120362022-03-05 17:10:11 -0800472 position->climber(),
473 output != nullptr ? &output_struct.climber_voltage : nullptr,
474 status->fbb());
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800475
Ravago Jones5da06352022-03-04 20:26:24 -0800476 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800477 intake_status_offset_front = intake_front_.Iterate(
478 unsafe_goal != nullptr ? unsafe_goal->intake_front() : nullptr,
Austin Schuh7f120362022-03-05 17:10:11 -0800479 position->intake_front(),
480 output != nullptr ? &output_struct.intake_voltage_front : nullptr,
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800481 status->fbb());
482
Ravago Jones5da06352022-03-04 20:26:24 -0800483 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800484 intake_status_offset_back = intake_back_.Iterate(
485 unsafe_goal != nullptr ? unsafe_goal->intake_back() : nullptr,
Austin Schuh7f120362022-03-05 17:10:11 -0800486 position->intake_back(),
487 output != nullptr ? &output_struct.intake_voltage_back : nullptr,
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800488 status->fbb());
489
Ravago Jones5da06352022-03-04 20:26:24 -0800490 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Austin Schuh7f120362022-03-05 17:10:11 -0800491 turret_status_offset = turret_.Iterate(
492 turret_goal, position->turret(),
493 output != nullptr ? &output_struct.turret_voltage : nullptr,
494 status->fbb());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800495
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800496 if (output != nullptr) {
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800497 output_struct.roller_voltage_front = roller_speed_compensated_front;
498 output_struct.roller_voltage_back = roller_speed_compensated_back;
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700499 output_struct.transfer_roller_voltage = transfer_roller_speed;
Ravago Jones5da06352022-03-04 20:26:24 -0800500 output_struct.flipper_arms_voltage = flipper_arms_voltage;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800501
milind-u086d7262022-01-19 20:44:18 -0800502 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
503 }
504
505 Status::Builder status_builder = status->MakeBuilder<Status>();
506
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800507 const bool zeroed = intake_front_.zeroed() && intake_back_.zeroed() &&
Ravago Jones5da06352022-03-04 20:26:24 -0800508 turret_.zeroed() && climber_.zeroed() &&
509 catapult_.zeroed();
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800510 const bool estopped = intake_front_.estopped() || intake_back_.estopped() ||
Ravago Jones5da06352022-03-04 20:26:24 -0800511 turret_.estopped() || climber_.estopped() ||
512 catapult_.estopped();
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800513
514 status_builder.add_zeroed(zeroed);
515 status_builder.add_estopped(estopped);
516
517 status_builder.add_intake_front(intake_status_offset_front);
518 status_builder.add_intake_back(intake_status_offset_back);
519 status_builder.add_turret(turret_status_offset);
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800520 status_builder.add_climber(climber_status_offset);
Ravago Jones5da06352022-03-04 20:26:24 -0800521
Austin Schuh39f26f62022-02-24 21:34:46 -0800522 status_builder.add_catapult(catapult_status_offset);
523 status_builder.add_solve_time(catapult_.solve_time());
Austin Schuh80fc2752022-02-25 13:33:56 -0800524 status_builder.add_shot_count(catapult_.shot_count());
Austin Schuh41472552022-03-13 18:09:41 -0700525 status_builder.add_mpc_horizon(catapult_.mpc_horizon());
Ravago Jones3283ce02022-03-09 19:31:29 -0800526 if (catapult_goal != nullptr) {
527 status_builder.add_shot_position(catapult_goal->shot_position());
528 status_builder.add_shot_velocity(catapult_goal->shot_velocity());
529 }
Ravago Jones5da06352022-03-04 20:26:24 -0800530
531 status_builder.add_flippers_open(flippers_open_);
532 status_builder.add_reseating_in_catapult(reseating_in_catapult_);
533 status_builder.add_fire(fire_);
Henry Speisera9c49a22022-04-01 15:22:35 -0700534 status_builder.add_moving_too_fast(moving_too_fast);
Henry Speiser77747b72022-03-06 17:18:29 -0800535 status_builder.add_ready_to_fire(state_ == SuperstructureState::LOADED &&
536 turret_near_goal && !collided);
Ravago Jones5da06352022-03-04 20:26:24 -0800537 status_builder.add_state(state_);
James Kuszmaul40526952022-03-13 15:56:38 -0700538 if (!front_intake_has_ball_ && !back_intake_has_ball_) {
539 status_builder.add_intake_state(IntakeState::NO_BALL);
540 } else if (front_intake_has_ball_ && back_intake_has_ball_) {
541 status_builder.add_intake_state(turret_intake_state_ ==
542 RequestedIntake::kFront
543 ? IntakeState::INTAKE_FRONT_BALL
544 : IntakeState::INTAKE_BACK_BALL);
545 } else {
546 status_builder.add_intake_state(front_intake_has_ball_
547 ? IntakeState::INTAKE_FRONT_BALL
548 : IntakeState::INTAKE_BACK_BALL);
549 }
550 status_builder.add_front_intake_has_ball(front_intake_has_ball_);
551 status_builder.add_back_intake_has_ball(back_intake_has_ball_);
milind-u086d7262022-01-19 20:44:18 -0800552
James Kuszmaul84083f42022-02-27 17:24:38 -0800553 status_builder.add_aimer(aimer_offset);
554
milind-u086d7262022-01-19 20:44:18 -0800555 (void)status->Send(status_builder.Finish());
556}
557
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800558double Superstructure::robot_velocity() const {
559 return (drivetrain_status_fetcher_.get() != nullptr
560 ? drivetrain_status_fetcher_->robot_speed()
561 : 0.0);
562}
563
milind-u086d7262022-01-19 20:44:18 -0800564} // namespace superstructure
565} // namespace control_loops
566} // namespace y2022