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