blob: 9b31a0f7f997015db7642d1d7dbc0c64f47d6015 [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"
Austin Schuhdf6cbb12019-02-02 13:46:52 -080023#include "aos/events/shm-event-loop.h"
John Park33858a32018-09-28 23:05:48 -070024#include "aos/logging/logging.h"
25#include "aos/logging/queue_logging.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080026#include "aos/make_unique.h"
27#include "aos/robot_state/robot_state.q.h"
28#include "aos/stl_mutex/stl_mutex.h"
John Park33858a32018-09-28 23:05:48 -070029#include "aos/time/time.h"
30#include "aos/util/log_interval.h"
John Park33858a32018-09-28 23:05:48 -070031#include "aos/util/wrapping_counter.h"
Philipp Schrader4bd29b12017-02-22 04:42:27 +000032#include "frc971/autonomous/auto.q.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000033#include "frc971/control_loops/control_loops.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000034#include "frc971/control_loops/drivetrain/drivetrain.q.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"
45#include "frc971/wpilib/logging.q.h"
46#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"
Comran Morshed225f0b92016-02-10 20:34:27 +000051#include "y2016/control_loops/shooter/shooter.q.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080052#include "y2016/control_loops/shooter/shooter.q.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000053#include "y2016/control_loops/superstructure/superstructure.q.h"
Comran Morshedaa0573c2016-03-05 19:05:54 +000054#include "y2016/queues/ball_detector.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000055
Brian Silvermanf819b442019-01-20 16:51:04 -080056using aos::make_unique;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070057using ::frc971::wpilib::LoopOutputHandler;
58using ::y2016::control_loops::shooter::ShooterQueue;
Austin Schuh9481d0d2019-06-29 21:56:17 -070059using ::y2016::control_loops::SuperstructureQueue;
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 Schuhdf6cbb12019-02-02 13:46:52 -0800148 SensorReader(::aos::EventLoop *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>(
Austin Schuha250b2d2019-05-27 16:14:02 -0700152 ".y2016.sensors.ball_detector")),
153 auto_mode_sender_(
154 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
Austin Schuhae023fb2019-06-29 17:11:45 -0700155 ".frc971.autonomous.auto_mode")),
156 shooter_position_sender_(event_loop->MakeSender<ShooterQueue::Position>(
Austin Schuh9481d0d2019-06-29 21:56:17 -0700157 ".y2016.control_loops.shooter.shooter_queue.position")),
158 superstructure_position_sender_(
159 event_loop->MakeSender<SuperstructureQueue::Position>(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700160 ".y2016.control_loops.superstructure_queue.position")),
161 drivetrain_position_sender_(
162 event_loop->MakeSender<
163 ::frc971::control_loops::DrivetrainQueue::Position>(
164 ".frc971.control_loops.drivetrain_queue.position")) {
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 {
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700261 auto drivetrain_message = drivetrain_position_sender_.MakeMessage();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000262 drivetrain_message->right_encoder =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800263 drivetrain_translate(-drivetrain_right_encoder_->GetRaw());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000264 drivetrain_message->left_encoder =
265 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
266 drivetrain_message->left_speed =
267 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
268 drivetrain_message->right_speed =
269 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
270
Comran Morshed9a9948c2016-01-16 15:58:04 +0000271 drivetrain_message->left_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000272 hall_translate(drivetrain_left_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000273 drivetrain_message->right_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000274 hall_translate(drivetrain_right_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000275
276 drivetrain_message.Send();
277 }
Austin Schuh54667ac2019-02-02 16:44:49 -0800278 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000279
Austin Schuh54667ac2019-02-02 16:44:49 -0800280 void RunDmaIteration() {
281 const auto &values = constants::GetValues();
Comran Morshed225f0b92016-02-10 20:34:27 +0000282 {
Austin Schuhae023fb2019-06-29 17:11:45 -0700283 auto shooter_message = shooter_position_sender_.MakeMessage();
Comran Morshed225f0b92016-02-10 20:34:27 +0000284 shooter_message->theta_left =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800285 shooter_translate(-shooter_left_encoder_->GetRaw());
Comran Morshed225f0b92016-02-10 20:34:27 +0000286 shooter_message->theta_right =
287 shooter_translate(shooter_right_encoder_->GetRaw());
288 shooter_message.Send();
289 }
290
291 {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700292 auto superstructure_message =
293 superstructure_position_sender_.MakeMessage();
Sabina Davis2243cab2019-02-05 21:45:08 -0800294 CopyPosition(intake_encoder_, &superstructure_message->intake,
295 intake_translate, intake_pot_translate, false,
296 values.intake.pot_offset);
297 CopyPosition(shoulder_encoder_, &superstructure_message->shoulder,
298 shoulder_translate, shoulder_pot_translate, false,
299 values.shoulder.pot_offset);
300 CopyPosition(wrist_encoder_, &superstructure_message->wrist,
301 wrist_translate, wrist_pot_translate, true,
302 values.wrist.pot_offset);
Comran Morshed225f0b92016-02-10 20:34:27 +0000303
304 superstructure_message.Send();
305 }
Comran Morshedaa0573c2016-03-05 19:05:54 +0000306
307 {
Austin Schuh4b652c92019-05-27 13:22:27 -0700308 auto ball_detector_message = ball_detector_sender_.MakeMessage();
Comran Morshedaa0573c2016-03-05 19:05:54 +0000309 ball_detector_message->voltage = ball_detector_->GetVoltage();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700310 AOS_LOG_STRUCT(DEBUG, "ball detector", *ball_detector_message);
Comran Morshedaa0573c2016-03-05 19:05:54 +0000311 ball_detector_message.Send();
312 }
Brian Silverman68cb5c22016-03-20 18:11:14 -0700313
314 {
Austin Schuha250b2d2019-05-27 16:14:02 -0700315 auto auto_mode_message = auto_mode_sender_.MakeMessage();
Brian Silverman68cb5c22016-03-20 18:11:14 -0700316 auto_mode_message->mode = 0;
317 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
318 if (autonomous_modes_[i]->Get()) {
319 auto_mode_message->mode |= 1 << i;
320 }
321 }
Austin Schuhf257f3c2019-10-27 21:00:43 -0700322 AOS_LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
Brian Silverman68cb5c22016-03-20 18:11:14 -0700323 auto_mode_message.Send();
324 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000325 }
326
Comran Morshed9a9948c2016-01-16 15:58:04 +0000327 private:
Austin Schuh4b652c92019-05-27 13:22:27 -0700328 ::aos::Sender<::y2016::sensors::BallDetector> ball_detector_sender_;
Austin Schuha250b2d2019-05-27 16:14:02 -0700329 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
Austin Schuhae023fb2019-06-29 17:11:45 -0700330 ::aos::Sender<ShooterQueue::Position> shooter_position_sender_;
Austin Schuh9481d0d2019-06-29 21:56:17 -0700331 ::aos::Sender<SuperstructureQueue::Position> superstructure_position_sender_;
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700332 ::aos::Sender<::frc971::control_loops::DrivetrainQueue::Position>
333 drivetrain_position_sender_;
Austin Schuh4b652c92019-05-27 13:22:27 -0700334
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700335 ::std::unique_ptr<::frc::AnalogInput> drivetrain_left_hall_,
336 drivetrain_right_hall_;
Comran Morshed225f0b92016-02-10 20:34:27 +0000337
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700338 ::std::unique_ptr<::frc::Encoder> shooter_left_encoder_,
339 shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000340 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
341 shoulder_encoder_, wrist_encoder_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700342 ::std::unique_ptr<::frc::AnalogInput> ball_detector_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000343
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700344 ::std::array<::std::unique_ptr<::frc::DigitalInput>, 4> autonomous_modes_;
Brian Silverman68cb5c22016-03-20 18:11:14 -0700345
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700346 ::frc::DigitalGlitchFilter hall_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000347};
348
349class SolenoidWriter {
350 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700351 SolenoidWriter(::aos::EventLoop *event_loop,
352 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000353 : pcm_(pcm),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700354 drivetrain_(
355 event_loop
356 ->MakeFetcher<::frc971::control_loops::DrivetrainQueue::Output>(
357 ".frc971.control_loops.drivetrain_queue.output")),
358 shooter_(event_loop->MakeFetcher<ShooterQueue::Output>(
359 ".y2016.control_loops.shooter.shooter_queue.output")),
360 superstructure_(event_loop->MakeFetcher<
361 ::y2016::control_loops::SuperstructureQueue::Output>(
362 ".y2016.control_loops.superstructure_queue.output")) {
363 event_loop->set_name("Solenoids");
364 event_loop->SetRuntimeRealtimePriority(27);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000365
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700366 event_loop->OnRun([this]() { compressor_->Start(); });
367
368 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
369 ::std::chrono::milliseconds(20),
370 ::std::chrono::milliseconds(1));
371 }
372
373 void set_compressor(::std::unique_ptr<::frc::Compressor> compressor) {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800374 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000375 }
376
Comran Morshed71466fe2016-04-21 20:21:14 -0700377 void set_drivetrain_shifter(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000378 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700379 drivetrain_shifter_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000380 }
381
Comran Morshed71466fe2016-04-21 20:21:14 -0700382 void set_climber_trigger(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000383 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700384 climber_trigger_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000385 }
386
Sabina Davis2243cab2019-02-05 21:45:08 -0800387 void set_traverse(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuh843412b2016-03-20 16:48:46 -0700388 traverse_ = ::std::move(s);
389 }
390
391 void set_traverse_latch(
392 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
393 traverse_latch_ = ::std::move(s);
394 }
395
Comran Morshedb79c4242016-02-06 18:27:26 +0000396 void set_shooter_clamp(
397 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
398 shooter_clamp_ = ::std::move(s);
399 }
400
401 void set_shooter_pusher(
402 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
403 shooter_pusher_ = ::std::move(s);
404 }
405
Sabina Davis2243cab2019-02-05 21:45:08 -0800406 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhe0729a62016-03-12 21:54:17 -0800407 lights_ = ::std::move(s);
408 }
409
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700410 void set_flashlight(::std::unique_ptr<::frc::Relay> relay) {
Austin Schuh8b89d332016-03-24 20:19:12 -0700411 flashlight_ = ::std::move(relay);
412 }
413
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700414 private:
415 void Loop(const int iterations) {
416 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700417 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700418 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000419
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700420 {
421 drivetrain_.Fetch();
422 if (drivetrain_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700423 AOS_LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700424 drivetrain_shifter_->Set(
425 !(drivetrain_->left_high || drivetrain_->right_high));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000426 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700427 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000428
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700429 {
430 shooter_.Fetch();
431 if (shooter_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700432 AOS_LOG_STRUCT(DEBUG, "solenoids", *shooter_);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700433 shooter_clamp_->Set(shooter_->clamp_open);
434 shooter_pusher_->Set(shooter_->push_to_shooter);
435 lights_->Set(shooter_->lights_on);
436 if (shooter_->forwards_flashlight) {
437 if (shooter_->backwards_flashlight) {
438 flashlight_->Set(::frc::Relay::kOn);
Austin Schuhb2c33382016-04-03 16:09:17 -0700439 } else {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700440 flashlight_->Set(::frc::Relay::kReverse);
441 }
442 } else {
443 if (shooter_->backwards_flashlight) {
444 flashlight_->Set(::frc::Relay::kForward);
445 } else {
446 flashlight_->Set(::frc::Relay::kOff);
Austin Schuhb2c33382016-04-03 16:09:17 -0700447 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000448 }
449 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700450 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000451
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700452 {
453 superstructure_.Fetch();
454 if (superstructure_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700455 AOS_LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
Comran Morshed71466fe2016-04-21 20:21:14 -0700456
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700457 climber_trigger_->Set(superstructure_->unfold_climber);
Comran Morshed71466fe2016-04-21 20:21:14 -0700458
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700459 traverse_->Set(superstructure_->traverse_down);
460 traverse_latch_->Set(superstructure_->traverse_unlatched);
Austin Schuh843412b2016-03-20 16:48:46 -0700461 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700462 }
Austin Schuh843412b2016-03-20 16:48:46 -0700463
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700464 {
465 ::frc971::wpilib::PneumaticsToLog to_log;
466 { to_log.compressor_on = compressor_->Enabled(); }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000467
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700468 pcm_->Flush();
469 to_log.read_solenoids = pcm_->GetAll();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700470 AOS_LOG_STRUCT(DEBUG, "pneumatics info", to_log);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000471 }
472 }
473
Comran Morshed9a9948c2016-01-16 15:58:04 +0000474 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
475
Comran Morshed71466fe2016-04-21 20:21:14 -0700476 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_shifter_,
477 shooter_clamp_, shooter_pusher_, lights_, traverse_, traverse_latch_,
478 climber_trigger_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700479 ::std::unique_ptr<::frc::Relay> flashlight_;
480 ::std::unique_ptr<::frc::Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000481
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700482 ::aos::Fetcher<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
483 ::aos::Fetcher<ShooterQueue::Output> shooter_;
484 ::aos::Fetcher<::y2016::control_loops::SuperstructureQueue::Output>
Austin Schuh843412b2016-03-20 16:48:46 -0700485 superstructure_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000486};
487
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700488class ShooterWriter : public LoopOutputHandler<ShooterQueue::Output> {
Comran Morshed225f0b92016-02-10 20:34:27 +0000489 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800490 ShooterWriter(::aos::EventLoop *event_loop)
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700491 : LoopOutputHandler<ShooterQueue::Output>(
492 event_loop, ".y2016.control_loops.shooter.shooter_queue.output") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800493
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700494 void set_shooter_left_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000495 shooter_left_talon_ = ::std::move(t);
496 }
497
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700498 void set_shooter_right_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000499 shooter_right_talon_ = ::std::move(t);
500 }
501
502 private:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700503 void Write(const ShooterQueue::Output &output) override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700504 AOS_LOG_STRUCT(DEBUG, "will output", output);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700505
506 shooter_left_talon_->SetSpeed(output.voltage_left / 12.0);
507 shooter_right_talon_->SetSpeed(-output.voltage_right / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000508 }
509
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700510 void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700511 AOS_LOG(WARNING, "Shooter output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800512 shooter_left_talon_->SetDisabled();
513 shooter_right_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000514 }
515
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700516 ::std::unique_ptr<::frc::Talon> shooter_left_talon_, shooter_right_talon_;
Comran Morshed225f0b92016-02-10 20:34:27 +0000517};
518
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700519class SuperstructureWriter
520 : public LoopOutputHandler<
521 ::y2016::control_loops::SuperstructureQueue::Output> {
Comran Morshed225f0b92016-02-10 20:34:27 +0000522 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800523 SuperstructureWriter(::aos::EventLoop *event_loop)
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700524 : LoopOutputHandler<::y2016::control_loops::SuperstructureQueue::Output>(
525 event_loop, ".y2016.control_loops.superstructure_queue.output") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800526
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700527 void set_intake_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000528 intake_talon_ = ::std::move(t);
529 }
530
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700531 void set_shoulder_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000532 shoulder_talon_ = ::std::move(t);
533 }
534
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700535 void set_wrist_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed225f0b92016-02-10 20:34:27 +0000536 wrist_talon_ = ::std::move(t);
537 }
538
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700539 void set_top_rollers_talon(::std::unique_ptr<::frc::Talon> t) {
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800540 top_rollers_talon_ = ::std::move(t);
541 }
542
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700543 void set_bottom_rollers_talon(::std::unique_ptr<::frc::Talon> t) {
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800544 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000545 }
546
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700547 void set_climber_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700548 climber_talon_ = ::std::move(t);
549 }
550
Comran Morshed225f0b92016-02-10 20:34:27 +0000551 private:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700552 virtual void Write(const ::y2016::control_loops::SuperstructureQueue::Output
553 &output) override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700554 AOS_LOG_STRUCT(DEBUG, "will output", output);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700555 intake_talon_->SetSpeed(::aos::Clip(output.voltage_intake,
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800556 -kMaxBringupPower, kMaxBringupPower) /
557 12.0);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700558 shoulder_talon_->SetSpeed(::aos::Clip(-output.voltage_shoulder,
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800559 -kMaxBringupPower, kMaxBringupPower) /
560 12.0);
561 wrist_talon_->SetSpeed(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700562 ::aos::Clip(output.voltage_wrist, -kMaxBringupPower, kMaxBringupPower) /
Austin Schuha9992ff2016-02-28 21:59:23 -0800563 12.0);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700564 top_rollers_talon_->SetSpeed(-output.voltage_top_rollers / 12.0);
565 bottom_rollers_talon_->SetSpeed(-output.voltage_bottom_rollers / 12.0);
566 climber_talon_->SetSpeed(-output.voltage_climber / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000567 }
568
569 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700570 AOS_LOG(WARNING, "Superstructure output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800571 intake_talon_->SetDisabled();
572 shoulder_talon_->SetDisabled();
573 wrist_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000574 }
575
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700576 ::std::unique_ptr<::frc::Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Comran Morshed71466fe2016-04-21 20:21:14 -0700577 top_rollers_talon_, bottom_rollers_talon_, climber_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000578};
579
Comran Morshed9a9948c2016-01-16 15:58:04 +0000580class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
581 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700582 ::std::unique_ptr<::frc::Encoder> make_encoder(int index) {
583 return make_unique<::frc::Encoder>(10 + index * 2, 11 + index * 2, false,
584 ::frc::Encoder::k4X);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000585 }
586
587 void Run() override {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700588 // Thread 1.
589 ::aos::ShmEventLoop joystick_sender_event_loop;
590 ::frc971::wpilib::JoystickSender joystick_sender(
591 &joystick_sender_event_loop);
592 AddLoop(&joystick_sender_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000593
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700594 // Thread 2.
595 ::aos::ShmEventLoop pdp_fetcher_event_loop;
596 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
597 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800598
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700599 // Thread 3.
600 ::aos::ShmEventLoop sensor_reader_event_loop;
601 SensorReader sensor_reader(&sensor_reader_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000602
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700603 sensor_reader.set_drivetrain_left_encoder(make_encoder(5));
604 sensor_reader.set_drivetrain_right_encoder(make_encoder(6));
605 sensor_reader.set_drivetrain_left_hall(make_unique<::frc::AnalogInput>(5));
606 sensor_reader.set_drivetrain_right_hall(make_unique<::frc::AnalogInput>(6));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000607
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700608 sensor_reader.set_shooter_left_encoder(make_encoder(7));
609 sensor_reader.set_shooter_right_encoder(make_encoder(-3));
Comran Morshed225f0b92016-02-10 20:34:27 +0000610
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700611 sensor_reader.set_intake_encoder(make_encoder(0));
612 sensor_reader.set_intake_index(make_unique<::frc::DigitalInput>(0));
613 sensor_reader.set_intake_potentiometer(make_unique<::frc::AnalogInput>(0));
Comran Morshed225f0b92016-02-10 20:34:27 +0000614
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700615 sensor_reader.set_shoulder_encoder(make_encoder(4));
616 sensor_reader.set_shoulder_index(make_unique<::frc::DigitalInput>(2));
617 sensor_reader.set_shoulder_potentiometer(
618 make_unique<::frc::AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000619
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700620 sensor_reader.set_wrist_encoder(make_encoder(1));
621 sensor_reader.set_wrist_index(make_unique<::frc::DigitalInput>(1));
622 sensor_reader.set_wrist_potentiometer(make_unique<::frc::AnalogInput>(1));
Comran Morshed225f0b92016-02-10 20:34:27 +0000623
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700624 sensor_reader.set_ball_detector(make_unique<::frc::AnalogInput>(7));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000625
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700626 sensor_reader.set_autonomous_mode(0, make_unique<::frc::DigitalInput>(9));
627 sensor_reader.set_autonomous_mode(1, make_unique<::frc::DigitalInput>(8));
628 sensor_reader.set_autonomous_mode(2, make_unique<::frc::DigitalInput>(7));
629 sensor_reader.set_autonomous_mode(3, make_unique<::frc::DigitalInput>(6));
630 AddLoop(&sensor_reader_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000631
Brian Silverman003a4732018-03-11 14:02:15 -0700632 // TODO(Brian): This interacts poorly with the SpiRxClearer in ADIS16448.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700633 // Thread 4.
634 ::aos::ShmEventLoop gyro_event_loop;
635 ::frc971::wpilib::GyroSender gyro_sender(&gyro_event_loop);
636 AddLoop(&gyro_event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000637
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700638 // Thread 5.
639 ::aos::ShmEventLoop imu_event_loop;
640 auto imu_trigger = make_unique<::frc::DigitalInput>(3);
641 ::frc971::wpilib::ADIS16448 imu(&imu_event_loop, ::frc::SPI::Port::kMXP,
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800642 imu_trigger.get());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700643 AddLoop(&imu_event_loop);
Brian Silverman5f17a972016-02-28 01:49:32 -0500644
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700645 // Thread 5.
646 ::aos::ShmEventLoop output_event_loop;
647 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
Sabina Davisb71bc282019-02-03 01:17:23 -0800648 drivetrain_writer.set_left_controller0(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700649 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(5)), false);
Sabina Davisb71bc282019-02-03 01:17:23 -0800650 drivetrain_writer.set_right_controller0(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700651 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(4)), true);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000652
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700653 ShooterWriter shooter_writer(&output_event_loop);
Comran Morshed225f0b92016-02-10 20:34:27 +0000654 shooter_writer.set_shooter_left_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700655 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000656 shooter_writer.set_shooter_right_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700657 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000658
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700659 SuperstructureWriter superstructure_writer(&output_event_loop);
Comran Morshed225f0b92016-02-10 20:34:27 +0000660 superstructure_writer.set_intake_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700661 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000662 superstructure_writer.set_shoulder_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700663 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000664 superstructure_writer.set_wrist_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700665 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800666 superstructure_writer.set_top_rollers_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700667 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800668 superstructure_writer.set_bottom_rollers_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700669 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(0)));
Comran Morshed71466fe2016-04-21 20:21:14 -0700670 superstructure_writer.set_climber_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700671 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(7)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000672
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700673 AddLoop(&output_event_loop);
674
675 // Thread 6.
676 ::aos::ShmEventLoop solenoid_writer_event_loop;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000677 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
678 new ::frc971::wpilib::BufferedPcm());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700679 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm);
Comran Morshed71466fe2016-04-21 20:21:14 -0700680 solenoid_writer.set_drivetrain_shifter(pcm->MakeSolenoid(0));
Austin Schuh843412b2016-03-20 16:48:46 -0700681 solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2));
682 solenoid_writer.set_traverse(pcm->MakeSolenoid(3));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800683 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
684 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Austin Schuhe0729a62016-03-12 21:54:17 -0800685 solenoid_writer.set_lights(pcm->MakeSolenoid(6));
Comran Morshed71466fe2016-04-21 20:21:14 -0700686 solenoid_writer.set_climber_trigger(pcm->MakeSolenoid(1));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700687 solenoid_writer.set_flashlight(make_unique<::frc::Relay>(0));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000688
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700689 solenoid_writer.set_compressor(make_unique<::frc::Compressor>());
690 AddLoop(&solenoid_writer_event_loop);
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800691
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700692 RunLoops();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000693 }
694};
695
696} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000697} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000698
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000699AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);