blob: 3466b828d56c701970631ff9bd41e9cbb7e08a9c [file] [log] [blame]
Comran Morshed9a9948c2016-01-16 15:58:04 +00001#include <unistd.h>
Comran Morshed9a9948c2016-01-16 15:58:04 +00002
Brian Silverman68cb5c22016-03-20 18:11:14 -07003#include <array>
Sabina Davis2243cab2019-02-05 21:45:08 -08004#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005#include <cinttypes>
6#include <cstdio>
7#include <cstring>
Sabina Davis2243cab2019-02-05 21:45:08 -08008#include <functional>
9#include <mutex>
10#include <thread>
Comran Morshed9a9948c2016-01-16 15:58:04 +000011
Parker Schuhd3b7a8872018-02-19 16:42:27 -080012#include "frc971/wpilib/ahal/AnalogInput.h"
13#include "frc971/wpilib/ahal/Compressor.h"
14#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
15#include "frc971/wpilib/ahal/DriverStation.h"
16#include "frc971/wpilib/ahal/Encoder.h"
17#include "frc971/wpilib/ahal/Relay.h"
18#include "frc971/wpilib/ahal/Talon.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000019#include "frc971/wpilib/wpilib_robot_base.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000020#undef ERROR
21
Brian Silvermanf819b442019-01-20 16:51:04 -080022#include "aos/commonmath.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070023#include "aos/events/shm_event_loop.h"
John Park33858a32018-09-28 23:05:48 -070024#include "aos/logging/logging.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080025#include "aos/stl_mutex/stl_mutex.h"
John Park33858a32018-09-28 23:05:48 -070026#include "aos/time/time.h"
27#include "aos/util/log_interval.h"
John Park33858a32018-09-28 23:05:48 -070028#include "aos/util/wrapping_counter.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070029#include "frc971/autonomous/auto_generated.h"
Austin Schuhed5b26d2019-12-05 20:51:59 -080030#include "frc971/autonomous/auto_mode_generated.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070031#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
32#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070033#include "frc971/input/robot_state_generated.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080034#include "frc971/wpilib/ADIS16448.h"
35#include "frc971/wpilib/buffered_pcm.h"
36#include "frc971/wpilib/buffered_solenoid.h"
37#include "frc971/wpilib/dma.h"
38#include "frc971/wpilib/dma_edge_counting.h"
Sabina Davisb71bc282019-02-03 01:17:23 -080039#include "frc971/wpilib/drivetrain_writer.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080040#include "frc971/wpilib/encoder_and_potentiometer.h"
41#include "frc971/wpilib/gyro_sender.h"
42#include "frc971/wpilib/interrupt_edge_counting.h"
43#include "frc971/wpilib/joystick_sender.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070044#include "frc971/wpilib/logging_generated.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080045#include "frc971/wpilib/loop_output_handler.h"
46#include "frc971/wpilib/pdp_fetcher.h"
47#include "frc971/wpilib/sensor_reader.h"
Comran Morshed6c6a0a92016-01-17 12:45:16 +000048#include "y2016/constants.h"
Comran Morshed5bb12112016-02-16 13:48:57 +000049#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070050#include "y2016/control_loops/shooter/shooter_output_generated.h"
51#include "y2016/control_loops/shooter/shooter_position_generated.h"
52#include "y2016/control_loops/superstructure/superstructure_output_generated.h"
Maxwell Hendersoncb78f352024-01-15 00:27:16 -080053#include "y2016/control_loops/superstructure/superstructure_position_static.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070054#include "y2016/queues/ball_detector_generated.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000055
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070056using ::frc971::wpilib::LoopOutputHandler;
Tyler Chatowbf0609c2021-07-31 16:13:27 -070057using std::make_unique;
Alex Perrycb7da4b2019-08-28 19:35:56 -070058namespace shooter = ::y2016::control_loops::shooter;
59namespace superstructure = ::y2016::control_loops::superstructure;
Comran Morshed9a9948c2016-01-16 15:58:04 +000060
Stephan Pleinesf63bde82024-01-13 15:59:33 -080061namespace y2016::wpilib {
Austin Schuha9992ff2016-02-28 21:59:23 -080062namespace {
63constexpr double kMaxBringupPower = 12.0;
64} // namespace
Comran Morshed9a9948c2016-01-16 15:58:04 +000065
66// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
67// DMA stuff and then removing the * 2.0 in *_translate.
68// The low bit is direction.
69
Comran Morshed225f0b92016-02-10 20:34:27 +000070// Translates for the sensor values to convert raw index pulses into something
71// with proper units.
72
73// TODO(comran): Template these methods since there is a lot of repetition here.
74double hall_translate(double in) {
75 // Turn voltage from our 3-state halls into a ratio that the loop can use.
76 return in / 5.0;
77}
78
Comran Morshed9a9948c2016-01-16 15:58:04 +000079double drivetrain_translate(int32_t in) {
Comran Morshed6c6a0a92016-01-17 12:45:16 +000080 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000081 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080082 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000083}
84
85double drivetrain_velocity_translate(double in) {
86 return (1.0 / in) / 256.0 /*cpr*/ *
Comran Morshed225f0b92016-02-10 20:34:27 +000087 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080088 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000089}
90
Comran Morshed225f0b92016-02-10 20:34:27 +000091double shooter_translate(int32_t in) {
Comran Morshed5bb12112016-02-16 13:48:57 +000092 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000093 constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
94}
Comran Morshed9a9948c2016-01-16 15:58:04 +000095
Comran Morshed225f0b92016-02-10 20:34:27 +000096double intake_translate(int32_t in) {
Austin Schuh9f77fd22016-02-21 02:53:58 -080097 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000098 constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
99}
100
101double shoulder_translate(int32_t in) {
102 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
103 constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/);
104}
105
106double wrist_translate(int32_t in) {
107 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
108 constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/);
109}
110
111double intake_pot_translate(double voltage) {
112 return voltage * constants::Values::kIntakePotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000113 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000114}
115
116double shoulder_pot_translate(double voltage) {
117 return voltage * constants::Values::kShoulderPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000118 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000119}
120
121double wrist_pot_translate(double voltage) {
122 return voltage * constants::Values::kWristPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000123 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000124}
125
Comran Morshed1b764322016-02-14 20:18:12 +0000126constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
127 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
128 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
129 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
130
131constexpr double kMaxShooterEncoderPulsesPerSecond =
132 18700.0 /* 775pro free speed RPM */ * 12.0 /
133 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
134 128.0 /* CPR */ * 4 /* edges per cycle */;
135
136double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max(
137 kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond);
138
139constexpr double kMaxSuperstructureEncoderPulsesPerSecond =
140 18700.0 /* 775pro free speed RPM */ * 12.0 /
141 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
142 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000143
Comran Morshed225f0b92016-02-10 20:34:27 +0000144// Class to send position messages with sensor readings to our loops.
Austin Schuh54667ac2019-02-02 16:44:49 -0800145class SensorReader : public ::frc971::wpilib::SensorReader {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000146 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800147 SensorReader(::aos::ShmEventLoop *event_loop)
Austin Schuh4b652c92019-05-27 13:22:27 -0700148 : ::frc971::wpilib::SensorReader(event_loop),
149 ball_detector_sender_(
150 event_loop->MakeSender<::y2016::sensors::BallDetector>(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700151 "/superstructure")),
Austin Schuha250b2d2019-05-27 16:14:02 -0700152 auto_mode_sender_(
153 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
Tyler Chatow24b5db12020-01-06 21:16:56 -0800154 "/autonomous")),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700155 shooter_position_sender_(
156 event_loop->MakeSender<shooter::Position>("/shooter")),
Austin Schuh9481d0d2019-06-29 21:56:17 -0700157 superstructure_position_sender_(
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800158 event_loop->MakeSender<superstructure::PositionStatic>(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700159 "/superstructure")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700160 drivetrain_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161 event_loop
162 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
163 "/drivetrain")) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000164 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
165 // we should ever see.
Austin Schuh54667ac2019-02-02 16:44:49 -0800166 UpdateFastEncoderFilterHz(kMaxDrivetrainShooterEncoderPulsesPerSecond);
167 UpdateMediumEncoderFilterHz(kMaxSuperstructureEncoderPulsesPerSecond);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000168 hall_filter_.SetPeriodNanoSeconds(100000);
169 }
170
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700171 void set_drivetrain_left_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000172 drivetrain_left_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000173 }
174
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700175 void set_drivetrain_right_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000176 drivetrain_right_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000177 }
178
Comran Morshed225f0b92016-02-10 20:34:27 +0000179 // Shooter setters.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700180 void set_shooter_left_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800181 fast_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000182 shooter_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000183 }
184
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700185 void set_shooter_right_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800186 fast_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000187 shooter_right_encoder_ = ::std::move(encoder);
188 }
189
190 // Intake setters.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700191 void set_intake_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800192 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000193 intake_encoder_.set_encoder(::std::move(encoder));
194 }
195
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700196 void set_intake_potentiometer(
197 ::std::unique_ptr<::frc::AnalogInput> potentiometer) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000198 intake_encoder_.set_potentiometer(::std::move(potentiometer));
199 }
200
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700201 void set_intake_index(::std::unique_ptr<::frc::DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800202 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000203 intake_encoder_.set_index(::std::move(index));
204 }
205
206 // Shoulder setters.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700207 void set_shoulder_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800208 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000209 shoulder_encoder_.set_encoder(::std::move(encoder));
210 }
211
212 void set_shoulder_potentiometer(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700213 ::std::unique_ptr<::frc::AnalogInput> potentiometer) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000214 shoulder_encoder_.set_potentiometer(::std::move(potentiometer));
215 }
216
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700217 void set_shoulder_index(::std::unique_ptr<::frc::DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800218 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000219 shoulder_encoder_.set_index(::std::move(index));
220 }
221
222 // Wrist setters.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700223 void set_wrist_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800224 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000225 wrist_encoder_.set_encoder(::std::move(encoder));
226 }
227
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700228 void set_wrist_potentiometer(
229 ::std::unique_ptr<::frc::AnalogInput> potentiometer) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000230 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
231 }
232
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700233 void set_wrist_index(::std::unique_ptr<::frc::DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800234 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000235 wrist_encoder_.set_index(::std::move(index));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000236 }
237
Comran Morshedaa0573c2016-03-05 19:05:54 +0000238 // Ball detector setter.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700239 void set_ball_detector(::std::unique_ptr<::frc::AnalogInput> analog) {
Comran Morshedaa0573c2016-03-05 19:05:54 +0000240 ball_detector_ = ::std::move(analog);
241 }
242
Brian Silverman68cb5c22016-03-20 18:11:14 -0700243 // Autonomous mode switch setter.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700244 void set_autonomous_mode(int i,
245 ::std::unique_ptr<::frc::DigitalInput> sensor) {
Brian Silverman68cb5c22016-03-20 18:11:14 -0700246 autonomous_modes_.at(i) = ::std::move(sensor);
247 }
248
Comran Morshed9a9948c2016-01-16 15:58:04 +0000249 // All of the DMA-related set_* calls must be made before this, and it doesn't
250 // hurt to do all of them.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000251
Austin Schuh54667ac2019-02-02 16:44:49 -0800252 void Start() {
253 AddToDMA(&intake_encoder_);
254 AddToDMA(&shoulder_encoder_);
255 AddToDMA(&wrist_encoder_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000256 }
257
258 void RunIteration() {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000259 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700260 auto builder = drivetrain_position_sender_.MakeBuilder();
261 frc971::control_loops::drivetrain::Position::Builder position_builder =
262 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000263
Alex Perrycb7da4b2019-08-28 19:35:56 -0700264 position_builder.add_right_encoder(
265 drivetrain_translate(-drivetrain_right_encoder_->GetRaw()));
266 position_builder.add_left_encoder(
267 -drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
268 position_builder.add_left_speed(
269 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
270 position_builder.add_right_speed(drivetrain_velocity_translate(
271 drivetrain_right_encoder_->GetPeriod()));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000272
Alex Perrycb7da4b2019-08-28 19:35:56 -0700273 position_builder.add_left_shifter_position(
274 hall_translate(drivetrain_left_hall_->GetVoltage()));
275 position_builder.add_right_shifter_position(
276 hall_translate(drivetrain_right_hall_->GetVoltage()));
277
milind1f1dca32021-07-03 13:50:07 -0700278 builder.CheckOk(builder.Send(position_builder.Finish()));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000279 }
Austin Schuh54667ac2019-02-02 16:44:49 -0800280 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000281
Austin Schuh54667ac2019-02-02 16:44:49 -0800282 void RunDmaIteration() {
283 const auto &values = constants::GetValues();
Comran Morshed225f0b92016-02-10 20:34:27 +0000284 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700285 auto builder = shooter_position_sender_.MakeBuilder();
286 shooter::Position::Builder shooter_builder =
287 builder.MakeBuilder<shooter::Position>();
288 shooter_builder.add_theta_left(
289 shooter_translate(-shooter_left_encoder_->GetRaw()));
290 shooter_builder.add_theta_right(
291 shooter_translate(shooter_right_encoder_->GetRaw()));
milind1f1dca32021-07-03 13:50:07 -0700292 builder.CheckOk(builder.Send(shooter_builder.Finish()));
Comran Morshed225f0b92016-02-10 20:34:27 +0000293 }
294
295 {
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800296 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
297 superstructure_position_sender_.MakeStaticBuilder();
Comran Morshed225f0b92016-02-10 20:34:27 +0000298
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800299 CopyPosition(intake_encoder_, builder->add_intake(), intake_translate,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700300 intake_pot_translate, false, values.intake.pot_offset);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800301 CopyPosition(shoulder_encoder_, builder->add_shoulder(),
302 shoulder_translate, shoulder_pot_translate, false,
303 values.shoulder.pot_offset);
304 CopyPosition(wrist_encoder_, builder->add_wrist(), wrist_translate,
305 wrist_pot_translate, true, values.wrist.pot_offset);
306 builder.CheckOk(builder.Send());
Comran Morshed225f0b92016-02-10 20:34:27 +0000307 }
Comran Morshedaa0573c2016-03-05 19:05:54 +0000308
309 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700310 auto builder = ball_detector_sender_.MakeBuilder();
311 ::y2016::sensors::BallDetector::Builder ball_detector_builder =
312 builder.MakeBuilder<y2016::sensors::BallDetector>();
313 ball_detector_builder.add_voltage(ball_detector_->GetVoltage());
milind1f1dca32021-07-03 13:50:07 -0700314 builder.CheckOk(builder.Send(ball_detector_builder.Finish()));
Comran Morshedaa0573c2016-03-05 19:05:54 +0000315 }
Brian Silverman68cb5c22016-03-20 18:11:14 -0700316
317 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700318 auto builder = auto_mode_sender_.MakeBuilder();
319 ::frc971::autonomous::AutonomousMode::Builder auto_builder =
320 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
321 int mode = 0;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700322 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
323 if (autonomous_modes_[i]->Get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700324 mode |= 1 << i;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700325 }
326 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700327 auto_builder.add_mode(mode);
milind1f1dca32021-07-03 13:50:07 -0700328 builder.CheckOk(builder.Send(auto_builder.Finish()));
Brian Silverman68cb5c22016-03-20 18:11:14 -0700329 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000330 }
331
Comran Morshed9a9948c2016-01-16 15:58:04 +0000332 private:
Austin Schuh4b652c92019-05-27 13:22:27 -0700333 ::aos::Sender<::y2016::sensors::BallDetector> ball_detector_sender_;
Austin Schuha250b2d2019-05-27 16:14:02 -0700334 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700335 ::aos::Sender<shooter::Position> shooter_position_sender_;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800336 ::aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700337 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700338 drivetrain_position_sender_;
Austin Schuh4b652c92019-05-27 13:22:27 -0700339
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700340 ::std::unique_ptr<::frc::AnalogInput> drivetrain_left_hall_,
341 drivetrain_right_hall_;
Comran Morshed225f0b92016-02-10 20:34:27 +0000342
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700343 ::std::unique_ptr<::frc::Encoder> shooter_left_encoder_,
344 shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000345 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
346 shoulder_encoder_, wrist_encoder_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700347 ::std::unique_ptr<::frc::AnalogInput> ball_detector_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000348
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700349 ::std::array<::std::unique_ptr<::frc::DigitalInput>, 4> autonomous_modes_;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700350
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700351 ::frc::DigitalGlitchFilter hall_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000352};
353
354class SolenoidWriter {
355 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800356 SolenoidWriter(::aos::ShmEventLoop *event_loop,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700357 ::frc971::wpilib::BufferedPcm *pcm)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000358 : pcm_(pcm),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700359 pneumatics_to_log_sender_(
360 event_loop->MakeSender<::frc971::wpilib::PneumaticsToLog>("/aos")),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700361 drivetrain_(
362 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700363 ->MakeFetcher<::frc971::control_loops::drivetrain::Output>(
364 "/drivetrain")),
365 shooter_(event_loop->MakeFetcher<shooter::Output>("/shooter")),
366 superstructure_(
367 event_loop
368 ->MakeFetcher<::y2016::control_loops::superstructure::Output>(
369 "/superstructure")) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700370 event_loop->set_name("Solenoids");
371 event_loop->SetRuntimeRealtimePriority(27);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000372
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700373 event_loop->OnRun([this]() { compressor_->Start(); });
374
375 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
376 ::std::chrono::milliseconds(20),
377 ::std::chrono::milliseconds(1));
378 }
379
380 void set_compressor(::std::unique_ptr<::frc::Compressor> compressor) {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800381 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000382 }
383
Comran Morshed71466fe2016-04-21 20:21:14 -0700384 void set_drivetrain_shifter(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000385 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700386 drivetrain_shifter_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000387 }
388
Comran Morshed71466fe2016-04-21 20:21:14 -0700389 void set_climber_trigger(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000390 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700391 climber_trigger_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000392 }
393
Sabina Davis2243cab2019-02-05 21:45:08 -0800394 void set_traverse(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuh843412b2016-03-20 16:48:46 -0700395 traverse_ = ::std::move(s);
396 }
397
398 void set_traverse_latch(
399 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
400 traverse_latch_ = ::std::move(s);
401 }
402
Comran Morshedb79c4242016-02-06 18:27:26 +0000403 void set_shooter_clamp(
404 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
405 shooter_clamp_ = ::std::move(s);
406 }
407
408 void set_shooter_pusher(
409 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
410 shooter_pusher_ = ::std::move(s);
411 }
412
Sabina Davis2243cab2019-02-05 21:45:08 -0800413 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhe0729a62016-03-12 21:54:17 -0800414 lights_ = ::std::move(s);
415 }
416
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700417 void set_flashlight(::std::unique_ptr<::frc::Relay> relay) {
Austin Schuh8b89d332016-03-24 20:19:12 -0700418 flashlight_ = ::std::move(relay);
419 }
420
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700421 private:
422 void Loop(const int iterations) {
423 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700424 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700425 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000426
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700427 {
428 drivetrain_.Fetch();
429 if (drivetrain_.get()) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700430 drivetrain_shifter_->Set(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700431 !(drivetrain_->left_high() || drivetrain_->right_high()));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000432 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700433 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000434
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700435 {
436 shooter_.Fetch();
437 if (shooter_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700438 shooter_clamp_->Set(shooter_->clamp_open());
439 shooter_pusher_->Set(shooter_->push_to_shooter());
440 lights_->Set(shooter_->lights_on());
441 if (shooter_->forwards_flashlight()) {
442 if (shooter_->backwards_flashlight()) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700443 flashlight_->Set(::frc::Relay::kOn);
Austin Schuhb2c33382016-04-03 16:09:17 -0700444 } else {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700445 flashlight_->Set(::frc::Relay::kReverse);
446 }
447 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700448 if (shooter_->backwards_flashlight()) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700449 flashlight_->Set(::frc::Relay::kForward);
450 } else {
451 flashlight_->Set(::frc::Relay::kOff);
Austin Schuhb2c33382016-04-03 16:09:17 -0700452 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000453 }
454 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700455 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000456
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700457 {
458 superstructure_.Fetch();
459 if (superstructure_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700460 climber_trigger_->Set(superstructure_->unfold_climber());
Comran Morshed71466fe2016-04-21 20:21:14 -0700461
Alex Perrycb7da4b2019-08-28 19:35:56 -0700462 traverse_->Set(superstructure_->traverse_down());
463 traverse_latch_->Set(superstructure_->traverse_unlatched());
Austin Schuh843412b2016-03-20 16:48:46 -0700464 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700465 }
Austin Schuh843412b2016-03-20 16:48:46 -0700466
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700467 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700468 auto builder = pneumatics_to_log_sender_.MakeBuilder();
469
470 ::frc971::wpilib::PneumaticsToLog::Builder to_log_builder =
471 builder.MakeBuilder<frc971::wpilib::PneumaticsToLog>();
472
473 to_log_builder.add_compressor_on(compressor_->Enabled());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000474
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700475 pcm_->Flush();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700476 to_log_builder.add_read_solenoids(pcm_->GetAll());
milind1f1dca32021-07-03 13:50:07 -0700477 (void)builder.Send(to_log_builder.Finish());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000478 }
479 }
480
Alex Perrycb7da4b2019-08-28 19:35:56 -0700481 ::frc971::wpilib::BufferedPcm *pcm_;
482 aos::Sender<::frc971::wpilib::PneumaticsToLog> pneumatics_to_log_sender_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000483
Comran Morshed71466fe2016-04-21 20:21:14 -0700484 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_shifter_,
485 shooter_clamp_, shooter_pusher_, lights_, traverse_, traverse_latch_,
486 climber_trigger_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700487 ::std::unique_ptr<::frc::Relay> flashlight_;
488 ::std::unique_ptr<::frc::Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000489
Alex Perrycb7da4b2019-08-28 19:35:56 -0700490 ::aos::Fetcher<::frc971::control_loops::drivetrain::Output> drivetrain_;
491 ::aos::Fetcher<shooter::Output> shooter_;
492 ::aos::Fetcher<::y2016::control_loops::superstructure::Output>
Austin Schuh843412b2016-03-20 16:48:46 -0700493 superstructure_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000494};
495
Alex Perrycb7da4b2019-08-28 19:35:56 -0700496class ShooterWriter : public LoopOutputHandler<shooter::Output> {
Comran Morshed225f0b92016-02-10 20:34:27 +0000497 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800498 ShooterWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700499 : LoopOutputHandler<shooter::Output>(event_loop, "/shooter") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800500
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700501 void set_shooter_left_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000502 shooter_left_talon_ = ::std::move(t);
503 }
504
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700505 void set_shooter_right_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000506 shooter_right_talon_ = ::std::move(t);
507 }
508
509 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700510 void Write(const shooter::Output &output) override {
511 shooter_left_talon_->SetSpeed(output.voltage_left() / 12.0);
512 shooter_right_talon_->SetSpeed(-output.voltage_right() / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000513 }
514
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700515 void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700516 AOS_LOG(WARNING, "Shooter output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800517 shooter_left_talon_->SetDisabled();
518 shooter_right_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000519 }
520
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700521 ::std::unique_ptr<::frc::Talon> shooter_left_talon_, shooter_right_talon_;
Comran Morshed225f0b92016-02-10 20:34:27 +0000522};
523
Alex Perrycb7da4b2019-08-28 19:35:56 -0700524class SuperstructureWriter : public LoopOutputHandler<superstructure::Output> {
Comran Morshed225f0b92016-02-10 20:34:27 +0000525 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800526 SuperstructureWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700527 : LoopOutputHandler<superstructure::Output>(event_loop,
528 "/superstructure") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800529
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700530 void set_intake_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000531 intake_talon_ = ::std::move(t);
532 }
533
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700534 void set_shoulder_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000535 shoulder_talon_ = ::std::move(t);
536 }
537
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700538 void set_wrist_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000539 wrist_talon_ = ::std::move(t);
540 }
541
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700542 void set_top_rollers_talon(::std::unique_ptr<::frc::Talon> t) {
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800543 top_rollers_talon_ = ::std::move(t);
544 }
545
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700546 void set_bottom_rollers_talon(::std::unique_ptr<::frc::Talon> t) {
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800547 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000548 }
549
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700550 void set_climber_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700551 climber_talon_ = ::std::move(t);
552 }
553
Comran Morshed225f0b92016-02-10 20:34:27 +0000554 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700555 virtual void Write(const superstructure::Output &output) override {
556 intake_talon_->SetSpeed(::aos::Clip(output.voltage_intake(),
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800557 -kMaxBringupPower, kMaxBringupPower) /
558 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700559 shoulder_talon_->SetSpeed(::aos::Clip(-output.voltage_shoulder(),
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800560 -kMaxBringupPower, kMaxBringupPower) /
561 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700562 wrist_talon_->SetSpeed(::aos::Clip(output.voltage_wrist(),
563 -kMaxBringupPower, kMaxBringupPower) /
564 12.0);
565 top_rollers_talon_->SetSpeed(-output.voltage_top_rollers() / 12.0);
566 bottom_rollers_talon_->SetSpeed(-output.voltage_bottom_rollers() / 12.0);
567 climber_talon_->SetSpeed(-output.voltage_climber() / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000568 }
569
570 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700571 AOS_LOG(WARNING, "Superstructure output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800572 intake_talon_->SetDisabled();
573 shoulder_talon_->SetDisabled();
574 wrist_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000575 }
576
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700577 ::std::unique_ptr<::frc::Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Comran Morshed71466fe2016-04-21 20:21:14 -0700578 top_rollers_talon_, bottom_rollers_talon_, climber_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000579};
580
Comran Morshed9a9948c2016-01-16 15:58:04 +0000581class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
582 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700583 ::std::unique_ptr<::frc::Encoder> make_encoder(int index) {
584 return make_unique<::frc::Encoder>(10 + index * 2, 11 + index * 2, false,
585 ::frc::Encoder::k4X);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000586 }
587
588 void Run() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700589 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800590 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700591
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700592 // Thread 1.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700593 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700594 ::frc971::wpilib::JoystickSender joystick_sender(
595 &joystick_sender_event_loop);
596 AddLoop(&joystick_sender_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000597
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700598 // Thread 2.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700599 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700600 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
601 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800602
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700603 // Thread 3.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700604 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700605 SensorReader sensor_reader(&sensor_reader_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000606
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700607 sensor_reader.set_drivetrain_left_encoder(make_encoder(5));
608 sensor_reader.set_drivetrain_right_encoder(make_encoder(6));
609 sensor_reader.set_drivetrain_left_hall(make_unique<::frc::AnalogInput>(5));
610 sensor_reader.set_drivetrain_right_hall(make_unique<::frc::AnalogInput>(6));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000611
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700612 sensor_reader.set_shooter_left_encoder(make_encoder(7));
613 sensor_reader.set_shooter_right_encoder(make_encoder(-3));
Comran Morshed225f0b92016-02-10 20:34:27 +0000614
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700615 sensor_reader.set_intake_encoder(make_encoder(0));
616 sensor_reader.set_intake_index(make_unique<::frc::DigitalInput>(0));
617 sensor_reader.set_intake_potentiometer(make_unique<::frc::AnalogInput>(0));
Comran Morshed225f0b92016-02-10 20:34:27 +0000618
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700619 sensor_reader.set_shoulder_encoder(make_encoder(4));
620 sensor_reader.set_shoulder_index(make_unique<::frc::DigitalInput>(2));
621 sensor_reader.set_shoulder_potentiometer(
622 make_unique<::frc::AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000623
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700624 sensor_reader.set_wrist_encoder(make_encoder(1));
625 sensor_reader.set_wrist_index(make_unique<::frc::DigitalInput>(1));
626 sensor_reader.set_wrist_potentiometer(make_unique<::frc::AnalogInput>(1));
Comran Morshed225f0b92016-02-10 20:34:27 +0000627
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700628 sensor_reader.set_ball_detector(make_unique<::frc::AnalogInput>(7));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000629
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700630 sensor_reader.set_autonomous_mode(0, make_unique<::frc::DigitalInput>(9));
631 sensor_reader.set_autonomous_mode(1, make_unique<::frc::DigitalInput>(8));
632 sensor_reader.set_autonomous_mode(2, make_unique<::frc::DigitalInput>(7));
633 sensor_reader.set_autonomous_mode(3, make_unique<::frc::DigitalInput>(6));
634 AddLoop(&sensor_reader_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000635
Brian Silverman003a4732018-03-11 14:02:15 -0700636 // TODO(Brian): This interacts poorly with the SpiRxClearer in ADIS16448.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700637 // Thread 4.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700638 ::aos::ShmEventLoop gyro_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700639 ::frc971::wpilib::GyroSender gyro_sender(&gyro_event_loop);
640 AddLoop(&gyro_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000641
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700642 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700643 ::aos::ShmEventLoop imu_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700644 auto imu_trigger = make_unique<::frc::DigitalInput>(3);
645 ::frc971::wpilib::ADIS16448 imu(&imu_event_loop, ::frc::SPI::Port::kMXP,
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800646 imu_trigger.get());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700647 AddLoop(&imu_event_loop);
Brian Silverman5f17a972016-02-28 01:49:32 -0500648
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700649 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700650 ::aos::ShmEventLoop output_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700651 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
Sabina Davisb71bc282019-02-03 01:17:23 -0800652 drivetrain_writer.set_left_controller0(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700653 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(5)), false);
Sabina Davisb71bc282019-02-03 01:17:23 -0800654 drivetrain_writer.set_right_controller0(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700655 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(4)), true);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000656
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700657 ShooterWriter shooter_writer(&output_event_loop);
Comran Morshed225f0b92016-02-10 20:34:27 +0000658 shooter_writer.set_shooter_left_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700659 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000660 shooter_writer.set_shooter_right_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700661 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000662
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700663 SuperstructureWriter superstructure_writer(&output_event_loop);
Comran Morshed225f0b92016-02-10 20:34:27 +0000664 superstructure_writer.set_intake_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700665 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000666 superstructure_writer.set_shoulder_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700667 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000668 superstructure_writer.set_wrist_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700669 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800670 superstructure_writer.set_top_rollers_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700671 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800672 superstructure_writer.set_bottom_rollers_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700673 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(0)));
Comran Morshed71466fe2016-04-21 20:21:14 -0700674 superstructure_writer.set_climber_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700675 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(7)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000676
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700677 AddLoop(&output_event_loop);
678
679 // Thread 6.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700680 ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000681 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
682 new ::frc971::wpilib::BufferedPcm());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700683 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm.get());
Comran Morshed71466fe2016-04-21 20:21:14 -0700684 solenoid_writer.set_drivetrain_shifter(pcm->MakeSolenoid(0));
Austin Schuh843412b2016-03-20 16:48:46 -0700685 solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2));
686 solenoid_writer.set_traverse(pcm->MakeSolenoid(3));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800687 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
688 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Austin Schuhe0729a62016-03-12 21:54:17 -0800689 solenoid_writer.set_lights(pcm->MakeSolenoid(6));
Comran Morshed71466fe2016-04-21 20:21:14 -0700690 solenoid_writer.set_climber_trigger(pcm->MakeSolenoid(1));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700691 solenoid_writer.set_flashlight(make_unique<::frc::Relay>(0));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000692
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700693 solenoid_writer.set_compressor(make_unique<::frc::Compressor>());
694 AddLoop(&solenoid_writer_event_loop);
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800695
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700696 RunLoops();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000697 }
698};
699
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800700} // namespace y2016::wpilib
Comran Morshed9a9948c2016-01-16 15:58:04 +0000701
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000702AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);