blob: 4022670bd0f961a9b3b1f331277657a82a20911a [file] [log] [blame]
Sabina Davis5ae0c7c2017-10-21 20:51:55 -07001#include <inttypes.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
5
6#include <array>
7#include <chrono>
8#include <cmath>
9#include <functional>
10#include <mutex>
11#include <thread>
12
13#include "AnalogInput.h"
14#include "DigitalGlitchFilter.h"
15#include "DriverStation.h"
16#include "Encoder.h"
17#include "Compressor.h"
18#include "VictorSP.h"
19#undef ERROR
20
John Park33858a32018-09-28 23:05:48 -070021#include "aos/commonmath.h"
22#include "aos/logging/logging.h"
23#include "aos/logging/queue_logging.h"
24#include "aos/robot_state/robot_state.q.h"
25#include "aos/stl_mutex/stl_mutex.h"
26#include "aos/time/time.h"
27#include "aos/util/compiler_memory_barrier.h"
28#include "aos/util/log_interval.h"
29#include "aos/util/phased_loop.h"
Sabina Davis5ae0c7c2017-10-21 20:51:55 -070030#include "aos/linux_code/init.h"
31
32#include "frc971/control_loops/control_loops.q.h"
33#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Sabina Davis5ae0c7c2017-10-21 20:51:55 -070034#include "frc971/wpilib/buffered_pcm.h"
35#include "frc971/wpilib/buffered_solenoid.h"
36#include "frc971/wpilib/gyro_sender.h"
37#include "frc971/wpilib/dma.h"
38#include "frc971/wpilib/dma_edge_counting.h"
39#include "frc971/wpilib/encoder_and_potentiometer.h"
40#include "frc971/wpilib/interrupt_edge_counting.h"
41#include "frc971/wpilib/joystick_sender.h"
42#include "frc971/wpilib/logging.q.h"
43#include "frc971/wpilib/loop_output_handler.h"
44#include "frc971/wpilib/pdp_fetcher.h"
45#include "frc971/wpilib/wpilib_interface.h"
46#include "frc971/wpilib/wpilib_robot_base.h"
47
Sabina Davis5ae0c7c2017-10-21 20:51:55 -070048#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Sabina Davisb6b987d2017-10-22 20:50:21 -070049#include "y2017_bot3/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
50#include "y2017_bot3/control_loops/superstructure/superstructure.q.h"
Sabina Davis5ae0c7c2017-10-21 20:51:55 -070051
52#ifndef M_PI
53#define M_PI 3.14159265358979323846
54#endif
55
56using ::frc971::control_loops::drivetrain_queue;
Sabina Davisb6b987d2017-10-22 20:50:21 -070057using ::y2017_bot3::control_loops::superstructure_queue;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -070058using ::aos::monotonic_clock;
59namespace chrono = ::std::chrono;
60
61namespace y2017_bot3 {
62namespace wpilib {
63namespace {
64
65constexpr double kMaxBringupPower = 12.0;
66
Sabina Davis82b19182017-11-10 09:30:25 -080067constexpr double kDrivetrainCyclesPerRevolution = 128.0;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -070068constexpr double kDrivetrainEncoderCountsPerRevolution =
69 kDrivetrainCyclesPerRevolution * 4;
Sabina Davis82b19182017-11-10 09:30:25 -080070constexpr double kDrivetrainEncoderRatio = 1.0;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -070071constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
72 control_loops::drivetrain::kFreeSpeed *
73 control_loops::drivetrain::kHighOutputRatio /
74 kDrivetrainEncoderRatio *
75 kDrivetrainEncoderCountsPerRevolution;
76
77// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
78// DMA stuff and then removing the * 2.0 in *_translate.
79// The low bit is direction.
80
81// TODO(brian): Replace this with ::std::make_unique once all our toolchains
82// have support.
83template <class T, class... U>
84std::unique_ptr<T> make_unique(U &&... u) {
85 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
86}
87
88// TODO(brian): Use ::std::max instead once we have C++14 so that can be
89// constexpr.
90template <typename T>
91constexpr T max(T a, T b) {
92 return (a > b) ? a : b;
93}
94template <typename T, typename... Rest>
95constexpr T max(T a, T b, T c, Rest... rest) {
96 return max(max(a, b), c, rest...);
97}
98
99double hall_translate(double in) {
100 // Turn voltage from our 3-state halls into a ratio that the loop can use.
101 return in / 5.0;
102}
103
104double drivetrain_translate(int32_t in) {
Sabina Davis82b19182017-11-10 09:30:25 -0800105 return -static_cast<double>(in) / (kDrivetrainCyclesPerRevolution /*cpr*/ * 4.0 /*4x*/) *
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700106 kDrivetrainEncoderRatio *
Sabina Davisb6b987d2017-10-22 20:50:21 -0700107 control_loops::drivetrain::kWheelRadius *
108 2.0 * M_PI;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700109}
110
111double drivetrain_velocity_translate(double in) {
112 return (1.0 / in) / 256.0 /*cpr*/ *
113 kDrivetrainEncoderRatio *
114 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
115}
116
117constexpr double kMaxFastEncoderPulsesPerSecond =
118 kMaxDrivetrainEncoderPulsesPerSecond;
119static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
120 "fast encoders are too fast");
121
122// Class to send position messages with sensor readings to our loops.
123class SensorReader {
124 public:
125 SensorReader() {
126 // Set to filter out anything shorter than 1/4 of the minimum pulse width
127 // we should ever see.
128 fast_encoder_filter_.SetPeriodNanoSeconds(
129 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
130 kMaxFastEncoderPulsesPerSecond * 1e9 +
131 0.5));
132 hall_filter_.SetPeriodNanoSeconds(100000);
133 }
134
135 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
136 fast_encoder_filter_.Add(encoder.get());
137 drivetrain_left_encoder_ = ::std::move(encoder);
138 }
139
140 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
141 fast_encoder_filter_.Add(encoder.get());
142 drivetrain_right_encoder_ = ::std::move(encoder);
143 }
144
145 void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) {
146 drivetrain_left_hall_ = ::std::move(analog);
147 }
148
149 void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) {
150 drivetrain_right_hall_ = ::std::move(analog);
151 }
152
153 void set_pwm_trigger(::std::unique_ptr<DigitalInput> pwm_trigger) {
154 medium_encoder_filter_.Add(pwm_trigger.get());
155 pwm_trigger_ = ::std::move(pwm_trigger);
156 }
157
158 // All of the DMA-related set_* calls must be made before this, and it
159 // doesn't
160 // hurt to do all of them.
161 void set_dma(::std::unique_ptr<DMA> dma) {
162 dma_synchronizer_.reset(
163 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
164 }
165
166 void RunPWMDetecter() {
167 ::aos::SetCurrentThreadRealtimePriority(41);
168
169 pwm_trigger_->RequestInterrupts();
170 // Rising edge only.
171 pwm_trigger_->SetUpSourceEdge(true, false);
172
173 monotonic_clock::time_point last_posedge_monotonic =
174 monotonic_clock::min_time;
175
176 while (run_) {
177 auto ret = pwm_trigger_->WaitForInterrupt(1.0, true);
178 if (ret == InterruptableSensorBase::WaitResult::kRisingEdge) {
179 // Grab all the clocks.
180 const double pwm_fpga_time = pwm_trigger_->ReadRisingTimestamp();
181
182 aos_compiler_memory_barrier();
183 const double fpga_time_before = GetFPGATime() * 1e-6;
184 aos_compiler_memory_barrier();
185 const monotonic_clock::time_point monotonic_now =
186 monotonic_clock::now();
187 aos_compiler_memory_barrier();
188 const double fpga_time_after = GetFPGATime() * 1e-6;
189 aos_compiler_memory_barrier();
190
191 const double fpga_offset =
192 (fpga_time_after + fpga_time_before) / 2.0 - pwm_fpga_time;
193
194 // Compute when the edge was.
195 const monotonic_clock::time_point monotonic_edge =
196 monotonic_now - chrono::duration_cast<chrono::nanoseconds>(
197 chrono::duration<double>(fpga_offset));
198
199 LOG(INFO, "Got PWM pulse %f spread, %f offset, %lld trigger\n",
200 fpga_time_after - fpga_time_before, fpga_offset,
201 monotonic_edge.time_since_epoch().count());
202
203 // Compute bounds on the timestep and sampling times.
204 const double fpga_sample_length = fpga_time_after - fpga_time_before;
205 const chrono::nanoseconds elapsed_time =
206 monotonic_edge - last_posedge_monotonic;
207
208 last_posedge_monotonic = monotonic_edge;
209
210 // Verify that the values are sane.
211 if (fpga_sample_length > 2e-5 || fpga_sample_length < 0) {
212 continue;
213 }
214 if (fpga_offset < 0 || fpga_offset > 0.00015) {
215 continue;
216 }
217 if (elapsed_time >
218 chrono::microseconds(5050) + chrono::microseconds(4) ||
219 elapsed_time <
220 chrono::microseconds(5050) - chrono::microseconds(4)) {
221 continue;
222 }
223 // Good edge!
224 {
225 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
226 last_tick_time_monotonic_timepoint_ = last_posedge_monotonic;
227 last_period_ = elapsed_time;
228 }
229 } else {
230 LOG(INFO, "PWM triggered %d\n", ret);
231 }
232 }
233 pwm_trigger_->CancelInterrupts();
234 }
235
236 void operator()() {
237 ::aos::SetCurrentThreadName("SensorReader");
238
239 my_pid_ = getpid();
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700240
241 dma_synchronizer_->Start();
242
243 ::aos::time::PhasedLoop phased_loop(last_period_,
244 ::std::chrono::milliseconds(3));
245 chrono::nanoseconds filtered_period = last_period_;
246
247 ::std::thread pwm_detecter_thread(
248 ::std::bind(&SensorReader::RunPWMDetecter, this));
249
250 ::aos::SetCurrentThreadRealtimePriority(40);
251 while (run_) {
252 {
253 const int iterations = phased_loop.SleepUntilNext();
254 if (iterations != 1) {
255 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
256 }
257 }
258 RunIteration();
259
260 monotonic_clock::time_point last_tick_timepoint;
261 chrono::nanoseconds period;
262 {
263 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
264 last_tick_timepoint = last_tick_time_monotonic_timepoint_;
265 period = last_period_;
266 }
267
268 if (last_tick_timepoint == monotonic_clock::min_time) {
269 continue;
270 }
271 chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime(
272 period, last_tick_timepoint + chrono::microseconds(2050));
273
274 // TODO(austin): If this is the first edge in a while, skip to it (plus
275 // an offset). Otherwise, slowly drift time to line up.
276
277 phased_loop.set_interval_and_offset(period, new_offset);
278 }
279 pwm_detecter_thread.join();
280 }
281
282 void RunIteration() {
Austin Schuh94f51e92017-10-30 19:25:32 -0700283 ::frc971::wpilib::SendRobotState(my_pid_);
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700284
285 {
286 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
287 drivetrain_message->right_encoder =
Sabina Davis82b19182017-11-10 09:30:25 -0800288 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700289 drivetrain_message->right_speed =
290 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
291
292 drivetrain_message->left_encoder =
Sabina Davis82b19182017-11-10 09:30:25 -0800293 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700294 drivetrain_message->left_speed =
295 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
296
297 drivetrain_message->left_shifter_position =
298 hall_translate(drivetrain_left_hall_->GetVoltage());
299 drivetrain_message->right_shifter_position =
300 hall_translate(drivetrain_right_hall_->GetVoltage());
301
302 drivetrain_message.Send();
303 }
304
Sabina Davisb6b987d2017-10-22 20:50:21 -0700305 {
306 auto superstructure_message = superstructure_queue.position.MakeMessage();
307 superstructure_message.Send();
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700308 }
Sabina Davisb6b987d2017-10-22 20:50:21 -0700309 dma_synchronizer_->RunIteration();
310 }
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700311
312 void Quit() { run_ = false; }
313
314 private:
315 double encoder_translate(int32_t value, double counts_per_revolution,
316 double ratio) {
317 return static_cast<double>(value) / counts_per_revolution * ratio *
318 (2.0 * M_PI);
319 }
320
321 int32_t my_pid_;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700322
323 // Mutex to manage access to the period and tick time variables.
324 ::aos::stl_mutex tick_time_mutex_;
325 monotonic_clock::time_point last_tick_time_monotonic_timepoint_ =
326 monotonic_clock::min_time;
327 chrono::nanoseconds last_period_ = chrono::microseconds(5050);
328
329 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
330
331 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
332 drivetrain_right_encoder_;
333
334 ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_;
335
336 ::std::unique_ptr<DigitalInput> pwm_trigger_;
337
338 ::std::atomic<bool> run_{true};
339
Sabina Davisb6b987d2017-10-22 20:50:21 -0700340 DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_,
341 hall_filter_;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700342};
343
344class SolenoidWriter {
345 public:
346 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
347 : pcm_(pcm),
Sabina Davis82b19182017-11-10 09:30:25 -0800348 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
Sabina Davisb6b987d2017-10-22 20:50:21 -0700349 superstructure_(
350 ".y2017_bot3.control_loops.superstructure_queue.output") {}
351
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700352 void set_compressor(::std::unique_ptr<Compressor> compressor) {
Sabina Davisb6b987d2017-10-22 20:50:21 -0700353 compressor_ = ::std::move(compressor);
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700354 }
355
Sabina Davis82b19182017-11-10 09:30:25 -0800356 void set_left_drivetrain_shifter(
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700357 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Sabina Davis82b19182017-11-10 09:30:25 -0800358 left_drivetrain_shifter_ = ::std::move(s);
359 }
360
361 void set_right_drivetrain_shifter(
362 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
363 right_drivetrain_shifter_ = ::std::move(s);
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700364 }
365
Sabina Davisb6b987d2017-10-22 20:50:21 -0700366 void set_fingers(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
367 fingers_ = ::std::move(s);
368 }
369
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700370 void operator()() {
371 compressor_->Start();
372 ::aos::SetCurrentThreadName("Solenoids");
373 ::aos::SetCurrentThreadRealtimePriority(27);
374
375 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
376 ::std::chrono::milliseconds(1));
377
378 while (run_) {
379 {
380 int iterations = phased_loop.SleepUntilNext();
381 if (iterations != 1) {
382 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
383 }
384 }
385
386 {
387 drivetrain_.FetchLatest();
388 if (drivetrain_.get()) {
389 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Sabina Davis82b19182017-11-10 09:30:25 -0800390 left_drivetrain_shifter_->Set(!drivetrain_->left_high);
391 right_drivetrain_shifter_->Set(!drivetrain_->right_high);
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700392 }
393 }
394
395 {
Sabina Davisb6b987d2017-10-22 20:50:21 -0700396 superstructure_.FetchLatest();
397 if (superstructure_.get()) {
398 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
399 fingers_->Set(superstructure_->fingers_out);
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700400 }
Sabina Davisb6b987d2017-10-22 20:50:21 -0700401 }
402
403 {
404 ::frc971::wpilib::PneumaticsToLog to_log;
405 { to_log.compressor_on = compressor_->Enabled(); }
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700406
407 pcm_->Flush();
408 to_log.read_solenoids = pcm_->GetAll();
409 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
410 }
411 }
412 }
413
414 void Quit() { run_ = false; }
415
416 private:
417 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Sabina Davis82b19182017-11-10 09:30:25 -0800418 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid>
419 left_drivetrain_shifter_;
420 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid>
421 right_drivetrain_shifter_;
Sabina Davisb6b987d2017-10-22 20:50:21 -0700422 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> fingers_;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700423
424 ::std::unique_ptr<Compressor> compressor_;
425
426 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Sabina Davisb6b987d2017-10-22 20:50:21 -0700427 ::aos::Queue<::y2017_bot3::control_loops::SuperstructureQueue::Output>
428 superstructure_;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700429
430 ::std::atomic<bool> run_{true};
431};
432
433class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
434 public:
Sabina Davis82b19182017-11-10 09:30:25 -0800435 void set_drivetrain_left0_victor(::std::unique_ptr<::frc::VictorSP> t) {
436 drivetrain_left0_victor_ = ::std::move(t);
437 }
438 void set_drivetrain_left1_victor(::std::unique_ptr<::frc::VictorSP> t) {
439 drivetrain_left1_victor_ = ::std::move(t);
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700440 }
441
Sabina Davis82b19182017-11-10 09:30:25 -0800442 void set_drivetrain_right0_victor(::std::unique_ptr<::frc::VictorSP> t) {
443 drivetrain_right0_victor_ = ::std::move(t);
444 }
445 void set_drivetrain_right1_victor(::std::unique_ptr<::frc::VictorSP> t) {
446 drivetrain_right1_victor_ = ::std::move(t);
447 }
Sabina Davisb6b987d2017-10-22 20:50:21 -0700448
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700449 private:
450 virtual void Read() override {
451 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
452 }
453
454 virtual void Write() override {
455 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
456 LOG_STRUCT(DEBUG, "will output", *queue);
Sabina Davis82b19182017-11-10 09:30:25 -0800457 drivetrain_left0_victor_->SetSpeed(queue->left_voltage / 12.0);
458 drivetrain_left1_victor_->SetSpeed(queue->left_voltage / 12.0);
459 drivetrain_right0_victor_->SetSpeed(-queue->right_voltage / 12.0);
460 drivetrain_right1_victor_->SetSpeed(-queue->right_voltage / 12.0);
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700461 }
462
463 virtual void Stop() override {
464 LOG(WARNING, "drivetrain output too old\n");
Sabina Davis82b19182017-11-10 09:30:25 -0800465 drivetrain_left0_victor_->SetDisabled();
466 drivetrain_left1_victor_->SetDisabled();
467 drivetrain_right0_victor_->SetDisabled();
468 drivetrain_right1_victor_->SetDisabled();
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700469 }
470
Sabina Davis82b19182017-11-10 09:30:25 -0800471 ::std::unique_ptr<::frc::VictorSP> drivetrain_left0_victor_,
472 drivetrain_left1_victor_, drivetrain_right0_victor_,
473 drivetrain_right1_victor_;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700474};
475
Sabina Davisb6b987d2017-10-22 20:50:21 -0700476class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
477 public:
478 void set_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
479 rollers_victor_ = ::std::move(t);
480 }
481
Sabina Davis82b19182017-11-10 09:30:25 -0800482 void set_hanger0_victor(::std::unique_ptr<::frc::VictorSP> t) {
483 hanger0_victor_ = ::std::move(t);
484 }
485 void set_hanger1_victor(::std::unique_ptr<::frc::VictorSP> t) {
486 hanger1_victor_ = ::std::move(t);
487 }
488
Sabina Davisb6b987d2017-10-22 20:50:21 -0700489 private:
490 virtual void Read() override {
491 ::y2017_bot3::control_loops::superstructure_queue.output.FetchAnother();
492 }
493
494 virtual void Write() override {
495 auto &queue = ::y2017_bot3::control_loops::superstructure_queue.output;
496 LOG_STRUCT(DEBUG, "will output", *queue);
497 rollers_victor_->SetSpeed(queue->voltage_rollers / 12.0);
Sabina Davis82b19182017-11-10 09:30:25 -0800498 hanger0_victor_->SetSpeed(queue->hanger_voltage / 12.0);
499 hanger1_victor_->SetSpeed(queue->hanger_voltage / 12.0);
Sabina Davisb6b987d2017-10-22 20:50:21 -0700500 }
501
502 virtual void Stop() override {
503 LOG(WARNING, "Superstructure output too old.\n");
504 rollers_victor_->SetDisabled();
Sabina Davis82b19182017-11-10 09:30:25 -0800505 hanger0_victor_->SetDisabled();
506 hanger1_victor_->SetDisabled();
Sabina Davisb6b987d2017-10-22 20:50:21 -0700507 }
508
509 ::std::unique_ptr<::frc::VictorSP> rollers_victor_;
Sabina Davis82b19182017-11-10 09:30:25 -0800510 ::std::unique_ptr<::frc::VictorSP> hanger0_victor_, hanger1_victor_;
Sabina Davisb6b987d2017-10-22 20:50:21 -0700511};
512
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700513class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
514 public:
515 ::std::unique_ptr<Encoder> make_encoder(int index) {
516 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
517 Encoder::k4X);
518 }
519
520 void Run() override {
521 ::aos::InitNRT();
522 ::aos::SetCurrentThreadName("StartCompetition");
523
524 ::frc971::wpilib::JoystickSender joystick_sender;
525 ::std::thread joystick_thread(::std::ref(joystick_sender));
526
527 ::frc971::wpilib::PDPFetcher pdp_fetcher;
528 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
529 SensorReader reader;
530
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700531 reader.set_drivetrain_left_encoder(make_encoder(0));
532 reader.set_drivetrain_right_encoder(make_encoder(1));
533 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(0));
534 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(1));
535
Sabina Davisb6b987d2017-10-22 20:50:21 -0700536 reader.set_pwm_trigger(make_unique<DigitalInput>(0));
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700537 reader.set_dma(make_unique<DMA>());
538 ::std::thread reader_thread(::std::ref(reader));
539
540 ::frc971::wpilib::GyroSender gyro_sender;
541 ::std::thread gyro_thread(::std::ref(gyro_sender));
542
543 DrivetrainWriter drivetrain_writer;
Sabina Davis82b19182017-11-10 09:30:25 -0800544 drivetrain_writer.set_drivetrain_left0_victor(
545 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
546 drivetrain_writer.set_drivetrain_left1_victor(
547 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
548 drivetrain_writer.set_drivetrain_right0_victor(
549 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
550 drivetrain_writer.set_drivetrain_right1_victor(
551 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700552 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
553
Sabina Davisb6b987d2017-10-22 20:50:21 -0700554 SuperstructureWriter superstructure_writer;
555 superstructure_writer.set_rollers_victor(
556 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Sabina Davis82b19182017-11-10 09:30:25 -0800557 superstructure_writer.set_hanger0_victor(
558 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)));
559 superstructure_writer.set_hanger1_victor(
560 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
561 ::std::thread superstructure_writer_thread(::std::ref(superstructure_writer));
Sabina Davisb6b987d2017-10-22 20:50:21 -0700562
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700563 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
564 new ::frc971::wpilib::BufferedPcm());
565 SolenoidWriter solenoid_writer(pcm);
Sabina Davis82b19182017-11-10 09:30:25 -0800566 solenoid_writer.set_left_drivetrain_shifter(pcm->MakeSolenoid(3));
567 solenoid_writer.set_right_drivetrain_shifter(pcm->MakeSolenoid(1));
Sabina Davisb6b987d2017-10-22 20:50:21 -0700568 solenoid_writer.set_fingers(pcm->MakeSolenoid(2));
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700569
570 solenoid_writer.set_compressor(make_unique<Compressor>());
571
572 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
573
574 // Wait forever. Not much else to do...
575 while (true) {
576 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
577 if (r != 0) {
578 PLOG(WARNING, "infinite select failed");
579 } else {
580 PLOG(WARNING, "infinite select succeeded??\n");
581 }
582 }
583
584 LOG(ERROR, "Exiting WPILibRobot\n");
585
586 joystick_sender.Quit();
587 joystick_thread.join();
588 pdp_fetcher.Quit();
589 pdp_fetcher_thread.join();
590 reader.Quit();
591 reader_thread.join();
592 gyro_sender.Quit();
593 gyro_thread.join();
594
595 drivetrain_writer.Quit();
596 drivetrain_writer_thread.join();
597 solenoid_writer.Quit();
598 solenoid_thread.join();
599
600 ::aos::Cleanup();
601 }
602};
603
604} // namespace
605} // namespace wpilib
606} // namespace y2017_bot3
607
608AOS_ROBOT_CLASS(::y2017_bot3::wpilib::WPILibRobot);