blob: 30d15498fcc033a8286eecbeab6b83f8dda8721c [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) {
Niko Sohmers4d93d4c2024-03-24 14:48:26 -070082 return 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 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_);
Maxwell Henderson3c00b7d2024-03-21 21:17:51 -0700144 if (aos::network::GetTeamNumber() != 9971) {
145 AddToDMA(&turret_encoder_.reader());
146 AddToDMA(&altitude_encoder_.reader());
147 }
Maxwell Hendersonafc5c6d2024-02-20 15:34:13 -0800148 }
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800149
150 // Auto mode switches.
151 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
152 autonomous_modes_.at(i) = ::std::move(sensor);
153 }
154
155 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
156 imu_yaw_rate_input_ = ::std::move(sensor);
157 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
158 }
159
160 void RunIteration() override {
Maxwell Henderson3c00b7d2024-03-21 21:17:51 -0700161 if (aos::network::GetTeamNumber() != 9971) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800162 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
163 superstructure_position_sender_.MakeStaticBuilder();
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800164
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800165 CopyPosition(intake_pivot_encoder_, builder->add_intake_pivot(),
166 Values::kIntakePivotEncoderCountsPerRevolution(),
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800167 Values::kIntakePivotEncoderRatio(), /* reversed: */ false);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800168
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700169 CopyPosition(*climber_potentiometer_, builder->add_climber(),
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800170 climber_pot_translate, false,
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700171 robot_constants_->robot()->climber_potentiometer_offset());
Filip Kujawa749f2442024-02-04 01:12:35 -0800172
Filip Kujawad75252a2024-02-10 16:54:35 -0800173 CopyPosition(extend_encoder_, builder->add_extend(),
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800174 Values::kExtendEncoderCountsPerRevolution(),
175 Values::kExtendEncoderMetersPerRadian(),
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800176 extend_pot_translate, false,
Filip Kujawad75252a2024-02-10 16:54:35 -0800177 robot_constants_->robot()
178 ->extend_constants()
179 ->potentiometer_offset());
180
Niko Sohmers27d92c62024-02-19 14:15:07 -0800181 CopyPosition(catapult_encoder_, builder->add_catapult(),
182 Values::kCatapultEncoderCountsPerRevolution(),
183 Values::kCatapultEncoderRatio(), catapult_pot_translate,
184 true,
185 robot_constants_->robot()
186 ->catapult_constants()
187 ->potentiometer_offset());
188
189 CopyPosition(turret_encoder_, builder->add_turret(),
190 Values::kTurretEncoderCountsPerRevolution(),
191 Values::kTurretEncoderRatio(), turret_pot_translate, true,
192 robot_constants_->robot()
193 ->turret_constants()
194 ->potentiometer_offset());
195
196 CopyPosition(altitude_encoder_, builder->add_altitude(),
197 Values::kAltitudeEncoderCountsPerRevolution(),
198 Values::kAltitudeEncoderRatio(), altitude_pot_translate,
199 true,
200 robot_constants_->robot()
201 ->altitude_constants()
202 ->potentiometer_offset());
203
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800204 builder->set_transfer_beambreak(transfer_beam_break_->Get());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800205 builder->set_extend_beambreak(extend_beam_break_->Get());
Maxwell Henderson576b0c92024-02-23 17:10:48 -0800206 builder->set_catapult_beambreak(catapult_beam_break_->Get());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800207 builder.CheckOk(builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800208 }
209
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800210 SendDrivetrainPosition(drivetrain_position_sender_.MakeStaticBuilder(),
211 drivetrain_velocity_translate,
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800212 constants::Values::DrivetrainEncoderToMeters, true,
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800213 false);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800214
215 {
216 auto builder = gyro_sender_.MakeBuilder();
217 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
218 builder.MakeBuilder<::frc971::sensors::GyroReading>();
219 // +/- 2000 deg / sec
220 constexpr double kMaxVelocity = 4000; // degrees / second
221 constexpr double kVelocityRadiansPerSecond =
222 kMaxVelocity / 360 * (2.0 * M_PI);
223
224 // Only part of the full range is used to prevent being 100% on or off.
225 constexpr double kScaledRangeLow = 0.1;
226 constexpr double kScaledRangeHigh = 0.9;
227
228 constexpr double kPWMFrequencyHz = 200;
229 double velocity_duty_cycle =
230 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
231
232 constexpr double kDutyCycleScale =
233 1 / (kScaledRangeHigh - kScaledRangeLow);
234 // scale from 0.1 - 0.9 to 0 - 1
235 double rescaled_velocity_duty_cycle =
236 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
237
238 if (!std::isnan(rescaled_velocity_duty_cycle)) {
239 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
240 kVelocityRadiansPerSecond);
241 }
242 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
243 }
244
245 {
246 auto builder = auto_mode_sender_.MakeBuilder();
247
248 uint32_t mode = 0;
249 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
250 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
251 mode |= 1 << i;
252 }
253 }
254
255 auto auto_mode_builder =
256 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
257
258 auto_mode_builder.add_mode(mode);
259
260 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
261 }
262 }
263
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700264 void set_climber_potentiometer(
265 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
266 climber_potentiometer_ = ::std::move(potentiometer);
267 }
268
Maxwell Henderson62917832024-02-07 11:08:11 -0800269 void set_intake_pivot(::std::unique_ptr<frc::Encoder> encoder,
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800270 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800271 fast_encoder_filter_.Add(encoder.get());
272 intake_pivot_encoder_.set_encoder(::std::move(encoder));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800273 intake_pivot_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800274 }
275
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800276 void set_transfer_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
277 transfer_beam_break_ = ::std::move(sensor);
278 }
279
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800280 void set_extend_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
281 extend_beam_break_ = ::std::move(sensor);
282 }
283
Maxwell Henderson576b0c92024-02-23 17:10:48 -0800284 void set_catapult_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
285 catapult_beam_break_ = ::std::move(sensor);
286 }
287
Filip Kujawad75252a2024-02-10 16:54:35 -0800288 void set_extend(::std::unique_ptr<frc::Encoder> encoder,
289 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
290 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
291 fast_encoder_filter_.Add(encoder.get());
292 extend_encoder_.set_encoder(::std::move(encoder));
293 extend_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
294 extend_encoder_.set_potentiometer(::std::move(potentiometer));
295 }
296
Niko Sohmers27d92c62024-02-19 14:15:07 -0800297 void set_catapult(::std::unique_ptr<frc::Encoder> encoder,
298 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
299 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
300 fast_encoder_filter_.Add(encoder.get());
301 catapult_encoder_.set_encoder(::std::move(encoder));
302 catapult_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
303 catapult_encoder_.set_potentiometer(::std::move(potentiometer));
304 }
305
306 void set_turret(::std::unique_ptr<frc::Encoder> encoder,
307 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
308 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
309 fast_encoder_filter_.Add(encoder.get());
310 turret_encoder_.set_encoder(::std::move(encoder));
311 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
312 turret_encoder_.set_potentiometer(::std::move(potentiometer));
313 }
314
315 void set_altitude(::std::unique_ptr<frc::Encoder> encoder,
316 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
317 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
318 fast_encoder_filter_.Add(encoder.get());
319 altitude_encoder_.set_encoder(::std::move(encoder));
320 altitude_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
321 altitude_encoder_.set_potentiometer(::std::move(potentiometer));
322 }
323
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800324 private:
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800325 const Constants *robot_constants_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800326
327 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800328 aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800329 aos::Sender<frc971::control_loops::drivetrain::PositionStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800330 drivetrain_position_sender_;
331 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
332
333 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
334
Maxwell Henderson576b0c92024-02-23 17:10:48 -0800335 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_, transfer_beam_break_,
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800336 extend_beam_break_, catapult_beam_break_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800337
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700338 std::unique_ptr<frc::AnalogInput> climber_potentiometer_;
339
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800340 frc971::wpilib::AbsoluteEncoder intake_pivot_encoder_;
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700341 frc971::wpilib::AbsoluteEncoderAndPotentiometer catapult_encoder_,
342 extend_encoder_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800343
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800344 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
Maxwell Hendersonafc5c6d2024-02-20 15:34:13 -0800345
346 frc971::wpilib::DMAAbsoluteEncoderAndPotentiometer turret_encoder_,
347 altitude_encoder_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800348};
349
Maxwell Hendersonabcc92a2024-02-21 19:06:26 -0800350class SuperstructurePWMWriter
351 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
352 public:
353 SuperstructurePWMWriter(aos::EventLoop *event_loop)
354 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
355 event_loop, "/superstructure") {}
356
357 void set_catapult_kraken_one(::std::unique_ptr<::frc::TalonFX> t) {
358 catapult_kraken_one_ = ::std::move(t);
359 }
360 void set_catapult_kraken_two(::std::unique_ptr<::frc::TalonFX> t) {
361 catapult_kraken_two_ = ::std::move(t);
362 }
363
364 private:
365 void Stop() override {
366 AOS_LOG(WARNING, "Superstructure output too old.\n");
367 catapult_kraken_one_->SetDisabled();
368 catapult_kraken_two_->SetDisabled();
369 }
370
371 void Write(const superstructure::Output &output) override {
372 WritePwm(output.catapult_voltage(), catapult_kraken_one_.get());
Maxwell Henderson12d427b2024-02-22 13:57:45 -0800373 WritePwm(output.catapult_voltage(), catapult_kraken_two_.get());
Maxwell Hendersonabcc92a2024-02-21 19:06:26 -0800374 }
375
376 template <typename T>
377 static void WritePwm(const double voltage, T *motor) {
378 motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) /
379 12.0);
380 }
381 ::std::unique_ptr<::frc::TalonFX> catapult_kraken_one_, catapult_kraken_two_;
382};
383
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800384class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
385 public:
386 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
387 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
388 frc::Encoder::k4X);
389 }
390
391 void Run() override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800392 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
393 aos::configuration::ReadConfig("aos_config.json");
394
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800395 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
396
Maxwell Henderson3c00b7d2024-03-21 21:17:51 -0700397 const int team_number = aos::network::GetTeamNumber();
398
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800399 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
400 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(
401 &constant_fetcher_event_loop);
402 const Constants *robot_constants = &constants_fetcher.constants();
403
Maxwell Hendersonca7c9b62024-02-21 13:20:09 -0800404 AddLoop(&constant_fetcher_event_loop);
405
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800406 // Thread 1.
407 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
408 ::frc971::wpilib::JoystickSender joystick_sender(
409 &joystick_sender_event_loop);
410 AddLoop(&joystick_sender_event_loop);
411
412 // Thread 2.
413 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
414 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
415 AddLoop(&pdp_fetcher_event_loop);
416
417 // Thread 3.
418 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800419 SensorReader sensor_reader(&sensor_reader_event_loop, robot_constants);
Austin Schuh65b4f9d2024-03-17 16:03:10 -0700420 sensor_reader.set_pwm_trigger(false);
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800421 sensor_reader.set_drivetrain_left_encoder(
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800422 std::make_unique<frc::Encoder>(8, 9));
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800423 sensor_reader.set_drivetrain_right_encoder(
424 std::make_unique<frc::Encoder>(6, 7));
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800425 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(25));
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800426
Maxwell Henderson3c00b7d2024-03-21 21:17:51 -0700427 if (team_number != 9971) {
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));
431 sensor_reader.set_extend_beambreak(make_unique<frc::DigitalInput>(24));
432 sensor_reader.set_catapult_beambreak(make_unique<frc::DigitalInput>(22));
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700433 sensor_reader.set_climber_potentiometer(make_unique<frc::AnalogInput>(4));
Maxwell Henderson3c00b7d2024-03-21 21:17:51 -0700434 sensor_reader.set_extend(make_encoder(5),
435 make_unique<frc::DigitalInput>(5),
436 make_unique<frc::AnalogInput>(5));
437 sensor_reader.set_catapult(make_encoder(0),
438 make_unique<frc::DigitalInput>(0),
439 make_unique<frc::AnalogInput>(0));
440 sensor_reader.set_turret(make_encoder(2),
441 make_unique<frc::DigitalInput>(2),
442 make_unique<frc::AnalogInput>(2));
443 sensor_reader.set_altitude(make_encoder(1),
444 make_unique<frc::DigitalInput>(1),
445 make_unique<frc::AnalogInput>(1));
446 }
Filip Kujawad75252a2024-02-10 16:54:35 -0800447
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800448 AddLoop(&sensor_reader_event_loop);
449
Maxwell Henderson3c00b7d2024-03-21 21:17:51 -0700450 if (team_number == 9971) {
451 std::vector<ctre::phoenix6::BaseStatusSignal *> signal_registry;
452
453 if (!FLAGS_ctre_diag_server) {
454 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
455 c_Phoenix_Diagnostics_Dispose();
456 }
457
458 const CurrentLimits *current_limits =
459 robot_constants->common()->current_limits();
460
461 std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>(
462 2, true, "Drivetrain Bus", &signal_registry,
463 current_limits->drivetrain_supply_current_limit(),
464 current_limits->drivetrain_stator_current_limit());
465 std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>(
466 1, true, "Drivetrain Bus", &signal_registry,
467 current_limits->drivetrain_supply_current_limit(),
468 current_limits->drivetrain_stator_current_limit());
469 std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>(
470 4, false, "Drivetrain Bus", &signal_registry,
471 current_limits->drivetrain_supply_current_limit(),
472 current_limits->drivetrain_stator_current_limit());
473 std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>(
474 5, false, "Drivetrain Bus", &signal_registry,
475 current_limits->drivetrain_supply_current_limit(),
476 current_limits->drivetrain_stator_current_limit());
477
478 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
479 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
480 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
481 constants::Values::kDrivetrainWriterPriority, true, "Drivetrain Bus");
482
483 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
484 can_sensor_reader_event_loop.set_name("CANSensorReader");
485
486 std::vector<std::shared_ptr<TalonFX>> drivetrain_krakens;
487
488 for (auto talonfx : {right_front, right_back, left_front, left_back}) {
489 drivetrain_krakens.push_back(talonfx);
490 }
491
492 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>
493 drivetrain_can_position_sender =
494 can_sensor_reader_event_loop.MakeSender<
495 frc971::control_loops::drivetrain::CANPositionStatic>(
496 "/drivetrain");
497
498 frc971::wpilib::CANSensorReader can_sensor_reader(
499 &can_sensor_reader_event_loop, std::move(signal_registry),
500 drivetrain_krakens,
501 [drivetrain_krakens,
502 &drivetrain_can_position_sender](ctre::phoenix::StatusCode status) {
503 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>::
504 StaticBuilder drivetrain_can_builder =
505 drivetrain_can_position_sender.MakeStaticBuilder();
506
507 auto drivetrain_falcon_vector =
508 CHECK_NOTNULL(drivetrain_can_builder->add_talonfxs());
509
510 for (auto talonfx : drivetrain_krakens) {
511 talonfx->SerializePosition(
512 drivetrain_falcon_vector->emplace_back(),
513 control_loops::drivetrain::kHighOutputRatio);
514 }
515
516 drivetrain_can_builder->set_status(static_cast<int>(status));
517
518 drivetrain_can_builder.CheckOk(drivetrain_can_builder.Send());
519 });
520
521 AddLoop(&can_sensor_reader_event_loop);
522
523 ::aos::ShmEventLoop can_output_event_loop(&config.message());
524 can_output_event_loop.set_name("CANOutputWriter");
525
526 frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer(
527 &can_output_event_loop);
528
529 can_drivetrain_writer.set_talonfxs({right_front, right_back},
530 {left_front, left_back});
531 can_output_event_loop.MakeWatcher(
532 "/roborio", [&can_drivetrain_writer](
533 const frc971::CANConfiguration &configuration) {
534 can_drivetrain_writer.HandleCANConfiguration(configuration);
535 });
536
537 AddLoop(&can_output_event_loop);
538
539 RunLoops();
540 return;
541 }
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800542 // Thread 4.
543 // Set up CAN.
544 if (!FLAGS_ctre_diag_server) {
545 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
546 c_Phoenix_Diagnostics_Dispose();
547 }
548
Niko Sohmers84273952024-02-14 18:40:55 -0800549 std::vector<ctre::phoenix6::BaseStatusSignal *> canivore_signal_registry;
550 std::vector<ctre::phoenix6::BaseStatusSignal *> rio_signal_registry;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800551
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800552 const CurrentLimits *current_limits =
553 robot_constants->common()->current_limits();
554
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800555 std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800556 2, true, "Drivetrain Bus", &canivore_signal_registry,
James Kuszmaulafab1c82024-05-04 14:24:24 -0700557 current_limits->drivetrain_stator_current_limit(),
558 current_limits->drivetrain_supply_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800559 std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800560 1, true, "Drivetrain Bus", &canivore_signal_registry,
James Kuszmaulafab1c82024-05-04 14:24:24 -0700561 current_limits->drivetrain_stator_current_limit(),
562 current_limits->drivetrain_supply_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800563 std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800564 4, false, "Drivetrain Bus", &canivore_signal_registry,
James Kuszmaulafab1c82024-05-04 14:24:24 -0700565 current_limits->drivetrain_stator_current_limit(),
566 current_limits->drivetrain_supply_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800567 std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800568 5, false, "Drivetrain Bus", &canivore_signal_registry,
James Kuszmaulafab1c82024-05-04 14:24:24 -0700569 current_limits->drivetrain_stator_current_limit(),
570 current_limits->drivetrain_supply_current_limit());
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800571 std::shared_ptr<TalonFX> intake_pivot = std::make_shared<TalonFX>(
Maxwell Henderson30ea8b32024-02-22 16:14:05 -0800572 6, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800573 current_limits->intake_pivot_stator_current_limit(),
574 current_limits->intake_pivot_supply_current_limit());
Niko Sohmers27d92c62024-02-19 14:15:07 -0800575 std::shared_ptr<TalonFX> altitude = std::make_shared<TalonFX>(
Maxwell Henderson30ea8b32024-02-22 16:14:05 -0800576 9, false, "Drivetrain Bus", &canivore_signal_registry,
Niko Sohmers27d92c62024-02-19 14:15:07 -0800577 current_limits->altitude_stator_current_limit(),
578 current_limits->altitude_supply_current_limit());
579 std::shared_ptr<TalonFX> turret = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800580 3, true, "Drivetrain Bus", &canivore_signal_registry,
Niko Sohmers27d92c62024-02-19 14:15:07 -0800581 current_limits->turret_stator_current_limit(),
582 current_limits->turret_supply_current_limit());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800583 std::shared_ptr<TalonFX> climber = std::make_shared<TalonFX>(
James Kuszmaul42a87452024-04-05 17:30:47 -0700584 7, false, "rio", &rio_signal_registry,
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800585 current_limits->climber_stator_current_limit(),
586 current_limits->climber_supply_current_limit());
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700587 std::shared_ptr<TalonFX> extend =
Niko Sohmers58461f52024-03-20 20:12:10 -0700588 (robot_constants->robot()->disable_extend())
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700589 ? nullptr
590 : std::make_shared<TalonFX>(
591 12, false, "Drivetrain Bus", &canivore_signal_registry,
592 current_limits->extend_stator_current_limit(),
593 current_limits->extend_supply_current_limit());
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800594 std::shared_ptr<TalonFX> intake_roller = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800595 8, false, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800596 current_limits->intake_roller_stator_current_limit(),
597 current_limits->intake_roller_supply_current_limit());
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800598 std::shared_ptr<TalonFX> retention_roller = std::make_shared<TalonFX>(
Maxwell Hendersonefead392024-02-23 19:10:50 -0800599 10, true, "rio", &rio_signal_registry,
Maxwell Hendersonfe735652024-02-23 19:22:31 -0800600 current_limits->retention_roller_stator_current_limit(),
601 current_limits->retention_roller_supply_current_limit());
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800602 std::shared_ptr<TalonFX> transfer_roller = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800603 11, true, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800604 current_limits->transfer_roller_stator_current_limit(),
605 current_limits->transfer_roller_supply_current_limit());
Filip Kujawad75252a2024-02-10 16:54:35 -0800606 std::shared_ptr<TalonFX> extend_roller = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800607 13, true, "rio", &rio_signal_registry,
Filip Kujawad75252a2024-02-10 16:54:35 -0800608 current_limits->extend_roller_stator_current_limit(),
609 current_limits->extend_roller_supply_current_limit());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800610 std::shared_ptr<TalonFX> catapult_one = std::make_shared<TalonFX>(
611 14, false, "Drivetrain Bus", &canivore_signal_registry,
612 current_limits->catapult_stator_current_limit(),
613 current_limits->catapult_supply_current_limit());
614 std::shared_ptr<TalonFX> catapult_two = std::make_shared<TalonFX>(
615 15, false, "Drivetrain Bus", &canivore_signal_registry,
616 current_limits->catapult_stator_current_limit(),
617 current_limits->catapult_supply_current_limit());
Filip Kujawad75252a2024-02-10 16:54:35 -0800618
Stephan Pleines5aabf632024-03-13 20:31:18 -0700619 transfer_roller->set_neutral_mode(
620 ctre::phoenix6::signals::NeutralModeValue::Coast);
621 intake_roller->set_neutral_mode(
622 ctre::phoenix6::signals::NeutralModeValue::Coast);
623
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800624 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
625 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
626 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
Austin Schuh46e0d102024-03-17 16:04:57 -0700627 constants::Values::kDrivetrainWriterPriority, true, "Drivetrain Bus");
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800628
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800629 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
630 can_sensor_reader_event_loop.set_name("CANSensorReader");
631
Niko Sohmers84273952024-02-14 18:40:55 -0800632 ::aos::ShmEventLoop rio_sensor_reader_event_loop(&config.message());
633 rio_sensor_reader_event_loop.set_name("RioSensorReader");
634
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800635 // Creating list of talonfx for CANSensorReader
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800636 std::vector<std::shared_ptr<TalonFX>> drivetrain_talonfxs;
Niko Sohmers84273952024-02-14 18:40:55 -0800637 std::vector<std::shared_ptr<TalonFX>> canivore_talonfxs;
638 std::vector<std::shared_ptr<TalonFX>> rio_talonfxs;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800639
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800640 for (auto talonfx : {right_front, right_back, left_front, left_back}) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800641 drivetrain_talonfxs.push_back(talonfx);
Niko Sohmers84273952024-02-14 18:40:55 -0800642 canivore_talonfxs.push_back(talonfx);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800643 }
644
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800645 for (auto talonfx :
Austin Schuh95764db2024-03-01 19:21:09 -0800646 {intake_pivot, turret, altitude, catapult_one, catapult_two, extend}) {
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700647 if (talonfx != nullptr) {
648 canivore_talonfxs.push_back(talonfx);
649 }
Niko Sohmers84273952024-02-14 18:40:55 -0800650 }
651
Austin Schuh95764db2024-03-01 19:21:09 -0800652 for (auto talonfx : {intake_roller, transfer_roller, climber, extend_roller,
653 retention_roller}) {
Niko Sohmers84273952024-02-14 18:40:55 -0800654 rio_talonfxs.push_back(talonfx);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800655 }
656
657 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>
658 drivetrain_can_position_sender =
659 can_sensor_reader_event_loop.MakeSender<
660 frc971::control_loops::drivetrain::CANPositionStatic>(
661 "/drivetrain");
662
663 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
664 superstructure_can_position_sender =
665 can_sensor_reader_event_loop.MakeSender<
666 y2024::control_loops::superstructure::CANPositionStatic>(
Niko Sohmers84273952024-02-14 18:40:55 -0800667 "/superstructure/canivore");
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800668
Niko Sohmers84273952024-02-14 18:40:55 -0800669 frc971::wpilib::CANSensorReader canivore_can_sensor_reader(
670 &can_sensor_reader_event_loop, std::move(canivore_signal_registry),
671 canivore_talonfxs,
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800672 [drivetrain_talonfxs, &intake_pivot, &turret, &altitude, &catapult_one,
673 &catapult_two, &drivetrain_can_position_sender,
Austin Schuh95764db2024-03-01 19:21:09 -0800674 &superstructure_can_position_sender,
675 &extend](ctre::phoenix::StatusCode status) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800676 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>::
677 StaticBuilder drivetrain_can_builder =
678 drivetrain_can_position_sender.MakeStaticBuilder();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800679
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800680 auto drivetrain_falcon_vector =
Maxwell Henderson563efed2024-02-17 21:11:33 -0800681 CHECK_NOTNULL(drivetrain_can_builder->add_talonfxs());
Maxwell Henderson9116e5b2024-01-21 12:14:26 -0800682
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800683 for (auto talonfx : drivetrain_talonfxs) {
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800684 talonfx->SerializePosition(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800685 drivetrain_falcon_vector->emplace_back(),
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800686 control_loops::drivetrain::kHighOutputRatio);
687 }
688
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800689 drivetrain_can_builder->set_status(static_cast<int>(status));
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800690
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800691 drivetrain_can_builder.CheckOk(drivetrain_can_builder.Send());
692
693 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
694 StaticBuilder superstructure_can_builder =
695 superstructure_can_position_sender.MakeStaticBuilder();
696
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800697 intake_pivot->SerializePosition(
698 superstructure_can_builder->add_intake_pivot(),
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800699 control_loops::superstructure::intake_pivot::kOutputRatio);
Niko Sohmers27d92c62024-02-19 14:15:07 -0800700 turret->SerializePosition(
701 superstructure_can_builder->add_turret(),
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800702 control_loops::superstructure::turret::kOutputRatio);
703 altitude->SerializePosition(
704 superstructure_can_builder->add_altitude(),
705 control_loops::superstructure::altitude::kOutputRatio);
706 catapult_one->SerializePosition(
707 superstructure_can_builder->add_catapult_one(),
708 control_loops::superstructure::catapult::kOutputRatio);
709 catapult_two->SerializePosition(
710 superstructure_can_builder->add_catapult_two(),
711 control_loops::superstructure::catapult::kOutputRatio);
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700712 if (extend != nullptr) {
713 extend->SerializePosition(superstructure_can_builder->add_extend(),
714 superstructure::extend::kOutputRatio);
715 }
Niko Sohmers84273952024-02-14 18:40:55 -0800716
Niko Sohmers84273952024-02-14 18:40:55 -0800717 superstructure_can_builder->set_status(static_cast<int>(status));
718 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
719 });
720
721 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
722 superstructure_rio_position_sender =
723 rio_sensor_reader_event_loop.MakeSender<
724 y2024::control_loops::superstructure::CANPositionStatic>(
725 "/superstructure/rio");
726
727 frc971::wpilib::CANSensorReader rio_can_sensor_reader(
728 &rio_sensor_reader_event_loop, std::move(rio_signal_registry),
729 rio_talonfxs,
Austin Schuh95764db2024-03-01 19:21:09 -0800730 [&intake_roller, &transfer_roller, &climber, &extend_roller,
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800731 &retention_roller, &superstructure_rio_position_sender](
Niko Sohmers84273952024-02-14 18:40:55 -0800732 ctre::phoenix::StatusCode status) {
733 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
734 StaticBuilder superstructure_can_builder =
735 superstructure_rio_position_sender.MakeStaticBuilder();
736
737 intake_roller->SerializePosition(
738 superstructure_can_builder->add_intake_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800739 constants::Values::kIntakeRollerOutputRatio);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800740 transfer_roller->SerializePosition(
741 superstructure_can_builder->add_transfer_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800742 constants::Values::kIntakeRollerOutputRatio);
743 climber->SerializePosition(superstructure_can_builder->add_climber(),
744 superstructure::climber::kOutputRatio);
Filip Kujawad75252a2024-02-10 16:54:35 -0800745 extend_roller->SerializePosition(
746 superstructure_can_builder->add_extend_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800747 constants::Values::kExtendRollerOutputRatio);
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800748 retention_roller->SerializePosition(
749 superstructure_can_builder->add_retention_roller(), 1.0);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800750
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800751 superstructure_can_builder->set_status(static_cast<int>(status));
752 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800753 },
754 frc971::wpilib::CANSensorReader::SignalSync::kNoSync);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800755
756 AddLoop(&can_sensor_reader_event_loop);
Niko Sohmers84273952024-02-14 18:40:55 -0800757 AddLoop(&rio_sensor_reader_event_loop);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800758
759 // Thread 5.
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800760 ::aos::ShmEventLoop can_output_event_loop(&config.message());
761 can_output_event_loop.set_name("CANOutputWriter");
762
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800763 frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer(
764 &can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800765
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800766 frc971::wpilib::GenericCANWriter<control_loops::superstructure::Output>
767 can_superstructure_writer(
768 &can_output_event_loop,
769 [](const control_loops::superstructure::Output &output,
770 const std::map<std::string_view, std::shared_ptr<TalonFX>>
771 &talonfx_map) {
772 talonfx_map.find("intake_pivot")
773 ->second->WriteVoltage(output.intake_pivot_voltage());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800774 talonfx_map.find("altitude")
775 ->second->WriteVoltage(output.altitude_voltage());
776 talonfx_map.find("catapult_one")
777 ->second->WriteVoltage(output.catapult_voltage());
778 talonfx_map.find("catapult_two")
779 ->second->WriteVoltage(output.catapult_voltage());
780 talonfx_map.find("turret")->second->WriteVoltage(
781 output.turret_voltage());
Filip Kujawa749f2442024-02-04 01:12:35 -0800782 talonfx_map.find("climber")->second->WriteVoltage(
783 output.climber_voltage());
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700784 if (talonfx_map.find("extend") != talonfx_map.end()) {
785 talonfx_map.find("extend")->second->WriteVoltage(
786 output.extend_voltage());
787 }
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800788 talonfx_map.find("intake_roller")
789 ->second->WriteVoltage(output.intake_roller_voltage());
790 talonfx_map.find("transfer_roller")
791 ->second->WriteVoltage(output.transfer_roller_voltage());
Filip Kujawad75252a2024-02-10 16:54:35 -0800792 talonfx_map.find("extend_roller")
793 ->second->WriteVoltage(output.extend_roller_voltage());
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800794 talonfx_map.find("retention_roller")
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800795 ->second->WriteCurrent(
796 output.retention_roller_stator_current_limit(),
797 output.retention_roller_voltage());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800798 });
799
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800800 can_drivetrain_writer.set_talonfxs({right_front, right_back},
801 {left_front, left_back});
802
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800803 can_superstructure_writer.add_talonfx("intake_pivot", intake_pivot);
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800804 can_superstructure_writer.add_talonfx("altitude", altitude);
805 can_superstructure_writer.add_talonfx("catapult_one", catapult_one);
806 can_superstructure_writer.add_talonfx("catapult_two", catapult_two);
807 can_superstructure_writer.add_talonfx("turret", turret);
Filip Kujawa749f2442024-02-04 01:12:35 -0800808 can_superstructure_writer.add_talonfx("climber", climber);
Niko Sohmers58461f52024-03-20 20:12:10 -0700809 if (!robot_constants->robot()->disable_extend()) {
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700810 can_superstructure_writer.add_talonfx("extend", extend);
811 }
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800812 can_superstructure_writer.add_talonfx("intake_roller", intake_roller);
813 can_superstructure_writer.add_talonfx("transfer_roller", transfer_roller);
Filip Kujawad75252a2024-02-10 16:54:35 -0800814 can_superstructure_writer.add_talonfx("extend_roller", extend_roller);
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800815 can_superstructure_writer.add_talonfx("retention_roller", retention_roller);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800816
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800817 can_output_event_loop.MakeWatcher(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800818 "/roborio", [&can_drivetrain_writer, &can_superstructure_writer](
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800819 const frc971::CANConfiguration &configuration) {
820 can_drivetrain_writer.HandleCANConfiguration(configuration);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800821 can_superstructure_writer.HandleCANConfiguration(configuration);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800822 });
823
824 AddLoop(&can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800825
Filip Kujawa70183d62024-03-02 17:02:51 -0800826 // Thread 6
827 // Setup led_indicator
828 ::aos::ShmEventLoop led_indicator_event_loop(&config.message());
829 led_indicator_event_loop.set_name("LedIndicator");
830 control_loops::superstructure::LedIndicator led_indicator(
831 &led_indicator_event_loop);
832 AddLoop(&led_indicator_event_loop);
833
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800834 RunLoops();
835 }
836};
837
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800838} // namespace y2024::wpilib
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800839
840AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot);