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