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