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" |
| 27 | #include "ctre/phoenix/motorcontrol/can/TalonFX.h" |
| 28 | #include "ctre/phoenix/motorcontrol/can/TalonSRX.h" |
| 29 | #include "ctre/phoenix6/TalonFX.hpp" |
| 30 | |
| 31 | #include "aos/commonmath.h" |
| 32 | #include "aos/containers/sized_array.h" |
| 33 | #include "aos/events/event_loop.h" |
| 34 | #include "aos/events/shm_event_loop.h" |
| 35 | #include "aos/init.h" |
| 36 | #include "aos/logging/logging.h" |
| 37 | #include "aos/realtime.h" |
| 38 | #include "aos/time/time.h" |
| 39 | #include "aos/util/log_interval.h" |
| 40 | #include "aos/util/phased_loop.h" |
| 41 | #include "aos/util/wrapping_counter.h" |
| 42 | #include "frc971/autonomous/auto_mode_generated.h" |
| 43 | #include "frc971/can_configuration_generated.h" |
| 44 | #include "frc971/control_loops/drivetrain/drivetrain_can_position_generated.h" |
| 45 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
| 46 | #include "frc971/input/robot_state_generated.h" |
| 47 | #include "frc971/queues/gyro_generated.h" |
| 48 | #include "frc971/wpilib/ADIS16448.h" |
| 49 | #include "frc971/wpilib/buffered_pcm.h" |
| 50 | #include "frc971/wpilib/buffered_solenoid.h" |
| 51 | #include "frc971/wpilib/dma.h" |
| 52 | #include "frc971/wpilib/drivetrain_writer.h" |
| 53 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 54 | #include "frc971/wpilib/joystick_sender.h" |
| 55 | #include "frc971/wpilib/logging_generated.h" |
| 56 | #include "frc971/wpilib/loop_output_handler.h" |
| 57 | #include "frc971/wpilib/pdp_fetcher.h" |
| 58 | #include "frc971/wpilib/sensor_reader.h" |
| 59 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 60 | #include "y2023_bot3/constants.h" |
| 61 | #include "y2023_bot3/control_loops/superstructure/led_indicator.h" |
| 62 | #include "y2023_bot3/control_loops/superstructure/superstructure_output_generated.h" |
| 63 | #include "y2023_bot3/control_loops/superstructure/superstructure_position_generated.h" |
| 64 | |
| 65 | DEFINE_bool(ctre_diag_server, false, |
| 66 | "If true, enable the diagnostics server for interacting with " |
| 67 | "devices on the CAN bus using Phoenix Tuner"); |
| 68 | |
| 69 | using ::aos::monotonic_clock; |
| 70 | using ::y2023_bot3::constants::Values; |
| 71 | namespace superstructure = ::y2023_bot3::control_loops::superstructure; |
| 72 | namespace drivetrain = ::y2023_bot3::control_loops::drivetrain; |
| 73 | namespace chrono = ::std::chrono; |
| 74 | using std::make_unique; |
| 75 | |
| 76 | namespace y2023_bot3 { |
| 77 | namespace wpilib { |
| 78 | namespace { |
| 79 | |
| 80 | constexpr double kMaxBringupPower = 12.0; |
| 81 | |
| 82 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 83 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 84 | // The low bit is direction. |
| 85 | |
| 86 | double drivetrain_velocity_translate(double in) { |
| 87 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
| 88 | (2.0 * M_PI)) * |
| 89 | Values::kDrivetrainEncoderRatio() * |
| 90 | control_loops::drivetrain::kWheelRadius; |
| 91 | } |
| 92 | |
| 93 | constexpr double kMaxFastEncoderPulsesPerSecond = std::max({ |
| 94 | Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
Maxwell Henderson | 0d22077 | 2023-11-06 11:09:58 -0800 | [diff] [blame^] | 95 | Values::kMaxPivotJointEncoderPulsesPerSecond(), |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 96 | }); |
| 97 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 98 | "fast encoders are too fast"); |
| 99 | |
| 100 | } // namespace |
| 101 | |
| 102 | static constexpr int kCANFalconCount = 6; |
| 103 | static constexpr units::frequency::hertz_t kCANUpdateFreqHz = 200_Hz; |
| 104 | |
| 105 | class Falcon { |
| 106 | public: |
| 107 | Falcon(int device_id, std::string canbus, |
| 108 | std::vector<ctre::phoenix6::BaseStatusSignal *> *signals) |
| 109 | : talon_(device_id, canbus), |
| 110 | device_id_(device_id), |
| 111 | device_temp_(talon_.GetDeviceTemp()), |
| 112 | supply_voltage_(talon_.GetSupplyVoltage()), |
| 113 | supply_current_(talon_.GetSupplyCurrent()), |
| 114 | torque_current_(talon_.GetTorqueCurrent()), |
| 115 | position_(talon_.GetPosition()), |
| 116 | duty_cycle_(talon_.GetDutyCycle()) { |
| 117 | // device temp is not timesynced so don't add it to the list of signals |
| 118 | device_temp_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 119 | |
| 120 | CHECK_NOTNULL(signals); |
| 121 | |
| 122 | supply_voltage_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 123 | signals->push_back(&supply_voltage_); |
| 124 | |
| 125 | supply_current_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 126 | signals->push_back(&supply_current_); |
| 127 | |
| 128 | torque_current_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 129 | signals->push_back(&torque_current_); |
| 130 | |
| 131 | position_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 132 | signals->push_back(&position_); |
| 133 | |
| 134 | duty_cycle_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 135 | signals->push_back(&duty_cycle_); |
| 136 | } |
| 137 | |
| 138 | void PrintConfigs() { |
| 139 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
| 140 | ctre::phoenix::StatusCode status = |
| 141 | talon_.GetConfigurator().Refresh(configuration); |
| 142 | if (!status.IsOK()) { |
| 143 | AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s", |
| 144 | status.GetName(), status.GetDescription()); |
| 145 | } |
| 146 | AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str()); |
| 147 | } |
| 148 | |
| 149 | void WriteConfigs(ctre::phoenix6::signals::InvertedValue invert) { |
| 150 | inverted_ = invert; |
| 151 | |
| 152 | ctre::phoenix6::configs::CurrentLimitsConfigs current_limits; |
| 153 | current_limits.StatorCurrentLimit = |
| 154 | constants::Values::kDrivetrainStatorCurrentLimit(); |
| 155 | current_limits.StatorCurrentLimitEnable = true; |
| 156 | current_limits.SupplyCurrentLimit = |
| 157 | constants::Values::kDrivetrainSupplyCurrentLimit(); |
| 158 | current_limits.SupplyCurrentLimitEnable = true; |
| 159 | |
| 160 | ctre::phoenix6::configs::MotorOutputConfigs output_configs; |
| 161 | output_configs.NeutralMode = |
| 162 | ctre::phoenix6::signals::NeutralModeValue::Brake; |
| 163 | output_configs.DutyCycleNeutralDeadband = 0; |
| 164 | |
| 165 | output_configs.Inverted = inverted_; |
| 166 | |
| 167 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
| 168 | configuration.CurrentLimits = current_limits; |
| 169 | configuration.MotorOutput = output_configs; |
| 170 | |
| 171 | ctre::phoenix::StatusCode status = |
| 172 | talon_.GetConfigurator().Apply(configuration); |
| 173 | if (!status.IsOK()) { |
| 174 | AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s", |
| 175 | status.GetName(), status.GetDescription()); |
| 176 | } |
| 177 | |
| 178 | PrintConfigs(); |
| 179 | } |
| 180 | |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 181 | void WriteRollerConfigs() { |
| 182 | ctre::phoenix6::configs::CurrentLimitsConfigs current_limits; |
| 183 | current_limits.StatorCurrentLimit = |
| 184 | constants::Values::kRollerStatorCurrentLimit(); |
| 185 | current_limits.StatorCurrentLimitEnable = true; |
| 186 | current_limits.SupplyCurrentLimit = |
| 187 | constants::Values::kRollerSupplyCurrentLimit(); |
| 188 | current_limits.SupplyCurrentLimitEnable = true; |
| 189 | |
| 190 | ctre::phoenix6::configs::MotorOutputConfigs output_configs; |
| 191 | output_configs.NeutralMode = |
| 192 | ctre::phoenix6::signals::NeutralModeValue::Brake; |
| 193 | output_configs.DutyCycleNeutralDeadband = 0; |
| 194 | |
| 195 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
| 196 | configuration.CurrentLimits = current_limits; |
| 197 | configuration.MotorOutput = output_configs; |
| 198 | |
| 199 | ctre::phoenix::StatusCode status = |
| 200 | talon_.GetConfigurator().Apply(configuration); |
| 201 | if (!status.IsOK()) { |
| 202 | AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s", |
| 203 | status.GetName(), status.GetDescription()); |
| 204 | } |
| 205 | |
| 206 | PrintConfigs(); |
| 207 | } |
| 208 | |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 209 | ctre::phoenix6::hardware::TalonFX *talon() { return &talon_; } |
| 210 | |
| 211 | flatbuffers::Offset<frc971::control_loops::CANFalcon> WritePosition( |
| 212 | flatbuffers::FlatBufferBuilder *fbb) { |
| 213 | frc971::control_loops::CANFalcon::Builder builder(*fbb); |
| 214 | builder.add_id(device_id_); |
| 215 | builder.add_device_temp(device_temp()); |
| 216 | builder.add_supply_voltage(supply_voltage()); |
| 217 | builder.add_supply_current(supply_current()); |
| 218 | builder.add_torque_current(torque_current()); |
| 219 | builder.add_duty_cycle(duty_cycle()); |
| 220 | |
| 221 | double invert = |
| 222 | (inverted_ == ctre::phoenix6::signals::InvertedValue::Clockwise_Positive |
| 223 | ? 1 |
| 224 | : -1); |
| 225 | |
| 226 | builder.add_position( |
| 227 | constants::Values::DrivetrainCANEncoderToMeters(position()) * invert); |
| 228 | |
| 229 | return builder.Finish(); |
| 230 | } |
| 231 | |
| 232 | int device_id() const { return device_id_; } |
| 233 | float device_temp() const { return device_temp_.GetValue().value(); } |
| 234 | float supply_voltage() const { return supply_voltage_.GetValue().value(); } |
| 235 | float supply_current() const { return supply_current_.GetValue().value(); } |
| 236 | float torque_current() const { return torque_current_.GetValue().value(); } |
| 237 | float duty_cycle() const { return duty_cycle_.GetValue().value(); } |
| 238 | float position() const { return position_.GetValue().value(); } |
| 239 | |
| 240 | // returns the monotonic timestamp of the latest timesynced reading in the |
| 241 | // timebase of the the syncronized CAN bus clock. |
| 242 | int64_t GetTimestamp() { |
| 243 | std::chrono::nanoseconds latest_timestamp = |
| 244 | torque_current_.GetTimestamp().GetTime(); |
| 245 | |
| 246 | return latest_timestamp.count(); |
| 247 | } |
| 248 | |
| 249 | void RefreshNontimesyncedSignals() { device_temp_.Refresh(); }; |
| 250 | |
| 251 | private: |
| 252 | ctre::phoenix6::hardware::TalonFX talon_; |
| 253 | int device_id_; |
| 254 | |
| 255 | ctre::phoenix6::signals::InvertedValue inverted_; |
| 256 | |
| 257 | ctre::phoenix6::StatusSignal<units::temperature::celsius_t> device_temp_; |
| 258 | ctre::phoenix6::StatusSignal<units::voltage::volt_t> supply_voltage_; |
| 259 | ctre::phoenix6::StatusSignal<units::current::ampere_t> supply_current_, |
| 260 | torque_current_; |
| 261 | ctre::phoenix6::StatusSignal<units::angle::turn_t> position_; |
| 262 | ctre::phoenix6::StatusSignal<units::dimensionless::scalar_t> duty_cycle_; |
| 263 | }; |
| 264 | |
| 265 | class CANSensorReader { |
| 266 | public: |
| 267 | CANSensorReader( |
| 268 | aos::EventLoop *event_loop, |
| 269 | std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry) |
| 270 | : event_loop_(event_loop), |
| 271 | signals_(signals_registry.begin(), signals_registry.end()), |
| 272 | can_position_sender_( |
| 273 | event_loop |
| 274 | ->MakeSender<frc971::control_loops::drivetrain::CANPosition>( |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 275 | "/drivetrain")), |
| 276 | roller_falcon_data_(std::nullopt) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 277 | event_loop->SetRuntimeRealtimePriority(40); |
| 278 | event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({1})); |
| 279 | timer_handler_ = event_loop->AddTimer([this]() { Loop(); }); |
| 280 | timer_handler_->set_name("CANSensorReader Loop"); |
| 281 | |
| 282 | event_loop->OnRun([this]() { |
| 283 | timer_handler_->Schedule(event_loop_->monotonic_now(), |
| 284 | 1 / kCANUpdateFreqHz); |
| 285 | }); |
| 286 | } |
| 287 | |
| 288 | void set_falcons(std::shared_ptr<Falcon> right_front, |
| 289 | std::shared_ptr<Falcon> right_back, |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 290 | std::shared_ptr<Falcon> left_front, |
Ariv Diggi | c892e92 | 2023-10-21 15:52:06 -0700 | [diff] [blame] | 291 | std::shared_ptr<Falcon> left_back, |
| 292 | std::shared_ptr<Falcon> roller_falcon) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 293 | right_front_ = std::move(right_front); |
| 294 | right_back_ = std::move(right_back); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 295 | left_front_ = std::move(left_front); |
| 296 | left_back_ = std::move(left_back); |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 297 | roller_falcon_ = std::move(roller_falcon); |
Ariv Diggi | c892e92 | 2023-10-21 15:52:06 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | std::optional<frc971::control_loops::CANFalconT> roller_falcon_data() { |
| 301 | std::unique_lock<aos::stl_mutex> lock(roller_mutex_); |
| 302 | return roller_falcon_data_; |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | private: |
| 306 | void Loop() { |
| 307 | ctre::phoenix::StatusCode status = |
| 308 | ctre::phoenix6::BaseStatusSignal::WaitForAll(2000_ms, signals_); |
| 309 | |
| 310 | if (!status.IsOK()) { |
| 311 | AOS_LOG(ERROR, "Failed to read signals from falcons: %s: %s", |
| 312 | status.GetName(), status.GetDescription()); |
| 313 | } |
| 314 | |
| 315 | auto builder = can_position_sender_.MakeBuilder(); |
| 316 | |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 317 | for (auto falcon : |
| 318 | {right_front_, right_back_, left_front_, left_back_, roller_falcon_}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 319 | falcon->RefreshNontimesyncedSignals(); |
| 320 | } |
| 321 | |
| 322 | aos::SizedArray<flatbuffers::Offset<frc971::control_loops::CANFalcon>, |
| 323 | kCANFalconCount> |
| 324 | falcons; |
| 325 | |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 326 | for (auto falcon : {right_front_, right_back_, left_front_, left_back_}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 327 | falcons.push_back(falcon->WritePosition(builder.fbb())); |
| 328 | } |
| 329 | |
| 330 | auto falcons_list = |
| 331 | builder.fbb() |
| 332 | ->CreateVector< |
| 333 | flatbuffers::Offset<frc971::control_loops::CANFalcon>>(falcons); |
| 334 | |
| 335 | frc971::control_loops::drivetrain::CANPosition::Builder |
| 336 | can_position_builder = |
| 337 | builder |
| 338 | .MakeBuilder<frc971::control_loops::drivetrain::CANPosition>(); |
| 339 | |
| 340 | can_position_builder.add_falcons(falcons_list); |
| 341 | can_position_builder.add_timestamp(right_front_->GetTimestamp()); |
| 342 | can_position_builder.add_status(static_cast<int>(status)); |
| 343 | |
| 344 | builder.CheckOk(builder.Send(can_position_builder.Finish())); |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 345 | |
| 346 | { |
| 347 | std::unique_lock<aos::stl_mutex> lock(roller_mutex_); |
| 348 | frc971::control_loops::CANFalconT roller_falcon_data; |
| 349 | roller_falcon_data.id = roller_falcon_->device_id(); |
| 350 | roller_falcon_data.supply_current = roller_falcon_->supply_current(); |
| 351 | roller_falcon_data.torque_current = -roller_falcon_->torque_current(); |
| 352 | roller_falcon_data.supply_voltage = roller_falcon_->supply_voltage(); |
| 353 | roller_falcon_data.device_temp = roller_falcon_->device_temp(); |
| 354 | roller_falcon_data.position = -roller_falcon_->position(); |
| 355 | roller_falcon_data.duty_cycle = roller_falcon_->duty_cycle(); |
| 356 | roller_falcon_data_ = |
| 357 | std::make_optional<frc971::control_loops::CANFalconT>( |
| 358 | roller_falcon_data); |
| 359 | } |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | aos::EventLoop *event_loop_; |
| 363 | |
| 364 | const std::vector<ctre::phoenix6::BaseStatusSignal *> signals_; |
| 365 | aos::Sender<frc971::control_loops::drivetrain::CANPosition> |
| 366 | can_position_sender_; |
| 367 | |
Ariv Diggi | c892e92 | 2023-10-21 15:52:06 -0700 | [diff] [blame] | 368 | std::shared_ptr<Falcon> right_front_, right_back_, left_front_, left_back_, |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 369 | roller_falcon_; |
Ariv Diggi | c892e92 | 2023-10-21 15:52:06 -0700 | [diff] [blame] | 370 | |
| 371 | std::optional<frc971::control_loops::CANFalconT> roller_falcon_data_; |
| 372 | |
| 373 | aos::stl_mutex roller_mutex_; |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 374 | |
| 375 | // Pointer to the timer handler used to modify the wakeup. |
| 376 | ::aos::TimerHandler *timer_handler_; |
| 377 | }; |
| 378 | |
| 379 | // Class to send position messages with sensor readings to our loops. |
| 380 | class SensorReader : public ::frc971::wpilib::SensorReader { |
| 381 | public: |
| 382 | SensorReader(::aos::ShmEventLoop *event_loop, |
| 383 | std::shared_ptr<const Values> values, |
| 384 | CANSensorReader *can_sensor_reader) |
| 385 | : ::frc971::wpilib::SensorReader(event_loop), |
| 386 | values_(std::move(values)), |
| 387 | auto_mode_sender_( |
| 388 | event_loop->MakeSender<::frc971::autonomous::AutonomousMode>( |
| 389 | "/autonomous")), |
| 390 | superstructure_position_sender_( |
| 391 | event_loop->MakeSender<superstructure::Position>( |
| 392 | "/superstructure")), |
| 393 | drivetrain_position_sender_( |
| 394 | event_loop |
| 395 | ->MakeSender<::frc971::control_loops::drivetrain::Position>( |
| 396 | "/drivetrain")), |
| 397 | gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>( |
| 398 | "/drivetrain")), |
| 399 | can_sensor_reader_(can_sensor_reader) { |
| 400 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 401 | // we should ever see. |
| 402 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 403 | event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0})); |
| 404 | } |
| 405 | |
| 406 | void Start() override { AddToDMA(&imu_yaw_rate_reader_); } |
| 407 | |
| 408 | // Auto mode switches. |
| 409 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
| 410 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 411 | } |
| 412 | |
| 413 | void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) { |
| 414 | imu_yaw_rate_input_ = ::std::move(sensor); |
| 415 | imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get()); |
| 416 | } |
| 417 | |
| 418 | void RunIteration() override { |
| 419 | superstructure_reading_->Set(true); |
Ariv Diggi | c892e92 | 2023-10-21 15:52:06 -0700 | [diff] [blame] | 420 | { |
| 421 | auto builder = superstructure_position_sender_.MakeBuilder(); |
| 422 | |
| 423 | flatbuffers::Offset<frc971::control_loops::CANFalcon> |
| 424 | roller_falcon_offset; |
| 425 | auto optional_roller_falcon = can_sensor_reader_->roller_falcon_data(); |
| 426 | if (optional_roller_falcon.has_value()) { |
| 427 | roller_falcon_offset = frc971::control_loops::CANFalcon::Pack( |
| 428 | *builder.fbb(), &optional_roller_falcon.value()); |
| 429 | } |
Ariv Diggi | c892e92 | 2023-10-21 15:52:06 -0700 | [diff] [blame] | 430 | superstructure::Position::Builder position_builder = |
| 431 | builder.MakeBuilder<superstructure::Position>(); |
| 432 | position_builder.add_end_effector_cube_beam_break( |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 433 | !end_effector_cube_beam_break_->Get()); |
Maxwell Henderson | 0d22077 | 2023-11-06 11:09:58 -0800 | [diff] [blame^] | 434 | |
Ariv Diggi | c892e92 | 2023-10-21 15:52:06 -0700 | [diff] [blame] | 435 | if (!roller_falcon_offset.IsNull()) { |
| 436 | position_builder.add_roller_falcon(roller_falcon_offset); |
| 437 | } |
| 438 | builder.CheckOk(builder.Send(position_builder.Finish())); |
| 439 | } |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 440 | |
| 441 | { |
| 442 | auto builder = drivetrain_position_sender_.MakeBuilder(); |
| 443 | frc971::control_loops::drivetrain::Position::Builder drivetrain_builder = |
| 444 | builder.MakeBuilder<frc971::control_loops::drivetrain::Position>(); |
| 445 | drivetrain_builder.add_left_encoder( |
| 446 | constants::Values::DrivetrainEncoderToMeters( |
| 447 | drivetrain_left_encoder_->GetRaw())); |
| 448 | drivetrain_builder.add_left_speed( |
| 449 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod())); |
| 450 | |
| 451 | drivetrain_builder.add_right_encoder( |
| 452 | -constants::Values::DrivetrainEncoderToMeters( |
| 453 | drivetrain_right_encoder_->GetRaw())); |
| 454 | drivetrain_builder.add_right_speed(-drivetrain_velocity_translate( |
| 455 | drivetrain_right_encoder_->GetPeriod())); |
| 456 | |
| 457 | builder.CheckOk(builder.Send(drivetrain_builder.Finish())); |
| 458 | } |
| 459 | |
| 460 | { |
| 461 | auto builder = gyro_sender_.MakeBuilder(); |
| 462 | ::frc971::sensors::GyroReading::Builder gyro_reading_builder = |
| 463 | builder.MakeBuilder<::frc971::sensors::GyroReading>(); |
| 464 | // +/- 2000 deg / sec |
| 465 | constexpr double kMaxVelocity = 4000; // degrees / second |
| 466 | constexpr double kVelocityRadiansPerSecond = |
| 467 | kMaxVelocity / 360 * (2.0 * M_PI); |
| 468 | |
| 469 | // Only part of the full range is used to prevent being 100% on or off. |
| 470 | constexpr double kScaledRangeLow = 0.1; |
| 471 | constexpr double kScaledRangeHigh = 0.9; |
| 472 | |
| 473 | constexpr double kPWMFrequencyHz = 200; |
| 474 | double velocity_duty_cycle = |
| 475 | imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz; |
| 476 | |
| 477 | constexpr double kDutyCycleScale = |
| 478 | 1 / (kScaledRangeHigh - kScaledRangeLow); |
| 479 | // scale from 0.1 - 0.9 to 0 - 1 |
| 480 | double rescaled_velocity_duty_cycle = |
| 481 | (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale; |
| 482 | |
| 483 | if (!std::isnan(rescaled_velocity_duty_cycle)) { |
| 484 | gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) * |
| 485 | kVelocityRadiansPerSecond); |
| 486 | } |
| 487 | builder.CheckOk(builder.Send(gyro_reading_builder.Finish())); |
| 488 | } |
| 489 | |
| 490 | { |
| 491 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 492 | |
| 493 | uint32_t mode = 0; |
| 494 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 495 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 496 | mode |= 1 << i; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | auto auto_mode_builder = |
| 501 | builder.MakeBuilder<frc971::autonomous::AutonomousMode>(); |
| 502 | |
| 503 | auto_mode_builder.add_mode(mode); |
| 504 | |
| 505 | builder.CheckOk(builder.Send(auto_mode_builder.Finish())); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | std::shared_ptr<frc::DigitalOutput> superstructure_reading_; |
| 510 | |
| 511 | void set_superstructure_reading( |
| 512 | std::shared_ptr<frc::DigitalOutput> superstructure_reading) { |
| 513 | superstructure_reading_ = superstructure_reading; |
| 514 | } |
| 515 | |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 516 | void set_end_effector_cube_beam_break( |
| 517 | ::std::unique_ptr<frc::DigitalInput> sensor) { |
| 518 | end_effector_cube_beam_break_ = ::std::move(sensor); |
| 519 | } |
| 520 | |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 521 | private: |
| 522 | std::shared_ptr<const Values> values_; |
| 523 | |
| 524 | aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_; |
| 525 | aos::Sender<superstructure::Position> superstructure_position_sender_; |
| 526 | aos::Sender<frc971::control_loops::drivetrain::Position> |
| 527 | drivetrain_position_sender_; |
| 528 | ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_; |
| 529 | |
| 530 | std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_; |
| 531 | |
Ariv Diggi | c892e92 | 2023-10-21 15:52:06 -0700 | [diff] [blame] | 532 | std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_, |
| 533 | end_effector_cube_beam_break_; |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 534 | |
| 535 | frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_; |
| 536 | |
| 537 | CANSensorReader *can_sensor_reader_; |
| 538 | }; |
| 539 | |
| 540 | class SuperstructureWriter |
| 541 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
| 542 | public: |
| 543 | SuperstructureWriter(aos::EventLoop *event_loop) |
| 544 | : frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 545 | event_loop, "/superstructure") { |
| 546 | event_loop->SetRuntimeRealtimePriority( |
| 547 | constants::Values::kDrivetrainWriterPriority); |
| 548 | } |
| 549 | |
| 550 | std::shared_ptr<frc::DigitalOutput> superstructure_reading_; |
| 551 | |
| 552 | void set_superstructure_reading( |
| 553 | std::shared_ptr<frc::DigitalOutput> superstructure_reading) { |
| 554 | superstructure_reading_ = superstructure_reading; |
| 555 | } |
| 556 | |
| 557 | private: |
| 558 | void Stop() override { AOS_LOG(WARNING, "Superstructure output too old.\n"); } |
| 559 | |
| 560 | void Write(const superstructure::Output &output) override { (void)output; } |
| 561 | |
| 562 | static void WriteCan(const double voltage, |
| 563 | ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) { |
| 564 | falcon->Set( |
| 565 | ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
| 566 | std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
| 567 | } |
| 568 | |
| 569 | template <typename T> |
| 570 | static void WritePwm(const double voltage, T *motor) { |
| 571 | motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / |
| 572 | 12.0); |
| 573 | } |
| 574 | }; |
| 575 | |
| 576 | class SuperstructureCANWriter |
| 577 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
| 578 | public: |
| 579 | SuperstructureCANWriter(::aos::EventLoop *event_loop) |
| 580 | : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 581 | event_loop, "/superstructure") { |
| 582 | event_loop->SetRuntimeRealtimePriority( |
| 583 | constants::Values::kSuperstructureCANWriterPriority); |
| 584 | |
| 585 | event_loop->OnRun([this]() { WriteConfigs(); }); |
| 586 | }; |
| 587 | |
| 588 | void HandleCANConfiguration(const frc971::CANConfiguration &configuration) { |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 589 | roller_falcon_->PrintConfigs(); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 590 | if (configuration.reapply()) { |
| 591 | WriteConfigs(); |
| 592 | } |
| 593 | } |
| 594 | |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 595 | void set_roller_falcon(std::shared_ptr<Falcon> roller_falcon) { |
| 596 | roller_falcon_ = std::move(roller_falcon); |
| 597 | } |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 598 | |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 599 | private: |
| 600 | void WriteConfigs() { roller_falcon_->WriteRollerConfigs(); } |
| 601 | |
| 602 | void Write(const superstructure::Output &output) override { |
| 603 | ctre::phoenix6::controls::DutyCycleOut roller_control( |
| 604 | SafeSpeed(-output.roller_voltage())); |
| 605 | roller_control.UpdateFreqHz = 0_Hz; |
| 606 | roller_control.EnableFOC = true; |
| 607 | |
| 608 | ctre::phoenix::StatusCode status = |
| 609 | roller_falcon_->talon()->SetControl(roller_control); |
| 610 | |
| 611 | if (!status.IsOK()) { |
| 612 | AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s", |
| 613 | status.GetName(), status.GetDescription()); |
| 614 | } |
| 615 | } |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 616 | |
| 617 | void Stop() override { |
| 618 | AOS_LOG(WARNING, "Superstructure CAN output too old.\n"); |
| 619 | ctre::phoenix6::controls::DutyCycleOut stop_command(0.0); |
| 620 | stop_command.UpdateFreqHz = 0_Hz; |
| 621 | stop_command.EnableFOC = true; |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 622 | |
| 623 | roller_falcon_->talon()->SetControl(stop_command); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | double SafeSpeed(double voltage) { |
| 627 | return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
| 628 | } |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 629 | |
| 630 | std::shared_ptr<Falcon> roller_falcon_; |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 631 | }; |
| 632 | |
| 633 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler< |
| 634 | ::frc971::control_loops::drivetrain::Output> { |
| 635 | public: |
| 636 | DrivetrainWriter(::aos::EventLoop *event_loop) |
| 637 | : ::frc971::wpilib::LoopOutputHandler< |
| 638 | ::frc971::control_loops::drivetrain::Output>(event_loop, |
| 639 | "/drivetrain") { |
| 640 | event_loop->SetRuntimeRealtimePriority( |
| 641 | constants::Values::kDrivetrainWriterPriority); |
| 642 | |
| 643 | event_loop->OnRun([this]() { WriteConfigs(); }); |
| 644 | } |
| 645 | |
| 646 | void set_falcons(std::shared_ptr<Falcon> right_front, |
| 647 | std::shared_ptr<Falcon> right_back, |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 648 | std::shared_ptr<Falcon> left_front, |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 649 | std::shared_ptr<Falcon> left_back) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 650 | right_front_ = std::move(right_front); |
| 651 | right_back_ = std::move(right_back); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 652 | left_front_ = std::move(left_front); |
| 653 | left_back_ = std::move(left_back); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | void set_right_inverted(ctre::phoenix6::signals::InvertedValue invert) { |
| 657 | right_inverted_ = invert; |
| 658 | } |
| 659 | |
| 660 | void set_left_inverted(ctre::phoenix6::signals::InvertedValue invert) { |
| 661 | left_inverted_ = invert; |
| 662 | } |
| 663 | |
| 664 | void HandleCANConfiguration(const frc971::CANConfiguration &configuration) { |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 665 | for (auto falcon : {right_front_, right_back_, left_front_, left_back_}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 666 | falcon->PrintConfigs(); |
| 667 | } |
| 668 | if (configuration.reapply()) { |
| 669 | WriteConfigs(); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | private: |
| 674 | void WriteConfigs() { |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 675 | for (auto falcon : {right_front_.get(), right_back_.get()}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 676 | falcon->WriteConfigs(right_inverted_); |
| 677 | } |
| 678 | |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 679 | for (auto falcon : {left_front_.get(), left_back_.get()}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 680 | falcon->WriteConfigs(left_inverted_); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | void Write( |
| 685 | const ::frc971::control_loops::drivetrain::Output &output) override { |
| 686 | ctre::phoenix6::controls::DutyCycleOut left_control( |
| 687 | SafeSpeed(output.left_voltage())); |
| 688 | left_control.UpdateFreqHz = 0_Hz; |
| 689 | left_control.EnableFOC = true; |
| 690 | |
| 691 | ctre::phoenix6::controls::DutyCycleOut right_control( |
| 692 | SafeSpeed(output.right_voltage())); |
| 693 | right_control.UpdateFreqHz = 0_Hz; |
| 694 | right_control.EnableFOC = true; |
| 695 | |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 696 | for (auto falcon : {left_front_.get(), left_back_.get()}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 697 | ctre::phoenix::StatusCode status = |
| 698 | falcon->talon()->SetControl(left_control); |
| 699 | |
| 700 | if (!status.IsOK()) { |
| 701 | AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s", |
| 702 | status.GetName(), status.GetDescription()); |
| 703 | } |
| 704 | } |
| 705 | |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 706 | for (auto falcon : {right_front_.get(), right_back_.get()}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 707 | ctre::phoenix::StatusCode status = |
| 708 | falcon->talon()->SetControl(right_control); |
| 709 | |
| 710 | if (!status.IsOK()) { |
| 711 | AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s", |
| 712 | status.GetName(), status.GetDescription()); |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | void Stop() override { |
| 718 | AOS_LOG(WARNING, "drivetrain output too old\n"); |
| 719 | ctre::phoenix6::controls::DutyCycleOut stop_command(0.0); |
| 720 | stop_command.UpdateFreqHz = 0_Hz; |
| 721 | stop_command.EnableFOC = true; |
| 722 | |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 723 | for (auto falcon : {right_front_.get(), right_back_.get(), |
| 724 | left_front_.get(), left_back_.get()}) { |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 725 | falcon->talon()->SetControl(stop_command); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | double SafeSpeed(double voltage) { |
| 730 | return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
| 731 | } |
| 732 | |
| 733 | ctre::phoenix6::signals::InvertedValue left_inverted_, right_inverted_; |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 734 | std::shared_ptr<Falcon> right_front_, right_back_, left_front_, left_back_; |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 735 | }; |
| 736 | |
| 737 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 738 | public: |
| 739 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 740 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 741 | frc::Encoder::k4X); |
| 742 | } |
| 743 | |
| 744 | void Run() override { |
| 745 | std::shared_ptr<const Values> values = |
| 746 | std::make_shared<const Values>(constants::MakeValues()); |
| 747 | |
| 748 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 749 | aos::configuration::ReadConfig("aos_config.json"); |
| 750 | |
| 751 | // Thread 1. |
| 752 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
| 753 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 754 | &joystick_sender_event_loop); |
| 755 | AddLoop(&joystick_sender_event_loop); |
| 756 | |
| 757 | // Thread 2. |
| 758 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 759 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 760 | AddLoop(&pdp_fetcher_event_loop); |
| 761 | |
| 762 | std::shared_ptr<frc::DigitalOutput> superstructure_reading = |
| 763 | make_unique<frc::DigitalOutput>(25); |
| 764 | |
| 765 | std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry; |
| 766 | std::shared_ptr<Falcon> right_front = |
| 767 | std::make_shared<Falcon>(1, "Drivetrain Bus", &signals_registry); |
| 768 | std::shared_ptr<Falcon> right_back = |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 769 | std::make_shared<Falcon>(0, "Drivetrain Bus", &signals_registry); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 770 | std::shared_ptr<Falcon> left_front = |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 771 | std::make_shared<Falcon>(2, "Drivetrain Bus", &signals_registry); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 772 | std::shared_ptr<Falcon> left_back = |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 773 | std::make_shared<Falcon>(3, "Drivetrain Bus", &signals_registry); |
| 774 | std::shared_ptr<Falcon> roller = |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 775 | std::make_shared<Falcon>(5, "Drivetrain Bus", &signals_registry); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 776 | |
| 777 | // Thread 3. |
| 778 | ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message()); |
| 779 | can_sensor_reader_event_loop.set_name("CANSensorReader"); |
| 780 | CANSensorReader can_sensor_reader(&can_sensor_reader_event_loop, |
| 781 | std::move(signals_registry)); |
| 782 | |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 783 | can_sensor_reader.set_falcons(right_front, right_back, left_front, |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 784 | left_back, roller); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 785 | |
| 786 | AddLoop(&can_sensor_reader_event_loop); |
| 787 | |
| 788 | // Thread 4. |
| 789 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
| 790 | SensorReader sensor_reader(&sensor_reader_event_loop, values, |
| 791 | &can_sensor_reader); |
| 792 | sensor_reader.set_pwm_trigger(true); |
| 793 | sensor_reader.set_drivetrain_left_encoder(make_encoder(1)); |
| 794 | sensor_reader.set_drivetrain_right_encoder(make_encoder(0)); |
| 795 | sensor_reader.set_superstructure_reading(superstructure_reading); |
| 796 | sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0)); |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 797 | sensor_reader.set_end_effector_cube_beam_break( |
| 798 | make_unique<frc::DigitalInput>(22)); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 799 | |
| 800 | AddLoop(&sensor_reader_event_loop); |
| 801 | |
| 802 | // Thread 5. |
| 803 | // Set up CAN. |
| 804 | if (!FLAGS_ctre_diag_server) { |
| 805 | c_Phoenix_Diagnostics_SetSecondsToStart(-1); |
| 806 | c_Phoenix_Diagnostics_Dispose(); |
| 807 | } |
| 808 | |
| 809 | ctre::phoenix::platform::can::CANComm_SetRxSchedPriority( |
| 810 | constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus"); |
| 811 | ctre::phoenix::platform::can::CANComm_SetTxSchedPriority( |
| 812 | constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus"); |
| 813 | |
| 814 | ::aos::ShmEventLoop can_output_event_loop(&config.message()); |
| 815 | can_output_event_loop.set_name("CANOutputWriter"); |
| 816 | DrivetrainWriter drivetrain_writer(&can_output_event_loop); |
| 817 | |
Mirabel Wang | f4e4267 | 2023-10-14 13:12:49 -0700 | [diff] [blame] | 818 | drivetrain_writer.set_falcons(right_front, right_back, left_front, |
| 819 | left_back); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 820 | drivetrain_writer.set_right_inverted( |
| 821 | ctre::phoenix6::signals::InvertedValue::Clockwise_Positive); |
| 822 | drivetrain_writer.set_left_inverted( |
| 823 | ctre::phoenix6::signals::InvertedValue::CounterClockwise_Positive); |
| 824 | |
| 825 | can_output_event_loop.MakeWatcher( |
| 826 | "/roborio", |
| 827 | [&drivetrain_writer](const frc971::CANConfiguration &configuration) { |
| 828 | drivetrain_writer.HandleCANConfiguration(configuration); |
| 829 | }); |
| 830 | |
| 831 | AddLoop(&can_output_event_loop); |
| 832 | |
| 833 | // Thread 6 |
| 834 | // Set up superstructure output. |
| 835 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
| 836 | output_event_loop.set_name("PWMOutputWriter"); |
| 837 | SuperstructureWriter superstructure_writer(&output_event_loop); |
| 838 | |
| 839 | superstructure_writer.set_superstructure_reading(superstructure_reading); |
| 840 | |
| 841 | AddLoop(&output_event_loop); |
| 842 | |
| 843 | // Thread 7 |
Maxwell Henderson | 3772d28 | 2023-11-06 11:07:49 -0800 | [diff] [blame] | 844 | // Setup superstructure CAN output. |
| 845 | SuperstructureCANWriter superstructure_can_writer(&can_output_event_loop); |
| 846 | superstructure_can_writer.set_roller_falcon(roller); |
| 847 | |
| 848 | can_output_event_loop.MakeWatcher( |
| 849 | "/roborio", [&drivetrain_writer, &superstructure_can_writer]( |
| 850 | const frc971::CANConfiguration &configuration) { |
| 851 | drivetrain_writer.HandleCANConfiguration(configuration); |
| 852 | superstructure_can_writer.HandleCANConfiguration(configuration); |
| 853 | }); |
| 854 | |
| 855 | AddLoop(&can_output_event_loop); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 856 | |
| 857 | RunLoops(); |
| 858 | } |
| 859 | }; |
| 860 | |
| 861 | } // namespace wpilib |
| 862 | } // namespace y2023_bot3 |
| 863 | |
| 864 | AOS_ROBOT_CLASS(::y2023_bot3::wpilib::WPILibRobot); |