Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <array> |
| 4 | #include <chrono> |
| 5 | #include <cinttypes> |
| 6 | #include <cmath> |
| 7 | #include <cstdio> |
| 8 | #include <cstring> |
| 9 | #include <functional> |
| 10 | #include <memory> |
| 11 | #include <mutex> |
| 12 | #include <thread> |
| 13 | |
| 14 | #include "ctre/phoenix/CANifier.h" |
| 15 | |
| 16 | #include "frc971/wpilib/ahal/AnalogInput.h" |
| 17 | #include "frc971/wpilib/ahal/Counter.h" |
| 18 | #include "frc971/wpilib/ahal/DigitalGlitchFilter.h" |
| 19 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 20 | #include "frc971/wpilib/ahal/Encoder.h" |
| 21 | #include "frc971/wpilib/ahal/Servo.h" |
| 22 | #include "frc971/wpilib/ahal/TalonFX.h" |
| 23 | #include "frc971/wpilib/ahal/VictorSP.h" |
| 24 | #undef ERROR |
| 25 | |
| 26 | #include "ctre/phoenix/cci/Diagnostics_CCI.h" |
| 27 | #include "ctre/phoenix6/TalonFX.hpp" |
| 28 | |
| 29 | #include "aos/commonmath.h" |
| 30 | #include "aos/containers/sized_array.h" |
| 31 | #include "aos/events/event_loop.h" |
| 32 | #include "aos/events/shm_event_loop.h" |
| 33 | #include "aos/init.h" |
| 34 | #include "aos/logging/logging.h" |
| 35 | #include "aos/realtime.h" |
| 36 | #include "aos/time/time.h" |
| 37 | #include "aos/util/log_interval.h" |
| 38 | #include "aos/util/phased_loop.h" |
| 39 | #include "aos/util/wrapping_counter.h" |
| 40 | #include "frc971/autonomous/auto_mode_generated.h" |
| 41 | #include "frc971/can_configuration_generated.h" |
| 42 | #include "frc971/control_loops/drivetrain/drivetrain_can_position_generated.h" |
Maxwell Henderson | dfa609a | 2024-01-12 20:48:36 -0800 | [diff] [blame] | 43 | #include "frc971/control_loops/drivetrain/drivetrain_can_position_static.h" |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 44 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
| 45 | #include "frc971/input/robot_state_generated.h" |
| 46 | #include "frc971/queues/gyro_generated.h" |
| 47 | #include "frc971/wpilib/ADIS16448.h" |
| 48 | #include "frc971/wpilib/buffered_pcm.h" |
| 49 | #include "frc971/wpilib/buffered_solenoid.h" |
| 50 | #include "frc971/wpilib/can_drivetrain_writer.h" |
| 51 | #include "frc971/wpilib/can_sensor_reader.h" |
| 52 | #include "frc971/wpilib/dma.h" |
| 53 | #include "frc971/wpilib/drivetrain_writer.h" |
| 54 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 55 | #include "frc971/wpilib/joystick_sender.h" |
| 56 | #include "frc971/wpilib/logging_generated.h" |
| 57 | #include "frc971/wpilib/loop_output_handler.h" |
| 58 | #include "frc971/wpilib/pdp_fetcher.h" |
| 59 | #include "frc971/wpilib/sensor_reader.h" |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 60 | #include "frc971/wpilib/talonfx.h" |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 61 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 62 | #include "y2024_defense/constants.h" |
| 63 | |
| 64 | DEFINE_bool(ctre_diag_server, false, |
| 65 | "If true, enable the diagnostics server for interacting with " |
| 66 | "devices on the CAN bus using Phoenix Tuner"); |
| 67 | |
| 68 | using ::aos::monotonic_clock; |
| 69 | using ::frc971::CANConfiguration; |
| 70 | using ::y2024_defense::constants::Values; |
| 71 | |
Maxwell Henderson | dfa609a | 2024-01-12 20:48:36 -0800 | [diff] [blame] | 72 | using frc971::control_loops::drivetrain::CANPositionStatic; |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 73 | using frc971::wpilib::TalonFX; |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 74 | |
| 75 | using std::make_unique; |
| 76 | |
| 77 | namespace y2024_defense { |
| 78 | namespace wpilib { |
| 79 | namespace { |
| 80 | |
| 81 | constexpr double kMaxBringupPower = 12.0; |
| 82 | |
| 83 | double drivetrain_velocity_translate(double in) { |
| 84 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
| 85 | (2.0 * M_PI)) * |
| 86 | Values::kDrivetrainEncoderRatio() * |
| 87 | control_loops::drivetrain::kWheelRadius; |
| 88 | } |
| 89 | |
| 90 | constexpr double kMaxFastEncoderPulsesPerSecond = std::max({ |
| 91 | Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
| 92 | }); |
| 93 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 94 | "fast encoders are too fast"); |
| 95 | |
| 96 | } // namespace |
| 97 | |
| 98 | // Class to send position messages with sensor readings to our loops. |
| 99 | class SensorReader : public ::frc971::wpilib::SensorReader { |
| 100 | public: |
| 101 | SensorReader(::aos::ShmEventLoop *event_loop, |
| 102 | std::shared_ptr<const Values> values) |
| 103 | : ::frc971::wpilib::SensorReader(event_loop), |
| 104 | values_(std::move(values)), |
| 105 | auto_mode_sender_( |
| 106 | event_loop->MakeSender<::frc971::autonomous::AutonomousMode>( |
| 107 | "/autonomous")), |
| 108 | drivetrain_position_sender_( |
| 109 | event_loop |
| 110 | ->MakeSender<::frc971::control_loops::drivetrain::Position>( |
| 111 | "/drivetrain")), |
| 112 | gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>( |
| 113 | "/drivetrain")) { |
| 114 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 115 | // we should ever see. |
| 116 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 117 | event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0})); |
| 118 | } |
| 119 | |
| 120 | void Start() override { AddToDMA(&imu_yaw_rate_reader_); } |
| 121 | |
| 122 | // Auto mode switches. |
| 123 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
| 124 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 125 | } |
| 126 | |
| 127 | void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) { |
| 128 | imu_yaw_rate_input_ = ::std::move(sensor); |
| 129 | imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get()); |
| 130 | } |
| 131 | |
| 132 | void RunIteration() override { |
| 133 | superstructure_reading_->Set(true); |
| 134 | { |
| 135 | { |
| 136 | auto builder = drivetrain_position_sender_.MakeBuilder(); |
| 137 | frc971::control_loops::drivetrain::Position::Builder |
| 138 | drivetrain_builder = |
| 139 | builder |
| 140 | .MakeBuilder<frc971::control_loops::drivetrain::Position>(); |
| 141 | drivetrain_builder.add_left_encoder( |
| 142 | constants::Values::DrivetrainEncoderToMeters( |
| 143 | drivetrain_left_encoder_->GetRaw())); |
| 144 | drivetrain_builder.add_left_speed(drivetrain_velocity_translate( |
| 145 | drivetrain_left_encoder_->GetPeriod())); |
| 146 | |
| 147 | drivetrain_builder.add_right_encoder( |
| 148 | -constants::Values::DrivetrainEncoderToMeters( |
| 149 | drivetrain_right_encoder_->GetRaw())); |
| 150 | drivetrain_builder.add_right_speed(-drivetrain_velocity_translate( |
| 151 | drivetrain_right_encoder_->GetPeriod())); |
| 152 | |
| 153 | builder.CheckOk(builder.Send(drivetrain_builder.Finish())); |
| 154 | } |
| 155 | |
| 156 | { |
| 157 | auto builder = gyro_sender_.MakeBuilder(); |
| 158 | ::frc971::sensors::GyroReading::Builder gyro_reading_builder = |
| 159 | builder.MakeBuilder<::frc971::sensors::GyroReading>(); |
| 160 | // +/- 2000 deg / sec |
| 161 | constexpr double kMaxVelocity = 4000; // degrees / second |
| 162 | constexpr double kVelocityRadiansPerSecond = |
| 163 | kMaxVelocity / 360 * (2.0 * M_PI); |
| 164 | |
| 165 | // Only part of the full range is used to prevent being 100% on or off. |
| 166 | constexpr double kScaledRangeLow = 0.1; |
| 167 | constexpr double kScaledRangeHigh = 0.9; |
| 168 | |
| 169 | constexpr double kPWMFrequencyHz = 200; |
| 170 | double velocity_duty_cycle = |
| 171 | imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz; |
| 172 | |
| 173 | constexpr double kDutyCycleScale = |
| 174 | 1 / (kScaledRangeHigh - kScaledRangeLow); |
| 175 | // scale from 0.1 - 0.9 to 0 - 1 |
| 176 | double rescaled_velocity_duty_cycle = |
| 177 | (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale; |
| 178 | |
| 179 | if (!std::isnan(rescaled_velocity_duty_cycle)) { |
| 180 | gyro_reading_builder.add_velocity( |
| 181 | (rescaled_velocity_duty_cycle - 0.5) * kVelocityRadiansPerSecond); |
| 182 | } |
| 183 | builder.CheckOk(builder.Send(gyro_reading_builder.Finish())); |
| 184 | } |
| 185 | |
| 186 | { |
| 187 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 188 | |
| 189 | uint32_t mode = 0; |
| 190 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 191 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 192 | mode |= 1 << i; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | auto auto_mode_builder = |
| 197 | builder.MakeBuilder<frc971::autonomous::AutonomousMode>(); |
| 198 | |
| 199 | auto_mode_builder.add_mode(mode); |
| 200 | |
| 201 | builder.CheckOk(builder.Send(auto_mode_builder.Finish())); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | std::shared_ptr<frc::DigitalOutput> superstructure_reading_; |
| 207 | |
| 208 | void set_superstructure_reading( |
| 209 | std::shared_ptr<frc::DigitalOutput> superstructure_reading) { |
| 210 | superstructure_reading_ = superstructure_reading; |
| 211 | } |
| 212 | |
| 213 | private: |
| 214 | std::shared_ptr<const Values> values_; |
| 215 | |
| 216 | aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_; |
| 217 | aos::Sender<frc971::control_loops::drivetrain::Position> |
| 218 | drivetrain_position_sender_; |
| 219 | ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_; |
| 220 | |
| 221 | std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_; |
| 222 | |
| 223 | std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_; |
| 224 | |
| 225 | frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_; |
| 226 | }; |
| 227 | |
| 228 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 229 | public: |
| 230 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 231 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 232 | frc::Encoder::k4X); |
| 233 | } |
| 234 | |
| 235 | void Run() override { |
| 236 | std::shared_ptr<const Values> values = |
| 237 | std::make_shared<const Values>(constants::MakeValues()); |
| 238 | |
| 239 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 240 | aos::configuration::ReadConfig("aos_config.json"); |
| 241 | |
| 242 | // Thread 1. |
| 243 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
| 244 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 245 | &joystick_sender_event_loop); |
| 246 | AddLoop(&joystick_sender_event_loop); |
| 247 | |
| 248 | // Thread 2. |
| 249 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 250 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 251 | AddLoop(&pdp_fetcher_event_loop); |
| 252 | |
| 253 | // Thread 3. |
| 254 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
| 255 | SensorReader sensor_reader(&sensor_reader_event_loop, values); |
| 256 | std::shared_ptr<frc::DigitalOutput> superstructure_reading = |
| 257 | make_unique<frc::DigitalOutput>(25); |
| 258 | |
| 259 | sensor_reader.set_pwm_trigger(true); |
| 260 | sensor_reader.set_drivetrain_left_encoder(make_encoder(1)); |
| 261 | sensor_reader.set_drivetrain_right_encoder(make_encoder(0)); |
| 262 | sensor_reader.set_superstructure_reading(superstructure_reading); |
| 263 | sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0)); |
| 264 | |
| 265 | AddLoop(&sensor_reader_event_loop); |
| 266 | |
| 267 | // Thread 4. |
| 268 | std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry; |
| 269 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 270 | std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>( |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 271 | 1, true, "Drivetrain Bus", &signals_registry, |
| 272 | constants::Values::kDrivetrainStatorCurrentLimit(), |
| 273 | constants::Values::kDrivetrainSupplyCurrentLimit()); |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 274 | std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>( |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 275 | 2, true, "Drivetrain Bus", &signals_registry, |
| 276 | constants::Values::kDrivetrainStatorCurrentLimit(), |
| 277 | constants::Values::kDrivetrainSupplyCurrentLimit()); |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 278 | std::shared_ptr<TalonFX> right_under = std::make_shared<TalonFX>( |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 279 | 3, true, "Drivetrain Bus", &signals_registry, |
| 280 | constants::Values::kDrivetrainStatorCurrentLimit(), |
| 281 | constants::Values::kDrivetrainSupplyCurrentLimit()); |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 282 | std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>( |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 283 | 4, false, "Drivetrain Bus", &signals_registry, |
| 284 | constants::Values::kDrivetrainStatorCurrentLimit(), |
| 285 | constants::Values::kDrivetrainSupplyCurrentLimit()); |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 286 | std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>( |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 287 | 5, false, "Drivetrain Bus", &signals_registry, |
| 288 | constants::Values::kDrivetrainStatorCurrentLimit(), |
| 289 | constants::Values::kDrivetrainSupplyCurrentLimit()); |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 290 | std::shared_ptr<TalonFX> left_under = std::make_shared<TalonFX>( |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 291 | 6, false, "Drivetrain Bus", &signals_registry, |
| 292 | constants::Values::kDrivetrainStatorCurrentLimit(), |
| 293 | constants::Values::kDrivetrainSupplyCurrentLimit()); |
| 294 | |
| 295 | // Setting up CAN. |
| 296 | if (!FLAGS_ctre_diag_server) { |
| 297 | c_Phoenix_Diagnostics_SetSecondsToStart(-1); |
| 298 | c_Phoenix_Diagnostics_Dispose(); |
| 299 | } |
| 300 | |
| 301 | // Creating list of falcons for CANSensorReader |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 302 | std::vector<std::shared_ptr<TalonFX>> falcons; |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 303 | for (auto falcon : {right_front, right_back, right_under, left_front, |
| 304 | left_back, left_under}) { |
| 305 | falcons.push_back(falcon); |
| 306 | } |
| 307 | |
| 308 | ctre::phoenix::platform::can::CANComm_SetRxSchedPriority( |
| 309 | constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus"); |
| 310 | ctre::phoenix::platform::can::CANComm_SetTxSchedPriority( |
| 311 | constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus"); |
| 312 | |
| 313 | ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message()); |
| 314 | can_sensor_reader_event_loop.set_name("CANSensorReader"); |
| 315 | |
Maxwell Henderson | dfa609a | 2024-01-12 20:48:36 -0800 | [diff] [blame] | 316 | aos::Sender<CANPositionStatic> can_position_sender = |
| 317 | can_sensor_reader_event_loop.MakeSender<CANPositionStatic>( |
| 318 | "/drivetrain"); |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 319 | |
| 320 | frc971::wpilib::CANSensorReader can_sensor_reader( |
| 321 | &can_sensor_reader_event_loop, std::move(signals_registry), falcons, |
| 322 | [falcons, &can_position_sender](ctre::phoenix::StatusCode status) { |
Maxwell Henderson | dfa609a | 2024-01-12 20:48:36 -0800 | [diff] [blame] | 323 | aos::Sender<CANPositionStatic>::StaticBuilder builder = |
| 324 | can_position_sender.MakeStaticBuilder(); |
| 325 | |
| 326 | auto falcon_vector = builder->add_talonfxs(); |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 327 | |
| 328 | for (auto falcon : falcons) { |
| 329 | falcon->SerializePosition( |
Maxwell Henderson | dfa609a | 2024-01-12 20:48:36 -0800 | [diff] [blame] | 330 | falcon_vector->emplace_back(), |
| 331 | control_loops::drivetrain::kHighOutputRatio); |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Maxwell Henderson | dfa609a | 2024-01-12 20:48:36 -0800 | [diff] [blame] | 334 | builder->set_timestamp(falcons.front()->GetTimestamp()); |
| 335 | builder->set_status(static_cast<int>(status)); |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 336 | |
Maxwell Henderson | dfa609a | 2024-01-12 20:48:36 -0800 | [diff] [blame] | 337 | builder.CheckOk(builder.Send()); |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 338 | }); |
| 339 | |
| 340 | AddLoop(&can_sensor_reader_event_loop); |
| 341 | |
| 342 | // Thread 5. |
| 343 | ::aos::ShmEventLoop can_drivetrain_writer_event_loop(&config.message()); |
| 344 | can_drivetrain_writer_event_loop.set_name("CANDrivetrainWriter"); |
| 345 | |
| 346 | frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer( |
| 347 | &can_drivetrain_writer_event_loop); |
| 348 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 349 | can_drivetrain_writer.set_talonfxs({right_front, right_back, right_under}, |
| 350 | {left_front, left_back, left_under}); |
Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame] | 351 | |
| 352 | can_drivetrain_writer_event_loop.MakeWatcher( |
| 353 | "/roborio", [&can_drivetrain_writer]( |
| 354 | const frc971::CANConfiguration &configuration) { |
| 355 | can_drivetrain_writer.HandleCANConfiguration(configuration); |
| 356 | }); |
| 357 | |
| 358 | AddLoop(&can_drivetrain_writer_event_loop); |
| 359 | |
| 360 | RunLoops(); |
| 361 | } |
| 362 | }; |
| 363 | |
| 364 | } // namespace wpilib |
| 365 | } // namespace y2024_defense |
| 366 | |
| 367 | AOS_ROBOT_CLASS(::y2024_defense::wpilib::WPILibRobot); |