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); |
| 187 | flatbuffers::Offset<superstructure::ArmPosition> arm_offset = |
| 188 | superstructure::CreateArmPosition(*builder.fbb(), proximal_offset, |
| 189 | distal_offset); |
| 190 | flatbuffers::Offset<frc971::PotAndAbsolutePosition> roll_joint_offset = |
| 191 | frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &roll_joint); |
| 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); |
| 199 | position_builder.add_roll_joint(roll_joint_offset); |
| 200 | position_builder.add_wrist(wrist_offset); |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame^] | 201 | builder.CheckOk(builder.Send(position_builder.Finish())); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 202 | } |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 203 | |
| 204 | { |
| 205 | auto builder = drivetrain_position_sender_.MakeBuilder(); |
| 206 | frc971::control_loops::drivetrain::Position::Builder drivetrain_builder = |
| 207 | builder.MakeBuilder<frc971::control_loops::drivetrain::Position>(); |
| 208 | drivetrain_builder.add_left_encoder( |
| 209 | constants::Values::DrivetrainEncoderToMeters( |
| 210 | drivetrain_left_encoder_->GetRaw())); |
| 211 | drivetrain_builder.add_left_speed( |
| 212 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod())); |
| 213 | |
| 214 | drivetrain_builder.add_right_encoder( |
| 215 | -constants::Values::DrivetrainEncoderToMeters( |
| 216 | drivetrain_right_encoder_->GetRaw())); |
| 217 | drivetrain_builder.add_right_speed(-drivetrain_velocity_translate( |
| 218 | drivetrain_right_encoder_->GetPeriod())); |
| 219 | |
| 220 | builder.CheckOk(builder.Send(drivetrain_builder.Finish())); |
| 221 | } |
| 222 | |
| 223 | { |
| 224 | auto builder = gyro_sender_.MakeBuilder(); |
| 225 | ::frc971::sensors::GyroReading::Builder gyro_reading_builder = |
| 226 | builder.MakeBuilder<::frc971::sensors::GyroReading>(); |
| 227 | // +/- 2000 deg / sec |
| 228 | constexpr double kMaxVelocity = 4000; // degrees / second |
| 229 | constexpr double kVelocityRadiansPerSecond = |
| 230 | kMaxVelocity / 360 * (2.0 * M_PI); |
| 231 | |
| 232 | // Only part of the full range is used to prevent being 100% on or off. |
| 233 | constexpr double kScaledRangeLow = 0.1; |
| 234 | constexpr double kScaledRangeHigh = 0.9; |
| 235 | |
| 236 | constexpr double kPWMFrequencyHz = 200; |
| 237 | double heading_duty_cycle = |
| 238 | imu_heading_reader_.last_width() * kPWMFrequencyHz; |
| 239 | double velocity_duty_cycle = |
| 240 | imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz; |
| 241 | |
| 242 | constexpr double kDutyCycleScale = |
| 243 | 1 / (kScaledRangeHigh - kScaledRangeLow); |
| 244 | // scale from 0.1 - 0.9 to 0 - 1 |
| 245 | double rescaled_heading_duty_cycle = |
| 246 | (heading_duty_cycle - kScaledRangeLow) * kDutyCycleScale; |
| 247 | double rescaled_velocity_duty_cycle = |
| 248 | (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale; |
| 249 | |
| 250 | if (!std::isnan(rescaled_heading_duty_cycle)) { |
| 251 | gyro_reading_builder.add_angle(rescaled_heading_duty_cycle * |
| 252 | (2.0 * M_PI)); |
| 253 | } |
| 254 | if (!std::isnan(rescaled_velocity_duty_cycle)) { |
| 255 | gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) * |
| 256 | kVelocityRadiansPerSecond); |
| 257 | } |
| 258 | builder.CheckOk(builder.Send(gyro_reading_builder.Finish())); |
| 259 | } |
| 260 | |
| 261 | { |
| 262 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 263 | |
| 264 | uint32_t mode = 0; |
| 265 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 266 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 267 | mode |= 1 << i; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | auto auto_mode_builder = |
| 272 | builder.MakeBuilder<frc971::autonomous::AutonomousMode>(); |
| 273 | |
| 274 | auto_mode_builder.add_mode(mode); |
| 275 | |
| 276 | builder.CheckOk(builder.Send(auto_mode_builder.Finish())); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | std::shared_ptr<frc::DigitalOutput> superstructure_reading_; |
| 281 | |
| 282 | void set_superstructure_reading( |
| 283 | std::shared_ptr<frc::DigitalOutput> superstructure_reading) { |
| 284 | superstructure_reading_ = superstructure_reading; |
| 285 | } |
| 286 | |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 287 | void set_proximal_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 288 | fast_encoder_filter_.Add(encoder.get()); |
| 289 | proximal_encoder_.set_encoder(::std::move(encoder)); |
| 290 | } |
| 291 | |
| 292 | void set_proximal_absolute_pwm( |
| 293 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 294 | proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 295 | } |
| 296 | |
| 297 | void set_proximal_potentiometer( |
| 298 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 299 | proximal_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 300 | } |
| 301 | |
| 302 | void set_distal_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 303 | fast_encoder_filter_.Add(encoder.get()); |
| 304 | distal_encoder_.set_encoder(::std::move(encoder)); |
| 305 | } |
| 306 | |
| 307 | void set_distal_absolute_pwm( |
| 308 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 309 | distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 310 | } |
| 311 | |
| 312 | void set_distal_potentiometer( |
| 313 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 314 | distal_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 315 | } |
| 316 | |
| 317 | void set_roll_joint_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 318 | fast_encoder_filter_.Add(encoder.get()); |
| 319 | roll_joint_encoder_.set_encoder(::std::move(encoder)); |
| 320 | } |
| 321 | |
| 322 | void set_roll_joint_absolute_pwm( |
| 323 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 324 | roll_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 325 | } |
| 326 | |
| 327 | void set_roll_joint_potentiometer( |
| 328 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 329 | roll_joint_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 330 | } |
| 331 | |
| 332 | void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 333 | fast_encoder_filter_.Add(encoder.get()); |
| 334 | wrist_encoder_.set_encoder(::std::move(encoder)); |
| 335 | } |
| 336 | |
| 337 | void set_wrist_absolute_pwm( |
| 338 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 339 | wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 340 | } |
| 341 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 342 | private: |
| 343 | std::shared_ptr<const Values> values_; |
| 344 | |
| 345 | aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_; |
| 346 | aos::Sender<superstructure::Position> superstructure_position_sender_; |
| 347 | aos::Sender<frc971::control_loops::drivetrain::Position> |
| 348 | drivetrain_position_sender_; |
| 349 | ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_; |
| 350 | |
| 351 | std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_; |
| 352 | |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 353 | std::unique_ptr<frc::DigitalInput> imu_heading_input_, imu_yaw_rate_input_; |
| 354 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 355 | frc971::wpilib::DMAPulseWidthReader imu_heading_reader_, imu_yaw_rate_reader_; |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 356 | |
| 357 | frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_, |
| 358 | distal_encoder_, roll_joint_encoder_; |
| 359 | frc971::wpilib::AbsoluteEncoder wrist_encoder_; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 360 | }; |
| 361 | |
| 362 | class SuperstructureWriter |
| 363 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
| 364 | public: |
| 365 | SuperstructureWriter(aos::EventLoop *event_loop) |
| 366 | : frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 367 | event_loop, "/superstructure") {} |
| 368 | |
| 369 | std::shared_ptr<frc::DigitalOutput> superstructure_reading_; |
| 370 | |
| 371 | void set_superstructure_reading( |
| 372 | std::shared_ptr<frc::DigitalOutput> superstructure_reading) { |
| 373 | superstructure_reading_ = superstructure_reading; |
| 374 | } |
| 375 | |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 376 | void set_proximal_falcon(::std::unique_ptr<::frc::TalonFX> t) { |
| 377 | proximal_falcon_ = ::std::move(t); |
| 378 | } |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 379 | |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 380 | void set_distal_falcon(::std::unique_ptr<::frc::TalonFX> t) { |
| 381 | distal_falcon_ = ::std::move(t); |
| 382 | } |
| 383 | |
| 384 | void set_roll_joint_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 385 | roll_joint_victor_ = ::std::move(t); |
| 386 | } |
| 387 | |
| 388 | void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 389 | wrist_victor_ = ::std::move(t); |
| 390 | } |
| 391 | |
| 392 | void set_roller_falcon( |
| 393 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) { |
| 394 | roller_falcon_ = ::std::move(t); |
| 395 | roller_falcon_->ConfigSupplyCurrentLimit( |
| 396 | {true, Values::kRollerSupplyCurrentLimit(), |
| 397 | Values::kRollerSupplyCurrentLimit(), 0}); |
| 398 | roller_falcon_->ConfigStatorCurrentLimit( |
| 399 | {true, Values::kRollerStatorCurrentLimit(), |
| 400 | Values::kRollerStatorCurrentLimit(), 0}); |
| 401 | } |
| 402 | |
| 403 | private: |
| 404 | void Stop() override { |
| 405 | AOS_LOG(WARNING, "Superstructure output too old.\n"); |
| 406 | proximal_falcon_->SetDisabled(); |
| 407 | distal_falcon_->SetDisabled(); |
| 408 | roll_joint_victor_->SetDisabled(); |
| 409 | wrist_victor_->SetDisabled(); |
| 410 | roller_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0); |
| 411 | } |
| 412 | |
| 413 | void Write(const superstructure::Output &output) override { |
| 414 | WritePwm(output.proximal_voltage(), proximal_falcon_.get()); |
| 415 | WritePwm(output.distal_voltage(), distal_falcon_.get()); |
| 416 | WritePwm(output.roll_joint_voltage(), roll_joint_victor_.get()); |
| 417 | WritePwm(output.wrist_voltage(), wrist_victor_.get()); |
| 418 | WriteCan(output.roller_voltage(), roller_falcon_.get()); |
| 419 | } |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 420 | |
| 421 | static void WriteCan(const double voltage, |
| 422 | ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) { |
| 423 | falcon->Set( |
| 424 | ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
| 425 | std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
| 426 | } |
| 427 | |
| 428 | template <typename T> |
| 429 | static void WritePwm(const double voltage, T *motor) { |
| 430 | motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / |
| 431 | 12.0); |
| 432 | } |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 433 | |
| 434 | ::std::unique_ptr<::frc::TalonFX> proximal_falcon_, distal_falcon_; |
| 435 | ::std::unique_ptr<::frc::VictorSP> roll_joint_victor_, wrist_victor_; |
| 436 | ::std::shared_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> roller_falcon_; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 437 | }; |
| 438 | |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 439 | static constexpr int kCANFalconCount = 6; |
| 440 | static constexpr int kCANSignalsCount = 3; |
| 441 | static constexpr units::frequency::hertz_t kCANUpdateFreqHz = 200_Hz; |
| 442 | |
| 443 | class Falcon { |
| 444 | public: |
| 445 | Falcon(int device_id, std::string canbus, |
| 446 | aos::SizedArray<ctre::phoenixpro::BaseStatusSignalValue *, |
| 447 | kCANFalconCount * kCANSignalsCount> *signals) |
| 448 | : talon_(device_id, canbus), |
| 449 | device_id_(device_id), |
| 450 | device_temp_(talon_.GetDeviceTemp()), |
| 451 | supply_voltage_(talon_.GetSupplyVoltage()), |
| 452 | supply_current_(talon_.GetSupplyCurrent()), |
| 453 | torque_current_(talon_.GetTorqueCurrent()), |
| 454 | position_(talon_.GetPosition()) { |
| 455 | // device temp is not timesynced so don't add it to the list of signals |
| 456 | device_temp_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 457 | |
| 458 | CHECK_EQ(kCANSignalsCount, 3); |
| 459 | CHECK_NOTNULL(signals); |
| 460 | CHECK_LE(signals->size() + 3u, signals->capacity()); |
| 461 | |
| 462 | supply_voltage_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 463 | signals->push_back(&supply_voltage_); |
| 464 | |
| 465 | supply_current_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 466 | signals->push_back(&supply_current_); |
| 467 | |
| 468 | torque_current_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 469 | signals->push_back(&torque_current_); |
| 470 | |
| 471 | position_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 472 | signals->push_back(&position_); |
| 473 | } |
| 474 | |
| 475 | void WriteConfigs(ctre::phoenixpro::signals::InvertedValue invert) { |
| 476 | inverted_ = invert; |
| 477 | |
| 478 | ctre::phoenixpro::configs::CurrentLimitsConfigs current_limits; |
| 479 | current_limits.StatorCurrentLimit = |
| 480 | constants::Values::kDrivetrainStatorCurrentLimit(); |
| 481 | current_limits.StatorCurrentLimitEnable = true; |
| 482 | current_limits.SupplyCurrentLimit = |
| 483 | constants::Values::kDrivetrainSupplyCurrentLimit(); |
| 484 | current_limits.SupplyCurrentLimitEnable = true; |
| 485 | |
| 486 | ctre::phoenixpro::configs::MotorOutputConfigs output_configs; |
| 487 | output_configs.NeutralMode = |
| 488 | ctre::phoenixpro::signals::NeutralModeValue::Brake; |
| 489 | output_configs.DutyCycleNeutralDeadband = 0; |
| 490 | |
| 491 | output_configs.Inverted = inverted_; |
| 492 | |
| 493 | ctre::phoenixpro::configs::TalonFXConfiguration configuration; |
| 494 | configuration.CurrentLimits = current_limits; |
| 495 | configuration.MotorOutput = output_configs; |
| 496 | |
| 497 | ctre::phoenix::StatusCode status = |
| 498 | talon_.GetConfigurator().Apply(configuration); |
| 499 | if (!status.IsOK()) { |
| 500 | AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s", |
| 501 | status.GetName(), status.GetDescription()); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | ctre::phoenixpro::hardware::TalonFX *talon() { return &talon_; } |
| 506 | |
| 507 | flatbuffers::Offset<drivetrain::CANFalcon> WritePosition( |
| 508 | flatbuffers::FlatBufferBuilder *fbb) { |
| 509 | drivetrain::CANFalcon::Builder builder(*fbb); |
| 510 | builder.add_id(device_id_); |
| 511 | builder.add_device_temp(device_temp_.GetValue().value()); |
| 512 | builder.add_supply_voltage(supply_voltage_.GetValue().value()); |
| 513 | builder.add_supply_current(supply_current_.GetValue().value()); |
| 514 | builder.add_torque_current(torque_current_.GetValue().value()); |
| 515 | |
| 516 | double invert = |
| 517 | (inverted_ == |
| 518 | ctre::phoenixpro::signals::InvertedValue::Clockwise_Positive |
| 519 | ? 1 |
| 520 | : -1); |
| 521 | |
| 522 | builder.add_position(constants::Values::DrivetrainCANEncoderToMeters( |
| 523 | position_.GetValue().value()) * |
| 524 | invert); |
| 525 | |
| 526 | return builder.Finish(); |
| 527 | } |
| 528 | |
| 529 | // returns the monotonic timestamp of the latest timesynced reading in the |
| 530 | // timebase of the the syncronized CAN bus clock. |
| 531 | int64_t GetTimestamp() { |
| 532 | std::chrono::nanoseconds latest_timestamp = |
| 533 | torque_current_.GetTimestamp().GetTime(); |
| 534 | |
| 535 | return latest_timestamp.count(); |
| 536 | } |
| 537 | |
| 538 | void RefreshNontimesyncedSignals() { device_temp_.Refresh(); }; |
| 539 | |
| 540 | private: |
| 541 | ctre::phoenixpro::hardware::TalonFX talon_; |
| 542 | int device_id_; |
| 543 | |
| 544 | ctre::phoenixpro::signals::InvertedValue inverted_; |
| 545 | |
| 546 | ctre::phoenixpro::StatusSignalValue<units::temperature::celsius_t> |
| 547 | device_temp_; |
| 548 | ctre::phoenixpro::StatusSignalValue<units::voltage::volt_t> supply_voltage_; |
| 549 | ctre::phoenixpro::StatusSignalValue<units::current::ampere_t> supply_current_, |
| 550 | torque_current_; |
| 551 | ctre::phoenixpro::StatusSignalValue<units::angle::turn_t> position_; |
| 552 | }; |
| 553 | |
| 554 | class CANSensorReader { |
| 555 | public: |
| 556 | CANSensorReader(aos::EventLoop *event_loop) |
| 557 | : event_loop_(event_loop), |
| 558 | can_position_sender_( |
| 559 | event_loop->MakeSender<drivetrain::CANPosition>("/drivetrain")) { |
| 560 | event_loop->SetRuntimeRealtimePriority(35); |
| 561 | timer_handler_ = event_loop->AddTimer([this]() { Loop(); }); |
| 562 | timer_handler_->set_name("CANSensorReader Loop"); |
| 563 | |
| 564 | event_loop->OnRun([this]() { |
| 565 | timer_handler_->Setup(event_loop_->monotonic_now(), 1 / kCANUpdateFreqHz); |
| 566 | }); |
| 567 | } |
| 568 | |
| 569 | aos::SizedArray<ctre::phoenixpro::BaseStatusSignalValue *, |
| 570 | kCANFalconCount * kCANSignalsCount> |
| 571 | *get_signals_registry() { |
| 572 | return &signals_; |
| 573 | }; |
| 574 | |
| 575 | void set_falcons(std::shared_ptr<Falcon> right_front, |
| 576 | std::shared_ptr<Falcon> right_back, |
| 577 | std::shared_ptr<Falcon> right_under, |
| 578 | std::shared_ptr<Falcon> left_front, |
| 579 | std::shared_ptr<Falcon> left_back, |
| 580 | std::shared_ptr<Falcon> left_under) { |
| 581 | right_front_ = std::move(right_front); |
| 582 | right_back_ = std::move(right_back); |
| 583 | right_under_ = std::move(right_under); |
| 584 | left_front_ = std::move(left_front); |
| 585 | left_back_ = std::move(left_back); |
| 586 | left_under_ = std::move(left_under); |
| 587 | } |
| 588 | |
| 589 | private: |
| 590 | void Loop() { |
| 591 | CHECK_EQ(signals_.size(), 18u); |
| 592 | ctre::phoenix::StatusCode status = |
| 593 | ctre::phoenixpro::BaseStatusSignalValue::WaitForAll( |
| 594 | 2000_ms, |
| 595 | {signals_[0], signals_[1], signals_[2], signals_[3], signals_[4], |
| 596 | signals_[5], signals_[6], signals_[7], signals_[8], signals_[9], |
| 597 | signals_[10], signals_[11], signals_[12], signals_[13], |
| 598 | signals_[14], signals_[15], signals_[16], signals_[17]}); |
| 599 | |
| 600 | if (!status.IsOK()) { |
| 601 | AOS_LOG(ERROR, "Failed to read signals from falcons: %s: %s", |
| 602 | status.GetName(), status.GetDescription()); |
| 603 | } |
| 604 | |
| 605 | auto builder = can_position_sender_.MakeBuilder(); |
| 606 | |
| 607 | for (auto falcon : {right_front_, right_back_, right_under_, left_front_, |
| 608 | left_back_, left_under_}) { |
| 609 | falcon->RefreshNontimesyncedSignals(); |
| 610 | } |
| 611 | |
| 612 | aos::SizedArray<flatbuffers::Offset<drivetrain::CANFalcon>, kCANFalconCount> |
| 613 | falcons; |
| 614 | |
| 615 | for (auto falcon : {right_front_, right_back_, right_under_, left_front_, |
| 616 | left_back_, left_under_}) { |
| 617 | falcons.push_back(falcon->WritePosition(builder.fbb())); |
| 618 | } |
| 619 | |
| 620 | auto falcons_list = |
| 621 | builder.fbb()->CreateVector<flatbuffers::Offset<drivetrain::CANFalcon>>( |
| 622 | falcons); |
| 623 | |
| 624 | drivetrain::CANPosition::Builder can_position_builder = |
| 625 | builder.MakeBuilder<drivetrain::CANPosition>(); |
| 626 | |
| 627 | can_position_builder.add_falcons(falcons_list); |
| 628 | can_position_builder.add_timestamp(right_front_->GetTimestamp()); |
| 629 | can_position_builder.add_status(static_cast<int>(status)); |
| 630 | |
| 631 | builder.CheckOk(builder.Send(can_position_builder.Finish())); |
| 632 | } |
| 633 | |
| 634 | aos::EventLoop *event_loop_; |
| 635 | |
| 636 | aos::SizedArray<ctre::phoenixpro::BaseStatusSignalValue *, |
| 637 | kCANFalconCount * kCANSignalsCount> |
| 638 | signals_; |
| 639 | aos::Sender<drivetrain::CANPosition> can_position_sender_; |
| 640 | |
| 641 | std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_, |
| 642 | left_back_, left_under_; |
| 643 | |
| 644 | // Pointer to the timer handler used to modify the wakeup. |
| 645 | ::aos::TimerHandler *timer_handler_; |
| 646 | }; |
| 647 | |
| 648 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler< |
| 649 | ::frc971::control_loops::drivetrain::Output> { |
| 650 | public: |
| 651 | DrivetrainWriter(::aos::EventLoop *event_loop) |
| 652 | : ::frc971::wpilib::LoopOutputHandler< |
| 653 | ::frc971::control_loops::drivetrain::Output>(event_loop, |
| 654 | "/drivetrain") { |
| 655 | event_loop->SetRuntimeRealtimePriority( |
| 656 | constants::Values::kDrivetrainWriterPriority); |
| 657 | |
| 658 | if (!FLAGS_ctre_diag_server) { |
| 659 | c_Phoenix_Diagnostics_SetSecondsToStart(-1); |
| 660 | c_Phoenix_Diagnostics_Dispose(); |
| 661 | } |
| 662 | |
| 663 | ctre::phoenix::platform::can::CANComm_SetRxSchedPriority( |
| 664 | constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus"); |
| 665 | ctre::phoenix::platform::can::CANComm_SetTxSchedPriority( |
| 666 | constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus"); |
| 667 | |
| 668 | event_loop->OnRun([this]() { WriteConfigs(); }); |
| 669 | } |
| 670 | |
| 671 | void set_falcons(std::shared_ptr<Falcon> right_front, |
| 672 | std::shared_ptr<Falcon> right_back, |
| 673 | std::shared_ptr<Falcon> right_under, |
| 674 | std::shared_ptr<Falcon> left_front, |
| 675 | std::shared_ptr<Falcon> left_back, |
| 676 | std::shared_ptr<Falcon> left_under) { |
| 677 | right_front_ = std::move(right_front); |
| 678 | right_back_ = std::move(right_back); |
| 679 | right_under_ = std::move(right_under); |
| 680 | left_front_ = std::move(left_front); |
| 681 | left_back_ = std::move(left_back); |
| 682 | left_under_ = std::move(left_under); |
| 683 | } |
| 684 | |
| 685 | void set_right_inverted(ctre::phoenixpro::signals::InvertedValue invert) { |
| 686 | right_inverted_ = invert; |
| 687 | } |
| 688 | |
| 689 | void set_left_inverted(ctre::phoenixpro::signals::InvertedValue invert) { |
| 690 | left_inverted_ = invert; |
| 691 | } |
| 692 | |
| 693 | private: |
| 694 | void WriteConfigs() { |
| 695 | for (auto falcon : |
| 696 | {right_front_.get(), right_back_.get(), right_under_.get()}) { |
| 697 | falcon->WriteConfigs(right_inverted_); |
| 698 | } |
| 699 | |
| 700 | for (auto falcon : |
| 701 | {left_front_.get(), left_back_.get(), left_under_.get()}) { |
| 702 | falcon->WriteConfigs(left_inverted_); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | void Write( |
| 707 | const ::frc971::control_loops::drivetrain::Output &output) override { |
| 708 | ctre::phoenixpro::controls::DutyCycleOut left_control( |
| 709 | SafeSpeed(output.left_voltage())); |
| 710 | left_control.UpdateFreqHz = 0_Hz; |
| 711 | left_control.EnableFOC = true; |
| 712 | |
| 713 | ctre::phoenixpro::controls::DutyCycleOut right_control( |
| 714 | SafeSpeed(output.right_voltage())); |
| 715 | right_control.UpdateFreqHz = 0_Hz; |
| 716 | right_control.EnableFOC = true; |
| 717 | |
| 718 | for (auto falcon : |
| 719 | {left_front_.get(), left_back_.get(), left_under_.get()}) { |
| 720 | ctre::phoenix::StatusCode status = |
| 721 | falcon->talon()->SetControl(left_control); |
| 722 | |
| 723 | if (!status.IsOK()) { |
| 724 | AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s", |
| 725 | status.GetName(), status.GetDescription()); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | for (auto falcon : |
| 730 | {right_front_.get(), right_back_.get(), right_under_.get()}) { |
| 731 | ctre::phoenix::StatusCode status = |
| 732 | falcon->talon()->SetControl(right_control); |
| 733 | |
| 734 | if (!status.IsOK()) { |
| 735 | AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s", |
| 736 | status.GetName(), status.GetDescription()); |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | void Stop() override { |
| 742 | AOS_LOG(WARNING, "drivetrain output too old\n"); |
| 743 | ctre::phoenixpro::controls::NeutralOut stop_command; |
| 744 | for (auto falcon : |
| 745 | {right_front_.get(), right_back_.get(), right_under_.get(), |
| 746 | left_front_.get(), left_back_.get(), left_under_.get()}) { |
| 747 | falcon->talon()->SetControl(stop_command); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | double SafeSpeed(double voltage) { |
| 752 | return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
| 753 | } |
| 754 | |
| 755 | ctre::phoenixpro::signals::InvertedValue left_inverted_, right_inverted_; |
| 756 | std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_, |
| 757 | left_back_, left_under_; |
| 758 | }; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 759 | |
| 760 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 761 | public: |
| 762 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 763 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 764 | frc::Encoder::k4X); |
| 765 | } |
| 766 | |
| 767 | void Run() override { |
| 768 | std::shared_ptr<const Values> values = |
| 769 | std::make_shared<const Values>(constants::MakeValues()); |
| 770 | |
| 771 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 772 | aos::configuration::ReadConfig("aos_config.json"); |
| 773 | |
| 774 | // Thread 1. |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 775 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
| 776 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 777 | &joystick_sender_event_loop); |
| 778 | AddLoop(&joystick_sender_event_loop); |
| 779 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 780 | // Thread 2. |
| 781 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 782 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 783 | AddLoop(&pdp_fetcher_event_loop); |
| 784 | |
| 785 | std::shared_ptr<frc::DigitalOutput> superstructure_reading = |
| 786 | make_unique<frc::DigitalOutput>(25); |
| 787 | |
| 788 | // Thread 3. |
| 789 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
| 790 | SensorReader sensor_reader(&sensor_reader_event_loop, values); |
| 791 | sensor_reader.set_pwm_trigger(true); |
| 792 | sensor_reader.set_drivetrain_left_encoder(make_encoder(1)); |
| 793 | sensor_reader.set_drivetrain_right_encoder(make_encoder(0)); |
| 794 | sensor_reader.set_superstructure_reading(superstructure_reading); |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame^] | 795 | sensor_reader.set_heading_input(make_unique<frc::DigitalInput>(1)); |
| 796 | sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0)); |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 797 | |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 798 | sensor_reader.set_proximal_encoder(make_encoder(3)); |
| 799 | sensor_reader.set_proximal_absolute_pwm(make_unique<frc::DigitalInput>(3)); |
| 800 | sensor_reader.set_proximal_potentiometer(make_unique<frc::AnalogInput>(3)); |
| 801 | |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame^] | 802 | sensor_reader.set_distal_encoder(make_encoder(2)); |
| 803 | sensor_reader.set_distal_absolute_pwm(make_unique<frc::DigitalInput>(2)); |
| 804 | sensor_reader.set_distal_potentiometer(make_unique<frc::AnalogInput>(2)); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 805 | |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame^] | 806 | sensor_reader.set_roll_joint_encoder(make_encoder(5)); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 807 | sensor_reader.set_roll_joint_absolute_pwm( |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame^] | 808 | make_unique<frc::DigitalInput>(5)); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 809 | sensor_reader.set_roll_joint_potentiometer( |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame^] | 810 | make_unique<frc::AnalogInput>(5)); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 811 | |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame^] | 812 | sensor_reader.set_wrist_encoder(make_encoder(4)); |
| 813 | sensor_reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(4)); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 814 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 815 | AddLoop(&sensor_reader_event_loop); |
| 816 | |
| 817 | // Thread 4. |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 818 | ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message()); |
| 819 | CANSensorReader can_sensor_reader(&can_sensor_reader_event_loop); |
| 820 | |
| 821 | std::shared_ptr<Falcon> right_front = std::make_shared<Falcon>( |
| 822 | 1, "Drivetrain Bus", can_sensor_reader.get_signals_registry()); |
| 823 | std::shared_ptr<Falcon> right_back = std::make_shared<Falcon>( |
| 824 | 2, "Drivetrain Bus", can_sensor_reader.get_signals_registry()); |
| 825 | std::shared_ptr<Falcon> right_under = std::make_shared<Falcon>( |
| 826 | 3, "Drivetrain Bus", can_sensor_reader.get_signals_registry()); |
| 827 | std::shared_ptr<Falcon> left_front = std::make_shared<Falcon>( |
| 828 | 4, "Drivetrain Bus", can_sensor_reader.get_signals_registry()); |
| 829 | std::shared_ptr<Falcon> left_back = std::make_shared<Falcon>( |
| 830 | 5, "Drivetrain Bus", can_sensor_reader.get_signals_registry()); |
| 831 | std::shared_ptr<Falcon> left_under = std::make_shared<Falcon>( |
| 832 | 6, "Drivetrain Bus", can_sensor_reader.get_signals_registry()); |
| 833 | |
| 834 | can_sensor_reader.set_falcons(right_front, right_back, right_under, |
| 835 | left_front, left_back, left_under); |
| 836 | |
| 837 | AddLoop(&can_sensor_reader_event_loop); |
| 838 | |
| 839 | // Thread 5. |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 840 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
Ravago Jones | 2060ee6 | 2023-02-03 18:12:24 -0800 | [diff] [blame] | 841 | DrivetrainWriter drivetrain_writer(&output_event_loop); |
| 842 | |
| 843 | drivetrain_writer.set_falcons(right_front, right_back, right_under, |
| 844 | left_front, left_back, left_under); |
| 845 | drivetrain_writer.set_right_inverted( |
| 846 | ctre::phoenixpro::signals::InvertedValue::Clockwise_Positive); |
| 847 | drivetrain_writer.set_left_inverted( |
| 848 | ctre::phoenixpro::signals::InvertedValue::CounterClockwise_Positive); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 849 | SuperstructureWriter superstructure_writer(&output_event_loop); |
| 850 | |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame^] | 851 | superstructure_writer.set_proximal_falcon(make_unique<::frc::TalonFX>(1)); |
| 852 | superstructure_writer.set_distal_falcon(make_unique<::frc::TalonFX>(0)); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 853 | |
| 854 | superstructure_writer.set_roll_joint_victor( |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame^] | 855 | make_unique<::frc::VictorSP>(3)); |
| 856 | superstructure_writer.set_wrist_victor(make_unique<::frc::VictorSP>(2)); |
| 857 | |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 858 | superstructure_writer.set_roller_falcon( |
Henry Speiser | e139f80 | 2023-02-21 14:14:48 -0800 | [diff] [blame^] | 859 | make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(0)); |
milind-u | 18934eb | 2023-02-20 16:28:58 -0800 | [diff] [blame] | 860 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 861 | superstructure_writer.set_superstructure_reading(superstructure_reading); |
| 862 | |
| 863 | AddLoop(&output_event_loop); |
| 864 | |
| 865 | RunLoops(); |
| 866 | } |
| 867 | }; |
| 868 | |
| 869 | } // namespace wpilib |
| 870 | } // namespace y2023 |
| 871 | |
| 872 | AOS_ROBOT_CLASS(::y2023::wpilib::WPILibRobot); |