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