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