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