blob: 0278ebcda964a495bf4382d47b939b8e1722163b [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <inttypes.h>
5
6#include <thread>
7#include <mutex>
8#include <functional>
9
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070010#include "Encoder.h"
11#include "Talon.h"
12#include "DriverStation.h"
13#include "AnalogInput.h"
14#include "Compressor.h"
15#include "Relay.h"
16#include "RobotBase.h"
17#include "dma.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070018#ifndef WPILIB2015
19#include "DigitalGlitchFilter.h"
20#endif
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -050021#include "PowerDistributionPanel.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070022#undef ERROR
23
Brian Silverman17f503e2015-08-02 18:17:18 -070024#include "aos/common/logging/logging.h"
25#include "aos/common/logging/queue_logging.h"
26#include "aos/common/time.h"
27#include "aos/common/util/log_interval.h"
28#include "aos/common/util/phased_loop.h"
29#include "aos/common/util/wrapping_counter.h"
30#include "aos/common/stl_mutex.h"
31#include "aos/linux_code/init.h"
32#include "aos/common/messages/robot_state.q.h"
33
Brian Silverman552350b2015-08-02 18:23:34 -070034#include "frc971/shifter_hall_effect.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050035
Brian Silverman17f503e2015-08-02 18:17:18 -070036#include "y2014/control_loops/drivetrain/drivetrain.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070037#include "y2014/control_loops/claw/claw.q.h"
Brian Silverman552350b2015-08-02 18:23:34 -070038#include "y2014/control_loops/shooter/shooter.q.h"
39#include "y2014/constants.h"
40#include "y2014/queues/auto_mode.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070041
Brian Silverman17f503e2015-08-02 18:17:18 -070042#include "frc971/wpilib/joystick_sender.h"
43#include "frc971/wpilib/loop_output_handler.h"
44#include "frc971/wpilib/buffered_solenoid.h"
45#include "frc971/wpilib/buffered_pcm.h"
46#include "frc971/wpilib/gyro_sender.h"
47#include "frc971/wpilib/dma_edge_counting.h"
48#include "frc971/wpilib/interrupt_edge_counting.h"
49#include "frc971/wpilib/encoder_and_potentiometer.h"
50#include "frc971/wpilib/logging.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050051#include "frc971/wpilib/wpilib_interface.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070052
Brian Silverman17f503e2015-08-02 18:17:18 -070053#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
Brian Silvermanb601d892015-12-20 18:24:38 -050057using ::y2014::control_loops::drivetrain_queue;
58using ::y2014::control_loops::claw_queue;
59using ::y2014::control_loops::shooter_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070060
Brian Silvermanb601d892015-12-20 18:24:38 -050061namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070062namespace wpilib {
63
Brian Silverman85fbb602015-08-29 19:28:20 -070064// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
65// DMA stuff and then removing the * 2.0 in *_translate.
66// The low bit is direction.
67
Brian Silverman552350b2015-08-02 18:23:34 -070068// TODO(brian): Replace this with ::std::make_unique once all our toolchains
69// have support.
70template <class T, class... U>
71std::unique_ptr<T> make_unique(U &&... u) {
72 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
73}
74
Brian Silverman17f503e2015-08-02 18:17:18 -070075double drivetrain_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070076 return -static_cast<double>(in) /
Brian Silverman17f503e2015-08-02 18:17:18 -070077 (256.0 /*cpr*/ * 4.0 /*4x*/) *
78 constants::GetValues().drivetrain_encoder_ratio *
Austin Schuh86f895e2015-11-08 13:40:51 -080079 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070080}
81
Brian Silverman51091a02015-12-26 15:56:58 -080082double drivetrain_velocity_translate(double in) {
83 return (1.0 / in) / 256.0 /*cpr*/ *
84 constants::GetValues().drivetrain_encoder_ratio *
85 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
86}
87
Brian Silverman552350b2015-08-02 18:23:34 -070088float hall_translate(const constants::ShifterHallEffect &k, float in_low,
89 float in_high) {
90 const float low_ratio =
91 0.5 * (in_low - static_cast<float>(k.low_gear_low)) /
92 static_cast<float>(k.low_gear_middle - k.low_gear_low);
93 const float high_ratio =
94 0.5 + 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
95 static_cast<float>(k.high_gear_high - k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070096
Brian Silverman552350b2015-08-02 18:23:34 -070097 // Return low when we are below 1/2, and high when we are above 1/2.
98 if (low_ratio + high_ratio < 1.0) {
99 return low_ratio;
100 } else {
101 return high_ratio;
102 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700103}
104
105double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -0700106 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -0700107 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -0700108 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700109}
110
Brian Silverman552350b2015-08-02 18:23:34 -0700111double shooter_translate(int32_t in) {
112 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
113 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
114 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700115}
116
117static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700118 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
119 18.0 / 32.0 /* big belt reduction */ *
120 18.0 / 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
121 60.0 /* seconds / minute */ * 256.0 /* CPR */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700122
123class SensorReader {
124 public:
125 SensorReader() {
126 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
127 // we should ever see.
Brian Silverman552350b2015-08-02 18:23:34 -0700128 encoder_filter_.SetPeriodNanoSeconds(
Brian Silverman17f503e2015-08-02 18:17:18 -0700129 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
Brian Silverman552350b2015-08-02 18:23:34 -0700130 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700131 }
132
Brian Silverman552350b2015-08-02 18:23:34 -0700133 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
134 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700135 }
136
Brian Silverman552350b2015-08-02 18:23:34 -0700137 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
138 drivetrain_left_encoder_ = ::std::move(encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800139 drivetrain_left_encoder_->SetMaxPeriod(0.005);
Brian Silverman17f503e2015-08-02 18:17:18 -0700140 }
141
Brian Silverman552350b2015-08-02 18:23:34 -0700142 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
143 drivetrain_right_encoder_ = ::std::move(encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800144 drivetrain_right_encoder_->SetMaxPeriod(0.005);
Brian Silverman17f503e2015-08-02 18:17:18 -0700145 }
146
Brian Silverman552350b2015-08-02 18:23:34 -0700147 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
148 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700149 }
150
Brian Silverman552350b2015-08-02 18:23:34 -0700151 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
152 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700153 }
154
Brian Silverman552350b2015-08-02 18:23:34 -0700155 void set_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
156 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700157 }
158
Brian Silverman552350b2015-08-02 18:23:34 -0700159 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
160 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700161 }
162
Brian Silverman552350b2015-08-02 18:23:34 -0700163 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
164 encoder_filter_.Add(encoder.get());
165 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700166 }
167
Austin Schuhc5e36082015-10-31 13:30:46 -0700168 void set_top_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700169 hall_filter_.Add(hall.get());
170 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700171 }
172
Austin Schuhc5e36082015-10-31 13:30:46 -0700173 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700174 hall_filter_.Add(hall.get());
175 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700176 }
177
Austin Schuhc5e36082015-10-31 13:30:46 -0700178 void set_top_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700179 hall_filter_.Add(hall.get());
180 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700181 }
182
Brian Silverman552350b2015-08-02 18:23:34 -0700183 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
184 encoder_filter_.Add(encoder.get());
185 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700186 }
187
Austin Schuhc5e36082015-10-31 13:30:46 -0700188 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700189 hall_filter_.Add(hall.get());
190 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700191 }
192
Austin Schuhc5e36082015-10-31 13:30:46 -0700193 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700194 hall_filter_.Add(hall.get());
195 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700196 }
197
Austin Schuhc5e36082015-10-31 13:30:46 -0700198 void set_bottom_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700199 hall_filter_.Add(hall.get());
200 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700201 }
202
Brian Silverman552350b2015-08-02 18:23:34 -0700203 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
204 encoder_filter_.Add(encoder.get());
205 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700206 }
207
Austin Schuhc5e36082015-10-31 13:30:46 -0700208 void set_shooter_proximal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700209 hall_filter_.Add(hall.get());
210 shooter_proximal_ = ::std::move(hall);
211 }
212
Austin Schuhc5e36082015-10-31 13:30:46 -0700213 void set_shooter_distal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700214 hall_filter_.Add(hall.get());
215 shooter_distal_ = ::std::move(hall);
216 }
217
Austin Schuhc5e36082015-10-31 13:30:46 -0700218 void set_shooter_plunger(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700219 hall_filter_.Add(hall.get());
220 shooter_plunger_ = ::std::move(hall);
221 shooter_plunger_reader_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500222 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_plunger_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700223 }
224
Austin Schuhc5e36082015-10-31 13:30:46 -0700225 void set_shooter_latch(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700226 hall_filter_.Add(hall.get());
227 shooter_latch_ = ::std::move(hall);
Brian Silvermanb601d892015-12-20 18:24:38 -0500228 shooter_latch_reader_ =
229 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700230 }
231
232 // All of the DMA-related set_* calls must be made before this, and it doesn't
233 // hurt to do all of them.
234 void set_dma(::std::unique_ptr<DMA> dma) {
Brian Silvermanb601d892015-12-20 18:24:38 -0500235 shooter_proximal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700236 shooter_encoder_.get(), shooter_proximal_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500237 shooter_distal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700238 shooter_encoder_.get(), shooter_distal_.get());
239
Brian Silvermanb601d892015-12-20 18:24:38 -0500240 dma_synchronizer_.reset(
241 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Brian Silverman552350b2015-08-02 18:23:34 -0700242 dma_synchronizer_->Add(shooter_proximal_counter_.get());
243 dma_synchronizer_->Add(shooter_distal_counter_.get());
244 dma_synchronizer_->Add(shooter_plunger_reader_.get());
245 dma_synchronizer_->Add(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700246 }
247
248 void operator()() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700249 ::aos::SetCurrentThreadName("SensorReader");
250
251 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700252 ds_ =
253#ifdef WPILIB2015
254 DriverStation::GetInstance();
255#else
256 &DriverStation::GetInstance();
257#endif
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500258 pdp_.reset(new PowerDistributionPanel());
Brian Silverman17f503e2015-08-02 18:17:18 -0700259
Brian Silverman552350b2015-08-02 18:23:34 -0700260 top_reader_.Start();
261 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700262 dma_synchronizer_->Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700263
264 ::aos::SetCurrentThreadRealtimePriority(kPriority);
265 while (run_) {
Austin Schuhadf2cde2015-11-08 20:35:16 -0800266 ::aos::time::PhasedLoopXMS(5, 4000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700267 RunIteration();
268 }
269
Brian Silverman552350b2015-08-02 18:23:34 -0700270 top_reader_.Quit();
271 bottom_reader_.Quit();
Brian Silverman17f503e2015-08-02 18:17:18 -0700272 }
273
274 void RunIteration() {
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500275 ::frc971::wpilib::SendRobotState(my_pid_, ds_, pdp_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700276
Brian Silverman552350b2015-08-02 18:23:34 -0700277 const auto &values = constants::GetValues();
278
Brian Silverman17f503e2015-08-02 18:17:18 -0700279 {
280 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
281 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700282 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700283 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700284 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800285 drivetrain_message->left_speed =
286 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
287 drivetrain_message->right_speed =
288 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Austin Schuha0c1e152015-11-08 14:10:13 -0800289
290 drivetrain_message->low_left_hall = low_left_drive_hall_->GetVoltage();
291 drivetrain_message->high_left_hall = high_left_drive_hall_->GetVoltage();
Brian Silverman552350b2015-08-02 18:23:34 -0700292 drivetrain_message->left_shifter_position =
Austin Schuha0c1e152015-11-08 14:10:13 -0800293 hall_translate(values.left_drive, drivetrain_message->low_left_hall,
294 drivetrain_message->high_left_hall);
295
296 drivetrain_message->low_right_hall = low_right_drive_hall_->GetVoltage();
297 drivetrain_message->high_right_hall =
298 high_right_drive_hall_->GetVoltage();
299 drivetrain_message->right_shifter_position =
300 hall_translate(values.right_drive, drivetrain_message->low_right_hall,
301 drivetrain_message->high_right_hall);
Brian Silverman17f503e2015-08-02 18:17:18 -0700302
303 drivetrain_message.Send();
304 }
305
Brian Silvermanb601d892015-12-20 18:24:38 -0500306 ::y2014::sensors::auto_mode.MakeWithBuilder()
Brian Silverman552350b2015-08-02 18:23:34 -0700307 .voltage(auto_selector_analog_->GetVoltage())
308 .Send();
309
Brian Silverman17f503e2015-08-02 18:17:18 -0700310 dma_synchronizer_->RunIteration();
311
Brian Silverman17f503e2015-08-02 18:17:18 -0700312 {
Brian Silverman552350b2015-08-02 18:23:34 -0700313 auto shooter_message = shooter_queue.position.MakeMessage();
314 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
Austin Schuh5c25ab72015-11-01 13:17:11 -0800315 shooter_message->plunger = !shooter_plunger_reader_->value();
316 shooter_message->latch = !shooter_latch_reader_->value();
Brian Silverman552350b2015-08-02 18:23:34 -0700317 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
318 &shooter_message->pusher_proximal);
319 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
320 &shooter_message->pusher_distal);
321
322 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700323 }
324
325 {
326 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700327 top_reader_.RunIteration(&claw_message->top);
328 bottom_reader_.RunIteration(&claw_message->bottom);
329
Brian Silverman17f503e2015-08-02 18:17:18 -0700330 claw_message.Send();
331 }
332 }
333
334 void Quit() { run_ = false; }
335
336 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700337 class HalfClawReader {
338 public:
339 HalfClawReader(bool reversed) : reversed_(reversed) {}
340
341 void set_encoder(::std::unique_ptr<Encoder> encoder) {
342 encoder_ = ::std::move(encoder);
343 }
344
Austin Schuhc5e36082015-10-31 13:30:46 -0700345 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700346 front_hall_ = ::std::move(front_hall);
347 }
348
349 void set_calibration_hall(
Austin Schuhc5e36082015-10-31 13:30:46 -0700350 ::std::unique_ptr<DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700351 calibration_hall_ = ::std::move(calibration_hall);
352 }
353
Austin Schuhc5e36082015-10-31 13:30:46 -0700354 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700355 back_hall_ = ::std::move(back_hall);
356 }
357
358 void Start() {
Brian Silvermanb601d892015-12-20 18:24:38 -0500359 front_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
360 encoder_.get(), front_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700361 synchronizer_.Add(front_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500362 calibration_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
363 encoder_.get(), calibration_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700364 synchronizer_.Add(calibration_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500365 back_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
366 encoder_.get(), back_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700367 synchronizer_.Add(back_counter_.get());
368 synchronized_encoder_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500369 make_unique<::frc971::wpilib::InterruptSynchronizedEncoder>(
370 encoder_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700371 synchronizer_.Add(synchronized_encoder_.get());
372
373 synchronizer_.Start();
374 }
375
376 void Quit() { synchronizer_.Quit(); }
377
378 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
379 const double multiplier = reversed_ ? -1.0 : 1.0;
380
381 synchronizer_.RunIteration();
382
383 CopyPosition(front_counter_.get(), &half_claw_position->front);
384 CopyPosition(calibration_counter_.get(),
385 &half_claw_position->calibration);
386 CopyPosition(back_counter_.get(), &half_claw_position->back);
387 half_claw_position->position =
388 multiplier * claw_translate(synchronized_encoder_->get());
389 }
390
391 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500392 void CopyPosition(const ::frc971::wpilib::EdgeCounter *counter,
393 ::frc971::HallEffectStruct *out) {
Brian Silverman552350b2015-08-02 18:23:34 -0700394 const double multiplier = reversed_ ? -1.0 : 1.0;
395
Austin Schuh5c25ab72015-11-01 13:17:11 -0800396 out->current = !counter->polled_value();
397 out->posedge_count = counter->negative_interrupt_count();
398 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700399 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800400 multiplier * claw_translate(counter->last_positive_encoder_value());
401 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700402 multiplier * claw_translate(counter->last_negative_encoder_value());
403 }
404
Brian Silvermanb601d892015-12-20 18:24:38 -0500405 ::frc971::wpilib::InterruptSynchronizer synchronizer_{kInterruptPriority};
Brian Silverman552350b2015-08-02 18:23:34 -0700406
Brian Silvermanb601d892015-12-20 18:24:38 -0500407 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> front_counter_;
408 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> calibration_counter_;
409 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> back_counter_;
410 ::std::unique_ptr<::frc971::wpilib::InterruptSynchronizedEncoder>
411 synchronized_encoder_;
Brian Silverman552350b2015-08-02 18:23:34 -0700412
413 ::std::unique_ptr<Encoder> encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700414 ::std::unique_ptr<DigitalInput> front_hall_;
415 ::std::unique_ptr<DigitalInput> calibration_hall_;
416 ::std::unique_ptr<DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700417
418 const bool reversed_;
419 };
420
Brian Silverman17f503e2015-08-02 18:17:18 -0700421 static const int kPriority = 30;
422 static const int kInterruptPriority = 55;
423
Brian Silvermanb601d892015-12-20 18:24:38 -0500424 void CopyShooterPosedgeCounts(
425 const ::frc971::wpilib::DMAEdgeCounter *counter,
426 ::frc971::PosedgeOnlyCountedHallEffectStruct *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800427 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700428 // These are inverted because the hall effects give logical false when
429 // there's a magnet in front of them.
430 output->posedge_count = counter->negative_count();
431 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700432 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700433 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700434 }
435
Brian Silverman17f503e2015-08-02 18:17:18 -0700436 int32_t my_pid_;
437 DriverStation *ds_;
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500438 ::std::unique_ptr<PowerDistributionPanel> pdp_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700439
Brian Silvermanb601d892015-12-20 18:24:38 -0500440 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700441
Brian Silverman552350b2015-08-02 18:23:34 -0700442 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700443
Brian Silverman552350b2015-08-02 18:23:34 -0700444 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
445 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
446 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
447 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
448 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
449 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700450
Brian Silverman552350b2015-08-02 18:23:34 -0700451 HalfClawReader top_reader_{false}, bottom_reader_{true};
452
453 ::std::unique_ptr<Encoder> shooter_encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700454 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
455 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500456 ::std::unique_ptr<::frc971::wpilib::DMAEdgeCounter> shooter_proximal_counter_,
Brian Silverman552350b2015-08-02 18:23:34 -0700457 shooter_distal_counter_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500458 ::std::unique_ptr<::frc971::wpilib::DMADigitalReader> shooter_plunger_reader_,
Brian Silverman552350b2015-08-02 18:23:34 -0700459 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700460
461 ::std::atomic<bool> run_{true};
Brian Silverman552350b2015-08-02 18:23:34 -0700462 DigitalGlitchFilter encoder_filter_, hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700463};
464
465class SolenoidWriter {
466 public:
Brian Silvermanb601d892015-12-20 18:24:38 -0500467 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Brian Silverman17f503e2015-08-02 18:17:18 -0700468 : pcm_(pcm),
Brian Silverman552350b2015-08-02 18:23:34 -0700469 shooter_(".frc971.control_loops.shooter_queue.output"),
470 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700471
Austin Schuhc5e36082015-10-31 13:30:46 -0700472 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700473 pressure_switch_ = ::std::move(pressure_switch);
474 }
475
476 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
477 compressor_relay_ = ::std::move(compressor_relay);
478 }
479
Brian Silvermanb601d892015-12-20 18:24:38 -0500480 void set_drivetrain_left(
481 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700482 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700483 }
484
Brian Silvermanb601d892015-12-20 18:24:38 -0500485 void set_drivetrain_right(
486 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700487 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700488 }
489
Brian Silvermanb601d892015-12-20 18:24:38 -0500490 void set_shooter_latch(
491 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700492 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700493 }
494
Brian Silvermanb601d892015-12-20 18:24:38 -0500495 void set_shooter_brake(
496 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700497 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700498 }
499
500 void operator()() {
501 ::aos::SetCurrentThreadName("Solenoids");
502 ::aos::SetCurrentThreadRealtimePriority(30);
503
504 while (run_) {
505 ::aos::time::PhasedLoopXMS(20, 1000);
506
507 {
Brian Silverman552350b2015-08-02 18:23:34 -0700508 shooter_.FetchLatest();
509 if (shooter_.get()) {
510 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
511 shooter_latch_->Set(!shooter_->latch_piston);
512 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700513 }
514 }
515
516 {
Brian Silverman552350b2015-08-02 18:23:34 -0700517 drivetrain_.FetchLatest();
518 if (drivetrain_.get()) {
519 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Austin Schuh86f895e2015-11-08 13:40:51 -0800520 drivetrain_left_->Set(!drivetrain_->left_high);
521 drivetrain_right_->Set(!drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700522 }
523 }
524
Brian Silverman17f503e2015-08-02 18:17:18 -0700525 {
Brian Silvermanb601d892015-12-20 18:24:38 -0500526 ::frc971::wpilib::PneumaticsToLog to_log;
Brian Silverman17f503e2015-08-02 18:17:18 -0700527 {
528 const bool compressor_on = !pressure_switch_->Get();
529 to_log.compressor_on = compressor_on;
530 if (compressor_on) {
531 compressor_relay_->Set(Relay::kForward);
532 } else {
533 compressor_relay_->Set(Relay::kOff);
534 }
535 }
536
537 pcm_->Flush();
538 to_log.read_solenoids = pcm_->GetAll();
539 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
540 }
541 }
542 }
543
544 void Quit() { run_ = false; }
545
546 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500547 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700548
Brian Silvermanb601d892015-12-20 18:24:38 -0500549 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
550 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
551 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
552 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
Brian Silverman552350b2015-08-02 18:23:34 -0700553
Austin Schuhc5e36082015-10-31 13:30:46 -0700554 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700555 ::std::unique_ptr<Relay> compressor_relay_;
556
Brian Silvermanb601d892015-12-20 18:24:38 -0500557 ::aos::Queue<::y2014::control_loops::ShooterQueue::Output> shooter_;
558 ::aos::Queue<::y2014::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700559
560 ::std::atomic<bool> run_{true};
561};
562
Brian Silvermanb601d892015-12-20 18:24:38 -0500563class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700564 public:
565 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
566 left_drivetrain_talon_ = ::std::move(t);
567 }
568
569 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
570 right_drivetrain_talon_ = ::std::move(t);
571 }
572
573 private:
574 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500575 ::y2014::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700576 }
577
578 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500579 auto &queue = ::y2014::control_loops::drivetrain_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700580 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman85fbb602015-08-29 19:28:20 -0700581 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
582 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700583 }
584
585 virtual void Stop() override {
586 LOG(WARNING, "drivetrain output too old\n");
587 left_drivetrain_talon_->Disable();
588 right_drivetrain_talon_->Disable();
589 }
590
591 ::std::unique_ptr<Talon> left_drivetrain_talon_;
592 ::std::unique_ptr<Talon> right_drivetrain_talon_;
593};
594
Brian Silvermanb601d892015-12-20 18:24:38 -0500595class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700596 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700597 void set_shooter_talon(::std::unique_ptr<Talon> t) {
598 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700599 }
600
601 private:
602 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500603 ::y2014::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700604 }
605
606 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500607 auto &queue = ::y2014::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700608 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700609 shooter_talon_->Set(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700610 }
611
612 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700613 LOG(WARNING, "shooter output too old\n");
614 shooter_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700615 }
616
Brian Silverman552350b2015-08-02 18:23:34 -0700617 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700618};
619
Brian Silvermanb601d892015-12-20 18:24:38 -0500620class ClawWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700621 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700622 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
623 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700624 }
625
Brian Silverman552350b2015-08-02 18:23:34 -0700626 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
627 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700628 }
629
Brian Silverman552350b2015-08-02 18:23:34 -0700630 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
631 left_tusk_talon_ = ::std::move(t);
632 }
633
634 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
635 right_tusk_talon_ = ::std::move(t);
636 }
637
638 void set_intake1_talon(::std::unique_ptr<Talon> t) {
639 intake1_talon_ = ::std::move(t);
640 }
641
642 void set_intake2_talon(::std::unique_ptr<Talon> t) {
643 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700644 }
645
646 private:
647 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500648 ::y2014::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700649 }
650
651 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500652 auto &queue = ::y2014::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700653 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700654 intake1_talon_->Set(queue->intake_voltage / 12.0);
655 intake2_talon_->Set(queue->intake_voltage / 12.0);
656 bottom_claw_talon_->Set(-queue->bottom_claw_voltage / 12.0);
657 top_claw_talon_->Set(queue->top_claw_voltage / 12.0);
658 left_tusk_talon_->Set(queue->tusk_voltage / 12.0);
659 right_tusk_talon_->Set(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700660 }
661
662 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700663 LOG(WARNING, "claw output too old\n");
664 intake1_talon_->Disable();
665 intake2_talon_->Disable();
666 bottom_claw_talon_->Disable();
667 top_claw_talon_->Disable();
668 left_tusk_talon_->Disable();
669 right_tusk_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700670 }
671
Brian Silverman552350b2015-08-02 18:23:34 -0700672 ::std::unique_ptr<Talon> top_claw_talon_;
673 ::std::unique_ptr<Talon> bottom_claw_talon_;
674 ::std::unique_ptr<Talon> left_tusk_talon_;
675 ::std::unique_ptr<Talon> right_tusk_talon_;
676 ::std::unique_ptr<Talon> intake1_talon_;
677 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700678};
679
Brian Silverman17f503e2015-08-02 18:17:18 -0700680class WPILibRobot : public RobotBase {
681 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700682 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700683 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
684 Encoder::k4X);
685 }
Brian Silverman552350b2015-08-02 18:23:34 -0700686
Brian Silverman17f503e2015-08-02 18:17:18 -0700687 virtual void StartCompetition() {
688 ::aos::InitNRT();
689 ::aos::SetCurrentThreadName("StartCompetition");
690
Brian Silvermanb601d892015-12-20 18:24:38 -0500691 ::frc971::wpilib::JoystickSender joystick_sender;
Brian Silverman17f503e2015-08-02 18:17:18 -0700692 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700693
694 SensorReader reader;
Brian Silverman17f503e2015-08-02 18:17:18 -0700695
Brian Silverman85fbb602015-08-29 19:28:20 -0700696 // Create this first to make sure it ends up in one of the lower-numbered
697 // FPGA slots so we can use it with DMA.
698 auto shooter_encoder_temp = make_encoder(2);
699
Brian Silverman552350b2015-08-02 18:23:34 -0700700 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700701
Brian Silverman552350b2015-08-02 18:23:34 -0700702 reader.set_drivetrain_left_encoder(make_encoder(0));
703 reader.set_drivetrain_right_encoder(make_encoder(1));
704 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
705 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
706 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
707 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700708
Brian Silverman552350b2015-08-02 18:23:34 -0700709 reader.set_top_claw_encoder(make_encoder(3));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800710 reader.set_top_claw_front_hall(make_unique<DigitalInput>(4)); // R2
711 reader.set_top_claw_calibration_hall(make_unique<DigitalInput>(3)); // R3
712 reader.set_top_claw_back_hall(make_unique<DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700713
Brian Silverman85fbb602015-08-29 19:28:20 -0700714 reader.set_bottom_claw_encoder(make_encoder(4));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800715 reader.set_bottom_claw_front_hall(make_unique<DigitalInput>(1)); // L2
716 reader.set_bottom_claw_calibration_hall(make_unique<DigitalInput>(0)); // L3
717 reader.set_bottom_claw_back_hall(make_unique<DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700718
Brian Silverman85fbb602015-08-29 19:28:20 -0700719 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800720 reader.set_shooter_proximal(make_unique<DigitalInput>(6)); // S1
721 reader.set_shooter_distal(make_unique<DigitalInput>(7)); // S2
722 reader.set_shooter_plunger(make_unique<DigitalInput>(8)); // S3
723 reader.set_shooter_latch(make_unique<DigitalInput>(9)); // S4
Brian Silverman552350b2015-08-02 18:23:34 -0700724
Brian Silverman17f503e2015-08-02 18:17:18 -0700725 reader.set_dma(make_unique<DMA>());
726 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700727
Brian Silvermanb601d892015-12-20 18:24:38 -0500728 ::frc971::wpilib::GyroSender gyro_sender;
Brian Silverman17f503e2015-08-02 18:17:18 -0700729 ::std::thread gyro_thread(::std::ref(gyro_sender));
730
731 DrivetrainWriter drivetrain_writer;
732 drivetrain_writer.set_left_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700733 ::std::unique_ptr<Talon>(new Talon(5)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700734 drivetrain_writer.set_right_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700735 ::std::unique_ptr<Talon>(new Talon(2)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700736 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
737
Brian Silvermanb601d892015-12-20 18:24:38 -0500738 ::y2014::wpilib::ClawWriter claw_writer;
Brian Silverman552350b2015-08-02 18:23:34 -0700739 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
740 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
741 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
742 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
743 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
744 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700745 ::std::thread claw_writer_thread(::std::ref(claw_writer));
746
Brian Silvermanb601d892015-12-20 18:24:38 -0500747 ::y2014::wpilib::ShooterWriter shooter_writer;
Brian Silverman552350b2015-08-02 18:23:34 -0700748 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
749 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
750
Brian Silverman17f503e2015-08-02 18:17:18 -0700751 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
752 new ::frc971::wpilib::BufferedPcm());
753 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700754 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
755 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
756 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
757 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700758
Austin Schuh016a6b02015-10-08 06:41:14 +0000759 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700760 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
761 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
762
763 // Wait forever. Not much else to do...
764 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
765
766 LOG(ERROR, "Exiting WPILibRobot\n");
767
768 joystick_sender.Quit();
769 joystick_thread.join();
770 reader.Quit();
771 reader_thread.join();
772 gyro_sender.Quit();
773 gyro_thread.join();
774
775 drivetrain_writer.Quit();
776 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700777 shooter_writer.Quit();
778 shooter_writer_thread.join();
779 claw_writer.Quit();
780 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700781 solenoid_writer.Quit();
782 solenoid_thread.join();
783
784 ::aos::Cleanup();
785 }
786};
787
788} // namespace wpilib
Brian Silvermanb601d892015-12-20 18:24:38 -0500789} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700790
791
Brian Silvermanb601d892015-12-20 18:24:38 -0500792START_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);