blob: 1f82e91f428208145b09840d223b43faedb48547 [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
Austin Schuh99f7c6a2024-06-25 22:07:44 -070014#include "absl/flags/flag.h"
milind-u086d7262022-01-19 20:44:18 -080015#include "ctre/phoenix/CANifier.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070016
milind-u086d7262022-01-19 20:44:18 -080017#include "frc971/wpilib/ahal/AnalogInput.h"
18#include "frc971/wpilib/ahal/Counter.h"
19#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
20#include "frc971/wpilib/ahal/DriverStation.h"
21#include "frc971/wpilib/ahal/Encoder.h"
milind-u6e7d8d42022-04-06 18:30:43 -070022#include "frc971/wpilib/ahal/Servo.h"
milind-u086d7262022-01-19 20:44:18 -080023#include "frc971/wpilib/ahal/TalonFX.h"
24#include "frc971/wpilib/ahal/VictorSP.h"
25#undef ERROR
26
Maxwell Henderson1c0843c2023-12-22 16:20:59 -080027#include "ctre/phoenix6/TalonFX.hpp"
Philipp Schrader790cb542023-07-05 21:06:52 -070028
milind-u086d7262022-01-19 20:44:18 -080029#include "aos/commonmath.h"
30#include "aos/events/event_loop.h"
31#include "aos/events/shm_event_loop.h"
32#include "aos/init.h"
33#include "aos/logging/logging.h"
34#include "aos/realtime.h"
35#include "aos/time/time.h"
36#include "aos/util/log_interval.h"
37#include "aos/util/phased_loop.h"
38#include "aos/util/wrapping_counter.h"
milind-u086d7262022-01-19 20:44:18 -080039#include "frc971/autonomous/auto_mode_generated.h"
40#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
41#include "frc971/input/robot_state_generated.h"
Ravago Jones0e86e242022-02-12 18:38:14 -080042#include "frc971/queues/gyro_generated.h"
milind-u086d7262022-01-19 20:44:18 -080043#include "frc971/wpilib/ADIS16448.h"
44#include "frc971/wpilib/buffered_pcm.h"
45#include "frc971/wpilib/buffered_solenoid.h"
46#include "frc971/wpilib/dma.h"
47#include "frc971/wpilib/drivetrain_writer.h"
48#include "frc971/wpilib/encoder_and_potentiometer.h"
49#include "frc971/wpilib/joystick_sender.h"
50#include "frc971/wpilib/logging_generated.h"
51#include "frc971/wpilib/loop_output_handler.h"
52#include "frc971/wpilib/pdp_fetcher.h"
53#include "frc971/wpilib/sensor_reader.h"
54#include "frc971/wpilib/wpilib_robot_base.h"
55#include "y2022/constants.h"
Henry Speiser77747b72022-03-06 17:18:29 -080056#include "y2022/control_loops/superstructure/led_indicator.h"
Milind Upadhyay482b0ba2022-02-26 21:51:59 -080057#include "y2022/control_loops/superstructure/superstructure_can_position_generated.h"
milind-u086d7262022-01-19 20:44:18 -080058#include "y2022/control_loops/superstructure/superstructure_output_generated.h"
Maxwell Hendersoncb78f352024-01-15 00:27:16 -080059#include "y2022/control_loops/superstructure/superstructure_position_static.h"
milind-u086d7262022-01-19 20:44:18 -080060
61using ::aos::monotonic_clock;
62using ::y2022::constants::Values;
63namespace superstructure = ::y2022::control_loops::superstructure;
64namespace chrono = ::std::chrono;
65using std::make_unique;
66
Austin Schuh99f7c6a2024-06-25 22:07:44 -070067ABSL_FLAG(bool, can_catapult, false,
68 "If true, use CAN to control the catapult.");
Austin Schuhda7e3e12022-03-26 15:14:31 -070069
Stephan Pleinesf63bde82024-01-13 15:59:33 -080070namespace y2022::wpilib {
milind-u086d7262022-01-19 20:44:18 -080071namespace {
72
73constexpr double kMaxBringupPower = 12.0;
74
75// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
76// DMA stuff and then removing the * 2.0 in *_translate.
77// The low bit is direction.
78
milind-u086d7262022-01-19 20:44:18 -080079double drivetrain_velocity_translate(double in) {
80 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
81 (2.0 * M_PI)) *
82 Values::kDrivetrainEncoderRatio() *
83 control_loops::drivetrain::kWheelRadius;
84}
85
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080086double climber_pot_translate(double voltage) {
Nathan Leong342b85e2023-01-08 13:49:33 -080087 return voltage * Values::kClimberPotMetersPerVolt();
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080088}
89
Griffin Buibcbef482022-02-23 15:32:10 -080090double flipper_arms_pot_translate(double voltage) {
Nathan Leong342b85e2023-01-08 13:49:33 -080091 return voltage * Values::kFlipperArmsPotRadiansPerVolt();
Griffin Buibcbef482022-02-23 15:32:10 -080092}
93
Henry Speiser55aa3ba2022-02-21 23:21:12 -080094double intake_pot_translate(double voltage) {
Nathan Leong342b85e2023-01-08 13:49:33 -080095 return voltage * Values::kIntakePotRadiansPerVolt();
Henry Speiser55aa3ba2022-02-21 23:21:12 -080096}
97
98double turret_pot_translate(double voltage) {
Nathan Leong342b85e2023-01-08 13:49:33 -080099 return voltage * Values::kTurretPotRadiansPerVolt();
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800100}
101
milind-u086d7262022-01-19 20:44:18 -0800102constexpr double kMaxFastEncoderPulsesPerSecond =
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800103 std::max({Values::kMaxDrivetrainEncoderPulsesPerSecond(),
104 Values::kMaxIntakeEncoderPulsesPerSecond()});
milind-u086d7262022-01-19 20:44:18 -0800105static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
106 "fast encoders are too fast");
107constexpr double kMaxMediumEncoderPulsesPerSecond =
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800108 Values::kMaxTurretEncoderPulsesPerSecond();
milind-u086d7262022-01-19 20:44:18 -0800109
110static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
111 "medium encoders are too fast");
112
Austin Schuh39f26f62022-02-24 21:34:46 -0800113double catapult_pot_translate(double voltage) {
114 return voltage * Values::kCatapultPotRatio() *
115 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
116}
117
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800118void PrintConfigs(ctre::phoenix6::hardware::TalonFX *talon) {
119 ctre::phoenix6::configs::TalonFXConfiguration configuration;
120 ctre::phoenix::StatusCode status =
121 talon->GetConfigurator().Refresh(configuration);
122 if (!status.IsOK()) {
123 AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s",
124 status.GetName(), status.GetDescription());
125 }
126 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
127}
128
129void WriteConfigs(ctre::phoenix6::hardware::TalonFX *talon,
130 double stator_current_limit, double supply_current_limit) {
131 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
132 current_limits.StatorCurrentLimit = stator_current_limit;
133 current_limits.StatorCurrentLimitEnable = true;
134 current_limits.SupplyCurrentLimit = supply_current_limit;
135 current_limits.SupplyCurrentLimitEnable = true;
136
137 ctre::phoenix6::configs::TalonFXConfiguration configuration;
138 configuration.CurrentLimits = current_limits;
139
140 ctre::phoenix::StatusCode status =
141 talon->GetConfigurator().Apply(configuration);
142 if (!status.IsOK()) {
143 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
144 status.GetName(), status.GetDescription());
145 }
146
147 PrintConfigs(talon);
148}
149
150void Disable(ctre::phoenix6::hardware::TalonFX *talon) {
151 ctre::phoenix6::controls::DutyCycleOut stop_command(0.0);
152 stop_command.UpdateFreqHz = 0_Hz;
153 stop_command.EnableFOC = true;
154
155 talon->SetControl(stop_command);
156}
157
milind-u086d7262022-01-19 20:44:18 -0800158} // namespace
159
160// Class to send position messages with sensor readings to our loops.
161class SensorReader : public ::frc971::wpilib::SensorReader {
162 public:
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800163 SensorReader(::aos::ShmEventLoop *event_loop,
164 std::shared_ptr<const Values> values)
milind-u086d7262022-01-19 20:44:18 -0800165 : ::frc971::wpilib::SensorReader(event_loop),
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800166 values_(std::move(values)),
milind-u086d7262022-01-19 20:44:18 -0800167 auto_mode_sender_(
168 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
169 "/autonomous")),
170 superstructure_position_sender_(
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800171 event_loop->MakeSender<superstructure::PositionStatic>(
milind-u086d7262022-01-19 20:44:18 -0800172 "/superstructure")),
173 drivetrain_position_sender_(
174 event_loop
175 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
Ravago Jones0e86e242022-02-12 18:38:14 -0800176 "/drivetrain")),
177 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
178 "/drivetrain")) {
milind-u086d7262022-01-19 20:44:18 -0800179 // Set to filter out anything shorter than 1/4 of the minimum pulse width
180 // we should ever see.
181 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
182 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
183 }
184
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800185 void Start() override {
186 // TODO(Ravago): Figure out why adding multiple DMA readers results in weird
187 // behavior
188 // AddToDMA(&imu_heading_reader_);
189 AddToDMA(&imu_yaw_rate_reader_);
190 }
191
milind-u086d7262022-01-19 20:44:18 -0800192 // Auto mode switches.
193 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
194 autonomous_modes_.at(i) = ::std::move(sensor);
195 }
196
Austin Schuh39f26f62022-02-24 21:34:46 -0800197 void set_catapult_encoder(::std::unique_ptr<frc::Encoder> encoder) {
198 medium_encoder_filter_.Add(encoder.get());
199 catapult_encoder_.set_encoder(::std::move(encoder));
200 }
201
202 void set_catapult_absolute_pwm(
203 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
204 catapult_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
205 }
206
207 void set_catapult_potentiometer(
208 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
209 catapult_encoder_.set_potentiometer(::std::move(potentiometer));
210 }
211
Ravago Jones0e86e242022-02-12 18:38:14 -0800212 void set_heading_input(::std::unique_ptr<frc::DigitalInput> sensor) {
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800213 imu_heading_input_ = ::std::move(sensor);
214 imu_heading_reader_.set_input(imu_heading_input_.get());
Ravago Jones0e86e242022-02-12 18:38:14 -0800215 }
216
217 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800218 imu_yaw_rate_input_ = ::std::move(sensor);
219 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
Ravago Jones0e86e242022-02-12 18:38:14 -0800220 }
Austin Schuhda7e3e12022-03-26 15:14:31 -0700221 void set_catapult_falcon_1(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800222 ::std::shared_ptr<ctre::phoenix6::hardware::TalonFX> t1,
223 ::std::shared_ptr<ctre::phoenix6::hardware::TalonFX> t2) {
Austin Schuhda7e3e12022-03-26 15:14:31 -0700224 catapult_falcon_1_can_ = ::std::move(t1);
225 catapult_falcon_2_can_ = ::std::move(t2);
226 }
Ravago Jones0e86e242022-02-12 18:38:14 -0800227
milind-u086d7262022-01-19 20:44:18 -0800228 void RunIteration() override {
Austin Schuhda7e3e12022-03-26 15:14:31 -0700229 superstructure_reading_->Set(true);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800230 {
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800231 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
232 superstructure_position_sender_.MakeStaticBuilder();
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800233
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800234 CopyPosition(catapult_encoder_, builder->add_catapult(),
Austin Schuh39f26f62022-02-24 21:34:46 -0800235 Values::kCatapultEncoderCountsPerRevolution(),
236 Values::kCatapultEncoderRatio(), catapult_pot_translate,
Austin Schuh275f9812022-03-05 14:02:37 -0800237 false, values_->catapult.potentiometer_offset);
Austin Schuh39f26f62022-02-24 21:34:46 -0800238
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800239 CopyPosition(*climber_potentiometer_, builder->add_climber(),
240 climber_pot_translate, false,
241 values_->climber.potentiometer_offset);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800242
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800243 CopyPosition(*flipper_arm_left_potentiometer_,
244 builder->add_flipper_arm_left(), flipper_arms_pot_translate,
245 false, values_->flipper_arm_left.potentiometer_offset);
Griffin Buibcbef482022-02-23 15:32:10 -0800246
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800247 CopyPosition(*flipper_arm_right_potentiometer_,
248 builder->add_flipper_arm_right(), flipper_arms_pot_translate,
249 true, values_->flipper_arm_right.potentiometer_offset);
Griffin Buibcbef482022-02-23 15:32:10 -0800250
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800251 // Intake
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800252 CopyPosition(intake_encoder_front_, builder->add_intake_front(),
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800253 Values::kIntakeEncoderCountsPerRevolution(),
Austin Schuh275f9812022-03-05 14:02:37 -0800254 Values::kIntakeEncoderRatio(), intake_pot_translate, true,
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800255 values_->intake_front.potentiometer_offset);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800256 CopyPosition(intake_encoder_back_, builder->add_intake_back(),
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800257 Values::kIntakeEncoderCountsPerRevolution(),
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800258 Values::kIntakeEncoderRatio(), intake_pot_translate, true,
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800259 values_->intake_back.potentiometer_offset);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800260 CopyPosition(turret_encoder_, builder->add_turret(),
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800261 Values::kTurretEncoderCountsPerRevolution(),
262 Values::kTurretEncoderRatio(), turret_pot_translate, false,
263 values_->turret.potentiometer_offset);
264
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800265 builder->set_intake_beambreak_front(intake_beambreak_front_->Get());
266 builder->set_intake_beambreak_back(intake_beambreak_back_->Get());
267 builder->set_turret_beambreak(turret_beambreak_->Get());
268 builder.CheckOk(builder.Send());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800269 }
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800270
milind-u086d7262022-01-19 20:44:18 -0800271 {
272 auto builder = drivetrain_position_sender_.MakeBuilder();
273 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
274 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
275 drivetrain_builder.add_left_encoder(
James Kuszmaul53507e12022-02-12 18:36:40 -0800276 constants::Values::DrivetrainEncoderToMeters(
277 drivetrain_left_encoder_->GetRaw()));
milind-u086d7262022-01-19 20:44:18 -0800278 drivetrain_builder.add_left_speed(
279 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
280
281 drivetrain_builder.add_right_encoder(
James Kuszmaul53507e12022-02-12 18:36:40 -0800282 -constants::Values::DrivetrainEncoderToMeters(
283 drivetrain_right_encoder_->GetRaw()));
milind-u086d7262022-01-19 20:44:18 -0800284 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
285 drivetrain_right_encoder_->GetPeriod()));
286
287 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
288 }
289
290 {
Ravago Jones0e86e242022-02-12 18:38:14 -0800291 auto builder = gyro_sender_.MakeBuilder();
292 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
293 builder.MakeBuilder<::frc971::sensors::GyroReading>();
James Kuszmaulf34e7fd2022-03-13 20:30:34 -0700294 // +/- 2000 deg / sec
295 constexpr double kMaxVelocity = 4000; // degrees / second
Ravago Jones0e86e242022-02-12 18:38:14 -0800296 constexpr double kVelocityRadiansPerSecond =
297 kMaxVelocity / 360 * (2.0 * M_PI);
298
299 // Only part of the full range is used to prevent being 100% on or off.
300 constexpr double kScaledRangeLow = 0.1;
301 constexpr double kScaledRangeHigh = 0.9;
302
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800303 constexpr double kPWMFrequencyHz = 200;
304 double heading_duty_cycle =
305 imu_heading_reader_.last_width() * kPWMFrequencyHz;
306 double velocity_duty_cycle =
307 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
308
Ravago Jones0e86e242022-02-12 18:38:14 -0800309 constexpr double kDutyCycleScale =
310 1 / (kScaledRangeHigh - kScaledRangeLow);
Ravago Jones0e86e242022-02-12 18:38:14 -0800311 // scale from 0.1 - 0.9 to 0 - 1
312 double rescaled_heading_duty_cycle =
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800313 (heading_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
Ravago Jones0e86e242022-02-12 18:38:14 -0800314 double rescaled_velocity_duty_cycle =
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800315 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
Ravago Jones0e86e242022-02-12 18:38:14 -0800316
317 if (!std::isnan(rescaled_heading_duty_cycle)) {
318 gyro_reading_builder.add_angle(rescaled_heading_duty_cycle *
319 (2.0 * M_PI));
320 }
321 if (!std::isnan(rescaled_velocity_duty_cycle)) {
322 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
323 kVelocityRadiansPerSecond);
324 }
325 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
326 }
327
328 {
milind-u086d7262022-01-19 20:44:18 -0800329 auto builder = auto_mode_sender_.MakeBuilder();
330
331 uint32_t mode = 0;
332 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
333 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
334 mode |= 1 << i;
335 }
336 }
337
338 auto auto_mode_builder =
339 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
340
341 auto_mode_builder.add_mode(mode);
342
343 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
344 }
345 }
346
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800347 void set_climber_potentiometer(
348 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
349 climber_potentiometer_ = ::std::move(potentiometer);
350 }
351
Griffin Buibcbef482022-02-23 15:32:10 -0800352 void set_flipper_arm_left_potentiometer(
353 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
354 flipper_arm_left_potentiometer_ = ::std::move(potentiometer);
355 }
356
357 void set_flipper_arm_right_potentiometer(
358 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
359 flipper_arm_right_potentiometer_ = ::std::move(potentiometer);
360 }
361
Austin Schuhda7e3e12022-03-26 15:14:31 -0700362 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
363
364 void set_superstructure_reading(
365 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
366 superstructure_reading_ = superstructure_reading;
367 }
368
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800369 void set_intake_encoder_front(::std::unique_ptr<frc::Encoder> encoder) {
370 fast_encoder_filter_.Add(encoder.get());
371 intake_encoder_front_.set_encoder(::std::move(encoder));
372 }
373
374 void set_intake_encoder_back(::std::unique_ptr<frc::Encoder> encoder) {
375 fast_encoder_filter_.Add(encoder.get());
376 intake_encoder_back_.set_encoder(::std::move(encoder));
377 }
378
379 void set_intake_front_absolute_pwm(
380 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
381 intake_encoder_front_.set_absolute_pwm(::std::move(absolute_pwm));
382 }
383
384 void set_intake_front_potentiometer(
385 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
386 intake_encoder_front_.set_potentiometer(::std::move(potentiometer));
387 }
388
389 void set_intake_back_absolute_pwm(
390 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
391 intake_encoder_back_.set_absolute_pwm(::std::move(absolute_pwm));
392 }
393
394 void set_intake_back_potentiometer(
395 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
396 intake_encoder_back_.set_potentiometer(::std::move(potentiometer));
397 }
398
399 void set_turret_encoder(::std::unique_ptr<frc::Encoder> encoder) {
400 medium_encoder_filter_.Add(encoder.get());
401 turret_encoder_.set_encoder(::std::move(encoder));
402 }
403
404 void set_turret_absolute_pwm(
405 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
406 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
407 }
408
409 void set_turret_potentiometer(
410 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
411 turret_encoder_.set_potentiometer(::std::move(potentiometer));
412 }
413
Milo Lin4950ac52022-02-25 19:56:11 -0800414 void set_intake_beambreak_front(::std::unique_ptr<frc::DigitalInput> sensor) {
415 intake_beambreak_front_ = ::std::move(sensor);
416 }
417 void set_intake_beambreak_back(::std::unique_ptr<frc::DigitalInput> sensor) {
418 intake_beambreak_back_ = ::std::move(sensor);
419 }
420 void set_turret_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
421 turret_beambreak_ = ::std::move(sensor);
422 }
423
milind-u086d7262022-01-19 20:44:18 -0800424 private:
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800425 std::shared_ptr<const Values> values_;
426
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800427 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800428 aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800429 aos::Sender<frc971::control_loops::drivetrain::Position>
milind-u086d7262022-01-19 20:44:18 -0800430 drivetrain_position_sender_;
Ravago Jones0e86e242022-02-12 18:38:14 -0800431 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
milind-u086d7262022-01-19 20:44:18 -0800432
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800433 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800434
Milo Lin4950ac52022-02-25 19:56:11 -0800435 std::unique_ptr<frc::DigitalInput> intake_beambreak_front_,
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800436 intake_beambreak_back_, turret_beambreak_, imu_heading_input_,
437 imu_yaw_rate_input_;
Milo Lin4950ac52022-02-25 19:56:11 -0800438
Griffin Buibcbef482022-02-23 15:32:10 -0800439 std::unique_ptr<frc::AnalogInput> climber_potentiometer_,
440 flipper_arm_right_potentiometer_, flipper_arm_left_potentiometer_;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800441 frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_encoder_front_,
Ravago Jones0e86e242022-02-12 18:38:14 -0800442 intake_encoder_back_, turret_encoder_, catapult_encoder_;
Austin Schuh39f26f62022-02-24 21:34:46 -0800443
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800444 frc971::wpilib::DMAPulseWidthReader imu_heading_reader_, imu_yaw_rate_reader_;
Austin Schuhda7e3e12022-03-26 15:14:31 -0700445
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800446 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> catapult_falcon_1_can_,
447 catapult_falcon_2_can_;
milind-u086d7262022-01-19 20:44:18 -0800448};
449
450class SuperstructureWriter
451 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
452 public:
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800453 SuperstructureWriter(aos::EventLoop *event_loop)
454 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
Austin Schuhda7e3e12022-03-26 15:14:31 -0700455 event_loop, "/superstructure"),
456 catapult_reversal_(make_unique<frc::DigitalOutput>(0)) {}
milind-u086d7262022-01-19 20:44:18 -0800457
milind-u6e7d8d42022-04-06 18:30:43 -0700458 void set_climber_servo_left(::std::unique_ptr<::frc::Servo> t) {
459 climber_servo_left_ = ::std::move(t);
460 }
461 void set_climber_servo_right(::std::unique_ptr<::frc::Servo> t) {
462 climber_servo_right_ = ::std::move(t);
463 }
464
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800465 void set_climber_falcon(std::unique_ptr<frc::TalonFX> t) {
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800466 climber_falcon_ = std::move(t);
Griffin Bui67abb912022-01-22 16:16:21 -0800467 }
468
Jacob Ismael322ebb92022-02-09 20:12:47 -0800469 void set_turret_falcon(::std::unique_ptr<::frc::TalonFX> t) {
470 turret_falcon_ = ::std::move(t);
471 }
472
473 void set_catapult_falcon_1(::std::unique_ptr<::frc::TalonFX> t) {
474 catapult_falcon_1_ = ::std::move(t);
475 }
476
Austin Schuhe040ddc2022-03-17 00:16:47 -0700477 void set_catapult_falcon_1(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800478 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> t1,
479 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> t2) {
Austin Schuhe040ddc2022-03-17 00:16:47 -0700480 catapult_falcon_1_can_ = ::std::move(t1);
481 catapult_falcon_2_can_ = ::std::move(t2);
482
483 for (auto &falcon : {catapult_falcon_1_can_, catapult_falcon_2_can_}) {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800484 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
485 current_limits.StatorCurrentLimit =
486 Values::kIntakeRollerStatorCurrentLimit();
487 current_limits.StatorCurrentLimitEnable = true;
488 current_limits.SupplyCurrentLimit =
489 Values::kIntakeRollerSupplyCurrentLimit();
490 current_limits.SupplyCurrentLimitEnable = true;
491
492 ctre::phoenix6::configs::TalonFXConfiguration configuration;
493 configuration.CurrentLimits = current_limits;
494
495 ctre::phoenix::StatusCode status =
496 falcon->GetConfigurator().Apply(configuration);
497 if (!status.IsOK()) {
498 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
499 status.GetName(), status.GetDescription());
500 }
501
502 PrintConfigs(falcon.get());
503
504 // TODO(max): Figure out how to migrate these configs to phoenix6
505 /*falcon->SetStatusFramePeriod(
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700506 ctre::phoenix::motorcontrol::Status_1_General, 1);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700507 falcon->SetStatusFramePeriod(
508 ctre::phoenix::motorcontrol::Status_Brushless_Current, 50);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800509
Austin Schuhda7e3e12022-03-26 15:14:31 -0700510 falcon->ConfigOpenloopRamp(0.0);
511 falcon->ConfigClosedloopRamp(0.0);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800512 falcon->ConfigVoltageMeasurementFilter(1);*/
Austin Schuhe040ddc2022-03-17 00:16:47 -0700513 }
Austin Schuhe040ddc2022-03-17 00:16:47 -0700514 }
515
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800516 void set_intake_falcon_front(::std::unique_ptr<frc::TalonFX> t) {
517 intake_falcon_front_ = ::std::move(t);
Griffin Bui67abb912022-01-22 16:16:21 -0800518 }
milind-u086d7262022-01-19 20:44:18 -0800519
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800520 void set_intake_falcon_back(::std::unique_ptr<frc::TalonFX> t) {
521 intake_falcon_back_ = ::std::move(t);
522 }
523
524 void set_roller_falcon_front(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800525 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800526 roller_falcon_front_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800527 WriteConfigs(roller_falcon_front_.get(),
528 Values::kIntakeRollerStatorCurrentLimit(),
529 Values::kIntakeRollerSupplyCurrentLimit());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800530 }
531
532 void set_roller_falcon_back(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800533 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800534 roller_falcon_back_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800535 WriteConfigs(roller_falcon_back_.get(),
536 Values::kIntakeRollerStatorCurrentLimit(),
537 Values::kIntakeRollerSupplyCurrentLimit());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800538 }
539
Griffin Buibcbef482022-02-23 15:32:10 -0800540 void set_flipper_arms_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800541 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800542 flipper_arms_falcon_ = t;
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800543 WriteConfigs(flipper_arms_falcon_.get(),
544 Values::kFlipperArmSupplyCurrentLimit(),
545 Values::kFlipperArmStatorCurrentLimit());
Griffin Buibcbef482022-02-23 15:32:10 -0800546 }
547
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800548 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> flipper_arms_falcon() {
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800549 return flipper_arms_falcon_;
550 }
551
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700552 void set_transfer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) {
553 transfer_roller_victor_ = ::std::move(t);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800554 }
555
Austin Schuhda7e3e12022-03-26 15:14:31 -0700556 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
557
558 void set_superstructure_reading(
559 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
560 superstructure_reading_ = superstructure_reading;
561 }
562
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800563 private:
Griffin Bui67abb912022-01-22 16:16:21 -0800564 void Stop() override {
565 AOS_LOG(WARNING, "Superstructure output too old.\n");
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800566 climber_falcon_->SetDisabled();
milind-u6e7d8d42022-04-06 18:30:43 -0700567 climber_servo_left_->SetRaw(0);
568 climber_servo_right_->SetRaw(0);
569
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800570 Disable(roller_falcon_front_.get());
571 Disable(roller_falcon_back_.get());
572 Disable(flipper_arms_falcon_.get());
573
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800574 intake_falcon_front_->SetDisabled();
575 intake_falcon_back_->SetDisabled();
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700576 transfer_roller_victor_->SetDisabled();
Austin Schuhe040ddc2022-03-17 00:16:47 -0700577 if (catapult_falcon_1_) {
578 catapult_falcon_1_->SetDisabled();
579 }
580 if (catapult_falcon_1_can_) {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800581 Disable(catapult_falcon_1_can_.get());
582 Disable(catapult_falcon_2_can_.get());
Austin Schuhe040ddc2022-03-17 00:16:47 -0700583 }
Jacob Ismael322ebb92022-02-09 20:12:47 -0800584 turret_falcon_->SetDisabled();
Griffin Bui67abb912022-01-22 16:16:21 -0800585 }
586
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800587 void Write(const superstructure::Output &output) override {
Austin Schuh8507c9f2022-03-13 18:08:28 -0700588 WritePwm(-output.climber_voltage(), climber_falcon_.get());
milind-u6e7d8d42022-04-06 18:30:43 -0700589 climber_servo_left_->SetPosition(output.climber_servo_left());
590 climber_servo_right_->SetPosition(output.climber_servo_right());
Griffin Buibcbef482022-02-23 15:32:10 -0800591
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800592 WritePwm(output.intake_voltage_front(), intake_falcon_front_.get());
593 WritePwm(output.intake_voltage_back(), intake_falcon_back_.get());
594 WriteCan(output.roller_voltage_front(), roller_falcon_front_.get());
595 WriteCan(output.roller_voltage_back(), roller_falcon_back_.get());
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700596 WritePwm(output.transfer_roller_voltage(), transfer_roller_victor_.get());
Griffin Buibcbef482022-02-23 15:32:10 -0800597
Austin Schuh465a2882022-03-05 15:39:04 -0800598 WriteCan(-output.flipper_arms_voltage(), flipper_arms_falcon_.get());
Griffin Buibcbef482022-02-23 15:32:10 -0800599
Austin Schuhe040ddc2022-03-17 00:16:47 -0700600 if (catapult_falcon_1_) {
601 WritePwm(output.catapult_voltage(), catapult_falcon_1_.get());
Austin Schuhda7e3e12022-03-26 15:14:31 -0700602 superstructure_reading_->Set(false);
603 if (output.catapult_voltage() > 0) {
604 catapult_reversal_->Set(true);
605 } else {
606 catapult_reversal_->Set(false);
607 }
Austin Schuhe040ddc2022-03-17 00:16:47 -0700608 }
609 if (catapult_falcon_1_can_) {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800610 WriteCanCatapult(output.catapult_voltage(), catapult_falcon_1_can_.get());
611 WriteCanCatapult(output.catapult_voltage(), catapult_falcon_2_can_.get());
Austin Schuhe040ddc2022-03-17 00:16:47 -0700612 }
Griffin Buibcbef482022-02-23 15:32:10 -0800613
Austin Schuh465a2882022-03-05 15:39:04 -0800614 WritePwm(-output.turret_voltage(), turret_falcon_.get());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800615 }
616
617 static void WriteCan(const double voltage,
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800618 ::ctre::phoenix6::hardware::TalonFX *falcon) {
619 ctre::phoenix6::controls::DutyCycleOut control(
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800620 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800621 control.UpdateFreqHz = 0_Hz;
622 control.EnableFOC = true;
623
624 falcon->SetControl(control);
625 }
626 // We do this to set our UpdateFreqHz higher
627 static void WriteCanCatapult(const double voltage,
628 ::ctre::phoenix6::hardware::TalonFX *falcon) {
629 ctre::phoenix6::controls::DutyCycleOut control(
630 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
631 control.UpdateFreqHz = 1000_Hz;
632 control.EnableFOC = true;
633
634 falcon->SetControl(control);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800635 }
636
637 template <typename T>
638 static void WritePwm(const double voltage, T *motor) {
639 motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) /
640 12.0);
641 }
642
643 ::std::unique_ptr<frc::TalonFX> intake_falcon_front_, intake_falcon_back_;
644
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800645 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> roller_falcon_front_,
646 roller_falcon_back_;
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800647
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800648 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> flipper_arms_falcon_;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800649
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800650 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> catapult_falcon_1_can_,
651 catapult_falcon_2_can_;
Austin Schuhe040ddc2022-03-17 00:16:47 -0700652
Jacob Ismael322ebb92022-02-09 20:12:47 -0800653 ::std::unique_ptr<::frc::TalonFX> turret_falcon_, catapult_falcon_1_,
Austin Schuh465a2882022-03-05 15:39:04 -0800654 climber_falcon_;
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700655 ::std::unique_ptr<::frc::VictorSP> transfer_roller_victor_;
Austin Schuhda7e3e12022-03-26 15:14:31 -0700656
657 std::unique_ptr<frc::DigitalOutput> catapult_reversal_;
milind-u6e7d8d42022-04-06 18:30:43 -0700658
659 ::std::unique_ptr<::frc::Servo> climber_servo_left_, climber_servo_right_;
milind-u086d7262022-01-19 20:44:18 -0800660};
661
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800662class CANSensorReader {
663 public:
664 CANSensorReader(aos::EventLoop *event_loop)
665 : event_loop_(event_loop),
666 can_position_sender_(
667 event_loop->MakeSender<superstructure::CANPosition>(
668 "/superstructure")) {
669 event_loop->SetRuntimeRealtimePriority(16);
670
671 phased_loop_handler_ =
672 event_loop_->AddPhasedLoop([this](int) { Loop(); }, kPeriod);
673 phased_loop_handler_->set_name("CAN SensorReader Loop");
674
675 event_loop->OnRun([this]() { Loop(); });
676 }
677
678 void set_flipper_arms_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800679 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800680 flipper_arms_falcon_ = std::move(t);
681 }
682
683 private:
684 void Loop() {
685 auto builder = can_position_sender_.MakeBuilder();
686 superstructure::CANPosition::Builder can_position_builder =
687 builder.MakeBuilder<superstructure::CANPosition>();
688 can_position_builder.add_flipper_arm_integrated_sensor_velocity(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800689 flipper_arms_falcon_->GetVelocity().GetValue().value() *
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800690 kVelocityConversion);
691 builder.CheckOk(builder.Send(can_position_builder.Finish()));
692 }
693
694 static constexpr std::chrono::milliseconds kPeriod =
695 std::chrono::milliseconds(20);
696 // 2048 encoder counts / 100 ms to rad/sec
697 static constexpr double kVelocityConversion = (2.0 * M_PI / 2048) * 0.100;
698 aos::EventLoop *event_loop_;
699 ::aos::PhasedLoopHandler *phased_loop_handler_;
700
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800701 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> flipper_arms_falcon_;
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800702 aos::Sender<superstructure::CANPosition> can_position_sender_;
703};
704
milind-u086d7262022-01-19 20:44:18 -0800705class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
706 public:
707 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
708 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
709 frc::Encoder::k4X);
710 }
711
712 void Run() override {
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800713 std::shared_ptr<const Values> values =
714 std::make_shared<const Values>(constants::MakeValues());
715
milind-u086d7262022-01-19 20:44:18 -0800716 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800717 aos::configuration::ReadConfig("aos_config.json");
milind-u086d7262022-01-19 20:44:18 -0800718
719 // Thread 1.
720 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
721 ::frc971::wpilib::JoystickSender joystick_sender(
722 &joystick_sender_event_loop);
723 AddLoop(&joystick_sender_event_loop);
724
725 // Thread 2.
726 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
727 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800728 AddLoop(&pdp_fetcher_event_loop);
milind-u086d7262022-01-19 20:44:18 -0800729
Austin Schuhda7e3e12022-03-26 15:14:31 -0700730 std::shared_ptr<frc::DigitalOutput> superstructure_reading =
731 make_unique<frc::DigitalOutput>(25);
732
milind-u086d7262022-01-19 20:44:18 -0800733 // Thread 3.
734 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800735 SensorReader sensor_reader(&sensor_reader_event_loop, values);
Austin Schuh028d81d2022-03-26 15:11:42 -0700736 sensor_reader.set_pwm_trigger(true);
Austin Schuha8014282022-03-05 12:36:38 -0800737 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
738 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
Austin Schuhda7e3e12022-03-26 15:14:31 -0700739 sensor_reader.set_superstructure_reading(superstructure_reading);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800740
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800741 sensor_reader.set_intake_encoder_front(make_encoder(3));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800742 sensor_reader.set_intake_front_absolute_pwm(
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800743 make_unique<frc::DigitalInput>(3));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800744 sensor_reader.set_intake_front_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800745 make_unique<frc::AnalogInput>(3));
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800746
747 sensor_reader.set_intake_encoder_back(make_encoder(4));
748 sensor_reader.set_intake_back_absolute_pwm(
749 make_unique<frc::DigitalInput>(4));
750 sensor_reader.set_intake_back_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800751 make_unique<frc::AnalogInput>(4));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800752
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800753 sensor_reader.set_turret_encoder(make_encoder(5));
754 sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(5));
Austin Schuha8014282022-03-05 12:36:38 -0800755 sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(5));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800756
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800757 // TODO(milind): correct intake beambreak ports once set
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800758 sensor_reader.set_intake_beambreak_front(make_unique<frc::DigitalInput>(1));
Austin Schuh445ae832022-03-05 22:52:23 -0800759 sensor_reader.set_intake_beambreak_back(make_unique<frc::DigitalInput>(6));
Milo Lin4950ac52022-02-25 19:56:11 -0800760 sensor_reader.set_turret_beambreak(make_unique<frc::DigitalInput>(7));
761
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800762 sensor_reader.set_climber_potentiometer(make_unique<frc::AnalogInput>(7));
milind-u086d7262022-01-19 20:44:18 -0800763
Griffin Buibcbef482022-02-23 15:32:10 -0800764 sensor_reader.set_flipper_arm_left_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800765 make_unique<frc::AnalogInput>(0));
Griffin Buibcbef482022-02-23 15:32:10 -0800766 sensor_reader.set_flipper_arm_right_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800767 make_unique<frc::AnalogInput>(1));
Griffin Buibcbef482022-02-23 15:32:10 -0800768
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800769 // TODO(milind): correct catapult encoder and absolute pwm ports
Austin Schuha8014282022-03-05 12:36:38 -0800770 sensor_reader.set_catapult_encoder(make_encoder(2));
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800771 sensor_reader.set_catapult_absolute_pwm(
Austin Schuha8014282022-03-05 12:36:38 -0800772 std::make_unique<frc::DigitalInput>(2));
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800773 sensor_reader.set_catapult_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800774 std::make_unique<frc::AnalogInput>(2));
Austin Schuh39f26f62022-02-24 21:34:46 -0800775
Austin Schuhf9a166e2022-03-05 17:53:12 -0800776 sensor_reader.set_heading_input(make_unique<frc::DigitalInput>(9));
777 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(8));
Ravago Jones0e86e242022-02-12 18:38:14 -0800778
milind-u086d7262022-01-19 20:44:18 -0800779 AddLoop(&sensor_reader_event_loop);
780
781 // Thread 4.
782 ::aos::ShmEventLoop output_event_loop(&config.message());
783 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
784 drivetrain_writer.set_left_controller0(
Austin Schuh2d22fb12022-03-06 14:43:29 -0800785 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), false);
Austin Schuha8014282022-03-05 12:36:38 -0800786 drivetrain_writer.set_right_controller0(
Austin Schuh2d22fb12022-03-06 14:43:29 -0800787 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), true);
milind-u086d7262022-01-19 20:44:18 -0800788
789 SuperstructureWriter superstructure_writer(&output_event_loop);
790
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800791 superstructure_writer.set_turret_falcon(make_unique<::frc::TalonFX>(3));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800792 superstructure_writer.set_roller_falcon_front(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800793 make_unique<::ctre::phoenix6::hardware::TalonFX>(0));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800794 superstructure_writer.set_roller_falcon_back(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800795 make_unique<::ctre::phoenix6::hardware::TalonFX>(1));
Austin Schuh465a2882022-03-05 15:39:04 -0800796
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700797 superstructure_writer.set_transfer_roller_victor(
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800798 make_unique<::frc::VictorSP>(5));
Austin Schuh465a2882022-03-05 15:39:04 -0800799
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800800 superstructure_writer.set_intake_falcon_front(make_unique<frc::TalonFX>(2));
801 superstructure_writer.set_intake_falcon_back(make_unique<frc::TalonFX>(4));
Austin Schuh465a2882022-03-05 15:39:04 -0800802 superstructure_writer.set_climber_falcon(make_unique<frc::TalonFX>(8));
milind-u6e7d8d42022-04-06 18:30:43 -0700803 superstructure_writer.set_climber_servo_left(make_unique<frc::Servo>(7));
804 superstructure_writer.set_climber_servo_right(make_unique<frc::Servo>(6));
Griffin Buibcbef482022-02-23 15:32:10 -0800805 superstructure_writer.set_flipper_arms_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800806 make_unique<::ctre::phoenix6::hardware::TalonFX>(2));
Austin Schuhda7e3e12022-03-26 15:14:31 -0700807 superstructure_writer.set_superstructure_reading(superstructure_reading);
Griffin Bui67abb912022-01-22 16:16:21 -0800808
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700809 if (!absl::GetFlag(FLAGS_can_catapult)) {
Austin Schuhda7e3e12022-03-26 15:14:31 -0700810 superstructure_writer.set_catapult_falcon_1(make_unique<frc::TalonFX>(9));
811 } else {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800812 std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> catapult1 =
813 make_unique<::ctre::phoenix6::hardware::TalonFX>(3, "Catapult");
814 std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> catapult2 =
815 make_unique<::ctre::phoenix6::hardware::TalonFX>(4, "Catapult");
Austin Schuhda7e3e12022-03-26 15:14:31 -0700816 superstructure_writer.set_catapult_falcon_1(catapult1, catapult2);
817 sensor_reader.set_catapult_falcon_1(catapult1, catapult2);
818 }
Austin Schuh39f26f62022-02-24 21:34:46 -0800819
milind-u086d7262022-01-19 20:44:18 -0800820 AddLoop(&output_event_loop);
821
Henry Speiser77747b72022-03-06 17:18:29 -0800822 // Thread 5.
823 ::aos::ShmEventLoop led_indicator_event_loop(&config.message());
824 control_loops::superstructure::LedIndicator led_indicator(
825 &led_indicator_event_loop);
826 AddLoop(&led_indicator_event_loop);
827
milind-u086d7262022-01-19 20:44:18 -0800828 RunLoops();
829 }
830};
831
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800832} // namespace y2022::wpilib
milind-u086d7262022-01-19 20:44:18 -0800833
834AOS_ROBOT_CLASS(::y2022::wpilib::WPILibRobot);