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" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 15 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 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 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 26 | #include "ctre/phoenix/cci/Diagnostics_CCI.h" |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 27 | #include "ctre/phoenix6/TalonFX.hpp" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 28 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 29 | #include "aos/commonmath.h" |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 30 | #include "aos/containers/sized_array.h" |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 31 | #include "aos/events/event_loop.h" |
| 32 | #include "aos/events/shm_event_loop.h" |
| 33 | #include "aos/init.h" |
| 34 | #include "aos/logging/logging.h" |
| 35 | #include "aos/realtime.h" |
| 36 | #include "aos/time/time.h" |
| 37 | #include "aos/util/log_interval.h" |
| 38 | #include "aos/util/phased_loop.h" |
| 39 | #include "aos/util/wrapping_counter.h" |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 40 | #include "frc971/autonomous/auto_mode_generated.h" |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 41 | #include "frc971/can_configuration_generated.h" |
James Kuszmaul | 630ab1d | 2024-01-09 16:38:57 -0800 | [diff] [blame] | 42 | #include "frc971/constants/constants_sender_lib.h" |
Maxwell Henderson | cef6f04 | 2023-05-26 14:38:09 -0700 | [diff] [blame] | 43 | #include "frc971/control_loops/drivetrain/drivetrain_can_position_generated.h" |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 44 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
| 45 | #include "frc971/input/robot_state_generated.h" |
| 46 | #include "frc971/queues/gyro_generated.h" |
| 47 | #include "frc971/wpilib/ADIS16448.h" |
| 48 | #include "frc971/wpilib/buffered_pcm.h" |
| 49 | #include "frc971/wpilib/buffered_solenoid.h" |
| 50 | #include "frc971/wpilib/dma.h" |
| 51 | #include "frc971/wpilib/drivetrain_writer.h" |
| 52 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 53 | #include "frc971/wpilib/joystick_sender.h" |
| 54 | #include "frc971/wpilib/logging_generated.h" |
| 55 | #include "frc971/wpilib/loop_output_handler.h" |
| 56 | #include "frc971/wpilib/pdp_fetcher.h" |
| 57 | #include "frc971/wpilib/sensor_reader.h" |
| 58 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 59 | #include "y2023/constants.h" |
James Kuszmaul | 630ab1d | 2024-01-09 16:38:57 -0800 | [diff] [blame] | 60 | #include "y2023/constants/constants_generated.h" |
Maxwell Henderson | 2a2faa6 | 2023-03-11 15:05:46 -0800 | [diff] [blame] | 61 | #include "y2023/control_loops/superstructure/led_indicator.h" |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 62 | #include "y2023/control_loops/superstructure/superstructure_output_generated.h" |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 63 | #include "y2023/control_loops/superstructure/superstructure_position_static.h" |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 64 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 65 | ABSL_FLAG(bool, ctre_diag_server, false, |
| 66 | "If true, enable the diagnostics server for interacting with " |
| 67 | "devices on the CAN bus using Phoenix Tuner"); |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 68 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 69 | using ::aos::monotonic_clock; |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 70 | using ::frc971::CANConfiguration; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 71 | using ::y2023::constants::Values; |
| 72 | namespace superstructure = ::y2023::control_loops::superstructure; |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 73 | namespace drivetrain = ::y2023::control_loops::drivetrain; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 74 | namespace chrono = ::std::chrono; |
| 75 | using std::make_unique; |
| 76 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 77 | namespace y2023::wpilib { |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 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 | |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 93 | double proximal_pot_translate(double voltage) { |
| 94 | return voltage * Values::kProximalPotRadiansPerVolt(); |
| 95 | } |
| 96 | |
| 97 | double distal_pot_translate(double voltage) { |
| 98 | return voltage * Values::kDistalPotRadiansPerVolt(); |
| 99 | } |
| 100 | |
| 101 | double roll_joint_pot_translate(double voltage) { |
| 102 | return voltage * Values::kRollJointPotRadiansPerVolt(); |
| 103 | } |
| 104 | |
| 105 | constexpr double kMaxFastEncoderPulsesPerSecond = std::max({ |
| 106 | Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
| 107 | Values::kMaxProximalEncoderPulsesPerSecond(), |
| 108 | Values::kMaxDistalEncoderPulsesPerSecond(), |
| 109 | Values::kMaxRollJointEncoderPulsesPerSecond(), |
Austin Schuh | e5248cd | 2023-03-05 12:46:16 -0800 | [diff] [blame] | 110 | Values::kMaxCompWristEncoderPulsesPerSecond(), |
| 111 | Values::kMaxPracticeWristEncoderPulsesPerSecond(), |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 112 | }); |
| 113 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 114 | "fast encoders are too fast"); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 115 | |
| 116 | } // namespace |
| 117 | |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 118 | static constexpr int kCANFalconCount = 6; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 119 | static constexpr units::frequency::hertz_t kCANUpdateFreqHz = 200_Hz; |
| 120 | |
| 121 | class Falcon { |
| 122 | public: |
| 123 | Falcon(int device_id, std::string canbus, |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 124 | std::vector<ctre::phoenix6::BaseStatusSignal *> *signals) |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 125 | : talon_(device_id, canbus), |
| 126 | device_id_(device_id), |
| 127 | device_temp_(talon_.GetDeviceTemp()), |
| 128 | supply_voltage_(talon_.GetSupplyVoltage()), |
| 129 | supply_current_(talon_.GetSupplyCurrent()), |
| 130 | torque_current_(talon_.GetTorqueCurrent()), |
Ravago Jones | 088ca77 | 2023-03-25 22:14:24 -0700 | [diff] [blame] | 131 | position_(talon_.GetPosition()), |
| 132 | duty_cycle_(talon_.GetDutyCycle()) { |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 133 | // device temp is not timesynced so don't add it to the list of signals |
| 134 | device_temp_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 135 | |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 136 | CHECK(signals != nullptr); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 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_); |
Ravago Jones | 088ca77 | 2023-03-25 22:14:24 -0700 | [diff] [blame] | 149 | |
| 150 | duty_cycle_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 151 | signals->push_back(&duty_cycle_); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Austin Schuh | bb4c9ac | 2023-02-28 22:04:20 -0800 | [diff] [blame] | 154 | void PrintConfigs() { |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 155 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
Austin Schuh | bb4c9ac | 2023-02-28 22:04:20 -0800 | [diff] [blame] | 156 | ctre::phoenix::StatusCode status = |
| 157 | talon_.GetConfigurator().Refresh(configuration); |
| 158 | if (!status.IsOK()) { |
| 159 | AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s", |
| 160 | status.GetName(), status.GetDescription()); |
| 161 | } |
| 162 | AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str()); |
| 163 | } |
| 164 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 165 | void WriteConfigs(ctre::phoenix6::signals::InvertedValue invert) { |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 166 | inverted_ = invert; |
| 167 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 168 | ctre::phoenix6::configs::CurrentLimitsConfigs current_limits; |
James Kuszmaul | 83fc756 | 2024-10-29 22:15:43 -0700 | [diff] [blame] | 169 | current_limits.StatorCurrentLimit = units::current::ampere_t{ |
| 170 | constants::Values::kDrivetrainStatorCurrentLimit()}; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 171 | current_limits.StatorCurrentLimitEnable = true; |
James Kuszmaul | 83fc756 | 2024-10-29 22:15:43 -0700 | [diff] [blame] | 172 | current_limits.SupplyCurrentLimit = units::current::ampere_t{ |
| 173 | constants::Values::kDrivetrainSupplyCurrentLimit()}; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 174 | current_limits.SupplyCurrentLimitEnable = true; |
| 175 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 176 | ctre::phoenix6::configs::MotorOutputConfigs output_configs; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 177 | output_configs.NeutralMode = |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 178 | ctre::phoenix6::signals::NeutralModeValue::Brake; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 179 | output_configs.DutyCycleNeutralDeadband = 0; |
| 180 | |
| 181 | output_configs.Inverted = inverted_; |
| 182 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 183 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 184 | configuration.CurrentLimits = current_limits; |
| 185 | configuration.MotorOutput = output_configs; |
| 186 | |
| 187 | ctre::phoenix::StatusCode status = |
| 188 | talon_.GetConfigurator().Apply(configuration); |
| 189 | if (!status.IsOK()) { |
| 190 | AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s", |
| 191 | status.GetName(), status.GetDescription()); |
| 192 | } |
Austin Schuh | bb4c9ac | 2023-02-28 22:04:20 -0800 | [diff] [blame] | 193 | |
| 194 | PrintConfigs(); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void WriteRollerConfigs() { |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 198 | ctre::phoenix6::configs::CurrentLimitsConfigs current_limits; |
James Kuszmaul | 83fc756 | 2024-10-29 22:15:43 -0700 | [diff] [blame] | 199 | current_limits.StatorCurrentLimit = units::current::ampere_t{ |
| 200 | constants::Values::kRollerStatorCurrentLimit()}; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 201 | current_limits.StatorCurrentLimitEnable = true; |
James Kuszmaul | 83fc756 | 2024-10-29 22:15:43 -0700 | [diff] [blame] | 202 | current_limits.SupplyCurrentLimit = units::current::ampere_t{ |
| 203 | constants::Values::kRollerSupplyCurrentLimit()}; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 204 | current_limits.SupplyCurrentLimitEnable = true; |
| 205 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 206 | ctre::phoenix6::configs::MotorOutputConfigs output_configs; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 207 | output_configs.NeutralMode = |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 208 | ctre::phoenix6::signals::NeutralModeValue::Brake; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 209 | output_configs.DutyCycleNeutralDeadband = 0; |
| 210 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 211 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 212 | configuration.CurrentLimits = current_limits; |
| 213 | configuration.MotorOutput = output_configs; |
| 214 | |
| 215 | ctre::phoenix::StatusCode status = |
| 216 | talon_.GetConfigurator().Apply(configuration); |
| 217 | if (!status.IsOK()) { |
| 218 | AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s", |
| 219 | status.GetName(), status.GetDescription()); |
| 220 | } |
Austin Schuh | bb4c9ac | 2023-02-28 22:04:20 -0800 | [diff] [blame] | 221 | |
| 222 | PrintConfigs(); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 225 | ctre::phoenix6::hardware::TalonFX *talon() { return &talon_; } |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 226 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 227 | flatbuffers::Offset<frc971::control_loops::CANTalonFX> WritePosition( |
Maxwell Henderson | ca1d18f | 2023-07-26 21:06:14 -0700 | [diff] [blame] | 228 | flatbuffers::FlatBufferBuilder *fbb) { |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 229 | frc971::control_loops::CANTalonFX::Builder builder(*fbb); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 230 | builder.add_id(device_id_); |
| 231 | builder.add_device_temp(device_temp()); |
| 232 | builder.add_supply_voltage(supply_voltage()); |
| 233 | builder.add_supply_current(supply_current()); |
| 234 | builder.add_torque_current(torque_current()); |
Ravago Jones | 088ca77 | 2023-03-25 22:14:24 -0700 | [diff] [blame] | 235 | builder.add_duty_cycle(duty_cycle()); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 236 | |
| 237 | double invert = |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 238 | (inverted_ == ctre::phoenix6::signals::InvertedValue::Clockwise_Positive |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 239 | ? 1 |
| 240 | : -1); |
| 241 | |
| 242 | builder.add_position( |
| 243 | constants::Values::DrivetrainCANEncoderToMeters(position()) * invert); |
| 244 | |
| 245 | return builder.Finish(); |
| 246 | } |
| 247 | |
| 248 | int device_id() const { return device_id_; } |
| 249 | float device_temp() const { return device_temp_.GetValue().value(); } |
| 250 | float supply_voltage() const { return supply_voltage_.GetValue().value(); } |
| 251 | float supply_current() const { return supply_current_.GetValue().value(); } |
| 252 | float torque_current() const { return torque_current_.GetValue().value(); } |
Ravago Jones | 088ca77 | 2023-03-25 22:14:24 -0700 | [diff] [blame] | 253 | float duty_cycle() const { return duty_cycle_.GetValue().value(); } |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 254 | float position() const { return position_.GetValue().value(); } |
| 255 | |
| 256 | // returns the monotonic timestamp of the latest timesynced reading in the |
| 257 | // timebase of the the syncronized CAN bus clock. |
| 258 | int64_t GetTimestamp() { |
| 259 | std::chrono::nanoseconds latest_timestamp = |
| 260 | torque_current_.GetTimestamp().GetTime(); |
| 261 | |
| 262 | return latest_timestamp.count(); |
| 263 | } |
| 264 | |
| 265 | void RefreshNontimesyncedSignals() { device_temp_.Refresh(); }; |
| 266 | |
| 267 | private: |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 268 | ctre::phoenix6::hardware::TalonFX talon_; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 269 | int device_id_; |
| 270 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 271 | ctre::phoenix6::signals::InvertedValue inverted_; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 272 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 273 | ctre::phoenix6::StatusSignal<units::temperature::celsius_t> device_temp_; |
| 274 | ctre::phoenix6::StatusSignal<units::voltage::volt_t> supply_voltage_; |
| 275 | ctre::phoenix6::StatusSignal<units::current::ampere_t> supply_current_, |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 276 | torque_current_; |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 277 | ctre::phoenix6::StatusSignal<units::angle::turn_t> position_; |
| 278 | ctre::phoenix6::StatusSignal<units::dimensionless::scalar_t> duty_cycle_; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | class CANSensorReader { |
| 282 | public: |
Ravago Jones | 2bfcecd | 2023-03-14 13:13:26 -0700 | [diff] [blame] | 283 | CANSensorReader( |
| 284 | aos::EventLoop *event_loop, |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 285 | std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry) |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 286 | : event_loop_(event_loop), |
Ravago Jones | 2bfcecd | 2023-03-14 13:13:26 -0700 | [diff] [blame] | 287 | signals_(signals_registry.begin(), signals_registry.end()), |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 288 | can_position_sender_( |
Maxwell Henderson | cef6f04 | 2023-05-26 14:38:09 -0700 | [diff] [blame] | 289 | event_loop |
| 290 | ->MakeSender<frc971::control_loops::drivetrain::CANPosition>( |
| 291 | "/drivetrain")), |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 292 | roller_falcon_data_(std::nullopt) { |
| 293 | event_loop->SetRuntimeRealtimePriority(40); |
| 294 | event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({1})); |
| 295 | timer_handler_ = event_loop->AddTimer([this]() { Loop(); }); |
| 296 | timer_handler_->set_name("CANSensorReader Loop"); |
| 297 | |
| 298 | event_loop->OnRun([this]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 299 | timer_handler_->Schedule(event_loop_->monotonic_now(), |
| 300 | 1 / kCANUpdateFreqHz); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 301 | }); |
| 302 | } |
| 303 | |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 304 | void set_falcons(std::shared_ptr<Falcon> right_front, |
| 305 | std::shared_ptr<Falcon> right_back, |
| 306 | std::shared_ptr<Falcon> right_under, |
| 307 | std::shared_ptr<Falcon> left_front, |
| 308 | std::shared_ptr<Falcon> left_back, |
| 309 | std::shared_ptr<Falcon> left_under, |
| 310 | std::shared_ptr<Falcon> roller_falcon) { |
| 311 | right_front_ = std::move(right_front); |
| 312 | right_back_ = std::move(right_back); |
| 313 | right_under_ = std::move(right_under); |
| 314 | left_front_ = std::move(left_front); |
| 315 | left_back_ = std::move(left_back); |
| 316 | left_under_ = std::move(left_under); |
| 317 | roller_falcon_ = std::move(roller_falcon); |
| 318 | } |
| 319 | |
| 320 | std::optional<superstructure::CANFalconT> roller_falcon_data() { |
| 321 | std::unique_lock<aos::stl_mutex> lock(roller_mutex_); |
| 322 | return roller_falcon_data_; |
| 323 | } |
| 324 | |
| 325 | private: |
| 326 | void Loop() { |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 327 | ctre::phoenix::StatusCode status = |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 328 | ctre::phoenix6::BaseStatusSignal::WaitForAll(2000_ms, signals_); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 329 | |
| 330 | if (!status.IsOK()) { |
| 331 | AOS_LOG(ERROR, "Failed to read signals from falcons: %s: %s", |
| 332 | status.GetName(), status.GetDescription()); |
| 333 | } |
| 334 | |
| 335 | auto builder = can_position_sender_.MakeBuilder(); |
| 336 | |
| 337 | for (auto falcon : {right_front_, right_back_, right_under_, left_front_, |
| 338 | left_back_, left_under_, roller_falcon_}) { |
| 339 | falcon->RefreshNontimesyncedSignals(); |
| 340 | } |
| 341 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 342 | aos::SizedArray<flatbuffers::Offset<frc971::control_loops::CANTalonFX>, |
Maxwell Henderson | ca1d18f | 2023-07-26 21:06:14 -0700 | [diff] [blame] | 343 | kCANFalconCount> |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 344 | falcons; |
| 345 | |
| 346 | for (auto falcon : {right_front_, right_back_, right_under_, left_front_, |
| 347 | left_back_, left_under_}) { |
| 348 | falcons.push_back(falcon->WritePosition(builder.fbb())); |
| 349 | } |
| 350 | |
| 351 | auto falcons_list = |
Maxwell Henderson | cef6f04 | 2023-05-26 14:38:09 -0700 | [diff] [blame] | 352 | builder.fbb() |
Maxwell Henderson | ca1d18f | 2023-07-26 21:06:14 -0700 | [diff] [blame] | 353 | ->CreateVector< |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 354 | flatbuffers::Offset<frc971::control_loops::CANTalonFX>>( |
| 355 | falcons); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 356 | |
Maxwell Henderson | cef6f04 | 2023-05-26 14:38:09 -0700 | [diff] [blame] | 357 | frc971::control_loops::drivetrain::CANPosition::Builder |
| 358 | can_position_builder = |
| 359 | builder |
| 360 | .MakeBuilder<frc971::control_loops::drivetrain::CANPosition>(); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 361 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 362 | can_position_builder.add_talonfxs(falcons_list); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 363 | can_position_builder.add_status(static_cast<int>(status)); |
| 364 | |
| 365 | builder.CheckOk(builder.Send(can_position_builder.Finish())); |
| 366 | |
| 367 | { |
| 368 | std::unique_lock<aos::stl_mutex> lock(roller_mutex_); |
| 369 | superstructure::CANFalconT roller_falcon_data; |
| 370 | roller_falcon_data.id = roller_falcon_->device_id(); |
| 371 | roller_falcon_data.supply_current = roller_falcon_->supply_current(); |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 372 | roller_falcon_data.torque_current = -roller_falcon_->torque_current(); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 373 | roller_falcon_data.supply_voltage = roller_falcon_->supply_voltage(); |
| 374 | roller_falcon_data.device_temp = roller_falcon_->device_temp(); |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 375 | roller_falcon_data.position = -roller_falcon_->position(); |
Ravago Jones | 088ca77 | 2023-03-25 22:14:24 -0700 | [diff] [blame] | 376 | roller_falcon_data.duty_cycle = roller_falcon_->duty_cycle(); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 377 | roller_falcon_data_ = |
| 378 | std::make_optional<superstructure::CANFalconT>(roller_falcon_data); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | aos::EventLoop *event_loop_; |
| 383 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 384 | const std::vector<ctre::phoenix6::BaseStatusSignal *> signals_; |
Maxwell Henderson | cef6f04 | 2023-05-26 14:38:09 -0700 | [diff] [blame] | 385 | aos::Sender<frc971::control_loops::drivetrain::CANPosition> |
| 386 | can_position_sender_; |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 387 | |
| 388 | std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_, |
| 389 | left_back_, left_under_, roller_falcon_; |
| 390 | |
| 391 | std::optional<superstructure::CANFalconT> roller_falcon_data_; |
| 392 | |
| 393 | aos::stl_mutex roller_mutex_; |
| 394 | |
| 395 | // Pointer to the timer handler used to modify the wakeup. |
| 396 | ::aos::TimerHandler *timer_handler_; |
| 397 | }; |
| 398 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 399 | // Class to send position messages with sensor readings to our loops. |
| 400 | class SensorReader : public ::frc971::wpilib::SensorReader { |
| 401 | public: |
| 402 | SensorReader(::aos::ShmEventLoop *event_loop, |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 403 | std::shared_ptr<const Values> values, |
| 404 | CANSensorReader *can_sensor_reader) |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 405 | : ::frc971::wpilib::SensorReader(event_loop), |
| 406 | values_(std::move(values)), |
James Kuszmaul | 630ab1d | 2024-01-09 16:38:57 -0800 | [diff] [blame] | 407 | constants_fetcher_(event_loop), |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 408 | auto_mode_sender_( |
| 409 | event_loop->MakeSender<::frc971::autonomous::AutonomousMode>( |
| 410 | "/autonomous")), |
| 411 | superstructure_position_sender_( |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 412 | event_loop->MakeSender<superstructure::PositionStatic>( |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 413 | "/superstructure")), |
| 414 | drivetrain_position_sender_( |
| 415 | event_loop |
| 416 | ->MakeSender<::frc971::control_loops::drivetrain::Position>( |
| 417 | "/drivetrain")), |
| 418 | gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>( |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 419 | "/drivetrain")), |
| 420 | can_sensor_reader_(can_sensor_reader) { |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 421 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 422 | // we should ever see. |
| 423 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
Austin Schuh | 595ffc7 | 2023-02-24 16:24:37 -0800 | [diff] [blame] | 424 | event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0})); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void Start() override { |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 428 | AddToDMA(&imu_yaw_rate_reader_); |
milind-u | 3a7f921 | 2023-02-24 20:46:59 -0800 | [diff] [blame] | 429 | AddToDMA(&cone_position_sensor_); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | // Auto mode switches. |
| 433 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
| 434 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 435 | } |
| 436 | |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 437 | void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) { |
| 438 | imu_yaw_rate_input_ = ::std::move(sensor); |
| 439 | imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get()); |
| 440 | } |
| 441 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 442 | void RunIteration() override { |
| 443 | superstructure_reading_->Set(true); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 444 | { |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 445 | aos::Sender<superstructure::PositionStatic>::StaticBuilder builder = |
| 446 | superstructure_position_sender_.MakeStaticBuilder(); |
| 447 | |
| 448 | superstructure::ArmPositionStatic *arm = builder->add_arm(); |
| 449 | |
| 450 | CopyPosition(proximal_encoder_, arm->add_proximal(), |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 451 | Values::kProximalEncoderCountsPerRevolution(), |
| 452 | Values::kProximalEncoderRatio(), proximal_pot_translate, |
Austin Schuh | 7dcc49b | 2023-02-21 17:35:10 -0800 | [diff] [blame] | 453 | true, values_->arm_proximal.potentiometer_offset); |
Maxwell Henderson | ce81612 | 2023-03-27 20:12:38 -0700 | [diff] [blame] | 454 | CopyPosition( |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 455 | distal_encoder_, arm->add_distal(), |
| 456 | Values::kDistalEncoderCountsPerRevolution(), |
Maxwell Henderson | ce81612 | 2023-03-27 20:12:38 -0700 | [diff] [blame] | 457 | values_->arm_distal.zeroing.one_revolution_distance / (M_PI * 2.0), |
| 458 | distal_pot_translate, true, values_->arm_distal.potentiometer_offset); |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 459 | CopyPosition(roll_joint_encoder_, arm->add_roll_joint(), |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 460 | Values::kRollJointEncoderCountsPerRevolution(), |
| 461 | Values::kRollJointEncoderRatio(), roll_joint_pot_translate, |
Austin Schuh | 29d025c | 2023-03-03 21:41:04 -0800 | [diff] [blame] | 462 | false, values_->roll_joint.potentiometer_offset); |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 463 | CopyPosition(wrist_encoder_, builder->add_wrist(), |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 464 | Values::kWristEncoderCountsPerRevolution(), |
James Kuszmaul | 630ab1d | 2024-01-09 16:38:57 -0800 | [diff] [blame] | 465 | constants_fetcher_.constants() |
| 466 | .robot() |
| 467 | ->wrist_zero() |
| 468 | ->one_revolution_distance() / |
Austin Schuh | e5248cd | 2023-03-05 12:46:16 -0800 | [diff] [blame] | 469 | (M_PI * 2.0), |
| 470 | values_->wrist_flipped); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 471 | |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 472 | flatbuffers::Offset<superstructure::CANFalcon> roller_falcon_offset; |
| 473 | auto optional_roller_falcon = can_sensor_reader_->roller_falcon_data(); |
| 474 | if (optional_roller_falcon.has_value()) { |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 475 | superstructure::CANFalconT roller_falcon_buffer = |
| 476 | optional_roller_falcon.value(); |
| 477 | |
| 478 | superstructure::CANFalconStatic *roller_falcon = |
| 479 | builder->add_roller_falcon(); |
| 480 | roller_falcon->set_id(roller_falcon_buffer.id); |
| 481 | roller_falcon->set_supply_current(roller_falcon_buffer.supply_current); |
| 482 | roller_falcon->set_torque_current(roller_falcon_buffer.torque_current); |
| 483 | roller_falcon->set_supply_voltage(roller_falcon_buffer.supply_voltage); |
| 484 | roller_falcon->set_device_temp(roller_falcon_buffer.device_temp); |
| 485 | roller_falcon->set_position(roller_falcon_buffer.position); |
| 486 | roller_falcon->set_duty_cycle(roller_falcon_buffer.duty_cycle); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 489 | builder->set_end_effector_cube_beam_break( |
Maxwell Henderson | 6c7e61f | 2023-02-22 16:43:43 -0800 | [diff] [blame] | 490 | end_effector_cube_beam_break_->Get()); |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 491 | builder->set_cone_position(cone_position_sensor_.last_width() / |
| 492 | cone_position_sensor_.last_period()); |
| 493 | |
| 494 | builder.CheckOk(builder.Send()); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 495 | } |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 496 | |
| 497 | { |
| 498 | auto builder = drivetrain_position_sender_.MakeBuilder(); |
| 499 | frc971::control_loops::drivetrain::Position::Builder drivetrain_builder = |
| 500 | builder.MakeBuilder<frc971::control_loops::drivetrain::Position>(); |
| 501 | drivetrain_builder.add_left_encoder( |
| 502 | constants::Values::DrivetrainEncoderToMeters( |
| 503 | drivetrain_left_encoder_->GetRaw())); |
| 504 | drivetrain_builder.add_left_speed( |
| 505 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod())); |
| 506 | |
| 507 | drivetrain_builder.add_right_encoder( |
| 508 | -constants::Values::DrivetrainEncoderToMeters( |
| 509 | drivetrain_right_encoder_->GetRaw())); |
| 510 | drivetrain_builder.add_right_speed(-drivetrain_velocity_translate( |
| 511 | drivetrain_right_encoder_->GetPeriod())); |
| 512 | |
| 513 | builder.CheckOk(builder.Send(drivetrain_builder.Finish())); |
| 514 | } |
| 515 | |
| 516 | { |
| 517 | auto builder = gyro_sender_.MakeBuilder(); |
| 518 | ::frc971::sensors::GyroReading::Builder gyro_reading_builder = |
| 519 | builder.MakeBuilder<::frc971::sensors::GyroReading>(); |
| 520 | // +/- 2000 deg / sec |
| 521 | constexpr double kMaxVelocity = 4000; // degrees / second |
| 522 | constexpr double kVelocityRadiansPerSecond = |
| 523 | kMaxVelocity / 360 * (2.0 * M_PI); |
| 524 | |
| 525 | // Only part of the full range is used to prevent being 100% on or off. |
| 526 | constexpr double kScaledRangeLow = 0.1; |
| 527 | constexpr double kScaledRangeHigh = 0.9; |
| 528 | |
| 529 | constexpr double kPWMFrequencyHz = 200; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 530 | double velocity_duty_cycle = |
| 531 | imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz; |
| 532 | |
| 533 | constexpr double kDutyCycleScale = |
| 534 | 1 / (kScaledRangeHigh - kScaledRangeLow); |
| 535 | // scale from 0.1 - 0.9 to 0 - 1 |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 536 | double rescaled_velocity_duty_cycle = |
| 537 | (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale; |
| 538 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 539 | if (!std::isnan(rescaled_velocity_duty_cycle)) { |
| 540 | gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) * |
| 541 | kVelocityRadiansPerSecond); |
| 542 | } |
| 543 | builder.CheckOk(builder.Send(gyro_reading_builder.Finish())); |
| 544 | } |
| 545 | |
| 546 | { |
| 547 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 548 | |
| 549 | uint32_t mode = 0; |
| 550 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 551 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 552 | mode |= 1 << i; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | auto auto_mode_builder = |
| 557 | builder.MakeBuilder<frc971::autonomous::AutonomousMode>(); |
| 558 | |
| 559 | auto_mode_builder.add_mode(mode); |
| 560 | |
| 561 | builder.CheckOk(builder.Send(auto_mode_builder.Finish())); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | std::shared_ptr<frc::DigitalOutput> superstructure_reading_; |
| 566 | |
| 567 | void set_superstructure_reading( |
| 568 | std::shared_ptr<frc::DigitalOutput> superstructure_reading) { |
| 569 | superstructure_reading_ = superstructure_reading; |
| 570 | } |
| 571 | |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 572 | void set_proximal_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 573 | fast_encoder_filter_.Add(encoder.get()); |
| 574 | proximal_encoder_.set_encoder(::std::move(encoder)); |
| 575 | } |
| 576 | |
| 577 | void set_proximal_absolute_pwm( |
| 578 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 579 | proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 580 | } |
| 581 | |
| 582 | void set_proximal_potentiometer( |
| 583 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 584 | proximal_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 585 | } |
| 586 | |
| 587 | void set_distal_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 588 | fast_encoder_filter_.Add(encoder.get()); |
| 589 | distal_encoder_.set_encoder(::std::move(encoder)); |
| 590 | } |
| 591 | |
| 592 | void set_distal_absolute_pwm( |
| 593 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 594 | distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 595 | } |
| 596 | |
| 597 | void set_distal_potentiometer( |
| 598 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 599 | distal_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 600 | } |
| 601 | |
| 602 | void set_roll_joint_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 603 | fast_encoder_filter_.Add(encoder.get()); |
| 604 | roll_joint_encoder_.set_encoder(::std::move(encoder)); |
| 605 | } |
| 606 | |
| 607 | void set_roll_joint_absolute_pwm( |
| 608 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 609 | roll_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 610 | } |
| 611 | |
| 612 | void set_roll_joint_potentiometer( |
| 613 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 614 | roll_joint_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 615 | } |
| 616 | |
| 617 | void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 618 | fast_encoder_filter_.Add(encoder.get()); |
| 619 | wrist_encoder_.set_encoder(::std::move(encoder)); |
| 620 | } |
| 621 | |
| 622 | void set_wrist_absolute_pwm( |
| 623 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 624 | wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 625 | } |
| 626 | |
Maxwell Henderson | 6c7e61f | 2023-02-22 16:43:43 -0800 | [diff] [blame] | 627 | void set_end_effector_cube_beam_break( |
| 628 | ::std::unique_ptr<frc::DigitalInput> sensor) { |
| 629 | end_effector_cube_beam_break_ = ::std::move(sensor); |
| 630 | } |
| 631 | |
milind-u | 3a7f921 | 2023-02-24 20:46:59 -0800 | [diff] [blame] | 632 | void set_cone_position_sensor(::std::unique_ptr<frc::DigitalInput> sensor) { |
| 633 | cone_position_input_ = ::std::move(sensor); |
| 634 | cone_position_sensor_.set_input(cone_position_input_.get()); |
| 635 | } |
| 636 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 637 | private: |
| 638 | std::shared_ptr<const Values> values_; |
James Kuszmaul | 630ab1d | 2024-01-09 16:38:57 -0800 | [diff] [blame] | 639 | frc971::constants::ConstantsFetcher<Constants> constants_fetcher_; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 640 | |
| 641 | aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_; |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 642 | aos::Sender<superstructure::PositionStatic> superstructure_position_sender_; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 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 | |
Ravago Jones | 2544ad8 | 2023-03-04 22:24:49 -0800 | [diff] [blame] | 649 | std::unique_ptr<frc::DigitalInput> 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 | |
Ravago Jones | 2544ad8 | 2023-03-04 22:24:49 -0800 | [diff] [blame] | 652 | frc971::wpilib::DMAPulseWidthReader 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, |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 714 | ::ctre::phoenix6::hardware::TalonFX *talon) { |
| 715 | ctre::phoenix6::controls::DutyCycleOut motor_control(SafeSpeed(voltage)); |
| 716 | |
| 717 | motor_control.UpdateFreqHz = 0_Hz; |
| 718 | motor_control.EnableFOC = true; |
| 719 | |
| 720 | ctre::phoenix::StatusCode status = talon->SetControl(motor_control); |
| 721 | |
| 722 | if (!status.IsOK()) { |
| 723 | AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s", |
| 724 | status.GetName(), status.GetDescription()); |
| 725 | } |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | template <typename T> |
| 729 | static void WritePwm(const double voltage, T *motor) { |
Maxwell Henderson | 1c0843c | 2023-12-22 16:20:59 -0800 | [diff] [blame] | 730 | motor->SetSpeed(SafeSpeed(voltage)); |
| 731 | } |
| 732 | |
| 733 | static double SafeSpeed(double voltage) { |
| 734 | return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 735 | } |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 736 | |
| 737 | ::std::unique_ptr<::frc::TalonFX> proximal_falcon_, distal_falcon_; |
| 738 | ::std::unique_ptr<::frc::VictorSP> roll_joint_victor_, wrist_victor_; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 739 | }; |
| 740 | |
milind-u | 32d29d3 | 2023-02-24 21:11:51 -0800 | [diff] [blame] | 741 | class SuperstructureCANWriter |
| 742 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
| 743 | public: |
| 744 | SuperstructureCANWriter(::aos::EventLoop *event_loop) |
| 745 | : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 746 | event_loop, "/superstructure") { |
| 747 | event_loop->SetRuntimeRealtimePriority( |
| 748 | constants::Values::kSuperstructureCANWriterPriority); |
| 749 | |
| 750 | event_loop->OnRun([this]() { WriteConfigs(); }); |
| 751 | }; |
| 752 | |
Austin Schuh | bb4c9ac | 2023-02-28 22:04:20 -0800 | [diff] [blame] | 753 | void HandleCANConfiguration(const CANConfiguration &configuration) { |
| 754 | roller_falcon_->PrintConfigs(); |
| 755 | if (configuration.reapply()) { |
| 756 | WriteConfigs(); |
| 757 | } |
| 758 | } |
| 759 | |
milind-u | 32d29d3 | 2023-02-24 21:11:51 -0800 | [diff] [blame] | 760 | void set_roller_falcon(std::shared_ptr<Falcon> roller_falcon) { |
| 761 | roller_falcon_ = std::move(roller_falcon); |
| 762 | } |
| 763 | |
| 764 | private: |
| 765 | void WriteConfigs() { roller_falcon_->WriteRollerConfigs(); } |
| 766 | |
| 767 | void Write(const superstructure::Output &output) override { |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 768 | ctre::phoenix6::controls::DutyCycleOut roller_control( |
milind-u | 32d29d3 | 2023-02-24 21:11:51 -0800 | [diff] [blame] | 769 | SafeSpeed(-output.roller_voltage())); |
| 770 | roller_control.UpdateFreqHz = 0_Hz; |
| 771 | roller_control.EnableFOC = true; |
| 772 | |
| 773 | ctre::phoenix::StatusCode status = |
| 774 | roller_falcon_->talon()->SetControl(roller_control); |
| 775 | |
| 776 | if (!status.IsOK()) { |
| 777 | AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s", |
| 778 | status.GetName(), status.GetDescription()); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | void Stop() override { |
| 783 | AOS_LOG(WARNING, "Superstructure CAN output too old.\n"); |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 784 | ctre::phoenix6::controls::DutyCycleOut stop_command(0.0); |
Austin Schuh | 1780e00 | 2023-03-03 21:39:26 -0800 | [diff] [blame] | 785 | stop_command.UpdateFreqHz = 0_Hz; |
| 786 | stop_command.EnableFOC = true; |
milind-u | 32d29d3 | 2023-02-24 21:11:51 -0800 | [diff] [blame] | 787 | |
| 788 | roller_falcon_->talon()->SetControl(stop_command); |
| 789 | } |
| 790 | |
| 791 | double SafeSpeed(double voltage) { |
| 792 | return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
| 793 | } |
| 794 | |
| 795 | std::shared_ptr<Falcon> roller_falcon_; |
| 796 | }; |
| 797 | |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 798 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler< |
| 799 | ::frc971::control_loops::drivetrain::Output> { |
| 800 | public: |
| 801 | DrivetrainWriter(::aos::EventLoop *event_loop) |
| 802 | : ::frc971::wpilib::LoopOutputHandler< |
| 803 | ::frc971::control_loops::drivetrain::Output>(event_loop, |
| 804 | "/drivetrain") { |
| 805 | event_loop->SetRuntimeRealtimePriority( |
| 806 | constants::Values::kDrivetrainWriterPriority); |
| 807 | |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 808 | event_loop->OnRun([this]() { WriteConfigs(); }); |
| 809 | } |
| 810 | |
| 811 | void set_falcons(std::shared_ptr<Falcon> right_front, |
| 812 | std::shared_ptr<Falcon> right_back, |
| 813 | std::shared_ptr<Falcon> right_under, |
| 814 | std::shared_ptr<Falcon> left_front, |
| 815 | std::shared_ptr<Falcon> left_back, |
| 816 | std::shared_ptr<Falcon> left_under) { |
| 817 | right_front_ = std::move(right_front); |
| 818 | right_back_ = std::move(right_back); |
| 819 | right_under_ = std::move(right_under); |
| 820 | left_front_ = std::move(left_front); |
| 821 | left_back_ = std::move(left_back); |
| 822 | left_under_ = std::move(left_under); |
| 823 | } |
| 824 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 825 | void set_right_inverted(ctre::phoenix6::signals::InvertedValue invert) { |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 826 | right_inverted_ = invert; |
| 827 | } |
| 828 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 829 | void set_left_inverted(ctre::phoenix6::signals::InvertedValue invert) { |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 830 | left_inverted_ = invert; |
| 831 | } |
| 832 | |
Austin Schuh | bb4c9ac | 2023-02-28 22:04:20 -0800 | [diff] [blame] | 833 | void HandleCANConfiguration(const CANConfiguration &configuration) { |
| 834 | for (auto falcon : {right_front_, right_back_, right_under_, left_front_, |
| 835 | left_back_, left_under_}) { |
| 836 | falcon->PrintConfigs(); |
| 837 | } |
| 838 | if (configuration.reapply()) { |
| 839 | WriteConfigs(); |
| 840 | } |
| 841 | } |
| 842 | |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 843 | private: |
| 844 | void WriteConfigs() { |
| 845 | for (auto falcon : |
| 846 | {right_front_.get(), right_back_.get(), right_under_.get()}) { |
| 847 | falcon->WriteConfigs(right_inverted_); |
| 848 | } |
| 849 | |
| 850 | for (auto falcon : |
| 851 | {left_front_.get(), left_back_.get(), left_under_.get()}) { |
| 852 | falcon->WriteConfigs(left_inverted_); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | void Write( |
| 857 | const ::frc971::control_loops::drivetrain::Output &output) override { |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 858 | ctre::phoenix6::controls::DutyCycleOut left_control( |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 859 | SafeSpeed(output.left_voltage())); |
| 860 | left_control.UpdateFreqHz = 0_Hz; |
| 861 | left_control.EnableFOC = true; |
| 862 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 863 | ctre::phoenix6::controls::DutyCycleOut right_control( |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 864 | SafeSpeed(output.right_voltage())); |
| 865 | right_control.UpdateFreqHz = 0_Hz; |
| 866 | right_control.EnableFOC = true; |
| 867 | |
| 868 | for (auto falcon : |
| 869 | {left_front_.get(), left_back_.get(), left_under_.get()}) { |
| 870 | ctre::phoenix::StatusCode status = |
| 871 | falcon->talon()->SetControl(left_control); |
| 872 | |
| 873 | if (!status.IsOK()) { |
| 874 | AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s", |
| 875 | status.GetName(), status.GetDescription()); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | for (auto falcon : |
| 880 | {right_front_.get(), right_back_.get(), right_under_.get()}) { |
| 881 | ctre::phoenix::StatusCode status = |
| 882 | falcon->talon()->SetControl(right_control); |
| 883 | |
| 884 | if (!status.IsOK()) { |
| 885 | AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s", |
| 886 | status.GetName(), status.GetDescription()); |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | void Stop() override { |
| 892 | AOS_LOG(WARNING, "drivetrain output too old\n"); |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 893 | ctre::phoenix6::controls::DutyCycleOut stop_command(0.0); |
Austin Schuh | 1780e00 | 2023-03-03 21:39:26 -0800 | [diff] [blame] | 894 | stop_command.UpdateFreqHz = 0_Hz; |
| 895 | stop_command.EnableFOC = true; |
| 896 | |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 897 | for (auto falcon : |
| 898 | {right_front_.get(), right_back_.get(), right_under_.get(), |
| 899 | left_front_.get(), left_back_.get(), left_under_.get()}) { |
| 900 | falcon->talon()->SetControl(stop_command); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | double SafeSpeed(double voltage) { |
| 905 | return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
| 906 | } |
| 907 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 908 | ctre::phoenix6::signals::InvertedValue left_inverted_, right_inverted_; |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 909 | std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_, |
| 910 | left_back_, left_under_; |
| 911 | }; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 912 | |
| 913 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 914 | public: |
| 915 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 916 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 917 | frc::Encoder::k4X); |
| 918 | } |
| 919 | |
| 920 | void Run() override { |
| 921 | std::shared_ptr<const Values> values = |
| 922 | std::make_shared<const Values>(constants::MakeValues()); |
| 923 | |
| 924 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 925 | aos::configuration::ReadConfig("aos_config.json"); |
| 926 | |
James Kuszmaul | 630ab1d | 2024-01-09 16:38:57 -0800 | [diff] [blame] | 927 | frc971::constants::WaitForConstants<y2023::Constants>(&config.message()); |
| 928 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 929 | // Thread 1. |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 930 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
| 931 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 932 | &joystick_sender_event_loop); |
| 933 | AddLoop(&joystick_sender_event_loop); |
| 934 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 935 | // Thread 2. |
| 936 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 937 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 938 | AddLoop(&pdp_fetcher_event_loop); |
| 939 | |
| 940 | std::shared_ptr<frc::DigitalOutput> superstructure_reading = |
| 941 | make_unique<frc::DigitalOutput>(25); |
| 942 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 943 | std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry; |
Ravago Jones | 2bfcecd | 2023-03-14 13:13:26 -0700 | [diff] [blame] | 944 | std::shared_ptr<Falcon> right_front = |
| 945 | std::make_shared<Falcon>(1, "Drivetrain Bus", &signals_registry); |
| 946 | std::shared_ptr<Falcon> right_back = |
| 947 | std::make_shared<Falcon>(2, "Drivetrain Bus", &signals_registry); |
| 948 | std::shared_ptr<Falcon> right_under = |
| 949 | std::make_shared<Falcon>(3, "Drivetrain Bus", &signals_registry); |
| 950 | std::shared_ptr<Falcon> left_front = |
| 951 | std::make_shared<Falcon>(4, "Drivetrain Bus", &signals_registry); |
| 952 | std::shared_ptr<Falcon> left_back = |
| 953 | std::make_shared<Falcon>(5, "Drivetrain Bus", &signals_registry); |
| 954 | std::shared_ptr<Falcon> left_under = |
| 955 | std::make_shared<Falcon>(6, "Drivetrain Bus", &signals_registry); |
| 956 | std::shared_ptr<Falcon> roller = |
| 957 | std::make_shared<Falcon>(13, "Drivetrain Bus", &signals_registry); |
| 958 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 959 | // Thread 3. |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 960 | ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message()); |
| 961 | can_sensor_reader_event_loop.set_name("CANSensorReader"); |
Ravago Jones | 2bfcecd | 2023-03-14 13:13:26 -0700 | [diff] [blame] | 962 | CANSensorReader can_sensor_reader(&can_sensor_reader_event_loop, |
| 963 | std::move(signals_registry)); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 964 | |
| 965 | can_sensor_reader.set_falcons(right_front, right_back, right_under, |
| 966 | left_front, left_back, left_under, roller); |
| 967 | |
| 968 | AddLoop(&can_sensor_reader_event_loop); |
| 969 | |
| 970 | // Thread 4. |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 971 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
milind-u | 738832d | 2023-02-24 19:55:54 -0800 | [diff] [blame] | 972 | SensorReader sensor_reader(&sensor_reader_event_loop, values, |
| 973 | &can_sensor_reader); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 974 | sensor_reader.set_pwm_trigger(true); |
| 975 | sensor_reader.set_drivetrain_left_encoder(make_encoder(1)); |
| 976 | sensor_reader.set_drivetrain_right_encoder(make_encoder(0)); |
| 977 | sensor_reader.set_superstructure_reading(superstructure_reading); |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame] | 978 | sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0)); |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 979 | |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 980 | sensor_reader.set_proximal_encoder(make_encoder(3)); |
| 981 | sensor_reader.set_proximal_absolute_pwm(make_unique<frc::DigitalInput>(3)); |
| 982 | sensor_reader.set_proximal_potentiometer(make_unique<frc::AnalogInput>(3)); |
| 983 | |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame] | 984 | sensor_reader.set_distal_encoder(make_encoder(2)); |
| 985 | sensor_reader.set_distal_absolute_pwm(make_unique<frc::DigitalInput>(2)); |
| 986 | sensor_reader.set_distal_potentiometer(make_unique<frc::AnalogInput>(2)); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 987 | |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame] | 988 | sensor_reader.set_roll_joint_encoder(make_encoder(5)); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 989 | sensor_reader.set_roll_joint_absolute_pwm( |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame] | 990 | make_unique<frc::DigitalInput>(5)); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 991 | sensor_reader.set_roll_joint_potentiometer( |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame] | 992 | make_unique<frc::AnalogInput>(5)); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 993 | |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame] | 994 | sensor_reader.set_wrist_encoder(make_encoder(4)); |
| 995 | sensor_reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(4)); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 996 | |
Maxwell Henderson | 6c7e61f | 2023-02-22 16:43:43 -0800 | [diff] [blame] | 997 | sensor_reader.set_end_effector_cube_beam_break( |
| 998 | make_unique<frc::DigitalInput>(7)); |
milind-u | 3a7f921 | 2023-02-24 20:46:59 -0800 | [diff] [blame] | 999 | sensor_reader.set_cone_position_sensor(make_unique<frc::DigitalInput>(8)); |
Maxwell Henderson | 6c7e61f | 2023-02-22 16:43:43 -0800 | [diff] [blame] | 1000 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 1001 | AddLoop(&sensor_reader_event_loop); |
| 1002 | |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 1003 | // Thread 5. |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1004 | // Set up CAN. |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 1005 | if (!absl::GetFlag(FLAGS_ctre_diag_server)) { |
milind-u | 32d29d3 | 2023-02-24 21:11:51 -0800 | [diff] [blame] | 1006 | c_Phoenix_Diagnostics_SetSecondsToStart(-1); |
| 1007 | c_Phoenix_Diagnostics_Dispose(); |
| 1008 | } |
| 1009 | |
| 1010 | ctre::phoenix::platform::can::CANComm_SetRxSchedPriority( |
| 1011 | constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus"); |
| 1012 | ctre::phoenix::platform::can::CANComm_SetTxSchedPriority( |
| 1013 | constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus"); |
| 1014 | |
| 1015 | ::aos::ShmEventLoop can_output_event_loop(&config.message()); |
| 1016 | can_output_event_loop.set_name("CANOutputWriter"); |
| 1017 | DrivetrainWriter drivetrain_writer(&can_output_event_loop); |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 1018 | |
| 1019 | drivetrain_writer.set_falcons(right_front, right_back, right_under, |
| 1020 | left_front, left_back, left_under); |
| 1021 | drivetrain_writer.set_right_inverted( |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 1022 | ctre::phoenix6::signals::InvertedValue::Clockwise_Positive); |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 1023 | drivetrain_writer.set_left_inverted( |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 1024 | ctre::phoenix6::signals::InvertedValue::CounterClockwise_Positive); |
milind-u | 32d29d3 | 2023-02-24 21:11:51 -0800 | [diff] [blame] | 1025 | |
| 1026 | SuperstructureCANWriter superstructure_can_writer(&can_output_event_loop); |
| 1027 | superstructure_can_writer.set_roller_falcon(roller); |
| 1028 | |
Austin Schuh | bb4c9ac | 2023-02-28 22:04:20 -0800 | [diff] [blame] | 1029 | can_output_event_loop.MakeWatcher( |
| 1030 | "/roborio", [&drivetrain_writer, &superstructure_can_writer]( |
| 1031 | const CANConfiguration &configuration) { |
| 1032 | drivetrain_writer.HandleCANConfiguration(configuration); |
| 1033 | superstructure_can_writer.HandleCANConfiguration(configuration); |
| 1034 | }); |
| 1035 | |
milind-u | 32d29d3 | 2023-02-24 21:11:51 -0800 | [diff] [blame] | 1036 | AddLoop(&can_output_event_loop); |
| 1037 | |
Maxwell Henderson | 2a2faa6 | 2023-03-11 15:05:46 -0800 | [diff] [blame] | 1038 | // Thread 6 |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1039 | // Set up superstructure output. |
milind-u | 32d29d3 | 2023-02-24 21:11:51 -0800 | [diff] [blame] | 1040 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
| 1041 | output_event_loop.set_name("PWMOutputWriter"); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 1042 | SuperstructureWriter superstructure_writer(&output_event_loop); |
| 1043 | |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame] | 1044 | superstructure_writer.set_proximal_falcon(make_unique<::frc::TalonFX>(1)); |
| 1045 | superstructure_writer.set_distal_falcon(make_unique<::frc::TalonFX>(0)); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 1046 | |
| 1047 | superstructure_writer.set_roll_joint_victor( |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame] | 1048 | make_unique<::frc::VictorSP>(3)); |
| 1049 | superstructure_writer.set_wrist_victor(make_unique<::frc::VictorSP>(2)); |
| 1050 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 1051 | superstructure_writer.set_superstructure_reading(superstructure_reading); |
| 1052 | |
| 1053 | AddLoop(&output_event_loop); |
| 1054 | |
Maxwell Henderson | 2a2faa6 | 2023-03-11 15:05:46 -0800 | [diff] [blame] | 1055 | // Thread 7 |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1056 | // Set up led_indicator. |
Maxwell Henderson | 2a2faa6 | 2023-03-11 15:05:46 -0800 | [diff] [blame] | 1057 | ::aos::ShmEventLoop led_indicator_event_loop(&config.message()); |
| 1058 | led_indicator_event_loop.set_name("LedIndicator"); |
| 1059 | control_loops::superstructure::LedIndicator led_indicator( |
| 1060 | &led_indicator_event_loop); |
| 1061 | AddLoop(&led_indicator_event_loop); |
| 1062 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 1063 | RunLoops(); |
| 1064 | } |
| 1065 | }; |
| 1066 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 1067 | } // namespace y2023::wpilib |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 1068 | |
| 1069 | AOS_ROBOT_CLASS(::y2023::wpilib::WPILibRobot); |