blob: 6e7538794526bd25375fc8f20d24e4d632b656c1 [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
Filip Kujawa749f2442024-02-04 01:12:35 -080080double climber_pot_translate(double voltage) {
Maxwell Hendersonce232a92024-02-18 12:17:37 -080081 return voltage * Values::kClimberPotMetersPerVolt();
Filip Kujawa749f2442024-02-04 01:12:35 -080082}
83
Filip Kujawad75252a2024-02-10 16:54:35 -080084double extend_pot_translate(double voltage) {
85 return voltage * Values::kExtendPotMetersPerVolt();
86}
87
Niko Sohmers27d92c62024-02-19 14:15:07 -080088double catapult_pot_translate(double voltage) {
89 return voltage * Values::kCatapultPotRadiansPerVolt();
90}
91
92double turret_pot_translate(double voltage) {
93 return voltage * Values::kTurretPotRadiansPerVolt();
94}
95
96double altitude_pot_translate(double voltage) {
97 return voltage * Values::kAltitudePotRadiansPerVolt();
98}
99
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800100double drivetrain_velocity_translate(double in) {
101 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
102 (2.0 * M_PI)) *
103 Values::kDrivetrainEncoderRatio() *
104 control_loops::drivetrain::kWheelRadius;
105}
106
107constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
108 Values::kMaxDrivetrainEncoderPulsesPerSecond(),
Maxwell Henderson1de15492024-02-07 11:09:47 -0800109 Values::kMaxIntakePivotEncoderPulsesPerSecond(),
Filip Kujawa749f2442024-02-04 01:12:35 -0800110 Values::kMaxClimberEncoderPulsesPerSecond(),
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_);
144 AddToDMA(&turret_encoder_.reader());
145 AddToDMA(&altitude_encoder_.reader());
146 }
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800147
148 // Auto mode switches.
149 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
150 autonomous_modes_.at(i) = ::std::move(sensor);
151 }
152
153 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
154 imu_yaw_rate_input_ = ::std::move(sensor);
155 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
156 }
157
158 void RunIteration() override {
159 {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800160 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
161 superstructure_position_sender_.MakeStaticBuilder();
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800162
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800163 CopyPosition(intake_pivot_encoder_, builder->add_intake_pivot(),
164 Values::kIntakePivotEncoderCountsPerRevolution(),
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800165 Values::kIntakePivotEncoderRatio(), /* reversed: */ false);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800166
Filip Kujawa749f2442024-02-04 01:12:35 -0800167 CopyPosition(climber_encoder_, builder->add_climber(),
168 Values::kClimberEncoderCountsPerRevolution(),
Maxwell Hendersonce232a92024-02-18 12:17:37 -0800169 Values::kClimberEncoderMetersPerRevolution(),
170 climber_pot_translate, true,
Filip Kujawa749f2442024-02-04 01:12:35 -0800171 robot_constants_->robot()
172 ->climber_constants()
173 ->potentiometer_offset());
174
Filip Kujawad75252a2024-02-10 16:54:35 -0800175 CopyPosition(extend_encoder_, builder->add_extend(),
176 Values::kClimberEncoderCountsPerRevolution(),
177 Values::kClimberEncoderMetersPerRevolution(),
178 extend_pot_translate, true,
179 robot_constants_->robot()
180 ->extend_constants()
181 ->potentiometer_offset());
182
Niko Sohmers27d92c62024-02-19 14:15:07 -0800183 CopyPosition(catapult_encoder_, builder->add_catapult(),
184 Values::kCatapultEncoderCountsPerRevolution(),
185 Values::kCatapultEncoderRatio(), catapult_pot_translate,
186 true,
187 robot_constants_->robot()
188 ->catapult_constants()
189 ->potentiometer_offset());
190
191 CopyPosition(turret_encoder_, builder->add_turret(),
192 Values::kTurretEncoderCountsPerRevolution(),
193 Values::kTurretEncoderRatio(), turret_pot_translate, true,
194 robot_constants_->robot()
195 ->turret_constants()
196 ->potentiometer_offset());
197
198 CopyPosition(altitude_encoder_, builder->add_altitude(),
199 Values::kAltitudeEncoderCountsPerRevolution(),
200 Values::kAltitudeEncoderRatio(), altitude_pot_translate,
201 true,
202 robot_constants_->robot()
203 ->altitude_constants()
204 ->potentiometer_offset());
205
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800206 builder->set_transfer_beambreak(transfer_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,
212 constants::Values::DrivetrainEncoderToMeters, false,
213 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
Maxwell Henderson62917832024-02-07 11:08:11 -0800264 void set_intake_pivot(::std::unique_ptr<frc::Encoder> encoder,
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800265 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800266 fast_encoder_filter_.Add(encoder.get());
267 intake_pivot_encoder_.set_encoder(::std::move(encoder));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800268 intake_pivot_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800269 }
270
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800271 void set_transfer_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
272 transfer_beam_break_ = ::std::move(sensor);
273 }
274
Filip Kujawa749f2442024-02-04 01:12:35 -0800275 void set_climber(::std::unique_ptr<frc::Encoder> encoder,
276 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
277 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
278 fast_encoder_filter_.Add(encoder.get());
279 climber_encoder_.set_encoder(::std::move(encoder));
280 climber_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
281 climber_encoder_.set_potentiometer(::std::move(potentiometer));
282 }
283
Filip Kujawad75252a2024-02-10 16:54:35 -0800284 void set_extend(::std::unique_ptr<frc::Encoder> encoder,
285 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
286 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
287 fast_encoder_filter_.Add(encoder.get());
288 extend_encoder_.set_encoder(::std::move(encoder));
289 extend_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
290 extend_encoder_.set_potentiometer(::std::move(potentiometer));
291 }
292
Niko Sohmers27d92c62024-02-19 14:15:07 -0800293 void set_catapult(::std::unique_ptr<frc::Encoder> encoder,
294 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
295 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
296 fast_encoder_filter_.Add(encoder.get());
297 catapult_encoder_.set_encoder(::std::move(encoder));
298 catapult_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
299 catapult_encoder_.set_potentiometer(::std::move(potentiometer));
300 }
301
302 void set_turret(::std::unique_ptr<frc::Encoder> encoder,
303 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
304 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
305 fast_encoder_filter_.Add(encoder.get());
306 turret_encoder_.set_encoder(::std::move(encoder));
307 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
308 turret_encoder_.set_potentiometer(::std::move(potentiometer));
309 }
310
311 void set_altitude(::std::unique_ptr<frc::Encoder> encoder,
312 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
313 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
314 fast_encoder_filter_.Add(encoder.get());
315 altitude_encoder_.set_encoder(::std::move(encoder));
316 altitude_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
317 altitude_encoder_.set_potentiometer(::std::move(potentiometer));
318 }
319
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800320 private:
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800321 const Constants *robot_constants_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800322
323 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800324 aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800325 aos::Sender<frc971::control_loops::drivetrain::PositionStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800326 drivetrain_position_sender_;
327 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
328
329 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
330
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800331 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_, transfer_beam_break_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800332
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800333 frc971::wpilib::AbsoluteEncoder intake_pivot_encoder_;
Niko Sohmers27d92c62024-02-19 14:15:07 -0800334 frc971::wpilib::AbsoluteEncoderAndPotentiometer climber_encoder_,
Maxwell Hendersonafc5c6d2024-02-20 15:34:13 -0800335 catapult_encoder_, extend_encoder_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800336
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800337 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
Maxwell Hendersonafc5c6d2024-02-20 15:34:13 -0800338
339 frc971::wpilib::DMAAbsoluteEncoderAndPotentiometer turret_encoder_,
340 altitude_encoder_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800341};
342
Maxwell Hendersonabcc92a2024-02-21 19:06:26 -0800343class SuperstructurePWMWriter
344 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
345 public:
346 SuperstructurePWMWriter(aos::EventLoop *event_loop)
347 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
348 event_loop, "/superstructure") {}
349
350 void set_catapult_kraken_one(::std::unique_ptr<::frc::TalonFX> t) {
351 catapult_kraken_one_ = ::std::move(t);
352 }
353 void set_catapult_kraken_two(::std::unique_ptr<::frc::TalonFX> t) {
354 catapult_kraken_two_ = ::std::move(t);
355 }
356
357 private:
358 void Stop() override {
359 AOS_LOG(WARNING, "Superstructure output too old.\n");
360 catapult_kraken_one_->SetDisabled();
361 catapult_kraken_two_->SetDisabled();
362 }
363
364 void Write(const superstructure::Output &output) override {
365 WritePwm(output.catapult_voltage(), catapult_kraken_one_.get());
366 WritePwm(output.catapult_voltage(), catapult_kraken_one_.get());
367 }
368
369 template <typename T>
370 static void WritePwm(const double voltage, T *motor) {
371 motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) /
372 12.0);
373 }
374 ::std::unique_ptr<::frc::TalonFX> catapult_kraken_one_, catapult_kraken_two_;
375};
376
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800377class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
378 public:
379 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
380 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
381 frc::Encoder::k4X);
382 }
383
384 void Run() override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800385 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
386 aos::configuration::ReadConfig("aos_config.json");
387
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800388 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
389
390 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
391 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(
392 &constant_fetcher_event_loop);
393 const Constants *robot_constants = &constants_fetcher.constants();
394
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800395 // Thread 1.
396 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
397 ::frc971::wpilib::JoystickSender joystick_sender(
398 &joystick_sender_event_loop);
399 AddLoop(&joystick_sender_event_loop);
400
401 // Thread 2.
402 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
403 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
404 AddLoop(&pdp_fetcher_event_loop);
405
406 // Thread 3.
407 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800408 SensorReader sensor_reader(&sensor_reader_event_loop, robot_constants);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800409 sensor_reader.set_pwm_trigger(true);
410 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
411 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
412 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800413 // TODO: (niko) change values once robot is wired
Maxwell Henderson62917832024-02-07 11:08:11 -0800414 sensor_reader.set_intake_pivot(make_encoder(4),
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800415 make_unique<frc::DigitalInput>(4));
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800416 sensor_reader.set_transfer_beambreak(make_unique<frc::DigitalInput>(7));
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800417
Filip Kujawa749f2442024-02-04 01:12:35 -0800418 sensor_reader.set_climber(make_encoder(5),
419 make_unique<frc::DigitalInput>(5),
420 make_unique<frc::AnalogInput>(5));
Niko Sohmers27d92c62024-02-19 14:15:07 -0800421 sensor_reader.set_extend(make_encoder(7), make_unique<frc::DigitalInput>(7),
422 make_unique<frc::AnalogInput>(7));
423 sensor_reader.set_catapult(make_encoder(2),
424 make_unique<frc::DigitalInput>(2),
425 make_unique<frc::AnalogInput>(2));
426 sensor_reader.set_turret(make_encoder(3), make_unique<frc::DigitalInput>(3),
427 make_unique<frc::AnalogInput>(3));
428 sensor_reader.set_altitude(make_encoder(6),
429 make_unique<frc::DigitalInput>(6),
430 make_unique<frc::AnalogInput>(6));
Filip Kujawad75252a2024-02-10 16:54:35 -0800431
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800432 AddLoop(&sensor_reader_event_loop);
433
434 // Thread 4.
435 // Set up CAN.
436 if (!FLAGS_ctre_diag_server) {
437 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
438 c_Phoenix_Diagnostics_Dispose();
439 }
440
Niko Sohmers84273952024-02-14 18:40:55 -0800441 std::vector<ctre::phoenix6::BaseStatusSignal *> canivore_signal_registry;
442 std::vector<ctre::phoenix6::BaseStatusSignal *> rio_signal_registry;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800443
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800444 const CurrentLimits *current_limits =
445 robot_constants->common()->current_limits();
446
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800447 std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800448 2, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800449 current_limits->drivetrain_supply_current_limit(),
450 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800451 std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800452 1, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800453 current_limits->drivetrain_supply_current_limit(),
454 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800455 std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800456 4, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800457 current_limits->drivetrain_supply_current_limit(),
458 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800459 std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800460 5, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800461 current_limits->drivetrain_supply_current_limit(),
462 current_limits->drivetrain_stator_current_limit());
463 std::shared_ptr<TalonFX> intake_pivot = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800464 3, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800465 current_limits->intake_pivot_stator_current_limit(),
466 current_limits->intake_pivot_supply_current_limit());
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800467 // TODO(max): Assign these proper ids
Niko Sohmers27d92c62024-02-19 14:15:07 -0800468 std::shared_ptr<TalonFX> altitude = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800469 6, false, "Drivetrain Bus", &canivore_signal_registry,
Niko Sohmers27d92c62024-02-19 14:15:07 -0800470 current_limits->altitude_stator_current_limit(),
471 current_limits->altitude_supply_current_limit());
472 std::shared_ptr<TalonFX> turret = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800473 7, false, "Drivetrain Bus", &canivore_signal_registry,
Niko Sohmers27d92c62024-02-19 14:15:07 -0800474 current_limits->turret_stator_current_limit(),
475 current_limits->turret_supply_current_limit());
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800476 std::shared_ptr<TalonFX> intake_roller = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800477 8, false, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800478 current_limits->intake_roller_stator_current_limit(),
479 current_limits->intake_roller_supply_current_limit());
480 std::shared_ptr<TalonFX> transfer_roller = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800481 9, false, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800482 current_limits->transfer_roller_stator_current_limit(),
483 current_limits->transfer_roller_supply_current_limit());
Filip Kujawa749f2442024-02-04 01:12:35 -0800484 std::shared_ptr<TalonFX> climber = std::make_shared<TalonFX>(
Maxwell Henderson7be07d52024-02-20 07:59:16 -0800485 10, false, "rio", &rio_signal_registry,
Filip Kujawa749f2442024-02-04 01:12:35 -0800486 current_limits->climber_stator_current_limit(),
487 current_limits->climber_supply_current_limit());
488
Filip Kujawad75252a2024-02-10 16:54:35 -0800489 std::shared_ptr<TalonFX> extend = std::make_shared<TalonFX>(
Niko Sohmers27d92c62024-02-19 14:15:07 -0800490 8, false, "rio", &rio_signal_registry,
Filip Kujawad75252a2024-02-10 16:54:35 -0800491 current_limits->extend_stator_current_limit(),
492 current_limits->extend_supply_current_limit());
493
494 std::shared_ptr<TalonFX> extend_roller = std::make_shared<TalonFX>(
Niko Sohmers27d92c62024-02-19 14:15:07 -0800495 9, false, "rio", &rio_signal_registry,
Filip Kujawad75252a2024-02-10 16:54:35 -0800496 current_limits->extend_roller_stator_current_limit(),
497 current_limits->extend_roller_supply_current_limit());
498
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800499 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
500 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
501 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
502 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
503
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800504 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
505 can_sensor_reader_event_loop.set_name("CANSensorReader");
506
Niko Sohmers84273952024-02-14 18:40:55 -0800507 ::aos::ShmEventLoop rio_sensor_reader_event_loop(&config.message());
508 rio_sensor_reader_event_loop.set_name("RioSensorReader");
509
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800510 // Creating list of talonfx for CANSensorReader
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800511 std::vector<std::shared_ptr<TalonFX>> drivetrain_talonfxs;
Niko Sohmers84273952024-02-14 18:40:55 -0800512 std::vector<std::shared_ptr<TalonFX>> canivore_talonfxs;
513 std::vector<std::shared_ptr<TalonFX>> rio_talonfxs;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800514
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800515 for (auto talonfx : {right_front, right_back, left_front, left_back}) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800516 drivetrain_talonfxs.push_back(talonfx);
Niko Sohmers84273952024-02-14 18:40:55 -0800517 canivore_talonfxs.push_back(talonfx);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800518 }
519
Niko Sohmers27d92c62024-02-19 14:15:07 -0800520 for (auto talonfx : {intake_pivot, altitude, turret}) {
Niko Sohmers84273952024-02-14 18:40:55 -0800521 canivore_talonfxs.push_back(talonfx);
522 }
523
Filip Kujawad75252a2024-02-10 16:54:35 -0800524 for (auto talonfx :
525 {intake_roller, transfer_roller, climber, extend, extend_roller}) {
Niko Sohmers84273952024-02-14 18:40:55 -0800526 rio_talonfxs.push_back(talonfx);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800527 }
528
529 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>
530 drivetrain_can_position_sender =
531 can_sensor_reader_event_loop.MakeSender<
532 frc971::control_loops::drivetrain::CANPositionStatic>(
533 "/drivetrain");
534
535 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
536 superstructure_can_position_sender =
537 can_sensor_reader_event_loop.MakeSender<
538 y2024::control_loops::superstructure::CANPositionStatic>(
Niko Sohmers84273952024-02-14 18:40:55 -0800539 "/superstructure/canivore");
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800540
Niko Sohmers84273952024-02-14 18:40:55 -0800541 frc971::wpilib::CANSensorReader canivore_can_sensor_reader(
542 &can_sensor_reader_event_loop, std::move(canivore_signal_registry),
543 canivore_talonfxs,
Niko Sohmers27d92c62024-02-19 14:15:07 -0800544 [drivetrain_talonfxs, &intake_pivot, &altitude, &turret,
545 &drivetrain_can_position_sender, &superstructure_can_position_sender](
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800546 ctre::phoenix::StatusCode status) {
547 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>::
548 StaticBuilder drivetrain_can_builder =
549 drivetrain_can_position_sender.MakeStaticBuilder();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800550
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800551 auto drivetrain_falcon_vector =
Maxwell Henderson563efed2024-02-17 21:11:33 -0800552 CHECK_NOTNULL(drivetrain_can_builder->add_talonfxs());
Maxwell Henderson9116e5b2024-01-21 12:14:26 -0800553
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800554 for (auto talonfx : drivetrain_talonfxs) {
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800555 talonfx->SerializePosition(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800556 drivetrain_falcon_vector->emplace_back(),
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800557 control_loops::drivetrain::kHighOutputRatio);
558 }
559
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800560 drivetrain_can_builder->set_timestamp(
561 drivetrain_talonfxs.front()->GetTimestamp());
562 drivetrain_can_builder->set_status(static_cast<int>(status));
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800563
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800564 drivetrain_can_builder.CheckOk(drivetrain_can_builder.Send());
565
566 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
567 StaticBuilder superstructure_can_builder =
568 superstructure_can_position_sender.MakeStaticBuilder();
569
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800570 intake_pivot->SerializePosition(
571 superstructure_can_builder->add_intake_pivot(),
Niko Sohmers27d92c62024-02-19 14:15:07 -0800572 control_loops::drivetrain::kHighOutputRatio);
573 altitude->SerializePosition(
574 superstructure_can_builder->add_altitude(),
575 control_loops::drivetrain::kHighOutputRatio);
576 turret->SerializePosition(
577 superstructure_can_builder->add_turret(),
578 control_loops::drivetrain::kHighOutputRatio);
Niko Sohmers84273952024-02-14 18:40:55 -0800579
580 superstructure_can_builder->set_timestamp(
581 intake_pivot->GetTimestamp());
582 superstructure_can_builder->set_status(static_cast<int>(status));
583 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
584 });
585
586 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
587 superstructure_rio_position_sender =
588 rio_sensor_reader_event_loop.MakeSender<
589 y2024::control_loops::superstructure::CANPositionStatic>(
590 "/superstructure/rio");
591
592 frc971::wpilib::CANSensorReader rio_can_sensor_reader(
593 &rio_sensor_reader_event_loop, std::move(rio_signal_registry),
594 rio_talonfxs,
Filip Kujawad75252a2024-02-10 16:54:35 -0800595 [&intake_roller, &transfer_roller, &climber, &extend, &extend_roller,
Niko Sohmers84273952024-02-14 18:40:55 -0800596 &superstructure_rio_position_sender](
597 ctre::phoenix::StatusCode status) {
598 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
599 StaticBuilder superstructure_can_builder =
600 superstructure_rio_position_sender.MakeStaticBuilder();
601
602 intake_roller->SerializePosition(
603 superstructure_can_builder->add_intake_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800604 constants::Values::kIntakeRollerOutputRatio);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800605 transfer_roller->SerializePosition(
606 superstructure_can_builder->add_transfer_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800607 constants::Values::kIntakeRollerOutputRatio);
608 climber->SerializePosition(superstructure_can_builder->add_climber(),
609 superstructure::climber::kOutputRatio);
Filip Kujawad75252a2024-02-10 16:54:35 -0800610 extend->SerializePosition(superstructure_can_builder->add_extend(),
611 superstructure::extend::kOutputRatio);
612 extend_roller->SerializePosition(
613 superstructure_can_builder->add_extend_roller(),
Filip Kujawace385c32024-02-16 10:39:49 -0800614 constants::Values::kExtendRollerOutputRatio);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800615
616 superstructure_can_builder->set_timestamp(
617 intake_roller->GetTimestamp());
618 superstructure_can_builder->set_status(static_cast<int>(status));
619 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800620 });
621
622 AddLoop(&can_sensor_reader_event_loop);
Niko Sohmers84273952024-02-14 18:40:55 -0800623 AddLoop(&rio_sensor_reader_event_loop);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800624
625 // Thread 5.
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800626 ::aos::ShmEventLoop can_output_event_loop(&config.message());
627 can_output_event_loop.set_name("CANOutputWriter");
628
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800629 frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer(
630 &can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800631
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800632 frc971::wpilib::GenericCANWriter<control_loops::superstructure::Output>
633 can_superstructure_writer(
634 &can_output_event_loop,
635 [](const control_loops::superstructure::Output &output,
636 const std::map<std::string_view, std::shared_ptr<TalonFX>>
637 &talonfx_map) {
638 talonfx_map.find("intake_pivot")
639 ->second->WriteVoltage(output.intake_pivot_voltage());
640 talonfx_map.find("intake_roller")
641 ->second->WriteVoltage(output.intake_roller_voltage());
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800642 talonfx_map.find("transfer_roller")
643 ->second->WriteVoltage(output.transfer_roller_voltage());
Filip Kujawa749f2442024-02-04 01:12:35 -0800644 talonfx_map.find("climber")->second->WriteVoltage(
645 output.climber_voltage());
Filip Kujawad75252a2024-02-10 16:54:35 -0800646 talonfx_map.find("extend")->second->WriteVoltage(
647 output.extend_voltage());
648 talonfx_map.find("extend_roller")
649 ->second->WriteVoltage(output.extend_roller_voltage());
Niko Sohmers27d92c62024-02-19 14:15:07 -0800650 talonfx_map.find("altitude")
651 ->second->WriteVoltage(output.altitude_voltage());
652 talonfx_map.find("turret")->second->WriteVoltage(
653 output.turret_voltage());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800654 });
655
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800656 can_drivetrain_writer.set_talonfxs({right_front, right_back},
657 {left_front, left_back});
658
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800659 can_superstructure_writer.add_talonfx("intake_pivot", intake_pivot);
660 can_superstructure_writer.add_talonfx("intake_roller", intake_roller);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800661 can_superstructure_writer.add_talonfx("transfer_roller", transfer_roller);
Filip Kujawa749f2442024-02-04 01:12:35 -0800662 can_superstructure_writer.add_talonfx("climber", climber);
Filip Kujawad75252a2024-02-10 16:54:35 -0800663 can_superstructure_writer.add_talonfx("extend", extend);
664 can_superstructure_writer.add_talonfx("extend_roller", extend_roller);
Niko Sohmers27d92c62024-02-19 14:15:07 -0800665 can_superstructure_writer.add_talonfx("altitude", altitude);
666 can_superstructure_writer.add_talonfx("turret", turret);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800667
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800668 can_output_event_loop.MakeWatcher(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800669 "/roborio", [&can_drivetrain_writer, &can_superstructure_writer](
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800670 const frc971::CANConfiguration &configuration) {
671 can_drivetrain_writer.HandleCANConfiguration(configuration);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800672 can_superstructure_writer.HandleCANConfiguration(configuration);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800673 });
674
675 AddLoop(&can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800676
Maxwell Hendersonabcc92a2024-02-21 19:06:26 -0800677 ::aos::ShmEventLoop pwm_event_loop(&config.message());
678 SuperstructurePWMWriter superstructure_pwm_writer(&pwm_event_loop);
679 superstructure_pwm_writer.set_catapult_kraken_one(
680 make_unique<frc::TalonFX>(0));
681 superstructure_pwm_writer.set_catapult_kraken_one(
682 make_unique<frc::TalonFX>(1));
683
684 AddLoop(&pwm_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800685 // Thread 6
686
687 RunLoops();
688 }
689};
690
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800691} // namespace y2024::wpilib
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800692
693AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot);