blob: 37486d75fa7fabeaeca181fd0c003fbd3809c9da [file] [log] [blame]
Niko Sohmers3860f8a2024-01-12 21:05:19 -08001#include <unistd.h>
2
3#include <array>
4#include <chrono>
5#include <cinttypes>
Niko Sohmers3860f8a2024-01-12 21:05:19 -08006#include <cstdio>
7#include <cstring>
8#include <functional>
9#include <memory>
10#include <mutex>
11#include <thread>
12
Niko Sohmers3860f8a2024-01-12 21:05:19 -080013#include "frc971/wpilib/ahal/AnalogInput.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080014#include "frc971/wpilib/ahal/DriverStation.h"
15#include "frc971/wpilib/ahal/Encoder.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080016#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 Sohmers3860f8a2024-01-12 21:05:19 -080021
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 Sohmers1259b2a2024-01-29 18:00:37 -080035#include "frc971/constants/constants_sender_lib.h"
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080036#include "frc971/control_loops/drivetrain/drivetrain_can_position_static.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080037#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 Sohmers3860f8a2024-01-12 21:05:19 -080040#include "frc971/wpilib/buffered_pcm.h"
41#include "frc971/wpilib/buffered_solenoid.h"
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080042#include "frc971/wpilib/can_drivetrain_writer.h"
43#include "frc971/wpilib/can_sensor_reader.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080044#include "frc971/wpilib/dma.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080045#include "frc971/wpilib/encoder_and_potentiometer.h"
Niko Sohmers1259b2a2024-01-29 18:00:37 -080046#include "frc971/wpilib/generic_can_writer.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080047#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 Hendersonf75800f2024-01-12 19:52:05 -080052#include "frc971/wpilib/talonfx.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080053#include "frc971/wpilib/wpilib_robot_base.h"
54#include "y2024/constants.h"
Niko Sohmers1259b2a2024-01-29 18:00:37 -080055#include "y2024/constants/constants_generated.h"
56#include "y2024/control_loops/superstructure/superstructure_can_position_static.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080057#include "y2024/control_loops/superstructure/superstructure_output_generated.h"
58#include "y2024/control_loops/superstructure/superstructure_position_generated.h"
Niko Sohmers1259b2a2024-01-29 18:00:37 -080059#include "y2024/control_loops/superstructure/superstructure_position_static.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080060
61DEFINE_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
65using ::aos::monotonic_clock;
66using ::frc971::CANConfiguration;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080067using ::frc971::control_loops::drivetrain::CANPositionStatic;
68using ::frc971::wpilib::TalonFX;
Niko Sohmers3860f8a2024-01-12 21:05:19 -080069using ::y2024::constants::Values;
70namespace superstructure = ::y2024::control_loops::superstructure;
71namespace drivetrain = ::y2024::control_loops::drivetrain;
72namespace chrono = ::std::chrono;
73using std::make_unique;
74
Stephan Pleinesf63bde82024-01-13 15:59:33 -080075namespace y2024::wpilib {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080076namespace {
77
78constexpr double kMaxBringupPower = 12.0;
79
Filip Kujawa749f2442024-02-04 01:12:35 -080080double climber_pot_translate(double voltage) {
Maxwell Hendersonff831f52024-02-21 13:51:59 -080081 return -1 * voltage * Values::kClimberPotMetersPerVolt();
Filip Kujawa749f2442024-02-04 01:12:35 -080082}
83
Filip Kujawad75252a2024-02-10 16:54:35 -080084double extend_pot_translate(double voltage) {
85 return voltage * Values::kExtendPotMetersPerVolt();
86}
87
Niko Sohmers27d92c62024-02-19 14:15:07 -080088double catapult_pot_translate(double voltage) {
89 return voltage * Values::kCatapultPotRadiansPerVolt();
90}
91
92double turret_pot_translate(double voltage) {
Maxwell Henderson3d68e142024-02-25 09:58:11 -080093 return -1 * voltage * Values::kTurretPotRadiansPerVolt();
Niko Sohmers27d92c62024-02-19 14:15:07 -080094}
95
96double altitude_pot_translate(double voltage) {
Maxwell Henderson0b6fed62024-02-23 11:05:09 -080097 return -1 * voltage * Values::kAltitudePotRadiansPerVolt();
Niko Sohmers27d92c62024-02-19 14:15:07 -080098}
99
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800100double drivetrain_velocity_translate(double in) {
101 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
102 (2.0 * M_PI)) *
103 Values::kDrivetrainEncoderRatio() *
104 control_loops::drivetrain::kWheelRadius;
105}
106
107constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
108 Values::kMaxDrivetrainEncoderPulsesPerSecond(),
Maxwell Henderson1de15492024-02-07 11:09:47 -0800109 Values::kMaxIntakePivotEncoderPulsesPerSecond(),
Filip Kujawa749f2442024-02-04 01:12:35 -0800110 Values::kMaxClimberEncoderPulsesPerSecond(),
Filip Kujawad75252a2024-02-10 16:54:35 -0800111 Values::kMaxExtendEncoderPulsesPerSecond(),
Niko Sohmers27d92c62024-02-19 14:15:07 -0800112 Values::kMaxCatapultEncoderPulsesPerSecond(),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800113});
Niko Sohmers27d92c62024-02-19 14:15:07 -0800114
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800115static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
116 "fast encoders are too fast");
117
118} // namespace
119
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800120// Class to send position messages with sensor readings to our loops.
121class SensorReader : public ::frc971::wpilib::SensorReader {
122 public:
123 SensorReader(::aos::ShmEventLoop *event_loop,
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800124 const Constants *robot_constants)
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800125 : ::frc971::wpilib::SensorReader(event_loop),
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800126 robot_constants_(CHECK_NOTNULL(robot_constants)),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800127 auto_mode_sender_(
128 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
129 "/autonomous")),
130 superstructure_position_sender_(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800131 event_loop->MakeSender<superstructure::PositionStatic>(
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800132 "/superstructure")),
133 drivetrain_position_sender_(
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800134 event_loop->MakeSender<
135 ::frc971::control_loops::drivetrain::PositionStatic>(
136 "/drivetrain")),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800137 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
Maxwell Hendersonafc5c6d2024-02-20 15:34:13 -0800138 "/drivetrain")) {
139 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
140 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0}));
141 };
142 void Start() override {
143 AddToDMA(&imu_yaw_rate_reader_);
144 AddToDMA(&turret_encoder_.reader());
145 AddToDMA(&altitude_encoder_.reader());
146 }
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800147
148 // Auto mode switches.
149 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
150 autonomous_modes_.at(i) = ::std::move(sensor);
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
158 void RunIteration() override {
159 {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800160 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
161 superstructure_position_sender_.MakeStaticBuilder();
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800162
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800163 CopyPosition(intake_pivot_encoder_, builder->add_intake_pivot(),
164 Values::kIntakePivotEncoderCountsPerRevolution(),
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800165 Values::kIntakePivotEncoderRatio(), /* reversed: */ false);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800166
Filip Kujawa749f2442024-02-04 01:12:35 -0800167 CopyPosition(climber_encoder_, builder->add_climber(),
168 Values::kClimberEncoderCountsPerRevolution(),
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800169 Values::kClimberEncoderMetersPerRadian(),
170 climber_pot_translate, false,
Filip Kujawa749f2442024-02-04 01:12:35 -0800171 robot_constants_->robot()
172 ->climber_constants()
173 ->potentiometer_offset());
174
Filip Kujawad75252a2024-02-10 16:54:35 -0800175 CopyPosition(extend_encoder_, builder->add_extend(),
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800176 Values::kExtendEncoderCountsPerRevolution(),
177 Values::kExtendEncoderMetersPerRadian(),
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800178 extend_pot_translate, false,
Filip Kujawad75252a2024-02-10 16:54:35 -0800179 robot_constants_->robot()
180 ->extend_constants()
181 ->potentiometer_offset());
182
Niko Sohmers27d92c62024-02-19 14:15:07 -0800183 CopyPosition(catapult_encoder_, builder->add_catapult(),
184 Values::kCatapultEncoderCountsPerRevolution(),
185 Values::kCatapultEncoderRatio(), catapult_pot_translate,
186 true,
187 robot_constants_->robot()
188 ->catapult_constants()
189 ->potentiometer_offset());
190
191 CopyPosition(turret_encoder_, builder->add_turret(),
192 Values::kTurretEncoderCountsPerRevolution(),
193 Values::kTurretEncoderRatio(), turret_pot_translate, true,
194 robot_constants_->robot()
195 ->turret_constants()
196 ->potentiometer_offset());
197
198 CopyPosition(altitude_encoder_, builder->add_altitude(),
199 Values::kAltitudeEncoderCountsPerRevolution(),
200 Values::kAltitudeEncoderRatio(), altitude_pot_translate,
201 true,
202 robot_constants_->robot()
203 ->altitude_constants()
204 ->potentiometer_offset());
205
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800206 builder->set_transfer_beambreak(transfer_beam_break_->Get());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800207 builder->set_extend_beambreak(extend_beam_break_->Get());
Maxwell Henderson576b0c92024-02-23 17:10:48 -0800208 builder->set_catapult_beambreak(catapult_beam_break_->Get());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800209 builder.CheckOk(builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800210 }
211
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800212 SendDrivetrainPosition(drivetrain_position_sender_.MakeStaticBuilder(),
213 drivetrain_velocity_translate,
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800214 constants::Values::DrivetrainEncoderToMeters, true,
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800215 false);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800216
217 {
218 auto builder = gyro_sender_.MakeBuilder();
219 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
220 builder.MakeBuilder<::frc971::sensors::GyroReading>();
221 // +/- 2000 deg / sec
222 constexpr double kMaxVelocity = 4000; // degrees / second
223 constexpr double kVelocityRadiansPerSecond =
224 kMaxVelocity / 360 * (2.0 * M_PI);
225
226 // Only part of the full range is used to prevent being 100% on or off.
227 constexpr double kScaledRangeLow = 0.1;
228 constexpr double kScaledRangeHigh = 0.9;
229
230 constexpr double kPWMFrequencyHz = 200;
231 double velocity_duty_cycle =
232 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
233
234 constexpr double kDutyCycleScale =
235 1 / (kScaledRangeHigh - kScaledRangeLow);
236 // scale from 0.1 - 0.9 to 0 - 1
237 double rescaled_velocity_duty_cycle =
238 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
239
240 if (!std::isnan(rescaled_velocity_duty_cycle)) {
241 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
242 kVelocityRadiansPerSecond);
243 }
244 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
245 }
246
247 {
248 auto builder = auto_mode_sender_.MakeBuilder();
249
250 uint32_t mode = 0;
251 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
252 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
253 mode |= 1 << i;
254 }
255 }
256
257 auto auto_mode_builder =
258 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
259
260 auto_mode_builder.add_mode(mode);
261
262 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
263 }
264 }
265
Maxwell Henderson62917832024-02-07 11:08:11 -0800266 void set_intake_pivot(::std::unique_ptr<frc::Encoder> encoder,
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800267 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800268 fast_encoder_filter_.Add(encoder.get());
269 intake_pivot_encoder_.set_encoder(::std::move(encoder));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800270 intake_pivot_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800271 }
272
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800273 void set_transfer_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
274 transfer_beam_break_ = ::std::move(sensor);
275 }
276
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800277 void set_extend_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
278 extend_beam_break_ = ::std::move(sensor);
279 }
280
Maxwell Henderson576b0c92024-02-23 17:10:48 -0800281 void set_catapult_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
282 catapult_beam_break_ = ::std::move(sensor);
283 }
284
Filip Kujawa749f2442024-02-04 01:12:35 -0800285 void set_climber(::std::unique_ptr<frc::Encoder> encoder,
286 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
287 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
288 fast_encoder_filter_.Add(encoder.get());
289 climber_encoder_.set_encoder(::std::move(encoder));
290 climber_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
291 climber_encoder_.set_potentiometer(::std::move(potentiometer));
292 }
293
Filip Kujawad75252a2024-02-10 16:54:35 -0800294 void set_extend(::std::unique_ptr<frc::Encoder> encoder,
295 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
296 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
297 fast_encoder_filter_.Add(encoder.get());
298 extend_encoder_.set_encoder(::std::move(encoder));
299 extend_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
300 extend_encoder_.set_potentiometer(::std::move(potentiometer));
301 }
302
Niko Sohmers27d92c62024-02-19 14:15:07 -0800303 void set_catapult(::std::unique_ptr<frc::Encoder> encoder,
304 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
305 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
306 fast_encoder_filter_.Add(encoder.get());
307 catapult_encoder_.set_encoder(::std::move(encoder));
308 catapult_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
309 catapult_encoder_.set_potentiometer(::std::move(potentiometer));
310 }
311
312 void set_turret(::std::unique_ptr<frc::Encoder> encoder,
313 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
314 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
315 fast_encoder_filter_.Add(encoder.get());
316 turret_encoder_.set_encoder(::std::move(encoder));
317 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
318 turret_encoder_.set_potentiometer(::std::move(potentiometer));
319 }
320
321 void set_altitude(::std::unique_ptr<frc::Encoder> encoder,
322 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
323 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
324 fast_encoder_filter_.Add(encoder.get());
325 altitude_encoder_.set_encoder(::std::move(encoder));
326 altitude_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
327 altitude_encoder_.set_potentiometer(::std::move(potentiometer));
328 }
329
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800330 private:
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800331 const Constants *robot_constants_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800332
333 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800334 aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800335 aos::Sender<frc971::control_loops::drivetrain::PositionStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800336 drivetrain_position_sender_;
337 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
338
339 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
340
Maxwell Henderson576b0c92024-02-23 17:10:48 -0800341 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_, transfer_beam_break_,
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800342 extend_beam_break_, catapult_beam_break_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800343
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800344 frc971::wpilib::AbsoluteEncoder intake_pivot_encoder_;
Niko Sohmers27d92c62024-02-19 14:15:07 -0800345 frc971::wpilib::AbsoluteEncoderAndPotentiometer climber_encoder_,
Maxwell Hendersonafc5c6d2024-02-20 15:34:13 -0800346 catapult_encoder_, extend_encoder_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800347
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800348 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
Maxwell Hendersonafc5c6d2024-02-20 15:34:13 -0800349
350 frc971::wpilib::DMAAbsoluteEncoderAndPotentiometer turret_encoder_,
351 altitude_encoder_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800352};
353
Maxwell Hendersonabcc92a2024-02-21 19:06:26 -0800354class SuperstructurePWMWriter
355 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
356 public:
357 SuperstructurePWMWriter(aos::EventLoop *event_loop)
358 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
359 event_loop, "/superstructure") {}
360
361 void set_catapult_kraken_one(::std::unique_ptr<::frc::TalonFX> t) {
362 catapult_kraken_one_ = ::std::move(t);
363 }
364 void set_catapult_kraken_two(::std::unique_ptr<::frc::TalonFX> t) {
365 catapult_kraken_two_ = ::std::move(t);
366 }
367
368 private:
369 void Stop() override {
370 AOS_LOG(WARNING, "Superstructure output too old.\n");
371 catapult_kraken_one_->SetDisabled();
372 catapult_kraken_two_->SetDisabled();
373 }
374
375 void Write(const superstructure::Output &output) override {
376 WritePwm(output.catapult_voltage(), catapult_kraken_one_.get());
Maxwell Henderson12d427b2024-02-22 13:57:45 -0800377 WritePwm(output.catapult_voltage(), catapult_kraken_two_.get());
Maxwell Hendersonabcc92a2024-02-21 19:06:26 -0800378 }
379
380 template <typename T>
381 static void WritePwm(const double voltage, T *motor) {
382 motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) /
383 12.0);
384 }
385 ::std::unique_ptr<::frc::TalonFX> catapult_kraken_one_, catapult_kraken_two_;
386};
387
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800388class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
389 public:
390 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
391 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
392 frc::Encoder::k4X);
393 }
394
395 void Run() override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800396 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
397 aos::configuration::ReadConfig("aos_config.json");
398
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800399 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
400
401 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
402 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(
403 &constant_fetcher_event_loop);
404 const Constants *robot_constants = &constants_fetcher.constants();
405
Maxwell Hendersonca7c9b62024-02-21 13:20:09 -0800406 AddLoop(&constant_fetcher_event_loop);
407
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800408 // Thread 1.
409 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
410 ::frc971::wpilib::JoystickSender joystick_sender(
411 &joystick_sender_event_loop);
412 AddLoop(&joystick_sender_event_loop);
413
414 // Thread 2.
415 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
416 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
417 AddLoop(&pdp_fetcher_event_loop);
418
419 // Thread 3.
420 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800421 SensorReader sensor_reader(&sensor_reader_event_loop, robot_constants);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800422 sensor_reader.set_pwm_trigger(true);
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800423 sensor_reader.set_drivetrain_left_encoder(
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800424 std::make_unique<frc::Encoder>(8, 9));
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800425 sensor_reader.set_drivetrain_right_encoder(
426 std::make_unique<frc::Encoder>(6, 7));
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800427 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(25));
428 sensor_reader.set_intake_pivot(make_encoder(3),
429 make_unique<frc::DigitalInput>(3));
430 sensor_reader.set_transfer_beambreak(make_unique<frc::DigitalInput>(23));
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800431 sensor_reader.set_extend_beambreak(make_unique<frc::DigitalInput>(24));
432 sensor_reader.set_catapult_beambreak(make_unique<frc::DigitalInput>(22));
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800433
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800434 sensor_reader.set_climber(make_encoder(4),
435 make_unique<frc::DigitalInput>(4),
436 make_unique<frc::AnalogInput>(4));
437 sensor_reader.set_extend(make_encoder(5), make_unique<frc::DigitalInput>(5),
438 make_unique<frc::AnalogInput>(5));
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800439 sensor_reader.set_catapult(make_encoder(0),
440 make_unique<frc::DigitalInput>(0),
441 make_unique<frc::AnalogInput>(0));
442 sensor_reader.set_turret(make_encoder(2), make_unique<frc::DigitalInput>(2),
443 make_unique<frc::AnalogInput>(2));
444 sensor_reader.set_altitude(make_encoder(1),
445 make_unique<frc::DigitalInput>(1),
446 make_unique<frc::AnalogInput>(1));
Filip Kujawad75252a2024-02-10 16:54:35 -0800447
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800448 AddLoop(&sensor_reader_event_loop);
449
450 // Thread 4.
451 // Set up CAN.
452 if (!FLAGS_ctre_diag_server) {
453 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
454 c_Phoenix_Diagnostics_Dispose();
455 }
456
Niko Sohmers84273952024-02-14 18:40:55 -0800457 std::vector<ctre::phoenix6::BaseStatusSignal *> canivore_signal_registry;
458 std::vector<ctre::phoenix6::BaseStatusSignal *> rio_signal_registry;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800459
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800460 const CurrentLimits *current_limits =
461 robot_constants->common()->current_limits();
462
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800463 std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800464 2, true, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800465 current_limits->drivetrain_supply_current_limit(),
466 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800467 std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800468 1, true, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800469 current_limits->drivetrain_supply_current_limit(),
470 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800471 std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800472 4, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800473 current_limits->drivetrain_supply_current_limit(),
474 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800475 std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800476 5, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800477 current_limits->drivetrain_supply_current_limit(),
478 current_limits->drivetrain_stator_current_limit());
479 std::shared_ptr<TalonFX> intake_pivot = std::make_shared<TalonFX>(
Maxwell Henderson30ea8b32024-02-22 16:14:05 -0800480 6, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800481 current_limits->intake_pivot_stator_current_limit(),
482 current_limits->intake_pivot_supply_current_limit());
Niko Sohmers27d92c62024-02-19 14:15:07 -0800483 std::shared_ptr<TalonFX> altitude = std::make_shared<TalonFX>(
Maxwell Henderson30ea8b32024-02-22 16:14:05 -0800484 9, false, "Drivetrain Bus", &canivore_signal_registry,
Niko Sohmers27d92c62024-02-19 14:15:07 -0800485 current_limits->altitude_stator_current_limit(),
486 current_limits->altitude_supply_current_limit());
487 std::shared_ptr<TalonFX> turret = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800488 3, true, "Drivetrain Bus", &canivore_signal_registry,
Niko Sohmers27d92c62024-02-19 14:15:07 -0800489 current_limits->turret_stator_current_limit(),
490 current_limits->turret_supply_current_limit());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800491 std::shared_ptr<TalonFX> climber = std::make_shared<TalonFX>(
492 7, true, "rio", &rio_signal_registry,
493 current_limits->climber_stator_current_limit(),
494 current_limits->climber_supply_current_limit());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800495 std::shared_ptr<TalonFX> extend = std::make_shared<TalonFX>(
Austin Schuh95764db2024-03-01 19:21:09 -0800496 12, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800497 current_limits->extend_stator_current_limit(),
498 current_limits->extend_supply_current_limit());
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800499 std::shared_ptr<TalonFX> intake_roller = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800500 8, false, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800501 current_limits->intake_roller_stator_current_limit(),
502 current_limits->intake_roller_supply_current_limit());
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800503 std::shared_ptr<TalonFX> retention_roller = std::make_shared<TalonFX>(
Maxwell Hendersonefead392024-02-23 19:10:50 -0800504 10, true, "rio", &rio_signal_registry,
Maxwell Hendersonfe735652024-02-23 19:22:31 -0800505 current_limits->retention_roller_stator_current_limit(),
506 current_limits->retention_roller_supply_current_limit());
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800507 std::shared_ptr<TalonFX> transfer_roller = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800508 11, true, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800509 current_limits->transfer_roller_stator_current_limit(),
510 current_limits->transfer_roller_supply_current_limit());
Filip Kujawad75252a2024-02-10 16:54:35 -0800511 std::shared_ptr<TalonFX> extend_roller = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800512 13, true, "rio", &rio_signal_registry,
Filip Kujawad75252a2024-02-10 16:54:35 -0800513 current_limits->extend_roller_stator_current_limit(),
514 current_limits->extend_roller_supply_current_limit());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800515 std::shared_ptr<TalonFX> catapult_one = std::make_shared<TalonFX>(
516 14, false, "Drivetrain Bus", &canivore_signal_registry,
517 current_limits->catapult_stator_current_limit(),
518 current_limits->catapult_supply_current_limit());
519 std::shared_ptr<TalonFX> catapult_two = std::make_shared<TalonFX>(
520 15, false, "Drivetrain Bus", &canivore_signal_registry,
521 current_limits->catapult_stator_current_limit(),
522 current_limits->catapult_supply_current_limit());
Filip Kujawad75252a2024-02-10 16:54:35 -0800523
Stephan Pleines5aabf632024-03-13 20:31:18 -0700524 transfer_roller->set_neutral_mode(
525 ctre::phoenix6::signals::NeutralModeValue::Coast);
526 intake_roller->set_neutral_mode(
527 ctre::phoenix6::signals::NeutralModeValue::Coast);
528
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800529 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
530 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
531 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
532 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
533
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800534 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
535 can_sensor_reader_event_loop.set_name("CANSensorReader");
536
Niko Sohmers84273952024-02-14 18:40:55 -0800537 ::aos::ShmEventLoop rio_sensor_reader_event_loop(&config.message());
538 rio_sensor_reader_event_loop.set_name("RioSensorReader");
539
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800540 // Creating list of talonfx for CANSensorReader
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800541 std::vector<std::shared_ptr<TalonFX>> drivetrain_talonfxs;
Niko Sohmers84273952024-02-14 18:40:55 -0800542 std::vector<std::shared_ptr<TalonFX>> canivore_talonfxs;
543 std::vector<std::shared_ptr<TalonFX>> rio_talonfxs;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800544
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800545 for (auto talonfx : {right_front, right_back, left_front, left_back}) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800546 drivetrain_talonfxs.push_back(talonfx);
Niko Sohmers84273952024-02-14 18:40:55 -0800547 canivore_talonfxs.push_back(talonfx);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800548 }
549
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800550 for (auto talonfx :
Austin Schuh95764db2024-03-01 19:21:09 -0800551 {intake_pivot, turret, altitude, catapult_one, catapult_two, extend}) {
Niko Sohmers84273952024-02-14 18:40:55 -0800552 canivore_talonfxs.push_back(talonfx);
553 }
554
Austin Schuh95764db2024-03-01 19:21:09 -0800555 for (auto talonfx : {intake_roller, transfer_roller, climber, extend_roller,
556 retention_roller}) {
Niko Sohmers84273952024-02-14 18:40:55 -0800557 rio_talonfxs.push_back(talonfx);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800558 }
559
560 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>
561 drivetrain_can_position_sender =
562 can_sensor_reader_event_loop.MakeSender<
563 frc971::control_loops::drivetrain::CANPositionStatic>(
564 "/drivetrain");
565
566 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
567 superstructure_can_position_sender =
568 can_sensor_reader_event_loop.MakeSender<
569 y2024::control_loops::superstructure::CANPositionStatic>(
Niko Sohmers84273952024-02-14 18:40:55 -0800570 "/superstructure/canivore");
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800571
Niko Sohmers84273952024-02-14 18:40:55 -0800572 frc971::wpilib::CANSensorReader canivore_can_sensor_reader(
573 &can_sensor_reader_event_loop, std::move(canivore_signal_registry),
574 canivore_talonfxs,
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800575 [drivetrain_talonfxs, &intake_pivot, &turret, &altitude, &catapult_one,
576 &catapult_two, &drivetrain_can_position_sender,
Austin Schuh95764db2024-03-01 19:21:09 -0800577 &superstructure_can_position_sender,
578 &extend](ctre::phoenix::StatusCode status) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800579 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>::
580 StaticBuilder drivetrain_can_builder =
581 drivetrain_can_position_sender.MakeStaticBuilder();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800582
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800583 auto drivetrain_falcon_vector =
Maxwell Henderson563efed2024-02-17 21:11:33 -0800584 CHECK_NOTNULL(drivetrain_can_builder->add_talonfxs());
Maxwell Henderson9116e5b2024-01-21 12:14:26 -0800585
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800586 for (auto talonfx : drivetrain_talonfxs) {
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800587 talonfx->SerializePosition(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800588 drivetrain_falcon_vector->emplace_back(),
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800589 control_loops::drivetrain::kHighOutputRatio);
590 }
591
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800592 drivetrain_can_builder->set_status(static_cast<int>(status));
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800593
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800594 drivetrain_can_builder.CheckOk(drivetrain_can_builder.Send());
595
596 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
597 StaticBuilder superstructure_can_builder =
598 superstructure_can_position_sender.MakeStaticBuilder();
599
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800600 intake_pivot->SerializePosition(
601 superstructure_can_builder->add_intake_pivot(),
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800602 control_loops::superstructure::intake_pivot::kOutputRatio);
Niko Sohmers27d92c62024-02-19 14:15:07 -0800603 turret->SerializePosition(
604 superstructure_can_builder->add_turret(),
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800605 control_loops::superstructure::turret::kOutputRatio);
606 altitude->SerializePosition(
607 superstructure_can_builder->add_altitude(),
608 control_loops::superstructure::altitude::kOutputRatio);
609 catapult_one->SerializePosition(
610 superstructure_can_builder->add_catapult_one(),
611 control_loops::superstructure::catapult::kOutputRatio);
612 catapult_two->SerializePosition(
613 superstructure_can_builder->add_catapult_two(),
614 control_loops::superstructure::catapult::kOutputRatio);
Austin Schuh95764db2024-03-01 19:21:09 -0800615 extend->SerializePosition(superstructure_can_builder->add_extend(),
616 superstructure::extend::kOutputRatio);
Niko Sohmers84273952024-02-14 18:40:55 -0800617
Niko Sohmers84273952024-02-14 18:40:55 -0800618 superstructure_can_builder->set_status(static_cast<int>(status));
619 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
620 });
621
622 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
623 superstructure_rio_position_sender =
624 rio_sensor_reader_event_loop.MakeSender<
625 y2024::control_loops::superstructure::CANPositionStatic>(
626 "/superstructure/rio");
627
628 frc971::wpilib::CANSensorReader rio_can_sensor_reader(
629 &rio_sensor_reader_event_loop, std::move(rio_signal_registry),
630 rio_talonfxs,
Austin Schuh95764db2024-03-01 19:21:09 -0800631 [&intake_roller, &transfer_roller, &climber, &extend_roller,
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800632 &retention_roller, &superstructure_rio_position_sender](
Niko Sohmers84273952024-02-14 18:40:55 -0800633 ctre::phoenix::StatusCode status) {
634 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
635 StaticBuilder superstructure_can_builder =
636 superstructure_rio_position_sender.MakeStaticBuilder();
637
638 intake_roller->SerializePosition(
639 superstructure_can_builder->add_intake_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800640 constants::Values::kIntakeRollerOutputRatio);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800641 transfer_roller->SerializePosition(
642 superstructure_can_builder->add_transfer_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800643 constants::Values::kIntakeRollerOutputRatio);
644 climber->SerializePosition(superstructure_can_builder->add_climber(),
645 superstructure::climber::kOutputRatio);
Filip Kujawad75252a2024-02-10 16:54:35 -0800646 extend_roller->SerializePosition(
647 superstructure_can_builder->add_extend_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800648 constants::Values::kExtendRollerOutputRatio);
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800649 retention_roller->SerializePosition(
650 superstructure_can_builder->add_retention_roller(), 1.0);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800651
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800652 superstructure_can_builder->set_status(static_cast<int>(status));
653 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800654 },
655 frc971::wpilib::CANSensorReader::SignalSync::kNoSync);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800656
657 AddLoop(&can_sensor_reader_event_loop);
Niko Sohmers84273952024-02-14 18:40:55 -0800658 AddLoop(&rio_sensor_reader_event_loop);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800659
660 // Thread 5.
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800661 ::aos::ShmEventLoop can_output_event_loop(&config.message());
662 can_output_event_loop.set_name("CANOutputWriter");
663
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800664 frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer(
665 &can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800666
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800667 frc971::wpilib::GenericCANWriter<control_loops::superstructure::Output>
668 can_superstructure_writer(
669 &can_output_event_loop,
670 [](const control_loops::superstructure::Output &output,
671 const std::map<std::string_view, std::shared_ptr<TalonFX>>
672 &talonfx_map) {
673 talonfx_map.find("intake_pivot")
674 ->second->WriteVoltage(output.intake_pivot_voltage());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800675 talonfx_map.find("altitude")
676 ->second->WriteVoltage(output.altitude_voltage());
677 talonfx_map.find("catapult_one")
678 ->second->WriteVoltage(output.catapult_voltage());
679 talonfx_map.find("catapult_two")
680 ->second->WriteVoltage(output.catapult_voltage());
681 talonfx_map.find("turret")->second->WriteVoltage(
682 output.turret_voltage());
Filip Kujawa749f2442024-02-04 01:12:35 -0800683 talonfx_map.find("climber")->second->WriteVoltage(
684 output.climber_voltage());
Filip Kujawad75252a2024-02-10 16:54:35 -0800685 talonfx_map.find("extend")->second->WriteVoltage(
686 output.extend_voltage());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800687 talonfx_map.find("intake_roller")
688 ->second->WriteVoltage(output.intake_roller_voltage());
689 talonfx_map.find("transfer_roller")
690 ->second->WriteVoltage(output.transfer_roller_voltage());
Filip Kujawad75252a2024-02-10 16:54:35 -0800691 talonfx_map.find("extend_roller")
692 ->second->WriteVoltage(output.extend_roller_voltage());
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800693 talonfx_map.find("retention_roller")
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800694 ->second->WriteCurrent(
695 output.retention_roller_stator_current_limit(),
696 output.retention_roller_voltage());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800697 });
698
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800699 can_drivetrain_writer.set_talonfxs({right_front, right_back},
700 {left_front, left_back});
701
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800702 can_superstructure_writer.add_talonfx("intake_pivot", intake_pivot);
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800703 can_superstructure_writer.add_talonfx("altitude", altitude);
704 can_superstructure_writer.add_talonfx("catapult_one", catapult_one);
705 can_superstructure_writer.add_talonfx("catapult_two", catapult_two);
706 can_superstructure_writer.add_talonfx("turret", turret);
Filip Kujawa749f2442024-02-04 01:12:35 -0800707 can_superstructure_writer.add_talonfx("climber", climber);
Filip Kujawad75252a2024-02-10 16:54:35 -0800708 can_superstructure_writer.add_talonfx("extend", extend);
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800709 can_superstructure_writer.add_talonfx("intake_roller", intake_roller);
710 can_superstructure_writer.add_talonfx("transfer_roller", transfer_roller);
Filip Kujawad75252a2024-02-10 16:54:35 -0800711 can_superstructure_writer.add_talonfx("extend_roller", extend_roller);
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800712 can_superstructure_writer.add_talonfx("retention_roller", retention_roller);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800713
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800714 can_output_event_loop.MakeWatcher(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800715 "/roborio", [&can_drivetrain_writer, &can_superstructure_writer](
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800716 const frc971::CANConfiguration &configuration) {
717 can_drivetrain_writer.HandleCANConfiguration(configuration);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800718 can_superstructure_writer.HandleCANConfiguration(configuration);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800719 });
720
721 AddLoop(&can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800722
Maxwell Hendersonabcc92a2024-02-21 19:06:26 -0800723 ::aos::ShmEventLoop pwm_event_loop(&config.message());
724 SuperstructurePWMWriter superstructure_pwm_writer(&pwm_event_loop);
725 superstructure_pwm_writer.set_catapult_kraken_one(
726 make_unique<frc::TalonFX>(0));
Maxwell Henderson12d427b2024-02-22 13:57:45 -0800727 superstructure_pwm_writer.set_catapult_kraken_two(
Maxwell Hendersonabcc92a2024-02-21 19:06:26 -0800728 make_unique<frc::TalonFX>(1));
729
730 AddLoop(&pwm_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800731 // Thread 6
732
733 RunLoops();
734 }
735};
736
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800737} // namespace y2024::wpilib
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800738
739AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot);