blob: b7082660c78c8d1d43a9da76dde545e628c808b2 [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(),
93});
94static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
95 "fast encoders are too fast");
96
97} // namespace
98
Niko Sohmers3860f8a2024-01-12 21:05:19 -080099// Class to send position messages with sensor readings to our loops.
100class SensorReader : public ::frc971::wpilib::SensorReader {
101 public:
102 SensorReader(::aos::ShmEventLoop *event_loop,
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800103 const Constants *robot_constants)
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800104 : ::frc971::wpilib::SensorReader(event_loop),
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800105 robot_constants_(CHECK_NOTNULL(robot_constants)),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800106 auto_mode_sender_(
107 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
108 "/autonomous")),
109 superstructure_position_sender_(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800110 event_loop->MakeSender<superstructure::PositionStatic>(
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800111 "/superstructure")),
112 drivetrain_position_sender_(
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800113 event_loop->MakeSender<
114 ::frc971::control_loops::drivetrain::PositionStatic>(
115 "/drivetrain")),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800116 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
117 "/drivetrain")){};
118 void Start() override { AddToDMA(&imu_yaw_rate_reader_); }
119
120 // Auto mode switches.
121 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
122 autonomous_modes_.at(i) = ::std::move(sensor);
123 }
124
125 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
126 imu_yaw_rate_input_ = ::std::move(sensor);
127 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
128 }
129
130 void RunIteration() override {
131 {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800132 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
133 superstructure_position_sender_.MakeStaticBuilder();
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800134
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800135 CopyPosition(intake_pivot_encoder_, builder->add_intake_pivot(),
136 Values::kIntakePivotEncoderCountsPerRevolution(),
137 Values::kIntakePivotEncoderRatio(), intake_pot_translate,
138 true,
139 robot_constants_->robot()
140 ->intake_constants()
141 ->potentiometer_offset());
142
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800143 builder->set_transfer_beambreak(transfer_beam_break_->Get());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800144 builder.CheckOk(builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800145 }
146
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800147 SendDrivetrainPosition(drivetrain_position_sender_.MakeStaticBuilder(),
148 drivetrain_velocity_translate,
149 constants::Values::DrivetrainEncoderToMeters, false,
150 false);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800151
152 {
153 auto builder = gyro_sender_.MakeBuilder();
154 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
155 builder.MakeBuilder<::frc971::sensors::GyroReading>();
156 // +/- 2000 deg / sec
157 constexpr double kMaxVelocity = 4000; // degrees / second
158 constexpr double kVelocityRadiansPerSecond =
159 kMaxVelocity / 360 * (2.0 * M_PI);
160
161 // Only part of the full range is used to prevent being 100% on or off.
162 constexpr double kScaledRangeLow = 0.1;
163 constexpr double kScaledRangeHigh = 0.9;
164
165 constexpr double kPWMFrequencyHz = 200;
166 double velocity_duty_cycle =
167 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
168
169 constexpr double kDutyCycleScale =
170 1 / (kScaledRangeHigh - kScaledRangeLow);
171 // scale from 0.1 - 0.9 to 0 - 1
172 double rescaled_velocity_duty_cycle =
173 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
174
175 if (!std::isnan(rescaled_velocity_duty_cycle)) {
176 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
177 kVelocityRadiansPerSecond);
178 }
179 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
180 }
181
182 {
183 auto builder = auto_mode_sender_.MakeBuilder();
184
185 uint32_t mode = 0;
186 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
187 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
188 mode |= 1 << i;
189 }
190 }
191
192 auto auto_mode_builder =
193 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
194
195 auto_mode_builder.add_mode(mode);
196
197 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
198 }
199 }
200
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800201 void set_intake_pivot_encoder(::std::unique_ptr<frc::Encoder> encoder) {
202 fast_encoder_filter_.Add(encoder.get());
203 intake_pivot_encoder_.set_encoder(::std::move(encoder));
204 }
205
206 void set_intake_pivot_absolute_pwm(
207 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
208 intake_pivot_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
209 }
210
211 void set_intake_pivot_potentiometer(
212 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
213 intake_pivot_encoder_.set_potentiometer(::std::move(potentiometer));
214 }
215
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800216 void set_transfer_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
217 transfer_beam_break_ = ::std::move(sensor);
218 }
219
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800220 private:
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800221 const Constants *robot_constants_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800222
223 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800224 aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800225 aos::Sender<frc971::control_loops::drivetrain::PositionStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800226 drivetrain_position_sender_;
227 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
228
229 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
230
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800231 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_, transfer_beam_break_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800232
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800233 frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_pivot_encoder_;
234
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800235 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
236};
237
238class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
239 public:
240 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
241 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
242 frc::Encoder::k4X);
243 }
244
245 void Run() override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800246 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
247 aos::configuration::ReadConfig("aos_config.json");
248
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800249 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
250
251 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
252 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(
253 &constant_fetcher_event_loop);
254 const Constants *robot_constants = &constants_fetcher.constants();
255
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800256 // Thread 1.
257 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
258 ::frc971::wpilib::JoystickSender joystick_sender(
259 &joystick_sender_event_loop);
260 AddLoop(&joystick_sender_event_loop);
261
262 // Thread 2.
263 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
264 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
265 AddLoop(&pdp_fetcher_event_loop);
266
267 // Thread 3.
268 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800269 SensorReader sensor_reader(&sensor_reader_event_loop, robot_constants);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800270 sensor_reader.set_pwm_trigger(true);
271 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
272 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
273 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800274 // TODO: (niko) change values once robot is wired
275 sensor_reader.set_intake_pivot_encoder(make_encoder(4));
276 sensor_reader.set_intake_pivot_absolute_pwm(
277 make_unique<frc::DigitalInput>(4));
278 sensor_reader.set_intake_pivot_potentiometer(
279 make_unique<frc::AnalogInput>(4));
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800280 sensor_reader.set_transfer_beambreak(make_unique<frc::DigitalInput>(7));
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800281
282 AddLoop(&sensor_reader_event_loop);
283
284 // Thread 4.
285 // Set up CAN.
286 if (!FLAGS_ctre_diag_server) {
287 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
288 c_Phoenix_Diagnostics_Dispose();
289 }
290
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800291 std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry;
292
293 std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>(
294 0, false, "Drivetrain Bus", &signals_registry,
295 constants::Values::kDrivetrainStatorCurrentLimit(),
296 constants::Values::kDrivetrainSupplyCurrentLimit());
297 std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>(
298 1, false, "Drivetrain Bus", &signals_registry,
299 constants::Values::kDrivetrainStatorCurrentLimit(),
300 constants::Values::kDrivetrainSupplyCurrentLimit());
301 std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>(
302 2, false, "Drivetrain Bus", &signals_registry,
303 constants::Values::kDrivetrainStatorCurrentLimit(),
304 constants::Values::kDrivetrainSupplyCurrentLimit());
305 std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>(
306 3, false, "Drivetrain Bus", &signals_registry,
307 constants::Values::kDrivetrainStatorCurrentLimit(),
308 constants::Values::kDrivetrainSupplyCurrentLimit());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800309 std::shared_ptr<TalonFX> intake_pivot =
310 std::make_shared<TalonFX>(4, false, "Drivetrain Bus", &signals_registry,
311 robot_constants->common()
312 ->current_limits()
313 ->intake_pivot_stator_current_limit(),
314 robot_constants->common()
315 ->current_limits()
316 ->intake_pivot_supply_current_limit());
317 std::shared_ptr<TalonFX> intake_roller =
318 std::make_shared<TalonFX>(5, false, "Drivetrain Bus", &signals_registry,
319 robot_constants->common()
320 ->current_limits()
321 ->intake_roller_stator_current_limit(),
322 robot_constants->common()
323 ->current_limits()
324 ->intake_roller_supply_current_limit());
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800325 std::shared_ptr<TalonFX> transfer_roller =
326 std::make_shared<TalonFX>(6, false, "Drivetrain Bus", &signals_registry,
327 robot_constants->common()
328 ->current_limits()
329 ->transfer_roller_stator_current_limit(),
330 robot_constants->common()
331 ->current_limits()
332 ->transfer_roller_supply_current_limit());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800333 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
334 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
335 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
336 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
337
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800338 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
339 can_sensor_reader_event_loop.set_name("CANSensorReader");
340
341 // Creating list of talonfx for CANSensorReader
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800342 std::vector<std::shared_ptr<TalonFX>> drivetrain_talonfxs;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800343 std::vector<std::shared_ptr<TalonFX>> talonfxs;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800344
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800345 for (auto talonfx : {right_front, right_back, left_front, left_back}) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800346 drivetrain_talonfxs.push_back(talonfx);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800347 talonfxs.push_back(talonfx);
348 }
349
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800350 for (auto talonfx : {intake_pivot, intake_roller, transfer_roller}) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800351 talonfxs.push_back(talonfx);
352 }
353
354 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>
355 drivetrain_can_position_sender =
356 can_sensor_reader_event_loop.MakeSender<
357 frc971::control_loops::drivetrain::CANPositionStatic>(
358 "/drivetrain");
359
360 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
361 superstructure_can_position_sender =
362 can_sensor_reader_event_loop.MakeSender<
363 y2024::control_loops::superstructure::CANPositionStatic>(
364 "/superstructure");
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800365
366 frc971::wpilib::CANSensorReader can_sensor_reader(
367 &can_sensor_reader_event_loop, std::move(signals_registry), talonfxs,
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800368 [drivetrain_talonfxs, &intake_pivot, &intake_roller, &transfer_roller,
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800369 &drivetrain_can_position_sender, &superstructure_can_position_sender](
370 ctre::phoenix::StatusCode status) {
371 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>::
372 StaticBuilder drivetrain_can_builder =
373 drivetrain_can_position_sender.MakeStaticBuilder();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800374
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800375 auto drivetrain_falcon_vector =
376 drivetrain_can_builder->add_talonfxs();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800377
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800378 for (auto talonfx : drivetrain_talonfxs) {
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800379 talonfx->SerializePosition(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800380 drivetrain_falcon_vector->emplace_back(),
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800381 control_loops::drivetrain::kHighOutputRatio);
382 }
383
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800384 drivetrain_can_builder->set_timestamp(
385 drivetrain_talonfxs.front()->GetTimestamp());
386 drivetrain_can_builder->set_status(static_cast<int>(status));
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800387
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800388 drivetrain_can_builder.CheckOk(drivetrain_can_builder.Send());
389
390 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
391 StaticBuilder superstructure_can_builder =
392 superstructure_can_position_sender.MakeStaticBuilder();
393
394 intake_roller->SerializePosition(
395 superstructure_can_builder->add_intake_roller(),
396 control_loops::drivetrain::kHighOutputRatio);
397 intake_pivot->SerializePosition(
398 superstructure_can_builder->add_intake_pivot(),
399 control_loops::drivetrain::kHighOutputRatio);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800400 transfer_roller->SerializePosition(
401 superstructure_can_builder->add_transfer_roller(),
402 control_loops::drivetrain::kHighOutputRatio);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800403
404 superstructure_can_builder->set_timestamp(
405 intake_roller->GetTimestamp());
406 superstructure_can_builder->set_status(static_cast<int>(status));
407 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800408 });
409
410 AddLoop(&can_sensor_reader_event_loop);
411
412 // Thread 5.
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800413 ::aos::ShmEventLoop can_output_event_loop(&config.message());
414 can_output_event_loop.set_name("CANOutputWriter");
415
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800416 frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer(
417 &can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800418
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800419 frc971::wpilib::GenericCANWriter<control_loops::superstructure::Output>
420 can_superstructure_writer(
421 &can_output_event_loop,
422 [](const control_loops::superstructure::Output &output,
423 const std::map<std::string_view, std::shared_ptr<TalonFX>>
424 &talonfx_map) {
425 talonfx_map.find("intake_pivot")
426 ->second->WriteVoltage(output.intake_pivot_voltage());
427 talonfx_map.find("intake_roller")
428 ->second->WriteVoltage(output.intake_roller_voltage());
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800429 talonfx_map.find("transfer_roller")
430 ->second->WriteVoltage(output.transfer_roller_voltage());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800431 });
432
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800433 can_drivetrain_writer.set_talonfxs({right_front, right_back},
434 {left_front, left_back});
435
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800436 can_superstructure_writer.add_talonfx("intake_pivot", intake_pivot);
437 can_superstructure_writer.add_talonfx("intake_roller", intake_roller);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800438 can_superstructure_writer.add_talonfx("transfer_roller", transfer_roller);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800439
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800440 can_output_event_loop.MakeWatcher(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800441 "/roborio", [&can_drivetrain_writer, &can_superstructure_writer](
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800442 const frc971::CANConfiguration &configuration) {
443 can_drivetrain_writer.HandleCANConfiguration(configuration);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800444 can_superstructure_writer.HandleCANConfiguration(configuration);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800445 });
446
447 AddLoop(&can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800448
449 // Thread 6
450
451 RunLoops();
452 }
453};
454
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800455} // namespace y2024::wpilib
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800456
457AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot);