blob: dd2224cdac726c24fd6e4310f4124c16d691b465 [file] [log] [blame]
milind-u086d7262022-01-19 20:44:18 -08001#include <unistd.h>
2
3#include <array>
4#include <chrono>
5#include <cinttypes>
6#include <cmath>
7#include <cstdio>
8#include <cstring>
9#include <functional>
10#include <memory>
11#include <mutex>
12#include <thread>
13
14#include "ctre/phoenix/CANifier.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070015
milind-u086d7262022-01-19 20:44:18 -080016#include "frc971/wpilib/ahal/AnalogInput.h"
17#include "frc971/wpilib/ahal/Counter.h"
18#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
19#include "frc971/wpilib/ahal/DriverStation.h"
20#include "frc971/wpilib/ahal/Encoder.h"
milind-u6e7d8d42022-04-06 18:30:43 -070021#include "frc971/wpilib/ahal/Servo.h"
milind-u086d7262022-01-19 20:44:18 -080022#include "frc971/wpilib/ahal/TalonFX.h"
23#include "frc971/wpilib/ahal/VictorSP.h"
24#undef ERROR
25
Maxwell Henderson1c0843c2023-12-22 16:20:59 -080026#include "ctre/phoenix6/TalonFX.hpp"
Philipp Schrader790cb542023-07-05 21:06:52 -070027
milind-u086d7262022-01-19 20:44:18 -080028#include "aos/commonmath.h"
29#include "aos/events/event_loop.h"
30#include "aos/events/shm_event_loop.h"
31#include "aos/init.h"
32#include "aos/logging/logging.h"
33#include "aos/realtime.h"
34#include "aos/time/time.h"
35#include "aos/util/log_interval.h"
36#include "aos/util/phased_loop.h"
37#include "aos/util/wrapping_counter.h"
milind-u086d7262022-01-19 20:44:18 -080038#include "frc971/autonomous/auto_mode_generated.h"
39#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
40#include "frc971/input/robot_state_generated.h"
Ravago Jones0e86e242022-02-12 18:38:14 -080041#include "frc971/queues/gyro_generated.h"
milind-u086d7262022-01-19 20:44:18 -080042#include "frc971/wpilib/ADIS16448.h"
43#include "frc971/wpilib/buffered_pcm.h"
44#include "frc971/wpilib/buffered_solenoid.h"
45#include "frc971/wpilib/dma.h"
46#include "frc971/wpilib/drivetrain_writer.h"
47#include "frc971/wpilib/encoder_and_potentiometer.h"
48#include "frc971/wpilib/joystick_sender.h"
49#include "frc971/wpilib/logging_generated.h"
50#include "frc971/wpilib/loop_output_handler.h"
51#include "frc971/wpilib/pdp_fetcher.h"
52#include "frc971/wpilib/sensor_reader.h"
53#include "frc971/wpilib/wpilib_robot_base.h"
54#include "y2022/constants.h"
Henry Speiser77747b72022-03-06 17:18:29 -080055#include "y2022/control_loops/superstructure/led_indicator.h"
Milind Upadhyay482b0ba2022-02-26 21:51:59 -080056#include "y2022/control_loops/superstructure/superstructure_can_position_generated.h"
milind-u086d7262022-01-19 20:44:18 -080057#include "y2022/control_loops/superstructure/superstructure_output_generated.h"
Maxwell Hendersoncb78f352024-01-15 00:27:16 -080058#include "y2022/control_loops/superstructure/superstructure_position_static.h"
milind-u086d7262022-01-19 20:44:18 -080059
60using ::aos::monotonic_clock;
61using ::y2022::constants::Values;
62namespace superstructure = ::y2022::control_loops::superstructure;
63namespace chrono = ::std::chrono;
64using std::make_unique;
65
Austin Schuhda7e3e12022-03-26 15:14:31 -070066DEFINE_bool(can_catapult, false, "If true, use CAN to control the catapult.");
67
Stephan Pleinesf63bde82024-01-13 15:59:33 -080068namespace y2022::wpilib {
milind-u086d7262022-01-19 20:44:18 -080069namespace {
70
71constexpr double kMaxBringupPower = 12.0;
72
73// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
74// DMA stuff and then removing the * 2.0 in *_translate.
75// The low bit is direction.
76
milind-u086d7262022-01-19 20:44:18 -080077double drivetrain_velocity_translate(double in) {
78 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
79 (2.0 * M_PI)) *
80 Values::kDrivetrainEncoderRatio() *
81 control_loops::drivetrain::kWheelRadius;
82}
83
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080084double climber_pot_translate(double voltage) {
Nathan Leong342b85e2023-01-08 13:49:33 -080085 return voltage * Values::kClimberPotMetersPerVolt();
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080086}
87
Griffin Buibcbef482022-02-23 15:32:10 -080088double flipper_arms_pot_translate(double voltage) {
Nathan Leong342b85e2023-01-08 13:49:33 -080089 return voltage * Values::kFlipperArmsPotRadiansPerVolt();
Griffin Buibcbef482022-02-23 15:32:10 -080090}
91
Henry Speiser55aa3ba2022-02-21 23:21:12 -080092double intake_pot_translate(double voltage) {
Nathan Leong342b85e2023-01-08 13:49:33 -080093 return voltage * Values::kIntakePotRadiansPerVolt();
Henry Speiser55aa3ba2022-02-21 23:21:12 -080094}
95
96double turret_pot_translate(double voltage) {
Nathan Leong342b85e2023-01-08 13:49:33 -080097 return voltage * Values::kTurretPotRadiansPerVolt();
Henry Speiser55aa3ba2022-02-21 23:21:12 -080098}
99
milind-u086d7262022-01-19 20:44:18 -0800100constexpr double kMaxFastEncoderPulsesPerSecond =
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800101 std::max({Values::kMaxDrivetrainEncoderPulsesPerSecond(),
102 Values::kMaxIntakeEncoderPulsesPerSecond()});
milind-u086d7262022-01-19 20:44:18 -0800103static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
104 "fast encoders are too fast");
105constexpr double kMaxMediumEncoderPulsesPerSecond =
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800106 Values::kMaxTurretEncoderPulsesPerSecond();
milind-u086d7262022-01-19 20:44:18 -0800107
108static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
109 "medium encoders are too fast");
110
Austin Schuh39f26f62022-02-24 21:34:46 -0800111double catapult_pot_translate(double voltage) {
112 return voltage * Values::kCatapultPotRatio() *
113 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
114}
115
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800116void PrintConfigs(ctre::phoenix6::hardware::TalonFX *talon) {
117 ctre::phoenix6::configs::TalonFXConfiguration configuration;
118 ctre::phoenix::StatusCode status =
119 talon->GetConfigurator().Refresh(configuration);
120 if (!status.IsOK()) {
121 AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s",
122 status.GetName(), status.GetDescription());
123 }
124 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
125}
126
127void WriteConfigs(ctre::phoenix6::hardware::TalonFX *talon,
128 double stator_current_limit, double supply_current_limit) {
129 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
130 current_limits.StatorCurrentLimit = stator_current_limit;
131 current_limits.StatorCurrentLimitEnable = true;
132 current_limits.SupplyCurrentLimit = supply_current_limit;
133 current_limits.SupplyCurrentLimitEnable = true;
134
135 ctre::phoenix6::configs::TalonFXConfiguration configuration;
136 configuration.CurrentLimits = current_limits;
137
138 ctre::phoenix::StatusCode status =
139 talon->GetConfigurator().Apply(configuration);
140 if (!status.IsOK()) {
141 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
142 status.GetName(), status.GetDescription());
143 }
144
145 PrintConfigs(talon);
146}
147
148void Disable(ctre::phoenix6::hardware::TalonFX *talon) {
149 ctre::phoenix6::controls::DutyCycleOut stop_command(0.0);
150 stop_command.UpdateFreqHz = 0_Hz;
151 stop_command.EnableFOC = true;
152
153 talon->SetControl(stop_command);
154}
155
milind-u086d7262022-01-19 20:44:18 -0800156} // namespace
157
158// Class to send position messages with sensor readings to our loops.
159class SensorReader : public ::frc971::wpilib::SensorReader {
160 public:
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800161 SensorReader(::aos::ShmEventLoop *event_loop,
162 std::shared_ptr<const Values> values)
milind-u086d7262022-01-19 20:44:18 -0800163 : ::frc971::wpilib::SensorReader(event_loop),
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800164 values_(std::move(values)),
milind-u086d7262022-01-19 20:44:18 -0800165 auto_mode_sender_(
166 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
167 "/autonomous")),
168 superstructure_position_sender_(
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800169 event_loop->MakeSender<superstructure::PositionStatic>(
milind-u086d7262022-01-19 20:44:18 -0800170 "/superstructure")),
171 drivetrain_position_sender_(
172 event_loop
173 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
Ravago Jones0e86e242022-02-12 18:38:14 -0800174 "/drivetrain")),
175 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
176 "/drivetrain")) {
milind-u086d7262022-01-19 20:44:18 -0800177 // Set to filter out anything shorter than 1/4 of the minimum pulse width
178 // we should ever see.
179 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
180 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
181 }
182
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800183 void Start() override {
184 // TODO(Ravago): Figure out why adding multiple DMA readers results in weird
185 // behavior
186 // AddToDMA(&imu_heading_reader_);
187 AddToDMA(&imu_yaw_rate_reader_);
188 }
189
milind-u086d7262022-01-19 20:44:18 -0800190 // Auto mode switches.
191 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
192 autonomous_modes_.at(i) = ::std::move(sensor);
193 }
194
Austin Schuh39f26f62022-02-24 21:34:46 -0800195 void set_catapult_encoder(::std::unique_ptr<frc::Encoder> encoder) {
196 medium_encoder_filter_.Add(encoder.get());
197 catapult_encoder_.set_encoder(::std::move(encoder));
198 }
199
200 void set_catapult_absolute_pwm(
201 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
202 catapult_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
203 }
204
205 void set_catapult_potentiometer(
206 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
207 catapult_encoder_.set_potentiometer(::std::move(potentiometer));
208 }
209
Ravago Jones0e86e242022-02-12 18:38:14 -0800210 void set_heading_input(::std::unique_ptr<frc::DigitalInput> sensor) {
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800211 imu_heading_input_ = ::std::move(sensor);
212 imu_heading_reader_.set_input(imu_heading_input_.get());
Ravago Jones0e86e242022-02-12 18:38:14 -0800213 }
214
215 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800216 imu_yaw_rate_input_ = ::std::move(sensor);
217 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
Ravago Jones0e86e242022-02-12 18:38:14 -0800218 }
Austin Schuhda7e3e12022-03-26 15:14:31 -0700219 void set_catapult_falcon_1(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800220 ::std::shared_ptr<ctre::phoenix6::hardware::TalonFX> t1,
221 ::std::shared_ptr<ctre::phoenix6::hardware::TalonFX> t2) {
Austin Schuhda7e3e12022-03-26 15:14:31 -0700222 catapult_falcon_1_can_ = ::std::move(t1);
223 catapult_falcon_2_can_ = ::std::move(t2);
224 }
Ravago Jones0e86e242022-02-12 18:38:14 -0800225
milind-u086d7262022-01-19 20:44:18 -0800226 void RunIteration() override {
Austin Schuhda7e3e12022-03-26 15:14:31 -0700227 superstructure_reading_->Set(true);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800228 {
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800229 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
230 superstructure_position_sender_.MakeStaticBuilder();
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800231
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800232 CopyPosition(catapult_encoder_, builder->add_catapult(),
Austin Schuh39f26f62022-02-24 21:34:46 -0800233 Values::kCatapultEncoderCountsPerRevolution(),
234 Values::kCatapultEncoderRatio(), catapult_pot_translate,
Austin Schuh275f9812022-03-05 14:02:37 -0800235 false, values_->catapult.potentiometer_offset);
Austin Schuh39f26f62022-02-24 21:34:46 -0800236
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800237 CopyPosition(*climber_potentiometer_, builder->add_climber(),
238 climber_pot_translate, false,
239 values_->climber.potentiometer_offset);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800240
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800241 CopyPosition(*flipper_arm_left_potentiometer_,
242 builder->add_flipper_arm_left(), flipper_arms_pot_translate,
243 false, values_->flipper_arm_left.potentiometer_offset);
Griffin Buibcbef482022-02-23 15:32:10 -0800244
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800245 CopyPosition(*flipper_arm_right_potentiometer_,
246 builder->add_flipper_arm_right(), flipper_arms_pot_translate,
247 true, values_->flipper_arm_right.potentiometer_offset);
Griffin Buibcbef482022-02-23 15:32:10 -0800248
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800249 // Intake
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800250 CopyPosition(intake_encoder_front_, builder->add_intake_front(),
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800251 Values::kIntakeEncoderCountsPerRevolution(),
Austin Schuh275f9812022-03-05 14:02:37 -0800252 Values::kIntakeEncoderRatio(), intake_pot_translate, true,
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800253 values_->intake_front.potentiometer_offset);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800254 CopyPosition(intake_encoder_back_, builder->add_intake_back(),
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800255 Values::kIntakeEncoderCountsPerRevolution(),
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800256 Values::kIntakeEncoderRatio(), intake_pot_translate, true,
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800257 values_->intake_back.potentiometer_offset);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800258 CopyPosition(turret_encoder_, builder->add_turret(),
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800259 Values::kTurretEncoderCountsPerRevolution(),
260 Values::kTurretEncoderRatio(), turret_pot_translate, false,
261 values_->turret.potentiometer_offset);
262
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800263 builder->set_intake_beambreak_front(intake_beambreak_front_->Get());
264 builder->set_intake_beambreak_back(intake_beambreak_back_->Get());
265 builder->set_turret_beambreak(turret_beambreak_->Get());
266 builder.CheckOk(builder.Send());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800267 }
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800268
milind-u086d7262022-01-19 20:44:18 -0800269 {
270 auto builder = drivetrain_position_sender_.MakeBuilder();
271 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
272 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
273 drivetrain_builder.add_left_encoder(
James Kuszmaul53507e12022-02-12 18:36:40 -0800274 constants::Values::DrivetrainEncoderToMeters(
275 drivetrain_left_encoder_->GetRaw()));
milind-u086d7262022-01-19 20:44:18 -0800276 drivetrain_builder.add_left_speed(
277 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
278
279 drivetrain_builder.add_right_encoder(
James Kuszmaul53507e12022-02-12 18:36:40 -0800280 -constants::Values::DrivetrainEncoderToMeters(
281 drivetrain_right_encoder_->GetRaw()));
milind-u086d7262022-01-19 20:44:18 -0800282 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
283 drivetrain_right_encoder_->GetPeriod()));
284
285 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
286 }
287
288 {
Ravago Jones0e86e242022-02-12 18:38:14 -0800289 auto builder = gyro_sender_.MakeBuilder();
290 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
291 builder.MakeBuilder<::frc971::sensors::GyroReading>();
James Kuszmaulf34e7fd2022-03-13 20:30:34 -0700292 // +/- 2000 deg / sec
293 constexpr double kMaxVelocity = 4000; // degrees / second
Ravago Jones0e86e242022-02-12 18:38:14 -0800294 constexpr double kVelocityRadiansPerSecond =
295 kMaxVelocity / 360 * (2.0 * M_PI);
296
297 // Only part of the full range is used to prevent being 100% on or off.
298 constexpr double kScaledRangeLow = 0.1;
299 constexpr double kScaledRangeHigh = 0.9;
300
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800301 constexpr double kPWMFrequencyHz = 200;
302 double heading_duty_cycle =
303 imu_heading_reader_.last_width() * kPWMFrequencyHz;
304 double velocity_duty_cycle =
305 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
306
Ravago Jones0e86e242022-02-12 18:38:14 -0800307 constexpr double kDutyCycleScale =
308 1 / (kScaledRangeHigh - kScaledRangeLow);
Ravago Jones0e86e242022-02-12 18:38:14 -0800309 // scale from 0.1 - 0.9 to 0 - 1
310 double rescaled_heading_duty_cycle =
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800311 (heading_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
Ravago Jones0e86e242022-02-12 18:38:14 -0800312 double rescaled_velocity_duty_cycle =
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800313 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
Ravago Jones0e86e242022-02-12 18:38:14 -0800314
315 if (!std::isnan(rescaled_heading_duty_cycle)) {
316 gyro_reading_builder.add_angle(rescaled_heading_duty_cycle *
317 (2.0 * M_PI));
318 }
319 if (!std::isnan(rescaled_velocity_duty_cycle)) {
320 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
321 kVelocityRadiansPerSecond);
322 }
323 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
324 }
325
326 {
milind-u086d7262022-01-19 20:44:18 -0800327 auto builder = auto_mode_sender_.MakeBuilder();
328
329 uint32_t mode = 0;
330 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
331 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
332 mode |= 1 << i;
333 }
334 }
335
336 auto auto_mode_builder =
337 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
338
339 auto_mode_builder.add_mode(mode);
340
341 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
342 }
343 }
344
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800345 void set_climber_potentiometer(
346 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
347 climber_potentiometer_ = ::std::move(potentiometer);
348 }
349
Griffin Buibcbef482022-02-23 15:32:10 -0800350 void set_flipper_arm_left_potentiometer(
351 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
352 flipper_arm_left_potentiometer_ = ::std::move(potentiometer);
353 }
354
355 void set_flipper_arm_right_potentiometer(
356 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
357 flipper_arm_right_potentiometer_ = ::std::move(potentiometer);
358 }
359
Austin Schuhda7e3e12022-03-26 15:14:31 -0700360 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
361
362 void set_superstructure_reading(
363 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
364 superstructure_reading_ = superstructure_reading;
365 }
366
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800367 void set_intake_encoder_front(::std::unique_ptr<frc::Encoder> encoder) {
368 fast_encoder_filter_.Add(encoder.get());
369 intake_encoder_front_.set_encoder(::std::move(encoder));
370 }
371
372 void set_intake_encoder_back(::std::unique_ptr<frc::Encoder> encoder) {
373 fast_encoder_filter_.Add(encoder.get());
374 intake_encoder_back_.set_encoder(::std::move(encoder));
375 }
376
377 void set_intake_front_absolute_pwm(
378 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
379 intake_encoder_front_.set_absolute_pwm(::std::move(absolute_pwm));
380 }
381
382 void set_intake_front_potentiometer(
383 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
384 intake_encoder_front_.set_potentiometer(::std::move(potentiometer));
385 }
386
387 void set_intake_back_absolute_pwm(
388 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
389 intake_encoder_back_.set_absolute_pwm(::std::move(absolute_pwm));
390 }
391
392 void set_intake_back_potentiometer(
393 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
394 intake_encoder_back_.set_potentiometer(::std::move(potentiometer));
395 }
396
397 void set_turret_encoder(::std::unique_ptr<frc::Encoder> encoder) {
398 medium_encoder_filter_.Add(encoder.get());
399 turret_encoder_.set_encoder(::std::move(encoder));
400 }
401
402 void set_turret_absolute_pwm(
403 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
404 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
405 }
406
407 void set_turret_potentiometer(
408 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
409 turret_encoder_.set_potentiometer(::std::move(potentiometer));
410 }
411
Milo Lin4950ac52022-02-25 19:56:11 -0800412 void set_intake_beambreak_front(::std::unique_ptr<frc::DigitalInput> sensor) {
413 intake_beambreak_front_ = ::std::move(sensor);
414 }
415 void set_intake_beambreak_back(::std::unique_ptr<frc::DigitalInput> sensor) {
416 intake_beambreak_back_ = ::std::move(sensor);
417 }
418 void set_turret_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
419 turret_beambreak_ = ::std::move(sensor);
420 }
421
milind-u086d7262022-01-19 20:44:18 -0800422 private:
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800423 std::shared_ptr<const Values> values_;
424
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800425 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800426 aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800427 aos::Sender<frc971::control_loops::drivetrain::Position>
milind-u086d7262022-01-19 20:44:18 -0800428 drivetrain_position_sender_;
Ravago Jones0e86e242022-02-12 18:38:14 -0800429 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
milind-u086d7262022-01-19 20:44:18 -0800430
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800431 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800432
Milo Lin4950ac52022-02-25 19:56:11 -0800433 std::unique_ptr<frc::DigitalInput> intake_beambreak_front_,
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800434 intake_beambreak_back_, turret_beambreak_, imu_heading_input_,
435 imu_yaw_rate_input_;
Milo Lin4950ac52022-02-25 19:56:11 -0800436
Griffin Buibcbef482022-02-23 15:32:10 -0800437 std::unique_ptr<frc::AnalogInput> climber_potentiometer_,
438 flipper_arm_right_potentiometer_, flipper_arm_left_potentiometer_;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800439 frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_encoder_front_,
Ravago Jones0e86e242022-02-12 18:38:14 -0800440 intake_encoder_back_, turret_encoder_, catapult_encoder_;
Austin Schuh39f26f62022-02-24 21:34:46 -0800441
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800442 frc971::wpilib::DMAPulseWidthReader imu_heading_reader_, imu_yaw_rate_reader_;
Austin Schuhda7e3e12022-03-26 15:14:31 -0700443
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800444 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> catapult_falcon_1_can_,
445 catapult_falcon_2_can_;
milind-u086d7262022-01-19 20:44:18 -0800446};
447
448class SuperstructureWriter
449 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
450 public:
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800451 SuperstructureWriter(aos::EventLoop *event_loop)
452 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
Austin Schuhda7e3e12022-03-26 15:14:31 -0700453 event_loop, "/superstructure"),
454 catapult_reversal_(make_unique<frc::DigitalOutput>(0)) {}
milind-u086d7262022-01-19 20:44:18 -0800455
milind-u6e7d8d42022-04-06 18:30:43 -0700456 void set_climber_servo_left(::std::unique_ptr<::frc::Servo> t) {
457 climber_servo_left_ = ::std::move(t);
458 }
459 void set_climber_servo_right(::std::unique_ptr<::frc::Servo> t) {
460 climber_servo_right_ = ::std::move(t);
461 }
462
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800463 void set_climber_falcon(std::unique_ptr<frc::TalonFX> t) {
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800464 climber_falcon_ = std::move(t);
Griffin Bui67abb912022-01-22 16:16:21 -0800465 }
466
Jacob Ismael322ebb92022-02-09 20:12:47 -0800467 void set_turret_falcon(::std::unique_ptr<::frc::TalonFX> t) {
468 turret_falcon_ = ::std::move(t);
469 }
470
471 void set_catapult_falcon_1(::std::unique_ptr<::frc::TalonFX> t) {
472 catapult_falcon_1_ = ::std::move(t);
473 }
474
Austin Schuhe040ddc2022-03-17 00:16:47 -0700475 void set_catapult_falcon_1(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800476 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> t1,
477 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> t2) {
Austin Schuhe040ddc2022-03-17 00:16:47 -0700478 catapult_falcon_1_can_ = ::std::move(t1);
479 catapult_falcon_2_can_ = ::std::move(t2);
480
481 for (auto &falcon : {catapult_falcon_1_can_, catapult_falcon_2_can_}) {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800482 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
483 current_limits.StatorCurrentLimit =
484 Values::kIntakeRollerStatorCurrentLimit();
485 current_limits.StatorCurrentLimitEnable = true;
486 current_limits.SupplyCurrentLimit =
487 Values::kIntakeRollerSupplyCurrentLimit();
488 current_limits.SupplyCurrentLimitEnable = true;
489
490 ctre::phoenix6::configs::TalonFXConfiguration configuration;
491 configuration.CurrentLimits = current_limits;
492
493 ctre::phoenix::StatusCode status =
494 falcon->GetConfigurator().Apply(configuration);
495 if (!status.IsOK()) {
496 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
497 status.GetName(), status.GetDescription());
498 }
499
500 PrintConfigs(falcon.get());
501
502 // TODO(max): Figure out how to migrate these configs to phoenix6
503 /*falcon->SetStatusFramePeriod(
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700504 ctre::phoenix::motorcontrol::Status_1_General, 1);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700505 falcon->SetStatusFramePeriod(
506 ctre::phoenix::motorcontrol::Status_Brushless_Current, 50);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800507
Austin Schuhda7e3e12022-03-26 15:14:31 -0700508 falcon->ConfigOpenloopRamp(0.0);
509 falcon->ConfigClosedloopRamp(0.0);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800510 falcon->ConfigVoltageMeasurementFilter(1);*/
Austin Schuhe040ddc2022-03-17 00:16:47 -0700511 }
Austin Schuhe040ddc2022-03-17 00:16:47 -0700512 }
513
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800514 void set_intake_falcon_front(::std::unique_ptr<frc::TalonFX> t) {
515 intake_falcon_front_ = ::std::move(t);
Griffin Bui67abb912022-01-22 16:16:21 -0800516 }
milind-u086d7262022-01-19 20:44:18 -0800517
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800518 void set_intake_falcon_back(::std::unique_ptr<frc::TalonFX> t) {
519 intake_falcon_back_ = ::std::move(t);
520 }
521
522 void set_roller_falcon_front(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800523 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800524 roller_falcon_front_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800525 WriteConfigs(roller_falcon_front_.get(),
526 Values::kIntakeRollerStatorCurrentLimit(),
527 Values::kIntakeRollerSupplyCurrentLimit());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800528 }
529
530 void set_roller_falcon_back(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800531 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800532 roller_falcon_back_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800533 WriteConfigs(roller_falcon_back_.get(),
534 Values::kIntakeRollerStatorCurrentLimit(),
535 Values::kIntakeRollerSupplyCurrentLimit());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800536 }
537
Griffin Buibcbef482022-02-23 15:32:10 -0800538 void set_flipper_arms_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800539 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800540 flipper_arms_falcon_ = t;
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800541 WriteConfigs(flipper_arms_falcon_.get(),
542 Values::kFlipperArmSupplyCurrentLimit(),
543 Values::kFlipperArmStatorCurrentLimit());
Griffin Buibcbef482022-02-23 15:32:10 -0800544 }
545
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800546 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> flipper_arms_falcon() {
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800547 return flipper_arms_falcon_;
548 }
549
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700550 void set_transfer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) {
551 transfer_roller_victor_ = ::std::move(t);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800552 }
553
Austin Schuhda7e3e12022-03-26 15:14:31 -0700554 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
555
556 void set_superstructure_reading(
557 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
558 superstructure_reading_ = superstructure_reading;
559 }
560
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800561 private:
Griffin Bui67abb912022-01-22 16:16:21 -0800562 void Stop() override {
563 AOS_LOG(WARNING, "Superstructure output too old.\n");
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800564 climber_falcon_->SetDisabled();
milind-u6e7d8d42022-04-06 18:30:43 -0700565 climber_servo_left_->SetRaw(0);
566 climber_servo_right_->SetRaw(0);
567
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800568 Disable(roller_falcon_front_.get());
569 Disable(roller_falcon_back_.get());
570 Disable(flipper_arms_falcon_.get());
571
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800572 intake_falcon_front_->SetDisabled();
573 intake_falcon_back_->SetDisabled();
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700574 transfer_roller_victor_->SetDisabled();
Austin Schuhe040ddc2022-03-17 00:16:47 -0700575 if (catapult_falcon_1_) {
576 catapult_falcon_1_->SetDisabled();
577 }
578 if (catapult_falcon_1_can_) {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800579 Disable(catapult_falcon_1_can_.get());
580 Disable(catapult_falcon_2_can_.get());
Austin Schuhe040ddc2022-03-17 00:16:47 -0700581 }
Jacob Ismael322ebb92022-02-09 20:12:47 -0800582 turret_falcon_->SetDisabled();
Griffin Bui67abb912022-01-22 16:16:21 -0800583 }
584
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800585 void Write(const superstructure::Output &output) override {
Austin Schuh8507c9f2022-03-13 18:08:28 -0700586 WritePwm(-output.climber_voltage(), climber_falcon_.get());
milind-u6e7d8d42022-04-06 18:30:43 -0700587 climber_servo_left_->SetPosition(output.climber_servo_left());
588 climber_servo_right_->SetPosition(output.climber_servo_right());
Griffin Buibcbef482022-02-23 15:32:10 -0800589
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800590 WritePwm(output.intake_voltage_front(), intake_falcon_front_.get());
591 WritePwm(output.intake_voltage_back(), intake_falcon_back_.get());
592 WriteCan(output.roller_voltage_front(), roller_falcon_front_.get());
593 WriteCan(output.roller_voltage_back(), roller_falcon_back_.get());
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700594 WritePwm(output.transfer_roller_voltage(), transfer_roller_victor_.get());
Griffin Buibcbef482022-02-23 15:32:10 -0800595
Austin Schuh465a2882022-03-05 15:39:04 -0800596 WriteCan(-output.flipper_arms_voltage(), flipper_arms_falcon_.get());
Griffin Buibcbef482022-02-23 15:32:10 -0800597
Austin Schuhe040ddc2022-03-17 00:16:47 -0700598 if (catapult_falcon_1_) {
599 WritePwm(output.catapult_voltage(), catapult_falcon_1_.get());
Austin Schuhda7e3e12022-03-26 15:14:31 -0700600 superstructure_reading_->Set(false);
601 if (output.catapult_voltage() > 0) {
602 catapult_reversal_->Set(true);
603 } else {
604 catapult_reversal_->Set(false);
605 }
Austin Schuhe040ddc2022-03-17 00:16:47 -0700606 }
607 if (catapult_falcon_1_can_) {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800608 WriteCanCatapult(output.catapult_voltage(), catapult_falcon_1_can_.get());
609 WriteCanCatapult(output.catapult_voltage(), catapult_falcon_2_can_.get());
Austin Schuhe040ddc2022-03-17 00:16:47 -0700610 }
Griffin Buibcbef482022-02-23 15:32:10 -0800611
Austin Schuh465a2882022-03-05 15:39:04 -0800612 WritePwm(-output.turret_voltage(), turret_falcon_.get());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800613 }
614
615 static void WriteCan(const double voltage,
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800616 ::ctre::phoenix6::hardware::TalonFX *falcon) {
617 ctre::phoenix6::controls::DutyCycleOut control(
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800618 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800619 control.UpdateFreqHz = 0_Hz;
620 control.EnableFOC = true;
621
622 falcon->SetControl(control);
623 }
624 // We do this to set our UpdateFreqHz higher
625 static void WriteCanCatapult(const double voltage,
626 ::ctre::phoenix6::hardware::TalonFX *falcon) {
627 ctre::phoenix6::controls::DutyCycleOut control(
628 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
629 control.UpdateFreqHz = 1000_Hz;
630 control.EnableFOC = true;
631
632 falcon->SetControl(control);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800633 }
634
635 template <typename T>
636 static void WritePwm(const double voltage, T *motor) {
637 motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) /
638 12.0);
639 }
640
641 ::std::unique_ptr<frc::TalonFX> intake_falcon_front_, intake_falcon_back_;
642
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800643 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> roller_falcon_front_,
644 roller_falcon_back_;
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800645
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800646 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> flipper_arms_falcon_;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800647
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800648 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> catapult_falcon_1_can_,
649 catapult_falcon_2_can_;
Austin Schuhe040ddc2022-03-17 00:16:47 -0700650
Jacob Ismael322ebb92022-02-09 20:12:47 -0800651 ::std::unique_ptr<::frc::TalonFX> turret_falcon_, catapult_falcon_1_,
Austin Schuh465a2882022-03-05 15:39:04 -0800652 climber_falcon_;
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700653 ::std::unique_ptr<::frc::VictorSP> transfer_roller_victor_;
Austin Schuhda7e3e12022-03-26 15:14:31 -0700654
655 std::unique_ptr<frc::DigitalOutput> catapult_reversal_;
milind-u6e7d8d42022-04-06 18:30:43 -0700656
657 ::std::unique_ptr<::frc::Servo> climber_servo_left_, climber_servo_right_;
milind-u086d7262022-01-19 20:44:18 -0800658};
659
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800660class CANSensorReader {
661 public:
662 CANSensorReader(aos::EventLoop *event_loop)
663 : event_loop_(event_loop),
664 can_position_sender_(
665 event_loop->MakeSender<superstructure::CANPosition>(
666 "/superstructure")) {
667 event_loop->SetRuntimeRealtimePriority(16);
668
669 phased_loop_handler_ =
670 event_loop_->AddPhasedLoop([this](int) { Loop(); }, kPeriod);
671 phased_loop_handler_->set_name("CAN SensorReader Loop");
672
673 event_loop->OnRun([this]() { Loop(); });
674 }
675
676 void set_flipper_arms_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800677 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800678 flipper_arms_falcon_ = std::move(t);
679 }
680
681 private:
682 void Loop() {
683 auto builder = can_position_sender_.MakeBuilder();
684 superstructure::CANPosition::Builder can_position_builder =
685 builder.MakeBuilder<superstructure::CANPosition>();
686 can_position_builder.add_flipper_arm_integrated_sensor_velocity(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800687 flipper_arms_falcon_->GetVelocity().GetValue().value() *
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800688 kVelocityConversion);
689 builder.CheckOk(builder.Send(can_position_builder.Finish()));
690 }
691
692 static constexpr std::chrono::milliseconds kPeriod =
693 std::chrono::milliseconds(20);
694 // 2048 encoder counts / 100 ms to rad/sec
695 static constexpr double kVelocityConversion = (2.0 * M_PI / 2048) * 0.100;
696 aos::EventLoop *event_loop_;
697 ::aos::PhasedLoopHandler *phased_loop_handler_;
698
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800699 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> flipper_arms_falcon_;
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800700 aos::Sender<superstructure::CANPosition> can_position_sender_;
701};
702
milind-u086d7262022-01-19 20:44:18 -0800703class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
704 public:
705 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
706 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
707 frc::Encoder::k4X);
708 }
709
710 void Run() override {
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800711 std::shared_ptr<const Values> values =
712 std::make_shared<const Values>(constants::MakeValues());
713
milind-u086d7262022-01-19 20:44:18 -0800714 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800715 aos::configuration::ReadConfig("aos_config.json");
milind-u086d7262022-01-19 20:44:18 -0800716
717 // Thread 1.
718 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
719 ::frc971::wpilib::JoystickSender joystick_sender(
720 &joystick_sender_event_loop);
721 AddLoop(&joystick_sender_event_loop);
722
723 // Thread 2.
724 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
725 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800726 AddLoop(&pdp_fetcher_event_loop);
milind-u086d7262022-01-19 20:44:18 -0800727
Austin Schuhda7e3e12022-03-26 15:14:31 -0700728 std::shared_ptr<frc::DigitalOutput> superstructure_reading =
729 make_unique<frc::DigitalOutput>(25);
730
milind-u086d7262022-01-19 20:44:18 -0800731 // Thread 3.
732 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800733 SensorReader sensor_reader(&sensor_reader_event_loop, values);
Austin Schuh028d81d2022-03-26 15:11:42 -0700734 sensor_reader.set_pwm_trigger(true);
Austin Schuha8014282022-03-05 12:36:38 -0800735 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
736 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
Austin Schuhda7e3e12022-03-26 15:14:31 -0700737 sensor_reader.set_superstructure_reading(superstructure_reading);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800738
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800739 sensor_reader.set_intake_encoder_front(make_encoder(3));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800740 sensor_reader.set_intake_front_absolute_pwm(
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800741 make_unique<frc::DigitalInput>(3));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800742 sensor_reader.set_intake_front_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800743 make_unique<frc::AnalogInput>(3));
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800744
745 sensor_reader.set_intake_encoder_back(make_encoder(4));
746 sensor_reader.set_intake_back_absolute_pwm(
747 make_unique<frc::DigitalInput>(4));
748 sensor_reader.set_intake_back_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800749 make_unique<frc::AnalogInput>(4));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800750
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800751 sensor_reader.set_turret_encoder(make_encoder(5));
752 sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(5));
Austin Schuha8014282022-03-05 12:36:38 -0800753 sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(5));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800754
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800755 // TODO(milind): correct intake beambreak ports once set
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800756 sensor_reader.set_intake_beambreak_front(make_unique<frc::DigitalInput>(1));
Austin Schuh445ae832022-03-05 22:52:23 -0800757 sensor_reader.set_intake_beambreak_back(make_unique<frc::DigitalInput>(6));
Milo Lin4950ac52022-02-25 19:56:11 -0800758 sensor_reader.set_turret_beambreak(make_unique<frc::DigitalInput>(7));
759
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800760 sensor_reader.set_climber_potentiometer(make_unique<frc::AnalogInput>(7));
milind-u086d7262022-01-19 20:44:18 -0800761
Griffin Buibcbef482022-02-23 15:32:10 -0800762 sensor_reader.set_flipper_arm_left_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800763 make_unique<frc::AnalogInput>(0));
Griffin Buibcbef482022-02-23 15:32:10 -0800764 sensor_reader.set_flipper_arm_right_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800765 make_unique<frc::AnalogInput>(1));
Griffin Buibcbef482022-02-23 15:32:10 -0800766
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800767 // TODO(milind): correct catapult encoder and absolute pwm ports
Austin Schuha8014282022-03-05 12:36:38 -0800768 sensor_reader.set_catapult_encoder(make_encoder(2));
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800769 sensor_reader.set_catapult_absolute_pwm(
Austin Schuha8014282022-03-05 12:36:38 -0800770 std::make_unique<frc::DigitalInput>(2));
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800771 sensor_reader.set_catapult_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800772 std::make_unique<frc::AnalogInput>(2));
Austin Schuh39f26f62022-02-24 21:34:46 -0800773
Austin Schuhf9a166e2022-03-05 17:53:12 -0800774 sensor_reader.set_heading_input(make_unique<frc::DigitalInput>(9));
775 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(8));
Ravago Jones0e86e242022-02-12 18:38:14 -0800776
milind-u086d7262022-01-19 20:44:18 -0800777 AddLoop(&sensor_reader_event_loop);
778
779 // Thread 4.
780 ::aos::ShmEventLoop output_event_loop(&config.message());
781 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
782 drivetrain_writer.set_left_controller0(
Austin Schuh2d22fb12022-03-06 14:43:29 -0800783 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), false);
Austin Schuha8014282022-03-05 12:36:38 -0800784 drivetrain_writer.set_right_controller0(
Austin Schuh2d22fb12022-03-06 14:43:29 -0800785 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), true);
milind-u086d7262022-01-19 20:44:18 -0800786
787 SuperstructureWriter superstructure_writer(&output_event_loop);
788
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800789 superstructure_writer.set_turret_falcon(make_unique<::frc::TalonFX>(3));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800790 superstructure_writer.set_roller_falcon_front(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800791 make_unique<::ctre::phoenix6::hardware::TalonFX>(0));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800792 superstructure_writer.set_roller_falcon_back(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800793 make_unique<::ctre::phoenix6::hardware::TalonFX>(1));
Austin Schuh465a2882022-03-05 15:39:04 -0800794
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700795 superstructure_writer.set_transfer_roller_victor(
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800796 make_unique<::frc::VictorSP>(5));
Austin Schuh465a2882022-03-05 15:39:04 -0800797
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800798 superstructure_writer.set_intake_falcon_front(make_unique<frc::TalonFX>(2));
799 superstructure_writer.set_intake_falcon_back(make_unique<frc::TalonFX>(4));
Austin Schuh465a2882022-03-05 15:39:04 -0800800 superstructure_writer.set_climber_falcon(make_unique<frc::TalonFX>(8));
milind-u6e7d8d42022-04-06 18:30:43 -0700801 superstructure_writer.set_climber_servo_left(make_unique<frc::Servo>(7));
802 superstructure_writer.set_climber_servo_right(make_unique<frc::Servo>(6));
Griffin Buibcbef482022-02-23 15:32:10 -0800803 superstructure_writer.set_flipper_arms_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800804 make_unique<::ctre::phoenix6::hardware::TalonFX>(2));
Austin Schuhda7e3e12022-03-26 15:14:31 -0700805 superstructure_writer.set_superstructure_reading(superstructure_reading);
Griffin Bui67abb912022-01-22 16:16:21 -0800806
Austin Schuhda7e3e12022-03-26 15:14:31 -0700807 if (!FLAGS_can_catapult) {
808 superstructure_writer.set_catapult_falcon_1(make_unique<frc::TalonFX>(9));
809 } else {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800810 std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> catapult1 =
811 make_unique<::ctre::phoenix6::hardware::TalonFX>(3, "Catapult");
812 std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> catapult2 =
813 make_unique<::ctre::phoenix6::hardware::TalonFX>(4, "Catapult");
Austin Schuhda7e3e12022-03-26 15:14:31 -0700814 superstructure_writer.set_catapult_falcon_1(catapult1, catapult2);
815 sensor_reader.set_catapult_falcon_1(catapult1, catapult2);
816 }
Austin Schuh39f26f62022-02-24 21:34:46 -0800817
milind-u086d7262022-01-19 20:44:18 -0800818 AddLoop(&output_event_loop);
819
Henry Speiser77747b72022-03-06 17:18:29 -0800820 // Thread 5.
821 ::aos::ShmEventLoop led_indicator_event_loop(&config.message());
822 control_loops::superstructure::LedIndicator led_indicator(
823 &led_indicator_event_loop);
824 AddLoop(&led_indicator_event_loop);
825
milind-u086d7262022-01-19 20:44:18 -0800826 RunLoops();
827 }
828};
829
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800830} // namespace y2022::wpilib
milind-u086d7262022-01-19 20:44:18 -0800831
832AOS_ROBOT_CLASS(::y2022::wpilib::WPILibRobot);