blob: 301ab797fa1703d358c40667d0b9a6f2f726ea32 [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"
Filip Kujawa70183d62024-03-02 17:02:51 -080056#include "y2024/control_loops/superstructure/led_indicator.h"
Niko Sohmers1259b2a2024-01-29 18:00:37 -080057#include "y2024/control_loops/superstructure/superstructure_can_position_static.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080058#include "y2024/control_loops/superstructure/superstructure_output_generated.h"
59#include "y2024/control_loops/superstructure/superstructure_position_generated.h"
Niko Sohmers1259b2a2024-01-29 18:00:37 -080060#include "y2024/control_loops/superstructure/superstructure_position_static.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080061
62DEFINE_bool(ctre_diag_server, false,
63 "If true, enable the diagnostics server for interacting with "
64 "devices on the CAN bus using Phoenix Tuner");
65
66using ::aos::monotonic_clock;
67using ::frc971::CANConfiguration;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080068using ::frc971::control_loops::drivetrain::CANPositionStatic;
69using ::frc971::wpilib::TalonFX;
Niko Sohmers3860f8a2024-01-12 21:05:19 -080070using ::y2024::constants::Values;
71namespace superstructure = ::y2024::control_loops::superstructure;
72namespace drivetrain = ::y2024::control_loops::drivetrain;
73namespace chrono = ::std::chrono;
74using std::make_unique;
75
Stephan Pleinesf63bde82024-01-13 15:59:33 -080076namespace y2024::wpilib {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080077namespace {
78
79constexpr double kMaxBringupPower = 12.0;
80
Filip Kujawa749f2442024-02-04 01:12:35 -080081double climber_pot_translate(double voltage) {
Maxwell Hendersonff831f52024-02-21 13:51:59 -080082 return -1 * voltage * Values::kClimberPotMetersPerVolt();
Filip Kujawa749f2442024-02-04 01:12:35 -080083}
84
Filip Kujawad75252a2024-02-10 16:54:35 -080085double extend_pot_translate(double voltage) {
86 return voltage * Values::kExtendPotMetersPerVolt();
87}
88
Niko Sohmers27d92c62024-02-19 14:15:07 -080089double catapult_pot_translate(double voltage) {
90 return voltage * Values::kCatapultPotRadiansPerVolt();
91}
92
93double turret_pot_translate(double voltage) {
Maxwell Henderson3d68e142024-02-25 09:58:11 -080094 return -1 * voltage * Values::kTurretPotRadiansPerVolt();
Niko Sohmers27d92c62024-02-19 14:15:07 -080095}
96
97double altitude_pot_translate(double voltage) {
Maxwell Henderson0b6fed62024-02-23 11:05:09 -080098 return -1 * voltage * Values::kAltitudePotRadiansPerVolt();
Niko Sohmers27d92c62024-02-19 14:15:07 -080099}
100
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800101double drivetrain_velocity_translate(double in) {
102 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
103 (2.0 * M_PI)) *
104 Values::kDrivetrainEncoderRatio() *
105 control_loops::drivetrain::kWheelRadius;
106}
107
108constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
109 Values::kMaxDrivetrainEncoderPulsesPerSecond(),
Maxwell Henderson1de15492024-02-07 11:09:47 -0800110 Values::kMaxIntakePivotEncoderPulsesPerSecond(),
Filip Kujawa749f2442024-02-04 01:12:35 -0800111 Values::kMaxClimberEncoderPulsesPerSecond(),
Filip Kujawad75252a2024-02-10 16:54:35 -0800112 Values::kMaxExtendEncoderPulsesPerSecond(),
Niko Sohmers27d92c62024-02-19 14:15:07 -0800113 Values::kMaxCatapultEncoderPulsesPerSecond(),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800114});
Niko Sohmers27d92c62024-02-19 14:15:07 -0800115
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800116static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
117 "fast encoders are too fast");
118
119} // namespace
120
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800121// Class to send position messages with sensor readings to our loops.
122class SensorReader : public ::frc971::wpilib::SensorReader {
123 public:
124 SensorReader(::aos::ShmEventLoop *event_loop,
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800125 const Constants *robot_constants)
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800126 : ::frc971::wpilib::SensorReader(event_loop),
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800127 robot_constants_(CHECK_NOTNULL(robot_constants)),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800128 auto_mode_sender_(
129 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
130 "/autonomous")),
131 superstructure_position_sender_(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800132 event_loop->MakeSender<superstructure::PositionStatic>(
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800133 "/superstructure")),
134 drivetrain_position_sender_(
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800135 event_loop->MakeSender<
136 ::frc971::control_loops::drivetrain::PositionStatic>(
137 "/drivetrain")),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800138 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
Maxwell Hendersonafc5c6d2024-02-20 15:34:13 -0800139 "/drivetrain")) {
140 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
141 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0}));
142 };
143 void Start() override {
144 AddToDMA(&imu_yaw_rate_reader_);
145 AddToDMA(&turret_encoder_.reader());
146 AddToDMA(&altitude_encoder_.reader());
147 }
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800148
149 // Auto mode switches.
150 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
151 autonomous_modes_.at(i) = ::std::move(sensor);
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
159 void RunIteration() override {
160 {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800161 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
162 superstructure_position_sender_.MakeStaticBuilder();
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800163
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800164 CopyPosition(intake_pivot_encoder_, builder->add_intake_pivot(),
165 Values::kIntakePivotEncoderCountsPerRevolution(),
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800166 Values::kIntakePivotEncoderRatio(), /* reversed: */ false);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800167
Filip Kujawa749f2442024-02-04 01:12:35 -0800168 CopyPosition(climber_encoder_, builder->add_climber(),
169 Values::kClimberEncoderCountsPerRevolution(),
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800170 Values::kClimberEncoderMetersPerRadian(),
171 climber_pot_translate, false,
Filip Kujawa749f2442024-02-04 01:12:35 -0800172 robot_constants_->robot()
173 ->climber_constants()
174 ->potentiometer_offset());
175
Filip Kujawad75252a2024-02-10 16:54:35 -0800176 CopyPosition(extend_encoder_, builder->add_extend(),
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800177 Values::kExtendEncoderCountsPerRevolution(),
178 Values::kExtendEncoderMetersPerRadian(),
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800179 extend_pot_translate, false,
Filip Kujawad75252a2024-02-10 16:54:35 -0800180 robot_constants_->robot()
181 ->extend_constants()
182 ->potentiometer_offset());
183
Niko Sohmers27d92c62024-02-19 14:15:07 -0800184 CopyPosition(catapult_encoder_, builder->add_catapult(),
185 Values::kCatapultEncoderCountsPerRevolution(),
186 Values::kCatapultEncoderRatio(), catapult_pot_translate,
187 true,
188 robot_constants_->robot()
189 ->catapult_constants()
190 ->potentiometer_offset());
191
192 CopyPosition(turret_encoder_, builder->add_turret(),
193 Values::kTurretEncoderCountsPerRevolution(),
194 Values::kTurretEncoderRatio(), turret_pot_translate, true,
195 robot_constants_->robot()
196 ->turret_constants()
197 ->potentiometer_offset());
198
199 CopyPosition(altitude_encoder_, builder->add_altitude(),
200 Values::kAltitudeEncoderCountsPerRevolution(),
201 Values::kAltitudeEncoderRatio(), altitude_pot_translate,
202 true,
203 robot_constants_->robot()
204 ->altitude_constants()
205 ->potentiometer_offset());
206
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800207 builder->set_transfer_beambreak(transfer_beam_break_->Get());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800208 builder->set_extend_beambreak(extend_beam_break_->Get());
Maxwell Henderson576b0c92024-02-23 17:10:48 -0800209 builder->set_catapult_beambreak(catapult_beam_break_->Get());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800210 builder.CheckOk(builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800211 }
212
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800213 SendDrivetrainPosition(drivetrain_position_sender_.MakeStaticBuilder(),
214 drivetrain_velocity_translate,
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800215 constants::Values::DrivetrainEncoderToMeters, true,
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800216 false);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800217
218 {
219 auto builder = gyro_sender_.MakeBuilder();
220 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
221 builder.MakeBuilder<::frc971::sensors::GyroReading>();
222 // +/- 2000 deg / sec
223 constexpr double kMaxVelocity = 4000; // degrees / second
224 constexpr double kVelocityRadiansPerSecond =
225 kMaxVelocity / 360 * (2.0 * M_PI);
226
227 // Only part of the full range is used to prevent being 100% on or off.
228 constexpr double kScaledRangeLow = 0.1;
229 constexpr double kScaledRangeHigh = 0.9;
230
231 constexpr double kPWMFrequencyHz = 200;
232 double velocity_duty_cycle =
233 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
234
235 constexpr double kDutyCycleScale =
236 1 / (kScaledRangeHigh - kScaledRangeLow);
237 // scale from 0.1 - 0.9 to 0 - 1
238 double rescaled_velocity_duty_cycle =
239 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
240
241 if (!std::isnan(rescaled_velocity_duty_cycle)) {
242 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
243 kVelocityRadiansPerSecond);
244 }
245 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
246 }
247
248 {
249 auto builder = auto_mode_sender_.MakeBuilder();
250
251 uint32_t mode = 0;
252 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
253 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
254 mode |= 1 << i;
255 }
256 }
257
258 auto auto_mode_builder =
259 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
260
261 auto_mode_builder.add_mode(mode);
262
263 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
264 }
265 }
266
Maxwell Henderson62917832024-02-07 11:08:11 -0800267 void set_intake_pivot(::std::unique_ptr<frc::Encoder> encoder,
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800268 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800269 fast_encoder_filter_.Add(encoder.get());
270 intake_pivot_encoder_.set_encoder(::std::move(encoder));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800271 intake_pivot_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800272 }
273
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800274 void set_transfer_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
275 transfer_beam_break_ = ::std::move(sensor);
276 }
277
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800278 void set_extend_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
279 extend_beam_break_ = ::std::move(sensor);
280 }
281
Maxwell Henderson576b0c92024-02-23 17:10:48 -0800282 void set_catapult_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
283 catapult_beam_break_ = ::std::move(sensor);
284 }
285
Filip Kujawa749f2442024-02-04 01:12:35 -0800286 void set_climber(::std::unique_ptr<frc::Encoder> encoder,
287 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
288 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
289 fast_encoder_filter_.Add(encoder.get());
290 climber_encoder_.set_encoder(::std::move(encoder));
291 climber_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
292 climber_encoder_.set_potentiometer(::std::move(potentiometer));
293 }
294
Filip Kujawad75252a2024-02-10 16:54:35 -0800295 void set_extend(::std::unique_ptr<frc::Encoder> encoder,
296 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
297 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
298 fast_encoder_filter_.Add(encoder.get());
299 extend_encoder_.set_encoder(::std::move(encoder));
300 extend_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
301 extend_encoder_.set_potentiometer(::std::move(potentiometer));
302 }
303
Niko Sohmers27d92c62024-02-19 14:15:07 -0800304 void set_catapult(::std::unique_ptr<frc::Encoder> encoder,
305 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
306 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
307 fast_encoder_filter_.Add(encoder.get());
308 catapult_encoder_.set_encoder(::std::move(encoder));
309 catapult_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
310 catapult_encoder_.set_potentiometer(::std::move(potentiometer));
311 }
312
313 void set_turret(::std::unique_ptr<frc::Encoder> encoder,
314 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
315 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
316 fast_encoder_filter_.Add(encoder.get());
317 turret_encoder_.set_encoder(::std::move(encoder));
318 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
319 turret_encoder_.set_potentiometer(::std::move(potentiometer));
320 }
321
322 void set_altitude(::std::unique_ptr<frc::Encoder> encoder,
323 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
324 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
325 fast_encoder_filter_.Add(encoder.get());
326 altitude_encoder_.set_encoder(::std::move(encoder));
327 altitude_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
328 altitude_encoder_.set_potentiometer(::std::move(potentiometer));
329 }
330
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800331 private:
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800332 const Constants *robot_constants_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800333
334 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800335 aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800336 aos::Sender<frc971::control_loops::drivetrain::PositionStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800337 drivetrain_position_sender_;
338 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
339
340 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
341
Maxwell Henderson576b0c92024-02-23 17:10:48 -0800342 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_, transfer_beam_break_,
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800343 extend_beam_break_, catapult_beam_break_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800344
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800345 frc971::wpilib::AbsoluteEncoder intake_pivot_encoder_;
Niko Sohmers27d92c62024-02-19 14:15:07 -0800346 frc971::wpilib::AbsoluteEncoderAndPotentiometer climber_encoder_,
Maxwell Hendersonafc5c6d2024-02-20 15:34:13 -0800347 catapult_encoder_, extend_encoder_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800348
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800349 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
Maxwell Hendersonafc5c6d2024-02-20 15:34:13 -0800350
351 frc971::wpilib::DMAAbsoluteEncoderAndPotentiometer turret_encoder_,
352 altitude_encoder_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800353};
354
Maxwell Hendersonabcc92a2024-02-21 19:06:26 -0800355class SuperstructurePWMWriter
356 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
357 public:
358 SuperstructurePWMWriter(aos::EventLoop *event_loop)
359 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
360 event_loop, "/superstructure") {}
361
362 void set_catapult_kraken_one(::std::unique_ptr<::frc::TalonFX> t) {
363 catapult_kraken_one_ = ::std::move(t);
364 }
365 void set_catapult_kraken_two(::std::unique_ptr<::frc::TalonFX> t) {
366 catapult_kraken_two_ = ::std::move(t);
367 }
368
369 private:
370 void Stop() override {
371 AOS_LOG(WARNING, "Superstructure output too old.\n");
372 catapult_kraken_one_->SetDisabled();
373 catapult_kraken_two_->SetDisabled();
374 }
375
376 void Write(const superstructure::Output &output) override {
377 WritePwm(output.catapult_voltage(), catapult_kraken_one_.get());
Maxwell Henderson12d427b2024-02-22 13:57:45 -0800378 WritePwm(output.catapult_voltage(), catapult_kraken_two_.get());
Maxwell Hendersonabcc92a2024-02-21 19:06:26 -0800379 }
380
381 template <typename T>
382 static void WritePwm(const double voltage, T *motor) {
383 motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) /
384 12.0);
385 }
386 ::std::unique_ptr<::frc::TalonFX> catapult_kraken_one_, catapult_kraken_two_;
387};
388
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800389class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
390 public:
391 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
392 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
393 frc::Encoder::k4X);
394 }
395
396 void Run() override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800397 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
398 aos::configuration::ReadConfig("aos_config.json");
399
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800400 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
401
402 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
403 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(
404 &constant_fetcher_event_loop);
405 const Constants *robot_constants = &constants_fetcher.constants();
406
Maxwell Hendersonca7c9b62024-02-21 13:20:09 -0800407 AddLoop(&constant_fetcher_event_loop);
408
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800409 // Thread 1.
410 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
411 ::frc971::wpilib::JoystickSender joystick_sender(
412 &joystick_sender_event_loop);
413 AddLoop(&joystick_sender_event_loop);
414
415 // Thread 2.
416 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
417 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
418 AddLoop(&pdp_fetcher_event_loop);
419
420 // Thread 3.
421 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800422 SensorReader sensor_reader(&sensor_reader_event_loop, robot_constants);
Austin Schuh65b4f9d2024-03-17 16:03:10 -0700423 sensor_reader.set_pwm_trigger(false);
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800424 sensor_reader.set_drivetrain_left_encoder(
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800425 std::make_unique<frc::Encoder>(8, 9));
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800426 sensor_reader.set_drivetrain_right_encoder(
427 std::make_unique<frc::Encoder>(6, 7));
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800428 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(25));
429 sensor_reader.set_intake_pivot(make_encoder(3),
430 make_unique<frc::DigitalInput>(3));
431 sensor_reader.set_transfer_beambreak(make_unique<frc::DigitalInput>(23));
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800432 sensor_reader.set_extend_beambreak(make_unique<frc::DigitalInput>(24));
433 sensor_reader.set_catapult_beambreak(make_unique<frc::DigitalInput>(22));
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800434
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800435 sensor_reader.set_climber(make_encoder(4),
436 make_unique<frc::DigitalInput>(4),
437 make_unique<frc::AnalogInput>(4));
438 sensor_reader.set_extend(make_encoder(5), make_unique<frc::DigitalInput>(5),
439 make_unique<frc::AnalogInput>(5));
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800440 sensor_reader.set_catapult(make_encoder(0),
441 make_unique<frc::DigitalInput>(0),
442 make_unique<frc::AnalogInput>(0));
443 sensor_reader.set_turret(make_encoder(2), make_unique<frc::DigitalInput>(2),
444 make_unique<frc::AnalogInput>(2));
445 sensor_reader.set_altitude(make_encoder(1),
446 make_unique<frc::DigitalInput>(1),
447 make_unique<frc::AnalogInput>(1));
Filip Kujawad75252a2024-02-10 16:54:35 -0800448
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800449 AddLoop(&sensor_reader_event_loop);
450
451 // Thread 4.
452 // Set up CAN.
453 if (!FLAGS_ctre_diag_server) {
454 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
455 c_Phoenix_Diagnostics_Dispose();
456 }
457
Niko Sohmers84273952024-02-14 18:40:55 -0800458 std::vector<ctre::phoenix6::BaseStatusSignal *> canivore_signal_registry;
459 std::vector<ctre::phoenix6::BaseStatusSignal *> rio_signal_registry;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800460
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800461 const CurrentLimits *current_limits =
462 robot_constants->common()->current_limits();
463
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800464 std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800465 2, true, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800466 current_limits->drivetrain_supply_current_limit(),
467 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800468 std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800469 1, true, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800470 current_limits->drivetrain_supply_current_limit(),
471 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800472 std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800473 4, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800474 current_limits->drivetrain_supply_current_limit(),
475 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800476 std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800477 5, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800478 current_limits->drivetrain_supply_current_limit(),
479 current_limits->drivetrain_stator_current_limit());
480 std::shared_ptr<TalonFX> intake_pivot = std::make_shared<TalonFX>(
Maxwell Henderson30ea8b32024-02-22 16:14:05 -0800481 6, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800482 current_limits->intake_pivot_stator_current_limit(),
483 current_limits->intake_pivot_supply_current_limit());
Niko Sohmers27d92c62024-02-19 14:15:07 -0800484 std::shared_ptr<TalonFX> altitude = std::make_shared<TalonFX>(
Maxwell Henderson30ea8b32024-02-22 16:14:05 -0800485 9, false, "Drivetrain Bus", &canivore_signal_registry,
Niko Sohmers27d92c62024-02-19 14:15:07 -0800486 current_limits->altitude_stator_current_limit(),
487 current_limits->altitude_supply_current_limit());
488 std::shared_ptr<TalonFX> turret = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800489 3, true, "Drivetrain Bus", &canivore_signal_registry,
Niko Sohmers27d92c62024-02-19 14:15:07 -0800490 current_limits->turret_stator_current_limit(),
491 current_limits->turret_supply_current_limit());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800492 std::shared_ptr<TalonFX> climber = std::make_shared<TalonFX>(
493 7, true, "rio", &rio_signal_registry,
494 current_limits->climber_stator_current_limit(),
495 current_limits->climber_supply_current_limit());
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700496 std::shared_ptr<TalonFX> extend =
Niko Sohmers58461f52024-03-20 20:12:10 -0700497 (robot_constants->robot()->disable_extend())
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700498 ? nullptr
499 : std::make_shared<TalonFX>(
500 12, false, "Drivetrain Bus", &canivore_signal_registry,
501 current_limits->extend_stator_current_limit(),
502 current_limits->extend_supply_current_limit());
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800503 std::shared_ptr<TalonFX> intake_roller = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800504 8, false, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800505 current_limits->intake_roller_stator_current_limit(),
506 current_limits->intake_roller_supply_current_limit());
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800507 std::shared_ptr<TalonFX> retention_roller = std::make_shared<TalonFX>(
Maxwell Hendersonefead392024-02-23 19:10:50 -0800508 10, true, "rio", &rio_signal_registry,
Maxwell Hendersonfe735652024-02-23 19:22:31 -0800509 current_limits->retention_roller_stator_current_limit(),
510 current_limits->retention_roller_supply_current_limit());
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800511 std::shared_ptr<TalonFX> transfer_roller = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800512 11, true, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800513 current_limits->transfer_roller_stator_current_limit(),
514 current_limits->transfer_roller_supply_current_limit());
Filip Kujawad75252a2024-02-10 16:54:35 -0800515 std::shared_ptr<TalonFX> extend_roller = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800516 13, true, "rio", &rio_signal_registry,
Filip Kujawad75252a2024-02-10 16:54:35 -0800517 current_limits->extend_roller_stator_current_limit(),
518 current_limits->extend_roller_supply_current_limit());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800519 std::shared_ptr<TalonFX> catapult_one = std::make_shared<TalonFX>(
520 14, false, "Drivetrain Bus", &canivore_signal_registry,
521 current_limits->catapult_stator_current_limit(),
522 current_limits->catapult_supply_current_limit());
523 std::shared_ptr<TalonFX> catapult_two = std::make_shared<TalonFX>(
524 15, false, "Drivetrain Bus", &canivore_signal_registry,
525 current_limits->catapult_stator_current_limit(),
526 current_limits->catapult_supply_current_limit());
Filip Kujawad75252a2024-02-10 16:54:35 -0800527
Stephan Pleines5aabf632024-03-13 20:31:18 -0700528 transfer_roller->set_neutral_mode(
529 ctre::phoenix6::signals::NeutralModeValue::Coast);
530 intake_roller->set_neutral_mode(
531 ctre::phoenix6::signals::NeutralModeValue::Coast);
532
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800533 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
534 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
535 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
Austin Schuh46e0d102024-03-17 16:04:57 -0700536 constants::Values::kDrivetrainWriterPriority, true, "Drivetrain Bus");
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800537
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800538 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
539 can_sensor_reader_event_loop.set_name("CANSensorReader");
540
Niko Sohmers84273952024-02-14 18:40:55 -0800541 ::aos::ShmEventLoop rio_sensor_reader_event_loop(&config.message());
542 rio_sensor_reader_event_loop.set_name("RioSensorReader");
543
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800544 // Creating list of talonfx for CANSensorReader
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800545 std::vector<std::shared_ptr<TalonFX>> drivetrain_talonfxs;
Niko Sohmers84273952024-02-14 18:40:55 -0800546 std::vector<std::shared_ptr<TalonFX>> canivore_talonfxs;
547 std::vector<std::shared_ptr<TalonFX>> rio_talonfxs;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800548
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800549 for (auto talonfx : {right_front, right_back, left_front, left_back}) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800550 drivetrain_talonfxs.push_back(talonfx);
Niko Sohmers84273952024-02-14 18:40:55 -0800551 canivore_talonfxs.push_back(talonfx);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800552 }
553
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800554 for (auto talonfx :
Austin Schuh95764db2024-03-01 19:21:09 -0800555 {intake_pivot, turret, altitude, catapult_one, catapult_two, extend}) {
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700556 if (talonfx != nullptr) {
557 canivore_talonfxs.push_back(talonfx);
558 }
Niko Sohmers84273952024-02-14 18:40:55 -0800559 }
560
Austin Schuh95764db2024-03-01 19:21:09 -0800561 for (auto talonfx : {intake_roller, transfer_roller, climber, extend_roller,
562 retention_roller}) {
Niko Sohmers84273952024-02-14 18:40:55 -0800563 rio_talonfxs.push_back(talonfx);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800564 }
565
566 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>
567 drivetrain_can_position_sender =
568 can_sensor_reader_event_loop.MakeSender<
569 frc971::control_loops::drivetrain::CANPositionStatic>(
570 "/drivetrain");
571
572 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
573 superstructure_can_position_sender =
574 can_sensor_reader_event_loop.MakeSender<
575 y2024::control_loops::superstructure::CANPositionStatic>(
Niko Sohmers84273952024-02-14 18:40:55 -0800576 "/superstructure/canivore");
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800577
Niko Sohmers84273952024-02-14 18:40:55 -0800578 frc971::wpilib::CANSensorReader canivore_can_sensor_reader(
579 &can_sensor_reader_event_loop, std::move(canivore_signal_registry),
580 canivore_talonfxs,
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800581 [drivetrain_talonfxs, &intake_pivot, &turret, &altitude, &catapult_one,
582 &catapult_two, &drivetrain_can_position_sender,
Austin Schuh95764db2024-03-01 19:21:09 -0800583 &superstructure_can_position_sender,
584 &extend](ctre::phoenix::StatusCode status) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800585 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>::
586 StaticBuilder drivetrain_can_builder =
587 drivetrain_can_position_sender.MakeStaticBuilder();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800588
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800589 auto drivetrain_falcon_vector =
Maxwell Henderson563efed2024-02-17 21:11:33 -0800590 CHECK_NOTNULL(drivetrain_can_builder->add_talonfxs());
Maxwell Henderson9116e5b2024-01-21 12:14:26 -0800591
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800592 for (auto talonfx : drivetrain_talonfxs) {
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800593 talonfx->SerializePosition(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800594 drivetrain_falcon_vector->emplace_back(),
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800595 control_loops::drivetrain::kHighOutputRatio);
596 }
597
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800598 drivetrain_can_builder->set_status(static_cast<int>(status));
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800599
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800600 drivetrain_can_builder.CheckOk(drivetrain_can_builder.Send());
601
602 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
603 StaticBuilder superstructure_can_builder =
604 superstructure_can_position_sender.MakeStaticBuilder();
605
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800606 intake_pivot->SerializePosition(
607 superstructure_can_builder->add_intake_pivot(),
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800608 control_loops::superstructure::intake_pivot::kOutputRatio);
Niko Sohmers27d92c62024-02-19 14:15:07 -0800609 turret->SerializePosition(
610 superstructure_can_builder->add_turret(),
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800611 control_loops::superstructure::turret::kOutputRatio);
612 altitude->SerializePosition(
613 superstructure_can_builder->add_altitude(),
614 control_loops::superstructure::altitude::kOutputRatio);
615 catapult_one->SerializePosition(
616 superstructure_can_builder->add_catapult_one(),
617 control_loops::superstructure::catapult::kOutputRatio);
618 catapult_two->SerializePosition(
619 superstructure_can_builder->add_catapult_two(),
620 control_loops::superstructure::catapult::kOutputRatio);
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700621 if (extend != nullptr) {
622 extend->SerializePosition(superstructure_can_builder->add_extend(),
623 superstructure::extend::kOutputRatio);
624 }
Niko Sohmers84273952024-02-14 18:40:55 -0800625
Niko Sohmers84273952024-02-14 18:40:55 -0800626 superstructure_can_builder->set_status(static_cast<int>(status));
627 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
628 });
629
630 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
631 superstructure_rio_position_sender =
632 rio_sensor_reader_event_loop.MakeSender<
633 y2024::control_loops::superstructure::CANPositionStatic>(
634 "/superstructure/rio");
635
636 frc971::wpilib::CANSensorReader rio_can_sensor_reader(
637 &rio_sensor_reader_event_loop, std::move(rio_signal_registry),
638 rio_talonfxs,
Austin Schuh95764db2024-03-01 19:21:09 -0800639 [&intake_roller, &transfer_roller, &climber, &extend_roller,
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800640 &retention_roller, &superstructure_rio_position_sender](
Niko Sohmers84273952024-02-14 18:40:55 -0800641 ctre::phoenix::StatusCode status) {
642 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
643 StaticBuilder superstructure_can_builder =
644 superstructure_rio_position_sender.MakeStaticBuilder();
645
646 intake_roller->SerializePosition(
647 superstructure_can_builder->add_intake_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800648 constants::Values::kIntakeRollerOutputRatio);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800649 transfer_roller->SerializePosition(
650 superstructure_can_builder->add_transfer_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800651 constants::Values::kIntakeRollerOutputRatio);
652 climber->SerializePosition(superstructure_can_builder->add_climber(),
653 superstructure::climber::kOutputRatio);
Filip Kujawad75252a2024-02-10 16:54:35 -0800654 extend_roller->SerializePosition(
655 superstructure_can_builder->add_extend_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800656 constants::Values::kExtendRollerOutputRatio);
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800657 retention_roller->SerializePosition(
658 superstructure_can_builder->add_retention_roller(), 1.0);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800659
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800660 superstructure_can_builder->set_status(static_cast<int>(status));
661 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800662 },
663 frc971::wpilib::CANSensorReader::SignalSync::kNoSync);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800664
665 AddLoop(&can_sensor_reader_event_loop);
Niko Sohmers84273952024-02-14 18:40:55 -0800666 AddLoop(&rio_sensor_reader_event_loop);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800667
668 // Thread 5.
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800669 ::aos::ShmEventLoop can_output_event_loop(&config.message());
670 can_output_event_loop.set_name("CANOutputWriter");
671
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800672 frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer(
673 &can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800674
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800675 frc971::wpilib::GenericCANWriter<control_loops::superstructure::Output>
676 can_superstructure_writer(
677 &can_output_event_loop,
678 [](const control_loops::superstructure::Output &output,
679 const std::map<std::string_view, std::shared_ptr<TalonFX>>
680 &talonfx_map) {
681 talonfx_map.find("intake_pivot")
682 ->second->WriteVoltage(output.intake_pivot_voltage());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800683 talonfx_map.find("altitude")
684 ->second->WriteVoltage(output.altitude_voltage());
685 talonfx_map.find("catapult_one")
686 ->second->WriteVoltage(output.catapult_voltage());
687 talonfx_map.find("catapult_two")
688 ->second->WriteVoltage(output.catapult_voltage());
689 talonfx_map.find("turret")->second->WriteVoltage(
690 output.turret_voltage());
Filip Kujawa749f2442024-02-04 01:12:35 -0800691 talonfx_map.find("climber")->second->WriteVoltage(
692 output.climber_voltage());
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700693 if (talonfx_map.find("extend") != talonfx_map.end()) {
694 talonfx_map.find("extend")->second->WriteVoltage(
695 output.extend_voltage());
696 }
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800697 talonfx_map.find("intake_roller")
698 ->second->WriteVoltage(output.intake_roller_voltage());
699 talonfx_map.find("transfer_roller")
700 ->second->WriteVoltage(output.transfer_roller_voltage());
Filip Kujawad75252a2024-02-10 16:54:35 -0800701 talonfx_map.find("extend_roller")
702 ->second->WriteVoltage(output.extend_roller_voltage());
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800703 talonfx_map.find("retention_roller")
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800704 ->second->WriteCurrent(
705 output.retention_roller_stator_current_limit(),
706 output.retention_roller_voltage());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800707 });
708
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800709 can_drivetrain_writer.set_talonfxs({right_front, right_back},
710 {left_front, left_back});
711
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800712 can_superstructure_writer.add_talonfx("intake_pivot", intake_pivot);
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800713 can_superstructure_writer.add_talonfx("altitude", altitude);
714 can_superstructure_writer.add_talonfx("catapult_one", catapult_one);
715 can_superstructure_writer.add_talonfx("catapult_two", catapult_two);
716 can_superstructure_writer.add_talonfx("turret", turret);
Filip Kujawa749f2442024-02-04 01:12:35 -0800717 can_superstructure_writer.add_talonfx("climber", climber);
Niko Sohmers58461f52024-03-20 20:12:10 -0700718 if (!robot_constants->robot()->disable_extend()) {
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700719 can_superstructure_writer.add_talonfx("extend", extend);
720 }
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800721 can_superstructure_writer.add_talonfx("intake_roller", intake_roller);
722 can_superstructure_writer.add_talonfx("transfer_roller", transfer_roller);
Filip Kujawad75252a2024-02-10 16:54:35 -0800723 can_superstructure_writer.add_talonfx("extend_roller", extend_roller);
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800724 can_superstructure_writer.add_talonfx("retention_roller", retention_roller);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800725
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800726 can_output_event_loop.MakeWatcher(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800727 "/roborio", [&can_drivetrain_writer, &can_superstructure_writer](
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800728 const frc971::CANConfiguration &configuration) {
729 can_drivetrain_writer.HandleCANConfiguration(configuration);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800730 can_superstructure_writer.HandleCANConfiguration(configuration);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800731 });
732
733 AddLoop(&can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800734
Filip Kujawa70183d62024-03-02 17:02:51 -0800735 // Thread 6
736 // Setup led_indicator
737 ::aos::ShmEventLoop led_indicator_event_loop(&config.message());
738 led_indicator_event_loop.set_name("LedIndicator");
739 control_loops::superstructure::LedIndicator led_indicator(
740 &led_indicator_event_loop);
741 AddLoop(&led_indicator_event_loop);
742
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800743 RunLoops();
744 }
745};
746
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800747} // namespace y2024::wpilib
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800748
749AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot);