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