blob: 5c2fcd6ab249f2a980d6f5a745a769888056aa9b [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/make_unique.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070026#include "aos/robot_state/robot_state_generated.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080027#include "aos/stl_mutex/stl_mutex.h"
John Park33858a32018-09-28 23:05:48 -070028#include "aos/time/time.h"
29#include "aos/util/log_interval.h"
John Park33858a32018-09-28 23:05:48 -070030#include "aos/util/wrapping_counter.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070031#include "frc971/autonomous/auto_generated.h"
Austin Schuhed5b26d2019-12-05 20:51:59 -080032#include "frc971/autonomous/auto_mode_generated.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070033#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
34#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080035#include "frc971/wpilib/ADIS16448.h"
36#include "frc971/wpilib/buffered_pcm.h"
37#include "frc971/wpilib/buffered_solenoid.h"
38#include "frc971/wpilib/dma.h"
39#include "frc971/wpilib/dma_edge_counting.h"
Sabina Davisb71bc282019-02-03 01:17:23 -080040#include "frc971/wpilib/drivetrain_writer.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080041#include "frc971/wpilib/encoder_and_potentiometer.h"
42#include "frc971/wpilib/gyro_sender.h"
43#include "frc971/wpilib/interrupt_edge_counting.h"
44#include "frc971/wpilib/joystick_sender.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070045#include "frc971/wpilib/logging_generated.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080046#include "frc971/wpilib/loop_output_handler.h"
47#include "frc971/wpilib/pdp_fetcher.h"
48#include "frc971/wpilib/sensor_reader.h"
Comran Morshed6c6a0a92016-01-17 12:45:16 +000049#include "y2016/constants.h"
Comran Morshed5bb12112016-02-16 13:48:57 +000050#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070051#include "y2016/control_loops/shooter/shooter_output_generated.h"
52#include "y2016/control_loops/shooter/shooter_position_generated.h"
53#include "y2016/control_loops/superstructure/superstructure_output_generated.h"
54#include "y2016/control_loops/superstructure/superstructure_position_generated.h"
55#include "y2016/queues/ball_detector_generated.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000056
Brian Silvermanf819b442019-01-20 16:51:04 -080057using aos::make_unique;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070058using ::frc971::wpilib::LoopOutputHandler;
Alex Perrycb7da4b2019-08-28 19:35:56 -070059namespace shooter = ::y2016::control_loops::shooter;
60namespace superstructure = ::y2016::control_loops::superstructure;
Comran Morshed9a9948c2016-01-16 15:58:04 +000061
Comran Morshed6c6a0a92016-01-17 12:45:16 +000062namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000063namespace wpilib {
Austin Schuha9992ff2016-02-28 21:59:23 -080064namespace {
65constexpr double kMaxBringupPower = 12.0;
66} // namespace
Comran Morshed9a9948c2016-01-16 15:58:04 +000067
68// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
69// DMA stuff and then removing the * 2.0 in *_translate.
70// The low bit is direction.
71
Comran Morshed225f0b92016-02-10 20:34:27 +000072// Translates for the sensor values to convert raw index pulses into something
73// with proper units.
74
75// TODO(comran): Template these methods since there is a lot of repetition here.
76double hall_translate(double in) {
77 // Turn voltage from our 3-state halls into a ratio that the loop can use.
78 return in / 5.0;
79}
80
Comran Morshed9a9948c2016-01-16 15:58:04 +000081double drivetrain_translate(int32_t in) {
Comran Morshed6c6a0a92016-01-17 12:45:16 +000082 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000083 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080084 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000085}
86
87double drivetrain_velocity_translate(double in) {
88 return (1.0 / in) / 256.0 /*cpr*/ *
Comran Morshed225f0b92016-02-10 20:34:27 +000089 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080090 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000091}
92
Comran Morshed225f0b92016-02-10 20:34:27 +000093double shooter_translate(int32_t in) {
Comran Morshed5bb12112016-02-16 13:48:57 +000094 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000095 constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
96}
Comran Morshed9a9948c2016-01-16 15:58:04 +000097
Comran Morshed225f0b92016-02-10 20:34:27 +000098double intake_translate(int32_t in) {
Austin Schuh9f77fd22016-02-21 02:53:58 -080099 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000100 constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
101}
102
103double shoulder_translate(int32_t in) {
104 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
105 constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/);
106}
107
108double wrist_translate(int32_t in) {
109 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
110 constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/);
111}
112
113double intake_pot_translate(double voltage) {
114 return voltage * constants::Values::kIntakePotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000115 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000116}
117
118double shoulder_pot_translate(double voltage) {
119 return voltage * constants::Values::kShoulderPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000120 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000121}
122
123double wrist_pot_translate(double voltage) {
124 return voltage * constants::Values::kWristPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000125 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000126}
127
Comran Morshed1b764322016-02-14 20:18:12 +0000128constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
129 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
130 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
131 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
132
133constexpr double kMaxShooterEncoderPulsesPerSecond =
134 18700.0 /* 775pro free speed RPM */ * 12.0 /
135 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
136 128.0 /* CPR */ * 4 /* edges per cycle */;
137
138double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max(
139 kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond);
140
141constexpr double kMaxSuperstructureEncoderPulsesPerSecond =
142 18700.0 /* 775pro free speed RPM */ * 12.0 /
143 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
144 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000145
Comran Morshed225f0b92016-02-10 20:34:27 +0000146// Class to send position messages with sensor readings to our loops.
Austin Schuh54667ac2019-02-02 16:44:49 -0800147class SensorReader : public ::frc971::wpilib::SensorReader {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000148 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800149 SensorReader(::aos::ShmEventLoop *event_loop)
Austin Schuh4b652c92019-05-27 13:22:27 -0700150 : ::frc971::wpilib::SensorReader(event_loop),
151 ball_detector_sender_(
152 event_loop->MakeSender<::y2016::sensors::BallDetector>(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700153 "/superstructure")),
Austin Schuha250b2d2019-05-27 16:14:02 -0700154 auto_mode_sender_(
155 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
Tyler Chatow24b5db12020-01-06 21:16:56 -0800156 "/autonomous")),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700157 shooter_position_sender_(
158 event_loop->MakeSender<shooter::Position>("/shooter")),
Austin Schuh9481d0d2019-06-29 21:56:17 -0700159 superstructure_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700160 event_loop->MakeSender<superstructure::Position>(
161 "/superstructure")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700162 drivetrain_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700163 event_loop
164 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
165 "/drivetrain")) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000166 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
167 // we should ever see.
Austin Schuh54667ac2019-02-02 16:44:49 -0800168 UpdateFastEncoderFilterHz(kMaxDrivetrainShooterEncoderPulsesPerSecond);
169 UpdateMediumEncoderFilterHz(kMaxSuperstructureEncoderPulsesPerSecond);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000170 hall_filter_.SetPeriodNanoSeconds(100000);
171 }
172
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700173 void set_drivetrain_left_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000174 drivetrain_left_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000175 }
176
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700177 void set_drivetrain_right_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000178 drivetrain_right_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000179 }
180
Comran Morshed225f0b92016-02-10 20:34:27 +0000181 // Shooter setters.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700182 void set_shooter_left_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800183 fast_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000184 shooter_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000185 }
186
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700187 void set_shooter_right_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800188 fast_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000189 shooter_right_encoder_ = ::std::move(encoder);
190 }
191
192 // Intake setters.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700193 void set_intake_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800194 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000195 intake_encoder_.set_encoder(::std::move(encoder));
196 }
197
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700198 void set_intake_potentiometer(
199 ::std::unique_ptr<::frc::AnalogInput> potentiometer) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000200 intake_encoder_.set_potentiometer(::std::move(potentiometer));
201 }
202
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700203 void set_intake_index(::std::unique_ptr<::frc::DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800204 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000205 intake_encoder_.set_index(::std::move(index));
206 }
207
208 // Shoulder setters.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700209 void set_shoulder_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800210 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000211 shoulder_encoder_.set_encoder(::std::move(encoder));
212 }
213
214 void set_shoulder_potentiometer(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700215 ::std::unique_ptr<::frc::AnalogInput> potentiometer) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000216 shoulder_encoder_.set_potentiometer(::std::move(potentiometer));
217 }
218
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700219 void set_shoulder_index(::std::unique_ptr<::frc::DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800220 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000221 shoulder_encoder_.set_index(::std::move(index));
222 }
223
224 // Wrist setters.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700225 void set_wrist_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800226 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000227 wrist_encoder_.set_encoder(::std::move(encoder));
228 }
229
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700230 void set_wrist_potentiometer(
231 ::std::unique_ptr<::frc::AnalogInput> potentiometer) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000232 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
233 }
234
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700235 void set_wrist_index(::std::unique_ptr<::frc::DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800236 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000237 wrist_encoder_.set_index(::std::move(index));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000238 }
239
Comran Morshedaa0573c2016-03-05 19:05:54 +0000240 // Ball detector setter.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700241 void set_ball_detector(::std::unique_ptr<::frc::AnalogInput> analog) {
Comran Morshedaa0573c2016-03-05 19:05:54 +0000242 ball_detector_ = ::std::move(analog);
243 }
244
Brian Silverman68cb5c22016-03-20 18:11:14 -0700245 // Autonomous mode switch setter.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700246 void set_autonomous_mode(int i,
247 ::std::unique_ptr<::frc::DigitalInput> sensor) {
Brian Silverman68cb5c22016-03-20 18:11:14 -0700248 autonomous_modes_.at(i) = ::std::move(sensor);
249 }
250
Comran Morshed9a9948c2016-01-16 15:58:04 +0000251 // All of the DMA-related set_* calls must be made before this, and it doesn't
252 // hurt to do all of them.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000253
Austin Schuh54667ac2019-02-02 16:44:49 -0800254 void Start() {
255 AddToDMA(&intake_encoder_);
256 AddToDMA(&shoulder_encoder_);
257 AddToDMA(&wrist_encoder_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000258 }
259
260 void RunIteration() {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000261 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700262 auto builder = drivetrain_position_sender_.MakeBuilder();
263 frc971::control_loops::drivetrain::Position::Builder position_builder =
264 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000265
Alex Perrycb7da4b2019-08-28 19:35:56 -0700266 position_builder.add_right_encoder(
267 drivetrain_translate(-drivetrain_right_encoder_->GetRaw()));
268 position_builder.add_left_encoder(
269 -drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
270 position_builder.add_left_speed(
271 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
272 position_builder.add_right_speed(drivetrain_velocity_translate(
273 drivetrain_right_encoder_->GetPeriod()));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000274
Alex Perrycb7da4b2019-08-28 19:35:56 -0700275 position_builder.add_left_shifter_position(
276 hall_translate(drivetrain_left_hall_->GetVoltage()));
277 position_builder.add_right_shifter_position(
278 hall_translate(drivetrain_right_hall_->GetVoltage()));
279
280 builder.Send(position_builder.Finish());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000281 }
Austin Schuh54667ac2019-02-02 16:44:49 -0800282 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000283
Austin Schuh54667ac2019-02-02 16:44:49 -0800284 void RunDmaIteration() {
285 const auto &values = constants::GetValues();
Comran Morshed225f0b92016-02-10 20:34:27 +0000286 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700287 auto builder = shooter_position_sender_.MakeBuilder();
288 shooter::Position::Builder shooter_builder =
289 builder.MakeBuilder<shooter::Position>();
290 shooter_builder.add_theta_left(
291 shooter_translate(-shooter_left_encoder_->GetRaw()));
292 shooter_builder.add_theta_right(
293 shooter_translate(shooter_right_encoder_->GetRaw()));
294 builder.Send(shooter_builder.Finish());
Comran Morshed225f0b92016-02-10 20:34:27 +0000295 }
296
297 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700298 auto builder = superstructure_position_sender_.MakeBuilder();
Comran Morshed225f0b92016-02-10 20:34:27 +0000299
Alex Perrycb7da4b2019-08-28 19:35:56 -0700300 frc971::PotAndIndexPositionT intake;
301 CopyPosition(intake_encoder_, &intake, intake_translate,
302 intake_pot_translate, false, values.intake.pot_offset);
303 flatbuffers::Offset<frc971::PotAndIndexPosition> intake_offset =
304 frc971::PotAndIndexPosition::Pack(*builder.fbb(), &intake);
305
306 frc971::PotAndIndexPositionT shoulder;
307 CopyPosition(shoulder_encoder_, &shoulder, shoulder_translate,
308 shoulder_pot_translate, false, values.shoulder.pot_offset);
309 flatbuffers::Offset<frc971::PotAndIndexPosition> shoulder_offset =
310 frc971::PotAndIndexPosition::Pack(*builder.fbb(), &shoulder);
311
312 frc971::PotAndIndexPositionT wrist;
313 CopyPosition(wrist_encoder_, &wrist, wrist_translate, wrist_pot_translate,
314 true, values.wrist.pot_offset);
315 flatbuffers::Offset<frc971::PotAndIndexPosition> wrist_offset =
316 frc971::PotAndIndexPosition::Pack(*builder.fbb(), &wrist);
317
318 superstructure::Position::Builder position_builder =
319 builder.MakeBuilder<superstructure::Position>();
320
321 position_builder.add_intake(intake_offset);
322 position_builder.add_shoulder(shoulder_offset);
323 position_builder.add_wrist(wrist_offset);
324
325 builder.Send(position_builder.Finish());
Comran Morshed225f0b92016-02-10 20:34:27 +0000326 }
Comran Morshedaa0573c2016-03-05 19:05:54 +0000327
328 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700329 auto builder = ball_detector_sender_.MakeBuilder();
330 ::y2016::sensors::BallDetector::Builder ball_detector_builder =
331 builder.MakeBuilder<y2016::sensors::BallDetector>();
332 ball_detector_builder.add_voltage(ball_detector_->GetVoltage());
333 builder.Send(ball_detector_builder.Finish());
Comran Morshedaa0573c2016-03-05 19:05:54 +0000334 }
Brian Silverman68cb5c22016-03-20 18:11:14 -0700335
336 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700337 auto builder = auto_mode_sender_.MakeBuilder();
338 ::frc971::autonomous::AutonomousMode::Builder auto_builder =
339 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
340 int mode = 0;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700341 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
342 if (autonomous_modes_[i]->Get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700343 mode |= 1 << i;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700344 }
345 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700346 auto_builder.add_mode(mode);
347 builder.Send(auto_builder.Finish());
Brian Silverman68cb5c22016-03-20 18:11:14 -0700348 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000349 }
350
Comran Morshed9a9948c2016-01-16 15:58:04 +0000351 private:
Austin Schuh4b652c92019-05-27 13:22:27 -0700352 ::aos::Sender<::y2016::sensors::BallDetector> ball_detector_sender_;
Austin Schuha250b2d2019-05-27 16:14:02 -0700353 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700354 ::aos::Sender<shooter::Position> shooter_position_sender_;
355 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
356 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700357 drivetrain_position_sender_;
Austin Schuh4b652c92019-05-27 13:22:27 -0700358
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700359 ::std::unique_ptr<::frc::AnalogInput> drivetrain_left_hall_,
360 drivetrain_right_hall_;
Comran Morshed225f0b92016-02-10 20:34:27 +0000361
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700362 ::std::unique_ptr<::frc::Encoder> shooter_left_encoder_,
363 shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000364 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
365 shoulder_encoder_, wrist_encoder_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700366 ::std::unique_ptr<::frc::AnalogInput> ball_detector_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000367
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700368 ::std::array<::std::unique_ptr<::frc::DigitalInput>, 4> autonomous_modes_;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700369
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700370 ::frc::DigitalGlitchFilter hall_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000371};
372
373class SolenoidWriter {
374 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800375 SolenoidWriter(::aos::ShmEventLoop *event_loop,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700376 ::frc971::wpilib::BufferedPcm *pcm)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000377 : pcm_(pcm),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700378 pneumatics_to_log_sender_(
379 event_loop->MakeSender<::frc971::wpilib::PneumaticsToLog>("/aos")),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700380 drivetrain_(
381 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700382 ->MakeFetcher<::frc971::control_loops::drivetrain::Output>(
383 "/drivetrain")),
384 shooter_(event_loop->MakeFetcher<shooter::Output>("/shooter")),
385 superstructure_(
386 event_loop
387 ->MakeFetcher<::y2016::control_loops::superstructure::Output>(
388 "/superstructure")) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700389 event_loop->set_name("Solenoids");
390 event_loop->SetRuntimeRealtimePriority(27);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000391
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700392 event_loop->OnRun([this]() { compressor_->Start(); });
393
394 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
395 ::std::chrono::milliseconds(20),
396 ::std::chrono::milliseconds(1));
397 }
398
399 void set_compressor(::std::unique_ptr<::frc::Compressor> compressor) {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800400 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000401 }
402
Comran Morshed71466fe2016-04-21 20:21:14 -0700403 void set_drivetrain_shifter(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000404 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700405 drivetrain_shifter_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000406 }
407
Comran Morshed71466fe2016-04-21 20:21:14 -0700408 void set_climber_trigger(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000409 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700410 climber_trigger_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000411 }
412
Sabina Davis2243cab2019-02-05 21:45:08 -0800413 void set_traverse(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuh843412b2016-03-20 16:48:46 -0700414 traverse_ = ::std::move(s);
415 }
416
417 void set_traverse_latch(
418 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
419 traverse_latch_ = ::std::move(s);
420 }
421
Comran Morshedb79c4242016-02-06 18:27:26 +0000422 void set_shooter_clamp(
423 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
424 shooter_clamp_ = ::std::move(s);
425 }
426
427 void set_shooter_pusher(
428 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
429 shooter_pusher_ = ::std::move(s);
430 }
431
Sabina Davis2243cab2019-02-05 21:45:08 -0800432 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhe0729a62016-03-12 21:54:17 -0800433 lights_ = ::std::move(s);
434 }
435
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700436 void set_flashlight(::std::unique_ptr<::frc::Relay> relay) {
Austin Schuh8b89d332016-03-24 20:19:12 -0700437 flashlight_ = ::std::move(relay);
438 }
439
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700440 private:
441 void Loop(const int iterations) {
442 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700443 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700444 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000445
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700446 {
447 drivetrain_.Fetch();
448 if (drivetrain_.get()) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700449 drivetrain_shifter_->Set(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700450 !(drivetrain_->left_high() || drivetrain_->right_high()));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000451 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700452 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000453
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700454 {
455 shooter_.Fetch();
456 if (shooter_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700457 shooter_clamp_->Set(shooter_->clamp_open());
458 shooter_pusher_->Set(shooter_->push_to_shooter());
459 lights_->Set(shooter_->lights_on());
460 if (shooter_->forwards_flashlight()) {
461 if (shooter_->backwards_flashlight()) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700462 flashlight_->Set(::frc::Relay::kOn);
Austin Schuhb2c33382016-04-03 16:09:17 -0700463 } else {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700464 flashlight_->Set(::frc::Relay::kReverse);
465 }
466 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700467 if (shooter_->backwards_flashlight()) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700468 flashlight_->Set(::frc::Relay::kForward);
469 } else {
470 flashlight_->Set(::frc::Relay::kOff);
Austin Schuhb2c33382016-04-03 16:09:17 -0700471 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000472 }
473 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700474 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000475
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700476 {
477 superstructure_.Fetch();
478 if (superstructure_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700479 climber_trigger_->Set(superstructure_->unfold_climber());
Comran Morshed71466fe2016-04-21 20:21:14 -0700480
Alex Perrycb7da4b2019-08-28 19:35:56 -0700481 traverse_->Set(superstructure_->traverse_down());
482 traverse_latch_->Set(superstructure_->traverse_unlatched());
Austin Schuh843412b2016-03-20 16:48:46 -0700483 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700484 }
Austin Schuh843412b2016-03-20 16:48:46 -0700485
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700486 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700487 auto builder = pneumatics_to_log_sender_.MakeBuilder();
488
489 ::frc971::wpilib::PneumaticsToLog::Builder to_log_builder =
490 builder.MakeBuilder<frc971::wpilib::PneumaticsToLog>();
491
492 to_log_builder.add_compressor_on(compressor_->Enabled());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000493
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700494 pcm_->Flush();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700495 to_log_builder.add_read_solenoids(pcm_->GetAll());
496 builder.Send(to_log_builder.Finish());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000497 }
498 }
499
Alex Perrycb7da4b2019-08-28 19:35:56 -0700500 ::frc971::wpilib::BufferedPcm *pcm_;
501 aos::Sender<::frc971::wpilib::PneumaticsToLog> pneumatics_to_log_sender_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000502
Comran Morshed71466fe2016-04-21 20:21:14 -0700503 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_shifter_,
504 shooter_clamp_, shooter_pusher_, lights_, traverse_, traverse_latch_,
505 climber_trigger_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700506 ::std::unique_ptr<::frc::Relay> flashlight_;
507 ::std::unique_ptr<::frc::Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000508
Alex Perrycb7da4b2019-08-28 19:35:56 -0700509 ::aos::Fetcher<::frc971::control_loops::drivetrain::Output> drivetrain_;
510 ::aos::Fetcher<shooter::Output> shooter_;
511 ::aos::Fetcher<::y2016::control_loops::superstructure::Output>
Austin Schuh843412b2016-03-20 16:48:46 -0700512 superstructure_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000513};
514
Alex Perrycb7da4b2019-08-28 19:35:56 -0700515class ShooterWriter : public LoopOutputHandler<shooter::Output> {
Comran Morshed225f0b92016-02-10 20:34:27 +0000516 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800517 ShooterWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700518 : LoopOutputHandler<shooter::Output>(event_loop, "/shooter") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800519
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700520 void set_shooter_left_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000521 shooter_left_talon_ = ::std::move(t);
522 }
523
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700524 void set_shooter_right_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000525 shooter_right_talon_ = ::std::move(t);
526 }
527
528 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700529 void Write(const shooter::Output &output) override {
530 shooter_left_talon_->SetSpeed(output.voltage_left() / 12.0);
531 shooter_right_talon_->SetSpeed(-output.voltage_right() / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000532 }
533
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700534 void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700535 AOS_LOG(WARNING, "Shooter output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800536 shooter_left_talon_->SetDisabled();
537 shooter_right_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000538 }
539
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700540 ::std::unique_ptr<::frc::Talon> shooter_left_talon_, shooter_right_talon_;
Comran Morshed225f0b92016-02-10 20:34:27 +0000541};
542
Alex Perrycb7da4b2019-08-28 19:35:56 -0700543class SuperstructureWriter : public LoopOutputHandler<superstructure::Output> {
Comran Morshed225f0b92016-02-10 20:34:27 +0000544 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800545 SuperstructureWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700546 : LoopOutputHandler<superstructure::Output>(event_loop,
547 "/superstructure") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800548
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700549 void set_intake_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000550 intake_talon_ = ::std::move(t);
551 }
552
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700553 void set_shoulder_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000554 shoulder_talon_ = ::std::move(t);
555 }
556
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700557 void set_wrist_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000558 wrist_talon_ = ::std::move(t);
559 }
560
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700561 void set_top_rollers_talon(::std::unique_ptr<::frc::Talon> t) {
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800562 top_rollers_talon_ = ::std::move(t);
563 }
564
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700565 void set_bottom_rollers_talon(::std::unique_ptr<::frc::Talon> t) {
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800566 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000567 }
568
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700569 void set_climber_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700570 climber_talon_ = ::std::move(t);
571 }
572
Comran Morshed225f0b92016-02-10 20:34:27 +0000573 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700574 virtual void Write(const superstructure::Output &output) override {
575 intake_talon_->SetSpeed(::aos::Clip(output.voltage_intake(),
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800576 -kMaxBringupPower, kMaxBringupPower) /
577 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700578 shoulder_talon_->SetSpeed(::aos::Clip(-output.voltage_shoulder(),
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800579 -kMaxBringupPower, kMaxBringupPower) /
580 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700581 wrist_talon_->SetSpeed(::aos::Clip(output.voltage_wrist(),
582 -kMaxBringupPower, kMaxBringupPower) /
583 12.0);
584 top_rollers_talon_->SetSpeed(-output.voltage_top_rollers() / 12.0);
585 bottom_rollers_talon_->SetSpeed(-output.voltage_bottom_rollers() / 12.0);
586 climber_talon_->SetSpeed(-output.voltage_climber() / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000587 }
588
589 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700590 AOS_LOG(WARNING, "Superstructure output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800591 intake_talon_->SetDisabled();
592 shoulder_talon_->SetDisabled();
593 wrist_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000594 }
595
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700596 ::std::unique_ptr<::frc::Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Comran Morshed71466fe2016-04-21 20:21:14 -0700597 top_rollers_talon_, bottom_rollers_talon_, climber_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000598};
599
Comran Morshed9a9948c2016-01-16 15:58:04 +0000600class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
601 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700602 ::std::unique_ptr<::frc::Encoder> make_encoder(int index) {
603 return make_unique<::frc::Encoder>(10 + index * 2, 11 + index * 2, false,
604 ::frc::Encoder::k4X);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000605 }
606
607 void Run() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700608 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
609 aos::configuration::ReadConfig("config.json");
610
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700611 // Thread 1.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700612 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700613 ::frc971::wpilib::JoystickSender joystick_sender(
614 &joystick_sender_event_loop);
615 AddLoop(&joystick_sender_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000616
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700617 // Thread 2.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700618 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700619 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
620 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800621
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700622 // Thread 3.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700623 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700624 SensorReader sensor_reader(&sensor_reader_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000625
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700626 sensor_reader.set_drivetrain_left_encoder(make_encoder(5));
627 sensor_reader.set_drivetrain_right_encoder(make_encoder(6));
628 sensor_reader.set_drivetrain_left_hall(make_unique<::frc::AnalogInput>(5));
629 sensor_reader.set_drivetrain_right_hall(make_unique<::frc::AnalogInput>(6));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000630
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700631 sensor_reader.set_shooter_left_encoder(make_encoder(7));
632 sensor_reader.set_shooter_right_encoder(make_encoder(-3));
Comran Morshed225f0b92016-02-10 20:34:27 +0000633
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700634 sensor_reader.set_intake_encoder(make_encoder(0));
635 sensor_reader.set_intake_index(make_unique<::frc::DigitalInput>(0));
636 sensor_reader.set_intake_potentiometer(make_unique<::frc::AnalogInput>(0));
Comran Morshed225f0b92016-02-10 20:34:27 +0000637
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700638 sensor_reader.set_shoulder_encoder(make_encoder(4));
639 sensor_reader.set_shoulder_index(make_unique<::frc::DigitalInput>(2));
640 sensor_reader.set_shoulder_potentiometer(
641 make_unique<::frc::AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000642
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700643 sensor_reader.set_wrist_encoder(make_encoder(1));
644 sensor_reader.set_wrist_index(make_unique<::frc::DigitalInput>(1));
645 sensor_reader.set_wrist_potentiometer(make_unique<::frc::AnalogInput>(1));
Comran Morshed225f0b92016-02-10 20:34:27 +0000646
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700647 sensor_reader.set_ball_detector(make_unique<::frc::AnalogInput>(7));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000648
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700649 sensor_reader.set_autonomous_mode(0, make_unique<::frc::DigitalInput>(9));
650 sensor_reader.set_autonomous_mode(1, make_unique<::frc::DigitalInput>(8));
651 sensor_reader.set_autonomous_mode(2, make_unique<::frc::DigitalInput>(7));
652 sensor_reader.set_autonomous_mode(3, make_unique<::frc::DigitalInput>(6));
653 AddLoop(&sensor_reader_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000654
Brian Silverman003a4732018-03-11 14:02:15 -0700655 // TODO(Brian): This interacts poorly with the SpiRxClearer in ADIS16448.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700656 // Thread 4.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700657 ::aos::ShmEventLoop gyro_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700658 ::frc971::wpilib::GyroSender gyro_sender(&gyro_event_loop);
659 AddLoop(&gyro_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000660
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700661 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700662 ::aos::ShmEventLoop imu_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700663 auto imu_trigger = make_unique<::frc::DigitalInput>(3);
664 ::frc971::wpilib::ADIS16448 imu(&imu_event_loop, ::frc::SPI::Port::kMXP,
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800665 imu_trigger.get());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700666 AddLoop(&imu_event_loop);
Brian Silverman5f17a972016-02-28 01:49:32 -0500667
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700668 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700669 ::aos::ShmEventLoop output_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700670 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
Sabina Davisb71bc282019-02-03 01:17:23 -0800671 drivetrain_writer.set_left_controller0(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700672 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(5)), false);
Sabina Davisb71bc282019-02-03 01:17:23 -0800673 drivetrain_writer.set_right_controller0(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700674 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(4)), true);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000675
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700676 ShooterWriter shooter_writer(&output_event_loop);
Comran Morshed225f0b92016-02-10 20:34:27 +0000677 shooter_writer.set_shooter_left_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700678 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000679 shooter_writer.set_shooter_right_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700680 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000681
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700682 SuperstructureWriter superstructure_writer(&output_event_loop);
Comran Morshed225f0b92016-02-10 20:34:27 +0000683 superstructure_writer.set_intake_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700684 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000685 superstructure_writer.set_shoulder_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700686 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000687 superstructure_writer.set_wrist_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700688 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800689 superstructure_writer.set_top_rollers_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700690 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800691 superstructure_writer.set_bottom_rollers_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700692 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(0)));
Comran Morshed71466fe2016-04-21 20:21:14 -0700693 superstructure_writer.set_climber_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700694 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(7)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000695
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700696 AddLoop(&output_event_loop);
697
698 // Thread 6.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700699 ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000700 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
701 new ::frc971::wpilib::BufferedPcm());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700702 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm.get());
Comran Morshed71466fe2016-04-21 20:21:14 -0700703 solenoid_writer.set_drivetrain_shifter(pcm->MakeSolenoid(0));
Austin Schuh843412b2016-03-20 16:48:46 -0700704 solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2));
705 solenoid_writer.set_traverse(pcm->MakeSolenoid(3));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800706 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
707 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Austin Schuhe0729a62016-03-12 21:54:17 -0800708 solenoid_writer.set_lights(pcm->MakeSolenoid(6));
Comran Morshed71466fe2016-04-21 20:21:14 -0700709 solenoid_writer.set_climber_trigger(pcm->MakeSolenoid(1));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700710 solenoid_writer.set_flashlight(make_unique<::frc::Relay>(0));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000711
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700712 solenoid_writer.set_compressor(make_unique<::frc::Compressor>());
713 AddLoop(&solenoid_writer_event_loop);
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800714
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700715 RunLoops();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000716 }
717};
718
719} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000720} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000721
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000722AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);