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