blob: e8ad2f611559406a9495e476a28e54644918381b [file] [log] [blame]
Sabina Davis2243cab2019-02-05 21:45:08 -08001#include <inttypes.h>
Comran Morshed9a9948c2016-01-16 15:58:04 +00002#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Comran Morshed9a9948c2016-01-16 15:58:04 +00005
Brian Silverman68cb5c22016-03-20 18:11:14 -07006#include <array>
Sabina Davis2243cab2019-02-05 21:45:08 -08007#include <chrono>
8#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"
53#include "y2016/control_loops/superstructure/superstructure_position_generated.h"
54#include "y2016/queues/ball_detector_generated.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000055
Vinay Sivae52a6b32021-07-10 15:19:26 -070056using std::make_unique;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070057using ::frc971::wpilib::LoopOutputHandler;
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_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700159 event_loop->MakeSender<superstructure::Position>(
160 "/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
279 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()));
293 builder.Send(shooter_builder.Finish());
Comran Morshed225f0b92016-02-10 20:34:27 +0000294 }
295
296 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700297 auto builder = superstructure_position_sender_.MakeBuilder();
Comran Morshed225f0b92016-02-10 20:34:27 +0000298
Alex Perrycb7da4b2019-08-28 19:35:56 -0700299 frc971::PotAndIndexPositionT intake;
300 CopyPosition(intake_encoder_, &intake, intake_translate,
301 intake_pot_translate, false, values.intake.pot_offset);
302 flatbuffers::Offset<frc971::PotAndIndexPosition> intake_offset =
303 frc971::PotAndIndexPosition::Pack(*builder.fbb(), &intake);
304
305 frc971::PotAndIndexPositionT shoulder;
306 CopyPosition(shoulder_encoder_, &shoulder, shoulder_translate,
307 shoulder_pot_translate, false, values.shoulder.pot_offset);
308 flatbuffers::Offset<frc971::PotAndIndexPosition> shoulder_offset =
309 frc971::PotAndIndexPosition::Pack(*builder.fbb(), &shoulder);
310
311 frc971::PotAndIndexPositionT wrist;
312 CopyPosition(wrist_encoder_, &wrist, wrist_translate, wrist_pot_translate,
313 true, values.wrist.pot_offset);
314 flatbuffers::Offset<frc971::PotAndIndexPosition> wrist_offset =
315 frc971::PotAndIndexPosition::Pack(*builder.fbb(), &wrist);
316
317 superstructure::Position::Builder position_builder =
318 builder.MakeBuilder<superstructure::Position>();
319
320 position_builder.add_intake(intake_offset);
321 position_builder.add_shoulder(shoulder_offset);
322 position_builder.add_wrist(wrist_offset);
323
324 builder.Send(position_builder.Finish());
Comran Morshed225f0b92016-02-10 20:34:27 +0000325 }
Comran Morshedaa0573c2016-03-05 19:05:54 +0000326
327 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700328 auto builder = ball_detector_sender_.MakeBuilder();
329 ::y2016::sensors::BallDetector::Builder ball_detector_builder =
330 builder.MakeBuilder<y2016::sensors::BallDetector>();
331 ball_detector_builder.add_voltage(ball_detector_->GetVoltage());
332 builder.Send(ball_detector_builder.Finish());
Comran Morshedaa0573c2016-03-05 19:05:54 +0000333 }
Brian Silverman68cb5c22016-03-20 18:11:14 -0700334
335 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700336 auto builder = auto_mode_sender_.MakeBuilder();
337 ::frc971::autonomous::AutonomousMode::Builder auto_builder =
338 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
339 int mode = 0;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700340 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
341 if (autonomous_modes_[i]->Get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700342 mode |= 1 << i;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700343 }
344 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700345 auto_builder.add_mode(mode);
346 builder.Send(auto_builder.Finish());
Brian Silverman68cb5c22016-03-20 18:11:14 -0700347 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000348 }
349
Comran Morshed9a9948c2016-01-16 15:58:04 +0000350 private:
Austin Schuh4b652c92019-05-27 13:22:27 -0700351 ::aos::Sender<::y2016::sensors::BallDetector> ball_detector_sender_;
Austin Schuha250b2d2019-05-27 16:14:02 -0700352 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700353 ::aos::Sender<shooter::Position> shooter_position_sender_;
354 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
355 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700356 drivetrain_position_sender_;
Austin Schuh4b652c92019-05-27 13:22:27 -0700357
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700358 ::std::unique_ptr<::frc::AnalogInput> drivetrain_left_hall_,
359 drivetrain_right_hall_;
Comran Morshed225f0b92016-02-10 20:34:27 +0000360
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700361 ::std::unique_ptr<::frc::Encoder> shooter_left_encoder_,
362 shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000363 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
364 shoulder_encoder_, wrist_encoder_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700365 ::std::unique_ptr<::frc::AnalogInput> ball_detector_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000366
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700367 ::std::array<::std::unique_ptr<::frc::DigitalInput>, 4> autonomous_modes_;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700368
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700369 ::frc::DigitalGlitchFilter hall_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000370};
371
372class SolenoidWriter {
373 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800374 SolenoidWriter(::aos::ShmEventLoop *event_loop,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700375 ::frc971::wpilib::BufferedPcm *pcm)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000376 : pcm_(pcm),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700377 pneumatics_to_log_sender_(
378 event_loop->MakeSender<::frc971::wpilib::PneumaticsToLog>("/aos")),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700379 drivetrain_(
380 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700381 ->MakeFetcher<::frc971::control_loops::drivetrain::Output>(
382 "/drivetrain")),
383 shooter_(event_loop->MakeFetcher<shooter::Output>("/shooter")),
384 superstructure_(
385 event_loop
386 ->MakeFetcher<::y2016::control_loops::superstructure::Output>(
387 "/superstructure")) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700388 event_loop->set_name("Solenoids");
389 event_loop->SetRuntimeRealtimePriority(27);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000390
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700391 event_loop->OnRun([this]() { compressor_->Start(); });
392
393 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
394 ::std::chrono::milliseconds(20),
395 ::std::chrono::milliseconds(1));
396 }
397
398 void set_compressor(::std::unique_ptr<::frc::Compressor> compressor) {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800399 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000400 }
401
Comran Morshed71466fe2016-04-21 20:21:14 -0700402 void set_drivetrain_shifter(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000403 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700404 drivetrain_shifter_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000405 }
406
Comran Morshed71466fe2016-04-21 20:21:14 -0700407 void set_climber_trigger(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000408 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700409 climber_trigger_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000410 }
411
Sabina Davis2243cab2019-02-05 21:45:08 -0800412 void set_traverse(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuh843412b2016-03-20 16:48:46 -0700413 traverse_ = ::std::move(s);
414 }
415
416 void set_traverse_latch(
417 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
418 traverse_latch_ = ::std::move(s);
419 }
420
Comran Morshedb79c4242016-02-06 18:27:26 +0000421 void set_shooter_clamp(
422 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
423 shooter_clamp_ = ::std::move(s);
424 }
425
426 void set_shooter_pusher(
427 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
428 shooter_pusher_ = ::std::move(s);
429 }
430
Sabina Davis2243cab2019-02-05 21:45:08 -0800431 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhe0729a62016-03-12 21:54:17 -0800432 lights_ = ::std::move(s);
433 }
434
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700435 void set_flashlight(::std::unique_ptr<::frc::Relay> relay) {
Austin Schuh8b89d332016-03-24 20:19:12 -0700436 flashlight_ = ::std::move(relay);
437 }
438
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700439 private:
440 void Loop(const int iterations) {
441 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700442 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700443 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000444
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700445 {
446 drivetrain_.Fetch();
447 if (drivetrain_.get()) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700448 drivetrain_shifter_->Set(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700449 !(drivetrain_->left_high() || drivetrain_->right_high()));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000450 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700451 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000452
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700453 {
454 shooter_.Fetch();
455 if (shooter_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700456 shooter_clamp_->Set(shooter_->clamp_open());
457 shooter_pusher_->Set(shooter_->push_to_shooter());
458 lights_->Set(shooter_->lights_on());
459 if (shooter_->forwards_flashlight()) {
460 if (shooter_->backwards_flashlight()) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700461 flashlight_->Set(::frc::Relay::kOn);
Austin Schuhb2c33382016-04-03 16:09:17 -0700462 } else {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700463 flashlight_->Set(::frc::Relay::kReverse);
464 }
465 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700466 if (shooter_->backwards_flashlight()) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700467 flashlight_->Set(::frc::Relay::kForward);
468 } else {
469 flashlight_->Set(::frc::Relay::kOff);
Austin Schuhb2c33382016-04-03 16:09:17 -0700470 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000471 }
472 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700473 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000474
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700475 {
476 superstructure_.Fetch();
477 if (superstructure_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700478 climber_trigger_->Set(superstructure_->unfold_climber());
Comran Morshed71466fe2016-04-21 20:21:14 -0700479
Alex Perrycb7da4b2019-08-28 19:35:56 -0700480 traverse_->Set(superstructure_->traverse_down());
481 traverse_latch_->Set(superstructure_->traverse_unlatched());
Austin Schuh843412b2016-03-20 16:48:46 -0700482 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700483 }
Austin Schuh843412b2016-03-20 16:48:46 -0700484
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700485 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700486 auto builder = pneumatics_to_log_sender_.MakeBuilder();
487
488 ::frc971::wpilib::PneumaticsToLog::Builder to_log_builder =
489 builder.MakeBuilder<frc971::wpilib::PneumaticsToLog>();
490
491 to_log_builder.add_compressor_on(compressor_->Enabled());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000492
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700493 pcm_->Flush();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700494 to_log_builder.add_read_solenoids(pcm_->GetAll());
495 builder.Send(to_log_builder.Finish());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000496 }
497 }
498
Alex Perrycb7da4b2019-08-28 19:35:56 -0700499 ::frc971::wpilib::BufferedPcm *pcm_;
500 aos::Sender<::frc971::wpilib::PneumaticsToLog> pneumatics_to_log_sender_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000501
Comran Morshed71466fe2016-04-21 20:21:14 -0700502 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_shifter_,
503 shooter_clamp_, shooter_pusher_, lights_, traverse_, traverse_latch_,
504 climber_trigger_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700505 ::std::unique_ptr<::frc::Relay> flashlight_;
506 ::std::unique_ptr<::frc::Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000507
Alex Perrycb7da4b2019-08-28 19:35:56 -0700508 ::aos::Fetcher<::frc971::control_loops::drivetrain::Output> drivetrain_;
509 ::aos::Fetcher<shooter::Output> shooter_;
510 ::aos::Fetcher<::y2016::control_loops::superstructure::Output>
Austin Schuh843412b2016-03-20 16:48:46 -0700511 superstructure_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000512};
513
Alex Perrycb7da4b2019-08-28 19:35:56 -0700514class ShooterWriter : public LoopOutputHandler<shooter::Output> {
Comran Morshed225f0b92016-02-10 20:34:27 +0000515 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800516 ShooterWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700517 : LoopOutputHandler<shooter::Output>(event_loop, "/shooter") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800518
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700519 void set_shooter_left_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000520 shooter_left_talon_ = ::std::move(t);
521 }
522
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700523 void set_shooter_right_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000524 shooter_right_talon_ = ::std::move(t);
525 }
526
527 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700528 void Write(const shooter::Output &output) override {
529 shooter_left_talon_->SetSpeed(output.voltage_left() / 12.0);
530 shooter_right_talon_->SetSpeed(-output.voltage_right() / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000531 }
532
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700533 void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700534 AOS_LOG(WARNING, "Shooter output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800535 shooter_left_talon_->SetDisabled();
536 shooter_right_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000537 }
538
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700539 ::std::unique_ptr<::frc::Talon> shooter_left_talon_, shooter_right_talon_;
Comran Morshed225f0b92016-02-10 20:34:27 +0000540};
541
Alex Perrycb7da4b2019-08-28 19:35:56 -0700542class SuperstructureWriter : public LoopOutputHandler<superstructure::Output> {
Comran Morshed225f0b92016-02-10 20:34:27 +0000543 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800544 SuperstructureWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700545 : LoopOutputHandler<superstructure::Output>(event_loop,
546 "/superstructure") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800547
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700548 void set_intake_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000549 intake_talon_ = ::std::move(t);
550 }
551
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700552 void set_shoulder_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000553 shoulder_talon_ = ::std::move(t);
554 }
555
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700556 void set_wrist_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000557 wrist_talon_ = ::std::move(t);
558 }
559
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700560 void set_top_rollers_talon(::std::unique_ptr<::frc::Talon> t) {
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800561 top_rollers_talon_ = ::std::move(t);
562 }
563
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700564 void set_bottom_rollers_talon(::std::unique_ptr<::frc::Talon> t) {
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800565 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000566 }
567
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700568 void set_climber_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700569 climber_talon_ = ::std::move(t);
570 }
571
Comran Morshed225f0b92016-02-10 20:34:27 +0000572 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700573 virtual void Write(const superstructure::Output &output) override {
574 intake_talon_->SetSpeed(::aos::Clip(output.voltage_intake(),
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800575 -kMaxBringupPower, kMaxBringupPower) /
576 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700577 shoulder_talon_->SetSpeed(::aos::Clip(-output.voltage_shoulder(),
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800578 -kMaxBringupPower, kMaxBringupPower) /
579 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700580 wrist_talon_->SetSpeed(::aos::Clip(output.voltage_wrist(),
581 -kMaxBringupPower, kMaxBringupPower) /
582 12.0);
583 top_rollers_talon_->SetSpeed(-output.voltage_top_rollers() / 12.0);
584 bottom_rollers_talon_->SetSpeed(-output.voltage_bottom_rollers() / 12.0);
585 climber_talon_->SetSpeed(-output.voltage_climber() / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000586 }
587
588 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700589 AOS_LOG(WARNING, "Superstructure output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800590 intake_talon_->SetDisabled();
591 shoulder_talon_->SetDisabled();
592 wrist_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000593 }
594
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700595 ::std::unique_ptr<::frc::Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Comran Morshed71466fe2016-04-21 20:21:14 -0700596 top_rollers_talon_, bottom_rollers_talon_, climber_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000597};
598
Comran Morshed9a9948c2016-01-16 15:58:04 +0000599class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
600 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700601 ::std::unique_ptr<::frc::Encoder> make_encoder(int index) {
602 return make_unique<::frc::Encoder>(10 + index * 2, 11 + index * 2, false,
603 ::frc::Encoder::k4X);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000604 }
605
606 void Run() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700607 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
608 aos::configuration::ReadConfig("config.json");
609
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700610 // Thread 1.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700611 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700612 ::frc971::wpilib::JoystickSender joystick_sender(
613 &joystick_sender_event_loop);
614 AddLoop(&joystick_sender_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000615
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700616 // Thread 2.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700617 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700618 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
619 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800620
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700621 // Thread 3.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700622 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700623 SensorReader sensor_reader(&sensor_reader_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000624
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700625 sensor_reader.set_drivetrain_left_encoder(make_encoder(5));
626 sensor_reader.set_drivetrain_right_encoder(make_encoder(6));
627 sensor_reader.set_drivetrain_left_hall(make_unique<::frc::AnalogInput>(5));
628 sensor_reader.set_drivetrain_right_hall(make_unique<::frc::AnalogInput>(6));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000629
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700630 sensor_reader.set_shooter_left_encoder(make_encoder(7));
631 sensor_reader.set_shooter_right_encoder(make_encoder(-3));
Comran Morshed225f0b92016-02-10 20:34:27 +0000632
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700633 sensor_reader.set_intake_encoder(make_encoder(0));
634 sensor_reader.set_intake_index(make_unique<::frc::DigitalInput>(0));
635 sensor_reader.set_intake_potentiometer(make_unique<::frc::AnalogInput>(0));
Comran Morshed225f0b92016-02-10 20:34:27 +0000636
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700637 sensor_reader.set_shoulder_encoder(make_encoder(4));
638 sensor_reader.set_shoulder_index(make_unique<::frc::DigitalInput>(2));
639 sensor_reader.set_shoulder_potentiometer(
640 make_unique<::frc::AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000641
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700642 sensor_reader.set_wrist_encoder(make_encoder(1));
643 sensor_reader.set_wrist_index(make_unique<::frc::DigitalInput>(1));
644 sensor_reader.set_wrist_potentiometer(make_unique<::frc::AnalogInput>(1));
Comran Morshed225f0b92016-02-10 20:34:27 +0000645
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700646 sensor_reader.set_ball_detector(make_unique<::frc::AnalogInput>(7));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000647
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700648 sensor_reader.set_autonomous_mode(0, make_unique<::frc::DigitalInput>(9));
649 sensor_reader.set_autonomous_mode(1, make_unique<::frc::DigitalInput>(8));
650 sensor_reader.set_autonomous_mode(2, make_unique<::frc::DigitalInput>(7));
651 sensor_reader.set_autonomous_mode(3, make_unique<::frc::DigitalInput>(6));
652 AddLoop(&sensor_reader_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000653
Brian Silverman003a4732018-03-11 14:02:15 -0700654 // TODO(Brian): This interacts poorly with the SpiRxClearer in ADIS16448.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700655 // Thread 4.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700656 ::aos::ShmEventLoop gyro_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700657 ::frc971::wpilib::GyroSender gyro_sender(&gyro_event_loop);
658 AddLoop(&gyro_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000659
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700660 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700661 ::aos::ShmEventLoop imu_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700662 auto imu_trigger = make_unique<::frc::DigitalInput>(3);
663 ::frc971::wpilib::ADIS16448 imu(&imu_event_loop, ::frc::SPI::Port::kMXP,
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800664 imu_trigger.get());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700665 AddLoop(&imu_event_loop);
Brian Silverman5f17a972016-02-28 01:49:32 -0500666
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700667 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700668 ::aos::ShmEventLoop output_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700669 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
Sabina Davisb71bc282019-02-03 01:17:23 -0800670 drivetrain_writer.set_left_controller0(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700671 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(5)), false);
Sabina Davisb71bc282019-02-03 01:17:23 -0800672 drivetrain_writer.set_right_controller0(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700673 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(4)), true);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000674
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700675 ShooterWriter shooter_writer(&output_event_loop);
Comran Morshed225f0b92016-02-10 20:34:27 +0000676 shooter_writer.set_shooter_left_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700677 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000678 shooter_writer.set_shooter_right_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700679 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000680
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700681 SuperstructureWriter superstructure_writer(&output_event_loop);
Comran Morshed225f0b92016-02-10 20:34:27 +0000682 superstructure_writer.set_intake_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700683 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000684 superstructure_writer.set_shoulder_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700685 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000686 superstructure_writer.set_wrist_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700687 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800688 superstructure_writer.set_top_rollers_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700689 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800690 superstructure_writer.set_bottom_rollers_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700691 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(0)));
Comran Morshed71466fe2016-04-21 20:21:14 -0700692 superstructure_writer.set_climber_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700693 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(7)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000694
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700695 AddLoop(&output_event_loop);
696
697 // Thread 6.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700698 ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000699 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
700 new ::frc971::wpilib::BufferedPcm());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700701 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm.get());
Comran Morshed71466fe2016-04-21 20:21:14 -0700702 solenoid_writer.set_drivetrain_shifter(pcm->MakeSolenoid(0));
Austin Schuh843412b2016-03-20 16:48:46 -0700703 solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2));
704 solenoid_writer.set_traverse(pcm->MakeSolenoid(3));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800705 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
706 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Austin Schuhe0729a62016-03-12 21:54:17 -0800707 solenoid_writer.set_lights(pcm->MakeSolenoid(6));
Comran Morshed71466fe2016-04-21 20:21:14 -0700708 solenoid_writer.set_climber_trigger(pcm->MakeSolenoid(1));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700709 solenoid_writer.set_flashlight(make_unique<::frc::Relay>(0));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000710
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700711 solenoid_writer.set_compressor(make_unique<::frc::Compressor>());
712 AddLoop(&solenoid_writer_event_loop);
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800713
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700714 RunLoops();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000715 }
716};
717
718} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000719} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000720
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000721AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);