Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <array> |
| 4 | #include <chrono> |
| 5 | #include <cinttypes> |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 6 | #include <cstdio> |
| 7 | #include <cstring> |
| 8 | #include <functional> |
| 9 | #include <memory> |
| 10 | #include <mutex> |
| 11 | #include <thread> |
| 12 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 13 | #include "frc971/wpilib/ahal/AnalogInput.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 14 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 15 | #include "frc971/wpilib/ahal/Encoder.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 16 | #include "frc971/wpilib/ahal/TalonFX.h" |
| 17 | #include "frc971/wpilib/ahal/VictorSP.h" |
| 18 | #undef ERROR |
| 19 | |
| 20 | #include "ctre/phoenix/cci/Diagnostics_CCI.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 21 | |
| 22 | #include "aos/commonmath.h" |
| 23 | #include "aos/containers/sized_array.h" |
| 24 | #include "aos/events/event_loop.h" |
| 25 | #include "aos/events/shm_event_loop.h" |
| 26 | #include "aos/init.h" |
| 27 | #include "aos/logging/logging.h" |
| 28 | #include "aos/realtime.h" |
| 29 | #include "aos/time/time.h" |
| 30 | #include "aos/util/log_interval.h" |
| 31 | #include "aos/util/phased_loop.h" |
| 32 | #include "aos/util/wrapping_counter.h" |
| 33 | #include "frc971/autonomous/auto_mode_generated.h" |
| 34 | #include "frc971/can_configuration_generated.h" |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 35 | #include "frc971/constants/constants_sender_lib.h" |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 36 | #include "frc971/control_loops/drivetrain/drivetrain_can_position_static.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 37 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
| 38 | #include "frc971/input/robot_state_generated.h" |
| 39 | #include "frc971/queues/gyro_generated.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 40 | #include "frc971/wpilib/buffered_pcm.h" |
| 41 | #include "frc971/wpilib/buffered_solenoid.h" |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 42 | #include "frc971/wpilib/can_drivetrain_writer.h" |
| 43 | #include "frc971/wpilib/can_sensor_reader.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 44 | #include "frc971/wpilib/dma.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 45 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 46 | #include "frc971/wpilib/generic_can_writer.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 47 | #include "frc971/wpilib/joystick_sender.h" |
| 48 | #include "frc971/wpilib/logging_generated.h" |
| 49 | #include "frc971/wpilib/loop_output_handler.h" |
| 50 | #include "frc971/wpilib/pdp_fetcher.h" |
| 51 | #include "frc971/wpilib/sensor_reader.h" |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 52 | #include "frc971/wpilib/talonfx.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 53 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 54 | #include "y2024/constants.h" |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 55 | #include "y2024/constants/constants_generated.h" |
| 56 | #include "y2024/control_loops/superstructure/superstructure_can_position_static.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 57 | #include "y2024/control_loops/superstructure/superstructure_output_generated.h" |
| 58 | #include "y2024/control_loops/superstructure/superstructure_position_generated.h" |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 59 | #include "y2024/control_loops/superstructure/superstructure_position_static.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 60 | |
| 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 | |
| 65 | using ::aos::monotonic_clock; |
| 66 | using ::frc971::CANConfiguration; |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 67 | using ::frc971::control_loops::drivetrain::CANPositionStatic; |
| 68 | using ::frc971::wpilib::TalonFX; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 69 | using ::y2024::constants::Values; |
| 70 | namespace superstructure = ::y2024::control_loops::superstructure; |
| 71 | namespace drivetrain = ::y2024::control_loops::drivetrain; |
| 72 | namespace chrono = ::std::chrono; |
| 73 | using std::make_unique; |
| 74 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 75 | namespace y2024::wpilib { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 76 | namespace { |
| 77 | |
| 78 | constexpr double kMaxBringupPower = 12.0; |
| 79 | |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 80 | double climber_pot_translate(double voltage) { |
| 81 | return voltage * Values::kClimberPotRadiansPerVolt(); |
| 82 | } |
| 83 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 84 | double drivetrain_velocity_translate(double in) { |
| 85 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
| 86 | (2.0 * M_PI)) * |
| 87 | Values::kDrivetrainEncoderRatio() * |
| 88 | control_loops::drivetrain::kWheelRadius; |
| 89 | } |
| 90 | |
| 91 | constexpr double kMaxFastEncoderPulsesPerSecond = std::max({ |
| 92 | Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
Maxwell Henderson | 1de1549 | 2024-02-07 11:09:47 -0800 | [diff] [blame] | 93 | Values::kMaxIntakePivotEncoderPulsesPerSecond(), |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 94 | Values::kMaxClimberEncoderPulsesPerSecond(), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 95 | }); |
| 96 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 97 | "fast encoders are too fast"); |
| 98 | |
| 99 | } // namespace |
| 100 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 101 | // Class to send position messages with sensor readings to our loops. |
| 102 | class SensorReader : public ::frc971::wpilib::SensorReader { |
| 103 | public: |
| 104 | SensorReader(::aos::ShmEventLoop *event_loop, |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 105 | const Constants *robot_constants) |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 106 | : ::frc971::wpilib::SensorReader(event_loop), |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 107 | robot_constants_(CHECK_NOTNULL(robot_constants)), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 108 | auto_mode_sender_( |
| 109 | event_loop->MakeSender<::frc971::autonomous::AutonomousMode>( |
| 110 | "/autonomous")), |
| 111 | superstructure_position_sender_( |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 112 | event_loop->MakeSender<superstructure::PositionStatic>( |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 113 | "/superstructure")), |
| 114 | drivetrain_position_sender_( |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 115 | event_loop->MakeSender< |
| 116 | ::frc971::control_loops::drivetrain::PositionStatic>( |
| 117 | "/drivetrain")), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 118 | gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>( |
| 119 | "/drivetrain")){}; |
| 120 | void Start() override { AddToDMA(&imu_yaw_rate_reader_); } |
| 121 | |
| 122 | // Auto mode switches. |
| 123 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
| 124 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 125 | } |
| 126 | |
| 127 | void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) { |
| 128 | imu_yaw_rate_input_ = ::std::move(sensor); |
| 129 | imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get()); |
| 130 | } |
| 131 | |
| 132 | void RunIteration() override { |
| 133 | { |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 134 | aos::Sender<superstructure::PositionStatic>::StaticBuilder builder = |
| 135 | superstructure_position_sender_.MakeStaticBuilder(); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 136 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 137 | CopyPosition(intake_pivot_encoder_, builder->add_intake_pivot(), |
| 138 | Values::kIntakePivotEncoderCountsPerRevolution(), |
Niko Sohmers | 74b0ad5 | 2024-02-03 18:00:31 -0800 | [diff] [blame^] | 139 | Values::kIntakePivotEncoderRatio(), /* reversed: */ false); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 140 | |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 141 | CopyPosition(climber_encoder_, builder->add_climber(), |
| 142 | Values::kClimberEncoderCountsPerRevolution(), |
| 143 | Values::kClimberEncoderRatio(), climber_pot_translate, true, |
| 144 | robot_constants_->robot() |
| 145 | ->climber_constants() |
| 146 | ->potentiometer_offset()); |
| 147 | |
Niko Sohmers | ed7ffc4 | 2024-02-03 16:05:19 -0800 | [diff] [blame] | 148 | builder->set_transfer_beambreak(transfer_beam_break_->Get()); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 149 | builder.CheckOk(builder.Send()); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 152 | SendDrivetrainPosition(drivetrain_position_sender_.MakeStaticBuilder(), |
| 153 | drivetrain_velocity_translate, |
| 154 | constants::Values::DrivetrainEncoderToMeters, false, |
| 155 | false); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 156 | |
| 157 | { |
| 158 | auto builder = gyro_sender_.MakeBuilder(); |
| 159 | ::frc971::sensors::GyroReading::Builder gyro_reading_builder = |
| 160 | builder.MakeBuilder<::frc971::sensors::GyroReading>(); |
| 161 | // +/- 2000 deg / sec |
| 162 | constexpr double kMaxVelocity = 4000; // degrees / second |
| 163 | constexpr double kVelocityRadiansPerSecond = |
| 164 | kMaxVelocity / 360 * (2.0 * M_PI); |
| 165 | |
| 166 | // Only part of the full range is used to prevent being 100% on or off. |
| 167 | constexpr double kScaledRangeLow = 0.1; |
| 168 | constexpr double kScaledRangeHigh = 0.9; |
| 169 | |
| 170 | constexpr double kPWMFrequencyHz = 200; |
| 171 | double velocity_duty_cycle = |
| 172 | imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz; |
| 173 | |
| 174 | constexpr double kDutyCycleScale = |
| 175 | 1 / (kScaledRangeHigh - kScaledRangeLow); |
| 176 | // scale from 0.1 - 0.9 to 0 - 1 |
| 177 | double rescaled_velocity_duty_cycle = |
| 178 | (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale; |
| 179 | |
| 180 | if (!std::isnan(rescaled_velocity_duty_cycle)) { |
| 181 | gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) * |
| 182 | kVelocityRadiansPerSecond); |
| 183 | } |
| 184 | builder.CheckOk(builder.Send(gyro_reading_builder.Finish())); |
| 185 | } |
| 186 | |
| 187 | { |
| 188 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 189 | |
| 190 | uint32_t mode = 0; |
| 191 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 192 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 193 | mode |= 1 << i; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | auto auto_mode_builder = |
| 198 | builder.MakeBuilder<frc971::autonomous::AutonomousMode>(); |
| 199 | |
| 200 | auto_mode_builder.add_mode(mode); |
| 201 | |
| 202 | builder.CheckOk(builder.Send(auto_mode_builder.Finish())); |
| 203 | } |
| 204 | } |
| 205 | |
Maxwell Henderson | 6291783 | 2024-02-07 11:08:11 -0800 | [diff] [blame] | 206 | void set_intake_pivot(::std::unique_ptr<frc::Encoder> encoder, |
Niko Sohmers | 74b0ad5 | 2024-02-03 18:00:31 -0800 | [diff] [blame^] | 207 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 208 | fast_encoder_filter_.Add(encoder.get()); |
| 209 | intake_pivot_encoder_.set_encoder(::std::move(encoder)); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 210 | intake_pivot_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Niko Sohmers | ed7ffc4 | 2024-02-03 16:05:19 -0800 | [diff] [blame] | 213 | void set_transfer_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) { |
| 214 | transfer_beam_break_ = ::std::move(sensor); |
| 215 | } |
| 216 | |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 217 | void set_climber(::std::unique_ptr<frc::Encoder> encoder, |
| 218 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm, |
| 219 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 220 | fast_encoder_filter_.Add(encoder.get()); |
| 221 | climber_encoder_.set_encoder(::std::move(encoder)); |
| 222 | climber_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 223 | climber_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 224 | } |
| 225 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 226 | private: |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 227 | const Constants *robot_constants_; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 228 | |
| 229 | aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_; |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 230 | aos::Sender<superstructure::PositionStatic> superstructure_position_sender_; |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 231 | aos::Sender<frc971::control_loops::drivetrain::PositionStatic> |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 232 | drivetrain_position_sender_; |
| 233 | ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_; |
| 234 | |
| 235 | std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_; |
| 236 | |
Niko Sohmers | ed7ffc4 | 2024-02-03 16:05:19 -0800 | [diff] [blame] | 237 | std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_, transfer_beam_break_; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 238 | |
Niko Sohmers | 74b0ad5 | 2024-02-03 18:00:31 -0800 | [diff] [blame^] | 239 | frc971::wpilib::AbsoluteEncoder intake_pivot_encoder_; |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 240 | frc971::wpilib::AbsoluteEncoderAndPotentiometer climber_encoder_; |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 241 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 242 | frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_; |
| 243 | }; |
| 244 | |
| 245 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 246 | public: |
| 247 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 248 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 249 | frc::Encoder::k4X); |
| 250 | } |
| 251 | |
| 252 | void Run() override { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 253 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 254 | aos::configuration::ReadConfig("aos_config.json"); |
| 255 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 256 | frc971::constants::WaitForConstants<y2024::Constants>(&config.message()); |
| 257 | |
| 258 | ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message()); |
| 259 | frc971::constants::ConstantsFetcher<Constants> constants_fetcher( |
| 260 | &constant_fetcher_event_loop); |
| 261 | const Constants *robot_constants = &constants_fetcher.constants(); |
| 262 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 263 | // Thread 1. |
| 264 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
| 265 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 266 | &joystick_sender_event_loop); |
| 267 | AddLoop(&joystick_sender_event_loop); |
| 268 | |
| 269 | // Thread 2. |
| 270 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 271 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 272 | AddLoop(&pdp_fetcher_event_loop); |
| 273 | |
| 274 | // Thread 3. |
| 275 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 276 | SensorReader sensor_reader(&sensor_reader_event_loop, robot_constants); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 277 | sensor_reader.set_pwm_trigger(true); |
| 278 | sensor_reader.set_drivetrain_left_encoder(make_encoder(1)); |
| 279 | sensor_reader.set_drivetrain_right_encoder(make_encoder(0)); |
| 280 | sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0)); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 281 | // TODO: (niko) change values once robot is wired |
Maxwell Henderson | 6291783 | 2024-02-07 11:08:11 -0800 | [diff] [blame] | 282 | sensor_reader.set_intake_pivot(make_encoder(4), |
Niko Sohmers | 74b0ad5 | 2024-02-03 18:00:31 -0800 | [diff] [blame^] | 283 | make_unique<frc::DigitalInput>(4)); |
Niko Sohmers | ed7ffc4 | 2024-02-03 16:05:19 -0800 | [diff] [blame] | 284 | sensor_reader.set_transfer_beambreak(make_unique<frc::DigitalInput>(7)); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 285 | |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 286 | sensor_reader.set_climber(make_encoder(5), |
| 287 | make_unique<frc::DigitalInput>(5), |
| 288 | make_unique<frc::AnalogInput>(5)); |
| 289 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 290 | AddLoop(&sensor_reader_event_loop); |
| 291 | |
| 292 | // Thread 4. |
| 293 | // Set up CAN. |
| 294 | if (!FLAGS_ctre_diag_server) { |
| 295 | c_Phoenix_Diagnostics_SetSecondsToStart(-1); |
| 296 | c_Phoenix_Diagnostics_Dispose(); |
| 297 | } |
| 298 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 299 | std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry; |
| 300 | |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 301 | const CurrentLimits *current_limits = |
| 302 | robot_constants->common()->current_limits(); |
| 303 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 304 | std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>( |
| 305 | 0, false, "Drivetrain Bus", &signals_registry, |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 306 | current_limits->drivetrain_supply_current_limit(), |
| 307 | current_limits->drivetrain_stator_current_limit()); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 308 | std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>( |
| 309 | 1, false, "Drivetrain Bus", &signals_registry, |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 310 | current_limits->drivetrain_supply_current_limit(), |
| 311 | current_limits->drivetrain_stator_current_limit()); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 312 | std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>( |
| 313 | 2, false, "Drivetrain Bus", &signals_registry, |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 314 | current_limits->drivetrain_supply_current_limit(), |
| 315 | current_limits->drivetrain_stator_current_limit()); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 316 | std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>( |
| 317 | 3, false, "Drivetrain Bus", &signals_registry, |
Maxwell Henderson | fb3bfea | 2024-02-03 19:24:46 -0800 | [diff] [blame] | 318 | current_limits->drivetrain_supply_current_limit(), |
| 319 | current_limits->drivetrain_stator_current_limit()); |
| 320 | std::shared_ptr<TalonFX> intake_pivot = std::make_shared<TalonFX>( |
| 321 | 4, false, "Drivetrain Bus", &signals_registry, |
| 322 | current_limits->intake_pivot_stator_current_limit(), |
| 323 | current_limits->intake_pivot_supply_current_limit()); |
| 324 | std::shared_ptr<TalonFX> intake_roller = std::make_shared<TalonFX>( |
| 325 | 5, false, "Drivetrain Bus", &signals_registry, |
| 326 | current_limits->intake_roller_stator_current_limit(), |
| 327 | current_limits->intake_roller_supply_current_limit()); |
| 328 | std::shared_ptr<TalonFX> transfer_roller = std::make_shared<TalonFX>( |
| 329 | 6, false, "Drivetrain Bus", &signals_registry, |
| 330 | current_limits->transfer_roller_stator_current_limit(), |
| 331 | current_limits->transfer_roller_supply_current_limit()); |
| 332 | |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 333 | std::shared_ptr<TalonFX> climber = std::make_shared<TalonFX>( |
| 334 | 7, false, "Drivetrain Bus", &signals_registry, |
| 335 | current_limits->climber_stator_current_limit(), |
| 336 | current_limits->climber_supply_current_limit()); |
| 337 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 338 | ctre::phoenix::platform::can::CANComm_SetRxSchedPriority( |
| 339 | constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus"); |
| 340 | ctre::phoenix::platform::can::CANComm_SetTxSchedPriority( |
| 341 | constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus"); |
| 342 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 343 | ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message()); |
| 344 | can_sensor_reader_event_loop.set_name("CANSensorReader"); |
| 345 | |
| 346 | // Creating list of talonfx for CANSensorReader |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 347 | std::vector<std::shared_ptr<TalonFX>> drivetrain_talonfxs; |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 348 | std::vector<std::shared_ptr<TalonFX>> talonfxs; |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 349 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 350 | for (auto talonfx : {right_front, right_back, left_front, left_back}) { |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 351 | drivetrain_talonfxs.push_back(talonfx); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 352 | talonfxs.push_back(talonfx); |
| 353 | } |
| 354 | |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 355 | for (auto talonfx : |
| 356 | {intake_pivot, intake_roller, transfer_roller, climber}) { |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 357 | talonfxs.push_back(talonfx); |
| 358 | } |
| 359 | |
| 360 | aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic> |
| 361 | drivetrain_can_position_sender = |
| 362 | can_sensor_reader_event_loop.MakeSender< |
| 363 | frc971::control_loops::drivetrain::CANPositionStatic>( |
| 364 | "/drivetrain"); |
| 365 | |
| 366 | aos::Sender<y2024::control_loops::superstructure::CANPositionStatic> |
| 367 | superstructure_can_position_sender = |
| 368 | can_sensor_reader_event_loop.MakeSender< |
| 369 | y2024::control_loops::superstructure::CANPositionStatic>( |
| 370 | "/superstructure"); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 371 | |
| 372 | frc971::wpilib::CANSensorReader can_sensor_reader( |
| 373 | &can_sensor_reader_event_loop, std::move(signals_registry), talonfxs, |
Niko Sohmers | ed7ffc4 | 2024-02-03 16:05:19 -0800 | [diff] [blame] | 374 | [drivetrain_talonfxs, &intake_pivot, &intake_roller, &transfer_roller, |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 375 | &climber, &drivetrain_can_position_sender, |
| 376 | &superstructure_can_position_sender]( |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 377 | ctre::phoenix::StatusCode status) { |
| 378 | aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>:: |
| 379 | StaticBuilder drivetrain_can_builder = |
| 380 | drivetrain_can_position_sender.MakeStaticBuilder(); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 381 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 382 | auto drivetrain_falcon_vector = |
| 383 | drivetrain_can_builder->add_talonfxs(); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 384 | |
Maxwell Henderson | 9116e5b | 2024-01-21 12:14:26 -0800 | [diff] [blame] | 385 | CHECK(drivetrain_falcon_vector->reserve(drivetrain_talonfxs.size())); |
| 386 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 387 | for (auto talonfx : drivetrain_talonfxs) { |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 388 | talonfx->SerializePosition( |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 389 | drivetrain_falcon_vector->emplace_back(), |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 390 | control_loops::drivetrain::kHighOutputRatio); |
| 391 | } |
| 392 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 393 | drivetrain_can_builder->set_timestamp( |
| 394 | drivetrain_talonfxs.front()->GetTimestamp()); |
| 395 | drivetrain_can_builder->set_status(static_cast<int>(status)); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 396 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 397 | drivetrain_can_builder.CheckOk(drivetrain_can_builder.Send()); |
| 398 | |
| 399 | aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>:: |
| 400 | StaticBuilder superstructure_can_builder = |
| 401 | superstructure_can_position_sender.MakeStaticBuilder(); |
| 402 | |
| 403 | intake_roller->SerializePosition( |
| 404 | superstructure_can_builder->add_intake_roller(), |
| 405 | control_loops::drivetrain::kHighOutputRatio); |
| 406 | intake_pivot->SerializePosition( |
| 407 | superstructure_can_builder->add_intake_pivot(), |
| 408 | control_loops::drivetrain::kHighOutputRatio); |
Niko Sohmers | ed7ffc4 | 2024-02-03 16:05:19 -0800 | [diff] [blame] | 409 | transfer_roller->SerializePosition( |
| 410 | superstructure_can_builder->add_transfer_roller(), |
| 411 | control_loops::drivetrain::kHighOutputRatio); |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 412 | climber->SerializePosition( |
| 413 | superstructure_can_builder->add_climber(), |
| 414 | control_loops::drivetrain::kHighOutputRatio); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 415 | |
| 416 | superstructure_can_builder->set_timestamp( |
| 417 | intake_roller->GetTimestamp()); |
| 418 | superstructure_can_builder->set_status(static_cast<int>(status)); |
| 419 | superstructure_can_builder.CheckOk(superstructure_can_builder.Send()); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 420 | }); |
| 421 | |
| 422 | AddLoop(&can_sensor_reader_event_loop); |
| 423 | |
| 424 | // Thread 5. |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 425 | ::aos::ShmEventLoop can_output_event_loop(&config.message()); |
| 426 | can_output_event_loop.set_name("CANOutputWriter"); |
| 427 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 428 | frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer( |
| 429 | &can_output_event_loop); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 430 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 431 | frc971::wpilib::GenericCANWriter<control_loops::superstructure::Output> |
| 432 | can_superstructure_writer( |
| 433 | &can_output_event_loop, |
| 434 | [](const control_loops::superstructure::Output &output, |
| 435 | const std::map<std::string_view, std::shared_ptr<TalonFX>> |
| 436 | &talonfx_map) { |
| 437 | talonfx_map.find("intake_pivot") |
| 438 | ->second->WriteVoltage(output.intake_pivot_voltage()); |
| 439 | talonfx_map.find("intake_roller") |
| 440 | ->second->WriteVoltage(output.intake_roller_voltage()); |
Niko Sohmers | ed7ffc4 | 2024-02-03 16:05:19 -0800 | [diff] [blame] | 441 | talonfx_map.find("transfer_roller") |
| 442 | ->second->WriteVoltage(output.transfer_roller_voltage()); |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 443 | talonfx_map.find("climber")->second->WriteVoltage( |
| 444 | output.climber_voltage()); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 445 | }); |
| 446 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 447 | can_drivetrain_writer.set_talonfxs({right_front, right_back}, |
| 448 | {left_front, left_back}); |
| 449 | |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 450 | can_superstructure_writer.add_talonfx("intake_pivot", intake_pivot); |
| 451 | can_superstructure_writer.add_talonfx("intake_roller", intake_roller); |
Niko Sohmers | ed7ffc4 | 2024-02-03 16:05:19 -0800 | [diff] [blame] | 452 | can_superstructure_writer.add_talonfx("transfer_roller", transfer_roller); |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 453 | can_superstructure_writer.add_talonfx("climber", climber); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 454 | |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 455 | can_output_event_loop.MakeWatcher( |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 456 | "/roborio", [&can_drivetrain_writer, &can_superstructure_writer]( |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 457 | const frc971::CANConfiguration &configuration) { |
| 458 | can_drivetrain_writer.HandleCANConfiguration(configuration); |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 459 | can_superstructure_writer.HandleCANConfiguration(configuration); |
Maxwell Henderson | f75800f | 2024-01-12 19:52:05 -0800 | [diff] [blame] | 460 | }); |
| 461 | |
| 462 | AddLoop(&can_output_event_loop); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 463 | |
| 464 | // Thread 6 |
| 465 | |
| 466 | RunLoops(); |
| 467 | } |
| 468 | }; |
| 469 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 470 | } // namespace y2024::wpilib |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 471 | |
| 472 | AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot); |