blob: 55cf5104de8e897711e900035422c6d1a4557a55 [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
milind-u086d7262022-01-19 20:44:18 -080068namespace y2022 {
69namespace wpilib {
70namespace {
71
72constexpr double kMaxBringupPower = 12.0;
73
74// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
75// DMA stuff and then removing the * 2.0 in *_translate.
76// The low bit is direction.
77
milind-u086d7262022-01-19 20:44:18 -080078double drivetrain_velocity_translate(double in) {
79 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
80 (2.0 * M_PI)) *
81 Values::kDrivetrainEncoderRatio() *
82 control_loops::drivetrain::kWheelRadius;
83}
84
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080085double climber_pot_translate(double voltage) {
Nathan Leong342b85e2023-01-08 13:49:33 -080086 return voltage * Values::kClimberPotMetersPerVolt();
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080087}
88
Griffin Buibcbef482022-02-23 15:32:10 -080089double flipper_arms_pot_translate(double voltage) {
Nathan Leong342b85e2023-01-08 13:49:33 -080090 return voltage * Values::kFlipperArmsPotRadiansPerVolt();
Griffin Buibcbef482022-02-23 15:32:10 -080091}
92
Henry Speiser55aa3ba2022-02-21 23:21:12 -080093double intake_pot_translate(double voltage) {
Nathan Leong342b85e2023-01-08 13:49:33 -080094 return voltage * Values::kIntakePotRadiansPerVolt();
Henry Speiser55aa3ba2022-02-21 23:21:12 -080095}
96
97double turret_pot_translate(double voltage) {
Nathan Leong342b85e2023-01-08 13:49:33 -080098 return voltage * Values::kTurretPotRadiansPerVolt();
Henry Speiser55aa3ba2022-02-21 23:21:12 -080099}
100
milind-u086d7262022-01-19 20:44:18 -0800101constexpr double kMaxFastEncoderPulsesPerSecond =
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800102 std::max({Values::kMaxDrivetrainEncoderPulsesPerSecond(),
103 Values::kMaxIntakeEncoderPulsesPerSecond()});
milind-u086d7262022-01-19 20:44:18 -0800104static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
105 "fast encoders are too fast");
106constexpr double kMaxMediumEncoderPulsesPerSecond =
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800107 Values::kMaxTurretEncoderPulsesPerSecond();
milind-u086d7262022-01-19 20:44:18 -0800108
109static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
110 "medium encoders are too fast");
111
Austin Schuh39f26f62022-02-24 21:34:46 -0800112double catapult_pot_translate(double voltage) {
113 return voltage * Values::kCatapultPotRatio() *
114 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
115}
116
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800117void PrintConfigs(ctre::phoenix6::hardware::TalonFX *talon) {
118 ctre::phoenix6::configs::TalonFXConfiguration configuration;
119 ctre::phoenix::StatusCode status =
120 talon->GetConfigurator().Refresh(configuration);
121 if (!status.IsOK()) {
122 AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s",
123 status.GetName(), status.GetDescription());
124 }
125 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
126}
127
128void WriteConfigs(ctre::phoenix6::hardware::TalonFX *talon,
129 double stator_current_limit, double supply_current_limit) {
130 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
131 current_limits.StatorCurrentLimit = stator_current_limit;
132 current_limits.StatorCurrentLimitEnable = true;
133 current_limits.SupplyCurrentLimit = supply_current_limit;
134 current_limits.SupplyCurrentLimitEnable = true;
135
136 ctre::phoenix6::configs::TalonFXConfiguration configuration;
137 configuration.CurrentLimits = current_limits;
138
139 ctre::phoenix::StatusCode status =
140 talon->GetConfigurator().Apply(configuration);
141 if (!status.IsOK()) {
142 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
143 status.GetName(), status.GetDescription());
144 }
145
146 PrintConfigs(talon);
147}
148
149void Disable(ctre::phoenix6::hardware::TalonFX *talon) {
150 ctre::phoenix6::controls::DutyCycleOut stop_command(0.0);
151 stop_command.UpdateFreqHz = 0_Hz;
152 stop_command.EnableFOC = true;
153
154 talon->SetControl(stop_command);
155}
156
milind-u086d7262022-01-19 20:44:18 -0800157} // namespace
158
159// Class to send position messages with sensor readings to our loops.
160class SensorReader : public ::frc971::wpilib::SensorReader {
161 public:
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800162 SensorReader(::aos::ShmEventLoop *event_loop,
163 std::shared_ptr<const Values> values)
milind-u086d7262022-01-19 20:44:18 -0800164 : ::frc971::wpilib::SensorReader(event_loop),
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800165 values_(std::move(values)),
milind-u086d7262022-01-19 20:44:18 -0800166 auto_mode_sender_(
167 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
168 "/autonomous")),
169 superstructure_position_sender_(
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800170 event_loop->MakeSender<superstructure::PositionStatic>(
milind-u086d7262022-01-19 20:44:18 -0800171 "/superstructure")),
172 drivetrain_position_sender_(
173 event_loop
174 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
Ravago Jones0e86e242022-02-12 18:38:14 -0800175 "/drivetrain")),
176 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
177 "/drivetrain")) {
milind-u086d7262022-01-19 20:44:18 -0800178 // Set to filter out anything shorter than 1/4 of the minimum pulse width
179 // we should ever see.
180 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
181 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
182 }
183
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800184 void Start() override {
185 // TODO(Ravago): Figure out why adding multiple DMA readers results in weird
186 // behavior
187 // AddToDMA(&imu_heading_reader_);
188 AddToDMA(&imu_yaw_rate_reader_);
189 }
190
milind-u086d7262022-01-19 20:44:18 -0800191 // Auto mode switches.
192 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
193 autonomous_modes_.at(i) = ::std::move(sensor);
194 }
195
Austin Schuh39f26f62022-02-24 21:34:46 -0800196 void set_catapult_encoder(::std::unique_ptr<frc::Encoder> encoder) {
197 medium_encoder_filter_.Add(encoder.get());
198 catapult_encoder_.set_encoder(::std::move(encoder));
199 }
200
201 void set_catapult_absolute_pwm(
202 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
203 catapult_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
204 }
205
206 void set_catapult_potentiometer(
207 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
208 catapult_encoder_.set_potentiometer(::std::move(potentiometer));
209 }
210
Ravago Jones0e86e242022-02-12 18:38:14 -0800211 void set_heading_input(::std::unique_ptr<frc::DigitalInput> sensor) {
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800212 imu_heading_input_ = ::std::move(sensor);
213 imu_heading_reader_.set_input(imu_heading_input_.get());
Ravago Jones0e86e242022-02-12 18:38:14 -0800214 }
215
216 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800217 imu_yaw_rate_input_ = ::std::move(sensor);
218 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
Ravago Jones0e86e242022-02-12 18:38:14 -0800219 }
Austin Schuhda7e3e12022-03-26 15:14:31 -0700220 void set_catapult_falcon_1(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800221 ::std::shared_ptr<ctre::phoenix6::hardware::TalonFX> t1,
222 ::std::shared_ptr<ctre::phoenix6::hardware::TalonFX> t2) {
Austin Schuhda7e3e12022-03-26 15:14:31 -0700223 catapult_falcon_1_can_ = ::std::move(t1);
224 catapult_falcon_2_can_ = ::std::move(t2);
225 }
Ravago Jones0e86e242022-02-12 18:38:14 -0800226
milind-u086d7262022-01-19 20:44:18 -0800227 void RunIteration() override {
Austin Schuhda7e3e12022-03-26 15:14:31 -0700228 superstructure_reading_->Set(true);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800229 {
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800230 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
231 superstructure_position_sender_.MakeStaticBuilder();
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800232
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800233 CopyPosition(catapult_encoder_, builder->add_catapult(),
Austin Schuh39f26f62022-02-24 21:34:46 -0800234 Values::kCatapultEncoderCountsPerRevolution(),
235 Values::kCatapultEncoderRatio(), catapult_pot_translate,
Austin Schuh275f9812022-03-05 14:02:37 -0800236 false, values_->catapult.potentiometer_offset);
Austin Schuh39f26f62022-02-24 21:34:46 -0800237
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800238 CopyPosition(*climber_potentiometer_, builder->add_climber(),
239 climber_pot_translate, false,
240 values_->climber.potentiometer_offset);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800241
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800242 CopyPosition(*flipper_arm_left_potentiometer_,
243 builder->add_flipper_arm_left(), flipper_arms_pot_translate,
244 false, values_->flipper_arm_left.potentiometer_offset);
Griffin Buibcbef482022-02-23 15:32:10 -0800245
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800246 CopyPosition(*flipper_arm_right_potentiometer_,
247 builder->add_flipper_arm_right(), flipper_arms_pot_translate,
248 true, values_->flipper_arm_right.potentiometer_offset);
Griffin Buibcbef482022-02-23 15:32:10 -0800249
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800250 // Intake
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800251 CopyPosition(intake_encoder_front_, builder->add_intake_front(),
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800252 Values::kIntakeEncoderCountsPerRevolution(),
Austin Schuh275f9812022-03-05 14:02:37 -0800253 Values::kIntakeEncoderRatio(), intake_pot_translate, true,
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800254 values_->intake_front.potentiometer_offset);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800255 CopyPosition(intake_encoder_back_, builder->add_intake_back(),
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800256 Values::kIntakeEncoderCountsPerRevolution(),
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800257 Values::kIntakeEncoderRatio(), intake_pot_translate, true,
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800258 values_->intake_back.potentiometer_offset);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800259 CopyPosition(turret_encoder_, builder->add_turret(),
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800260 Values::kTurretEncoderCountsPerRevolution(),
261 Values::kTurretEncoderRatio(), turret_pot_translate, false,
262 values_->turret.potentiometer_offset);
263
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800264 builder->set_intake_beambreak_front(intake_beambreak_front_->Get());
265 builder->set_intake_beambreak_back(intake_beambreak_back_->Get());
266 builder->set_turret_beambreak(turret_beambreak_->Get());
267 builder.CheckOk(builder.Send());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800268 }
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800269
milind-u086d7262022-01-19 20:44:18 -0800270 {
271 auto builder = drivetrain_position_sender_.MakeBuilder();
272 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
273 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
274 drivetrain_builder.add_left_encoder(
James Kuszmaul53507e12022-02-12 18:36:40 -0800275 constants::Values::DrivetrainEncoderToMeters(
276 drivetrain_left_encoder_->GetRaw()));
milind-u086d7262022-01-19 20:44:18 -0800277 drivetrain_builder.add_left_speed(
278 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
279
280 drivetrain_builder.add_right_encoder(
James Kuszmaul53507e12022-02-12 18:36:40 -0800281 -constants::Values::DrivetrainEncoderToMeters(
282 drivetrain_right_encoder_->GetRaw()));
milind-u086d7262022-01-19 20:44:18 -0800283 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
284 drivetrain_right_encoder_->GetPeriod()));
285
286 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
287 }
288
289 {
Ravago Jones0e86e242022-02-12 18:38:14 -0800290 auto builder = gyro_sender_.MakeBuilder();
291 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
292 builder.MakeBuilder<::frc971::sensors::GyroReading>();
James Kuszmaulf34e7fd2022-03-13 20:30:34 -0700293 // +/- 2000 deg / sec
294 constexpr double kMaxVelocity = 4000; // degrees / second
Ravago Jones0e86e242022-02-12 18:38:14 -0800295 constexpr double kVelocityRadiansPerSecond =
296 kMaxVelocity / 360 * (2.0 * M_PI);
297
298 // Only part of the full range is used to prevent being 100% on or off.
299 constexpr double kScaledRangeLow = 0.1;
300 constexpr double kScaledRangeHigh = 0.9;
301
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800302 constexpr double kPWMFrequencyHz = 200;
303 double heading_duty_cycle =
304 imu_heading_reader_.last_width() * kPWMFrequencyHz;
305 double velocity_duty_cycle =
306 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
307
Ravago Jones0e86e242022-02-12 18:38:14 -0800308 constexpr double kDutyCycleScale =
309 1 / (kScaledRangeHigh - kScaledRangeLow);
Ravago Jones0e86e242022-02-12 18:38:14 -0800310 // scale from 0.1 - 0.9 to 0 - 1
311 double rescaled_heading_duty_cycle =
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800312 (heading_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
Ravago Jones0e86e242022-02-12 18:38:14 -0800313 double rescaled_velocity_duty_cycle =
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800314 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
Ravago Jones0e86e242022-02-12 18:38:14 -0800315
316 if (!std::isnan(rescaled_heading_duty_cycle)) {
317 gyro_reading_builder.add_angle(rescaled_heading_duty_cycle *
318 (2.0 * M_PI));
319 }
320 if (!std::isnan(rescaled_velocity_duty_cycle)) {
321 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
322 kVelocityRadiansPerSecond);
323 }
324 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
325 }
326
327 {
milind-u086d7262022-01-19 20:44:18 -0800328 auto builder = auto_mode_sender_.MakeBuilder();
329
330 uint32_t mode = 0;
331 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
332 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
333 mode |= 1 << i;
334 }
335 }
336
337 auto auto_mode_builder =
338 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
339
340 auto_mode_builder.add_mode(mode);
341
342 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
343 }
344 }
345
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800346 void set_climber_potentiometer(
347 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
348 climber_potentiometer_ = ::std::move(potentiometer);
349 }
350
Griffin Buibcbef482022-02-23 15:32:10 -0800351 void set_flipper_arm_left_potentiometer(
352 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
353 flipper_arm_left_potentiometer_ = ::std::move(potentiometer);
354 }
355
356 void set_flipper_arm_right_potentiometer(
357 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
358 flipper_arm_right_potentiometer_ = ::std::move(potentiometer);
359 }
360
Austin Schuhda7e3e12022-03-26 15:14:31 -0700361 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
362
363 void set_superstructure_reading(
364 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
365 superstructure_reading_ = superstructure_reading;
366 }
367
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800368 void set_intake_encoder_front(::std::unique_ptr<frc::Encoder> encoder) {
369 fast_encoder_filter_.Add(encoder.get());
370 intake_encoder_front_.set_encoder(::std::move(encoder));
371 }
372
373 void set_intake_encoder_back(::std::unique_ptr<frc::Encoder> encoder) {
374 fast_encoder_filter_.Add(encoder.get());
375 intake_encoder_back_.set_encoder(::std::move(encoder));
376 }
377
378 void set_intake_front_absolute_pwm(
379 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
380 intake_encoder_front_.set_absolute_pwm(::std::move(absolute_pwm));
381 }
382
383 void set_intake_front_potentiometer(
384 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
385 intake_encoder_front_.set_potentiometer(::std::move(potentiometer));
386 }
387
388 void set_intake_back_absolute_pwm(
389 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
390 intake_encoder_back_.set_absolute_pwm(::std::move(absolute_pwm));
391 }
392
393 void set_intake_back_potentiometer(
394 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
395 intake_encoder_back_.set_potentiometer(::std::move(potentiometer));
396 }
397
398 void set_turret_encoder(::std::unique_ptr<frc::Encoder> encoder) {
399 medium_encoder_filter_.Add(encoder.get());
400 turret_encoder_.set_encoder(::std::move(encoder));
401 }
402
403 void set_turret_absolute_pwm(
404 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
405 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
406 }
407
408 void set_turret_potentiometer(
409 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
410 turret_encoder_.set_potentiometer(::std::move(potentiometer));
411 }
412
Milo Lin4950ac52022-02-25 19:56:11 -0800413 void set_intake_beambreak_front(::std::unique_ptr<frc::DigitalInput> sensor) {
414 intake_beambreak_front_ = ::std::move(sensor);
415 }
416 void set_intake_beambreak_back(::std::unique_ptr<frc::DigitalInput> sensor) {
417 intake_beambreak_back_ = ::std::move(sensor);
418 }
419 void set_turret_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
420 turret_beambreak_ = ::std::move(sensor);
421 }
422
milind-u086d7262022-01-19 20:44:18 -0800423 private:
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800424 std::shared_ptr<const Values> values_;
425
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800426 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800427 aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800428 aos::Sender<frc971::control_loops::drivetrain::Position>
milind-u086d7262022-01-19 20:44:18 -0800429 drivetrain_position_sender_;
Ravago Jones0e86e242022-02-12 18:38:14 -0800430 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
milind-u086d7262022-01-19 20:44:18 -0800431
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800432 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800433
Milo Lin4950ac52022-02-25 19:56:11 -0800434 std::unique_ptr<frc::DigitalInput> intake_beambreak_front_,
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800435 intake_beambreak_back_, turret_beambreak_, imu_heading_input_,
436 imu_yaw_rate_input_;
Milo Lin4950ac52022-02-25 19:56:11 -0800437
Griffin Buibcbef482022-02-23 15:32:10 -0800438 std::unique_ptr<frc::AnalogInput> climber_potentiometer_,
439 flipper_arm_right_potentiometer_, flipper_arm_left_potentiometer_;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800440 frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_encoder_front_,
Ravago Jones0e86e242022-02-12 18:38:14 -0800441 intake_encoder_back_, turret_encoder_, catapult_encoder_;
Austin Schuh39f26f62022-02-24 21:34:46 -0800442
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800443 frc971::wpilib::DMAPulseWidthReader imu_heading_reader_, imu_yaw_rate_reader_;
Austin Schuhda7e3e12022-03-26 15:14:31 -0700444
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800445 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> catapult_falcon_1_can_,
446 catapult_falcon_2_can_;
milind-u086d7262022-01-19 20:44:18 -0800447};
448
449class SuperstructureWriter
450 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
451 public:
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800452 SuperstructureWriter(aos::EventLoop *event_loop)
453 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
Austin Schuhda7e3e12022-03-26 15:14:31 -0700454 event_loop, "/superstructure"),
455 catapult_reversal_(make_unique<frc::DigitalOutput>(0)) {}
milind-u086d7262022-01-19 20:44:18 -0800456
milind-u6e7d8d42022-04-06 18:30:43 -0700457 void set_climber_servo_left(::std::unique_ptr<::frc::Servo> t) {
458 climber_servo_left_ = ::std::move(t);
459 }
460 void set_climber_servo_right(::std::unique_ptr<::frc::Servo> t) {
461 climber_servo_right_ = ::std::move(t);
462 }
463
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800464 void set_climber_falcon(std::unique_ptr<frc::TalonFX> t) {
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800465 climber_falcon_ = std::move(t);
Griffin Bui67abb912022-01-22 16:16:21 -0800466 }
467
Jacob Ismael322ebb92022-02-09 20:12:47 -0800468 void set_turret_falcon(::std::unique_ptr<::frc::TalonFX> t) {
469 turret_falcon_ = ::std::move(t);
470 }
471
472 void set_catapult_falcon_1(::std::unique_ptr<::frc::TalonFX> t) {
473 catapult_falcon_1_ = ::std::move(t);
474 }
475
Austin Schuhe040ddc2022-03-17 00:16:47 -0700476 void set_catapult_falcon_1(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800477 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> t1,
478 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> t2) {
Austin Schuhe040ddc2022-03-17 00:16:47 -0700479 catapult_falcon_1_can_ = ::std::move(t1);
480 catapult_falcon_2_can_ = ::std::move(t2);
481
482 for (auto &falcon : {catapult_falcon_1_can_, catapult_falcon_2_can_}) {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800483 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
484 current_limits.StatorCurrentLimit =
485 Values::kIntakeRollerStatorCurrentLimit();
486 current_limits.StatorCurrentLimitEnable = true;
487 current_limits.SupplyCurrentLimit =
488 Values::kIntakeRollerSupplyCurrentLimit();
489 current_limits.SupplyCurrentLimitEnable = true;
490
491 ctre::phoenix6::configs::TalonFXConfiguration configuration;
492 configuration.CurrentLimits = current_limits;
493
494 ctre::phoenix::StatusCode status =
495 falcon->GetConfigurator().Apply(configuration);
496 if (!status.IsOK()) {
497 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
498 status.GetName(), status.GetDescription());
499 }
500
501 PrintConfigs(falcon.get());
502
503 // TODO(max): Figure out how to migrate these configs to phoenix6
504 /*falcon->SetStatusFramePeriod(
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700505 ctre::phoenix::motorcontrol::Status_1_General, 1);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700506 falcon->SetStatusFramePeriod(
507 ctre::phoenix::motorcontrol::Status_Brushless_Current, 50);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800508
Austin Schuhda7e3e12022-03-26 15:14:31 -0700509 falcon->ConfigOpenloopRamp(0.0);
510 falcon->ConfigClosedloopRamp(0.0);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800511 falcon->ConfigVoltageMeasurementFilter(1);*/
Austin Schuhe040ddc2022-03-17 00:16:47 -0700512 }
Austin Schuhe040ddc2022-03-17 00:16:47 -0700513 }
514
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800515 void set_intake_falcon_front(::std::unique_ptr<frc::TalonFX> t) {
516 intake_falcon_front_ = ::std::move(t);
Griffin Bui67abb912022-01-22 16:16:21 -0800517 }
milind-u086d7262022-01-19 20:44:18 -0800518
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800519 void set_intake_falcon_back(::std::unique_ptr<frc::TalonFX> t) {
520 intake_falcon_back_ = ::std::move(t);
521 }
522
523 void set_roller_falcon_front(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800524 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800525 roller_falcon_front_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800526 WriteConfigs(roller_falcon_front_.get(),
527 Values::kIntakeRollerStatorCurrentLimit(),
528 Values::kIntakeRollerSupplyCurrentLimit());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800529 }
530
531 void set_roller_falcon_back(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800532 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800533 roller_falcon_back_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800534 WriteConfigs(roller_falcon_back_.get(),
535 Values::kIntakeRollerStatorCurrentLimit(),
536 Values::kIntakeRollerSupplyCurrentLimit());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800537 }
538
Griffin Buibcbef482022-02-23 15:32:10 -0800539 void set_flipper_arms_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800540 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800541 flipper_arms_falcon_ = t;
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800542 WriteConfigs(flipper_arms_falcon_.get(),
543 Values::kFlipperArmSupplyCurrentLimit(),
544 Values::kFlipperArmStatorCurrentLimit());
Griffin Buibcbef482022-02-23 15:32:10 -0800545 }
546
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800547 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> flipper_arms_falcon() {
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800548 return flipper_arms_falcon_;
549 }
550
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700551 void set_transfer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) {
552 transfer_roller_victor_ = ::std::move(t);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800553 }
554
Austin Schuhda7e3e12022-03-26 15:14:31 -0700555 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
556
557 void set_superstructure_reading(
558 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
559 superstructure_reading_ = superstructure_reading;
560 }
561
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800562 private:
Griffin Bui67abb912022-01-22 16:16:21 -0800563 void Stop() override {
564 AOS_LOG(WARNING, "Superstructure output too old.\n");
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800565 climber_falcon_->SetDisabled();
milind-u6e7d8d42022-04-06 18:30:43 -0700566 climber_servo_left_->SetRaw(0);
567 climber_servo_right_->SetRaw(0);
568
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800569 Disable(roller_falcon_front_.get());
570 Disable(roller_falcon_back_.get());
571 Disable(flipper_arms_falcon_.get());
572
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800573 intake_falcon_front_->SetDisabled();
574 intake_falcon_back_->SetDisabled();
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700575 transfer_roller_victor_->SetDisabled();
Austin Schuhe040ddc2022-03-17 00:16:47 -0700576 if (catapult_falcon_1_) {
577 catapult_falcon_1_->SetDisabled();
578 }
579 if (catapult_falcon_1_can_) {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800580 Disable(catapult_falcon_1_can_.get());
581 Disable(catapult_falcon_2_can_.get());
Austin Schuhe040ddc2022-03-17 00:16:47 -0700582 }
Jacob Ismael322ebb92022-02-09 20:12:47 -0800583 turret_falcon_->SetDisabled();
Griffin Bui67abb912022-01-22 16:16:21 -0800584 }
585
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800586 void Write(const superstructure::Output &output) override {
Austin Schuh8507c9f2022-03-13 18:08:28 -0700587 WritePwm(-output.climber_voltage(), climber_falcon_.get());
milind-u6e7d8d42022-04-06 18:30:43 -0700588 climber_servo_left_->SetPosition(output.climber_servo_left());
589 climber_servo_right_->SetPosition(output.climber_servo_right());
Griffin Buibcbef482022-02-23 15:32:10 -0800590
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800591 WritePwm(output.intake_voltage_front(), intake_falcon_front_.get());
592 WritePwm(output.intake_voltage_back(), intake_falcon_back_.get());
593 WriteCan(output.roller_voltage_front(), roller_falcon_front_.get());
594 WriteCan(output.roller_voltage_back(), roller_falcon_back_.get());
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700595 WritePwm(output.transfer_roller_voltage(), transfer_roller_victor_.get());
Griffin Buibcbef482022-02-23 15:32:10 -0800596
Austin Schuh465a2882022-03-05 15:39:04 -0800597 WriteCan(-output.flipper_arms_voltage(), flipper_arms_falcon_.get());
Griffin Buibcbef482022-02-23 15:32:10 -0800598
Austin Schuhe040ddc2022-03-17 00:16:47 -0700599 if (catapult_falcon_1_) {
600 WritePwm(output.catapult_voltage(), catapult_falcon_1_.get());
Austin Schuhda7e3e12022-03-26 15:14:31 -0700601 superstructure_reading_->Set(false);
602 if (output.catapult_voltage() > 0) {
603 catapult_reversal_->Set(true);
604 } else {
605 catapult_reversal_->Set(false);
606 }
Austin Schuhe040ddc2022-03-17 00:16:47 -0700607 }
608 if (catapult_falcon_1_can_) {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800609 WriteCanCatapult(output.catapult_voltage(), catapult_falcon_1_can_.get());
610 WriteCanCatapult(output.catapult_voltage(), catapult_falcon_2_can_.get());
Austin Schuhe040ddc2022-03-17 00:16:47 -0700611 }
Griffin Buibcbef482022-02-23 15:32:10 -0800612
Austin Schuh465a2882022-03-05 15:39:04 -0800613 WritePwm(-output.turret_voltage(), turret_falcon_.get());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800614 }
615
616 static void WriteCan(const double voltage,
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800617 ::ctre::phoenix6::hardware::TalonFX *falcon) {
618 ctre::phoenix6::controls::DutyCycleOut control(
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800619 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800620 control.UpdateFreqHz = 0_Hz;
621 control.EnableFOC = true;
622
623 falcon->SetControl(control);
624 }
625 // We do this to set our UpdateFreqHz higher
626 static void WriteCanCatapult(const double voltage,
627 ::ctre::phoenix6::hardware::TalonFX *falcon) {
628 ctre::phoenix6::controls::DutyCycleOut control(
629 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
630 control.UpdateFreqHz = 1000_Hz;
631 control.EnableFOC = true;
632
633 falcon->SetControl(control);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800634 }
635
636 template <typename T>
637 static void WritePwm(const double voltage, T *motor) {
638 motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) /
639 12.0);
640 }
641
642 ::std::unique_ptr<frc::TalonFX> intake_falcon_front_, intake_falcon_back_;
643
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800644 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> roller_falcon_front_,
645 roller_falcon_back_;
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800646
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800647 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> flipper_arms_falcon_;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800648
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800649 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> catapult_falcon_1_can_,
650 catapult_falcon_2_can_;
Austin Schuhe040ddc2022-03-17 00:16:47 -0700651
Jacob Ismael322ebb92022-02-09 20:12:47 -0800652 ::std::unique_ptr<::frc::TalonFX> turret_falcon_, catapult_falcon_1_,
Austin Schuh465a2882022-03-05 15:39:04 -0800653 climber_falcon_;
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700654 ::std::unique_ptr<::frc::VictorSP> transfer_roller_victor_;
Austin Schuhda7e3e12022-03-26 15:14:31 -0700655
656 std::unique_ptr<frc::DigitalOutput> catapult_reversal_;
milind-u6e7d8d42022-04-06 18:30:43 -0700657
658 ::std::unique_ptr<::frc::Servo> climber_servo_left_, climber_servo_right_;
milind-u086d7262022-01-19 20:44:18 -0800659};
660
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800661class CANSensorReader {
662 public:
663 CANSensorReader(aos::EventLoop *event_loop)
664 : event_loop_(event_loop),
665 can_position_sender_(
666 event_loop->MakeSender<superstructure::CANPosition>(
667 "/superstructure")) {
668 event_loop->SetRuntimeRealtimePriority(16);
669
670 phased_loop_handler_ =
671 event_loop_->AddPhasedLoop([this](int) { Loop(); }, kPeriod);
672 phased_loop_handler_->set_name("CAN SensorReader Loop");
673
674 event_loop->OnRun([this]() { Loop(); });
675 }
676
677 void set_flipper_arms_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800678 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800679 flipper_arms_falcon_ = std::move(t);
680 }
681
682 private:
683 void Loop() {
684 auto builder = can_position_sender_.MakeBuilder();
685 superstructure::CANPosition::Builder can_position_builder =
686 builder.MakeBuilder<superstructure::CANPosition>();
687 can_position_builder.add_flipper_arm_integrated_sensor_velocity(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800688 flipper_arms_falcon_->GetVelocity().GetValue().value() *
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800689 kVelocityConversion);
690 builder.CheckOk(builder.Send(can_position_builder.Finish()));
691 }
692
693 static constexpr std::chrono::milliseconds kPeriod =
694 std::chrono::milliseconds(20);
695 // 2048 encoder counts / 100 ms to rad/sec
696 static constexpr double kVelocityConversion = (2.0 * M_PI / 2048) * 0.100;
697 aos::EventLoop *event_loop_;
698 ::aos::PhasedLoopHandler *phased_loop_handler_;
699
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800700 ::std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> flipper_arms_falcon_;
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800701 aos::Sender<superstructure::CANPosition> can_position_sender_;
702};
703
milind-u086d7262022-01-19 20:44:18 -0800704class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
705 public:
706 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
707 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
708 frc::Encoder::k4X);
709 }
710
711 void Run() override {
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800712 std::shared_ptr<const Values> values =
713 std::make_shared<const Values>(constants::MakeValues());
714
milind-u086d7262022-01-19 20:44:18 -0800715 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800716 aos::configuration::ReadConfig("aos_config.json");
milind-u086d7262022-01-19 20:44:18 -0800717
718 // Thread 1.
719 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
720 ::frc971::wpilib::JoystickSender joystick_sender(
721 &joystick_sender_event_loop);
722 AddLoop(&joystick_sender_event_loop);
723
724 // Thread 2.
725 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
726 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800727 AddLoop(&pdp_fetcher_event_loop);
milind-u086d7262022-01-19 20:44:18 -0800728
Austin Schuhda7e3e12022-03-26 15:14:31 -0700729 std::shared_ptr<frc::DigitalOutput> superstructure_reading =
730 make_unique<frc::DigitalOutput>(25);
731
milind-u086d7262022-01-19 20:44:18 -0800732 // Thread 3.
733 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800734 SensorReader sensor_reader(&sensor_reader_event_loop, values);
Austin Schuh028d81d2022-03-26 15:11:42 -0700735 sensor_reader.set_pwm_trigger(true);
Austin Schuha8014282022-03-05 12:36:38 -0800736 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
737 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
Austin Schuhda7e3e12022-03-26 15:14:31 -0700738 sensor_reader.set_superstructure_reading(superstructure_reading);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800739
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800740 sensor_reader.set_intake_encoder_front(make_encoder(3));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800741 sensor_reader.set_intake_front_absolute_pwm(
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800742 make_unique<frc::DigitalInput>(3));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800743 sensor_reader.set_intake_front_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800744 make_unique<frc::AnalogInput>(3));
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800745
746 sensor_reader.set_intake_encoder_back(make_encoder(4));
747 sensor_reader.set_intake_back_absolute_pwm(
748 make_unique<frc::DigitalInput>(4));
749 sensor_reader.set_intake_back_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800750 make_unique<frc::AnalogInput>(4));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800751
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800752 sensor_reader.set_turret_encoder(make_encoder(5));
753 sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(5));
Austin Schuha8014282022-03-05 12:36:38 -0800754 sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(5));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800755
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800756 // TODO(milind): correct intake beambreak ports once set
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800757 sensor_reader.set_intake_beambreak_front(make_unique<frc::DigitalInput>(1));
Austin Schuh445ae832022-03-05 22:52:23 -0800758 sensor_reader.set_intake_beambreak_back(make_unique<frc::DigitalInput>(6));
Milo Lin4950ac52022-02-25 19:56:11 -0800759 sensor_reader.set_turret_beambreak(make_unique<frc::DigitalInput>(7));
760
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800761 sensor_reader.set_climber_potentiometer(make_unique<frc::AnalogInput>(7));
milind-u086d7262022-01-19 20:44:18 -0800762
Griffin Buibcbef482022-02-23 15:32:10 -0800763 sensor_reader.set_flipper_arm_left_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800764 make_unique<frc::AnalogInput>(0));
Griffin Buibcbef482022-02-23 15:32:10 -0800765 sensor_reader.set_flipper_arm_right_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800766 make_unique<frc::AnalogInput>(1));
Griffin Buibcbef482022-02-23 15:32:10 -0800767
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800768 // TODO(milind): correct catapult encoder and absolute pwm ports
Austin Schuha8014282022-03-05 12:36:38 -0800769 sensor_reader.set_catapult_encoder(make_encoder(2));
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800770 sensor_reader.set_catapult_absolute_pwm(
Austin Schuha8014282022-03-05 12:36:38 -0800771 std::make_unique<frc::DigitalInput>(2));
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800772 sensor_reader.set_catapult_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800773 std::make_unique<frc::AnalogInput>(2));
Austin Schuh39f26f62022-02-24 21:34:46 -0800774
Austin Schuhf9a166e2022-03-05 17:53:12 -0800775 sensor_reader.set_heading_input(make_unique<frc::DigitalInput>(9));
776 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(8));
Ravago Jones0e86e242022-02-12 18:38:14 -0800777
milind-u086d7262022-01-19 20:44:18 -0800778 AddLoop(&sensor_reader_event_loop);
779
780 // Thread 4.
781 ::aos::ShmEventLoop output_event_loop(&config.message());
782 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
783 drivetrain_writer.set_left_controller0(
Austin Schuh2d22fb12022-03-06 14:43:29 -0800784 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), false);
Austin Schuha8014282022-03-05 12:36:38 -0800785 drivetrain_writer.set_right_controller0(
Austin Schuh2d22fb12022-03-06 14:43:29 -0800786 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), true);
milind-u086d7262022-01-19 20:44:18 -0800787
788 SuperstructureWriter superstructure_writer(&output_event_loop);
789
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800790 superstructure_writer.set_turret_falcon(make_unique<::frc::TalonFX>(3));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800791 superstructure_writer.set_roller_falcon_front(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800792 make_unique<::ctre::phoenix6::hardware::TalonFX>(0));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800793 superstructure_writer.set_roller_falcon_back(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800794 make_unique<::ctre::phoenix6::hardware::TalonFX>(1));
Austin Schuh465a2882022-03-05 15:39:04 -0800795
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700796 superstructure_writer.set_transfer_roller_victor(
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800797 make_unique<::frc::VictorSP>(5));
Austin Schuh465a2882022-03-05 15:39:04 -0800798
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800799 superstructure_writer.set_intake_falcon_front(make_unique<frc::TalonFX>(2));
800 superstructure_writer.set_intake_falcon_back(make_unique<frc::TalonFX>(4));
Austin Schuh465a2882022-03-05 15:39:04 -0800801 superstructure_writer.set_climber_falcon(make_unique<frc::TalonFX>(8));
milind-u6e7d8d42022-04-06 18:30:43 -0700802 superstructure_writer.set_climber_servo_left(make_unique<frc::Servo>(7));
803 superstructure_writer.set_climber_servo_right(make_unique<frc::Servo>(6));
Griffin Buibcbef482022-02-23 15:32:10 -0800804 superstructure_writer.set_flipper_arms_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800805 make_unique<::ctre::phoenix6::hardware::TalonFX>(2));
Austin Schuhda7e3e12022-03-26 15:14:31 -0700806 superstructure_writer.set_superstructure_reading(superstructure_reading);
Griffin Bui67abb912022-01-22 16:16:21 -0800807
Austin Schuhda7e3e12022-03-26 15:14:31 -0700808 if (!FLAGS_can_catapult) {
809 superstructure_writer.set_catapult_falcon_1(make_unique<frc::TalonFX>(9));
810 } else {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800811 std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> catapult1 =
812 make_unique<::ctre::phoenix6::hardware::TalonFX>(3, "Catapult");
813 std::shared_ptr<::ctre::phoenix6::hardware::TalonFX> catapult2 =
814 make_unique<::ctre::phoenix6::hardware::TalonFX>(4, "Catapult");
Austin Schuhda7e3e12022-03-26 15:14:31 -0700815 superstructure_writer.set_catapult_falcon_1(catapult1, catapult2);
816 sensor_reader.set_catapult_falcon_1(catapult1, catapult2);
817 }
Austin Schuh39f26f62022-02-24 21:34:46 -0800818
milind-u086d7262022-01-19 20:44:18 -0800819 AddLoop(&output_event_loop);
820
Henry Speiser77747b72022-03-06 17:18:29 -0800821 // Thread 5.
822 ::aos::ShmEventLoop led_indicator_event_loop(&config.message());
823 control_loops::superstructure::LedIndicator led_indicator(
824 &led_indicator_event_loop);
825 AddLoop(&led_indicator_event_loop);
826
milind-u086d7262022-01-19 20:44:18 -0800827 RunLoops();
828 }
829};
830
831} // namespace wpilib
832} // namespace y2022
833
834AOS_ROBOT_CLASS(::y2022::wpilib::WPILibRobot);