Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <array> |
| 4 | #include <chrono> |
| 5 | #include <cinttypes> |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 6 | #include <cstdio> |
| 7 | #include <cstring> |
| 8 | #include <functional> |
| 9 | #include <memory> |
| 10 | #include <mutex> |
| 11 | #include <thread> |
| 12 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 13 | #include "frc971/wpilib/ahal/AnalogInput.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 14 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 15 | #include "frc971/wpilib/ahal/Encoder.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 16 | #include "frc971/wpilib/ahal/TalonFX.h" |
| 17 | #include "frc971/wpilib/ahal/VictorSP.h" |
| 18 | #undef ERROR |
| 19 | |
| 20 | #include "ctre/phoenix/cci/Diagnostics_CCI.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 21 | |
| 22 | #include "aos/commonmath.h" |
| 23 | #include "aos/containers/sized_array.h" |
| 24 | #include "aos/events/event_loop.h" |
| 25 | #include "aos/events/shm_event_loop.h" |
| 26 | #include "aos/init.h" |
| 27 | #include "aos/logging/logging.h" |
| 28 | #include "aos/realtime.h" |
| 29 | #include "aos/time/time.h" |
| 30 | #include "aos/util/log_interval.h" |
| 31 | #include "aos/util/phased_loop.h" |
| 32 | #include "aos/util/wrapping_counter.h" |
| 33 | #include "frc971/autonomous/auto_mode_generated.h" |
| 34 | #include "frc971/can_configuration_generated.h" |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 35 | #include "frc971/constants/constants_sender_lib.h" |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 36 | #include "frc971/control_loops/drivetrain/drivetrain_can_position_static.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 37 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
| 38 | #include "frc971/input/robot_state_generated.h" |
| 39 | #include "frc971/queues/gyro_generated.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 40 | #include "frc971/wpilib/buffered_pcm.h" |
| 41 | #include "frc971/wpilib/buffered_solenoid.h" |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 42 | #include "frc971/wpilib/can_drivetrain_writer.h" |
| 43 | #include "frc971/wpilib/can_sensor_reader.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 44 | #include "frc971/wpilib/dma.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 45 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 46 | #include "frc971/wpilib/generic_can_writer.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 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" |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 52 | #include "frc971/wpilib/talonfx.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 53 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 54 | #include "y2024/constants.h" |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 55 | #include "y2024/constants/constants_generated.h" |
| 56 | #include "y2024/control_loops/superstructure/superstructure_can_position_static.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 57 | #include "y2024/control_loops/superstructure/superstructure_output_generated.h" |
| 58 | #include "y2024/control_loops/superstructure/superstructure_position_generated.h" |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 59 | #include "y2024/control_loops/superstructure/superstructure_position_static.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 60 | |
| 61 | DEFINE_bool(ctre_diag_server, false, |
| 62 | "If true, enable the diagnostics server for interacting with " |
| 63 | "devices on the CAN bus using Phoenix Tuner"); |
| 64 | |
| 65 | using ::aos::monotonic_clock; |
| 66 | using ::frc971::CANConfiguration; |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 67 | using ::frc971::control_loops::drivetrain::CANPositionStatic; |
| 68 | using ::frc971::wpilib::TalonFX; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 69 | using ::y2024::constants::Values; |
| 70 | namespace superstructure = ::y2024::control_loops::superstructure; |
| 71 | namespace drivetrain = ::y2024::control_loops::drivetrain; |
| 72 | namespace chrono = ::std::chrono; |
| 73 | using std::make_unique; |
| 74 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 75 | namespace y2024::wpilib { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 76 | namespace { |
| 77 | |
| 78 | constexpr double kMaxBringupPower = 12.0; |
| 79 | |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 80 | double climber_pot_translate(double voltage) { |
Maxwell Henderson | ff831f5 | 2024-02-21 13:51:59 -0800 | [diff] [blame] | 81 | return -1 * voltage * Values::kClimberPotMetersPerVolt(); |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Filip Kujawa | d75252a | 2024-02-10 16:54:35 -0800 | [diff] [blame] | 84 | double extend_pot_translate(double voltage) { |
| 85 | return voltage * Values::kExtendPotMetersPerVolt(); |
| 86 | } |
| 87 | |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 88 | double catapult_pot_translate(double voltage) { |
| 89 | return voltage * Values::kCatapultPotRadiansPerVolt(); |
| 90 | } |
| 91 | |
| 92 | double turret_pot_translate(double voltage) { |
| 93 | return voltage * Values::kTurretPotRadiansPerVolt(); |
| 94 | } |
| 95 | |
| 96 | double altitude_pot_translate(double voltage) { |
Maxwell Henderson | 0b6fed6 | 2024-02-23 11:05:09 -0800 | [diff] [blame] | 97 | return -1 * voltage * Values::kAltitudePotRadiansPerVolt(); |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 100 | double drivetrain_velocity_translate(double in) { |
| 101 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
| 102 | (2.0 * M_PI)) * |
| 103 | Values::kDrivetrainEncoderRatio() * |
| 104 | control_loops::drivetrain::kWheelRadius; |
| 105 | } |
| 106 | |
| 107 | constexpr double kMaxFastEncoderPulsesPerSecond = std::max({ |
| 108 | Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
Maxwell Henderson | 1de1549 | 2024-02-07 11:09:47 -0800 | [diff] [blame] | 109 | Values::kMaxIntakePivotEncoderPulsesPerSecond(), |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 110 | Values::kMaxClimberEncoderPulsesPerSecond(), |
Filip Kujawa | d75252a | 2024-02-10 16:54:35 -0800 | [diff] [blame] | 111 | Values::kMaxExtendEncoderPulsesPerSecond(), |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 112 | Values::kMaxCatapultEncoderPulsesPerSecond(), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 113 | }); |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 114 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 115 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 116 | "fast encoders are too fast"); |
| 117 | |
| 118 | } // namespace |
| 119 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 120 | // Class to send position messages with sensor readings to our loops. |
| 121 | class SensorReader : public ::frc971::wpilib::SensorReader { |
| 122 | public: |
| 123 | SensorReader(::aos::ShmEventLoop *event_loop, |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 124 | const Constants *robot_constants) |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 125 | : ::frc971::wpilib::SensorReader(event_loop), |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 126 | robot_constants_(CHECK_NOTNULL(robot_constants)), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 127 | auto_mode_sender_( |
| 128 | event_loop->MakeSender<::frc971::autonomous::AutonomousMode>( |
| 129 | "/autonomous")), |
| 130 | superstructure_position_sender_( |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 131 | event_loop->MakeSender<superstructure::PositionStatic>( |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 132 | "/superstructure")), |
| 133 | drivetrain_position_sender_( |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 134 | event_loop->MakeSender< |
| 135 | ::frc971::control_loops::drivetrain::PositionStatic>( |
| 136 | "/drivetrain")), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 137 | gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>( |
Maxwell Henderson | afc5c6d | 2024-02-20 15:34:13 -0800 | [diff] [blame] | 138 | "/drivetrain")) { |
| 139 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 140 | event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0})); |
| 141 | }; |
| 142 | void Start() override { |
| 143 | AddToDMA(&imu_yaw_rate_reader_); |
| 144 | AddToDMA(&turret_encoder_.reader()); |
| 145 | AddToDMA(&altitude_encoder_.reader()); |
| 146 | } |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 147 | |
| 148 | // Auto mode switches. |
| 149 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
| 150 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 151 | } |
| 152 | |
| 153 | void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) { |
| 154 | imu_yaw_rate_input_ = ::std::move(sensor); |
| 155 | imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get()); |
| 156 | } |
| 157 | |
| 158 | void RunIteration() override { |
| 159 | { |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 160 | aos::Sender<superstructure::PositionStatic>::StaticBuilder builder = |
| 161 | superstructure_position_sender_.MakeStaticBuilder(); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 162 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 163 | CopyPosition(intake_pivot_encoder_, builder->add_intake_pivot(), |
| 164 | Values::kIntakePivotEncoderCountsPerRevolution(), |
Niko Sohmers | 74b0ad5 | 2024-02-03 18:00:31 -0800 | [diff] [blame] | 165 | Values::kIntakePivotEncoderRatio(), /* reversed: */ false); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 166 | |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 167 | CopyPosition(climber_encoder_, builder->add_climber(), |
| 168 | Values::kClimberEncoderCountsPerRevolution(), |
Maxwell Henderson | 9e3b310 | 2024-02-20 15:35:02 -0800 | [diff] [blame] | 169 | Values::kClimberEncoderMetersPerRadian(), |
| 170 | climber_pot_translate, false, |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 171 | robot_constants_->robot() |
| 172 | ->climber_constants() |
| 173 | ->potentiometer_offset()); |
| 174 | |
Filip Kujawa | d75252a | 2024-02-10 16:54:35 -0800 | [diff] [blame] | 175 | CopyPosition(extend_encoder_, builder->add_extend(), |
Maxwell Henderson | 9e3b310 | 2024-02-20 15:35:02 -0800 | [diff] [blame] | 176 | Values::kExtendEncoderCountsPerRevolution(), |
| 177 | Values::kExtendEncoderMetersPerRadian(), |
Filip Kujawa | d75252a | 2024-02-10 16:54:35 -0800 | [diff] [blame] | 178 | extend_pot_translate, true, |
| 179 | robot_constants_->robot() |
| 180 | ->extend_constants() |
| 181 | ->potentiometer_offset()); |
| 182 | |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 183 | CopyPosition(catapult_encoder_, builder->add_catapult(), |
| 184 | Values::kCatapultEncoderCountsPerRevolution(), |
| 185 | Values::kCatapultEncoderRatio(), catapult_pot_translate, |
| 186 | true, |
| 187 | robot_constants_->robot() |
| 188 | ->catapult_constants() |
| 189 | ->potentiometer_offset()); |
| 190 | |
| 191 | CopyPosition(turret_encoder_, builder->add_turret(), |
| 192 | Values::kTurretEncoderCountsPerRevolution(), |
| 193 | Values::kTurretEncoderRatio(), turret_pot_translate, true, |
| 194 | robot_constants_->robot() |
| 195 | ->turret_constants() |
| 196 | ->potentiometer_offset()); |
| 197 | |
| 198 | CopyPosition(altitude_encoder_, builder->add_altitude(), |
| 199 | Values::kAltitudeEncoderCountsPerRevolution(), |
| 200 | Values::kAltitudeEncoderRatio(), altitude_pot_translate, |
| 201 | true, |
| 202 | robot_constants_->robot() |
| 203 | ->altitude_constants() |
| 204 | ->potentiometer_offset()); |
| 205 | |
Niko Sohmers | ed7ffc4 | 2024-02-03 16:05:19 -0800 | [diff] [blame] | 206 | builder->set_transfer_beambreak(transfer_beam_break_->Get()); |
Maxwell Henderson | 576b0c9 | 2024-02-23 17:10:48 -0800 | [diff] [blame^] | 207 | builder->set_catapult_beambreak(catapult_beam_break_->Get()); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 208 | builder.CheckOk(builder.Send()); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 211 | SendDrivetrainPosition(drivetrain_position_sender_.MakeStaticBuilder(), |
| 212 | drivetrain_velocity_translate, |
Maxwell Henderson | 9e3b310 | 2024-02-20 15:35:02 -0800 | [diff] [blame] | 213 | constants::Values::DrivetrainEncoderToMeters, true, |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 214 | false); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 215 | |
| 216 | { |
| 217 | auto builder = gyro_sender_.MakeBuilder(); |
| 218 | ::frc971::sensors::GyroReading::Builder gyro_reading_builder = |
| 219 | builder.MakeBuilder<::frc971::sensors::GyroReading>(); |
| 220 | // +/- 2000 deg / sec |
| 221 | constexpr double kMaxVelocity = 4000; // degrees / second |
| 222 | constexpr double kVelocityRadiansPerSecond = |
| 223 | kMaxVelocity / 360 * (2.0 * M_PI); |
| 224 | |
| 225 | // Only part of the full range is used to prevent being 100% on or off. |
| 226 | constexpr double kScaledRangeLow = 0.1; |
| 227 | constexpr double kScaledRangeHigh = 0.9; |
| 228 | |
| 229 | constexpr double kPWMFrequencyHz = 200; |
| 230 | double velocity_duty_cycle = |
| 231 | imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz; |
| 232 | |
| 233 | constexpr double kDutyCycleScale = |
| 234 | 1 / (kScaledRangeHigh - kScaledRangeLow); |
| 235 | // scale from 0.1 - 0.9 to 0 - 1 |
| 236 | double rescaled_velocity_duty_cycle = |
| 237 | (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale; |
| 238 | |
| 239 | if (!std::isnan(rescaled_velocity_duty_cycle)) { |
| 240 | gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) * |
| 241 | kVelocityRadiansPerSecond); |
| 242 | } |
| 243 | builder.CheckOk(builder.Send(gyro_reading_builder.Finish())); |
| 244 | } |
| 245 | |
| 246 | { |
| 247 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 248 | |
| 249 | uint32_t mode = 0; |
| 250 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 251 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 252 | mode |= 1 << i; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | auto auto_mode_builder = |
| 257 | builder.MakeBuilder<frc971::autonomous::AutonomousMode>(); |
| 258 | |
| 259 | auto_mode_builder.add_mode(mode); |
| 260 | |
| 261 | builder.CheckOk(builder.Send(auto_mode_builder.Finish())); |
| 262 | } |
| 263 | } |
| 264 | |
Maxwell Henderson | 6291783 | 2024-02-07 11:08:11 -0800 | [diff] [blame] | 265 | void set_intake_pivot(::std::unique_ptr<frc::Encoder> encoder, |
Niko Sohmers | 74b0ad5 | 2024-02-03 18:00:31 -0800 | [diff] [blame] | 266 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 267 | fast_encoder_filter_.Add(encoder.get()); |
| 268 | intake_pivot_encoder_.set_encoder(::std::move(encoder)); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 269 | intake_pivot_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Niko Sohmers | ed7ffc4 | 2024-02-03 16:05:19 -0800 | [diff] [blame] | 272 | void set_transfer_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) { |
| 273 | transfer_beam_break_ = ::std::move(sensor); |
| 274 | } |
| 275 | |
Maxwell Henderson | 576b0c9 | 2024-02-23 17:10:48 -0800 | [diff] [blame^] | 276 | void set_catapult_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) { |
| 277 | catapult_beam_break_ = ::std::move(sensor); |
| 278 | } |
| 279 | |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 280 | void set_climber(::std::unique_ptr<frc::Encoder> encoder, |
| 281 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm, |
| 282 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 283 | fast_encoder_filter_.Add(encoder.get()); |
| 284 | climber_encoder_.set_encoder(::std::move(encoder)); |
| 285 | climber_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 286 | climber_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 287 | } |
| 288 | |
Filip Kujawa | d75252a | 2024-02-10 16:54:35 -0800 | [diff] [blame] | 289 | void set_extend(::std::unique_ptr<frc::Encoder> encoder, |
| 290 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm, |
| 291 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 292 | fast_encoder_filter_.Add(encoder.get()); |
| 293 | extend_encoder_.set_encoder(::std::move(encoder)); |
| 294 | extend_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 295 | extend_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 296 | } |
| 297 | |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 298 | void set_catapult(::std::unique_ptr<frc::Encoder> encoder, |
| 299 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm, |
| 300 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 301 | fast_encoder_filter_.Add(encoder.get()); |
| 302 | catapult_encoder_.set_encoder(::std::move(encoder)); |
| 303 | catapult_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 304 | catapult_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 305 | } |
| 306 | |
| 307 | void set_turret(::std::unique_ptr<frc::Encoder> encoder, |
| 308 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm, |
| 309 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 310 | fast_encoder_filter_.Add(encoder.get()); |
| 311 | turret_encoder_.set_encoder(::std::move(encoder)); |
| 312 | turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 313 | turret_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 314 | } |
| 315 | |
| 316 | void set_altitude(::std::unique_ptr<frc::Encoder> encoder, |
| 317 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm, |
| 318 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 319 | fast_encoder_filter_.Add(encoder.get()); |
| 320 | altitude_encoder_.set_encoder(::std::move(encoder)); |
| 321 | altitude_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 322 | altitude_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 323 | } |
| 324 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 325 | private: |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 326 | const Constants *robot_constants_; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 327 | |
| 328 | aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_; |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 329 | aos::Sender<superstructure::PositionStatic> superstructure_position_sender_; |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 330 | aos::Sender<frc971::control_loops::drivetrain::PositionStatic> |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 331 | drivetrain_position_sender_; |
| 332 | ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_; |
| 333 | |
| 334 | std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_; |
| 335 | |
Maxwell Henderson | 576b0c9 | 2024-02-23 17:10:48 -0800 | [diff] [blame^] | 336 | std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_, transfer_beam_break_, |
| 337 | catapult_beam_break_; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 338 | |
Niko Sohmers | 74b0ad5 | 2024-02-03 18:00:31 -0800 | [diff] [blame] | 339 | frc971::wpilib::AbsoluteEncoder intake_pivot_encoder_; |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 340 | frc971::wpilib::AbsoluteEncoderAndPotentiometer climber_encoder_, |
Maxwell Henderson | afc5c6d | 2024-02-20 15:34:13 -0800 | [diff] [blame] | 341 | catapult_encoder_, extend_encoder_; |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 342 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 343 | frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_; |
Maxwell Henderson | afc5c6d | 2024-02-20 15:34:13 -0800 | [diff] [blame] | 344 | |
| 345 | frc971::wpilib::DMAAbsoluteEncoderAndPotentiometer turret_encoder_, |
| 346 | altitude_encoder_; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 347 | }; |
| 348 | |
Maxwell Henderson | abcc92a | 2024-02-21 19:06:26 -0800 | [diff] [blame] | 349 | class SuperstructurePWMWriter |
| 350 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
| 351 | public: |
| 352 | SuperstructurePWMWriter(aos::EventLoop *event_loop) |
| 353 | : frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 354 | event_loop, "/superstructure") {} |
| 355 | |
| 356 | void set_catapult_kraken_one(::std::unique_ptr<::frc::TalonFX> t) { |
| 357 | catapult_kraken_one_ = ::std::move(t); |
| 358 | } |
| 359 | void set_catapult_kraken_two(::std::unique_ptr<::frc::TalonFX> t) { |
| 360 | catapult_kraken_two_ = ::std::move(t); |
| 361 | } |
| 362 | |
| 363 | private: |
| 364 | void Stop() override { |
| 365 | AOS_LOG(WARNING, "Superstructure output too old.\n"); |
| 366 | catapult_kraken_one_->SetDisabled(); |
| 367 | catapult_kraken_two_->SetDisabled(); |
| 368 | } |
| 369 | |
| 370 | void Write(const superstructure::Output &output) override { |
| 371 | WritePwm(output.catapult_voltage(), catapult_kraken_one_.get()); |
Maxwell Henderson | 12d427b | 2024-02-22 13:57:45 -0800 | [diff] [blame] | 372 | WritePwm(output.catapult_voltage(), catapult_kraken_two_.get()); |
Maxwell Henderson | abcc92a | 2024-02-21 19:06:26 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | template <typename T> |
| 376 | static void WritePwm(const double voltage, T *motor) { |
| 377 | motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / |
| 378 | 12.0); |
| 379 | } |
| 380 | ::std::unique_ptr<::frc::TalonFX> catapult_kraken_one_, catapult_kraken_two_; |
| 381 | }; |
| 382 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 383 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 384 | public: |
| 385 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 386 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 387 | frc::Encoder::k4X); |
| 388 | } |
| 389 | |
| 390 | void Run() override { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 391 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 392 | aos::configuration::ReadConfig("aos_config.json"); |
| 393 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 394 | frc971::constants::WaitForConstants<y2024::Constants>(&config.message()); |
| 395 | |
| 396 | ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message()); |
| 397 | frc971::constants::ConstantsFetcher<Constants> constants_fetcher( |
| 398 | &constant_fetcher_event_loop); |
| 399 | const Constants *robot_constants = &constants_fetcher.constants(); |
| 400 | |
Maxwell Henderson | ca7c9b6 | 2024-02-21 13:20:09 -0800 | [diff] [blame] | 401 | AddLoop(&constant_fetcher_event_loop); |
| 402 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 403 | // Thread 1. |
| 404 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
| 405 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 406 | &joystick_sender_event_loop); |
| 407 | AddLoop(&joystick_sender_event_loop); |
| 408 | |
| 409 | // Thread 2. |
| 410 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 411 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 412 | AddLoop(&pdp_fetcher_event_loop); |
| 413 | |
| 414 | // Thread 3. |
| 415 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 416 | SensorReader sensor_reader(&sensor_reader_event_loop, robot_constants); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 417 | sensor_reader.set_pwm_trigger(true); |
Maxwell Henderson | 9e3b310 | 2024-02-20 15:35:02 -0800 | [diff] [blame] | 418 | sensor_reader.set_drivetrain_left_encoder( |
| 419 | std::make_unique<frc::Encoder>(6, 7)); |
| 420 | sensor_reader.set_drivetrain_right_encoder( |
| 421 | std::make_unique<frc::Encoder>(8, 9)); |
| 422 | sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(25)); |
| 423 | sensor_reader.set_intake_pivot(make_encoder(3), |
| 424 | make_unique<frc::DigitalInput>(3)); |
| 425 | sensor_reader.set_transfer_beambreak(make_unique<frc::DigitalInput>(23)); |
Maxwell Henderson | 576b0c9 | 2024-02-23 17:10:48 -0800 | [diff] [blame^] | 426 | sensor_reader.set_catapult_beambreak(make_unique<frc::DigitalInput>(24)); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 427 | |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 428 | sensor_reader.set_climber(make_encoder(5), |
| 429 | make_unique<frc::DigitalInput>(5), |
| 430 | make_unique<frc::AnalogInput>(5)); |
Maxwell Henderson | 9e3b310 | 2024-02-20 15:35:02 -0800 | [diff] [blame] | 431 | sensor_reader.set_extend(make_encoder(4), make_unique<frc::DigitalInput>(4), |
| 432 | make_unique<frc::AnalogInput>(4)); |
| 433 | sensor_reader.set_catapult(make_encoder(0), |
| 434 | make_unique<frc::DigitalInput>(0), |
| 435 | make_unique<frc::AnalogInput>(0)); |
| 436 | sensor_reader.set_turret(make_encoder(2), make_unique<frc::DigitalInput>(2), |
| 437 | make_unique<frc::AnalogInput>(2)); |
| 438 | sensor_reader.set_altitude(make_encoder(1), |
| 439 | make_unique<frc::DigitalInput>(1), |
| 440 | make_unique<frc::AnalogInput>(1)); |
Filip Kujawa | d75252a | 2024-02-10 16:54:35 -0800 | [diff] [blame] | 441 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 442 | AddLoop(&sensor_reader_event_loop); |
| 443 | |
| 444 | // Thread 4. |
| 445 | // Set up CAN. |
| 446 | if (!FLAGS_ctre_diag_server) { |
| 447 | c_Phoenix_Diagnostics_SetSecondsToStart(-1); |
| 448 | c_Phoenix_Diagnostics_Dispose(); |
| 449 | } |
| 450 | |
Niko Sohmers | 8427395 | 2024-02-14 18:40:55 -0800 | [diff] [blame] | 451 | std::vector<ctre::phoenix6::BaseStatusSignal *> canivore_signal_registry; |
| 452 | std::vector<ctre::phoenix6::BaseStatusSignal *> rio_signal_registry; |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 453 | |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 454 | const CurrentLimits *current_limits = |
| 455 | robot_constants->common()->current_limits(); |
| 456 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 457 | std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>( |
Maxwell Henderson | 7be07d5 | 2024-02-20 07:59:16 -0800 | [diff] [blame] | 458 | 2, false, "Drivetrain Bus", &canivore_signal_registry, |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 459 | current_limits->drivetrain_supply_current_limit(), |
| 460 | current_limits->drivetrain_stator_current_limit()); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 461 | std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>( |
Niko Sohmers | 8427395 | 2024-02-14 18:40:55 -0800 | [diff] [blame] | 462 | 1, false, "Drivetrain Bus", &canivore_signal_registry, |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 463 | current_limits->drivetrain_supply_current_limit(), |
| 464 | current_limits->drivetrain_stator_current_limit()); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 465 | std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>( |
Maxwell Henderson | 7be07d5 | 2024-02-20 07:59:16 -0800 | [diff] [blame] | 466 | 4, false, "Drivetrain Bus", &canivore_signal_registry, |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 467 | current_limits->drivetrain_supply_current_limit(), |
| 468 | current_limits->drivetrain_stator_current_limit()); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 469 | std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>( |
Maxwell Henderson | 7be07d5 | 2024-02-20 07:59:16 -0800 | [diff] [blame] | 470 | 5, false, "Drivetrain Bus", &canivore_signal_registry, |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 471 | current_limits->drivetrain_supply_current_limit(), |
| 472 | current_limits->drivetrain_stator_current_limit()); |
| 473 | std::shared_ptr<TalonFX> intake_pivot = std::make_shared<TalonFX>( |
Maxwell Henderson | 30ea8b3 | 2024-02-22 16:14:05 -0800 | [diff] [blame] | 474 | 6, false, "Drivetrain Bus", &canivore_signal_registry, |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 475 | current_limits->intake_pivot_stator_current_limit(), |
| 476 | current_limits->intake_pivot_supply_current_limit()); |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 477 | std::shared_ptr<TalonFX> altitude = std::make_shared<TalonFX>( |
Maxwell Henderson | 30ea8b3 | 2024-02-22 16:14:05 -0800 | [diff] [blame] | 478 | 9, false, "Drivetrain Bus", &canivore_signal_registry, |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 479 | current_limits->altitude_stator_current_limit(), |
| 480 | current_limits->altitude_supply_current_limit()); |
| 481 | std::shared_ptr<TalonFX> turret = std::make_shared<TalonFX>( |
Maxwell Henderson | 30ea8b3 | 2024-02-22 16:14:05 -0800 | [diff] [blame] | 482 | 3, false, "Drivetrain Bus", &canivore_signal_registry, |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 483 | current_limits->turret_stator_current_limit(), |
| 484 | current_limits->turret_supply_current_limit()); |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 485 | std::shared_ptr<TalonFX> intake_roller = std::make_shared<TalonFX>( |
Maxwell Henderson | 7be07d5 | 2024-02-20 07:59:16 -0800 | [diff] [blame] | 486 | 8, false, "rio", &rio_signal_registry, |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 487 | current_limits->intake_roller_stator_current_limit(), |
| 488 | current_limits->intake_roller_supply_current_limit()); |
Maxwell Henderson | 7b7754b | 2024-02-22 16:50:35 -0800 | [diff] [blame] | 489 | std::shared_ptr<TalonFX> retention_roller = std::make_shared<TalonFX>( |
| 490 | 10, false, "rio", &rio_signal_registry, |
| 491 | current_limits->intake_roller_stator_current_limit(), |
| 492 | current_limits->intake_roller_supply_current_limit()); |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 493 | std::shared_ptr<TalonFX> transfer_roller = std::make_shared<TalonFX>( |
Maxwell Henderson | 7be07d5 | 2024-02-20 07:59:16 -0800 | [diff] [blame] | 494 | 9, false, "rio", &rio_signal_registry, |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 495 | current_limits->transfer_roller_stator_current_limit(), |
| 496 | current_limits->transfer_roller_supply_current_limit()); |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 497 | std::shared_ptr<TalonFX> climber = std::make_shared<TalonFX>( |
Maxwell Henderson | 30ea8b3 | 2024-02-22 16:14:05 -0800 | [diff] [blame] | 498 | 7, false, "rio", &rio_signal_registry, |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 499 | current_limits->climber_stator_current_limit(), |
| 500 | current_limits->climber_supply_current_limit()); |
Filip Kujawa | d75252a | 2024-02-10 16:54:35 -0800 | [diff] [blame] | 501 | std::shared_ptr<TalonFX> extend = std::make_shared<TalonFX>( |
Maxwell Henderson | 30ea8b3 | 2024-02-22 16:14:05 -0800 | [diff] [blame] | 502 | 11, false, "rio", &rio_signal_registry, |
Filip Kujawa | d75252a | 2024-02-10 16:54:35 -0800 | [diff] [blame] | 503 | current_limits->extend_stator_current_limit(), |
| 504 | current_limits->extend_supply_current_limit()); |
Filip Kujawa | d75252a | 2024-02-10 16:54:35 -0800 | [diff] [blame] | 505 | std::shared_ptr<TalonFX> extend_roller = std::make_shared<TalonFX>( |
Maxwell Henderson | 7b7754b | 2024-02-22 16:50:35 -0800 | [diff] [blame] | 506 | 12, false, "rio", &rio_signal_registry, |
Filip Kujawa | d75252a | 2024-02-10 16:54:35 -0800 | [diff] [blame] | 507 | current_limits->extend_roller_stator_current_limit(), |
| 508 | current_limits->extend_roller_supply_current_limit()); |
| 509 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 510 | ctre::phoenix::platform::can::CANComm_SetRxSchedPriority( |
| 511 | constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus"); |
| 512 | ctre::phoenix::platform::can::CANComm_SetTxSchedPriority( |
| 513 | constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus"); |
| 514 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 515 | ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message()); |
| 516 | can_sensor_reader_event_loop.set_name("CANSensorReader"); |
| 517 | |
Niko Sohmers | 8427395 | 2024-02-14 18:40:55 -0800 | [diff] [blame] | 518 | ::aos::ShmEventLoop rio_sensor_reader_event_loop(&config.message()); |
| 519 | rio_sensor_reader_event_loop.set_name("RioSensorReader"); |
| 520 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 521 | // Creating list of talonfx for CANSensorReader |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 522 | std::vector<std::shared_ptr<TalonFX>> drivetrain_talonfxs; |
Niko Sohmers | 8427395 | 2024-02-14 18:40:55 -0800 | [diff] [blame] | 523 | std::vector<std::shared_ptr<TalonFX>> canivore_talonfxs; |
| 524 | std::vector<std::shared_ptr<TalonFX>> rio_talonfxs; |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 525 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 526 | for (auto talonfx : {right_front, right_back, left_front, left_back}) { |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 527 | drivetrain_talonfxs.push_back(talonfx); |
Niko Sohmers | 8427395 | 2024-02-14 18:40:55 -0800 | [diff] [blame] | 528 | canivore_talonfxs.push_back(talonfx); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 531 | for (auto talonfx : {intake_pivot, altitude, turret}) { |
Niko Sohmers | 8427395 | 2024-02-14 18:40:55 -0800 | [diff] [blame] | 532 | canivore_talonfxs.push_back(talonfx); |
| 533 | } |
| 534 | |
Maxwell Henderson | 7b7754b | 2024-02-22 16:50:35 -0800 | [diff] [blame] | 535 | for (auto talonfx : {intake_roller, transfer_roller, climber, extend, |
| 536 | extend_roller, retention_roller}) { |
Niko Sohmers | 8427395 | 2024-02-14 18:40:55 -0800 | [diff] [blame] | 537 | rio_talonfxs.push_back(talonfx); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic> |
| 541 | drivetrain_can_position_sender = |
| 542 | can_sensor_reader_event_loop.MakeSender< |
| 543 | frc971::control_loops::drivetrain::CANPositionStatic>( |
| 544 | "/drivetrain"); |
| 545 | |
| 546 | aos::Sender<y2024::control_loops::superstructure::CANPositionStatic> |
| 547 | superstructure_can_position_sender = |
| 548 | can_sensor_reader_event_loop.MakeSender< |
| 549 | y2024::control_loops::superstructure::CANPositionStatic>( |
Niko Sohmers | 8427395 | 2024-02-14 18:40:55 -0800 | [diff] [blame] | 550 | "/superstructure/canivore"); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 551 | |
Niko Sohmers | 8427395 | 2024-02-14 18:40:55 -0800 | [diff] [blame] | 552 | frc971::wpilib::CANSensorReader canivore_can_sensor_reader( |
| 553 | &can_sensor_reader_event_loop, std::move(canivore_signal_registry), |
| 554 | canivore_talonfxs, |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 555 | [drivetrain_talonfxs, &intake_pivot, &altitude, &turret, |
| 556 | &drivetrain_can_position_sender, &superstructure_can_position_sender]( |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 557 | ctre::phoenix::StatusCode status) { |
| 558 | aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>:: |
| 559 | StaticBuilder drivetrain_can_builder = |
| 560 | drivetrain_can_position_sender.MakeStaticBuilder(); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 561 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 562 | auto drivetrain_falcon_vector = |
Maxwell Henderson | 563efed | 2024-02-17 21:11:33 -0800 | [diff] [blame] | 563 | CHECK_NOTNULL(drivetrain_can_builder->add_talonfxs()); |
Maxwell Henderson | 9116e5b | 2024-01-21 12:14:26 -0800 | [diff] [blame] | 564 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 565 | for (auto talonfx : drivetrain_talonfxs) { |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 566 | talonfx->SerializePosition( |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 567 | drivetrain_falcon_vector->emplace_back(), |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 568 | control_loops::drivetrain::kHighOutputRatio); |
| 569 | } |
| 570 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 571 | drivetrain_can_builder->set_timestamp( |
| 572 | drivetrain_talonfxs.front()->GetTimestamp()); |
| 573 | drivetrain_can_builder->set_status(static_cast<int>(status)); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 574 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 575 | drivetrain_can_builder.CheckOk(drivetrain_can_builder.Send()); |
| 576 | |
| 577 | aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>:: |
| 578 | StaticBuilder superstructure_can_builder = |
| 579 | superstructure_can_position_sender.MakeStaticBuilder(); |
| 580 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 581 | intake_pivot->SerializePosition( |
| 582 | superstructure_can_builder->add_intake_pivot(), |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 583 | control_loops::drivetrain::kHighOutputRatio); |
| 584 | altitude->SerializePosition( |
| 585 | superstructure_can_builder->add_altitude(), |
| 586 | control_loops::drivetrain::kHighOutputRatio); |
| 587 | turret->SerializePosition( |
| 588 | superstructure_can_builder->add_turret(), |
| 589 | control_loops::drivetrain::kHighOutputRatio); |
Niko Sohmers | 8427395 | 2024-02-14 18:40:55 -0800 | [diff] [blame] | 590 | |
| 591 | superstructure_can_builder->set_timestamp( |
| 592 | intake_pivot->GetTimestamp()); |
| 593 | superstructure_can_builder->set_status(static_cast<int>(status)); |
| 594 | superstructure_can_builder.CheckOk(superstructure_can_builder.Send()); |
| 595 | }); |
| 596 | |
| 597 | aos::Sender<y2024::control_loops::superstructure::CANPositionStatic> |
| 598 | superstructure_rio_position_sender = |
| 599 | rio_sensor_reader_event_loop.MakeSender< |
| 600 | y2024::control_loops::superstructure::CANPositionStatic>( |
| 601 | "/superstructure/rio"); |
| 602 | |
| 603 | frc971::wpilib::CANSensorReader rio_can_sensor_reader( |
| 604 | &rio_sensor_reader_event_loop, std::move(rio_signal_registry), |
| 605 | rio_talonfxs, |
Filip Kujawa | d75252a | 2024-02-10 16:54:35 -0800 | [diff] [blame] | 606 | [&intake_roller, &transfer_roller, &climber, &extend, &extend_roller, |
Maxwell Henderson | 7b7754b | 2024-02-22 16:50:35 -0800 | [diff] [blame] | 607 | &retention_roller, &superstructure_rio_position_sender]( |
Niko Sohmers | 8427395 | 2024-02-14 18:40:55 -0800 | [diff] [blame] | 608 | ctre::phoenix::StatusCode status) { |
| 609 | aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>:: |
| 610 | StaticBuilder superstructure_can_builder = |
| 611 | superstructure_rio_position_sender.MakeStaticBuilder(); |
| 612 | |
| 613 | intake_roller->SerializePosition( |
| 614 | superstructure_can_builder->add_intake_roller(), |
Filip Kujawa | ce385c3 | 2024-02-16 10:39:49 -0800 | [diff] [blame] | 615 | constants::Values::kIntakeRollerOutputRatio); |
Niko Sohmers | ed7ffc4 | 2024-02-03 16:05:19 -0800 | [diff] [blame] | 616 | transfer_roller->SerializePosition( |
| 617 | superstructure_can_builder->add_transfer_roller(), |
Filip Kujawa | ce385c3 | 2024-02-16 10:39:49 -0800 | [diff] [blame] | 618 | constants::Values::kIntakeRollerOutputRatio); |
| 619 | climber->SerializePosition(superstructure_can_builder->add_climber(), |
| 620 | superstructure::climber::kOutputRatio); |
Filip Kujawa | d75252a | 2024-02-10 16:54:35 -0800 | [diff] [blame] | 621 | extend->SerializePosition(superstructure_can_builder->add_extend(), |
| 622 | superstructure::extend::kOutputRatio); |
| 623 | extend_roller->SerializePosition( |
| 624 | superstructure_can_builder->add_extend_roller(), |
Filip Kujawa | ce385c3 | 2024-02-16 10:39:49 -0800 | [diff] [blame] | 625 | constants::Values::kExtendRollerOutputRatio); |
Maxwell Henderson | 7b7754b | 2024-02-22 16:50:35 -0800 | [diff] [blame] | 626 | retention_roller->SerializePosition( |
| 627 | superstructure_can_builder->add_retention_roller(), 1.0); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 628 | |
| 629 | superstructure_can_builder->set_timestamp( |
| 630 | intake_roller->GetTimestamp()); |
| 631 | superstructure_can_builder->set_status(static_cast<int>(status)); |
| 632 | superstructure_can_builder.CheckOk(superstructure_can_builder.Send()); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 633 | }); |
| 634 | |
| 635 | AddLoop(&can_sensor_reader_event_loop); |
Niko Sohmers | 8427395 | 2024-02-14 18:40:55 -0800 | [diff] [blame] | 636 | AddLoop(&rio_sensor_reader_event_loop); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 637 | |
| 638 | // Thread 5. |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 639 | ::aos::ShmEventLoop can_output_event_loop(&config.message()); |
| 640 | can_output_event_loop.set_name("CANOutputWriter"); |
| 641 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 642 | frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer( |
| 643 | &can_output_event_loop); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 644 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 645 | frc971::wpilib::GenericCANWriter<control_loops::superstructure::Output> |
| 646 | can_superstructure_writer( |
| 647 | &can_output_event_loop, |
| 648 | [](const control_loops::superstructure::Output &output, |
| 649 | const std::map<std::string_view, std::shared_ptr<TalonFX>> |
| 650 | &talonfx_map) { |
| 651 | talonfx_map.find("intake_pivot") |
| 652 | ->second->WriteVoltage(output.intake_pivot_voltage()); |
| 653 | talonfx_map.find("intake_roller") |
| 654 | ->second->WriteVoltage(output.intake_roller_voltage()); |
Niko Sohmers | ed7ffc4 | 2024-02-03 16:05:19 -0800 | [diff] [blame] | 655 | talonfx_map.find("transfer_roller") |
| 656 | ->second->WriteVoltage(output.transfer_roller_voltage()); |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 657 | talonfx_map.find("climber")->second->WriteVoltage( |
| 658 | output.climber_voltage()); |
Filip Kujawa | d75252a | 2024-02-10 16:54:35 -0800 | [diff] [blame] | 659 | talonfx_map.find("extend")->second->WriteVoltage( |
| 660 | output.extend_voltage()); |
| 661 | talonfx_map.find("extend_roller") |
| 662 | ->second->WriteVoltage(output.extend_roller_voltage()); |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 663 | talonfx_map.find("altitude") |
| 664 | ->second->WriteVoltage(output.altitude_voltage()); |
| 665 | talonfx_map.find("turret")->second->WriteVoltage( |
| 666 | output.turret_voltage()); |
Maxwell Henderson | 7b7754b | 2024-02-22 16:50:35 -0800 | [diff] [blame] | 667 | talonfx_map.find("retention_roller") |
| 668 | ->second->WriteVoltage(output.retention_roller_voltage()); |
Maxwell Henderson | 862fc2c | 2024-02-23 16:04:37 -0800 | [diff] [blame] | 669 | if (output.has_retention_roller_stator_current_limit()) { |
| 670 | talonfx_map.find("retention_roller") |
| 671 | ->second->set_stator_current_limit( |
| 672 | output.retention_roller_stator_current_limit()); |
| 673 | } |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 674 | }); |
| 675 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 676 | can_drivetrain_writer.set_talonfxs({right_front, right_back}, |
| 677 | {left_front, left_back}); |
| 678 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 679 | can_superstructure_writer.add_talonfx("intake_pivot", intake_pivot); |
| 680 | can_superstructure_writer.add_talonfx("intake_roller", intake_roller); |
Niko Sohmers | ed7ffc4 | 2024-02-03 16:05:19 -0800 | [diff] [blame] | 681 | can_superstructure_writer.add_talonfx("transfer_roller", transfer_roller); |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 682 | can_superstructure_writer.add_talonfx("climber", climber); |
Filip Kujawa | d75252a | 2024-02-10 16:54:35 -0800 | [diff] [blame] | 683 | can_superstructure_writer.add_talonfx("extend", extend); |
| 684 | can_superstructure_writer.add_talonfx("extend_roller", extend_roller); |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 685 | can_superstructure_writer.add_talonfx("altitude", altitude); |
| 686 | can_superstructure_writer.add_talonfx("turret", turret); |
Maxwell Henderson | 7b7754b | 2024-02-22 16:50:35 -0800 | [diff] [blame] | 687 | can_superstructure_writer.add_talonfx("retention_roller", retention_roller); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 688 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 689 | can_output_event_loop.MakeWatcher( |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 690 | "/roborio", [&can_drivetrain_writer, &can_superstructure_writer]( |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 691 | const frc971::CANConfiguration &configuration) { |
| 692 | can_drivetrain_writer.HandleCANConfiguration(configuration); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 693 | can_superstructure_writer.HandleCANConfiguration(configuration); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 694 | }); |
| 695 | |
| 696 | AddLoop(&can_output_event_loop); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 697 | |
Maxwell Henderson | abcc92a | 2024-02-21 19:06:26 -0800 | [diff] [blame] | 698 | ::aos::ShmEventLoop pwm_event_loop(&config.message()); |
| 699 | SuperstructurePWMWriter superstructure_pwm_writer(&pwm_event_loop); |
| 700 | superstructure_pwm_writer.set_catapult_kraken_one( |
| 701 | make_unique<frc::TalonFX>(0)); |
Maxwell Henderson | 12d427b | 2024-02-22 13:57:45 -0800 | [diff] [blame] | 702 | superstructure_pwm_writer.set_catapult_kraken_two( |
Maxwell Henderson | abcc92a | 2024-02-21 19:06:26 -0800 | [diff] [blame] | 703 | make_unique<frc::TalonFX>(1)); |
| 704 | |
| 705 | AddLoop(&pwm_event_loop); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 706 | // Thread 6 |
| 707 | |
| 708 | RunLoops(); |
| 709 | } |
| 710 | }; |
| 711 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 712 | } // namespace y2024::wpilib |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 713 | |
| 714 | AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot); |