blob: eb419e625bdb9d3bfd262651980ea7471ee89b05 [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_);
Maxwell Henderson3c00b7d2024-03-21 21:17:51 -0700145 if (aos::network::GetTeamNumber() != 9971) {
146 AddToDMA(&turret_encoder_.reader());
147 AddToDMA(&altitude_encoder_.reader());
148 }
Maxwell Hendersonafc5c6d2024-02-20 15:34:13 -0800149 }
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800150
151 // Auto mode switches.
152 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
153 autonomous_modes_.at(i) = ::std::move(sensor);
154 }
155
156 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
157 imu_yaw_rate_input_ = ::std::move(sensor);
158 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
159 }
160
161 void RunIteration() override {
Maxwell Henderson3c00b7d2024-03-21 21:17:51 -0700162 if (aos::network::GetTeamNumber() != 9971) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800163 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
164 superstructure_position_sender_.MakeStaticBuilder();
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800165
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800166 CopyPosition(intake_pivot_encoder_, builder->add_intake_pivot(),
167 Values::kIntakePivotEncoderCountsPerRevolution(),
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800168 Values::kIntakePivotEncoderRatio(), /* reversed: */ false);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800169
Filip Kujawa749f2442024-02-04 01:12:35 -0800170 CopyPosition(climber_encoder_, builder->add_climber(),
171 Values::kClimberEncoderCountsPerRevolution(),
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800172 Values::kClimberEncoderMetersPerRadian(),
173 climber_pot_translate, false,
Filip Kujawa749f2442024-02-04 01:12:35 -0800174 robot_constants_->robot()
175 ->climber_constants()
176 ->potentiometer_offset());
177
Filip Kujawad75252a2024-02-10 16:54:35 -0800178 CopyPosition(extend_encoder_, builder->add_extend(),
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800179 Values::kExtendEncoderCountsPerRevolution(),
180 Values::kExtendEncoderMetersPerRadian(),
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800181 extend_pot_translate, false,
Filip Kujawad75252a2024-02-10 16:54:35 -0800182 robot_constants_->robot()
183 ->extend_constants()
184 ->potentiometer_offset());
185
Niko Sohmers27d92c62024-02-19 14:15:07 -0800186 CopyPosition(catapult_encoder_, builder->add_catapult(),
187 Values::kCatapultEncoderCountsPerRevolution(),
188 Values::kCatapultEncoderRatio(), catapult_pot_translate,
189 true,
190 robot_constants_->robot()
191 ->catapult_constants()
192 ->potentiometer_offset());
193
194 CopyPosition(turret_encoder_, builder->add_turret(),
195 Values::kTurretEncoderCountsPerRevolution(),
196 Values::kTurretEncoderRatio(), turret_pot_translate, true,
197 robot_constants_->robot()
198 ->turret_constants()
199 ->potentiometer_offset());
200
201 CopyPosition(altitude_encoder_, builder->add_altitude(),
202 Values::kAltitudeEncoderCountsPerRevolution(),
203 Values::kAltitudeEncoderRatio(), altitude_pot_translate,
204 true,
205 robot_constants_->robot()
206 ->altitude_constants()
207 ->potentiometer_offset());
208
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800209 builder->set_transfer_beambreak(transfer_beam_break_->Get());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800210 builder->set_extend_beambreak(extend_beam_break_->Get());
Maxwell Henderson576b0c92024-02-23 17:10:48 -0800211 builder->set_catapult_beambreak(catapult_beam_break_->Get());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800212 builder.CheckOk(builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800213 }
214
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800215 SendDrivetrainPosition(drivetrain_position_sender_.MakeStaticBuilder(),
216 drivetrain_velocity_translate,
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800217 constants::Values::DrivetrainEncoderToMeters, true,
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800218 false);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800219
220 {
221 auto builder = gyro_sender_.MakeBuilder();
222 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
223 builder.MakeBuilder<::frc971::sensors::GyroReading>();
224 // +/- 2000 deg / sec
225 constexpr double kMaxVelocity = 4000; // degrees / second
226 constexpr double kVelocityRadiansPerSecond =
227 kMaxVelocity / 360 * (2.0 * M_PI);
228
229 // Only part of the full range is used to prevent being 100% on or off.
230 constexpr double kScaledRangeLow = 0.1;
231 constexpr double kScaledRangeHigh = 0.9;
232
233 constexpr double kPWMFrequencyHz = 200;
234 double velocity_duty_cycle =
235 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
236
237 constexpr double kDutyCycleScale =
238 1 / (kScaledRangeHigh - kScaledRangeLow);
239 // scale from 0.1 - 0.9 to 0 - 1
240 double rescaled_velocity_duty_cycle =
241 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
242
243 if (!std::isnan(rescaled_velocity_duty_cycle)) {
244 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
245 kVelocityRadiansPerSecond);
246 }
247 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
248 }
249
250 {
251 auto builder = auto_mode_sender_.MakeBuilder();
252
253 uint32_t mode = 0;
254 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
255 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
256 mode |= 1 << i;
257 }
258 }
259
260 auto auto_mode_builder =
261 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
262
263 auto_mode_builder.add_mode(mode);
264
265 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
266 }
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 Kujawa749f2442024-02-04 01:12:35 -0800288 void set_climber(::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 climber_encoder_.set_encoder(::std::move(encoder));
293 climber_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
294 climber_encoder_.set_potentiometer(::std::move(potentiometer));
295 }
296
Filip Kujawad75252a2024-02-10 16:54:35 -0800297 void set_extend(::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 extend_encoder_.set_encoder(::std::move(encoder));
302 extend_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
303 extend_encoder_.set_potentiometer(::std::move(potentiometer));
304 }
305
Niko Sohmers27d92c62024-02-19 14:15:07 -0800306 void set_catapult(::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 catapult_encoder_.set_encoder(::std::move(encoder));
311 catapult_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
312 catapult_encoder_.set_potentiometer(::std::move(potentiometer));
313 }
314
315 void set_turret(::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 turret_encoder_.set_encoder(::std::move(encoder));
320 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
321 turret_encoder_.set_potentiometer(::std::move(potentiometer));
322 }
323
324 void set_altitude(::std::unique_ptr<frc::Encoder> encoder,
325 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
326 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
327 fast_encoder_filter_.Add(encoder.get());
328 altitude_encoder_.set_encoder(::std::move(encoder));
329 altitude_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
330 altitude_encoder_.set_potentiometer(::std::move(potentiometer));
331 }
332
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800333 private:
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800334 const Constants *robot_constants_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800335
336 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800337 aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800338 aos::Sender<frc971::control_loops::drivetrain::PositionStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800339 drivetrain_position_sender_;
340 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
341
342 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
343
Maxwell Henderson576b0c92024-02-23 17:10:48 -0800344 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_, transfer_beam_break_,
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800345 extend_beam_break_, catapult_beam_break_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800346
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800347 frc971::wpilib::AbsoluteEncoder intake_pivot_encoder_;
Niko Sohmers27d92c62024-02-19 14:15:07 -0800348 frc971::wpilib::AbsoluteEncoderAndPotentiometer climber_encoder_,
Maxwell Hendersonafc5c6d2024-02-20 15:34:13 -0800349 catapult_encoder_, extend_encoder_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800350
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800351 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
Maxwell Hendersonafc5c6d2024-02-20 15:34:13 -0800352
353 frc971::wpilib::DMAAbsoluteEncoderAndPotentiometer turret_encoder_,
354 altitude_encoder_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800355};
356
Maxwell Hendersonabcc92a2024-02-21 19:06:26 -0800357class SuperstructurePWMWriter
358 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
359 public:
360 SuperstructurePWMWriter(aos::EventLoop *event_loop)
361 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
362 event_loop, "/superstructure") {}
363
364 void set_catapult_kraken_one(::std::unique_ptr<::frc::TalonFX> t) {
365 catapult_kraken_one_ = ::std::move(t);
366 }
367 void set_catapult_kraken_two(::std::unique_ptr<::frc::TalonFX> t) {
368 catapult_kraken_two_ = ::std::move(t);
369 }
370
371 private:
372 void Stop() override {
373 AOS_LOG(WARNING, "Superstructure output too old.\n");
374 catapult_kraken_one_->SetDisabled();
375 catapult_kraken_two_->SetDisabled();
376 }
377
378 void Write(const superstructure::Output &output) override {
379 WritePwm(output.catapult_voltage(), catapult_kraken_one_.get());
Maxwell Henderson12d427b2024-02-22 13:57:45 -0800380 WritePwm(output.catapult_voltage(), catapult_kraken_two_.get());
Maxwell Hendersonabcc92a2024-02-21 19:06:26 -0800381 }
382
383 template <typename T>
384 static void WritePwm(const double voltage, T *motor) {
385 motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) /
386 12.0);
387 }
388 ::std::unique_ptr<::frc::TalonFX> catapult_kraken_one_, catapult_kraken_two_;
389};
390
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800391class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
392 public:
393 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
394 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
395 frc::Encoder::k4X);
396 }
397
398 void Run() override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800399 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
400 aos::configuration::ReadConfig("aos_config.json");
401
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800402 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
403
Maxwell Henderson3c00b7d2024-03-21 21:17:51 -0700404 const int team_number = aos::network::GetTeamNumber();
405
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800406 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
407 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(
408 &constant_fetcher_event_loop);
409 const Constants *robot_constants = &constants_fetcher.constants();
410
Maxwell Hendersonca7c9b62024-02-21 13:20:09 -0800411 AddLoop(&constant_fetcher_event_loop);
412
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800413 // Thread 1.
414 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
415 ::frc971::wpilib::JoystickSender joystick_sender(
416 &joystick_sender_event_loop);
417 AddLoop(&joystick_sender_event_loop);
418
419 // Thread 2.
420 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
421 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
422 AddLoop(&pdp_fetcher_event_loop);
423
424 // Thread 3.
425 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800426 SensorReader sensor_reader(&sensor_reader_event_loop, robot_constants);
Austin Schuh65b4f9d2024-03-17 16:03:10 -0700427 sensor_reader.set_pwm_trigger(false);
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800428 sensor_reader.set_drivetrain_left_encoder(
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800429 std::make_unique<frc::Encoder>(8, 9));
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800430 sensor_reader.set_drivetrain_right_encoder(
431 std::make_unique<frc::Encoder>(6, 7));
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800432 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(25));
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800433
Maxwell Henderson3c00b7d2024-03-21 21:17:51 -0700434 if (team_number != 9971) {
435 sensor_reader.set_intake_pivot(make_encoder(3),
436 make_unique<frc::DigitalInput>(3));
437 sensor_reader.set_transfer_beambreak(make_unique<frc::DigitalInput>(23));
438 sensor_reader.set_extend_beambreak(make_unique<frc::DigitalInput>(24));
439 sensor_reader.set_catapult_beambreak(make_unique<frc::DigitalInput>(22));
440
441 sensor_reader.set_climber(make_encoder(4),
442 make_unique<frc::DigitalInput>(4),
443 make_unique<frc::AnalogInput>(4));
444 sensor_reader.set_extend(make_encoder(5),
445 make_unique<frc::DigitalInput>(5),
446 make_unique<frc::AnalogInput>(5));
447 sensor_reader.set_catapult(make_encoder(0),
448 make_unique<frc::DigitalInput>(0),
449 make_unique<frc::AnalogInput>(0));
450 sensor_reader.set_turret(make_encoder(2),
451 make_unique<frc::DigitalInput>(2),
452 make_unique<frc::AnalogInput>(2));
453 sensor_reader.set_altitude(make_encoder(1),
454 make_unique<frc::DigitalInput>(1),
455 make_unique<frc::AnalogInput>(1));
456 }
Filip Kujawad75252a2024-02-10 16:54:35 -0800457
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800458 AddLoop(&sensor_reader_event_loop);
459
Maxwell Henderson3c00b7d2024-03-21 21:17:51 -0700460 if (team_number == 9971) {
461 std::vector<ctre::phoenix6::BaseStatusSignal *> signal_registry;
462
463 if (!FLAGS_ctre_diag_server) {
464 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
465 c_Phoenix_Diagnostics_Dispose();
466 }
467
468 const CurrentLimits *current_limits =
469 robot_constants->common()->current_limits();
470
471 std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>(
472 2, true, "Drivetrain Bus", &signal_registry,
473 current_limits->drivetrain_supply_current_limit(),
474 current_limits->drivetrain_stator_current_limit());
475 std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>(
476 1, true, "Drivetrain Bus", &signal_registry,
477 current_limits->drivetrain_supply_current_limit(),
478 current_limits->drivetrain_stator_current_limit());
479 std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>(
480 4, false, "Drivetrain Bus", &signal_registry,
481 current_limits->drivetrain_supply_current_limit(),
482 current_limits->drivetrain_stator_current_limit());
483 std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>(
484 5, false, "Drivetrain Bus", &signal_registry,
485 current_limits->drivetrain_supply_current_limit(),
486 current_limits->drivetrain_stator_current_limit());
487
488 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
489 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
490 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
491 constants::Values::kDrivetrainWriterPriority, true, "Drivetrain Bus");
492
493 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
494 can_sensor_reader_event_loop.set_name("CANSensorReader");
495
496 std::vector<std::shared_ptr<TalonFX>> drivetrain_krakens;
497
498 for (auto talonfx : {right_front, right_back, left_front, left_back}) {
499 drivetrain_krakens.push_back(talonfx);
500 }
501
502 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>
503 drivetrain_can_position_sender =
504 can_sensor_reader_event_loop.MakeSender<
505 frc971::control_loops::drivetrain::CANPositionStatic>(
506 "/drivetrain");
507
508 frc971::wpilib::CANSensorReader can_sensor_reader(
509 &can_sensor_reader_event_loop, std::move(signal_registry),
510 drivetrain_krakens,
511 [drivetrain_krakens,
512 &drivetrain_can_position_sender](ctre::phoenix::StatusCode status) {
513 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>::
514 StaticBuilder drivetrain_can_builder =
515 drivetrain_can_position_sender.MakeStaticBuilder();
516
517 auto drivetrain_falcon_vector =
518 CHECK_NOTNULL(drivetrain_can_builder->add_talonfxs());
519
520 for (auto talonfx : drivetrain_krakens) {
521 talonfx->SerializePosition(
522 drivetrain_falcon_vector->emplace_back(),
523 control_loops::drivetrain::kHighOutputRatio);
524 }
525
526 drivetrain_can_builder->set_status(static_cast<int>(status));
527
528 drivetrain_can_builder.CheckOk(drivetrain_can_builder.Send());
529 });
530
531 AddLoop(&can_sensor_reader_event_loop);
532
533 ::aos::ShmEventLoop can_output_event_loop(&config.message());
534 can_output_event_loop.set_name("CANOutputWriter");
535
536 frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer(
537 &can_output_event_loop);
538
539 can_drivetrain_writer.set_talonfxs({right_front, right_back},
540 {left_front, left_back});
541 can_output_event_loop.MakeWatcher(
542 "/roborio", [&can_drivetrain_writer](
543 const frc971::CANConfiguration &configuration) {
544 can_drivetrain_writer.HandleCANConfiguration(configuration);
545 });
546
547 AddLoop(&can_output_event_loop);
548
549 RunLoops();
550 return;
551 }
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800552 // Thread 4.
553 // Set up CAN.
554 if (!FLAGS_ctre_diag_server) {
555 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
556 c_Phoenix_Diagnostics_Dispose();
557 }
558
Niko Sohmers84273952024-02-14 18:40:55 -0800559 std::vector<ctre::phoenix6::BaseStatusSignal *> canivore_signal_registry;
560 std::vector<ctre::phoenix6::BaseStatusSignal *> rio_signal_registry;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800561
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800562 const CurrentLimits *current_limits =
563 robot_constants->common()->current_limits();
564
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800565 std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800566 2, true, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800567 current_limits->drivetrain_supply_current_limit(),
568 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800569 std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800570 1, true, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800571 current_limits->drivetrain_supply_current_limit(),
572 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800573 std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800574 4, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800575 current_limits->drivetrain_supply_current_limit(),
576 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800577 std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800578 5, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800579 current_limits->drivetrain_supply_current_limit(),
580 current_limits->drivetrain_stator_current_limit());
581 std::shared_ptr<TalonFX> intake_pivot = std::make_shared<TalonFX>(
Maxwell Henderson30ea8b32024-02-22 16:14:05 -0800582 6, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800583 current_limits->intake_pivot_stator_current_limit(),
584 current_limits->intake_pivot_supply_current_limit());
Niko Sohmers27d92c62024-02-19 14:15:07 -0800585 std::shared_ptr<TalonFX> altitude = std::make_shared<TalonFX>(
Maxwell Henderson30ea8b32024-02-22 16:14:05 -0800586 9, false, "Drivetrain Bus", &canivore_signal_registry,
Niko Sohmers27d92c62024-02-19 14:15:07 -0800587 current_limits->altitude_stator_current_limit(),
588 current_limits->altitude_supply_current_limit());
589 std::shared_ptr<TalonFX> turret = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800590 3, true, "Drivetrain Bus", &canivore_signal_registry,
Niko Sohmers27d92c62024-02-19 14:15:07 -0800591 current_limits->turret_stator_current_limit(),
592 current_limits->turret_supply_current_limit());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800593 std::shared_ptr<TalonFX> climber = std::make_shared<TalonFX>(
594 7, true, "rio", &rio_signal_registry,
595 current_limits->climber_stator_current_limit(),
596 current_limits->climber_supply_current_limit());
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700597 std::shared_ptr<TalonFX> extend =
Niko Sohmers58461f52024-03-20 20:12:10 -0700598 (robot_constants->robot()->disable_extend())
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700599 ? nullptr
600 : std::make_shared<TalonFX>(
601 12, false, "Drivetrain Bus", &canivore_signal_registry,
602 current_limits->extend_stator_current_limit(),
603 current_limits->extend_supply_current_limit());
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800604 std::shared_ptr<TalonFX> intake_roller = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800605 8, false, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800606 current_limits->intake_roller_stator_current_limit(),
607 current_limits->intake_roller_supply_current_limit());
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800608 std::shared_ptr<TalonFX> retention_roller = std::make_shared<TalonFX>(
Maxwell Hendersonefead392024-02-23 19:10:50 -0800609 10, true, "rio", &rio_signal_registry,
Maxwell Hendersonfe735652024-02-23 19:22:31 -0800610 current_limits->retention_roller_stator_current_limit(),
611 current_limits->retention_roller_supply_current_limit());
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800612 std::shared_ptr<TalonFX> transfer_roller = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800613 11, true, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800614 current_limits->transfer_roller_stator_current_limit(),
615 current_limits->transfer_roller_supply_current_limit());
Filip Kujawad75252a2024-02-10 16:54:35 -0800616 std::shared_ptr<TalonFX> extend_roller = std::make_shared<TalonFX>(
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800617 13, true, "rio", &rio_signal_registry,
Filip Kujawad75252a2024-02-10 16:54:35 -0800618 current_limits->extend_roller_stator_current_limit(),
619 current_limits->extend_roller_supply_current_limit());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800620 std::shared_ptr<TalonFX> catapult_one = std::make_shared<TalonFX>(
621 14, false, "Drivetrain Bus", &canivore_signal_registry,
622 current_limits->catapult_stator_current_limit(),
623 current_limits->catapult_supply_current_limit());
624 std::shared_ptr<TalonFX> catapult_two = std::make_shared<TalonFX>(
625 15, false, "Drivetrain Bus", &canivore_signal_registry,
626 current_limits->catapult_stator_current_limit(),
627 current_limits->catapult_supply_current_limit());
Filip Kujawad75252a2024-02-10 16:54:35 -0800628
Stephan Pleines5aabf632024-03-13 20:31:18 -0700629 transfer_roller->set_neutral_mode(
630 ctre::phoenix6::signals::NeutralModeValue::Coast);
631 intake_roller->set_neutral_mode(
632 ctre::phoenix6::signals::NeutralModeValue::Coast);
633
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800634 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
635 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
636 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
Austin Schuh46e0d102024-03-17 16:04:57 -0700637 constants::Values::kDrivetrainWriterPriority, true, "Drivetrain Bus");
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800638
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800639 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
640 can_sensor_reader_event_loop.set_name("CANSensorReader");
641
Niko Sohmers84273952024-02-14 18:40:55 -0800642 ::aos::ShmEventLoop rio_sensor_reader_event_loop(&config.message());
643 rio_sensor_reader_event_loop.set_name("RioSensorReader");
644
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800645 // Creating list of talonfx for CANSensorReader
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800646 std::vector<std::shared_ptr<TalonFX>> drivetrain_talonfxs;
Niko Sohmers84273952024-02-14 18:40:55 -0800647 std::vector<std::shared_ptr<TalonFX>> canivore_talonfxs;
648 std::vector<std::shared_ptr<TalonFX>> rio_talonfxs;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800649
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800650 for (auto talonfx : {right_front, right_back, left_front, left_back}) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800651 drivetrain_talonfxs.push_back(talonfx);
Niko Sohmers84273952024-02-14 18:40:55 -0800652 canivore_talonfxs.push_back(talonfx);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800653 }
654
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800655 for (auto talonfx :
Austin Schuh95764db2024-03-01 19:21:09 -0800656 {intake_pivot, turret, altitude, catapult_one, catapult_two, extend}) {
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700657 if (talonfx != nullptr) {
658 canivore_talonfxs.push_back(talonfx);
659 }
Niko Sohmers84273952024-02-14 18:40:55 -0800660 }
661
Austin Schuh95764db2024-03-01 19:21:09 -0800662 for (auto talonfx : {intake_roller, transfer_roller, climber, extend_roller,
663 retention_roller}) {
Niko Sohmers84273952024-02-14 18:40:55 -0800664 rio_talonfxs.push_back(talonfx);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800665 }
666
667 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>
668 drivetrain_can_position_sender =
669 can_sensor_reader_event_loop.MakeSender<
670 frc971::control_loops::drivetrain::CANPositionStatic>(
671 "/drivetrain");
672
673 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
674 superstructure_can_position_sender =
675 can_sensor_reader_event_loop.MakeSender<
676 y2024::control_loops::superstructure::CANPositionStatic>(
Niko Sohmers84273952024-02-14 18:40:55 -0800677 "/superstructure/canivore");
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800678
Niko Sohmers84273952024-02-14 18:40:55 -0800679 frc971::wpilib::CANSensorReader canivore_can_sensor_reader(
680 &can_sensor_reader_event_loop, std::move(canivore_signal_registry),
681 canivore_talonfxs,
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800682 [drivetrain_talonfxs, &intake_pivot, &turret, &altitude, &catapult_one,
683 &catapult_two, &drivetrain_can_position_sender,
Austin Schuh95764db2024-03-01 19:21:09 -0800684 &superstructure_can_position_sender,
685 &extend](ctre::phoenix::StatusCode status) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800686 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>::
687 StaticBuilder drivetrain_can_builder =
688 drivetrain_can_position_sender.MakeStaticBuilder();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800689
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800690 auto drivetrain_falcon_vector =
Maxwell Henderson563efed2024-02-17 21:11:33 -0800691 CHECK_NOTNULL(drivetrain_can_builder->add_talonfxs());
Maxwell Henderson9116e5b2024-01-21 12:14:26 -0800692
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800693 for (auto talonfx : drivetrain_talonfxs) {
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800694 talonfx->SerializePosition(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800695 drivetrain_falcon_vector->emplace_back(),
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800696 control_loops::drivetrain::kHighOutputRatio);
697 }
698
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800699 drivetrain_can_builder->set_status(static_cast<int>(status));
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800700
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800701 drivetrain_can_builder.CheckOk(drivetrain_can_builder.Send());
702
703 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
704 StaticBuilder superstructure_can_builder =
705 superstructure_can_position_sender.MakeStaticBuilder();
706
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800707 intake_pivot->SerializePosition(
708 superstructure_can_builder->add_intake_pivot(),
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800709 control_loops::superstructure::intake_pivot::kOutputRatio);
Niko Sohmers27d92c62024-02-19 14:15:07 -0800710 turret->SerializePosition(
711 superstructure_can_builder->add_turret(),
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800712 control_loops::superstructure::turret::kOutputRatio);
713 altitude->SerializePosition(
714 superstructure_can_builder->add_altitude(),
715 control_loops::superstructure::altitude::kOutputRatio);
716 catapult_one->SerializePosition(
717 superstructure_can_builder->add_catapult_one(),
718 control_loops::superstructure::catapult::kOutputRatio);
719 catapult_two->SerializePosition(
720 superstructure_can_builder->add_catapult_two(),
721 control_loops::superstructure::catapult::kOutputRatio);
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700722 if (extend != nullptr) {
723 extend->SerializePosition(superstructure_can_builder->add_extend(),
724 superstructure::extend::kOutputRatio);
725 }
Niko Sohmers84273952024-02-14 18:40:55 -0800726
Niko Sohmers84273952024-02-14 18:40:55 -0800727 superstructure_can_builder->set_status(static_cast<int>(status));
728 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
729 });
730
731 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
732 superstructure_rio_position_sender =
733 rio_sensor_reader_event_loop.MakeSender<
734 y2024::control_loops::superstructure::CANPositionStatic>(
735 "/superstructure/rio");
736
737 frc971::wpilib::CANSensorReader rio_can_sensor_reader(
738 &rio_sensor_reader_event_loop, std::move(rio_signal_registry),
739 rio_talonfxs,
Austin Schuh95764db2024-03-01 19:21:09 -0800740 [&intake_roller, &transfer_roller, &climber, &extend_roller,
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800741 &retention_roller, &superstructure_rio_position_sender](
Niko Sohmers84273952024-02-14 18:40:55 -0800742 ctre::phoenix::StatusCode status) {
743 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
744 StaticBuilder superstructure_can_builder =
745 superstructure_rio_position_sender.MakeStaticBuilder();
746
747 intake_roller->SerializePosition(
748 superstructure_can_builder->add_intake_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800749 constants::Values::kIntakeRollerOutputRatio);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800750 transfer_roller->SerializePosition(
751 superstructure_can_builder->add_transfer_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800752 constants::Values::kIntakeRollerOutputRatio);
753 climber->SerializePosition(superstructure_can_builder->add_climber(),
754 superstructure::climber::kOutputRatio);
Filip Kujawad75252a2024-02-10 16:54:35 -0800755 extend_roller->SerializePosition(
756 superstructure_can_builder->add_extend_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800757 constants::Values::kExtendRollerOutputRatio);
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800758 retention_roller->SerializePosition(
759 superstructure_can_builder->add_retention_roller(), 1.0);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800760
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800761 superstructure_can_builder->set_status(static_cast<int>(status));
762 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800763 },
764 frc971::wpilib::CANSensorReader::SignalSync::kNoSync);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800765
766 AddLoop(&can_sensor_reader_event_loop);
Niko Sohmers84273952024-02-14 18:40:55 -0800767 AddLoop(&rio_sensor_reader_event_loop);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800768
769 // Thread 5.
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800770 ::aos::ShmEventLoop can_output_event_loop(&config.message());
771 can_output_event_loop.set_name("CANOutputWriter");
772
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800773 frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer(
774 &can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800775
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800776 frc971::wpilib::GenericCANWriter<control_loops::superstructure::Output>
777 can_superstructure_writer(
778 &can_output_event_loop,
779 [](const control_loops::superstructure::Output &output,
780 const std::map<std::string_view, std::shared_ptr<TalonFX>>
781 &talonfx_map) {
782 talonfx_map.find("intake_pivot")
783 ->second->WriteVoltage(output.intake_pivot_voltage());
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800784 talonfx_map.find("altitude")
785 ->second->WriteVoltage(output.altitude_voltage());
786 talonfx_map.find("catapult_one")
787 ->second->WriteVoltage(output.catapult_voltage());
788 talonfx_map.find("catapult_two")
789 ->second->WriteVoltage(output.catapult_voltage());
790 talonfx_map.find("turret")->second->WriteVoltage(
791 output.turret_voltage());
Filip Kujawa749f2442024-02-04 01:12:35 -0800792 talonfx_map.find("climber")->second->WriteVoltage(
793 output.climber_voltage());
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700794 if (talonfx_map.find("extend") != talonfx_map.end()) {
795 talonfx_map.find("extend")->second->WriteVoltage(
796 output.extend_voltage());
797 }
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800798 talonfx_map.find("intake_roller")
799 ->second->WriteVoltage(output.intake_roller_voltage());
800 talonfx_map.find("transfer_roller")
801 ->second->WriteVoltage(output.transfer_roller_voltage());
Filip Kujawad75252a2024-02-10 16:54:35 -0800802 talonfx_map.find("extend_roller")
803 ->second->WriteVoltage(output.extend_roller_voltage());
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800804 talonfx_map.find("retention_roller")
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800805 ->second->WriteCurrent(
806 output.retention_roller_stator_current_limit(),
807 output.retention_roller_voltage());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800808 });
809
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800810 can_drivetrain_writer.set_talonfxs({right_front, right_back},
811 {left_front, left_back});
812
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800813 can_superstructure_writer.add_talonfx("intake_pivot", intake_pivot);
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800814 can_superstructure_writer.add_talonfx("altitude", altitude);
815 can_superstructure_writer.add_talonfx("catapult_one", catapult_one);
816 can_superstructure_writer.add_talonfx("catapult_two", catapult_two);
817 can_superstructure_writer.add_talonfx("turret", turret);
Filip Kujawa749f2442024-02-04 01:12:35 -0800818 can_superstructure_writer.add_talonfx("climber", climber);
Niko Sohmers58461f52024-03-20 20:12:10 -0700819 if (!robot_constants->robot()->disable_extend()) {
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700820 can_superstructure_writer.add_talonfx("extend", extend);
821 }
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800822 can_superstructure_writer.add_talonfx("intake_roller", intake_roller);
823 can_superstructure_writer.add_talonfx("transfer_roller", transfer_roller);
Filip Kujawad75252a2024-02-10 16:54:35 -0800824 can_superstructure_writer.add_talonfx("extend_roller", extend_roller);
Maxwell Henderson7b7754b2024-02-22 16:50:35 -0800825 can_superstructure_writer.add_talonfx("retention_roller", retention_roller);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800826
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800827 can_output_event_loop.MakeWatcher(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800828 "/roborio", [&can_drivetrain_writer, &can_superstructure_writer](
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800829 const frc971::CANConfiguration &configuration) {
830 can_drivetrain_writer.HandleCANConfiguration(configuration);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800831 can_superstructure_writer.HandleCANConfiguration(configuration);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800832 });
833
834 AddLoop(&can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800835
Filip Kujawa70183d62024-03-02 17:02:51 -0800836 // Thread 6
837 // Setup led_indicator
838 ::aos::ShmEventLoop led_indicator_event_loop(&config.message());
839 led_indicator_event_loop.set_name("LedIndicator");
840 control_loops::superstructure::LedIndicator led_indicator(
841 &led_indicator_event_loop);
842 AddLoop(&led_indicator_event_loop);
843
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800844 RunLoops();
845 }
846};
847
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800848} // namespace y2024::wpilib
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800849
850AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot);