Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [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" |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 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" |
| 43 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
| 44 | #include "frc971/input/robot_state_generated.h" |
| 45 | #include "frc971/queues/gyro_generated.h" |
| 46 | #include "frc971/wpilib/ADIS16448.h" |
| 47 | #include "frc971/wpilib/buffered_pcm.h" |
| 48 | #include "frc971/wpilib/buffered_solenoid.h" |
| 49 | #include "frc971/wpilib/dma.h" |
| 50 | #include "frc971/wpilib/drivetrain_writer.h" |
| 51 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 52 | #include "frc971/wpilib/joystick_sender.h" |
| 53 | #include "frc971/wpilib/logging_generated.h" |
| 54 | #include "frc971/wpilib/loop_output_handler.h" |
| 55 | #include "frc971/wpilib/pdp_fetcher.h" |
| 56 | #include "frc971/wpilib/sensor_reader.h" |
| 57 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 58 | #include "y2023_bot3/constants.h" |
| 59 | #include "y2023_bot3/control_loops/superstructure/led_indicator.h" |
| 60 | #include "y2023_bot3/control_loops/superstructure/superstructure_output_generated.h" |
| 61 | #include "y2023_bot3/control_loops/superstructure/superstructure_position_generated.h" |
| 62 | |
| 63 | DEFINE_bool(ctre_diag_server, false, |
| 64 | "If true, enable the diagnostics server for interacting with " |
| 65 | "devices on the CAN bus using Phoenix Tuner"); |
| 66 | |
| 67 | using ::aos::monotonic_clock; |
| 68 | using ::y2023_bot3::constants::Values; |
| 69 | namespace superstructure = ::y2023_bot3::control_loops::superstructure; |
| 70 | namespace drivetrain = ::y2023_bot3::control_loops::drivetrain; |
| 71 | namespace chrono = ::std::chrono; |
| 72 | using std::make_unique; |
| 73 | |
| 74 | namespace y2023_bot3 { |
| 75 | namespace wpilib { |
| 76 | namespace { |
| 77 | |
| 78 | constexpr double kMaxBringupPower = 12.0; |
| 79 | |
| 80 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 81 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 82 | // The low bit is direction. |
| 83 | |
| 84 | double drivetrain_velocity_translate(double in) { |
| 85 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
| 86 | (2.0 * M_PI)) * |
| 87 | Values::kDrivetrainEncoderRatio() * |
| 88 | control_loops::drivetrain::kWheelRadius; |
| 89 | } |
| 90 | |
| 91 | constexpr double kMaxFastEncoderPulsesPerSecond = std::max({ |
| 92 | Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
| 93 | }); |
| 94 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 95 | "fast encoders are too fast"); |
| 96 | |
| 97 | } // namespace |
| 98 | |
| 99 | static constexpr int kCANFalconCount = 6; |
| 100 | static constexpr units::frequency::hertz_t kCANUpdateFreqHz = 200_Hz; |
| 101 | |
| 102 | class Falcon { |
| 103 | public: |
| 104 | Falcon(int device_id, std::string canbus, |
| 105 | std::vector<ctre::phoenix6::BaseStatusSignal *> *signals) |
| 106 | : talon_(device_id, canbus), |
| 107 | device_id_(device_id), |
| 108 | device_temp_(talon_.GetDeviceTemp()), |
| 109 | supply_voltage_(talon_.GetSupplyVoltage()), |
| 110 | supply_current_(talon_.GetSupplyCurrent()), |
| 111 | torque_current_(talon_.GetTorqueCurrent()), |
| 112 | position_(talon_.GetPosition()), |
| 113 | duty_cycle_(talon_.GetDutyCycle()) { |
| 114 | // device temp is not timesynced so don't add it to the list of signals |
| 115 | device_temp_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 116 | |
| 117 | CHECK_NOTNULL(signals); |
| 118 | |
| 119 | supply_voltage_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 120 | signals->push_back(&supply_voltage_); |
| 121 | |
| 122 | supply_current_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 123 | signals->push_back(&supply_current_); |
| 124 | |
| 125 | torque_current_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 126 | signals->push_back(&torque_current_); |
| 127 | |
| 128 | position_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 129 | signals->push_back(&position_); |
| 130 | |
| 131 | duty_cycle_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 132 | signals->push_back(&duty_cycle_); |
| 133 | } |
| 134 | |
| 135 | void PrintConfigs() { |
| 136 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
| 137 | ctre::phoenix::StatusCode status = |
| 138 | talon_.GetConfigurator().Refresh(configuration); |
| 139 | if (!status.IsOK()) { |
| 140 | AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s", |
| 141 | status.GetName(), status.GetDescription()); |
| 142 | } |
| 143 | AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str()); |
| 144 | } |
| 145 | |
| 146 | void WriteConfigs(ctre::phoenix6::signals::InvertedValue invert) { |
| 147 | inverted_ = invert; |
| 148 | |
| 149 | ctre::phoenix6::configs::CurrentLimitsConfigs current_limits; |
| 150 | current_limits.StatorCurrentLimit = |
| 151 | constants::Values::kDrivetrainStatorCurrentLimit(); |
| 152 | current_limits.StatorCurrentLimitEnable = true; |
| 153 | current_limits.SupplyCurrentLimit = |
| 154 | constants::Values::kDrivetrainSupplyCurrentLimit(); |
| 155 | current_limits.SupplyCurrentLimitEnable = true; |
| 156 | |
| 157 | ctre::phoenix6::configs::MotorOutputConfigs output_configs; |
| 158 | output_configs.NeutralMode = |
| 159 | ctre::phoenix6::signals::NeutralModeValue::Brake; |
| 160 | output_configs.DutyCycleNeutralDeadband = 0; |
| 161 | |
| 162 | output_configs.Inverted = inverted_; |
| 163 | |
| 164 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
| 165 | configuration.CurrentLimits = current_limits; |
| 166 | configuration.MotorOutput = output_configs; |
| 167 | |
| 168 | ctre::phoenix::StatusCode status = |
| 169 | talon_.GetConfigurator().Apply(configuration); |
| 170 | if (!status.IsOK()) { |
| 171 | AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s", |
| 172 | status.GetName(), status.GetDescription()); |
| 173 | } |
| 174 | |
| 175 | PrintConfigs(); |
| 176 | } |
| 177 | |
| 178 | ctre::phoenix6::hardware::TalonFX *talon() { return &talon_; } |
| 179 | |
| 180 | flatbuffers::Offset<frc971::control_loops::CANFalcon> WritePosition( |
| 181 | flatbuffers::FlatBufferBuilder *fbb) { |
| 182 | frc971::control_loops::CANFalcon::Builder builder(*fbb); |
| 183 | builder.add_id(device_id_); |
| 184 | builder.add_device_temp(device_temp()); |
| 185 | builder.add_supply_voltage(supply_voltage()); |
| 186 | builder.add_supply_current(supply_current()); |
| 187 | builder.add_torque_current(torque_current()); |
| 188 | builder.add_duty_cycle(duty_cycle()); |
| 189 | |
| 190 | double invert = |
| 191 | (inverted_ == ctre::phoenix6::signals::InvertedValue::Clockwise_Positive |
| 192 | ? 1 |
| 193 | : -1); |
| 194 | |
| 195 | builder.add_position( |
| 196 | constants::Values::DrivetrainCANEncoderToMeters(position()) * invert); |
| 197 | |
| 198 | return builder.Finish(); |
| 199 | } |
| 200 | |
| 201 | int device_id() const { return device_id_; } |
| 202 | float device_temp() const { return device_temp_.GetValue().value(); } |
| 203 | float supply_voltage() const { return supply_voltage_.GetValue().value(); } |
| 204 | float supply_current() const { return supply_current_.GetValue().value(); } |
| 205 | float torque_current() const { return torque_current_.GetValue().value(); } |
| 206 | float duty_cycle() const { return duty_cycle_.GetValue().value(); } |
| 207 | float position() const { return position_.GetValue().value(); } |
| 208 | |
| 209 | // returns the monotonic timestamp of the latest timesynced reading in the |
| 210 | // timebase of the the syncronized CAN bus clock. |
| 211 | int64_t GetTimestamp() { |
| 212 | std::chrono::nanoseconds latest_timestamp = |
| 213 | torque_current_.GetTimestamp().GetTime(); |
| 214 | |
| 215 | return latest_timestamp.count(); |
| 216 | } |
| 217 | |
| 218 | void RefreshNontimesyncedSignals() { device_temp_.Refresh(); }; |
| 219 | |
| 220 | private: |
| 221 | ctre::phoenix6::hardware::TalonFX talon_; |
| 222 | int device_id_; |
| 223 | |
| 224 | ctre::phoenix6::signals::InvertedValue inverted_; |
| 225 | |
| 226 | ctre::phoenix6::StatusSignal<units::temperature::celsius_t> device_temp_; |
| 227 | ctre::phoenix6::StatusSignal<units::voltage::volt_t> supply_voltage_; |
| 228 | ctre::phoenix6::StatusSignal<units::current::ampere_t> supply_current_, |
| 229 | torque_current_; |
| 230 | ctre::phoenix6::StatusSignal<units::angle::turn_t> position_; |
| 231 | ctre::phoenix6::StatusSignal<units::dimensionless::scalar_t> duty_cycle_; |
| 232 | }; |
| 233 | |
| 234 | class CANSensorReader { |
| 235 | public: |
| 236 | CANSensorReader( |
| 237 | aos::EventLoop *event_loop, |
| 238 | std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry) |
| 239 | : event_loop_(event_loop), |
| 240 | signals_(signals_registry.begin(), signals_registry.end()), |
| 241 | can_position_sender_( |
| 242 | event_loop |
| 243 | ->MakeSender<frc971::control_loops::drivetrain::CANPosition>( |
Maxwell Henderson | 4d4be54 | 2023-11-29 18:26:13 -0800 | [diff] [blame^] | 244 | "/drivetrain")) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 245 | event_loop->SetRuntimeRealtimePriority(40); |
| 246 | event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({1})); |
| 247 | timer_handler_ = event_loop->AddTimer([this]() { Loop(); }); |
| 248 | timer_handler_->set_name("CANSensorReader Loop"); |
| 249 | |
| 250 | event_loop->OnRun([this]() { |
| 251 | timer_handler_->Schedule(event_loop_->monotonic_now(), |
| 252 | 1 / kCANUpdateFreqHz); |
| 253 | }); |
| 254 | } |
| 255 | |
| 256 | void set_falcons(std::shared_ptr<Falcon> right_front, |
| 257 | std::shared_ptr<Falcon> right_back, |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 258 | std::shared_ptr<Falcon> left_front, |
Maxwell Henderson | 4d4be54 | 2023-11-29 18:26:13 -0800 | [diff] [blame^] | 259 | std::shared_ptr<Falcon> left_back) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 260 | right_front_ = std::move(right_front); |
| 261 | right_back_ = std::move(right_back); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 262 | left_front_ = std::move(left_front); |
| 263 | left_back_ = std::move(left_back); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | private: |
| 267 | void Loop() { |
| 268 | ctre::phoenix::StatusCode status = |
| 269 | ctre::phoenix6::BaseStatusSignal::WaitForAll(2000_ms, signals_); |
| 270 | |
| 271 | if (!status.IsOK()) { |
| 272 | AOS_LOG(ERROR, "Failed to read signals from falcons: %s: %s", |
| 273 | status.GetName(), status.GetDescription()); |
| 274 | } |
| 275 | |
| 276 | auto builder = can_position_sender_.MakeBuilder(); |
| 277 | |
Maxwell Henderson | 4d4be54 | 2023-11-29 18:26:13 -0800 | [diff] [blame^] | 278 | for (auto falcon : {right_front_, right_back_, left_front_, left_back_}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 279 | falcon->RefreshNontimesyncedSignals(); |
| 280 | } |
| 281 | |
| 282 | aos::SizedArray<flatbuffers::Offset<frc971::control_loops::CANFalcon>, |
| 283 | kCANFalconCount> |
| 284 | falcons; |
| 285 | |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 286 | for (auto falcon : {right_front_, right_back_, left_front_, left_back_}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 287 | falcons.push_back(falcon->WritePosition(builder.fbb())); |
| 288 | } |
| 289 | |
| 290 | auto falcons_list = |
| 291 | builder.fbb() |
| 292 | ->CreateVector< |
| 293 | flatbuffers::Offset<frc971::control_loops::CANFalcon>>(falcons); |
| 294 | |
| 295 | frc971::control_loops::drivetrain::CANPosition::Builder |
| 296 | can_position_builder = |
| 297 | builder |
| 298 | .MakeBuilder<frc971::control_loops::drivetrain::CANPosition>(); |
| 299 | |
| 300 | can_position_builder.add_falcons(falcons_list); |
| 301 | can_position_builder.add_timestamp(right_front_->GetTimestamp()); |
| 302 | can_position_builder.add_status(static_cast<int>(status)); |
| 303 | |
| 304 | builder.CheckOk(builder.Send(can_position_builder.Finish())); |
| 305 | } |
| 306 | |
| 307 | aos::EventLoop *event_loop_; |
| 308 | |
| 309 | const std::vector<ctre::phoenix6::BaseStatusSignal *> signals_; |
| 310 | aos::Sender<frc971::control_loops::drivetrain::CANPosition> |
| 311 | can_position_sender_; |
| 312 | |
Maxwell Henderson | 4d4be54 | 2023-11-29 18:26:13 -0800 | [diff] [blame^] | 313 | std::shared_ptr<Falcon> right_front_, right_back_, left_front_, left_back_; |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 314 | |
| 315 | // Pointer to the timer handler used to modify the wakeup. |
| 316 | ::aos::TimerHandler *timer_handler_; |
| 317 | }; |
| 318 | |
| 319 | // Class to send position messages with sensor readings to our loops. |
| 320 | class SensorReader : public ::frc971::wpilib::SensorReader { |
| 321 | public: |
| 322 | SensorReader(::aos::ShmEventLoop *event_loop, |
| 323 | std::shared_ptr<const Values> values, |
| 324 | CANSensorReader *can_sensor_reader) |
| 325 | : ::frc971::wpilib::SensorReader(event_loop), |
| 326 | values_(std::move(values)), |
| 327 | auto_mode_sender_( |
| 328 | event_loop->MakeSender<::frc971::autonomous::AutonomousMode>( |
| 329 | "/autonomous")), |
| 330 | superstructure_position_sender_( |
| 331 | event_loop->MakeSender<superstructure::Position>( |
| 332 | "/superstructure")), |
| 333 | drivetrain_position_sender_( |
| 334 | event_loop |
| 335 | ->MakeSender<::frc971::control_loops::drivetrain::Position>( |
| 336 | "/drivetrain")), |
| 337 | gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>( |
| 338 | "/drivetrain")), |
| 339 | can_sensor_reader_(can_sensor_reader) { |
| 340 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 341 | // we should ever see. |
| 342 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 343 | event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0})); |
| 344 | } |
| 345 | |
| 346 | void Start() override { AddToDMA(&imu_yaw_rate_reader_); } |
| 347 | |
| 348 | // Auto mode switches. |
| 349 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
| 350 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 351 | } |
| 352 | |
| 353 | void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) { |
| 354 | imu_yaw_rate_input_ = ::std::move(sensor); |
| 355 | imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get()); |
| 356 | } |
| 357 | |
| 358 | void RunIteration() override { |
| 359 | superstructure_reading_->Set(true); |
Ariv Diggi | c892e92 | 2023-10-21 15:52:06 -0700 | [diff] [blame] | 360 | { |
| 361 | auto builder = superstructure_position_sender_.MakeBuilder(); |
| 362 | |
Ariv Diggi | c892e92 | 2023-10-21 15:52:06 -0700 | [diff] [blame] | 363 | superstructure::Position::Builder position_builder = |
| 364 | builder.MakeBuilder<superstructure::Position>(); |
Ariv Diggi | c892e92 | 2023-10-21 15:52:06 -0700 | [diff] [blame] | 365 | builder.CheckOk(builder.Send(position_builder.Finish())); |
| 366 | } |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 367 | |
| 368 | { |
| 369 | auto builder = drivetrain_position_sender_.MakeBuilder(); |
| 370 | frc971::control_loops::drivetrain::Position::Builder drivetrain_builder = |
| 371 | builder.MakeBuilder<frc971::control_loops::drivetrain::Position>(); |
| 372 | drivetrain_builder.add_left_encoder( |
Filip Kujawa | 35b5aad | 2023-11-14 21:53:19 -0800 | [diff] [blame] | 373 | -constants::Values::DrivetrainEncoderToMeters( |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 374 | drivetrain_left_encoder_->GetRaw())); |
| 375 | drivetrain_builder.add_left_speed( |
| 376 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod())); |
| 377 | |
| 378 | drivetrain_builder.add_right_encoder( |
Filip Kujawa | 35b5aad | 2023-11-14 21:53:19 -0800 | [diff] [blame] | 379 | constants::Values::DrivetrainEncoderToMeters( |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 380 | drivetrain_right_encoder_->GetRaw())); |
| 381 | drivetrain_builder.add_right_speed(-drivetrain_velocity_translate( |
| 382 | drivetrain_right_encoder_->GetPeriod())); |
| 383 | |
| 384 | builder.CheckOk(builder.Send(drivetrain_builder.Finish())); |
| 385 | } |
| 386 | |
| 387 | { |
| 388 | auto builder = gyro_sender_.MakeBuilder(); |
| 389 | ::frc971::sensors::GyroReading::Builder gyro_reading_builder = |
| 390 | builder.MakeBuilder<::frc971::sensors::GyroReading>(); |
| 391 | // +/- 2000 deg / sec |
| 392 | constexpr double kMaxVelocity = 4000; // degrees / second |
| 393 | constexpr double kVelocityRadiansPerSecond = |
| 394 | kMaxVelocity / 360 * (2.0 * M_PI); |
| 395 | |
| 396 | // Only part of the full range is used to prevent being 100% on or off. |
| 397 | constexpr double kScaledRangeLow = 0.1; |
| 398 | constexpr double kScaledRangeHigh = 0.9; |
| 399 | |
| 400 | constexpr double kPWMFrequencyHz = 200; |
| 401 | double velocity_duty_cycle = |
| 402 | imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz; |
| 403 | |
| 404 | constexpr double kDutyCycleScale = |
| 405 | 1 / (kScaledRangeHigh - kScaledRangeLow); |
| 406 | // scale from 0.1 - 0.9 to 0 - 1 |
| 407 | double rescaled_velocity_duty_cycle = |
| 408 | (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale; |
| 409 | |
| 410 | if (!std::isnan(rescaled_velocity_duty_cycle)) { |
| 411 | gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) * |
| 412 | kVelocityRadiansPerSecond); |
| 413 | } |
| 414 | builder.CheckOk(builder.Send(gyro_reading_builder.Finish())); |
| 415 | } |
| 416 | |
| 417 | { |
| 418 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 419 | |
| 420 | uint32_t mode = 0; |
| 421 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 422 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 423 | mode |= 1 << i; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | auto auto_mode_builder = |
| 428 | builder.MakeBuilder<frc971::autonomous::AutonomousMode>(); |
| 429 | |
| 430 | auto_mode_builder.add_mode(mode); |
| 431 | |
| 432 | builder.CheckOk(builder.Send(auto_mode_builder.Finish())); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | std::shared_ptr<frc::DigitalOutput> superstructure_reading_; |
| 437 | |
| 438 | void set_superstructure_reading( |
| 439 | std::shared_ptr<frc::DigitalOutput> superstructure_reading) { |
| 440 | superstructure_reading_ = superstructure_reading; |
| 441 | } |
| 442 | |
| 443 | private: |
| 444 | std::shared_ptr<const Values> values_; |
| 445 | |
| 446 | aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_; |
| 447 | aos::Sender<superstructure::Position> superstructure_position_sender_; |
| 448 | aos::Sender<frc971::control_loops::drivetrain::Position> |
| 449 | drivetrain_position_sender_; |
| 450 | ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_; |
| 451 | |
| 452 | std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_; |
| 453 | |
Maxwell Henderson | 4d4be54 | 2023-11-29 18:26:13 -0800 | [diff] [blame^] | 454 | std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_; |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 455 | |
| 456 | frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_; |
| 457 | |
| 458 | CANSensorReader *can_sensor_reader_; |
| 459 | }; |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 460 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler< |
| 461 | ::frc971::control_loops::drivetrain::Output> { |
| 462 | public: |
| 463 | DrivetrainWriter(::aos::EventLoop *event_loop) |
| 464 | : ::frc971::wpilib::LoopOutputHandler< |
| 465 | ::frc971::control_loops::drivetrain::Output>(event_loop, |
| 466 | "/drivetrain") { |
| 467 | event_loop->SetRuntimeRealtimePriority( |
| 468 | constants::Values::kDrivetrainWriterPriority); |
| 469 | |
| 470 | event_loop->OnRun([this]() { WriteConfigs(); }); |
| 471 | } |
| 472 | |
| 473 | void set_falcons(std::shared_ptr<Falcon> right_front, |
| 474 | std::shared_ptr<Falcon> right_back, |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 475 | std::shared_ptr<Falcon> left_front, |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 476 | std::shared_ptr<Falcon> left_back) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 477 | right_front_ = std::move(right_front); |
| 478 | right_back_ = std::move(right_back); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 479 | left_front_ = std::move(left_front); |
| 480 | left_back_ = std::move(left_back); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void set_right_inverted(ctre::phoenix6::signals::InvertedValue invert) { |
| 484 | right_inverted_ = invert; |
| 485 | } |
| 486 | |
| 487 | void set_left_inverted(ctre::phoenix6::signals::InvertedValue invert) { |
| 488 | left_inverted_ = invert; |
| 489 | } |
| 490 | |
| 491 | void HandleCANConfiguration(const frc971::CANConfiguration &configuration) { |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 492 | for (auto falcon : {right_front_, right_back_, left_front_, left_back_}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 493 | falcon->PrintConfigs(); |
| 494 | } |
| 495 | if (configuration.reapply()) { |
| 496 | WriteConfigs(); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | private: |
| 501 | void WriteConfigs() { |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 502 | for (auto falcon : {right_front_.get(), right_back_.get()}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 503 | falcon->WriteConfigs(right_inverted_); |
| 504 | } |
| 505 | |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 506 | for (auto falcon : {left_front_.get(), left_back_.get()}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 507 | falcon->WriteConfigs(left_inverted_); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | void Write( |
| 512 | const ::frc971::control_loops::drivetrain::Output &output) override { |
| 513 | ctre::phoenix6::controls::DutyCycleOut left_control( |
| 514 | SafeSpeed(output.left_voltage())); |
| 515 | left_control.UpdateFreqHz = 0_Hz; |
| 516 | left_control.EnableFOC = true; |
| 517 | |
| 518 | ctre::phoenix6::controls::DutyCycleOut right_control( |
| 519 | SafeSpeed(output.right_voltage())); |
| 520 | right_control.UpdateFreqHz = 0_Hz; |
| 521 | right_control.EnableFOC = true; |
| 522 | |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 523 | for (auto falcon : {left_front_.get(), left_back_.get()}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 524 | ctre::phoenix::StatusCode status = |
| 525 | falcon->talon()->SetControl(left_control); |
| 526 | |
| 527 | if (!status.IsOK()) { |
| 528 | AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s", |
| 529 | status.GetName(), status.GetDescription()); |
| 530 | } |
| 531 | } |
| 532 | |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 533 | for (auto falcon : {right_front_.get(), right_back_.get()}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 534 | ctre::phoenix::StatusCode status = |
| 535 | falcon->talon()->SetControl(right_control); |
| 536 | |
| 537 | if (!status.IsOK()) { |
| 538 | AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s", |
| 539 | status.GetName(), status.GetDescription()); |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | void Stop() override { |
| 545 | AOS_LOG(WARNING, "drivetrain output too old\n"); |
| 546 | ctre::phoenix6::controls::DutyCycleOut stop_command(0.0); |
| 547 | stop_command.UpdateFreqHz = 0_Hz; |
| 548 | stop_command.EnableFOC = true; |
| 549 | |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 550 | for (auto falcon : {right_front_.get(), right_back_.get(), |
| 551 | left_front_.get(), left_back_.get()}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 552 | falcon->talon()->SetControl(stop_command); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | double SafeSpeed(double voltage) { |
| 557 | return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
| 558 | } |
| 559 | |
| 560 | ctre::phoenix6::signals::InvertedValue left_inverted_, right_inverted_; |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 561 | std::shared_ptr<Falcon> right_front_, right_back_, left_front_, left_back_; |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 562 | }; |
| 563 | |
| 564 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 565 | public: |
| 566 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 567 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 568 | frc::Encoder::k4X); |
| 569 | } |
| 570 | |
| 571 | void Run() override { |
| 572 | std::shared_ptr<const Values> values = |
| 573 | std::make_shared<const Values>(constants::MakeValues()); |
| 574 | |
| 575 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 576 | aos::configuration::ReadConfig("aos_config.json"); |
| 577 | |
| 578 | // Thread 1. |
| 579 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
| 580 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 581 | &joystick_sender_event_loop); |
| 582 | AddLoop(&joystick_sender_event_loop); |
| 583 | |
| 584 | // Thread 2. |
| 585 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 586 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 587 | AddLoop(&pdp_fetcher_event_loop); |
| 588 | |
| 589 | std::shared_ptr<frc::DigitalOutput> superstructure_reading = |
| 590 | make_unique<frc::DigitalOutput>(25); |
| 591 | |
| 592 | std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry; |
| 593 | std::shared_ptr<Falcon> right_front = |
| 594 | std::make_shared<Falcon>(1, "Drivetrain Bus", &signals_registry); |
| 595 | std::shared_ptr<Falcon> right_back = |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 596 | std::make_shared<Falcon>(0, "Drivetrain Bus", &signals_registry); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 597 | std::shared_ptr<Falcon> left_front = |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 598 | std::make_shared<Falcon>(2, "Drivetrain Bus", &signals_registry); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 599 | std::shared_ptr<Falcon> left_back = |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 600 | std::make_shared<Falcon>(3, "Drivetrain Bus", &signals_registry); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 601 | |
| 602 | // Thread 3. |
| 603 | ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message()); |
| 604 | can_sensor_reader_event_loop.set_name("CANSensorReader"); |
| 605 | CANSensorReader can_sensor_reader(&can_sensor_reader_event_loop, |
| 606 | std::move(signals_registry)); |
| 607 | |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 608 | can_sensor_reader.set_falcons(right_front, right_back, left_front, |
Maxwell Henderson | 4d4be54 | 2023-11-29 18:26:13 -0800 | [diff] [blame^] | 609 | left_back); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 610 | |
| 611 | AddLoop(&can_sensor_reader_event_loop); |
| 612 | |
| 613 | // Thread 4. |
| 614 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
| 615 | SensorReader sensor_reader(&sensor_reader_event_loop, values, |
| 616 | &can_sensor_reader); |
| 617 | sensor_reader.set_pwm_trigger(true); |
Filip Kujawa | 35b5aad | 2023-11-14 21:53:19 -0800 | [diff] [blame] | 618 | sensor_reader.set_drivetrain_left_encoder(make_encoder(4)); |
| 619 | sensor_reader.set_drivetrain_right_encoder(make_encoder(5)); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 620 | sensor_reader.set_superstructure_reading(superstructure_reading); |
Filip Kujawa | 35b5aad | 2023-11-14 21:53:19 -0800 | [diff] [blame] | 621 | sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(3)); |
Maxwell Henderson | 9435b5c | 2023-11-06 13:48:51 -0800 | [diff] [blame] | 622 | |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 623 | AddLoop(&sensor_reader_event_loop); |
| 624 | |
| 625 | // Thread 5. |
| 626 | // Set up CAN. |
| 627 | if (!FLAGS_ctre_diag_server) { |
| 628 | c_Phoenix_Diagnostics_SetSecondsToStart(-1); |
| 629 | c_Phoenix_Diagnostics_Dispose(); |
| 630 | } |
| 631 | |
| 632 | ctre::phoenix::platform::can::CANComm_SetRxSchedPriority( |
| 633 | constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus"); |
| 634 | ctre::phoenix::platform::can::CANComm_SetTxSchedPriority( |
| 635 | constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus"); |
| 636 | |
| 637 | ::aos::ShmEventLoop can_output_event_loop(&config.message()); |
| 638 | can_output_event_loop.set_name("CANOutputWriter"); |
| 639 | DrivetrainWriter drivetrain_writer(&can_output_event_loop); |
| 640 | |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 641 | drivetrain_writer.set_falcons(right_front, right_back, left_front, |
| 642 | left_back); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 643 | drivetrain_writer.set_right_inverted( |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 644 | ctre::phoenix6::signals::InvertedValue::CounterClockwise_Positive); |
Filip Kujawa | 35b5aad | 2023-11-14 21:53:19 -0800 | [diff] [blame] | 645 | drivetrain_writer.set_left_inverted( |
| 646 | ctre::phoenix6::signals::InvertedValue::Clockwise_Positive); |
| 647 | |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 648 | can_output_event_loop.MakeWatcher( |
Maxwell Henderson | 4d4be54 | 2023-11-29 18:26:13 -0800 | [diff] [blame^] | 649 | "/roborio", |
| 650 | [&drivetrain_writer](const frc971::CANConfiguration &configuration) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 651 | drivetrain_writer.HandleCANConfiguration(configuration); |
| 652 | }); |
| 653 | |
| 654 | AddLoop(&can_output_event_loop); |
| 655 | |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 656 | RunLoops(); |
| 657 | } |
| 658 | }; |
| 659 | |
| 660 | } // namespace wpilib |
| 661 | } // namespace y2023_bot3 |
| 662 | |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 663 | AOS_ROBOT_CLASS(::y2023_bot3::wpilib::WPILibRobot); |