blob: 6442a2a1616c70ff99e1c13c8ae62118f5349197 [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
6#include <thread>
7#include <mutex>
8#include <functional>
9
10#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 "frc971/wpilib/wpilib_robot_base.h"
17#include "dma.h"
18#ifndef WPILIB2015
19#include "DigitalGlitchFilter.h"
20#endif
21#undef ERROR
22
23#include "aos/common/logging/logging.h"
24#include "aos/common/logging/queue_logging.h"
25#include "aos/common/time.h"
26#include "aos/common/util/log_interval.h"
27#include "aos/common/util/phased_loop.h"
28#include "aos/common/util/wrapping_counter.h"
29#include "aos/common/stl_mutex.h"
30#include "aos/linux_code/init.h"
31#include "aos/common/messages/robot_state.q.h"
32
33#include "frc971/shifter_hall_effect.h"
34
35#include "frc971/control_loops/drivetrain/drivetrain.q.h"
36#include "y2014/control_loops/claw/claw.q.h"
37#include "y2014/control_loops/shooter/shooter.q.h"
38#include "y2014/constants.h"
39#include "y2014/queues/auto_mode.q.h"
40
41#include "frc971/wpilib/joystick_sender.h"
42#include "frc971/wpilib/loop_output_handler.h"
43#include "frc971/wpilib/buffered_solenoid.h"
44#include "frc971/wpilib/buffered_pcm.h"
45#include "frc971/wpilib/gyro_sender.h"
46#include "frc971/wpilib/dma_edge_counting.h"
47#include "frc971/wpilib/interrupt_edge_counting.h"
48#include "frc971/wpilib/encoder_and_potentiometer.h"
49#include "frc971/wpilib/logging.q.h"
50#include "frc971/wpilib/wpilib_interface.h"
51#include "frc971/wpilib/pdp_fetcher.h"
52
53#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
57using ::frc971::control_loops::drivetrain_queue;
58using ::y2014::control_loops::claw_queue;
59using ::y2014::control_loops::shooter_queue;
60
61namespace y2014 {
62namespace wpilib {
63
64// 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
68// 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
75double drivetrain_translate(int32_t in) {
76 return -static_cast<double>(in) /
77 (256.0 /*cpr*/ * 4.0 /*4x*/) *
78 constants::GetValues().drivetrain_encoder_ratio *
79 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
80}
81
82double 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
88float 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);
96
97 // 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 }
103}
104
105double claw_translate(int32_t in) {
106 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
107 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
108 (M_PI / 180.0) * 2.0;
109}
110
111double 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*/);
115}
116
117static const double kMaximumEncoderPulsesPerSecond =
118 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 */;
122
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.
128 encoder_filter_.SetPeriodNanoSeconds(
129 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
130 hall_filter_.SetPeriodNanoSeconds(100000);
131 }
132
133 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
134 auto_selector_analog_ = ::std::move(analog);
135 }
136
137 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
138 drivetrain_left_encoder_ = ::std::move(encoder);
139 drivetrain_left_encoder_->SetMaxPeriod(0.005);
140 }
141
142 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
143 drivetrain_right_encoder_ = ::std::move(encoder);
144 drivetrain_right_encoder_->SetMaxPeriod(0.005);
145 }
146
147 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
148 high_left_drive_hall_ = ::std::move(analog);
149 }
150
151 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
152 low_right_drive_hall_ = ::std::move(analog);
153 }
154
155 void set_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
156 high_right_drive_hall_ = ::std::move(analog);
157 }
158
159 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
160 low_left_drive_hall_ = ::std::move(analog);
161 }
162
163 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
164 encoder_filter_.Add(encoder.get());
165 top_reader_.set_encoder(::std::move(encoder));
166 }
167
168 void set_top_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
169 hall_filter_.Add(hall.get());
170 top_reader_.set_front_hall(::std::move(hall));
171 }
172
173 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
174 hall_filter_.Add(hall.get());
175 top_reader_.set_calibration_hall(::std::move(hall));
176 }
177
178 void set_top_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
179 hall_filter_.Add(hall.get());
180 top_reader_.set_back_hall(::std::move(hall));
181 }
182
183 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
184 encoder_filter_.Add(encoder.get());
185 bottom_reader_.set_encoder(::std::move(encoder));
186 }
187
188 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
189 hall_filter_.Add(hall.get());
190 bottom_reader_.set_front_hall(::std::move(hall));
191 }
192
193 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
194 hall_filter_.Add(hall.get());
195 bottom_reader_.set_calibration_hall(::std::move(hall));
196 }
197
198 void set_bottom_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
199 hall_filter_.Add(hall.get());
200 bottom_reader_.set_back_hall(::std::move(hall));
201 }
202
203 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
204 encoder_filter_.Add(encoder.get());
205 shooter_encoder_ = ::std::move(encoder);
206 }
207
208 void set_shooter_proximal(::std::unique_ptr<DigitalInput> hall) {
209 hall_filter_.Add(hall.get());
210 shooter_proximal_ = ::std::move(hall);
211 }
212
213 void set_shooter_distal(::std::unique_ptr<DigitalInput> hall) {
214 hall_filter_.Add(hall.get());
215 shooter_distal_ = ::std::move(hall);
216 }
217
218 void set_shooter_plunger(::std::unique_ptr<DigitalInput> hall) {
219 hall_filter_.Add(hall.get());
220 shooter_plunger_ = ::std::move(hall);
221 shooter_plunger_reader_ =
222 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_plunger_.get());
223 }
224
225 void set_shooter_latch(::std::unique_ptr<DigitalInput> hall) {
226 hall_filter_.Add(hall.get());
227 shooter_latch_ = ::std::move(hall);
228 shooter_latch_reader_ =
229 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_latch_.get());
230 }
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) {
235 shooter_proximal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
236 shooter_encoder_.get(), shooter_proximal_.get());
237 shooter_distal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
238 shooter_encoder_.get(), shooter_distal_.get());
239
240 dma_synchronizer_.reset(
241 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
242 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());
246 }
247
248 void operator()() {
249 ::aos::SetCurrentThreadName("SensorReader");
250
251 my_pid_ = getpid();
252 ds_ =
253#ifdef WPILIB2015
254 DriverStation::GetInstance();
255#else
256 &DriverStation::GetInstance();
257#endif
258
259 top_reader_.Start();
260 bottom_reader_.Start();
261 dma_synchronizer_->Start();
262
263 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
264 ::aos::time::Time::InMS(4));
265
266 ::aos::SetCurrentThreadRealtimePriority(40);
267 while (run_) {
268 {
269 const int iterations = phased_loop.SleepUntilNext();
270 if (iterations != 1) {
271 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
272 }
273 }
274 RunIteration();
275 }
276
277 top_reader_.Quit();
278 bottom_reader_.Quit();
279 }
280
281 void RunIteration() {
282 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
283
284 const auto &values = constants::GetValues();
285
286 {
287 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
288 drivetrain_message->right_encoder =
289 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
290 drivetrain_message->left_encoder =
291 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
292 drivetrain_message->left_speed =
293 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
294 drivetrain_message->right_speed =
295 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
296
297 drivetrain_message->low_left_hall = low_left_drive_hall_->GetVoltage();
298 drivetrain_message->high_left_hall = high_left_drive_hall_->GetVoltage();
299 drivetrain_message->left_shifter_position =
300 hall_translate(values.left_drive, drivetrain_message->low_left_hall,
301 drivetrain_message->high_left_hall);
302
303 drivetrain_message->low_right_hall = low_right_drive_hall_->GetVoltage();
304 drivetrain_message->high_right_hall =
305 high_right_drive_hall_->GetVoltage();
306 drivetrain_message->right_shifter_position =
307 hall_translate(values.right_drive, drivetrain_message->low_right_hall,
308 drivetrain_message->high_right_hall);
309
310 drivetrain_message.Send();
311 }
312
313 ::y2014::sensors::auto_mode.MakeWithBuilder()
314 .voltage(auto_selector_analog_->GetVoltage())
315 .Send();
316
317 dma_synchronizer_->RunIteration();
318
319 {
320 auto shooter_message = shooter_queue.position.MakeMessage();
321 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
322 shooter_message->plunger = !shooter_plunger_reader_->value();
323 shooter_message->latch = !shooter_latch_reader_->value();
324 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
325 &shooter_message->pusher_proximal);
326 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
327 &shooter_message->pusher_distal);
328
329 shooter_message.Send();
330 }
331
332 {
333 auto claw_message = claw_queue.position.MakeMessage();
334 top_reader_.RunIteration(&claw_message->top);
335 bottom_reader_.RunIteration(&claw_message->bottom);
336
337 claw_message.Send();
338 }
339 }
340
341 void Quit() { run_ = false; }
342
343 private:
344 class HalfClawReader {
345 public:
346 HalfClawReader(bool reversed) : reversed_(reversed) {}
347
348 void set_encoder(::std::unique_ptr<Encoder> encoder) {
349 encoder_ = ::std::move(encoder);
350 }
351
352 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
353 front_hall_ = ::std::move(front_hall);
354 }
355
356 void set_calibration_hall(
357 ::std::unique_ptr<DigitalInput> calibration_hall) {
358 calibration_hall_ = ::std::move(calibration_hall);
359 }
360
361 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
362 back_hall_ = ::std::move(back_hall);
363 }
364
365 void Start() {
366 front_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
367 encoder_.get(), front_hall_.get());
368 synchronizer_.Add(front_counter_.get());
369 calibration_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
370 encoder_.get(), calibration_hall_.get());
371 synchronizer_.Add(calibration_counter_.get());
372 back_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
373 encoder_.get(), back_hall_.get());
374 synchronizer_.Add(back_counter_.get());
375 synchronized_encoder_ =
376 make_unique<::frc971::wpilib::InterruptSynchronizedEncoder>(
377 encoder_.get());
378 synchronizer_.Add(synchronized_encoder_.get());
379
380 synchronizer_.Start();
381 }
382
383 void Quit() { synchronizer_.Quit(); }
384
385 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
386 const double multiplier = reversed_ ? -1.0 : 1.0;
387
388 synchronizer_.RunIteration();
389
390 CopyPosition(front_counter_.get(), &half_claw_position->front);
391 CopyPosition(calibration_counter_.get(),
392 &half_claw_position->calibration);
393 CopyPosition(back_counter_.get(), &half_claw_position->back);
394 half_claw_position->position =
395 multiplier * claw_translate(synchronized_encoder_->get());
396 }
397
398 private:
399 void CopyPosition(const ::frc971::wpilib::EdgeCounter *counter,
400 ::frc971::HallEffectStruct *out) {
401 const double multiplier = reversed_ ? -1.0 : 1.0;
402
403 out->current = !counter->polled_value();
404 out->posedge_count = counter->negative_interrupt_count();
405 out->negedge_count = counter->positive_interrupt_count();
406 out->negedge_value =
407 multiplier * claw_translate(counter->last_positive_encoder_value());
408 out->posedge_value =
409 multiplier * claw_translate(counter->last_negative_encoder_value());
410 }
411
412 ::frc971::wpilib::InterruptSynchronizer synchronizer_{55};
413
414 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> front_counter_;
415 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> calibration_counter_;
416 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> back_counter_;
417 ::std::unique_ptr<::frc971::wpilib::InterruptSynchronizedEncoder>
418 synchronized_encoder_;
419
420 ::std::unique_ptr<Encoder> encoder_;
421 ::std::unique_ptr<DigitalInput> front_hall_;
422 ::std::unique_ptr<DigitalInput> calibration_hall_;
423 ::std::unique_ptr<DigitalInput> back_hall_;
424
425 const bool reversed_;
426 };
427
428 void CopyShooterPosedgeCounts(
429 const ::frc971::wpilib::DMAEdgeCounter *counter,
430 ::frc971::PosedgeOnlyCountedHallEffectStruct *output) {
431 output->current = !counter->polled_value();
432 // These are inverted because the hall effects give logical false when
433 // there's a magnet in front of them.
434 output->posedge_count = counter->negative_count();
435 output->negedge_count = counter->positive_count();
436 output->posedge_value =
437 shooter_translate(counter->last_negative_encoder_value());
438 }
439
440 int32_t my_pid_;
441 DriverStation *ds_;
442
443 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
444
445 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
446
447 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
448 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
449 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
450 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
451 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
452 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
453
454 HalfClawReader top_reader_{false}, bottom_reader_{true};
455
456 ::std::unique_ptr<Encoder> shooter_encoder_;
457 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
458 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
459 ::std::unique_ptr<::frc971::wpilib::DMAEdgeCounter> shooter_proximal_counter_,
460 shooter_distal_counter_;
461 ::std::unique_ptr<::frc971::wpilib::DMADigitalReader> shooter_plunger_reader_,
462 shooter_latch_reader_;
463
464 ::std::atomic<bool> run_{true};
465 DigitalGlitchFilter encoder_filter_, hall_filter_;
466};
467
468class SolenoidWriter {
469 public:
470 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
471 : pcm_(pcm),
472 shooter_(".y2014.control_loops.shooter_queue.output"),
473 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
474
475 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
476 pressure_switch_ = ::std::move(pressure_switch);
477 }
478
479 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
480 compressor_relay_ = ::std::move(compressor_relay);
481 }
482
483 void set_drivetrain_left(
484 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
485 drivetrain_left_ = ::std::move(s);
486 }
487
488 void set_drivetrain_right(
489 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
490 drivetrain_right_ = ::std::move(s);
491 }
492
493 void set_shooter_latch(
494 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
495 shooter_latch_ = ::std::move(s);
496 }
497
498 void set_shooter_brake(
499 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
500 shooter_brake_ = ::std::move(s);
501 }
502
503 void operator()() {
504 ::aos::SetCurrentThreadName("Solenoids");
505 ::aos::SetCurrentThreadRealtimePriority(27);
506
507 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(20),
508 ::aos::time::Time::InMS(1));
509
510 while (run_) {
511 {
512 const int iterations = phased_loop.SleepUntilNext();
513 if (iterations != 1) {
514 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
515 }
516 }
517
518 {
519 shooter_.FetchLatest();
520 if (shooter_.get()) {
521 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
522 shooter_latch_->Set(!shooter_->latch_piston);
523 shooter_brake_->Set(!shooter_->brake_piston);
524 }
525 }
526
527 {
528 drivetrain_.FetchLatest();
529 if (drivetrain_.get()) {
530 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
531 drivetrain_left_->Set(!drivetrain_->left_high);
532 drivetrain_right_->Set(!drivetrain_->right_high);
533 }
534 }
535
536 {
537 ::frc971::wpilib::PneumaticsToLog to_log;
538 {
539 const bool compressor_on = !pressure_switch_->Get();
540 to_log.compressor_on = compressor_on;
541 if (compressor_on) {
542 compressor_relay_->Set(Relay::kForward);
543 } else {
544 compressor_relay_->Set(Relay::kOff);
545 }
546 }
547
548 pcm_->Flush();
549 to_log.read_solenoids = pcm_->GetAll();
550 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
551 }
552 }
553 }
554
555 void Quit() { run_ = false; }
556
557 private:
558 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
559
560 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
561 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
562 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
563 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
564
565 ::std::unique_ptr<DigitalInput> pressure_switch_;
566 ::std::unique_ptr<Relay> compressor_relay_;
567
568 ::aos::Queue<::y2014::control_loops::ShooterQueue::Output> shooter_;
569 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
570
571 ::std::atomic<bool> run_{true};
572};
573
574class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
575 public:
576 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
577 left_drivetrain_talon_ = ::std::move(t);
578 }
579
580 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
581 right_drivetrain_talon_ = ::std::move(t);
582 }
583
584 private:
585 virtual void Read() override {
586 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
587 }
588
589 virtual void Write() override {
590 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
591 LOG_STRUCT(DEBUG, "will output", *queue);
592 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
593 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
594 }
595
596 virtual void Stop() override {
597 LOG(WARNING, "drivetrain output too old\n");
598 left_drivetrain_talon_->Disable();
599 right_drivetrain_talon_->Disable();
600 }
601
602 ::std::unique_ptr<Talon> left_drivetrain_talon_;
603 ::std::unique_ptr<Talon> right_drivetrain_talon_;
604};
605
606class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
607 public:
608 void set_shooter_talon(::std::unique_ptr<Talon> t) {
609 shooter_talon_ = ::std::move(t);
610 }
611
612 private:
613 virtual void Read() override {
614 ::y2014::control_loops::shooter_queue.output.FetchAnother();
615 }
616
617 virtual void Write() override {
618 auto &queue = ::y2014::control_loops::shooter_queue.output;
619 LOG_STRUCT(DEBUG, "will output", *queue);
620 shooter_talon_->Set(queue->voltage / 12.0);
621 }
622
623 virtual void Stop() override {
624 LOG(WARNING, "shooter output too old\n");
625 shooter_talon_->Disable();
626 }
627
628 ::std::unique_ptr<Talon> shooter_talon_;
629};
630
631class ClawWriter : public ::frc971::wpilib::LoopOutputHandler {
632 public:
633 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
634 top_claw_talon_ = ::std::move(t);
635 }
636
637 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
638 bottom_claw_talon_ = ::std::move(t);
639 }
640
641 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
642 left_tusk_talon_ = ::std::move(t);
643 }
644
645 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
646 right_tusk_talon_ = ::std::move(t);
647 }
648
649 void set_intake1_talon(::std::unique_ptr<Talon> t) {
650 intake1_talon_ = ::std::move(t);
651 }
652
653 void set_intake2_talon(::std::unique_ptr<Talon> t) {
654 intake2_talon_ = ::std::move(t);
655 }
656
657 private:
658 virtual void Read() override {
659 ::y2014::control_loops::claw_queue.output.FetchAnother();
660 }
661
662 virtual void Write() override {
663 auto &queue = ::y2014::control_loops::claw_queue.output;
664 LOG_STRUCT(DEBUG, "will output", *queue);
665 intake1_talon_->Set(queue->intake_voltage / 12.0);
666 intake2_talon_->Set(queue->intake_voltage / 12.0);
667 bottom_claw_talon_->Set(-queue->bottom_claw_voltage / 12.0);
668 top_claw_talon_->Set(queue->top_claw_voltage / 12.0);
669 left_tusk_talon_->Set(queue->tusk_voltage / 12.0);
670 right_tusk_talon_->Set(-queue->tusk_voltage / 12.0);
671 }
672
673 virtual void Stop() override {
674 LOG(WARNING, "claw output too old\n");
675 intake1_talon_->Disable();
676 intake2_talon_->Disable();
677 bottom_claw_talon_->Disable();
678 top_claw_talon_->Disable();
679 left_tusk_talon_->Disable();
680 right_tusk_talon_->Disable();
681 }
682
683 ::std::unique_ptr<Talon> top_claw_talon_;
684 ::std::unique_ptr<Talon> bottom_claw_talon_;
685 ::std::unique_ptr<Talon> left_tusk_talon_;
686 ::std::unique_ptr<Talon> right_tusk_talon_;
687 ::std::unique_ptr<Talon> intake1_talon_;
688 ::std::unique_ptr<Talon> intake2_talon_;
689};
690
691class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
692 public:
693 ::std::unique_ptr<Encoder> make_encoder(int index) {
694 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
695 Encoder::k4X);
696 }
697
698 void Run() override {
699 ::aos::InitNRT();
700 ::aos::SetCurrentThreadName("StartCompetition");
701
702 ::frc971::wpilib::JoystickSender joystick_sender;
703 ::std::thread joystick_thread(::std::ref(joystick_sender));
704
705 ::frc971::wpilib::PDPFetcher pdp_fetcher;
706 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
707 SensorReader reader;
708
709 // Create this first to make sure it ends up in one of the lower-numbered
710 // FPGA slots so we can use it with DMA.
711 auto shooter_encoder_temp = make_encoder(2);
712
713 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
714
715 reader.set_drivetrain_left_encoder(make_encoder(0));
716 reader.set_drivetrain_right_encoder(make_encoder(1));
717 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
718 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
719 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
720 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
721
722 reader.set_top_claw_encoder(make_encoder(3));
723 reader.set_top_claw_front_hall(make_unique<DigitalInput>(4)); // R2
724 reader.set_top_claw_calibration_hall(make_unique<DigitalInput>(3)); // R3
725 reader.set_top_claw_back_hall(make_unique<DigitalInput>(5)); // R1
726
727 reader.set_bottom_claw_encoder(make_encoder(4));
728 reader.set_bottom_claw_front_hall(make_unique<DigitalInput>(1)); // L2
729 reader.set_bottom_claw_calibration_hall(make_unique<DigitalInput>(0)); // L3
730 reader.set_bottom_claw_back_hall(make_unique<DigitalInput>(2)); // L1
731
732 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
733 reader.set_shooter_proximal(make_unique<DigitalInput>(6)); // S1
734 reader.set_shooter_distal(make_unique<DigitalInput>(7)); // S2
735 reader.set_shooter_plunger(make_unique<DigitalInput>(8)); // S3
736 reader.set_shooter_latch(make_unique<DigitalInput>(9)); // S4
737
738 reader.set_dma(make_unique<DMA>());
739 ::std::thread reader_thread(::std::ref(reader));
740
741 ::frc971::wpilib::GyroSender gyro_sender;
742 ::std::thread gyro_thread(::std::ref(gyro_sender));
743
744 DrivetrainWriter drivetrain_writer;
745 drivetrain_writer.set_left_drivetrain_talon(
746 ::std::unique_ptr<Talon>(new Talon(5)));
747 drivetrain_writer.set_right_drivetrain_talon(
748 ::std::unique_ptr<Talon>(new Talon(2)));
749 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
750
751 ::y2014::wpilib::ClawWriter claw_writer;
752 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
753 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
754 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
755 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
756 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
757 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
758 ::std::thread claw_writer_thread(::std::ref(claw_writer));
759
760 ::y2014::wpilib::ShooterWriter shooter_writer;
761 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
762 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
763
764 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
765 new ::frc971::wpilib::BufferedPcm());
766 SolenoidWriter solenoid_writer(pcm);
767 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
768 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
769 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
770 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
771
772 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
773 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
774 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
775
776 // Wait forever. Not much else to do...
777 while (true) {
778 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
779 if (r != 0) {
780 PLOG(WARNING, "infinite select failed");
781 } else {
782 PLOG(WARNING, "infinite select succeeded??\n");
783 }
784 }
785
786 LOG(ERROR, "Exiting WPILibRobot\n");
787
788 joystick_sender.Quit();
789 joystick_thread.join();
790 pdp_fetcher.Quit();
791 pdp_fetcher_thread.join();
792 reader.Quit();
793 reader_thread.join();
794 gyro_sender.Quit();
795 gyro_thread.join();
796
797 drivetrain_writer.Quit();
798 drivetrain_writer_thread.join();
799 shooter_writer.Quit();
800 shooter_writer_thread.join();
801 claw_writer.Quit();
802 claw_writer_thread.join();
803 solenoid_writer.Quit();
804 solenoid_thread.join();
805
806 ::aos::Cleanup();
807 }
808};
809
810} // namespace wpilib
811} // namespace y2014
812
813
814AOS_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);