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