blob: 320dd73b549a6a79184f519360a28effe6ac4bc5 [file] [log] [blame]
Comran Morshed9a9948c2016-01-16 15:58:04 +00001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <inttypes.h>
5
Austin Schuh8aec1ed2016-05-01 13:29:20 -07006#include <chrono>
Comran Morshed9a9948c2016-01-16 15:58:04 +00007#include <thread>
8#include <mutex>
9#include <functional>
Brian Silverman68cb5c22016-03-20 18:11:14 -070010#include <array>
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
John Park33858a32018-09-28 23:05:48 -070022#include "aos/logging/logging.h"
23#include "aos/logging/queue_logging.h"
24#include "aos/time/time.h"
25#include "aos/util/log_interval.h"
26#include "aos/util/phased_loop.h"
27#include "aos/util/wrapping_counter.h"
28#include "aos/stl_mutex/stl_mutex.h"
John Park398c74a2018-10-20 21:17:39 -070029#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070030#include "aos/robot_state/robot_state.q.h"
31#include "aos/commonmath.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000032
Philipp Schrader4bd29b12017-02-22 04:42:27 +000033#include "frc971/autonomous/auto.q.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000034#include "frc971/control_loops/control_loops.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000035#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Comran Morshedb79c4242016-02-06 18:27:26 +000036#include "y2016/control_loops/shooter/shooter.q.h"
Comran Morshed6c6a0a92016-01-17 12:45:16 +000037#include "y2016/constants.h"
Comran Morshed5bb12112016-02-16 13:48:57 +000038#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000039#include "y2016/control_loops/shooter/shooter.q.h"
40#include "y2016/control_loops/superstructure/superstructure.q.h"
Comran Morshedaa0573c2016-03-05 19:05:54 +000041#include "y2016/queues/ball_detector.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000042
43#include "frc971/wpilib/joystick_sender.h"
44#include "frc971/wpilib/loop_output_handler.h"
45#include "frc971/wpilib/buffered_solenoid.h"
46#include "frc971/wpilib/buffered_pcm.h"
47#include "frc971/wpilib/gyro_sender.h"
48#include "frc971/wpilib/dma_edge_counting.h"
49#include "frc971/wpilib/interrupt_edge_counting.h"
50#include "frc971/wpilib/encoder_and_potentiometer.h"
51#include "frc971/wpilib/logging.q.h"
52#include "frc971/wpilib/wpilib_interface.h"
53#include "frc971/wpilib/pdp_fetcher.h"
Brian Silverman5f17a972016-02-28 01:49:32 -050054#include "frc971/wpilib/ADIS16448.h"
Brian Silvermanb5b46ca2016-03-13 01:14:17 -050055#include "frc971/wpilib/dma.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000056
57#ifndef M_PI
58#define M_PI 3.14159265358979323846
59#endif
60
61using ::frc971::control_loops::drivetrain_queue;
Comran Morshed225f0b92016-02-10 20:34:27 +000062using ::y2016::control_loops::shooter::shooter_queue;
63using ::y2016::control_loops::superstructure_queue;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080064using namespace frc;
Comran Morshed9a9948c2016-01-16 15:58:04 +000065
Comran Morshed6c6a0a92016-01-17 12:45:16 +000066namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000067namespace wpilib {
Austin Schuha9992ff2016-02-28 21:59:23 -080068namespace {
69constexpr double kMaxBringupPower = 12.0;
70} // namespace
Comran Morshed9a9948c2016-01-16 15:58:04 +000071
72// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
73// DMA stuff and then removing the * 2.0 in *_translate.
74// The low bit is direction.
75
76// TODO(brian): Replace this with ::std::make_unique once all our toolchains
77// have support.
78template <class T, class... U>
79std::unique_ptr<T> make_unique(U &&... u) {
80 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
81}
82
Comran Morshed225f0b92016-02-10 20:34:27 +000083// Translates for the sensor values to convert raw index pulses into something
84// with proper units.
85
86// TODO(comran): Template these methods since there is a lot of repetition here.
87double hall_translate(double in) {
88 // Turn voltage from our 3-state halls into a ratio that the loop can use.
89 return in / 5.0;
90}
91
Comran Morshed9a9948c2016-01-16 15:58:04 +000092double drivetrain_translate(int32_t in) {
Comran Morshed6c6a0a92016-01-17 12:45:16 +000093 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000094 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080095 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000096}
97
98double drivetrain_velocity_translate(double in) {
99 return (1.0 / in) / 256.0 /*cpr*/ *
Comran Morshed225f0b92016-02-10 20:34:27 +0000100 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -0800101 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000102}
103
Comran Morshed225f0b92016-02-10 20:34:27 +0000104double shooter_translate(int32_t in) {
Comran Morshed5bb12112016-02-16 13:48:57 +0000105 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000106 constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
107}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000108
Comran Morshed225f0b92016-02-10 20:34:27 +0000109double intake_translate(int32_t in) {
Austin Schuh9f77fd22016-02-21 02:53:58 -0800110 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000111 constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
112}
113
114double shoulder_translate(int32_t in) {
115 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
116 constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/);
117}
118
119double wrist_translate(int32_t in) {
120 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
121 constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/);
122}
123
124double intake_pot_translate(double voltage) {
125 return voltage * constants::Values::kIntakePotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000126 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000127}
128
129double shoulder_pot_translate(double voltage) {
130 return voltage * constants::Values::kShoulderPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000131 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000132}
133
134double wrist_pot_translate(double voltage) {
135 return voltage * constants::Values::kWristPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000136 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000137}
138
Comran Morshed1b764322016-02-14 20:18:12 +0000139constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
140 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
141 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
142 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
143
144constexpr double kMaxShooterEncoderPulsesPerSecond =
145 18700.0 /* 775pro free speed RPM */ * 12.0 /
146 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
147 128.0 /* CPR */ * 4 /* edges per cycle */;
148
149double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max(
150 kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond);
151
152constexpr double kMaxSuperstructureEncoderPulsesPerSecond =
153 18700.0 /* 775pro free speed RPM */ * 12.0 /
154 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
155 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000156
Comran Morshed225f0b92016-02-10 20:34:27 +0000157// Class to send position messages with sensor readings to our loops.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000158class SensorReader {
159 public:
160 SensorReader() {
161 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
162 // we should ever see.
Comran Morshed1b764322016-02-14 20:18:12 +0000163 drivetrain_shooter_encoder_filter_.SetPeriodNanoSeconds(
164 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
165 kMaxDrivetrainShooterEncoderPulsesPerSecond * 1e9 +
166 0.5));
167 superstructure_encoder_filter_.SetPeriodNanoSeconds(
168 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
169 kMaxSuperstructureEncoderPulsesPerSecond * 1e9 +
170 0.5));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000171 hall_filter_.SetPeriodNanoSeconds(100000);
172 }
173
Comran Morshed225f0b92016-02-10 20:34:27 +0000174 // Drivetrain setters.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000175 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000176 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000177 drivetrain_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000178 }
179
180 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000181 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000182 drivetrain_right_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000183 }
184
Comran Morshed225f0b92016-02-10 20:34:27 +0000185 void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) {
186 drivetrain_left_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000187 }
188
Comran Morshed225f0b92016-02-10 20:34:27 +0000189 void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) {
190 drivetrain_right_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000191 }
192
Comran Morshed225f0b92016-02-10 20:34:27 +0000193 // Shooter setters.
194 void set_shooter_left_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000195 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000196 shooter_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000197 }
198
Comran Morshed225f0b92016-02-10 20:34:27 +0000199 void set_shooter_right_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000200 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000201 shooter_right_encoder_ = ::std::move(encoder);
202 }
203
204 // Intake setters.
205 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000206 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000207 intake_encoder_.set_encoder(::std::move(encoder));
208 }
209
210 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
211 intake_encoder_.set_potentiometer(::std::move(potentiometer));
212 }
213
214 void set_intake_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000215 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000216 intake_encoder_.set_index(::std::move(index));
217 }
218
219 // Shoulder setters.
220 void set_shoulder_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000221 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000222 shoulder_encoder_.set_encoder(::std::move(encoder));
223 }
224
225 void set_shoulder_potentiometer(
226 ::std::unique_ptr<AnalogInput> potentiometer) {
227 shoulder_encoder_.set_potentiometer(::std::move(potentiometer));
228 }
229
230 void set_shoulder_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000231 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000232 shoulder_encoder_.set_index(::std::move(index));
233 }
234
235 // Wrist setters.
236 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000237 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000238 wrist_encoder_.set_encoder(::std::move(encoder));
239 }
240
241 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
242 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
243 }
244
245 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000246 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000247 wrist_encoder_.set_index(::std::move(index));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000248 }
249
Comran Morshedaa0573c2016-03-05 19:05:54 +0000250 // Ball detector setter.
251 void set_ball_detector(::std::unique_ptr<AnalogInput> analog) {
252 ball_detector_ = ::std::move(analog);
253 }
254
Brian Silverman68cb5c22016-03-20 18:11:14 -0700255 // Autonomous mode switch setter.
256 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
257 autonomous_modes_.at(i) = ::std::move(sensor);
258 }
259
Comran Morshedaa0573c2016-03-05 19:05:54 +0000260
Comran Morshed9a9948c2016-01-16 15:58:04 +0000261 // All of the DMA-related set_* calls must be made before this, and it doesn't
262 // hurt to do all of them.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000263
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000264 void set_dma(::std::unique_ptr<DMA> dma) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000265 dma_synchronizer_.reset(
266 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000267 dma_synchronizer_->Add(&intake_encoder_);
268 dma_synchronizer_->Add(&shoulder_encoder_);
269 dma_synchronizer_->Add(&wrist_encoder_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000270 }
271
272 void operator()() {
273 ::aos::SetCurrentThreadName("SensorReader");
274
275 my_pid_ = getpid();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000276
Comran Morshed9a9948c2016-01-16 15:58:04 +0000277 dma_synchronizer_->Start();
278
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700279 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
280 ::std::chrono::milliseconds(4));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000281
282 ::aos::SetCurrentThreadRealtimePriority(40);
283 while (run_) {
284 {
285 const int iterations = phased_loop.SleepUntilNext();
286 if (iterations != 1) {
287 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
288 }
289 }
290 RunIteration();
291 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000292 }
293
294 void RunIteration() {
Austin Schuh94f51e92017-10-30 19:25:32 -0700295 ::frc971::wpilib::SendRobotState(my_pid_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000296
297 const auto &values = constants::GetValues();
298
299 {
300 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
301 drivetrain_message->right_encoder =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800302 drivetrain_translate(-drivetrain_right_encoder_->GetRaw());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000303 drivetrain_message->left_encoder =
304 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
305 drivetrain_message->left_speed =
306 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
307 drivetrain_message->right_speed =
308 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
309
Comran Morshed9a9948c2016-01-16 15:58:04 +0000310 drivetrain_message->left_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000311 hall_translate(drivetrain_left_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000312 drivetrain_message->right_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000313 hall_translate(drivetrain_right_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000314
315 drivetrain_message.Send();
316 }
317
Comran Morshed9a9948c2016-01-16 15:58:04 +0000318 dma_synchronizer_->RunIteration();
Comran Morshed225f0b92016-02-10 20:34:27 +0000319
320 {
321 auto shooter_message = shooter_queue.position.MakeMessage();
322 shooter_message->theta_left =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800323 shooter_translate(-shooter_left_encoder_->GetRaw());
Comran Morshed225f0b92016-02-10 20:34:27 +0000324 shooter_message->theta_right =
325 shooter_translate(shooter_right_encoder_->GetRaw());
326 shooter_message.Send();
327 }
328
329 {
330 auto superstructure_message = superstructure_queue.position.MakeMessage();
331 CopyPotAndIndexPosition(intake_encoder_, &superstructure_message->intake,
332 intake_translate, intake_pot_translate, false,
333 values.intake.pot_offset);
334 CopyPotAndIndexPosition(shoulder_encoder_,
335 &superstructure_message->shoulder,
336 shoulder_translate, shoulder_pot_translate, false,
337 values.shoulder.pot_offset);
338 CopyPotAndIndexPosition(wrist_encoder_, &superstructure_message->wrist,
Austin Schuh9f77fd22016-02-21 02:53:58 -0800339 wrist_translate, wrist_pot_translate, true,
Comran Morshed225f0b92016-02-10 20:34:27 +0000340 values.wrist.pot_offset);
341
342 superstructure_message.Send();
343 }
Comran Morshedaa0573c2016-03-05 19:05:54 +0000344
345 {
346 auto ball_detector_message =
347 ::y2016::sensors::ball_detector.MakeMessage();
348 ball_detector_message->voltage = ball_detector_->GetVoltage();
349 LOG_STRUCT(DEBUG, "ball detector", *ball_detector_message);
350 ball_detector_message.Send();
351 }
Brian Silverman68cb5c22016-03-20 18:11:14 -0700352
353 {
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000354 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
Brian Silverman68cb5c22016-03-20 18:11:14 -0700355 auto_mode_message->mode = 0;
356 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
357 if (autonomous_modes_[i]->Get()) {
358 auto_mode_message->mode |= 1 << i;
359 }
360 }
361 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
362 auto_mode_message.Send();
363 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000364 }
365
366 void Quit() { run_ = false; }
367
368 private:
Comran Morshed225f0b92016-02-10 20:34:27 +0000369 void CopyPotAndIndexPosition(
370 const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
371 ::frc971::PotAndIndexPosition *position,
372 ::std::function<double(int32_t)> encoder_translate,
373 ::std::function<double(double)> potentiometer_translate, bool reverse,
374 double pot_offset) {
375 const double multiplier = reverse ? -1.0 : 1.0;
376 position->encoder =
377 multiplier * encoder_translate(encoder.polled_encoder_value());
378 position->pot = multiplier * potentiometer_translate(
379 encoder.polled_potentiometer_voltage()) +
380 pot_offset;
381 position->latched_encoder =
382 multiplier * encoder_translate(encoder.last_encoder_value());
383 position->latched_pot =
384 multiplier *
385 potentiometer_translate(encoder.last_potentiometer_voltage()) +
386 pot_offset;
387 position->index_pulses = encoder.index_posedge_count();
388 }
389
Comran Morshed9a9948c2016-01-16 15:58:04 +0000390 int32_t my_pid_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000391
392 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
393
Comran Morshed225f0b92016-02-10 20:34:27 +0000394 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
395 drivetrain_right_encoder_;
396 ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_;
397
398 ::std::unique_ptr<Encoder> shooter_left_encoder_, shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000399 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
400 shoulder_encoder_, wrist_encoder_;
Comran Morshedaa0573c2016-03-05 19:05:54 +0000401 ::std::unique_ptr<AnalogInput> ball_detector_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000402
Brian Silverman68cb5c22016-03-20 18:11:14 -0700403 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
404
Comran Morshed9a9948c2016-01-16 15:58:04 +0000405 ::std::atomic<bool> run_{true};
Comran Morshed1b764322016-02-14 20:18:12 +0000406 DigitalGlitchFilter drivetrain_shooter_encoder_filter_, hall_filter_,
407 superstructure_encoder_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000408};
409
410class SolenoidWriter {
411 public:
412 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
413 : pcm_(pcm),
Comran Morshedb79c4242016-02-06 18:27:26 +0000414 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
Austin Schuh843412b2016-03-20 16:48:46 -0700415 shooter_(".y2016.control_loops.shooter.shooter_queue.output"),
416 superstructure_(
417 ".y2016.control_loops.superstructure_queue.output") {
418 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000419
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800420 void set_compressor(::std::unique_ptr<Compressor> compressor) {
421 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000422 }
423
Comran Morshed71466fe2016-04-21 20:21:14 -0700424 void set_drivetrain_shifter(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000425 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700426 drivetrain_shifter_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000427 }
428
Comran Morshed71466fe2016-04-21 20:21:14 -0700429 void set_climber_trigger(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000430 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700431 climber_trigger_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000432 }
433
Austin Schuh843412b2016-03-20 16:48:46 -0700434 void set_traverse(
435 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
436 traverse_ = ::std::move(s);
437 }
438
439 void set_traverse_latch(
440 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
441 traverse_latch_ = ::std::move(s);
442 }
443
Comran Morshedb79c4242016-02-06 18:27:26 +0000444 void set_shooter_clamp(
445 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
446 shooter_clamp_ = ::std::move(s);
447 }
448
449 void set_shooter_pusher(
450 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
451 shooter_pusher_ = ::std::move(s);
452 }
453
Austin Schuhe0729a62016-03-12 21:54:17 -0800454 void set_lights(
455 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
456 lights_ = ::std::move(s);
457 }
458
Austin Schuh8b89d332016-03-24 20:19:12 -0700459 void set_flashlight(::std::unique_ptr<Relay> relay) {
460 flashlight_ = ::std::move(relay);
461 }
462
Comran Morshed9a9948c2016-01-16 15:58:04 +0000463 void operator()() {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800464 compressor_->Start();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000465 ::aos::SetCurrentThreadName("Solenoids");
466 ::aos::SetCurrentThreadRealtimePriority(27);
467
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700468 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
469 ::std::chrono::milliseconds(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000470
471 while (run_) {
472 {
473 const int iterations = phased_loop.SleepUntilNext();
474 if (iterations != 1) {
475 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
476 }
477 }
478
479 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000480 drivetrain_.FetchLatest();
481 if (drivetrain_.get()) {
482 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Comran Morshed71466fe2016-04-21 20:21:14 -0700483 drivetrain_shifter_->Set(
484 !(drivetrain_->left_high || drivetrain_->right_high));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000485 }
486 }
487
488 {
Comran Morshedb79c4242016-02-06 18:27:26 +0000489 shooter_.FetchLatest();
490 if (shooter_.get()) {
491 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
492 shooter_clamp_->Set(shooter_->clamp_open);
493 shooter_pusher_->Set(shooter_->push_to_shooter);
Austin Schuhe0729a62016-03-12 21:54:17 -0800494 lights_->Set(shooter_->lights_on);
Austin Schuhb2c33382016-04-03 16:09:17 -0700495 if (shooter_->forwards_flashlight) {
496 if (shooter_->backwards_flashlight) {
497 flashlight_->Set(Relay::kOn);
498 } else {
499 flashlight_->Set(Relay::kReverse);
500 }
501 } else {
502 if (shooter_->backwards_flashlight) {
503 flashlight_->Set(Relay::kForward);
504 } else {
505 flashlight_->Set(Relay::kOff);
506 }
507 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000508 }
509 }
510
511 {
Austin Schuh843412b2016-03-20 16:48:46 -0700512 superstructure_.FetchLatest();
513 if (superstructure_.get()) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700514 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
515
516 climber_trigger_->Set(superstructure_->unfold_climber);
517
Austin Schuh843412b2016-03-20 16:48:46 -0700518 traverse_->Set(superstructure_->traverse_down);
519 traverse_latch_->Set(superstructure_->traverse_unlatched);
520 }
521 }
522
523 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000524 ::frc971::wpilib::PneumaticsToLog to_log;
525 {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800526 to_log.compressor_on = compressor_->Enabled();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000527 }
528
529 pcm_->Flush();
530 to_log.read_solenoids = pcm_->GetAll();
531 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
532 }
533 }
534 }
535
536 void Quit() { run_ = false; }
537
538 private:
539 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
540
Comran Morshed71466fe2016-04-21 20:21:14 -0700541 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_shifter_,
542 shooter_clamp_, shooter_pusher_, lights_, traverse_, traverse_latch_,
543 climber_trigger_;
Austin Schuh8b89d332016-03-24 20:19:12 -0700544 ::std::unique_ptr<Relay> flashlight_;
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800545 ::std::unique_ptr<Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000546
Comran Morshed9a9948c2016-01-16 15:58:04 +0000547 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Comran Morshed3263e8f2016-02-14 17:55:45 +0000548 ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_;
Austin Schuh843412b2016-03-20 16:48:46 -0700549 ::aos::Queue<
550 ::y2016::control_loops::SuperstructureQueue::Output>
551 superstructure_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000552
553 ::std::atomic<bool> run_{true};
554};
555
556class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
557 public:
Comran Morshed225f0b92016-02-10 20:34:27 +0000558 void set_drivetrain_left_talon(::std::unique_ptr<Talon> t) {
559 drivetrain_left_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000560 }
561
Comran Morshed225f0b92016-02-10 20:34:27 +0000562 void set_drivetrain_right_talon(::std::unique_ptr<Talon> t) {
563 drivetrain_right_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000564 }
565
566 private:
567 virtual void Read() override {
568 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
569 }
570
571 virtual void Write() override {
572 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
573 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800574 drivetrain_left_talon_->SetSpeed(queue->left_voltage / 12.0);
575 drivetrain_right_talon_->SetSpeed(-queue->right_voltage / 12.0);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000576 }
577
578 virtual void Stop() override {
579 LOG(WARNING, "drivetrain output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800580 drivetrain_left_talon_->SetDisabled();
581 drivetrain_right_talon_->SetDisabled();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000582 }
583
Comran Morshed225f0b92016-02-10 20:34:27 +0000584 ::std::unique_ptr<Talon> drivetrain_left_talon_, drivetrain_right_talon_;
585};
586
587class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
588 public:
589 void set_shooter_left_talon(::std::unique_ptr<Talon> t) {
590 shooter_left_talon_ = ::std::move(t);
591 }
592
593 void set_shooter_right_talon(::std::unique_ptr<Talon> t) {
594 shooter_right_talon_ = ::std::move(t);
595 }
596
597 private:
598 virtual void Read() override {
599 ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother();
600 }
601
602 virtual void Write() override {
603 auto &queue = ::y2016::control_loops::shooter::shooter_queue.output;
604 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800605
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800606 shooter_left_talon_->SetSpeed(queue->voltage_left / 12.0);
607 shooter_right_talon_->SetSpeed(-queue->voltage_right / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000608 }
609
610 virtual void Stop() override {
611 LOG(WARNING, "Shooter output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800612 shooter_left_talon_->SetDisabled();
613 shooter_right_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000614 }
615
616 ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_;
617};
618
619class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
620 public:
621 void set_intake_talon(::std::unique_ptr<Talon> t) {
622 intake_talon_ = ::std::move(t);
623 }
624
625 void set_shoulder_talon(::std::unique_ptr<Talon> t) {
626 shoulder_talon_ = ::std::move(t);
627 }
628
629 void set_wrist_talon(::std::unique_ptr<Talon> t) {
630 wrist_talon_ = ::std::move(t);
631 }
632
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800633 void set_top_rollers_talon(::std::unique_ptr<Talon> t) {
634 top_rollers_talon_ = ::std::move(t);
635 }
636
637 void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) {
638 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000639 }
640
Comran Morshed71466fe2016-04-21 20:21:14 -0700641 void set_climber_talon(::std::unique_ptr<Talon> t) {
642 climber_talon_ = ::std::move(t);
643 }
644
Comran Morshed225f0b92016-02-10 20:34:27 +0000645 private:
646 virtual void Read() override {
647 ::y2016::control_loops::superstructure_queue.output.FetchAnother();
648 }
649
650 virtual void Write() override {
651 auto &queue = ::y2016::control_loops::superstructure_queue.output;
652 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800653 intake_talon_->SetSpeed(::aos::Clip(queue->voltage_intake,
654 -kMaxBringupPower, kMaxBringupPower) /
655 12.0);
656 shoulder_talon_->SetSpeed(::aos::Clip(-queue->voltage_shoulder,
657 -kMaxBringupPower, kMaxBringupPower) /
658 12.0);
659 wrist_talon_->SetSpeed(
Austin Schuha9992ff2016-02-28 21:59:23 -0800660 ::aos::Clip(queue->voltage_wrist, -kMaxBringupPower, kMaxBringupPower) /
661 12.0);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800662 top_rollers_talon_->SetSpeed(-queue->voltage_top_rollers / 12.0);
663 bottom_rollers_talon_->SetSpeed(-queue->voltage_bottom_rollers / 12.0);
664 climber_talon_->SetSpeed(-queue->voltage_climber / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000665 }
666
667 virtual void Stop() override {
668 LOG(WARNING, "Superstructure output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800669 intake_talon_->SetDisabled();
670 shoulder_talon_->SetDisabled();
671 wrist_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000672 }
673
Comran Morshedf4cd7642016-02-15 20:40:49 +0000674 ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Comran Morshed71466fe2016-04-21 20:21:14 -0700675 top_rollers_talon_, bottom_rollers_talon_, climber_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000676};
677
Comran Morshed9a9948c2016-01-16 15:58:04 +0000678class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
679 public:
680 ::std::unique_ptr<Encoder> make_encoder(int index) {
681 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
682 Encoder::k4X);
683 }
684
685 void Run() override {
686 ::aos::InitNRT();
687 ::aos::SetCurrentThreadName("StartCompetition");
688
689 ::frc971::wpilib::JoystickSender joystick_sender;
690 ::std::thread joystick_thread(::std::ref(joystick_sender));
691
692 ::frc971::wpilib::PDPFetcher pdp_fetcher;
693 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
694 SensorReader reader;
695
Austin Schuh9f77fd22016-02-21 02:53:58 -0800696 reader.set_drivetrain_left_encoder(make_encoder(5));
697 reader.set_drivetrain_right_encoder(make_encoder(6));
698 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5));
699 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6));
Comran Morshed225f0b92016-02-10 20:34:27 +0000700
Austin Schuhf0c05762016-04-03 16:06:49 -0700701 reader.set_shooter_left_encoder(make_encoder(7));
702 reader.set_shooter_right_encoder(make_encoder(-3));
Comran Morshed225f0b92016-02-10 20:34:27 +0000703
704 reader.set_intake_encoder(make_encoder(0));
705 reader.set_intake_index(make_unique<DigitalInput>(0));
706 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
707
Austin Schuhf0c05762016-04-03 16:06:49 -0700708 reader.set_shoulder_encoder(make_encoder(4));
Austin Schuh9f77fd22016-02-21 02:53:58 -0800709 reader.set_shoulder_index(make_unique<DigitalInput>(2));
710 reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000711
Austin Schuh9f77fd22016-02-21 02:53:58 -0800712 reader.set_wrist_encoder(make_encoder(1));
713 reader.set_wrist_index(make_unique<DigitalInput>(1));
714 reader.set_wrist_potentiometer(make_unique<AnalogInput>(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000715
Comran Morshedaa0573c2016-03-05 19:05:54 +0000716 reader.set_ball_detector(make_unique<AnalogInput>(7));
717
Brian Silverman68cb5c22016-03-20 18:11:14 -0700718 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
719 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
720 reader.set_autonomous_mode(2, make_unique<DigitalInput>(7));
721 reader.set_autonomous_mode(3, make_unique<DigitalInput>(6));
722
Comran Morshed9a9948c2016-01-16 15:58:04 +0000723 reader.set_dma(make_unique<DMA>());
724 ::std::thread reader_thread(::std::ref(reader));
725
Brian Silverman003a4732018-03-11 14:02:15 -0700726 // TODO(Brian): This interacts poorly with the SpiRxClearer in ADIS16448.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000727 ::frc971::wpilib::GyroSender gyro_sender;
728 ::std::thread gyro_thread(::std::ref(gyro_sender));
729
Austin Schuhf0c05762016-04-03 16:06:49 -0700730 auto imu_trigger = make_unique<DigitalInput>(3);
731 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kMXP, imu_trigger.get());
Brian Silverman5f17a972016-02-28 01:49:32 -0500732 ::std::thread imu_thread(::std::ref(imu));
Brian Silverman5f17a972016-02-28 01:49:32 -0500733
Comran Morshed9a9948c2016-01-16 15:58:04 +0000734 DrivetrainWriter drivetrain_writer;
Comran Morshed225f0b92016-02-10 20:34:27 +0000735 drivetrain_writer.set_drivetrain_left_talon(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000736 ::std::unique_ptr<Talon>(new Talon(5)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000737 drivetrain_writer.set_drivetrain_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800738 ::std::unique_ptr<Talon>(new Talon(4)));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000739 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
740
Comran Morshed225f0b92016-02-10 20:34:27 +0000741 ShooterWriter shooter_writer;
742 shooter_writer.set_shooter_left_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800743 ::std::unique_ptr<Talon>(new Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000744 shooter_writer.set_shooter_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800745 ::std::unique_ptr<Talon>(new Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000746 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
747
748 SuperstructureWriter superstructure_writer;
749 superstructure_writer.set_intake_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800750 ::std::unique_ptr<Talon>(new Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000751 superstructure_writer.set_shoulder_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800752 ::std::unique_ptr<Talon>(new Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000753 superstructure_writer.set_wrist_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800754 ::std::unique_ptr<Talon>(new Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800755 superstructure_writer.set_top_rollers_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800756 ::std::unique_ptr<Talon>(new Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800757 superstructure_writer.set_bottom_rollers_talon(
758 ::std::unique_ptr<Talon>(new Talon(0)));
Comran Morshed71466fe2016-04-21 20:21:14 -0700759 superstructure_writer.set_climber_talon(
760 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000761 ::std::thread superstructure_writer_thread(
762 ::std::ref(superstructure_writer));
763
Comran Morshed9a9948c2016-01-16 15:58:04 +0000764 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
765 new ::frc971::wpilib::BufferedPcm());
766 SolenoidWriter solenoid_writer(pcm);
Comran Morshed71466fe2016-04-21 20:21:14 -0700767 solenoid_writer.set_drivetrain_shifter(pcm->MakeSolenoid(0));
Austin Schuh843412b2016-03-20 16:48:46 -0700768 solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2));
769 solenoid_writer.set_traverse(pcm->MakeSolenoid(3));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800770 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
771 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Austin Schuhe0729a62016-03-12 21:54:17 -0800772 solenoid_writer.set_lights(pcm->MakeSolenoid(6));
Comran Morshed71466fe2016-04-21 20:21:14 -0700773 solenoid_writer.set_climber_trigger(pcm->MakeSolenoid(1));
Austin Schuh8b89d332016-03-24 20:19:12 -0700774 solenoid_writer.set_flashlight(make_unique<Relay>(0));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000775
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800776 solenoid_writer.set_compressor(make_unique<Compressor>());
777
Comran Morshed9a9948c2016-01-16 15:58:04 +0000778 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
779
780 // Wait forever. Not much else to do...
781 while (true) {
782 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
783 if (r != 0) {
784 PLOG(WARNING, "infinite select failed");
785 } else {
786 PLOG(WARNING, "infinite select succeeded??\n");
787 }
788 }
789
790 LOG(ERROR, "Exiting WPILibRobot\n");
791
792 joystick_sender.Quit();
793 joystick_thread.join();
794 pdp_fetcher.Quit();
795 pdp_fetcher_thread.join();
796 reader.Quit();
797 reader_thread.join();
798 gyro_sender.Quit();
799 gyro_thread.join();
Brian Silverman5f17a972016-02-28 01:49:32 -0500800 imu.Quit();
801 imu_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000802
803 drivetrain_writer.Quit();
804 drivetrain_writer_thread.join();
Comran Morshed225f0b92016-02-10 20:34:27 +0000805 shooter_writer.Quit();
806 shooter_writer_thread.join();
807 superstructure_writer.Quit();
808 superstructure_writer_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000809 solenoid_writer.Quit();
810 solenoid_thread.join();
811
812 ::aos::Cleanup();
813 }
814};
815
816} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000817} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000818
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000819AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);