blob: 56eba95b7938e30188e686ab0f278f8eaf58c54e [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"
15#include "frc971/wpilib/ahal/AnalogInput.h"
16#include "frc971/wpilib/ahal/Counter.h"
17#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
18#include "frc971/wpilib/ahal/DriverStation.h"
19#include "frc971/wpilib/ahal/Encoder.h"
20#include "frc971/wpilib/ahal/TalonFX.h"
21#include "frc971/wpilib/ahal/VictorSP.h"
22#undef ERROR
23
24#include "aos/commonmath.h"
25#include "aos/events/event_loop.h"
26#include "aos/events/shm_event_loop.h"
27#include "aos/init.h"
28#include "aos/logging/logging.h"
29#include "aos/realtime.h"
30#include "aos/time/time.h"
31#include "aos/util/log_interval.h"
32#include "aos/util/phased_loop.h"
33#include "aos/util/wrapping_counter.h"
34#include "ctre/phoenix/motorcontrol/can/TalonFX.h"
35#include "ctre/phoenix/motorcontrol/can/TalonSRX.h"
36#include "frc971/autonomous/auto_mode_generated.h"
37#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
38#include "frc971/input/robot_state_generated.h"
Ravago Jones0e86e242022-02-12 18:38:14 -080039#include "frc971/queues/gyro_generated.h"
milind-u086d7262022-01-19 20:44:18 -080040#include "frc971/wpilib/ADIS16448.h"
41#include "frc971/wpilib/buffered_pcm.h"
42#include "frc971/wpilib/buffered_solenoid.h"
43#include "frc971/wpilib/dma.h"
44#include "frc971/wpilib/drivetrain_writer.h"
45#include "frc971/wpilib/encoder_and_potentiometer.h"
46#include "frc971/wpilib/joystick_sender.h"
47#include "frc971/wpilib/logging_generated.h"
48#include "frc971/wpilib/loop_output_handler.h"
49#include "frc971/wpilib/pdp_fetcher.h"
50#include "frc971/wpilib/sensor_reader.h"
51#include "frc971/wpilib/wpilib_robot_base.h"
52#include "y2022/constants.h"
Henry Speiser77747b72022-03-06 17:18:29 -080053#include "y2022/control_loops/superstructure/led_indicator.h"
Milind Upadhyay482b0ba2022-02-26 21:51:59 -080054#include "y2022/control_loops/superstructure/superstructure_can_position_generated.h"
milind-u086d7262022-01-19 20:44:18 -080055#include "y2022/control_loops/superstructure/superstructure_output_generated.h"
56#include "y2022/control_loops/superstructure/superstructure_position_generated.h"
57
58using ::aos::monotonic_clock;
59using ::y2022::constants::Values;
60namespace superstructure = ::y2022::control_loops::superstructure;
61namespace chrono = ::std::chrono;
62using std::make_unique;
63
Austin Schuhda7e3e12022-03-26 15:14:31 -070064DEFINE_bool(can_catapult, false, "If true, use CAN to control the catapult.");
65
milind-u086d7262022-01-19 20:44:18 -080066namespace y2022 {
67namespace wpilib {
68namespace {
69
70constexpr double kMaxBringupPower = 12.0;
71
72// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
73// DMA stuff and then removing the * 2.0 in *_translate.
74// The low bit is direction.
75
milind-u086d7262022-01-19 20:44:18 -080076double drivetrain_velocity_translate(double in) {
77 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
78 (2.0 * M_PI)) *
79 Values::kDrivetrainEncoderRatio() *
80 control_loops::drivetrain::kWheelRadius;
81}
82
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080083double climber_pot_translate(double voltage) {
84 return voltage * Values::kClimberPotRatio() *
Austin Schuh8507c9f2022-03-13 18:08:28 -070085 (5.0 /*turns*/ / 5.0 /*volts*/) *
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080086 Values::kClimberPotMetersPerRevolution();
87}
88
Griffin Buibcbef482022-02-23 15:32:10 -080089double flipper_arms_pot_translate(double voltage) {
90 return voltage * Values::kFlipperArmsPotRatio() *
91 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
92}
93
Henry Speiser55aa3ba2022-02-21 23:21:12 -080094double intake_pot_translate(double voltage) {
95 return voltage * Values::kIntakePotRatio() * (3.0 /*turns*/ / 5.0 /*volts*/) *
96 (2 * M_PI /*radians*/);
97}
98
99double turret_pot_translate(double voltage) {
100 return voltage * Values::kTurretPotRatio() *
101 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
102}
103
milind-u086d7262022-01-19 20:44:18 -0800104constexpr double kMaxFastEncoderPulsesPerSecond =
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800105 std::max({Values::kMaxDrivetrainEncoderPulsesPerSecond(),
106 Values::kMaxIntakeEncoderPulsesPerSecond()});
milind-u086d7262022-01-19 20:44:18 -0800107static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
108 "fast encoders are too fast");
109constexpr double kMaxMediumEncoderPulsesPerSecond =
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800110 Values::kMaxTurretEncoderPulsesPerSecond();
milind-u086d7262022-01-19 20:44:18 -0800111
112static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
113 "medium encoders are too fast");
114
Austin Schuh39f26f62022-02-24 21:34:46 -0800115double catapult_pot_translate(double voltage) {
116 return voltage * Values::kCatapultPotRatio() *
117 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
118}
119
milind-u086d7262022-01-19 20:44:18 -0800120} // namespace
121
122// Class to send position messages with sensor readings to our loops.
123class SensorReader : public ::frc971::wpilib::SensorReader {
124 public:
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800125 SensorReader(::aos::ShmEventLoop *event_loop,
126 std::shared_ptr<const Values> values)
milind-u086d7262022-01-19 20:44:18 -0800127 : ::frc971::wpilib::SensorReader(event_loop),
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800128 values_(std::move(values)),
milind-u086d7262022-01-19 20:44:18 -0800129 auto_mode_sender_(
130 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
131 "/autonomous")),
132 superstructure_position_sender_(
133 event_loop->MakeSender<superstructure::Position>(
134 "/superstructure")),
135 drivetrain_position_sender_(
136 event_loop
137 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
Ravago Jones0e86e242022-02-12 18:38:14 -0800138 "/drivetrain")),
139 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
140 "/drivetrain")) {
milind-u086d7262022-01-19 20:44:18 -0800141 // Set to filter out anything shorter than 1/4 of the minimum pulse width
142 // we should ever see.
143 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
144 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
145 }
146
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800147 void Start() override {
148 // TODO(Ravago): Figure out why adding multiple DMA readers results in weird
149 // behavior
150 // AddToDMA(&imu_heading_reader_);
151 AddToDMA(&imu_yaw_rate_reader_);
152 }
153
milind-u086d7262022-01-19 20:44:18 -0800154 // Auto mode switches.
155 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
156 autonomous_modes_.at(i) = ::std::move(sensor);
157 }
158
Austin Schuh39f26f62022-02-24 21:34:46 -0800159 void set_catapult_encoder(::std::unique_ptr<frc::Encoder> encoder) {
160 medium_encoder_filter_.Add(encoder.get());
161 catapult_encoder_.set_encoder(::std::move(encoder));
162 }
163
164 void set_catapult_absolute_pwm(
165 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
166 catapult_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
167 }
168
169 void set_catapult_potentiometer(
170 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
171 catapult_encoder_.set_potentiometer(::std::move(potentiometer));
172 }
173
Ravago Jones0e86e242022-02-12 18:38:14 -0800174 void set_heading_input(::std::unique_ptr<frc::DigitalInput> sensor) {
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800175 imu_heading_input_ = ::std::move(sensor);
176 imu_heading_reader_.set_input(imu_heading_input_.get());
Ravago Jones0e86e242022-02-12 18:38:14 -0800177 }
178
179 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800180 imu_yaw_rate_input_ = ::std::move(sensor);
181 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
Ravago Jones0e86e242022-02-12 18:38:14 -0800182 }
Austin Schuhda7e3e12022-03-26 15:14:31 -0700183 void set_catapult_falcon_1(
184 ::std::shared_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t1,
185 ::std::shared_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t2) {
186 catapult_falcon_1_can_ = ::std::move(t1);
187 catapult_falcon_2_can_ = ::std::move(t2);
188 }
Ravago Jones0e86e242022-02-12 18:38:14 -0800189
milind-u086d7262022-01-19 20:44:18 -0800190 void RunIteration() override {
Austin Schuhda7e3e12022-03-26 15:14:31 -0700191 superstructure_reading_->Set(true);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800192 {
193 auto builder = superstructure_position_sender_.MakeBuilder();
194
Austin Schuh39f26f62022-02-24 21:34:46 -0800195 frc971::PotAndAbsolutePositionT catapult;
196 CopyPosition(catapult_encoder_, &catapult,
197 Values::kCatapultEncoderCountsPerRevolution(),
198 Values::kCatapultEncoderRatio(), catapult_pot_translate,
Austin Schuh275f9812022-03-05 14:02:37 -0800199 false, values_->catapult.potentiometer_offset);
Austin Schuh39f26f62022-02-24 21:34:46 -0800200 flatbuffers::Offset<frc971::PotAndAbsolutePosition> catapult_offset =
201 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &catapult);
202
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800203 frc971::RelativePositionT climber;
204 CopyPosition(*climber_potentiometer_, &climber, climber_pot_translate,
205 false, values_->climber.potentiometer_offset);
206 flatbuffers::Offset<frc971::RelativePosition> climber_offset =
207 frc971::RelativePosition::Pack(*builder.fbb(), &climber);
208
Griffin Buibcbef482022-02-23 15:32:10 -0800209 frc971::RelativePositionT flipper_arm_left;
210 CopyPosition(*flipper_arm_left_potentiometer_, &flipper_arm_left,
211 flipper_arms_pot_translate, false,
212 values_->flipper_arm_left.potentiometer_offset);
213
214 frc971::RelativePositionT flipper_arm_right;
215 CopyPosition(*flipper_arm_right_potentiometer_, &flipper_arm_right,
Austin Schuhab0eae32022-03-06 14:42:33 -0800216 flipper_arms_pot_translate, true,
Griffin Buibcbef482022-02-23 15:32:10 -0800217 values_->flipper_arm_right.potentiometer_offset);
218
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800219 // Intake
220 frc971::PotAndAbsolutePositionT intake_front;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800221 CopyPosition(intake_encoder_front_, &intake_front,
222 Values::kIntakeEncoderCountsPerRevolution(),
Austin Schuh275f9812022-03-05 14:02:37 -0800223 Values::kIntakeEncoderRatio(), intake_pot_translate, true,
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800224 values_->intake_front.potentiometer_offset);
Griffin Buibcbef482022-02-23 15:32:10 -0800225 frc971::PotAndAbsolutePositionT intake_back;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800226 CopyPosition(intake_encoder_back_, &intake_back,
227 Values::kIntakeEncoderCountsPerRevolution(),
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800228 Values::kIntakeEncoderRatio(), intake_pot_translate, true,
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800229 values_->intake_back.potentiometer_offset);
Griffin Buibcbef482022-02-23 15:32:10 -0800230 frc971::PotAndAbsolutePositionT turret;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800231 CopyPosition(turret_encoder_, &turret,
232 Values::kTurretEncoderCountsPerRevolution(),
233 Values::kTurretEncoderRatio(), turret_pot_translate, false,
234 values_->turret.potentiometer_offset);
235
236 flatbuffers::Offset<frc971::PotAndAbsolutePosition> intake_offset_front =
237 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &intake_front);
238 flatbuffers::Offset<frc971::PotAndAbsolutePosition> intake_offset_back =
239 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &intake_back);
240 flatbuffers::Offset<frc971::PotAndAbsolutePosition> turret_offset =
241 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &turret);
Griffin Buibcbef482022-02-23 15:32:10 -0800242 flatbuffers::Offset<frc971::RelativePosition> flipper_arm_left_offset =
243 frc971::RelativePosition::Pack(*builder.fbb(), &flipper_arm_left);
244 flatbuffers::Offset<frc971::RelativePosition> flipper_arm_right_offset =
245 frc971::RelativePosition::Pack(*builder.fbb(), &flipper_arm_right);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800246
247 superstructure::Position::Builder position_builder =
248 builder.MakeBuilder<superstructure::Position>();
249 position_builder.add_climber(climber_offset);
Griffin Buibcbef482022-02-23 15:32:10 -0800250 position_builder.add_flipper_arm_left(flipper_arm_left_offset);
251 position_builder.add_flipper_arm_right(flipper_arm_right_offset);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800252 position_builder.add_intake_front(intake_offset_front);
253 position_builder.add_intake_back(intake_offset_back);
254 position_builder.add_turret(turret_offset);
Milo Lin4950ac52022-02-25 19:56:11 -0800255 position_builder.add_intake_beambreak_front(
256 intake_beambreak_front_->Get());
257 position_builder.add_intake_beambreak_back(intake_beambreak_back_->Get());
258 position_builder.add_turret_beambreak(turret_beambreak_->Get());
Austin Schuh39f26f62022-02-24 21:34:46 -0800259 position_builder.add_catapult(catapult_offset);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800260 builder.CheckOk(builder.Send(position_builder.Finish()));
261 }
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800262
milind-u086d7262022-01-19 20:44:18 -0800263 {
264 auto builder = drivetrain_position_sender_.MakeBuilder();
265 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
266 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
267 drivetrain_builder.add_left_encoder(
James Kuszmaul53507e12022-02-12 18:36:40 -0800268 constants::Values::DrivetrainEncoderToMeters(
269 drivetrain_left_encoder_->GetRaw()));
milind-u086d7262022-01-19 20:44:18 -0800270 drivetrain_builder.add_left_speed(
271 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
272
273 drivetrain_builder.add_right_encoder(
James Kuszmaul53507e12022-02-12 18:36:40 -0800274 -constants::Values::DrivetrainEncoderToMeters(
275 drivetrain_right_encoder_->GetRaw()));
milind-u086d7262022-01-19 20:44:18 -0800276 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
277 drivetrain_right_encoder_->GetPeriod()));
278
279 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
280 }
281
282 {
Ravago Jones0e86e242022-02-12 18:38:14 -0800283 auto builder = gyro_sender_.MakeBuilder();
284 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
285 builder.MakeBuilder<::frc971::sensors::GyroReading>();
James Kuszmaulf34e7fd2022-03-13 20:30:34 -0700286 // +/- 2000 deg / sec
287 constexpr double kMaxVelocity = 4000; // degrees / second
Ravago Jones0e86e242022-02-12 18:38:14 -0800288 constexpr double kVelocityRadiansPerSecond =
289 kMaxVelocity / 360 * (2.0 * M_PI);
290
291 // Only part of the full range is used to prevent being 100% on or off.
292 constexpr double kScaledRangeLow = 0.1;
293 constexpr double kScaledRangeHigh = 0.9;
294
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800295 constexpr double kPWMFrequencyHz = 200;
296 double heading_duty_cycle =
297 imu_heading_reader_.last_width() * kPWMFrequencyHz;
298 double velocity_duty_cycle =
299 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
300
Ravago Jones0e86e242022-02-12 18:38:14 -0800301 constexpr double kDutyCycleScale =
302 1 / (kScaledRangeHigh - kScaledRangeLow);
Ravago Jones0e86e242022-02-12 18:38:14 -0800303 // scale from 0.1 - 0.9 to 0 - 1
304 double rescaled_heading_duty_cycle =
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800305 (heading_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
Ravago Jones0e86e242022-02-12 18:38:14 -0800306 double rescaled_velocity_duty_cycle =
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800307 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
Ravago Jones0e86e242022-02-12 18:38:14 -0800308
309 if (!std::isnan(rescaled_heading_duty_cycle)) {
310 gyro_reading_builder.add_angle(rescaled_heading_duty_cycle *
311 (2.0 * M_PI));
312 }
313 if (!std::isnan(rescaled_velocity_duty_cycle)) {
314 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
315 kVelocityRadiansPerSecond);
316 }
317 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
318 }
319
320 {
milind-u086d7262022-01-19 20:44:18 -0800321 auto builder = auto_mode_sender_.MakeBuilder();
322
323 uint32_t mode = 0;
324 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
325 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
326 mode |= 1 << i;
327 }
328 }
329
330 auto auto_mode_builder =
331 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
332
333 auto_mode_builder.add_mode(mode);
334
335 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
336 }
337 }
338
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800339 void set_climber_potentiometer(
340 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
341 climber_potentiometer_ = ::std::move(potentiometer);
342 }
343
Griffin Buibcbef482022-02-23 15:32:10 -0800344 void set_flipper_arm_left_potentiometer(
345 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
346 flipper_arm_left_potentiometer_ = ::std::move(potentiometer);
347 }
348
349 void set_flipper_arm_right_potentiometer(
350 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
351 flipper_arm_right_potentiometer_ = ::std::move(potentiometer);
352 }
353
Austin Schuhda7e3e12022-03-26 15:14:31 -0700354 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
355
356 void set_superstructure_reading(
357 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
358 superstructure_reading_ = superstructure_reading;
359 }
360
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800361 void set_intake_encoder_front(::std::unique_ptr<frc::Encoder> encoder) {
362 fast_encoder_filter_.Add(encoder.get());
363 intake_encoder_front_.set_encoder(::std::move(encoder));
364 }
365
366 void set_intake_encoder_back(::std::unique_ptr<frc::Encoder> encoder) {
367 fast_encoder_filter_.Add(encoder.get());
368 intake_encoder_back_.set_encoder(::std::move(encoder));
369 }
370
371 void set_intake_front_absolute_pwm(
372 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
373 intake_encoder_front_.set_absolute_pwm(::std::move(absolute_pwm));
374 }
375
376 void set_intake_front_potentiometer(
377 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
378 intake_encoder_front_.set_potentiometer(::std::move(potentiometer));
379 }
380
381 void set_intake_back_absolute_pwm(
382 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
383 intake_encoder_back_.set_absolute_pwm(::std::move(absolute_pwm));
384 }
385
386 void set_intake_back_potentiometer(
387 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
388 intake_encoder_back_.set_potentiometer(::std::move(potentiometer));
389 }
390
391 void set_turret_encoder(::std::unique_ptr<frc::Encoder> encoder) {
392 medium_encoder_filter_.Add(encoder.get());
393 turret_encoder_.set_encoder(::std::move(encoder));
394 }
395
396 void set_turret_absolute_pwm(
397 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
398 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
399 }
400
401 void set_turret_potentiometer(
402 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
403 turret_encoder_.set_potentiometer(::std::move(potentiometer));
404 }
405
Milo Lin4950ac52022-02-25 19:56:11 -0800406 void set_intake_beambreak_front(::std::unique_ptr<frc::DigitalInput> sensor) {
407 intake_beambreak_front_ = ::std::move(sensor);
408 }
409 void set_intake_beambreak_back(::std::unique_ptr<frc::DigitalInput> sensor) {
410 intake_beambreak_back_ = ::std::move(sensor);
411 }
412 void set_turret_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
413 turret_beambreak_ = ::std::move(sensor);
414 }
415
milind-u086d7262022-01-19 20:44:18 -0800416 private:
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800417 std::shared_ptr<const Values> values_;
418
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800419 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
420 aos::Sender<superstructure::Position> superstructure_position_sender_;
421 aos::Sender<frc971::control_loops::drivetrain::Position>
milind-u086d7262022-01-19 20:44:18 -0800422 drivetrain_position_sender_;
Ravago Jones0e86e242022-02-12 18:38:14 -0800423 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
milind-u086d7262022-01-19 20:44:18 -0800424
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800425 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800426
Milo Lin4950ac52022-02-25 19:56:11 -0800427 std::unique_ptr<frc::DigitalInput> intake_beambreak_front_,
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800428 intake_beambreak_back_, turret_beambreak_, imu_heading_input_,
429 imu_yaw_rate_input_;
Milo Lin4950ac52022-02-25 19:56:11 -0800430
Griffin Buibcbef482022-02-23 15:32:10 -0800431 std::unique_ptr<frc::AnalogInput> climber_potentiometer_,
432 flipper_arm_right_potentiometer_, flipper_arm_left_potentiometer_;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800433 frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_encoder_front_,
Ravago Jones0e86e242022-02-12 18:38:14 -0800434 intake_encoder_back_, turret_encoder_, catapult_encoder_;
Austin Schuh39f26f62022-02-24 21:34:46 -0800435
Ravago Jones7b28b1b2022-02-27 20:55:53 -0800436 frc971::wpilib::DMAPulseWidthReader imu_heading_reader_, imu_yaw_rate_reader_;
Austin Schuhda7e3e12022-03-26 15:14:31 -0700437
438 ::std::shared_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
439 catapult_falcon_1_can_, catapult_falcon_2_can_;
milind-u086d7262022-01-19 20:44:18 -0800440};
441
442class SuperstructureWriter
443 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
444 public:
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800445 SuperstructureWriter(aos::EventLoop *event_loop)
446 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
Austin Schuhda7e3e12022-03-26 15:14:31 -0700447 event_loop, "/superstructure"),
448 catapult_reversal_(make_unique<frc::DigitalOutput>(0)) {}
milind-u086d7262022-01-19 20:44:18 -0800449
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800450 void set_climber_falcon(std::unique_ptr<frc::TalonFX> t) {
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800451 climber_falcon_ = std::move(t);
Griffin Bui67abb912022-01-22 16:16:21 -0800452 }
453
Jacob Ismael322ebb92022-02-09 20:12:47 -0800454 void set_turret_falcon(::std::unique_ptr<::frc::TalonFX> t) {
455 turret_falcon_ = ::std::move(t);
456 }
457
458 void set_catapult_falcon_1(::std::unique_ptr<::frc::TalonFX> t) {
459 catapult_falcon_1_ = ::std::move(t);
460 }
461
Austin Schuhe040ddc2022-03-17 00:16:47 -0700462 void set_catapult_falcon_1(
Austin Schuhda7e3e12022-03-26 15:14:31 -0700463 ::std::shared_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t1,
464 ::std::shared_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t2) {
Austin Schuhe040ddc2022-03-17 00:16:47 -0700465 catapult_falcon_1_can_ = ::std::move(t1);
466 catapult_falcon_2_can_ = ::std::move(t2);
467
468 for (auto &falcon : {catapult_falcon_1_can_, catapult_falcon_2_can_}) {
469 falcon->ConfigSupplyCurrentLimit(
470 {false, Values::kIntakeRollerSupplyCurrentLimit(),
471 Values::kIntakeRollerSupplyCurrentLimit(), 0});
472 falcon->ConfigStatorCurrentLimit(
473 {false, Values::kIntakeRollerStatorCurrentLimit(),
474 Values::kIntakeRollerStatorCurrentLimit(), 0});
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700475 falcon->SetStatusFramePeriod(
476 ctre::phoenix::motorcontrol::Status_1_General, 1);
477 falcon->SetControlFramePeriod(
478 ctre::phoenix::motorcontrol::Control_3_General, 1);
479 falcon->SetStatusFramePeriod(
480 ctre::phoenix::motorcontrol::Status_Brushless_Current, 50);
Austin Schuhda7e3e12022-03-26 15:14:31 -0700481 falcon->ConfigOpenloopRamp(0.0);
482 falcon->ConfigClosedloopRamp(0.0);
483 falcon->ConfigVoltageMeasurementFilter(1);
Austin Schuhe040ddc2022-03-17 00:16:47 -0700484 }
Austin Schuhe040ddc2022-03-17 00:16:47 -0700485 }
486
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800487 void set_intake_falcon_front(::std::unique_ptr<frc::TalonFX> t) {
488 intake_falcon_front_ = ::std::move(t);
Griffin Bui67abb912022-01-22 16:16:21 -0800489 }
milind-u086d7262022-01-19 20:44:18 -0800490
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800491 void set_intake_falcon_back(::std::unique_ptr<frc::TalonFX> t) {
492 intake_falcon_back_ = ::std::move(t);
493 }
494
495 void set_roller_falcon_front(
496 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
497 roller_falcon_front_ = ::std::move(t);
498 roller_falcon_front_->ConfigSupplyCurrentLimit(
499 {true, Values::kIntakeRollerSupplyCurrentLimit(),
500 Values::kIntakeRollerSupplyCurrentLimit(), 0});
501 roller_falcon_front_->ConfigStatorCurrentLimit(
502 {true, Values::kIntakeRollerStatorCurrentLimit(),
503 Values::kIntakeRollerStatorCurrentLimit(), 0});
504 }
505
506 void set_roller_falcon_back(
507 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
508 roller_falcon_back_ = ::std::move(t);
509 roller_falcon_back_->ConfigSupplyCurrentLimit(
510 {true, Values::kIntakeRollerSupplyCurrentLimit(),
511 Values::kIntakeRollerSupplyCurrentLimit(), 0});
512 roller_falcon_back_->ConfigStatorCurrentLimit(
513 {true, Values::kIntakeRollerStatorCurrentLimit(),
514 Values::kIntakeRollerStatorCurrentLimit(), 0});
515 }
516
Griffin Buibcbef482022-02-23 15:32:10 -0800517 void set_flipper_arms_falcon(
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800518 ::std::shared_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
519 flipper_arms_falcon_ = t;
Griffin Buibcbef482022-02-23 15:32:10 -0800520 flipper_arms_falcon_->ConfigSupplyCurrentLimit(
521 {true, Values::kFlipperArmSupplyCurrentLimit(),
522 Values::kFlipperArmSupplyCurrentLimit(), 0});
523 flipper_arms_falcon_->ConfigStatorCurrentLimit(
524 {true, Values::kFlipperArmStatorCurrentLimit(),
525 Values::kFlipperArmStatorCurrentLimit(), 0});
526 }
527
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800528 ::std::shared_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
529 flipper_arms_falcon() {
530 return flipper_arms_falcon_;
531 }
532
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700533 void set_transfer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) {
534 transfer_roller_victor_ = ::std::move(t);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800535 }
536
Austin Schuhda7e3e12022-03-26 15:14:31 -0700537 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
538
539 void set_superstructure_reading(
540 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
541 superstructure_reading_ = superstructure_reading;
542 }
543
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800544 private:
Griffin Bui67abb912022-01-22 16:16:21 -0800545 void Stop() override {
546 AOS_LOG(WARNING, "Superstructure output too old.\n");
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800547 climber_falcon_->SetDisabled();
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800548 roller_falcon_front_->Set(
549 ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
550 roller_falcon_back_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled,
551 0);
Griffin Buibcbef482022-02-23 15:32:10 -0800552 flipper_arms_falcon_->Set(
553 ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800554 intake_falcon_front_->SetDisabled();
555 intake_falcon_back_->SetDisabled();
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700556 transfer_roller_victor_->SetDisabled();
Austin Schuhe040ddc2022-03-17 00:16:47 -0700557 if (catapult_falcon_1_) {
558 catapult_falcon_1_->SetDisabled();
559 }
560 if (catapult_falcon_1_can_) {
561 catapult_falcon_1_can_->Set(
562 ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
Austin Schuhda7e3e12022-03-26 15:14:31 -0700563 catapult_falcon_2_can_->Set(
564 ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
Austin Schuhe040ddc2022-03-17 00:16:47 -0700565 }
Jacob Ismael322ebb92022-02-09 20:12:47 -0800566 turret_falcon_->SetDisabled();
Griffin Bui67abb912022-01-22 16:16:21 -0800567 }
568
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800569 void Write(const superstructure::Output &output) override {
Austin Schuh8507c9f2022-03-13 18:08:28 -0700570 WritePwm(-output.climber_voltage(), climber_falcon_.get());
Griffin Buibcbef482022-02-23 15:32:10 -0800571
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800572 WritePwm(output.intake_voltage_front(), intake_falcon_front_.get());
573 WritePwm(output.intake_voltage_back(), intake_falcon_back_.get());
574 WriteCan(output.roller_voltage_front(), roller_falcon_front_.get());
575 WriteCan(output.roller_voltage_back(), roller_falcon_back_.get());
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700576 WritePwm(output.transfer_roller_voltage(), transfer_roller_victor_.get());
Griffin Buibcbef482022-02-23 15:32:10 -0800577
Austin Schuh465a2882022-03-05 15:39:04 -0800578 WriteCan(-output.flipper_arms_voltage(), flipper_arms_falcon_.get());
Griffin Buibcbef482022-02-23 15:32:10 -0800579
Austin Schuhe040ddc2022-03-17 00:16:47 -0700580 if (catapult_falcon_1_) {
581 WritePwm(output.catapult_voltage(), catapult_falcon_1_.get());
Austin Schuhda7e3e12022-03-26 15:14:31 -0700582 superstructure_reading_->Set(false);
583 if (output.catapult_voltage() > 0) {
584 catapult_reversal_->Set(true);
585 } else {
586 catapult_reversal_->Set(false);
587 }
Austin Schuhe040ddc2022-03-17 00:16:47 -0700588 }
589 if (catapult_falcon_1_can_) {
590 WriteCan(output.catapult_voltage(), catapult_falcon_1_can_.get());
Austin Schuhda7e3e12022-03-26 15:14:31 -0700591 WriteCan(output.catapult_voltage(), catapult_falcon_2_can_.get());
Austin Schuhe040ddc2022-03-17 00:16:47 -0700592 }
Griffin Buibcbef482022-02-23 15:32:10 -0800593
Austin Schuh465a2882022-03-05 15:39:04 -0800594 WritePwm(-output.turret_voltage(), turret_falcon_.get());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800595 }
596
597 static void WriteCan(const double voltage,
598 ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) {
599 falcon->Set(
600 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
601 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
602 }
603
604 template <typename T>
605 static void WritePwm(const double voltage, T *motor) {
606 motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) /
607 12.0);
608 }
609
610 ::std::unique_ptr<frc::TalonFX> intake_falcon_front_, intake_falcon_back_;
611
612 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800613 roller_falcon_front_, roller_falcon_back_;
614
615 ::std::shared_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
616 flipper_arms_falcon_;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800617
Austin Schuhe040ddc2022-03-17 00:16:47 -0700618 ::std::shared_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
619 catapult_falcon_1_can_, catapult_falcon_2_can_;
620
Jacob Ismael322ebb92022-02-09 20:12:47 -0800621 ::std::unique_ptr<::frc::TalonFX> turret_falcon_, catapult_falcon_1_,
Austin Schuh465a2882022-03-05 15:39:04 -0800622 climber_falcon_;
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700623 ::std::unique_ptr<::frc::VictorSP> transfer_roller_victor_;
Austin Schuhda7e3e12022-03-26 15:14:31 -0700624
625 std::unique_ptr<frc::DigitalOutput> catapult_reversal_;
milind-u086d7262022-01-19 20:44:18 -0800626};
627
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800628class CANSensorReader {
629 public:
630 CANSensorReader(aos::EventLoop *event_loop)
631 : event_loop_(event_loop),
632 can_position_sender_(
633 event_loop->MakeSender<superstructure::CANPosition>(
634 "/superstructure")) {
635 event_loop->SetRuntimeRealtimePriority(16);
636
637 phased_loop_handler_ =
638 event_loop_->AddPhasedLoop([this](int) { Loop(); }, kPeriod);
639 phased_loop_handler_->set_name("CAN SensorReader Loop");
640
641 event_loop->OnRun([this]() { Loop(); });
642 }
643
644 void set_flipper_arms_falcon(
645 ::std::shared_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
646 flipper_arms_falcon_ = std::move(t);
647 }
648
649 private:
650 void Loop() {
651 auto builder = can_position_sender_.MakeBuilder();
652 superstructure::CANPosition::Builder can_position_builder =
653 builder.MakeBuilder<superstructure::CANPosition>();
654 can_position_builder.add_flipper_arm_integrated_sensor_velocity(
655 flipper_arms_falcon_->GetSelectedSensorVelocity() *
656 kVelocityConversion);
657 builder.CheckOk(builder.Send(can_position_builder.Finish()));
658 }
659
660 static constexpr std::chrono::milliseconds kPeriod =
661 std::chrono::milliseconds(20);
662 // 2048 encoder counts / 100 ms to rad/sec
663 static constexpr double kVelocityConversion = (2.0 * M_PI / 2048) * 0.100;
664 aos::EventLoop *event_loop_;
665 ::aos::PhasedLoopHandler *phased_loop_handler_;
666
667 ::std::shared_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
668 flipper_arms_falcon_;
669 aos::Sender<superstructure::CANPosition> can_position_sender_;
670};
671
milind-u086d7262022-01-19 20:44:18 -0800672class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
673 public:
674 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
675 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
676 frc::Encoder::k4X);
677 }
678
679 void Run() override {
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800680 std::shared_ptr<const Values> values =
681 std::make_shared<const Values>(constants::MakeValues());
682
milind-u086d7262022-01-19 20:44:18 -0800683 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800684 aos::configuration::ReadConfig("aos_config.json");
milind-u086d7262022-01-19 20:44:18 -0800685
686 // Thread 1.
687 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
688 ::frc971::wpilib::JoystickSender joystick_sender(
689 &joystick_sender_event_loop);
690 AddLoop(&joystick_sender_event_loop);
691
692 // Thread 2.
693 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
694 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800695 AddLoop(&pdp_fetcher_event_loop);
milind-u086d7262022-01-19 20:44:18 -0800696
Austin Schuhda7e3e12022-03-26 15:14:31 -0700697 std::shared_ptr<frc::DigitalOutput> superstructure_reading =
698 make_unique<frc::DigitalOutput>(25);
699
milind-u086d7262022-01-19 20:44:18 -0800700 // Thread 3.
701 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800702 SensorReader sensor_reader(&sensor_reader_event_loop, values);
Austin Schuh028d81d2022-03-26 15:11:42 -0700703 sensor_reader.set_pwm_trigger(true);
Austin Schuha8014282022-03-05 12:36:38 -0800704 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
705 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
Austin Schuhda7e3e12022-03-26 15:14:31 -0700706 sensor_reader.set_superstructure_reading(superstructure_reading);
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800707
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800708 sensor_reader.set_intake_encoder_front(make_encoder(3));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800709 sensor_reader.set_intake_front_absolute_pwm(
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800710 make_unique<frc::DigitalInput>(3));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800711 sensor_reader.set_intake_front_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800712 make_unique<frc::AnalogInput>(3));
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800713
714 sensor_reader.set_intake_encoder_back(make_encoder(4));
715 sensor_reader.set_intake_back_absolute_pwm(
716 make_unique<frc::DigitalInput>(4));
717 sensor_reader.set_intake_back_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800718 make_unique<frc::AnalogInput>(4));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800719
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800720 sensor_reader.set_turret_encoder(make_encoder(5));
721 sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(5));
Austin Schuha8014282022-03-05 12:36:38 -0800722 sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(5));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800723
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800724 // TODO(milind): correct intake beambreak ports once set
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800725 sensor_reader.set_intake_beambreak_front(make_unique<frc::DigitalInput>(1));
Austin Schuh445ae832022-03-05 22:52:23 -0800726 sensor_reader.set_intake_beambreak_back(make_unique<frc::DigitalInput>(6));
Milo Lin4950ac52022-02-25 19:56:11 -0800727 sensor_reader.set_turret_beambreak(make_unique<frc::DigitalInput>(7));
728
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800729 sensor_reader.set_climber_potentiometer(make_unique<frc::AnalogInput>(7));
milind-u086d7262022-01-19 20:44:18 -0800730
Griffin Buibcbef482022-02-23 15:32:10 -0800731 sensor_reader.set_flipper_arm_left_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800732 make_unique<frc::AnalogInput>(0));
Griffin Buibcbef482022-02-23 15:32:10 -0800733 sensor_reader.set_flipper_arm_right_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800734 make_unique<frc::AnalogInput>(1));
Griffin Buibcbef482022-02-23 15:32:10 -0800735
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800736 // TODO(milind): correct catapult encoder and absolute pwm ports
Austin Schuha8014282022-03-05 12:36:38 -0800737 sensor_reader.set_catapult_encoder(make_encoder(2));
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800738 sensor_reader.set_catapult_absolute_pwm(
Austin Schuha8014282022-03-05 12:36:38 -0800739 std::make_unique<frc::DigitalInput>(2));
Milind Upadhyay482b0ba2022-02-26 21:51:59 -0800740 sensor_reader.set_catapult_potentiometer(
Austin Schuha8014282022-03-05 12:36:38 -0800741 std::make_unique<frc::AnalogInput>(2));
Austin Schuh39f26f62022-02-24 21:34:46 -0800742
Austin Schuhf9a166e2022-03-05 17:53:12 -0800743 sensor_reader.set_heading_input(make_unique<frc::DigitalInput>(9));
744 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(8));
Ravago Jones0e86e242022-02-12 18:38:14 -0800745
milind-u086d7262022-01-19 20:44:18 -0800746 AddLoop(&sensor_reader_event_loop);
747
748 // Thread 4.
749 ::aos::ShmEventLoop output_event_loop(&config.message());
750 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
751 drivetrain_writer.set_left_controller0(
Austin Schuh2d22fb12022-03-06 14:43:29 -0800752 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), false);
Austin Schuha8014282022-03-05 12:36:38 -0800753 drivetrain_writer.set_right_controller0(
Austin Schuh2d22fb12022-03-06 14:43:29 -0800754 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), true);
milind-u086d7262022-01-19 20:44:18 -0800755
756 SuperstructureWriter superstructure_writer(&output_event_loop);
757
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800758 superstructure_writer.set_turret_falcon(make_unique<::frc::TalonFX>(3));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800759 superstructure_writer.set_roller_falcon_front(
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800760 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(0));
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800761 superstructure_writer.set_roller_falcon_back(
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800762 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(1));
Austin Schuh465a2882022-03-05 15:39:04 -0800763
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700764 superstructure_writer.set_transfer_roller_victor(
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800765 make_unique<::frc::VictorSP>(5));
Austin Schuh465a2882022-03-05 15:39:04 -0800766
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800767 superstructure_writer.set_intake_falcon_front(make_unique<frc::TalonFX>(2));
768 superstructure_writer.set_intake_falcon_back(make_unique<frc::TalonFX>(4));
Austin Schuh465a2882022-03-05 15:39:04 -0800769 superstructure_writer.set_climber_falcon(make_unique<frc::TalonFX>(8));
Griffin Buibcbef482022-02-23 15:32:10 -0800770 superstructure_writer.set_flipper_arms_falcon(
Milind Upadhyay3020daa2022-03-04 19:44:47 -0800771 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(2));
Austin Schuhda7e3e12022-03-26 15:14:31 -0700772 superstructure_writer.set_superstructure_reading(superstructure_reading);
Griffin Bui67abb912022-01-22 16:16:21 -0800773
Austin Schuhda7e3e12022-03-26 15:14:31 -0700774 if (!FLAGS_can_catapult) {
775 superstructure_writer.set_catapult_falcon_1(make_unique<frc::TalonFX>(9));
776 } else {
777 std::shared_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> catapult1 =
778 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(3,
779 "Catapult");
780 std::shared_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> catapult2 =
781 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(4,
782 "Catapult");
783 superstructure_writer.set_catapult_falcon_1(catapult1, catapult2);
784 sensor_reader.set_catapult_falcon_1(catapult1, catapult2);
785 }
Austin Schuh39f26f62022-02-24 21:34:46 -0800786
milind-u086d7262022-01-19 20:44:18 -0800787 AddLoop(&output_event_loop);
788
Henry Speiser77747b72022-03-06 17:18:29 -0800789 // Thread 5.
790 ::aos::ShmEventLoop led_indicator_event_loop(&config.message());
791 control_loops::superstructure::LedIndicator led_indicator(
792 &led_indicator_event_loop);
793 AddLoop(&led_indicator_event_loop);
794
milind-u086d7262022-01-19 20:44:18 -0800795 RunLoops();
796 }
797};
798
799} // namespace wpilib
800} // namespace y2022
801
802AOS_ROBOT_CLASS(::y2022::wpilib::WPILibRobot);