blob: 0e980859511adc7ce3e0b5832ad86b00f0581b61 [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
Comran Morshed6c6a0a92016-01-17 12:45:16 +000061namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000062namespace wpilib {
Austin Schuha9992ff2016-02-28 21:59:23 -080063namespace {
64constexpr double kMaxBringupPower = 12.0;
65} // namespace
Comran Morshed9a9948c2016-01-16 15:58:04 +000066
67// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
68// DMA stuff and then removing the * 2.0 in *_translate.
69// The low bit is direction.
70
Comran Morshed225f0b92016-02-10 20:34:27 +000071// Translates for the sensor values to convert raw index pulses into something
72// with proper units.
73
74// TODO(comran): Template these methods since there is a lot of repetition here.
75double hall_translate(double in) {
76 // Turn voltage from our 3-state halls into a ratio that the loop can use.
77 return in / 5.0;
78}
79
Comran Morshed9a9948c2016-01-16 15:58:04 +000080double drivetrain_translate(int32_t in) {
Comran Morshed6c6a0a92016-01-17 12:45:16 +000081 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000082 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080083 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000084}
85
86double drivetrain_velocity_translate(double in) {
87 return (1.0 / in) / 256.0 /*cpr*/ *
Comran Morshed225f0b92016-02-10 20:34:27 +000088 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080089 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000090}
91
Comran Morshed225f0b92016-02-10 20:34:27 +000092double shooter_translate(int32_t in) {
Comran Morshed5bb12112016-02-16 13:48:57 +000093 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000094 constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
95}
Comran Morshed9a9948c2016-01-16 15:58:04 +000096
Comran Morshed225f0b92016-02-10 20:34:27 +000097double intake_translate(int32_t in) {
Austin Schuh9f77fd22016-02-21 02:53:58 -080098 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000099 constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
100}
101
102double shoulder_translate(int32_t in) {
103 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
104 constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/);
105}
106
107double wrist_translate(int32_t in) {
108 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
109 constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/);
110}
111
112double intake_pot_translate(double voltage) {
113 return voltage * constants::Values::kIntakePotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000114 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000115}
116
117double shoulder_pot_translate(double voltage) {
118 return voltage * constants::Values::kShoulderPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000119 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000120}
121
122double wrist_pot_translate(double voltage) {
123 return voltage * constants::Values::kWristPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000124 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000125}
126
Comran Morshed1b764322016-02-14 20:18:12 +0000127constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
128 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
129 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
130 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
131
132constexpr double kMaxShooterEncoderPulsesPerSecond =
133 18700.0 /* 775pro free speed RPM */ * 12.0 /
134 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
135 128.0 /* CPR */ * 4 /* edges per cycle */;
136
137double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max(
138 kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond);
139
140constexpr double kMaxSuperstructureEncoderPulsesPerSecond =
141 18700.0 /* 775pro free speed RPM */ * 12.0 /
142 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
143 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000144
Comran Morshed225f0b92016-02-10 20:34:27 +0000145// Class to send position messages with sensor readings to our loops.
Austin Schuh54667ac2019-02-02 16:44:49 -0800146class SensorReader : public ::frc971::wpilib::SensorReader {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000147 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800148 SensorReader(::aos::ShmEventLoop *event_loop)
Austin Schuh4b652c92019-05-27 13:22:27 -0700149 : ::frc971::wpilib::SensorReader(event_loop),
150 ball_detector_sender_(
151 event_loop->MakeSender<::y2016::sensors::BallDetector>(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700152 "/superstructure")),
Austin Schuha250b2d2019-05-27 16:14:02 -0700153 auto_mode_sender_(
154 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
Tyler Chatow24b5db12020-01-06 21:16:56 -0800155 "/autonomous")),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700156 shooter_position_sender_(
157 event_loop->MakeSender<shooter::Position>("/shooter")),
Austin Schuh9481d0d2019-06-29 21:56:17 -0700158 superstructure_position_sender_(
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800159 event_loop->MakeSender<superstructure::PositionStatic>(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700160 "/superstructure")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700161 drivetrain_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700162 event_loop
163 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
164 "/drivetrain")) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000165 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
166 // we should ever see.
Austin Schuh54667ac2019-02-02 16:44:49 -0800167 UpdateFastEncoderFilterHz(kMaxDrivetrainShooterEncoderPulsesPerSecond);
168 UpdateMediumEncoderFilterHz(kMaxSuperstructureEncoderPulsesPerSecond);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000169 hall_filter_.SetPeriodNanoSeconds(100000);
170 }
171
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700172 void set_drivetrain_left_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000173 drivetrain_left_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000174 }
175
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700176 void set_drivetrain_right_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000177 drivetrain_right_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000178 }
179
Comran Morshed225f0b92016-02-10 20:34:27 +0000180 // Shooter setters.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700181 void set_shooter_left_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800182 fast_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000183 shooter_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000184 }
185
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700186 void set_shooter_right_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800187 fast_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000188 shooter_right_encoder_ = ::std::move(encoder);
189 }
190
191 // Intake setters.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700192 void set_intake_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800193 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000194 intake_encoder_.set_encoder(::std::move(encoder));
195 }
196
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700197 void set_intake_potentiometer(
198 ::std::unique_ptr<::frc::AnalogInput> potentiometer) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000199 intake_encoder_.set_potentiometer(::std::move(potentiometer));
200 }
201
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700202 void set_intake_index(::std::unique_ptr<::frc::DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800203 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000204 intake_encoder_.set_index(::std::move(index));
205 }
206
207 // Shoulder setters.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700208 void set_shoulder_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800209 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000210 shoulder_encoder_.set_encoder(::std::move(encoder));
211 }
212
213 void set_shoulder_potentiometer(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700214 ::std::unique_ptr<::frc::AnalogInput> potentiometer) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000215 shoulder_encoder_.set_potentiometer(::std::move(potentiometer));
216 }
217
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700218 void set_shoulder_index(::std::unique_ptr<::frc::DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800219 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000220 shoulder_encoder_.set_index(::std::move(index));
221 }
222
223 // Wrist setters.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700224 void set_wrist_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800225 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000226 wrist_encoder_.set_encoder(::std::move(encoder));
227 }
228
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700229 void set_wrist_potentiometer(
230 ::std::unique_ptr<::frc::AnalogInput> potentiometer) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000231 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
232 }
233
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700234 void set_wrist_index(::std::unique_ptr<::frc::DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800235 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000236 wrist_encoder_.set_index(::std::move(index));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000237 }
238
Comran Morshedaa0573c2016-03-05 19:05:54 +0000239 // Ball detector setter.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700240 void set_ball_detector(::std::unique_ptr<::frc::AnalogInput> analog) {
Comran Morshedaa0573c2016-03-05 19:05:54 +0000241 ball_detector_ = ::std::move(analog);
242 }
243
Brian Silverman68cb5c22016-03-20 18:11:14 -0700244 // Autonomous mode switch setter.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700245 void set_autonomous_mode(int i,
246 ::std::unique_ptr<::frc::DigitalInput> sensor) {
Brian Silverman68cb5c22016-03-20 18:11:14 -0700247 autonomous_modes_.at(i) = ::std::move(sensor);
248 }
249
Comran Morshed9a9948c2016-01-16 15:58:04 +0000250 // All of the DMA-related set_* calls must be made before this, and it doesn't
251 // hurt to do all of them.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000252
Austin Schuh54667ac2019-02-02 16:44:49 -0800253 void Start() {
254 AddToDMA(&intake_encoder_);
255 AddToDMA(&shoulder_encoder_);
256 AddToDMA(&wrist_encoder_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000257 }
258
259 void RunIteration() {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000260 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700261 auto builder = drivetrain_position_sender_.MakeBuilder();
262 frc971::control_loops::drivetrain::Position::Builder position_builder =
263 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000264
Alex Perrycb7da4b2019-08-28 19:35:56 -0700265 position_builder.add_right_encoder(
266 drivetrain_translate(-drivetrain_right_encoder_->GetRaw()));
267 position_builder.add_left_encoder(
268 -drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
269 position_builder.add_left_speed(
270 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
271 position_builder.add_right_speed(drivetrain_velocity_translate(
272 drivetrain_right_encoder_->GetPeriod()));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000273
Alex Perrycb7da4b2019-08-28 19:35:56 -0700274 position_builder.add_left_shifter_position(
275 hall_translate(drivetrain_left_hall_->GetVoltage()));
276 position_builder.add_right_shifter_position(
277 hall_translate(drivetrain_right_hall_->GetVoltage()));
278
milind1f1dca32021-07-03 13:50:07 -0700279 builder.CheckOk(builder.Send(position_builder.Finish()));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000280 }
Austin Schuh54667ac2019-02-02 16:44:49 -0800281 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000282
Austin Schuh54667ac2019-02-02 16:44:49 -0800283 void RunDmaIteration() {
284 const auto &values = constants::GetValues();
Comran Morshed225f0b92016-02-10 20:34:27 +0000285 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700286 auto builder = shooter_position_sender_.MakeBuilder();
287 shooter::Position::Builder shooter_builder =
288 builder.MakeBuilder<shooter::Position>();
289 shooter_builder.add_theta_left(
290 shooter_translate(-shooter_left_encoder_->GetRaw()));
291 shooter_builder.add_theta_right(
292 shooter_translate(shooter_right_encoder_->GetRaw()));
milind1f1dca32021-07-03 13:50:07 -0700293 builder.CheckOk(builder.Send(shooter_builder.Finish()));
Comran Morshed225f0b92016-02-10 20:34:27 +0000294 }
295
296 {
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800297 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
298 superstructure_position_sender_.MakeStaticBuilder();
Comran Morshed225f0b92016-02-10 20:34:27 +0000299
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800300 CopyPosition(intake_encoder_, builder->add_intake(), intake_translate,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700301 intake_pot_translate, false, values.intake.pot_offset);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800302 CopyPosition(shoulder_encoder_, builder->add_shoulder(),
303 shoulder_translate, shoulder_pot_translate, false,
304 values.shoulder.pot_offset);
305 CopyPosition(wrist_encoder_, builder->add_wrist(), wrist_translate,
306 wrist_pot_translate, true, values.wrist.pot_offset);
307 builder.CheckOk(builder.Send());
Comran Morshed225f0b92016-02-10 20:34:27 +0000308 }
Comran Morshedaa0573c2016-03-05 19:05:54 +0000309
310 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700311 auto builder = ball_detector_sender_.MakeBuilder();
312 ::y2016::sensors::BallDetector::Builder ball_detector_builder =
313 builder.MakeBuilder<y2016::sensors::BallDetector>();
314 ball_detector_builder.add_voltage(ball_detector_->GetVoltage());
milind1f1dca32021-07-03 13:50:07 -0700315 builder.CheckOk(builder.Send(ball_detector_builder.Finish()));
Comran Morshedaa0573c2016-03-05 19:05:54 +0000316 }
Brian Silverman68cb5c22016-03-20 18:11:14 -0700317
318 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700319 auto builder = auto_mode_sender_.MakeBuilder();
320 ::frc971::autonomous::AutonomousMode::Builder auto_builder =
321 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
322 int mode = 0;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700323 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
324 if (autonomous_modes_[i]->Get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700325 mode |= 1 << i;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700326 }
327 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700328 auto_builder.add_mode(mode);
milind1f1dca32021-07-03 13:50:07 -0700329 builder.CheckOk(builder.Send(auto_builder.Finish()));
Brian Silverman68cb5c22016-03-20 18:11:14 -0700330 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000331 }
332
Comran Morshed9a9948c2016-01-16 15:58:04 +0000333 private:
Austin Schuh4b652c92019-05-27 13:22:27 -0700334 ::aos::Sender<::y2016::sensors::BallDetector> ball_detector_sender_;
Austin Schuha250b2d2019-05-27 16:14:02 -0700335 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700336 ::aos::Sender<shooter::Position> shooter_position_sender_;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800337 ::aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700338 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700339 drivetrain_position_sender_;
Austin Schuh4b652c92019-05-27 13:22:27 -0700340
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700341 ::std::unique_ptr<::frc::AnalogInput> drivetrain_left_hall_,
342 drivetrain_right_hall_;
Comran Morshed225f0b92016-02-10 20:34:27 +0000343
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700344 ::std::unique_ptr<::frc::Encoder> shooter_left_encoder_,
345 shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000346 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
347 shoulder_encoder_, wrist_encoder_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700348 ::std::unique_ptr<::frc::AnalogInput> ball_detector_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000349
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700350 ::std::array<::std::unique_ptr<::frc::DigitalInput>, 4> autonomous_modes_;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700351
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700352 ::frc::DigitalGlitchFilter hall_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000353};
354
355class SolenoidWriter {
356 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800357 SolenoidWriter(::aos::ShmEventLoop *event_loop,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700358 ::frc971::wpilib::BufferedPcm *pcm)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000359 : pcm_(pcm),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700360 pneumatics_to_log_sender_(
361 event_loop->MakeSender<::frc971::wpilib::PneumaticsToLog>("/aos")),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700362 drivetrain_(
363 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700364 ->MakeFetcher<::frc971::control_loops::drivetrain::Output>(
365 "/drivetrain")),
366 shooter_(event_loop->MakeFetcher<shooter::Output>("/shooter")),
367 superstructure_(
368 event_loop
369 ->MakeFetcher<::y2016::control_loops::superstructure::Output>(
370 "/superstructure")) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700371 event_loop->set_name("Solenoids");
372 event_loop->SetRuntimeRealtimePriority(27);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000373
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700374 event_loop->OnRun([this]() { compressor_->Start(); });
375
376 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
377 ::std::chrono::milliseconds(20),
378 ::std::chrono::milliseconds(1));
379 }
380
381 void set_compressor(::std::unique_ptr<::frc::Compressor> compressor) {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800382 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000383 }
384
Comran Morshed71466fe2016-04-21 20:21:14 -0700385 void set_drivetrain_shifter(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000386 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700387 drivetrain_shifter_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000388 }
389
Comran Morshed71466fe2016-04-21 20:21:14 -0700390 void set_climber_trigger(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000391 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700392 climber_trigger_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000393 }
394
Sabina Davis2243cab2019-02-05 21:45:08 -0800395 void set_traverse(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuh843412b2016-03-20 16:48:46 -0700396 traverse_ = ::std::move(s);
397 }
398
399 void set_traverse_latch(
400 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
401 traverse_latch_ = ::std::move(s);
402 }
403
Comran Morshedb79c4242016-02-06 18:27:26 +0000404 void set_shooter_clamp(
405 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
406 shooter_clamp_ = ::std::move(s);
407 }
408
409 void set_shooter_pusher(
410 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
411 shooter_pusher_ = ::std::move(s);
412 }
413
Sabina Davis2243cab2019-02-05 21:45:08 -0800414 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhe0729a62016-03-12 21:54:17 -0800415 lights_ = ::std::move(s);
416 }
417
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700418 void set_flashlight(::std::unique_ptr<::frc::Relay> relay) {
Austin Schuh8b89d332016-03-24 20:19:12 -0700419 flashlight_ = ::std::move(relay);
420 }
421
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700422 private:
423 void Loop(const int iterations) {
424 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700425 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700426 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000427
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700428 {
429 drivetrain_.Fetch();
430 if (drivetrain_.get()) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700431 drivetrain_shifter_->Set(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700432 !(drivetrain_->left_high() || drivetrain_->right_high()));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000433 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700434 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000435
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700436 {
437 shooter_.Fetch();
438 if (shooter_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700439 shooter_clamp_->Set(shooter_->clamp_open());
440 shooter_pusher_->Set(shooter_->push_to_shooter());
441 lights_->Set(shooter_->lights_on());
442 if (shooter_->forwards_flashlight()) {
443 if (shooter_->backwards_flashlight()) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700444 flashlight_->Set(::frc::Relay::kOn);
Austin Schuhb2c33382016-04-03 16:09:17 -0700445 } else {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700446 flashlight_->Set(::frc::Relay::kReverse);
447 }
448 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700449 if (shooter_->backwards_flashlight()) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700450 flashlight_->Set(::frc::Relay::kForward);
451 } else {
452 flashlight_->Set(::frc::Relay::kOff);
Austin Schuhb2c33382016-04-03 16:09:17 -0700453 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000454 }
455 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700456 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000457
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700458 {
459 superstructure_.Fetch();
460 if (superstructure_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700461 climber_trigger_->Set(superstructure_->unfold_climber());
Comran Morshed71466fe2016-04-21 20:21:14 -0700462
Alex Perrycb7da4b2019-08-28 19:35:56 -0700463 traverse_->Set(superstructure_->traverse_down());
464 traverse_latch_->Set(superstructure_->traverse_unlatched());
Austin Schuh843412b2016-03-20 16:48:46 -0700465 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700466 }
Austin Schuh843412b2016-03-20 16:48:46 -0700467
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700468 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700469 auto builder = pneumatics_to_log_sender_.MakeBuilder();
470
471 ::frc971::wpilib::PneumaticsToLog::Builder to_log_builder =
472 builder.MakeBuilder<frc971::wpilib::PneumaticsToLog>();
473
474 to_log_builder.add_compressor_on(compressor_->Enabled());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000475
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700476 pcm_->Flush();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700477 to_log_builder.add_read_solenoids(pcm_->GetAll());
milind1f1dca32021-07-03 13:50:07 -0700478 (void)builder.Send(to_log_builder.Finish());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000479 }
480 }
481
Alex Perrycb7da4b2019-08-28 19:35:56 -0700482 ::frc971::wpilib::BufferedPcm *pcm_;
483 aos::Sender<::frc971::wpilib::PneumaticsToLog> pneumatics_to_log_sender_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000484
Comran Morshed71466fe2016-04-21 20:21:14 -0700485 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_shifter_,
486 shooter_clamp_, shooter_pusher_, lights_, traverse_, traverse_latch_,
487 climber_trigger_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700488 ::std::unique_ptr<::frc::Relay> flashlight_;
489 ::std::unique_ptr<::frc::Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000490
Alex Perrycb7da4b2019-08-28 19:35:56 -0700491 ::aos::Fetcher<::frc971::control_loops::drivetrain::Output> drivetrain_;
492 ::aos::Fetcher<shooter::Output> shooter_;
493 ::aos::Fetcher<::y2016::control_loops::superstructure::Output>
Austin Schuh843412b2016-03-20 16:48:46 -0700494 superstructure_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000495};
496
Alex Perrycb7da4b2019-08-28 19:35:56 -0700497class ShooterWriter : public LoopOutputHandler<shooter::Output> {
Comran Morshed225f0b92016-02-10 20:34:27 +0000498 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800499 ShooterWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700500 : LoopOutputHandler<shooter::Output>(event_loop, "/shooter") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800501
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700502 void set_shooter_left_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000503 shooter_left_talon_ = ::std::move(t);
504 }
505
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700506 void set_shooter_right_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000507 shooter_right_talon_ = ::std::move(t);
508 }
509
510 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700511 void Write(const shooter::Output &output) override {
512 shooter_left_talon_->SetSpeed(output.voltage_left() / 12.0);
513 shooter_right_talon_->SetSpeed(-output.voltage_right() / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000514 }
515
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700516 void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700517 AOS_LOG(WARNING, "Shooter output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800518 shooter_left_talon_->SetDisabled();
519 shooter_right_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000520 }
521
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700522 ::std::unique_ptr<::frc::Talon> shooter_left_talon_, shooter_right_talon_;
Comran Morshed225f0b92016-02-10 20:34:27 +0000523};
524
Alex Perrycb7da4b2019-08-28 19:35:56 -0700525class SuperstructureWriter : public LoopOutputHandler<superstructure::Output> {
Comran Morshed225f0b92016-02-10 20:34:27 +0000526 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800527 SuperstructureWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700528 : LoopOutputHandler<superstructure::Output>(event_loop,
529 "/superstructure") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800530
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700531 void set_intake_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000532 intake_talon_ = ::std::move(t);
533 }
534
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700535 void set_shoulder_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000536 shoulder_talon_ = ::std::move(t);
537 }
538
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700539 void set_wrist_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000540 wrist_talon_ = ::std::move(t);
541 }
542
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700543 void set_top_rollers_talon(::std::unique_ptr<::frc::Talon> t) {
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800544 top_rollers_talon_ = ::std::move(t);
545 }
546
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700547 void set_bottom_rollers_talon(::std::unique_ptr<::frc::Talon> t) {
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800548 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000549 }
550
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700551 void set_climber_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700552 climber_talon_ = ::std::move(t);
553 }
554
Comran Morshed225f0b92016-02-10 20:34:27 +0000555 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700556 virtual void Write(const superstructure::Output &output) override {
557 intake_talon_->SetSpeed(::aos::Clip(output.voltage_intake(),
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800558 -kMaxBringupPower, kMaxBringupPower) /
559 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700560 shoulder_talon_->SetSpeed(::aos::Clip(-output.voltage_shoulder(),
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800561 -kMaxBringupPower, kMaxBringupPower) /
562 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700563 wrist_talon_->SetSpeed(::aos::Clip(output.voltage_wrist(),
564 -kMaxBringupPower, kMaxBringupPower) /
565 12.0);
566 top_rollers_talon_->SetSpeed(-output.voltage_top_rollers() / 12.0);
567 bottom_rollers_talon_->SetSpeed(-output.voltage_bottom_rollers() / 12.0);
568 climber_talon_->SetSpeed(-output.voltage_climber() / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000569 }
570
571 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700572 AOS_LOG(WARNING, "Superstructure output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800573 intake_talon_->SetDisabled();
574 shoulder_talon_->SetDisabled();
575 wrist_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000576 }
577
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700578 ::std::unique_ptr<::frc::Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Comran Morshed71466fe2016-04-21 20:21:14 -0700579 top_rollers_talon_, bottom_rollers_talon_, climber_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000580};
581
Comran Morshed9a9948c2016-01-16 15:58:04 +0000582class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
583 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700584 ::std::unique_ptr<::frc::Encoder> make_encoder(int index) {
585 return make_unique<::frc::Encoder>(10 + index * 2, 11 + index * 2, false,
586 ::frc::Encoder::k4X);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000587 }
588
589 void Run() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700590 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800591 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700592
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700593 // Thread 1.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700594 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700595 ::frc971::wpilib::JoystickSender joystick_sender(
596 &joystick_sender_event_loop);
597 AddLoop(&joystick_sender_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000598
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700599 // Thread 2.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700600 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700601 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
602 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800603
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700604 // Thread 3.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700605 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700606 SensorReader sensor_reader(&sensor_reader_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000607
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700608 sensor_reader.set_drivetrain_left_encoder(make_encoder(5));
609 sensor_reader.set_drivetrain_right_encoder(make_encoder(6));
610 sensor_reader.set_drivetrain_left_hall(make_unique<::frc::AnalogInput>(5));
611 sensor_reader.set_drivetrain_right_hall(make_unique<::frc::AnalogInput>(6));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000612
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700613 sensor_reader.set_shooter_left_encoder(make_encoder(7));
614 sensor_reader.set_shooter_right_encoder(make_encoder(-3));
Comran Morshed225f0b92016-02-10 20:34:27 +0000615
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700616 sensor_reader.set_intake_encoder(make_encoder(0));
617 sensor_reader.set_intake_index(make_unique<::frc::DigitalInput>(0));
618 sensor_reader.set_intake_potentiometer(make_unique<::frc::AnalogInput>(0));
Comran Morshed225f0b92016-02-10 20:34:27 +0000619
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700620 sensor_reader.set_shoulder_encoder(make_encoder(4));
621 sensor_reader.set_shoulder_index(make_unique<::frc::DigitalInput>(2));
622 sensor_reader.set_shoulder_potentiometer(
623 make_unique<::frc::AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000624
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700625 sensor_reader.set_wrist_encoder(make_encoder(1));
626 sensor_reader.set_wrist_index(make_unique<::frc::DigitalInput>(1));
627 sensor_reader.set_wrist_potentiometer(make_unique<::frc::AnalogInput>(1));
Comran Morshed225f0b92016-02-10 20:34:27 +0000628
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700629 sensor_reader.set_ball_detector(make_unique<::frc::AnalogInput>(7));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000630
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700631 sensor_reader.set_autonomous_mode(0, make_unique<::frc::DigitalInput>(9));
632 sensor_reader.set_autonomous_mode(1, make_unique<::frc::DigitalInput>(8));
633 sensor_reader.set_autonomous_mode(2, make_unique<::frc::DigitalInput>(7));
634 sensor_reader.set_autonomous_mode(3, make_unique<::frc::DigitalInput>(6));
635 AddLoop(&sensor_reader_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000636
Brian Silverman003a4732018-03-11 14:02:15 -0700637 // TODO(Brian): This interacts poorly with the SpiRxClearer in ADIS16448.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700638 // Thread 4.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700639 ::aos::ShmEventLoop gyro_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700640 ::frc971::wpilib::GyroSender gyro_sender(&gyro_event_loop);
641 AddLoop(&gyro_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000642
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700643 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700644 ::aos::ShmEventLoop imu_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700645 auto imu_trigger = make_unique<::frc::DigitalInput>(3);
646 ::frc971::wpilib::ADIS16448 imu(&imu_event_loop, ::frc::SPI::Port::kMXP,
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800647 imu_trigger.get());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700648 AddLoop(&imu_event_loop);
Brian Silverman5f17a972016-02-28 01:49:32 -0500649
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700650 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700651 ::aos::ShmEventLoop output_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700652 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
Sabina Davisb71bc282019-02-03 01:17:23 -0800653 drivetrain_writer.set_left_controller0(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700654 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(5)), false);
Sabina Davisb71bc282019-02-03 01:17:23 -0800655 drivetrain_writer.set_right_controller0(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700656 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(4)), true);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000657
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700658 ShooterWriter shooter_writer(&output_event_loop);
Comran Morshed225f0b92016-02-10 20:34:27 +0000659 shooter_writer.set_shooter_left_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700660 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000661 shooter_writer.set_shooter_right_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700662 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000663
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700664 SuperstructureWriter superstructure_writer(&output_event_loop);
Comran Morshed225f0b92016-02-10 20:34:27 +0000665 superstructure_writer.set_intake_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700666 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000667 superstructure_writer.set_shoulder_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700668 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000669 superstructure_writer.set_wrist_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700670 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800671 superstructure_writer.set_top_rollers_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700672 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800673 superstructure_writer.set_bottom_rollers_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700674 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(0)));
Comran Morshed71466fe2016-04-21 20:21:14 -0700675 superstructure_writer.set_climber_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700676 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(7)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000677
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700678 AddLoop(&output_event_loop);
679
680 // Thread 6.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700681 ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000682 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
683 new ::frc971::wpilib::BufferedPcm());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700684 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm.get());
Comran Morshed71466fe2016-04-21 20:21:14 -0700685 solenoid_writer.set_drivetrain_shifter(pcm->MakeSolenoid(0));
Austin Schuh843412b2016-03-20 16:48:46 -0700686 solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2));
687 solenoid_writer.set_traverse(pcm->MakeSolenoid(3));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800688 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
689 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Austin Schuhe0729a62016-03-12 21:54:17 -0800690 solenoid_writer.set_lights(pcm->MakeSolenoid(6));
Comran Morshed71466fe2016-04-21 20:21:14 -0700691 solenoid_writer.set_climber_trigger(pcm->MakeSolenoid(1));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700692 solenoid_writer.set_flashlight(make_unique<::frc::Relay>(0));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000693
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700694 solenoid_writer.set_compressor(make_unique<::frc::Compressor>());
695 AddLoop(&solenoid_writer_event_loop);
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800696
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700697 RunLoops();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000698 }
699};
700
701} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000702} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000703
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000704AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);