milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 1 | #include "y2022/control_loops/superstructure/superstructure.h" |
| 2 | |
| 3 | #include "aos/events/event_loop.h" |
Ravago Jones | 3283ce0 | 2022-03-09 19:31:29 -0800 | [diff] [blame] | 4 | #include "aos/flatbuffer_merge.h" |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 5 | #include "frc971/zeroing/wrap.h" |
Milind Upadhyay | 225156b | 2022-02-25 22:42:12 -0800 | [diff] [blame] | 6 | #include "y2022/control_loops/superstructure/collision_avoidance.h" |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 7 | |
James Kuszmaul | b9ba9a5 | 2022-03-31 22:16:01 -0700 | [diff] [blame] | 8 | DEFINE_bool(ignore_distance, false, |
| 9 | "If true, ignore distance when shooting and obay joystick_reader"); |
| 10 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 11 | namespace y2022 { |
| 12 | namespace control_loops { |
| 13 | namespace superstructure { |
| 14 | |
| 15 | using frc971::control_loops::AbsoluteEncoderProfiledJointStatus; |
| 16 | using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus; |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 17 | using frc971::control_loops::RelativeEncoderProfiledJointStatus; |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 18 | |
| 19 | Superstructure::Superstructure(::aos::EventLoop *event_loop, |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 20 | std::shared_ptr<const constants::Values> values, |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 21 | const ::std::string &name) |
| 22 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 23 | name), |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 24 | 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 Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 29 | catapult_(*values_), |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 30 | drivetrain_status_fetcher_( |
| 31 | event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>( |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 32 | "/drivetrain")), |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 33 | can_position_fetcher_( |
James Kuszmaul | b9ba9a5 | 2022-03-31 22:16:01 -0700 | [diff] [blame] | 34 | event_loop->MakeFetcher<CANPosition>("/superstructure")), |
Ravago Jones | d51af7a | 2022-03-26 21:44:20 -0700 | [diff] [blame] | 35 | joystick_state_fetcher_( |
| 36 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
Milind Upadhyay | e9075d1 | 2022-04-12 22:45:16 -0700 | [diff] [blame] | 37 | ball_color_fetcher_( |
| 38 | event_loop->MakeFetcher<y2022::vision::BallColor>("/superstructure")), |
James Kuszmaul | b9ba9a5 | 2022-03-31 22:16:01 -0700 | [diff] [blame] | 39 | aimer_(values) { |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 40 | event_loop->SetRuntimeRealtimePriority(30); |
| 41 | } |
| 42 | |
| 43 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 44 | const Position *position, |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 45 | aos::Sender<Output>::Builder *output, |
| 46 | aos::Sender<Status>::Builder *status) { |
| 47 | if (WasReset()) { |
| 48 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 49 | intake_front_.Reset(); |
| 50 | intake_back_.Reset(); |
| 51 | turret_.Reset(); |
| 52 | climber_.Reset(); |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 53 | catapult_.Reset(); |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 56 | OutputT output_struct; |
Milind Upadhyay | 225156b | 2022-02-25 22:42:12 -0800 | [diff] [blame] | 57 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 58 | aos::FlatbufferFixedAllocatorArray< |
Austin Schuh | fce2b60 | 2022-03-13 20:05:01 -0700 | [diff] [blame] | 59 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512> |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 60 | turret_loading_goal_buffer; |
Austin Schuh | fce2b60 | 2022-03-13 20:05:01 -0700 | [diff] [blame] | 61 | aos::FlatbufferFixedAllocatorArray<CatapultGoal, 512> catapult_goal_buffer; |
Ravago Jones | d51af7a | 2022-03-26 21:44:20 -0700 | [diff] [blame] | 62 | aos::FlatbufferFixedAllocatorArray<CatapultGoal, 512> |
| 63 | catapult_discarding_goal_buffer; |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 64 | |
| 65 | const aos::monotonic_clock::time_point timestamp = |
| 66 | event_loop()->context().monotonic_event_time; |
Milind Upadhyay | 225156b | 2022-02-25 22:42:12 -0800 | [diff] [blame] | 67 | |
Ravago Jones | d51af7a | 2022-03-26 21:44:20 -0700 | [diff] [blame] | 68 | if (joystick_state_fetcher_.Fetch() && |
| 69 | joystick_state_fetcher_->has_alliance()) { |
| 70 | alliance_ = joystick_state_fetcher_->alliance(); |
| 71 | } |
| 72 | |
| 73 | if (ball_color_fetcher_.Fetch() && ball_color_fetcher_->has_ball_color()) { |
| 74 | ball_color_ = ball_color_fetcher_->ball_color(); |
| 75 | } |
| 76 | |
| 77 | if (alliance_ != aos::Alliance::kInvalid && |
| 78 | ball_color_ != aos::Alliance::kInvalid && alliance_ != ball_color_) { |
| 79 | switch (state_) { |
| 80 | case SuperstructureState::IDLE: |
| 81 | break; |
| 82 | case SuperstructureState::TRANSFERRING: |
| 83 | break; |
| 84 | case SuperstructureState::LOADING: |
| 85 | break; |
| 86 | case SuperstructureState::LOADED: |
| 87 | discarding_ball_ = true; |
| 88 | break; |
| 89 | case SuperstructureState::SHOOTING: |
| 90 | if (!fire_) { |
| 91 | // we can still tell it not to shoot into the hub |
| 92 | // and change the turret and catapult goals |
| 93 | discarding_ball_ = true; |
| 94 | } |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 99 | drivetrain_status_fetcher_.Fetch(); |
| 100 | const float velocity = robot_velocity(); |
| 101 | |
James Kuszmaul | 84083f4 | 2022-02-27 17:24:38 -0800 | [diff] [blame] | 102 | const turret::Aimer::Goal *auto_aim_goal = nullptr; |
| 103 | if (drivetrain_status_fetcher_.get() != nullptr) { |
| 104 | aimer_.Update(drivetrain_status_fetcher_.get(), |
| 105 | turret::Aimer::ShotMode::kShootOnTheFly); |
| 106 | auto_aim_goal = aimer_.TurretGoal(); |
| 107 | } |
| 108 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 109 | const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal |
| 110 | *turret_goal = nullptr; |
Ravago Jones | 3283ce0 | 2022-03-09 19:31:29 -0800 | [diff] [blame] | 111 | const CatapultGoal *catapult_goal = nullptr; |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 112 | double roller_speed_compensated_front = 0.0; |
| 113 | double roller_speed_compensated_back = 0.0; |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 114 | double transfer_roller_speed = 0.0; |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 115 | double flipper_arms_voltage = 0.0; |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 116 | bool have_active_intake_request = false; |
milind-u | 6e7d8d4 | 2022-04-06 18:30:43 -0700 | [diff] [blame] | 117 | bool climber_servo = false; |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 118 | |
| 119 | if (unsafe_goal != nullptr) { |
| 120 | roller_speed_compensated_front = |
| 121 | unsafe_goal->roller_speed_front() + |
| 122 | std::max(velocity * unsafe_goal->roller_speed_compensation(), 0.0); |
| 123 | |
| 124 | roller_speed_compensated_back = |
| 125 | unsafe_goal->roller_speed_back() - |
| 126 | std::min(velocity * unsafe_goal->roller_speed_compensation(), 0.0); |
| 127 | |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 128 | transfer_roller_speed = unsafe_goal->transfer_roller_speed(); |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 129 | |
milind-u | 6e7d8d4 | 2022-04-06 18:30:43 -0700 | [diff] [blame] | 130 | climber_servo = unsafe_goal->climber_servo(); |
| 131 | |
Ravago Jones | d51af7a | 2022-03-26 21:44:20 -0700 | [diff] [blame] | 132 | turret_goal = unsafe_goal->auto_aim() && !discarding_ball_ |
| 133 | ? auto_aim_goal |
| 134 | : unsafe_goal->turret(); |
Ravago Jones | 3283ce0 | 2022-03-09 19:31:29 -0800 | [diff] [blame] | 135 | |
| 136 | catapult_goal = unsafe_goal->catapult(); |
| 137 | |
| 138 | constants::Values::ShotParams shot_params; |
| 139 | const double distance_to_goal = aimer_.DistanceToGoal(); |
James Kuszmaul | b9ba9a5 | 2022-03-31 22:16:01 -0700 | [diff] [blame] | 140 | if (!FLAGS_ignore_distance && unsafe_goal->auto_aim() && |
| 141 | values_->shot_interpolation_table.GetInRange(distance_to_goal, |
| 142 | &shot_params)) { |
Austin Schuh | fce2b60 | 2022-03-13 20:05:01 -0700 | [diff] [blame] | 143 | flatbuffers::FlatBufferBuilder *catapult_goal_fbb = |
| 144 | catapult_goal_buffer.fbb(); |
Ravago Jones | 3283ce0 | 2022-03-09 19:31:29 -0800 | [diff] [blame] | 145 | std::optional<flatbuffers::Offset< |
| 146 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal>> |
| 147 | return_position_offset; |
Ravago Jones | d51af7a | 2022-03-26 21:44:20 -0700 | [diff] [blame] | 148 | if (unsafe_goal->has_catapult() && |
Ravago Jones | 3283ce0 | 2022-03-09 19:31:29 -0800 | [diff] [blame] | 149 | unsafe_goal->catapult()->has_return_position()) { |
Austin Schuh | fce2b60 | 2022-03-13 20:05:01 -0700 | [diff] [blame] | 150 | return_position_offset = {aos::CopyFlatBuffer( |
| 151 | unsafe_goal->catapult()->return_position(), catapult_goal_fbb)}; |
Ravago Jones | 3283ce0 | 2022-03-09 19:31:29 -0800 | [diff] [blame] | 152 | } |
Austin Schuh | fce2b60 | 2022-03-13 20:05:01 -0700 | [diff] [blame] | 153 | CatapultGoal::Builder builder(*catapult_goal_fbb); |
Ravago Jones | 3283ce0 | 2022-03-09 19:31:29 -0800 | [diff] [blame] | 154 | builder.add_shot_position(shot_params.shot_angle); |
| 155 | builder.add_shot_velocity(shot_params.shot_velocity); |
| 156 | if (return_position_offset.has_value()) { |
| 157 | builder.add_return_position(return_position_offset.value()); |
| 158 | } |
| 159 | catapult_goal_buffer.Finish(builder.Finish()); |
| 160 | catapult_goal = &catapult_goal_buffer.message(); |
| 161 | } |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 162 | |
Ravago Jones | d51af7a | 2022-03-26 21:44:20 -0700 | [diff] [blame] | 163 | if (discarding_ball_) { |
| 164 | flatbuffers::FlatBufferBuilder *catapult_goal_fbb = |
| 165 | catapult_discarding_goal_buffer.fbb(); |
| 166 | std::optional<flatbuffers::Offset< |
| 167 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal>> |
| 168 | return_position_offset; |
| 169 | if (unsafe_goal->has_catapult() && |
| 170 | unsafe_goal->catapult()->has_return_position()) { |
| 171 | return_position_offset = {aos::CopyFlatBuffer( |
| 172 | unsafe_goal->catapult()->return_position(), catapult_goal_fbb)}; |
| 173 | } |
| 174 | CatapultGoal::Builder builder(*catapult_goal_fbb); |
| 175 | builder.add_shot_position(kDiscardingPosition); |
| 176 | builder.add_shot_velocity(kDiscardingVelocity); |
| 177 | if (return_position_offset.has_value()) { |
| 178 | builder.add_return_position(return_position_offset.value()); |
| 179 | } |
| 180 | catapult_discarding_goal_buffer.Finish(builder.Finish()); |
| 181 | catapult_goal = &catapult_discarding_goal_buffer.message(); |
| 182 | } |
| 183 | |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 184 | if (unsafe_goal->has_turret_intake()) { |
| 185 | have_active_intake_request = true; |
| 186 | } |
James Kuszmaul | 84083f4 | 2022-02-27 17:24:38 -0800 | [diff] [blame] | 187 | } |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 188 | |
Milind Upadhyay | 803bbf0 | 2022-03-11 17:56:26 -0800 | [diff] [blame] | 189 | // Superstructure state machine: |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 190 | // 1. IDLE: Wait until an intake beambreak is triggerred, meaning that a ball |
| 191 | // is being intaked. This means that the transfer rollers have a ball. If |
| 192 | // we've been waiting here for too long without any beambreak triggered, the |
| 193 | // ball got lost, so reset. |
| 194 | // 2. TRANSFERRING: Until the turret reaches the loading position where the |
| 195 | // ball can be transferred into the catapult, wiggle the ball in place. |
| 196 | // Once the turret reaches the loading position, send the ball forward with |
| 197 | // the transfer rollers until the turret beambreak is triggered. |
| 198 | // If we have been in this state for too long, the ball probably got lost so |
| 199 | // reset back to IDLE. |
| 200 | // 3. LOADING: To load the ball into the catapult, put the flippers at the |
| 201 | // feeding speed. Wait for a timeout, and then wait until the ball has gone |
| 202 | // past the turret beambreak and the flippers have stopped moving, meaning |
| 203 | // that the ball is fully loaded in the catapult. |
| 204 | // 4. LOADED: Wait until the user asks us to fire to transition to the |
| 205 | // shooting stage. If asked to cancel the shot, reset back to the IDLE state. |
| 206 | // 5. SHOOTING: Open the flippers to get ready for the shot. If they don't |
| 207 | // open quickly enough, try reseating the ball and going back to the LOADING |
| 208 | // stage, which moves the flippers in the opposite direction first. |
| 209 | // Now, hold the flippers open and wait until the turret has reached its |
| 210 | // aiming goal. Once the turret is ready, tell the catapult to fire. |
| 211 | // If the flippers move back for some reason now, it could damage the |
| 212 | // catapult, so estop it. Otherwise, wait until the catapult shoots a ball and |
| 213 | // goes back to its return position. We have now finished the shot, so return |
| 214 | // to IDLE. |
| 215 | |
Milind Upadhyay | 803bbf0 | 2022-03-11 17:56:26 -0800 | [diff] [blame] | 216 | // If we started off preloaded, skip to the loaded state. |
| 217 | // Make sure we weren't already there just in case. |
| 218 | if (unsafe_goal != nullptr && unsafe_goal->preloaded()) { |
| 219 | switch (state_) { |
| 220 | case SuperstructureState::IDLE: |
| 221 | case SuperstructureState::TRANSFERRING: |
| 222 | case SuperstructureState::LOADING: |
| 223 | state_ = SuperstructureState::LOADED; |
| 224 | loading_timer_ = timestamp; |
| 225 | break; |
| 226 | case SuperstructureState::LOADED: |
| 227 | case SuperstructureState::SHOOTING: |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 232 | if (position->intake_beambreak_front()) { |
| 233 | front_intake_has_ball_ = true; |
| 234 | front_intake_beambreak_timer_ = timestamp; |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 235 | } |
| 236 | |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 237 | if (position->intake_beambreak_back()) { |
| 238 | back_intake_has_ball_ = true; |
| 239 | back_intake_beambreak_timer_ = timestamp; |
Milind Upadhyay | 5dfabef | 2022-03-06 14:08:15 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Ravago Jones | f70107e | 2022-04-12 15:05:56 -0700 | [diff] [blame] | 242 | // Check if we're either spitting or have lost the ball. |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 243 | if ((transfer_roller_speed < 0.0 && front_intake_has_ball_) || |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 244 | timestamp > |
| 245 | front_intake_beambreak_timer_ + constants::Values::kBallLostTime()) { |
| 246 | front_intake_has_ball_ = false; |
| 247 | } |
| 248 | |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 249 | if ((transfer_roller_speed > 0.0 && back_intake_has_ball_) || |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 250 | timestamp > |
| 251 | back_intake_beambreak_timer_ + constants::Values::kBallLostTime()) { |
| 252 | back_intake_has_ball_ = false; |
| 253 | } |
| 254 | |
| 255 | // Wiggle transfer rollers: send the ball back and forth while waiting |
| 256 | // for the turret or waiting for another shot to be completed |
| 257 | const double wiggle_voltage = |
| 258 | constants::Values::kTransferRollerWiggleVoltage(); |
| 259 | if (front_intake_has_ball_) { |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 260 | roller_speed_compensated_front = 0.0; |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 261 | if (position->intake_beambreak_front()) { |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 262 | transfer_roller_speed = -wiggle_voltage; |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 263 | } else { |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 264 | transfer_roller_speed = wiggle_voltage; |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 268 | if (back_intake_has_ball_) { |
| 269 | roller_speed_compensated_back = 0.0; |
| 270 | if (position->intake_beambreak_back()) { |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 271 | transfer_roller_speed = wiggle_voltage; |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 272 | } else { |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 273 | transfer_roller_speed = -wiggle_voltage; |
Milind Upadhyay | 918ecf0 | 2022-03-12 15:55:46 -0800 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 277 | // Create the goal message for putting the turret in its loading position. |
| 278 | // This will then get used in the state machine for the states (IDLE and |
| 279 | // TRANSFERRING) that use it. |
| 280 | double turret_loading_position = |
| 281 | (turret_intake_state_ == RequestedIntake::kFront |
| 282 | ? constants::Values::kTurretFrontIntakePos() |
| 283 | : constants::Values::kTurretBackIntakePos()); |
Milind Upadhyay | 27d16d3 | 2022-04-16 13:41:45 -0700 | [diff] [blame^] | 284 | // Turn to the loading position as close to the middle of the range as |
| 285 | // possible. Do the unwraping before we have a ball so we don't have to unwrap |
| 286 | // to shoot. |
| 287 | turret_loading_position = frc971::zeroing::Wrap( |
| 288 | values_->turret_range.middle_soft(), turret_loading_position, 2.0 * M_PI); |
Milind Upadhyay | 918ecf0 | 2022-03-12 15:55:46 -0800 | [diff] [blame] | 289 | |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 290 | turret_loading_goal_buffer.Finish( |
| 291 | frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 292 | *turret_loading_goal_buffer.fbb(), turret_loading_position)); |
| 293 | |
milind-u | c63d094 | 2022-04-15 12:07:42 -0700 | [diff] [blame] | 294 | const bool catapult_near_return_position = |
| 295 | (unsafe_goal != nullptr && unsafe_goal->has_catapult() && |
| 296 | unsafe_goal->catapult()->has_return_position() && |
| 297 | std::abs(unsafe_goal->catapult()->return_position()->unsafe_goal() - |
| 298 | catapult_.estimated_position()) < kCatapultGoalThreshold); |
| 299 | |
Henry Speiser | 77747b7 | 2022-03-06 17:18:29 -0800 | [diff] [blame] | 300 | const bool turret_near_goal = |
| 301 | turret_goal != nullptr && |
| 302 | std::abs(turret_goal->unsafe_goal() - turret_.position()) < |
| 303 | kTurretGoalThreshold; |
| 304 | const bool collided = collision_avoidance_.IsCollided( |
| 305 | {.intake_front_position = intake_front_.estimated_position(), |
| 306 | .intake_back_position = intake_back_.estimated_position(), |
| 307 | .turret_position = turret_.estimated_position(), |
| 308 | .shooting = true}); |
| 309 | |
Henry Speiser | a9c49a2 | 2022-04-01 15:22:35 -0700 | [diff] [blame] | 310 | // Dont shoot if the robot is moving faster than this |
James Kuszmaul | b9ba9a5 | 2022-03-31 22:16:01 -0700 | [diff] [blame] | 311 | constexpr double kMaxShootSpeed = 2.7; |
Henry Speiser | a9c49a2 | 2022-04-01 15:22:35 -0700 | [diff] [blame] | 312 | const bool moving_too_fast = std::abs(robot_velocity()) > kMaxShootSpeed; |
| 313 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 314 | switch (state_) { |
| 315 | case SuperstructureState::IDLE: { |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 316 | // Only change the turret's goal loading position when idle, to prevent us |
| 317 | // spinning the turret around when TRANSFERRING... |
Milind Upadhyay | 9064853 | 2022-03-13 20:37:02 -0700 | [diff] [blame] | 318 | if (have_active_intake_request) { |
| 319 | turret_intake_state_ = unsafe_goal->turret_intake(); |
| 320 | } |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 321 | if (front_intake_has_ball_ != back_intake_has_ball_) { |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 322 | turret_intake_state_ = front_intake_has_ball_ ? RequestedIntake::kFront |
| 323 | : RequestedIntake::kBack; |
| 324 | } |
| 325 | // When IDLE with no specific intake button pressed, allow the goal |
| 326 | // message to override the intaking stuff. |
Austin Schuh | 9290881 | 2022-03-27 14:14:05 -0700 | [diff] [blame] | 327 | if (have_active_intake_request || (turret_goal == nullptr)) { |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 328 | turret_goal = &turret_loading_goal_buffer.message(); |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 329 | } |
| 330 | |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 331 | if (!front_intake_has_ball_ && !back_intake_has_ball_) { |
Austin Schuh | 9290881 | 2022-03-27 14:14:05 -0700 | [diff] [blame] | 332 | last_shot_angle_ = std::nullopt; |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 333 | break; |
| 334 | } |
| 335 | |
| 336 | state_ = SuperstructureState::TRANSFERRING; |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 337 | // Save the side the ball is on for later |
| 338 | |
| 339 | break; |
| 340 | } |
| 341 | case SuperstructureState::TRANSFERRING: { |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 342 | // If we've been transferring for too long, the ball probably got lost. |
| 343 | if ((turret_intake_state_ == RequestedIntake::kFront && |
| 344 | !front_intake_has_ball_) || |
| 345 | (turret_intake_state_ == RequestedIntake::kBack && |
| 346 | !back_intake_has_ball_)) { |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 347 | state_ = SuperstructureState::IDLE; |
| 348 | break; |
| 349 | } |
| 350 | |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 351 | turret_goal = &turret_loading_goal_buffer.message(); |
Milind Upadhyay | 27d16d3 | 2022-04-16 13:41:45 -0700 | [diff] [blame^] | 352 | aimer_.UpdateTurretGoal(turret_loading_position); |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 353 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 354 | const bool turret_near_goal = |
| 355 | std::abs(turret_.estimated_position() - turret_loading_position) < |
milind-u | c63d094 | 2022-04-15 12:07:42 -0700 | [diff] [blame] | 356 | kTurretGoalLoadingThreshold; |
| 357 | if (!turret_near_goal || !catapult_near_return_position) { |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 358 | break; // Wait for turret to reach the chosen intake |
| 359 | } |
| 360 | |
| 361 | // Transfer rollers and flipper arm belt on |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 362 | if (turret_intake_state_ == RequestedIntake::kFront) { |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 363 | transfer_roller_speed = constants::Values::kTransferRollerVoltage(); |
Milind Upadhyay | b1a74ea | 2022-03-09 20:34:35 -0800 | [diff] [blame] | 364 | } else { |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 365 | transfer_roller_speed = -constants::Values::kTransferRollerVoltage(); |
Milind Upadhyay | b1a74ea | 2022-03-09 20:34:35 -0800 | [diff] [blame] | 366 | } |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 367 | flipper_arms_voltage = constants::Values::kFlipperFeedVoltage(); |
| 368 | |
| 369 | // Ball is in catapult |
| 370 | if (position->turret_beambreak()) { |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 371 | if (turret_intake_state_ == RequestedIntake::kFront) { |
| 372 | front_intake_has_ball_ = false; |
| 373 | } else { |
| 374 | back_intake_has_ball_ = false; |
| 375 | } |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 376 | state_ = SuperstructureState::LOADING; |
| 377 | loading_timer_ = timestamp; |
| 378 | } |
| 379 | break; |
| 380 | } |
| 381 | case SuperstructureState::LOADING: { |
| 382 | flipper_arms_voltage = constants::Values::kFlipperFeedVoltage(); |
| 383 | |
| 384 | // Keep feeding for kExtraLoadingTime |
| 385 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 386 | // The ball should go past the turret beambreak to be loaded. |
Milind Upadhyay | 5dfabef | 2022-03-06 14:08:15 -0800 | [diff] [blame] | 387 | // If we got a CAN reading not too long ago, the flippers should have |
| 388 | // also stopped. |
Milind Upadhyay | 47e0d03 | 2022-03-05 23:06:25 -0800 | [diff] [blame] | 389 | if (position->turret_beambreak()) { |
| 390 | loading_timer_ = timestamp; |
| 391 | } else if (timestamp > |
| 392 | loading_timer_ + constants::Values::kExtraLoadingTime()) { |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 393 | state_ = SuperstructureState::LOADED; |
Ravago Jones | d51af7a | 2022-03-26 21:44:20 -0700 | [diff] [blame] | 394 | // reset color and wait for a new one once we know the ball is in place |
| 395 | ball_color_ = aos::Alliance::kInvalid; |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 396 | reseating_in_catapult_ = false; |
| 397 | } |
| 398 | break; |
| 399 | } |
| 400 | case SuperstructureState::LOADED: { |
| 401 | if (unsafe_goal != nullptr) { |
Austin Schuh | 9290881 | 2022-03-27 14:14:05 -0700 | [diff] [blame] | 402 | if (turret_goal == nullptr) { |
| 403 | if (last_shot_angle_) { |
| 404 | turret_loading_goal_buffer.mutable_message()->mutate_unsafe_goal( |
| 405 | *last_shot_angle_); |
| 406 | } |
| 407 | turret_goal = &turret_loading_goal_buffer.message(); |
| 408 | } |
Ravago Jones | d51af7a | 2022-03-26 21:44:20 -0700 | [diff] [blame] | 409 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 410 | if (unsafe_goal->cancel_shot()) { |
| 411 | // Cancel the shot process |
| 412 | state_ = SuperstructureState::IDLE; |
Ravago Jones | d51af7a | 2022-03-26 21:44:20 -0700 | [diff] [blame] | 413 | } else if (unsafe_goal->fire() || discarding_ball_) { |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 414 | // Start if we were asked to and the turret is at goal |
| 415 | state_ = SuperstructureState::SHOOTING; |
| 416 | prev_shot_count_ = catapult_.shot_count(); |
| 417 | |
| 418 | // Reset opening timeout |
| 419 | flipper_opening_start_time_ = timestamp; |
milind-u | c63d094 | 2022-04-15 12:07:42 -0700 | [diff] [blame] | 420 | loading_timer_ = timestamp; |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | break; |
| 424 | } |
| 425 | case SuperstructureState::SHOOTING: { |
Austin Schuh | 9290881 | 2022-03-27 14:14:05 -0700 | [diff] [blame] | 426 | if (turret_goal == nullptr) { |
| 427 | if (last_shot_angle_) { |
| 428 | turret_loading_goal_buffer.mutable_message()->mutate_unsafe_goal( |
| 429 | *last_shot_angle_); |
| 430 | } |
| 431 | turret_goal = &turret_loading_goal_buffer.message(); |
| 432 | last_shot_angle_ = turret_goal->unsafe_goal(); |
| 433 | } else { |
| 434 | last_shot_angle_ = std::nullopt; |
| 435 | } |
| 436 | const bool turret_near_goal = |
| 437 | turret_goal != nullptr && |
| 438 | std::abs(turret_goal->unsafe_goal() - turret_.position()) < |
| 439 | kTurretGoalThreshold; |
| 440 | |
milind-u | ee3b001 | 2022-03-14 21:19:57 -0700 | [diff] [blame] | 441 | // Don't open the flippers until the turret's ready: give them as little |
Henry Speiser | a9c49a2 | 2022-04-01 15:22:35 -0700 | [diff] [blame] | 442 | // time to get bumped as possible. Or moving to fast. |
| 443 | if (!turret_near_goal || collided || moving_too_fast) { |
milind-u | ee3b001 | 2022-03-14 21:19:57 -0700 | [diff] [blame] | 444 | break; |
| 445 | } |
| 446 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 447 | // Opening flipper arms could fail, wait until they are open using their |
| 448 | // potentiometers (the member below is just named encoder). |
| 449 | // Be a little more lenient if the flippers were already open in case of |
| 450 | // noise or collisions. |
| 451 | const double flipper_open_position = |
| 452 | (flippers_open_ ? constants::Values::kReseatFlipperPosition() |
| 453 | : constants::Values::kFlipperOpenPosition()); |
milind-u | 37dc40b | 2022-03-08 19:51:27 -0800 | [diff] [blame] | 454 | |
| 455 | // TODO(milind): add left arm back once it's fixed |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 456 | flippers_open_ = |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 457 | position->flipper_arm_right()->encoder() >= flipper_open_position; |
| 458 | |
| 459 | if (flippers_open_) { |
| 460 | // Hold at kFlipperHoldVoltage |
| 461 | flipper_arms_voltage = constants::Values::kFlipperHoldVoltage(); |
| 462 | } else { |
| 463 | // Open at kFlipperOpenVoltage |
| 464 | flipper_arms_voltage = constants::Values::kFlipperOpenVoltage(); |
| 465 | } |
| 466 | |
milind-u | ee3b001 | 2022-03-14 21:19:57 -0700 | [diff] [blame] | 467 | // There are two possible failures for the flippers: |
| 468 | // 1. They never open on time |
| 469 | // 2. They opened and we started firing, but we got bumped or something |
| 470 | // and they went back. |
| 471 | // If the flippers didn't open in a reasonable amount of time, try |
| 472 | // reseating the ball and reversing them. |
| 473 | if (!fire_ && !flippers_open_ && |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 474 | timestamp > |
| 475 | loading_timer_ + constants::Values::kFlipperOpeningTimeout()) { |
| 476 | // Reseat the ball and try again |
| 477 | state_ = SuperstructureState::LOADING; |
| 478 | loading_timer_ = timestamp; |
| 479 | reseating_in_catapult_ = true; |
| 480 | break; |
| 481 | } |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 482 | |
Henry Speiser | 28288cc | 2022-03-09 22:59:24 -0800 | [diff] [blame] | 483 | // If the turret reached the aiming goal and the catapult is safe to move |
| 484 | // up, fire! |
| 485 | if (flippers_open_ && turret_near_goal && !collided) { |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 486 | fire_ = true; |
| 487 | } |
| 488 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 489 | // Once the shot is complete and the catapult is back to its return |
| 490 | // position, go back to IDLE |
Milind Upadhyay | 27d16d3 | 2022-04-16 13:41:45 -0700 | [diff] [blame^] | 491 | if (catapult_.shot_count() > prev_shot_count_) { |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 492 | prev_shot_count_ = catapult_.shot_count(); |
| 493 | fire_ = false; |
Ravago Jones | d51af7a | 2022-03-26 21:44:20 -0700 | [diff] [blame] | 494 | discarding_ball_ = false; |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 495 | state_ = SuperstructureState::IDLE; |
| 496 | } |
| 497 | |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | collision_avoidance_.UpdateGoal( |
| 503 | {.intake_front_position = intake_front_.estimated_position(), |
| 504 | .intake_back_position = intake_back_.estimated_position(), |
Henry Speiser | 28288cc | 2022-03-09 22:59:24 -0800 | [diff] [blame] | 505 | .turret_position = turret_.estimated_position(), |
milind-u | c63d094 | 2022-04-15 12:07:42 -0700 | [diff] [blame] | 506 | .shooting = (state_ == SuperstructureState::SHOOTING) || |
| 507 | !catapult_near_return_position}, |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 508 | turret_goal); |
| 509 | |
| 510 | turret_.set_min_position(collision_avoidance_.min_turret_goal()); |
| 511 | turret_.set_max_position(collision_avoidance_.max_turret_goal()); |
| 512 | intake_front_.set_min_position(collision_avoidance_.min_intake_front_goal()); |
| 513 | intake_front_.set_max_position(collision_avoidance_.max_intake_front_goal()); |
| 514 | intake_back_.set_min_position(collision_avoidance_.min_intake_back_goal()); |
| 515 | intake_back_.set_max_position(collision_avoidance_.max_intake_back_goal()); |
| 516 | |
James Kuszmaul | 84083f4 | 2022-02-27 17:24:38 -0800 | [diff] [blame] | 517 | const flatbuffers::Offset<AimerStatus> aimer_offset = |
| 518 | aimer_.PopulateStatus(status->fbb()); |
| 519 | |
Milind Upadhyay | 5dfabef | 2022-03-06 14:08:15 -0800 | [diff] [blame] | 520 | // Disable the catapult if we want to restart to prevent damage with |
| 521 | // flippers |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 522 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
Austin Schuh | 97410d7 | 2022-03-12 15:37:23 -0800 | [diff] [blame] | 523 | catapult_status_offset = catapult_.Iterate( |
Ravago Jones | 3283ce0 | 2022-03-09 19:31:29 -0800 | [diff] [blame] | 524 | catapult_goal, position, robot_state().voltage_battery(), |
Austin Schuh | 97410d7 | 2022-03-12 15:37:23 -0800 | [diff] [blame] | 525 | output != nullptr && !catapult_.estopped() |
| 526 | ? &(output_struct.catapult_voltage) |
| 527 | : nullptr, |
| 528 | fire_, status->fbb()); |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 529 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 530 | const flatbuffers::Offset<RelativeEncoderProfiledJointStatus> |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 531 | climber_status_offset = climber_.Iterate( |
| 532 | unsafe_goal != nullptr ? unsafe_goal->climber() : nullptr, |
Austin Schuh | 7f12036 | 2022-03-05 17:10:11 -0800 | [diff] [blame] | 533 | position->climber(), |
| 534 | output != nullptr ? &output_struct.climber_voltage : nullptr, |
| 535 | status->fbb()); |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 536 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 537 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 538 | intake_status_offset_front = intake_front_.Iterate( |
| 539 | unsafe_goal != nullptr ? unsafe_goal->intake_front() : nullptr, |
Austin Schuh | 7f12036 | 2022-03-05 17:10:11 -0800 | [diff] [blame] | 540 | position->intake_front(), |
| 541 | output != nullptr ? &output_struct.intake_voltage_front : nullptr, |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 542 | status->fbb()); |
| 543 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 544 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 545 | intake_status_offset_back = intake_back_.Iterate( |
| 546 | unsafe_goal != nullptr ? unsafe_goal->intake_back() : nullptr, |
Austin Schuh | 7f12036 | 2022-03-05 17:10:11 -0800 | [diff] [blame] | 547 | position->intake_back(), |
| 548 | output != nullptr ? &output_struct.intake_voltage_back : nullptr, |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 549 | status->fbb()); |
| 550 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 551 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
Austin Schuh | 7f12036 | 2022-03-05 17:10:11 -0800 | [diff] [blame] | 552 | turret_status_offset = turret_.Iterate( |
| 553 | turret_goal, position->turret(), |
| 554 | output != nullptr ? &output_struct.turret_voltage : nullptr, |
| 555 | status->fbb()); |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 556 | |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 557 | if (output != nullptr) { |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 558 | output_struct.roller_voltage_front = roller_speed_compensated_front; |
| 559 | output_struct.roller_voltage_back = roller_speed_compensated_back; |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 560 | output_struct.transfer_roller_voltage = transfer_roller_speed; |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 561 | output_struct.flipper_arms_voltage = flipper_arms_voltage; |
milind-u | 6e7d8d4 | 2022-04-06 18:30:43 -0700 | [diff] [blame] | 562 | if (climber_servo) { |
Austin Schuh | 677af7e | 2022-04-13 19:45:39 -0700 | [diff] [blame] | 563 | output_struct.climber_servo_left = 0.5; |
milind-u | 6e7d8d4 | 2022-04-06 18:30:43 -0700 | [diff] [blame] | 564 | output_struct.climber_servo_right = 1.0; |
| 565 | } else { |
| 566 | output_struct.climber_servo_left = 1.0; |
| 567 | output_struct.climber_servo_right = 0.0; |
| 568 | } |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 569 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 570 | output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct))); |
| 571 | } |
| 572 | |
| 573 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
| 574 | |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 575 | const bool zeroed = intake_front_.zeroed() && intake_back_.zeroed() && |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 576 | turret_.zeroed() && climber_.zeroed() && |
| 577 | catapult_.zeroed(); |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 578 | const bool estopped = intake_front_.estopped() || intake_back_.estopped() || |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 579 | turret_.estopped() || climber_.estopped() || |
| 580 | catapult_.estopped(); |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 581 | |
| 582 | status_builder.add_zeroed(zeroed); |
| 583 | status_builder.add_estopped(estopped); |
| 584 | |
| 585 | status_builder.add_intake_front(intake_status_offset_front); |
| 586 | status_builder.add_intake_back(intake_status_offset_back); |
| 587 | status_builder.add_turret(turret_status_offset); |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 588 | status_builder.add_climber(climber_status_offset); |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 589 | |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 590 | status_builder.add_catapult(catapult_status_offset); |
| 591 | status_builder.add_solve_time(catapult_.solve_time()); |
Austin Schuh | 80fc275 | 2022-02-25 13:33:56 -0800 | [diff] [blame] | 592 | status_builder.add_shot_count(catapult_.shot_count()); |
Austin Schuh | 4147255 | 2022-03-13 18:09:41 -0700 | [diff] [blame] | 593 | status_builder.add_mpc_horizon(catapult_.mpc_horizon()); |
Ravago Jones | 3283ce0 | 2022-03-09 19:31:29 -0800 | [diff] [blame] | 594 | if (catapult_goal != nullptr) { |
| 595 | status_builder.add_shot_position(catapult_goal->shot_position()); |
| 596 | status_builder.add_shot_velocity(catapult_goal->shot_velocity()); |
| 597 | } |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 598 | |
| 599 | status_builder.add_flippers_open(flippers_open_); |
| 600 | status_builder.add_reseating_in_catapult(reseating_in_catapult_); |
| 601 | status_builder.add_fire(fire_); |
Henry Speiser | a9c49a2 | 2022-04-01 15:22:35 -0700 | [diff] [blame] | 602 | status_builder.add_moving_too_fast(moving_too_fast); |
Ravago Jones | d51af7a | 2022-03-26 21:44:20 -0700 | [diff] [blame] | 603 | status_builder.add_discarding_ball(discarding_ball_); |
Henry Speiser | 77747b7 | 2022-03-06 17:18:29 -0800 | [diff] [blame] | 604 | status_builder.add_ready_to_fire(state_ == SuperstructureState::LOADED && |
| 605 | turret_near_goal && !collided); |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 606 | status_builder.add_state(state_); |
James Kuszmaul | 4052695 | 2022-03-13 15:56:38 -0700 | [diff] [blame] | 607 | if (!front_intake_has_ball_ && !back_intake_has_ball_) { |
| 608 | status_builder.add_intake_state(IntakeState::NO_BALL); |
| 609 | } else if (front_intake_has_ball_ && back_intake_has_ball_) { |
| 610 | status_builder.add_intake_state(turret_intake_state_ == |
| 611 | RequestedIntake::kFront |
| 612 | ? IntakeState::INTAKE_FRONT_BALL |
| 613 | : IntakeState::INTAKE_BACK_BALL); |
| 614 | } else { |
| 615 | status_builder.add_intake_state(front_intake_has_ball_ |
| 616 | ? IntakeState::INTAKE_FRONT_BALL |
| 617 | : IntakeState::INTAKE_BACK_BALL); |
| 618 | } |
| 619 | status_builder.add_front_intake_has_ball(front_intake_has_ball_); |
| 620 | status_builder.add_back_intake_has_ball(back_intake_has_ball_); |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 621 | |
James Kuszmaul | 84083f4 | 2022-02-27 17:24:38 -0800 | [diff] [blame] | 622 | status_builder.add_aimer(aimer_offset); |
| 623 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 624 | (void)status->Send(status_builder.Finish()); |
| 625 | } |
| 626 | |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 627 | double Superstructure::robot_velocity() const { |
| 628 | return (drivetrain_status_fetcher_.get() != nullptr |
| 629 | ? drivetrain_status_fetcher_->robot_speed() |
| 630 | : 0.0); |
| 631 | } |
| 632 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 633 | } // namespace superstructure |
| 634 | } // namespace control_loops |
| 635 | } // namespace y2022 |