Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <array> |
| 4 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 5 | #include <cinttypes> |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 6 | #include <cmath> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 7 | #include <cstdio> |
| 8 | #include <cstring> |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 9 | #include <functional> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 10 | #include <memory> |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 11 | #include <mutex> |
| 12 | #include <thread> |
| 13 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 14 | #include "frc971/wpilib/ahal/AnalogInput.h" |
| 15 | #include "frc971/wpilib/ahal/Counter.h" |
| 16 | #include "frc971/wpilib/ahal/DigitalGlitchFilter.h" |
| 17 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 18 | #include "frc971/wpilib/ahal/Encoder.h" |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 19 | #include "frc971/wpilib/ahal/TalonFX.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 20 | #include "frc971/wpilib/ahal/VictorSP.h" |
| 21 | #undef ERROR |
| 22 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 23 | #include "absl/flags/flag.h" |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 24 | #include "ctre/phoenix6/TalonFX.hpp" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 25 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 26 | #include "aos/commonmath.h" |
| 27 | #include "aos/events/event_loop.h" |
| 28 | #include "aos/events/shm_event_loop.h" |
| 29 | #include "aos/init.h" |
| 30 | #include "aos/logging/logging.h" |
Austin Schuh | 83873c3 | 2020-02-22 14:58:39 -0800 | [diff] [blame] | 31 | #include "aos/network/team_number.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 32 | #include "aos/realtime.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 33 | #include "aos/time/time.h" |
| 34 | #include "aos/util/log_interval.h" |
| 35 | #include "aos/util/phased_loop.h" |
| 36 | #include "aos/util/wrapping_counter.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 37 | #include "frc971/autonomous/auto_mode_generated.h" |
| 38 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 39 | #include "frc971/input/robot_state_generated.h" |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 40 | #include "frc971/wpilib/ADIS16448.h" |
James Kuszmaul | a244a91 | 2020-01-18 13:50:50 -0800 | [diff] [blame] | 41 | #include "frc971/wpilib/ADIS16470.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 42 | #include "frc971/wpilib/buffered_pcm.h" |
| 43 | #include "frc971/wpilib/buffered_solenoid.h" |
| 44 | #include "frc971/wpilib/dma.h" |
| 45 | #include "frc971/wpilib/drivetrain_writer.h" |
| 46 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 47 | #include "frc971/wpilib/joystick_sender.h" |
| 48 | #include "frc971/wpilib/logging_generated.h" |
| 49 | #include "frc971/wpilib/loop_output_handler.h" |
| 50 | #include "frc971/wpilib/pdp_fetcher.h" |
| 51 | #include "frc971/wpilib/sensor_reader.h" |
| 52 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 53 | #include "y2020/constants.h" |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 54 | #include "y2020/control_loops/superstructure/shooter/shooter_tuning_readings_generated.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 55 | #include "y2020/control_loops/superstructure/superstructure_output_generated.h" |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 56 | #include "y2020/control_loops/superstructure/superstructure_position_static.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 57 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 58 | ABSL_FLAG(bool, shooter_tuning, true, |
| 59 | "If true, reads from ball beambreak sensors and sends shooter " |
| 60 | "tuning readings"); |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 61 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 62 | using ::aos::monotonic_clock; |
| 63 | using ::y2020::constants::Values; |
| 64 | namespace superstructure = ::y2020::control_loops::superstructure; |
| 65 | namespace chrono = ::std::chrono; |
Vinay Siva | e52a6b3 | 2021-07-10 15:19:26 -0700 | [diff] [blame] | 66 | using std::make_unique; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 67 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 68 | namespace y2020::wpilib { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 69 | namespace { |
| 70 | |
| 71 | constexpr double kMaxBringupPower = 12.0; |
| 72 | |
| 73 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 74 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 75 | // The low bit is direction. |
| 76 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 77 | double drivetrain_translate(int32_t in) { |
| 78 | return ((static_cast<double>(in) / |
| 79 | Values::kDrivetrainEncoderCountsPerRevolution()) * |
| 80 | (2.0 * M_PI)) * |
| 81 | Values::kDrivetrainEncoderRatio() * |
| 82 | control_loops::drivetrain::kWheelRadius; |
| 83 | } |
| 84 | |
| 85 | double drivetrain_velocity_translate(double in) { |
| 86 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
| 87 | (2.0 * M_PI)) * |
| 88 | Values::kDrivetrainEncoderRatio() * |
| 89 | control_loops::drivetrain::kWheelRadius; |
| 90 | } |
| 91 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 92 | double turret_pot_translate(double voltage) { |
| 93 | return voltage * Values::kTurretPotRatio() * |
| 94 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 95 | } |
| 96 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 97 | constexpr double kMaxFastEncoderPulsesPerSecond = |
Austin Schuh | 9dcd520 | 2020-02-20 20:06:04 -0800 | [diff] [blame] | 98 | std::max({Values::kMaxControlPanelEncoderPulsesPerSecond(), |
| 99 | Values::kMaxFinisherEncoderPulsesPerSecond(), |
| 100 | Values::kMaxAcceleratorEncoderPulsesPerSecond()}); |
| 101 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1000000.0, |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 102 | "fast encoders are too fast"); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 103 | constexpr double kMaxMediumEncoderPulsesPerSecond = |
Austin Schuh | 9dcd520 | 2020-02-20 20:06:04 -0800 | [diff] [blame] | 104 | std::max({Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
| 105 | Values::kMaxHoodEncoderPulsesPerSecond(), |
| 106 | Values::kMaxIntakeEncoderPulsesPerSecond(), |
| 107 | Values::kMaxTurretEncoderPulsesPerSecond()}); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 108 | |
Austin Schuh | 9dcd520 | 2020-02-20 20:06:04 -0800 | [diff] [blame] | 109 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000.0, |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 110 | "medium encoders are too fast"); |
| 111 | |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 112 | void PrintConfigs(ctre::phoenix6::hardware::TalonFX *talon) { |
| 113 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
| 114 | ctre::phoenix::StatusCode status = |
| 115 | talon->GetConfigurator().Refresh(configuration); |
| 116 | if (!status.IsOK()) { |
| 117 | AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s", |
| 118 | status.GetName(), status.GetDescription()); |
| 119 | } |
| 120 | AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str()); |
| 121 | } |
| 122 | |
| 123 | void WriteConfigs(ctre::phoenix6::hardware::TalonFX *talon, |
| 124 | double stator_current_limit, double supply_current_limit) { |
| 125 | ctre::phoenix6::configs::CurrentLimitsConfigs current_limits; |
James Kuszmaul | 2a951e2 | 2024-10-29 22:15:43 -0700 | [diff] [blame] | 126 | current_limits.StatorCurrentLimit = |
| 127 | units::current::ampere_t{stator_current_limit}; |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 128 | current_limits.StatorCurrentLimitEnable = true; |
James Kuszmaul | 2a951e2 | 2024-10-29 22:15:43 -0700 | [diff] [blame] | 129 | current_limits.SupplyCurrentLimit = |
| 130 | units::current::ampere_t{supply_current_limit}; |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 131 | current_limits.SupplyCurrentLimitEnable = true; |
| 132 | |
| 133 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
| 134 | configuration.CurrentLimits = current_limits; |
| 135 | |
| 136 | ctre::phoenix::StatusCode status = |
| 137 | talon->GetConfigurator().Apply(configuration); |
| 138 | if (!status.IsOK()) { |
| 139 | AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s", |
| 140 | status.GetName(), status.GetDescription()); |
| 141 | } |
| 142 | |
| 143 | PrintConfigs(talon); |
| 144 | } |
| 145 | |
| 146 | void Disable(ctre::phoenix6::hardware::TalonFX *talon) { |
| 147 | ctre::phoenix6::controls::DutyCycleOut stop_command(0.0); |
| 148 | stop_command.UpdateFreqHz = 0_Hz; |
| 149 | stop_command.EnableFOC = true; |
| 150 | |
| 151 | talon->SetControl(stop_command); |
| 152 | } |
| 153 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 154 | } // namespace |
| 155 | |
| 156 | // Class to send position messages with sensor readings to our loops. |
| 157 | class SensorReader : public ::frc971::wpilib::SensorReader { |
| 158 | public: |
| 159 | SensorReader(::aos::ShmEventLoop *event_loop) |
| 160 | : ::frc971::wpilib::SensorReader(event_loop), |
| 161 | auto_mode_sender_( |
| 162 | event_loop->MakeSender<::frc971::autonomous::AutonomousMode>( |
| 163 | "/autonomous")), |
| 164 | superstructure_position_sender_( |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 165 | event_loop->MakeSender<superstructure::PositionStatic>( |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 166 | "/superstructure")), |
| 167 | drivetrain_position_sender_( |
| 168 | event_loop |
| 169 | ->MakeSender<::frc971::control_loops::drivetrain::Position>( |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 170 | "/drivetrain")), |
| 171 | shooter_tuning_readings_sender_( |
| 172 | event_loop->MakeSender<superstructure::shooter::TuningReadings>( |
| 173 | "/superstructure")) { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 174 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 175 | // we should ever see. |
| 176 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 177 | UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond); |
milind-u | 62d4a8e | 2021-10-11 16:08:41 -0700 | [diff] [blame] | 178 | |
| 179 | constants::InitValues(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 182 | // Hood |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 183 | void set_hood_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 184 | medium_encoder_filter_.Add(encoder.get()); |
| 185 | hood_encoder_.set_encoder(::std::move(encoder)); |
| 186 | } |
| 187 | |
| 188 | void set_hood_absolute_pwm( |
| 189 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 190 | hood_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 191 | } |
| 192 | |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 193 | void set_hood_single_turn_absolute_pwm( |
| 194 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 195 | hood_encoder_.set_single_turn_absolute_pwm(::std::move(absolute_pwm)); |
| 196 | } |
| 197 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 198 | // Intake |
| 199 | |
| 200 | void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 201 | medium_encoder_filter_.Add(encoder.get()); |
| 202 | intake_joint_encoder_.set_encoder(::std::move(encoder)); |
| 203 | } |
| 204 | |
| 205 | void set_intake_absolute_pwm( |
| 206 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 207 | intake_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 208 | } |
| 209 | |
| 210 | // Turret |
| 211 | |
| 212 | void set_turret_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 213 | medium_encoder_filter_.Add(encoder.get()); |
| 214 | turret_encoder_.set_encoder(::std::move(encoder)); |
| 215 | } |
| 216 | |
| 217 | void set_turret_absolute_pwm( |
| 218 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 219 | turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 220 | } |
| 221 | |
| 222 | void set_turret_potentiometer( |
| 223 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 224 | turret_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 225 | } |
| 226 | |
| 227 | // Shooter |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 228 | void set_finisher_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 229 | fast_encoder_filter_.Add(encoder.get()); |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 230 | finisher_encoder_ = ::std::move(encoder); |
| 231 | } |
| 232 | void set_left_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 233 | fast_encoder_filter_.Add(encoder.get()); |
| 234 | left_accelerator_encoder_ = ::std::move(encoder); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 237 | void set_right_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 238 | fast_encoder_filter_.Add(encoder.get()); |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 239 | right_accelerator_encoder_ = ::std::move(encoder); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 242 | // Auto mode switches. |
| 243 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
Austin Schuh | 9dcd520 | 2020-02-20 20:06:04 -0800 | [diff] [blame] | 244 | medium_encoder_filter_.Add(sensor.get()); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 245 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 246 | } |
| 247 | |
James Kuszmaul | a244a91 | 2020-01-18 13:50:50 -0800 | [diff] [blame] | 248 | void set_imu(frc971::wpilib::ADIS16470 *imu) { imu_ = imu; } |
| 249 | |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 250 | void set_ball_beambreak_inputs(::std::unique_ptr<frc::DigitalInput> sensor1, |
| 251 | ::std::unique_ptr<frc::DigitalInput> sensor2) { |
| 252 | ball_beambreak_inputs_[0] = ::std::move(sensor1); |
| 253 | ball_beambreak_inputs_[1] = ::std::move(sensor2); |
Austin Schuh | 6c053ef | 2021-09-26 14:32:16 -0700 | [diff] [blame] | 254 | ball_beambreak_reader_.set_input_one(ball_beambreak_inputs_[0].get()); |
| 255 | ball_beambreak_reader_.set_input_two(ball_beambreak_inputs_[1].get()); |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Ravago Jones | f8b7bfe | 2021-10-09 16:25:29 -0700 | [diff] [blame] | 258 | void set_ball_intake_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) { |
| 259 | ball_intake_beambreak_ = ::std::move(sensor); |
| 260 | } |
| 261 | |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 262 | void Start() override { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 263 | if (absl::GetFlag(FLAGS_shooter_tuning)) { |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 264 | AddToDMA(&ball_beambreak_reader_); |
| 265 | } |
| 266 | } |
| 267 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 268 | void RunIteration() override { |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 269 | if (imu_ != nullptr) { |
| 270 | imu_->DoReads(); |
| 271 | } |
James Kuszmaul | a244a91 | 2020-01-18 13:50:50 -0800 | [diff] [blame] | 272 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 273 | { |
| 274 | auto builder = drivetrain_position_sender_.MakeBuilder(); |
| 275 | frc971::control_loops::drivetrain::Position::Builder drivetrain_builder = |
| 276 | builder.MakeBuilder<frc971::control_loops::drivetrain::Position>(); |
| 277 | drivetrain_builder.add_left_encoder( |
| 278 | drivetrain_translate(drivetrain_left_encoder_->GetRaw())); |
| 279 | drivetrain_builder.add_left_speed( |
| 280 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod())); |
| 281 | |
| 282 | drivetrain_builder.add_right_encoder( |
| 283 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw())); |
| 284 | drivetrain_builder.add_right_speed(-drivetrain_velocity_translate( |
| 285 | drivetrain_right_encoder_->GetPeriod())); |
| 286 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 287 | builder.CheckOk(builder.Send(drivetrain_builder.Finish())); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 288 | } |
Austin Schuh | 37d2c30 | 2021-10-12 22:07:43 -0700 | [diff] [blame] | 289 | const constants::Values &values = constants::GetValues(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 290 | |
| 291 | { |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 292 | aos::Sender<superstructure::PositionStatic>::StaticBuilder builder = |
| 293 | superstructure_position_sender_.MakeStaticBuilder(); |
Austin Schuh | 18ca45f | 2020-02-29 22:58:49 -0800 | [diff] [blame] | 294 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 295 | // TODO(alex): check new absolute encoder api. |
| 296 | // Hood |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 297 | CopyPosition(hood_encoder_, builder->add_hood(), |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 298 | Values::kHoodEncoderCountsPerRevolution(), |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 299 | Values::kHoodEncoderRatio(), |
| 300 | Values::kHoodSingleTurnEncoderRatio(), false); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 301 | // Intake |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 302 | CopyPosition(intake_joint_encoder_, builder->add_intake_joint(), |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 303 | Values::kIntakeEncoderCountsPerRevolution(), |
| 304 | Values::kIntakeEncoderRatio(), false); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 305 | // Turret |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 306 | CopyPosition(turret_encoder_, builder->add_turret(), |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 307 | Values::kTurretEncoderCountsPerRevolution(), |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 308 | Values::kTurretEncoderRatio(), turret_pot_translate, true, |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 309 | values.turret.potentiometer_offset); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 310 | // Shooter |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 311 | y2020::control_loops::superstructure::ShooterPositionStatic *shooter = |
| 312 | builder->add_shooter(); |
| 313 | shooter->set_theta_finisher( |
Austin Schuh | 0ad31d7 | 2021-03-06 17:07:04 -0800 | [diff] [blame] | 314 | encoder_translate(-finisher_encoder_->GetRaw(), |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 315 | Values::kFinisherEncoderCountsPerRevolution(), |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 316 | Values::kFinisherEncoderRatio())); |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 317 | |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 318 | shooter->set_theta_accelerator_left( |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 319 | encoder_translate(-left_accelerator_encoder_->GetRaw(), |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 320 | Values::kAcceleratorEncoderCountsPerRevolution(), |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 321 | Values::kAcceleratorEncoderRatio())); |
| 322 | shooter->set_theta_accelerator_right( |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 323 | encoder_translate(right_accelerator_encoder_->GetRaw(), |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 324 | Values::kAcceleratorEncoderCountsPerRevolution(), |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 325 | Values::kAcceleratorEncoderRatio())); |
| 326 | builder->set_intake_beambreak_triggered(ball_intake_beambreak_->Get()); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 327 | |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 328 | builder.CheckOk(builder.Send()); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | { |
| 332 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 333 | |
| 334 | uint32_t mode = 0; |
| 335 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 336 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 337 | mode |= 1 << i; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | auto auto_mode_builder = |
| 342 | builder.MakeBuilder<frc971::autonomous::AutonomousMode>(); |
| 343 | |
| 344 | auto_mode_builder.add_mode(mode); |
| 345 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 346 | builder.CheckOk(builder.Send(auto_mode_builder.Finish())); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 347 | } |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 348 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 349 | if (absl::GetFlag(FLAGS_shooter_tuning)) { |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 350 | // Distance between beambreak sensors, in meters. |
| 351 | constexpr double kDistanceBetweenBeambreaks = 0.4813; |
| 352 | |
Austin Schuh | 6c053ef | 2021-09-26 14:32:16 -0700 | [diff] [blame] | 353 | if (ball_beambreak_reader_.pulses_detected() > balls_detected_) { |
| 354 | balls_detected_ = ball_beambreak_reader_.pulses_detected(); |
| 355 | |
| 356 | auto builder = shooter_tuning_readings_sender_.MakeBuilder(); |
| 357 | auto shooter_tuning_readings_builder = |
| 358 | builder.MakeBuilder<superstructure::shooter::TuningReadings>(); |
| 359 | shooter_tuning_readings_builder.add_velocity_ball( |
| 360 | kDistanceBetweenBeambreaks / ball_beambreak_reader_.last_width()); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 361 | builder.CheckOk(builder.Send(shooter_tuning_readings_builder.Finish())); |
Austin Schuh | 6c053ef | 2021-09-26 14:32:16 -0700 | [diff] [blame] | 362 | } |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 363 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | private: |
| 367 | ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_; |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 368 | ::aos::Sender<superstructure::PositionStatic> superstructure_position_sender_; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 369 | ::aos::Sender<::frc971::control_loops::drivetrain::Position> |
| 370 | drivetrain_position_sender_; |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 371 | ::aos::Sender<superstructure::shooter::TuningReadings> |
| 372 | shooter_tuning_readings_sender_; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 373 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 374 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer turret_encoder_; |
| 375 | |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 376 | ::frc971::wpilib::AbsoluteAndAbsoluteEncoder hood_encoder_; |
| 377 | |
| 378 | ::frc971::wpilib::AbsoluteEncoder intake_joint_encoder_; |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 379 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 380 | ::std::unique_ptr<::frc::Encoder> finisher_encoder_, |
milind-u | 3d68aa7 | 2021-09-26 12:19:03 -0700 | [diff] [blame] | 381 | left_accelerator_encoder_, right_accelerator_encoder_; |
milind-u | 9aa62a8 | 2021-10-03 20:43:19 -0700 | [diff] [blame] | 382 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 383 | ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_; |
James Kuszmaul | a244a91 | 2020-01-18 13:50:50 -0800 | [diff] [blame] | 384 | |
| 385 | frc971::wpilib::ADIS16470 *imu_ = nullptr; |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 386 | |
Ravago Jones | f8b7bfe | 2021-10-09 16:25:29 -0700 | [diff] [blame] | 387 | std::unique_ptr<frc::DigitalInput> ball_intake_beambreak_; |
| 388 | |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 389 | // Used to interface with the two beam break sensors that the ball for tuning |
| 390 | // shooter parameters has to pass through. |
| 391 | // We will time how long it takes to pass between the two sensors to get its |
| 392 | // velocity. |
| 393 | std::array<std::unique_ptr<frc::DigitalInput>, 2> ball_beambreak_inputs_; |
| 394 | frc971::wpilib::DMAPulseSeparationReader ball_beambreak_reader_; |
| 395 | int balls_detected_ = 0; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 396 | }; |
| 397 | |
| 398 | class SuperstructureWriter |
| 399 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
| 400 | public: |
| 401 | SuperstructureWriter(::aos::EventLoop *event_loop) |
| 402 | : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 403 | event_loop, "/superstructure") {} |
| 404 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 405 | void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 406 | hood_victor_ = ::std::move(t); |
| 407 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 408 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 409 | void set_intake_joint_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 410 | intake_joint_victor_ = ::std::move(t); |
| 411 | } |
| 412 | |
| 413 | void set_intake_roller_falcon( |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 414 | ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) { |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 415 | intake_roller_falcon_ = ::std::move(t); |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 416 | |
| 417 | WriteConfigs(intake_roller_falcon_.get(), |
| 418 | Values::kIntakeRollerStatorCurrentLimit(), |
| 419 | Values::kIntakeRollerSupplyCurrentLimit()); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 423 | turret_victor_ = ::std::move(t); |
| 424 | } |
| 425 | |
Ravago Jones | 3dda560 | 2021-03-10 00:33:13 -0800 | [diff] [blame] | 426 | void set_feeder_falcon( |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 427 | ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) { |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 428 | feeder_falcon_ = ::std::move(t); |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 429 | |
| 430 | WriteConfigs(feeder_falcon_.get(), Values::kFeederStatorCurrentLimit(), |
| 431 | Values::kFeederSupplyCurrentLimit()); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 432 | } |
| 433 | |
milind-u | 9aa62a8 | 2021-10-03 20:43:19 -0700 | [diff] [blame] | 434 | void set_washing_machine_control_panel_victor( |
| 435 | ::std::unique_ptr<::frc::VictorSP> t) { |
| 436 | washing_machine_control_panel_victor_ = ::std::move(t); |
| 437 | } |
| 438 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 439 | void set_accelerator_left_falcon(::std::unique_ptr<::frc::TalonFX> t) { |
| 440 | accelerator_left_falcon_ = ::std::move(t); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 443 | void set_accelerator_right_falcon(::std::unique_ptr<::frc::TalonFX> t) { |
| 444 | accelerator_right_falcon_ = ::std::move(t); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Austin Schuh | 0ad31d7 | 2021-03-06 17:07:04 -0800 | [diff] [blame] | 447 | void set_finisher_falcon0(::std::unique_ptr<::frc::TalonFX> t) { |
| 448 | finisher_falcon0_ = ::std::move(t); |
| 449 | } |
| 450 | |
| 451 | void set_finisher_falcon1(::std::unique_ptr<::frc::TalonFX> t) { |
| 452 | finisher_falcon1_ = ::std::move(t); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | void set_climber_falcon( |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 456 | ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) { |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 457 | climber_falcon_ = ::std::move(t); |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 458 | ctre::phoenix6::configs::CurrentLimitsConfigs current_limits; |
James Kuszmaul | 2a951e2 | 2024-10-29 22:15:43 -0700 | [diff] [blame] | 459 | current_limits.SupplyCurrentLimit = |
| 460 | units::current::ampere_t{Values::kClimberSupplyCurrentLimit()}; |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 461 | current_limits.SupplyCurrentLimitEnable = true; |
| 462 | |
| 463 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
| 464 | configuration.CurrentLimits = current_limits; |
| 465 | |
| 466 | ctre::phoenix::StatusCode status = |
| 467 | climber_falcon_->GetConfigurator().Apply(configuration); |
| 468 | if (!status.IsOK()) { |
| 469 | AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s", |
| 470 | status.GetName(), status.GetDescription()); |
| 471 | } |
| 472 | |
| 473 | PrintConfigs(climber_falcon_.get()); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | private: |
| 477 | void Write(const superstructure::Output &output) override { |
Austin Schuh | 2efe168 | 2021-03-06 22:47:15 -0800 | [diff] [blame] | 478 | hood_victor_->SetSpeed(std::clamp(-output.hood_voltage(), -kMaxBringupPower, |
| 479 | kMaxBringupPower) / |
| 480 | 12.0); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 481 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 482 | intake_joint_victor_->SetSpeed(std::clamp(-output.intake_joint_voltage(), |
| 483 | -kMaxBringupPower, |
| 484 | kMaxBringupPower) / |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 485 | 12.0); |
| 486 | |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 487 | WriteCan(-output.intake_roller_voltage(), intake_roller_falcon_.get()); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 488 | |
milind-u | f70e8e1 | 2021-10-02 12:36:00 -0700 | [diff] [blame] | 489 | turret_victor_->SetSpeed(std::clamp(output.turret_voltage(), |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 490 | -kMaxBringupPower, kMaxBringupPower) / |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 491 | 12.0); |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 492 | WriteCan(output.feeder_voltage(), feeder_falcon_.get()); |
milind-u | 9aa62a8 | 2021-10-03 20:43:19 -0700 | [diff] [blame] | 493 | if (washing_machine_control_panel_victor_) { |
| 494 | washing_machine_control_panel_victor_->SetSpeed( |
| 495 | std::clamp(-output.washing_machine_spinner_voltage(), |
| 496 | -kMaxBringupPower, kMaxBringupPower) / |
| 497 | 12.0); |
| 498 | } |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 499 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 500 | accelerator_left_falcon_->SetSpeed( |
| 501 | std::clamp(-output.accelerator_left_voltage(), -kMaxBringupPower, |
| 502 | kMaxBringupPower) / |
| 503 | 12.0); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 504 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 505 | accelerator_right_falcon_->SetSpeed( |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 506 | std::clamp(output.accelerator_right_voltage(), -kMaxBringupPower, |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 507 | kMaxBringupPower) / |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 508 | 12.0); |
| 509 | |
Austin Schuh | 0ad31d7 | 2021-03-06 17:07:04 -0800 | [diff] [blame] | 510 | finisher_falcon1_->SetSpeed(std::clamp(output.finisher_voltage(), |
| 511 | -kMaxBringupPower, |
| 512 | kMaxBringupPower) / |
| 513 | 12.0); |
| 514 | finisher_falcon0_->SetSpeed(std::clamp(-output.finisher_voltage(), |
| 515 | -kMaxBringupPower, |
| 516 | kMaxBringupPower) / |
| 517 | 12.0); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 518 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 519 | if (climber_falcon_) { |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 520 | WriteCan(output.climber_voltage(), climber_falcon_.get()); |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 521 | } |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 524 | static void WriteCan(const double voltage, |
| 525 | ::ctre::phoenix6::hardware::TalonFX *falcon) { |
| 526 | ctre::phoenix6::controls::DutyCycleOut control( |
| 527 | std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
| 528 | control.UpdateFreqHz = 0_Hz; |
| 529 | control.EnableFOC = true; |
| 530 | |
| 531 | falcon->SetControl(control); |
| 532 | } |
| 533 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 534 | void Stop() override { |
| 535 | AOS_LOG(WARNING, "Superstructure output too old.\n"); |
| 536 | hood_victor_->SetDisabled(); |
| 537 | intake_joint_victor_->SetDisabled(); |
| 538 | turret_victor_->SetDisabled(); |
milind-u | 9aa62a8 | 2021-10-03 20:43:19 -0700 | [diff] [blame] | 539 | if (washing_machine_control_panel_victor_) { |
| 540 | washing_machine_control_panel_victor_->SetDisabled(); |
| 541 | } |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 542 | accelerator_left_falcon_->SetDisabled(); |
| 543 | accelerator_right_falcon_->SetDisabled(); |
Austin Schuh | 0ad31d7 | 2021-03-06 17:07:04 -0800 | [diff] [blame] | 544 | finisher_falcon0_->SetDisabled(); |
| 545 | finisher_falcon1_->SetDisabled(); |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 546 | Disable(feeder_falcon_.get()); |
| 547 | Disable(intake_roller_falcon_.get()); |
| 548 | Disable(climber_falcon_.get()); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | ::std::unique_ptr<::frc::VictorSP> hood_victor_, intake_joint_victor_, |
milind-u | 9aa62a8 | 2021-10-03 20:43:19 -0700 | [diff] [blame] | 552 | turret_victor_, washing_machine_control_panel_victor_; |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 553 | |
Ravago Jones | 3dda560 | 2021-03-10 00:33:13 -0800 | [diff] [blame] | 554 | ::std::unique_ptr<::frc::TalonFX> accelerator_left_falcon_, |
Austin Schuh | 0ad31d7 | 2021-03-06 17:07:04 -0800 | [diff] [blame] | 555 | accelerator_right_falcon_, finisher_falcon0_, finisher_falcon1_; |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 556 | |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 557 | ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> intake_roller_falcon_, |
| 558 | climber_falcon_, feeder_falcon_; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 559 | }; |
| 560 | |
| 561 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 562 | public: |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 563 | ::std::unique_ptr<frc::Encoder> make_encoder( |
| 564 | int index, frc::Encoder::EncodingType encodingType = frc::Encoder::k4X) { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 565 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 566 | encodingType); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | void Run() override { |
| 570 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 571 | aos::configuration::ReadConfig("aos_config.json"); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 572 | |
| 573 | // Thread 1. |
| 574 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
| 575 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 576 | &joystick_sender_event_loop); |
| 577 | AddLoop(&joystick_sender_event_loop); |
| 578 | |
| 579 | // Thread 2. |
| 580 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 581 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 582 | AddLoop(&pdp_fetcher_event_loop); |
| 583 | |
| 584 | // Thread 3. |
| 585 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
| 586 | SensorReader sensor_reader(&sensor_reader_event_loop); |
Austin Schuh | f7db58c | 2020-02-29 22:57:43 -0800 | [diff] [blame] | 587 | sensor_reader.set_pwm_trigger(true); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 588 | sensor_reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 589 | sensor_reader.set_drivetrain_right_encoder(make_encoder(1)); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 590 | // TODO: pin numbers |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 591 | sensor_reader.set_hood_encoder( |
| 592 | make_unique<frc::Encoder>(22, 23, false, frc::Encoder::k4X)); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 593 | |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 594 | sensor_reader.set_hood_absolute_pwm(make_unique<frc::DigitalInput>(25)); |
| 595 | sensor_reader.set_hood_single_turn_absolute_pwm( |
| 596 | make_unique<frc::DigitalInput>(24)); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 597 | |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 598 | sensor_reader.set_intake_encoder(make_encoder(5)); |
| 599 | sensor_reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(1)); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 600 | |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 601 | sensor_reader.set_turret_encoder(make_encoder(2)); |
| 602 | sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(0)); |
| 603 | sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(0)); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 604 | |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 605 | sensor_reader.set_finisher_encoder( |
| 606 | make_unique<frc::Encoder>(3, 2, false, frc::Encoder::k4X)); |
| 607 | sensor_reader.set_left_accelerator_encoder(make_encoder(4)); |
| 608 | sensor_reader.set_right_accelerator_encoder(make_encoder(3)); |
| 609 | |
Ravago Jones | f8b7bfe | 2021-10-09 16:25:29 -0700 | [diff] [blame] | 610 | sensor_reader.set_ball_intake_beambreak(make_unique<frc::DigitalInput>(4)); |
| 611 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 612 | if (absl::GetFlag(FLAGS_shooter_tuning)) { |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 613 | sensor_reader.set_ball_beambreak_inputs( |
| 614 | make_unique<frc::DigitalInput>(6), make_unique<frc::DigitalInput>(7)); |
| 615 | } |
| 616 | |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 617 | AddLoop(&sensor_reader_event_loop); |
| 618 | |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 619 | // Note: If ADIS16470 is plugged in directly to the roboRIO SPI port without |
| 620 | // the Spartan Board, then trigger is on 26, reset 27, and chip select is |
| 621 | // CS0. |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 622 | // TODO(james): Double check whether the above is still accurate/useful with |
| 623 | // the ADIS16448. No reason it shouldn't be. |
| 624 | frc::SPI::Port spi_port = frc::SPI::Port::kOnboardCS1; |
Austin Schuh | 83873c3 | 2020-02-22 14:58:39 -0800 | [diff] [blame] | 625 | std::unique_ptr<frc::DigitalInput> imu_trigger; |
| 626 | std::unique_ptr<frc::DigitalOutput> imu_reset; |
| 627 | if (::aos::network::GetTeamNumber() == |
| 628 | constants::Values::kCodingRobotTeamNumber) { |
| 629 | imu_trigger = make_unique<frc::DigitalInput>(26); |
| 630 | imu_reset = make_unique<frc::DigitalOutput>(27); |
| 631 | spi_port = frc::SPI::Port::kOnboardCS0; |
| 632 | } else { |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 633 | imu_trigger = make_unique<frc::DigitalInput>(9); |
| 634 | imu_reset = make_unique<frc::DigitalOutput>(8); |
Austin Schuh | 83873c3 | 2020-02-22 14:58:39 -0800 | [diff] [blame] | 635 | } |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 636 | ::aos::ShmEventLoop imu_event_loop(&config.message()); |
| 637 | std::unique_ptr<frc971::wpilib::ADIS16448> old_imu; |
| 638 | std::unique_ptr<frc971::wpilib::ADIS16470> new_imu; |
| 639 | std::unique_ptr<frc::SPI> imu_spi; |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 640 | if (::aos::network::GetTeamNumber() != |
| 641 | constants::Values::kCodingRobotTeamNumber) { |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 642 | old_imu = make_unique<frc971::wpilib::ADIS16448>( |
| 643 | &imu_event_loop, spi_port, imu_trigger.get()); |
| 644 | old_imu->SetDummySPI(frc::SPI::Port::kOnboardCS2); |
| 645 | old_imu->set_reset(imu_reset.get()); |
| 646 | } else { |
| 647 | imu_spi = make_unique<frc::SPI>(spi_port); |
| 648 | new_imu = make_unique<frc971::wpilib::ADIS16470>( |
| 649 | &imu_event_loop, imu_spi.get(), imu_trigger.get(), imu_reset.get()); |
| 650 | sensor_reader.set_imu(new_imu.get()); |
| 651 | } |
| 652 | AddLoop(&imu_event_loop); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 653 | |
| 654 | // Thread 4. |
| 655 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 656 | output_event_loop.set_name("output_writer"); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 657 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop); |
| 658 | drivetrain_writer.set_left_controller0( |
| 659 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true); |
| 660 | drivetrain_writer.set_right_controller0( |
| 661 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false); |
| 662 | |
| 663 | SuperstructureWriter superstructure_writer(&output_event_loop); |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 664 | superstructure_writer.set_hood_victor(make_unique<frc::VictorSP>(8)); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 665 | superstructure_writer.set_intake_joint_victor( |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 666 | make_unique<frc::VictorSP>(2)); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 667 | superstructure_writer.set_intake_roller_falcon( |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 668 | make_unique<::ctre::phoenix6::hardware::TalonFX>(0)); |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 669 | superstructure_writer.set_turret_victor(make_unique<frc::VictorSP>(7)); |
Ravago Jones | 3dda560 | 2021-03-10 00:33:13 -0800 | [diff] [blame] | 670 | superstructure_writer.set_feeder_falcon( |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 671 | make_unique<ctre::phoenix6::hardware::TalonFX>(1)); |
milind-u | 9aa62a8 | 2021-10-03 20:43:19 -0700 | [diff] [blame] | 672 | superstructure_writer.set_washing_machine_control_panel_victor( |
| 673 | make_unique<frc::VictorSP>(6)); |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 674 | superstructure_writer.set_accelerator_left_falcon( |
| 675 | make_unique<::frc::TalonFX>(5)); |
| 676 | superstructure_writer.set_accelerator_right_falcon( |
| 677 | make_unique<::frc::TalonFX>(4)); |
Austin Schuh | 0ad31d7 | 2021-03-06 17:07:04 -0800 | [diff] [blame] | 678 | superstructure_writer.set_finisher_falcon0(make_unique<::frc::TalonFX>(9)); |
| 679 | superstructure_writer.set_finisher_falcon1(make_unique<::frc::TalonFX>(3)); |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 680 | // TODO: check port |
Tyler Chatow | 1039e43 | 2020-02-28 21:37:50 -0800 | [diff] [blame] | 681 | superstructure_writer.set_climber_falcon( |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 682 | make_unique<::ctre::phoenix6::hardware::TalonFX>(2)); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 683 | |
| 684 | AddLoop(&output_event_loop); |
| 685 | |
| 686 | RunLoops(); |
| 687 | } |
| 688 | }; |
| 689 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 690 | } // namespace y2020::wpilib |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 691 | |
| 692 | AOS_ROBOT_CLASS(::y2020::wpilib::WPILibRobot); |