blob: a82114e1676268d799ff9ec2b6f0b752a9194fea [file] [log] [blame]
Niko Sohmers3860f8a2024-01-12 21:05:19 -08001#include <unistd.h>
2
3#include <array>
4#include <chrono>
5#include <cinttypes>
Niko Sohmers3860f8a2024-01-12 21:05:19 -08006#include <cstdio>
7#include <cstring>
8#include <functional>
9#include <memory>
10#include <mutex>
11#include <thread>
12
Niko Sohmers3860f8a2024-01-12 21:05:19 -080013#include "frc971/wpilib/ahal/AnalogInput.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080014#include "frc971/wpilib/ahal/DriverStation.h"
15#include "frc971/wpilib/ahal/Encoder.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080016#include "frc971/wpilib/ahal/TalonFX.h"
17#include "frc971/wpilib/ahal/VictorSP.h"
18#undef ERROR
19
20#include "ctre/phoenix/cci/Diagnostics_CCI.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080021
22#include "aos/commonmath.h"
23#include "aos/containers/sized_array.h"
24#include "aos/events/event_loop.h"
25#include "aos/events/shm_event_loop.h"
26#include "aos/init.h"
27#include "aos/logging/logging.h"
28#include "aos/realtime.h"
29#include "aos/time/time.h"
30#include "aos/util/log_interval.h"
31#include "aos/util/phased_loop.h"
32#include "aos/util/wrapping_counter.h"
33#include "frc971/autonomous/auto_mode_generated.h"
34#include "frc971/can_configuration_generated.h"
Niko Sohmers1259b2a2024-01-29 18:00:37 -080035#include "frc971/constants/constants_sender_lib.h"
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080036#include "frc971/control_loops/drivetrain/drivetrain_can_position_static.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080037#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
38#include "frc971/input/robot_state_generated.h"
39#include "frc971/queues/gyro_generated.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080040#include "frc971/wpilib/buffered_pcm.h"
41#include "frc971/wpilib/buffered_solenoid.h"
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080042#include "frc971/wpilib/can_drivetrain_writer.h"
43#include "frc971/wpilib/can_sensor_reader.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080044#include "frc971/wpilib/dma.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080045#include "frc971/wpilib/encoder_and_potentiometer.h"
Niko Sohmers1259b2a2024-01-29 18:00:37 -080046#include "frc971/wpilib/generic_can_writer.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080047#include "frc971/wpilib/joystick_sender.h"
48#include "frc971/wpilib/logging_generated.h"
49#include "frc971/wpilib/loop_output_handler.h"
50#include "frc971/wpilib/pdp_fetcher.h"
51#include "frc971/wpilib/sensor_reader.h"
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080052#include "frc971/wpilib/talonfx.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080053#include "frc971/wpilib/wpilib_robot_base.h"
54#include "y2024/constants.h"
Niko Sohmers1259b2a2024-01-29 18:00:37 -080055#include "y2024/constants/constants_generated.h"
56#include "y2024/control_loops/superstructure/superstructure_can_position_static.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080057#include "y2024/control_loops/superstructure/superstructure_output_generated.h"
58#include "y2024/control_loops/superstructure/superstructure_position_generated.h"
Niko Sohmers1259b2a2024-01-29 18:00:37 -080059#include "y2024/control_loops/superstructure/superstructure_position_static.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080060
61DEFINE_bool(ctre_diag_server, false,
62 "If true, enable the diagnostics server for interacting with "
63 "devices on the CAN bus using Phoenix Tuner");
64
65using ::aos::monotonic_clock;
66using ::frc971::CANConfiguration;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080067using ::frc971::control_loops::drivetrain::CANPositionStatic;
68using ::frc971::wpilib::TalonFX;
Niko Sohmers3860f8a2024-01-12 21:05:19 -080069using ::y2024::constants::Values;
70namespace superstructure = ::y2024::control_loops::superstructure;
71namespace drivetrain = ::y2024::control_loops::drivetrain;
72namespace chrono = ::std::chrono;
73using std::make_unique;
74
Stephan Pleinesf63bde82024-01-13 15:59:33 -080075namespace y2024::wpilib {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080076namespace {
77
78constexpr double kMaxBringupPower = 12.0;
79
Niko Sohmers1259b2a2024-01-29 18:00:37 -080080double intake_pot_translate(double voltage) {
81 return voltage * Values::kIntakePivotPotRadiansPerVolt();
82}
Niko Sohmers3860f8a2024-01-12 21:05:19 -080083
84double drivetrain_velocity_translate(double in) {
85 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
86 (2.0 * M_PI)) *
87 Values::kDrivetrainEncoderRatio() *
88 control_loops::drivetrain::kWheelRadius;
89}
90
91constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
92 Values::kMaxDrivetrainEncoderPulsesPerSecond(),
Maxwell Henderson1de15492024-02-07 11:09:47 -080093 Values::kMaxIntakePivotEncoderPulsesPerSecond(),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080094});
95static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
96 "fast encoders are too fast");
97
98} // namespace
99
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800100// Class to send position messages with sensor readings to our loops.
101class SensorReader : public ::frc971::wpilib::SensorReader {
102 public:
103 SensorReader(::aos::ShmEventLoop *event_loop,
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800104 const Constants *robot_constants)
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800105 : ::frc971::wpilib::SensorReader(event_loop),
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800106 robot_constants_(CHECK_NOTNULL(robot_constants)),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800107 auto_mode_sender_(
108 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
109 "/autonomous")),
110 superstructure_position_sender_(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800111 event_loop->MakeSender<superstructure::PositionStatic>(
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800112 "/superstructure")),
113 drivetrain_position_sender_(
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800114 event_loop->MakeSender<
115 ::frc971::control_loops::drivetrain::PositionStatic>(
116 "/drivetrain")),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800117 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
118 "/drivetrain")){};
119 void Start() override { AddToDMA(&imu_yaw_rate_reader_); }
120
121 // Auto mode switches.
122 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
123 autonomous_modes_.at(i) = ::std::move(sensor);
124 }
125
126 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
127 imu_yaw_rate_input_ = ::std::move(sensor);
128 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
129 }
130
131 void RunIteration() override {
132 {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800133 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
134 superstructure_position_sender_.MakeStaticBuilder();
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800135
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800136 CopyPosition(intake_pivot_encoder_, builder->add_intake_pivot(),
137 Values::kIntakePivotEncoderCountsPerRevolution(),
138 Values::kIntakePivotEncoderRatio(), intake_pot_translate,
139 true,
140 robot_constants_->robot()
141 ->intake_constants()
142 ->potentiometer_offset());
143
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800144 builder->set_transfer_beambreak(transfer_beam_break_->Get());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800145 builder.CheckOk(builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800146 }
147
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800148 SendDrivetrainPosition(drivetrain_position_sender_.MakeStaticBuilder(),
149 drivetrain_velocity_translate,
150 constants::Values::DrivetrainEncoderToMeters, false,
151 false);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800152
153 {
154 auto builder = gyro_sender_.MakeBuilder();
155 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
156 builder.MakeBuilder<::frc971::sensors::GyroReading>();
157 // +/- 2000 deg / sec
158 constexpr double kMaxVelocity = 4000; // degrees / second
159 constexpr double kVelocityRadiansPerSecond =
160 kMaxVelocity / 360 * (2.0 * M_PI);
161
162 // Only part of the full range is used to prevent being 100% on or off.
163 constexpr double kScaledRangeLow = 0.1;
164 constexpr double kScaledRangeHigh = 0.9;
165
166 constexpr double kPWMFrequencyHz = 200;
167 double velocity_duty_cycle =
168 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
169
170 constexpr double kDutyCycleScale =
171 1 / (kScaledRangeHigh - kScaledRangeLow);
172 // scale from 0.1 - 0.9 to 0 - 1
173 double rescaled_velocity_duty_cycle =
174 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
175
176 if (!std::isnan(rescaled_velocity_duty_cycle)) {
177 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
178 kVelocityRadiansPerSecond);
179 }
180 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
181 }
182
183 {
184 auto builder = auto_mode_sender_.MakeBuilder();
185
186 uint32_t mode = 0;
187 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
188 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
189 mode |= 1 << i;
190 }
191 }
192
193 auto auto_mode_builder =
194 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
195
196 auto_mode_builder.add_mode(mode);
197
198 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
199 }
200 }
201
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800202 void set_intake_pivot_encoder(::std::unique_ptr<frc::Encoder> encoder) {
203 fast_encoder_filter_.Add(encoder.get());
204 intake_pivot_encoder_.set_encoder(::std::move(encoder));
205 }
206
207 void set_intake_pivot_absolute_pwm(
208 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
209 intake_pivot_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
210 }
211
212 void set_intake_pivot_potentiometer(
213 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
214 intake_pivot_encoder_.set_potentiometer(::std::move(potentiometer));
215 }
216
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800217 void set_transfer_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
218 transfer_beam_break_ = ::std::move(sensor);
219 }
220
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800221 private:
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800222 const Constants *robot_constants_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800223
224 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800225 aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800226 aos::Sender<frc971::control_loops::drivetrain::PositionStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800227 drivetrain_position_sender_;
228 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
229
230 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
231
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800232 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_, transfer_beam_break_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800233
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800234 frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_pivot_encoder_;
235
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800236 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
237};
238
239class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
240 public:
241 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
242 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
243 frc::Encoder::k4X);
244 }
245
246 void Run() override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800247 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
248 aos::configuration::ReadConfig("aos_config.json");
249
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800250 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
251
252 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
253 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(
254 &constant_fetcher_event_loop);
255 const Constants *robot_constants = &constants_fetcher.constants();
256
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800257 // Thread 1.
258 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
259 ::frc971::wpilib::JoystickSender joystick_sender(
260 &joystick_sender_event_loop);
261 AddLoop(&joystick_sender_event_loop);
262
263 // Thread 2.
264 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
265 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
266 AddLoop(&pdp_fetcher_event_loop);
267
268 // Thread 3.
269 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800270 SensorReader sensor_reader(&sensor_reader_event_loop, robot_constants);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800271 sensor_reader.set_pwm_trigger(true);
272 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
273 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
274 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800275 // TODO: (niko) change values once robot is wired
276 sensor_reader.set_intake_pivot_encoder(make_encoder(4));
277 sensor_reader.set_intake_pivot_absolute_pwm(
278 make_unique<frc::DigitalInput>(4));
279 sensor_reader.set_intake_pivot_potentiometer(
280 make_unique<frc::AnalogInput>(4));
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800281 sensor_reader.set_transfer_beambreak(make_unique<frc::DigitalInput>(7));
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800282
283 AddLoop(&sensor_reader_event_loop);
284
285 // Thread 4.
286 // Set up CAN.
287 if (!FLAGS_ctre_diag_server) {
288 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
289 c_Phoenix_Diagnostics_Dispose();
290 }
291
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800292 std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry;
293
294 std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>(
295 0, false, "Drivetrain Bus", &signals_registry,
296 constants::Values::kDrivetrainStatorCurrentLimit(),
297 constants::Values::kDrivetrainSupplyCurrentLimit());
298 std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>(
299 1, false, "Drivetrain Bus", &signals_registry,
300 constants::Values::kDrivetrainStatorCurrentLimit(),
301 constants::Values::kDrivetrainSupplyCurrentLimit());
302 std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>(
303 2, false, "Drivetrain Bus", &signals_registry,
304 constants::Values::kDrivetrainStatorCurrentLimit(),
305 constants::Values::kDrivetrainSupplyCurrentLimit());
306 std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>(
307 3, false, "Drivetrain Bus", &signals_registry,
308 constants::Values::kDrivetrainStatorCurrentLimit(),
309 constants::Values::kDrivetrainSupplyCurrentLimit());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800310 std::shared_ptr<TalonFX> intake_pivot =
311 std::make_shared<TalonFX>(4, false, "Drivetrain Bus", &signals_registry,
312 robot_constants->common()
313 ->current_limits()
314 ->intake_pivot_stator_current_limit(),
315 robot_constants->common()
316 ->current_limits()
317 ->intake_pivot_supply_current_limit());
318 std::shared_ptr<TalonFX> intake_roller =
319 std::make_shared<TalonFX>(5, false, "Drivetrain Bus", &signals_registry,
320 robot_constants->common()
321 ->current_limits()
322 ->intake_roller_stator_current_limit(),
323 robot_constants->common()
324 ->current_limits()
325 ->intake_roller_supply_current_limit());
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800326 std::shared_ptr<TalonFX> transfer_roller =
327 std::make_shared<TalonFX>(6, false, "Drivetrain Bus", &signals_registry,
328 robot_constants->common()
329 ->current_limits()
330 ->transfer_roller_stator_current_limit(),
331 robot_constants->common()
332 ->current_limits()
333 ->transfer_roller_supply_current_limit());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800334 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
335 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
336 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
337 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
338
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800339 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
340 can_sensor_reader_event_loop.set_name("CANSensorReader");
341
342 // Creating list of talonfx for CANSensorReader
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800343 std::vector<std::shared_ptr<TalonFX>> drivetrain_talonfxs;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800344 std::vector<std::shared_ptr<TalonFX>> talonfxs;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800345
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800346 for (auto talonfx : {right_front, right_back, left_front, left_back}) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800347 drivetrain_talonfxs.push_back(talonfx);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800348 talonfxs.push_back(talonfx);
349 }
350
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800351 for (auto talonfx : {intake_pivot, intake_roller, transfer_roller}) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800352 talonfxs.push_back(talonfx);
353 }
354
355 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>
356 drivetrain_can_position_sender =
357 can_sensor_reader_event_loop.MakeSender<
358 frc971::control_loops::drivetrain::CANPositionStatic>(
359 "/drivetrain");
360
361 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
362 superstructure_can_position_sender =
363 can_sensor_reader_event_loop.MakeSender<
364 y2024::control_loops::superstructure::CANPositionStatic>(
365 "/superstructure");
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800366
367 frc971::wpilib::CANSensorReader can_sensor_reader(
368 &can_sensor_reader_event_loop, std::move(signals_registry), talonfxs,
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800369 [drivetrain_talonfxs, &intake_pivot, &intake_roller, &transfer_roller,
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800370 &drivetrain_can_position_sender, &superstructure_can_position_sender](
371 ctre::phoenix::StatusCode status) {
372 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>::
373 StaticBuilder drivetrain_can_builder =
374 drivetrain_can_position_sender.MakeStaticBuilder();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800375
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800376 auto drivetrain_falcon_vector =
377 drivetrain_can_builder->add_talonfxs();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800378
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800379 for (auto talonfx : drivetrain_talonfxs) {
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800380 talonfx->SerializePosition(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800381 drivetrain_falcon_vector->emplace_back(),
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800382 control_loops::drivetrain::kHighOutputRatio);
383 }
384
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800385 drivetrain_can_builder->set_timestamp(
386 drivetrain_talonfxs.front()->GetTimestamp());
387 drivetrain_can_builder->set_status(static_cast<int>(status));
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800388
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800389 drivetrain_can_builder.CheckOk(drivetrain_can_builder.Send());
390
391 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
392 StaticBuilder superstructure_can_builder =
393 superstructure_can_position_sender.MakeStaticBuilder();
394
395 intake_roller->SerializePosition(
396 superstructure_can_builder->add_intake_roller(),
397 control_loops::drivetrain::kHighOutputRatio);
398 intake_pivot->SerializePosition(
399 superstructure_can_builder->add_intake_pivot(),
400 control_loops::drivetrain::kHighOutputRatio);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800401 transfer_roller->SerializePosition(
402 superstructure_can_builder->add_transfer_roller(),
403 control_loops::drivetrain::kHighOutputRatio);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800404
405 superstructure_can_builder->set_timestamp(
406 intake_roller->GetTimestamp());
407 superstructure_can_builder->set_status(static_cast<int>(status));
408 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800409 });
410
411 AddLoop(&can_sensor_reader_event_loop);
412
413 // Thread 5.
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800414 ::aos::ShmEventLoop can_output_event_loop(&config.message());
415 can_output_event_loop.set_name("CANOutputWriter");
416
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800417 frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer(
418 &can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800419
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800420 frc971::wpilib::GenericCANWriter<control_loops::superstructure::Output>
421 can_superstructure_writer(
422 &can_output_event_loop,
423 [](const control_loops::superstructure::Output &output,
424 const std::map<std::string_view, std::shared_ptr<TalonFX>>
425 &talonfx_map) {
426 talonfx_map.find("intake_pivot")
427 ->second->WriteVoltage(output.intake_pivot_voltage());
428 talonfx_map.find("intake_roller")
429 ->second->WriteVoltage(output.intake_roller_voltage());
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800430 talonfx_map.find("transfer_roller")
431 ->second->WriteVoltage(output.transfer_roller_voltage());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800432 });
433
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800434 can_drivetrain_writer.set_talonfxs({right_front, right_back},
435 {left_front, left_back});
436
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800437 can_superstructure_writer.add_talonfx("intake_pivot", intake_pivot);
438 can_superstructure_writer.add_talonfx("intake_roller", intake_roller);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800439 can_superstructure_writer.add_talonfx("transfer_roller", transfer_roller);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800440
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800441 can_output_event_loop.MakeWatcher(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800442 "/roborio", [&can_drivetrain_writer, &can_superstructure_writer](
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800443 const frc971::CANConfiguration &configuration) {
444 can_drivetrain_writer.HandleCANConfiguration(configuration);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800445 can_superstructure_writer.HandleCANConfiguration(configuration);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800446 });
447
448 AddLoop(&can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800449
450 // Thread 6
451
452 RunLoops();
453 }
454};
455
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800456} // namespace y2024::wpilib
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800457
458AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot);