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