blob: 8fd2488819b75007455ed4507c055d8bf4933bab [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
21#include "aos/common/commonmath.h"
22#include "aos/common/logging/logging.h"
23#include "aos/common/logging/queue_logging.h"
24#include "aos/common/messages/robot_state.q.h"
25#include "aos/common/stl_mutex.h"
26#include "aos/common/time.h"
27#include "aos/common/util/compiler_memory_barrier.h"
28#include "aos/common/util/log_interval.h"
29#include "aos/common/util/phased_loop.h"
30#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"
34#include "frc971/wpilib/ADIS16448.h"
35#include "frc971/wpilib/buffered_pcm.h"
36#include "frc971/wpilib/buffered_solenoid.h"
37#include "frc971/wpilib/gyro_sender.h"
38#include "frc971/wpilib/dma.h"
39#include "frc971/wpilib/dma_edge_counting.h"
40#include "frc971/wpilib/encoder_and_potentiometer.h"
41#include "frc971/wpilib/interrupt_edge_counting.h"
42#include "frc971/wpilib/joystick_sender.h"
43#include "frc971/wpilib/logging.q.h"
44#include "frc971/wpilib/loop_output_handler.h"
45#include "frc971/wpilib/pdp_fetcher.h"
46#include "frc971/wpilib/wpilib_interface.h"
47#include "frc971/wpilib/wpilib_robot_base.h"
48
Sabina Davis5ae0c7c2017-10-21 20:51:55 -070049#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Sabina Davisb6b987d2017-10-22 20:50:21 -070050#include "y2017_bot3/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
51#include "y2017_bot3/control_loops/superstructure/superstructure.q.h"
Sabina Davis5ae0c7c2017-10-21 20:51:55 -070052
53#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
57using ::frc971::control_loops::drivetrain_queue;
Sabina Davisb6b987d2017-10-22 20:50:21 -070058using ::y2017_bot3::control_loops::superstructure_queue;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -070059using ::aos::monotonic_clock;
60namespace chrono = ::std::chrono;
61
62namespace y2017_bot3 {
63namespace wpilib {
64namespace {
65
66constexpr double kMaxBringupPower = 12.0;
67
Sabina Davis82b19182017-11-10 09:30:25 -080068constexpr double kDrivetrainCyclesPerRevolution = 128.0;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -070069constexpr double kDrivetrainEncoderCountsPerRevolution =
70 kDrivetrainCyclesPerRevolution * 4;
Sabina Davis82b19182017-11-10 09:30:25 -080071constexpr double kDrivetrainEncoderRatio = 1.0;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -070072constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
73 control_loops::drivetrain::kFreeSpeed *
74 control_loops::drivetrain::kHighOutputRatio /
75 kDrivetrainEncoderRatio *
76 kDrivetrainEncoderCountsPerRevolution;
77
78// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
79// DMA stuff and then removing the * 2.0 in *_translate.
80// The low bit is direction.
81
82// TODO(brian): Replace this with ::std::make_unique once all our toolchains
83// have support.
84template <class T, class... U>
85std::unique_ptr<T> make_unique(U &&... u) {
86 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
87}
88
89// TODO(brian): Use ::std::max instead once we have C++14 so that can be
90// constexpr.
91template <typename T>
92constexpr T max(T a, T b) {
93 return (a > b) ? a : b;
94}
95template <typename T, typename... Rest>
96constexpr T max(T a, T b, T c, Rest... rest) {
97 return max(max(a, b), c, rest...);
98}
99
100double hall_translate(double in) {
101 // Turn voltage from our 3-state halls into a ratio that the loop can use.
102 return in / 5.0;
103}
104
105double drivetrain_translate(int32_t in) {
Sabina Davis82b19182017-11-10 09:30:25 -0800106 return -static_cast<double>(in) / (kDrivetrainCyclesPerRevolution /*cpr*/ * 4.0 /*4x*/) *
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700107 kDrivetrainEncoderRatio *
Sabina Davisb6b987d2017-10-22 20:50:21 -0700108 control_loops::drivetrain::kWheelRadius *
109 2.0 * M_PI;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700110}
111
112double drivetrain_velocity_translate(double in) {
113 return (1.0 / in) / 256.0 /*cpr*/ *
114 kDrivetrainEncoderRatio *
115 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
116}
117
118constexpr double kMaxFastEncoderPulsesPerSecond =
119 kMaxDrivetrainEncoderPulsesPerSecond;
120static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
121 "fast encoders are too fast");
122
123// Class to send position messages with sensor readings to our loops.
124class SensorReader {
125 public:
126 SensorReader() {
127 // Set to filter out anything shorter than 1/4 of the minimum pulse width
128 // we should ever see.
129 fast_encoder_filter_.SetPeriodNanoSeconds(
130 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
131 kMaxFastEncoderPulsesPerSecond * 1e9 +
132 0.5));
133 hall_filter_.SetPeriodNanoSeconds(100000);
134 }
135
136 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
137 fast_encoder_filter_.Add(encoder.get());
138 drivetrain_left_encoder_ = ::std::move(encoder);
139 }
140
141 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
142 fast_encoder_filter_.Add(encoder.get());
143 drivetrain_right_encoder_ = ::std::move(encoder);
144 }
145
146 void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) {
147 drivetrain_left_hall_ = ::std::move(analog);
148 }
149
150 void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) {
151 drivetrain_right_hall_ = ::std::move(analog);
152 }
153
154 void set_pwm_trigger(::std::unique_ptr<DigitalInput> pwm_trigger) {
155 medium_encoder_filter_.Add(pwm_trigger.get());
156 pwm_trigger_ = ::std::move(pwm_trigger);
157 }
158
159 // All of the DMA-related set_* calls must be made before this, and it
160 // doesn't
161 // hurt to do all of them.
162 void set_dma(::std::unique_ptr<DMA> dma) {
163 dma_synchronizer_.reset(
164 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
165 }
166
167 void RunPWMDetecter() {
168 ::aos::SetCurrentThreadRealtimePriority(41);
169
170 pwm_trigger_->RequestInterrupts();
171 // Rising edge only.
172 pwm_trigger_->SetUpSourceEdge(true, false);
173
174 monotonic_clock::time_point last_posedge_monotonic =
175 monotonic_clock::min_time;
176
177 while (run_) {
178 auto ret = pwm_trigger_->WaitForInterrupt(1.0, true);
179 if (ret == InterruptableSensorBase::WaitResult::kRisingEdge) {
180 // Grab all the clocks.
181 const double pwm_fpga_time = pwm_trigger_->ReadRisingTimestamp();
182
183 aos_compiler_memory_barrier();
184 const double fpga_time_before = GetFPGATime() * 1e-6;
185 aos_compiler_memory_barrier();
186 const monotonic_clock::time_point monotonic_now =
187 monotonic_clock::now();
188 aos_compiler_memory_barrier();
189 const double fpga_time_after = GetFPGATime() * 1e-6;
190 aos_compiler_memory_barrier();
191
192 const double fpga_offset =
193 (fpga_time_after + fpga_time_before) / 2.0 - pwm_fpga_time;
194
195 // Compute when the edge was.
196 const monotonic_clock::time_point monotonic_edge =
197 monotonic_now - chrono::duration_cast<chrono::nanoseconds>(
198 chrono::duration<double>(fpga_offset));
199
200 LOG(INFO, "Got PWM pulse %f spread, %f offset, %lld trigger\n",
201 fpga_time_after - fpga_time_before, fpga_offset,
202 monotonic_edge.time_since_epoch().count());
203
204 // Compute bounds on the timestep and sampling times.
205 const double fpga_sample_length = fpga_time_after - fpga_time_before;
206 const chrono::nanoseconds elapsed_time =
207 monotonic_edge - last_posedge_monotonic;
208
209 last_posedge_monotonic = monotonic_edge;
210
211 // Verify that the values are sane.
212 if (fpga_sample_length > 2e-5 || fpga_sample_length < 0) {
213 continue;
214 }
215 if (fpga_offset < 0 || fpga_offset > 0.00015) {
216 continue;
217 }
218 if (elapsed_time >
219 chrono::microseconds(5050) + chrono::microseconds(4) ||
220 elapsed_time <
221 chrono::microseconds(5050) - chrono::microseconds(4)) {
222 continue;
223 }
224 // Good edge!
225 {
226 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
227 last_tick_time_monotonic_timepoint_ = last_posedge_monotonic;
228 last_period_ = elapsed_time;
229 }
230 } else {
231 LOG(INFO, "PWM triggered %d\n", ret);
232 }
233 }
234 pwm_trigger_->CancelInterrupts();
235 }
236
237 void operator()() {
238 ::aos::SetCurrentThreadName("SensorReader");
239
240 my_pid_ = getpid();
241 ds_ = &DriverStation::GetInstance();
242
243 dma_synchronizer_->Start();
244
245 ::aos::time::PhasedLoop phased_loop(last_period_,
246 ::std::chrono::milliseconds(3));
247 chrono::nanoseconds filtered_period = last_period_;
248
249 ::std::thread pwm_detecter_thread(
250 ::std::bind(&SensorReader::RunPWMDetecter, this));
251
252 ::aos::SetCurrentThreadRealtimePriority(40);
253 while (run_) {
254 {
255 const int iterations = phased_loop.SleepUntilNext();
256 if (iterations != 1) {
257 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
258 }
259 }
260 RunIteration();
261
262 monotonic_clock::time_point last_tick_timepoint;
263 chrono::nanoseconds period;
264 {
265 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
266 last_tick_timepoint = last_tick_time_monotonic_timepoint_;
267 period = last_period_;
268 }
269
270 if (last_tick_timepoint == monotonic_clock::min_time) {
271 continue;
272 }
273 chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime(
274 period, last_tick_timepoint + chrono::microseconds(2050));
275
276 // TODO(austin): If this is the first edge in a while, skip to it (plus
277 // an offset). Otherwise, slowly drift time to line up.
278
279 phased_loop.set_interval_and_offset(period, new_offset);
280 }
281 pwm_detecter_thread.join();
282 }
283
284 void RunIteration() {
285 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
286
287 {
288 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
289 drivetrain_message->right_encoder =
Sabina Davis82b19182017-11-10 09:30:25 -0800290 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700291 drivetrain_message->right_speed =
292 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
293
294 drivetrain_message->left_encoder =
Sabina Davis82b19182017-11-10 09:30:25 -0800295 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700296 drivetrain_message->left_speed =
297 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
298
299 drivetrain_message->left_shifter_position =
300 hall_translate(drivetrain_left_hall_->GetVoltage());
301 drivetrain_message->right_shifter_position =
302 hall_translate(drivetrain_right_hall_->GetVoltage());
303
304 drivetrain_message.Send();
305 }
306
Sabina Davisb6b987d2017-10-22 20:50:21 -0700307 {
308 auto superstructure_message = superstructure_queue.position.MakeMessage();
309 superstructure_message.Send();
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700310 }
Sabina Davisb6b987d2017-10-22 20:50:21 -0700311 dma_synchronizer_->RunIteration();
312 }
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700313
314 void Quit() { run_ = false; }
315
316 private:
317 double encoder_translate(int32_t value, double counts_per_revolution,
318 double ratio) {
319 return static_cast<double>(value) / counts_per_revolution * ratio *
320 (2.0 * M_PI);
321 }
322
323 int32_t my_pid_;
324 DriverStation *ds_;
325
326 // Mutex to manage access to the period and tick time variables.
327 ::aos::stl_mutex tick_time_mutex_;
328 monotonic_clock::time_point last_tick_time_monotonic_timepoint_ =
329 monotonic_clock::min_time;
330 chrono::nanoseconds last_period_ = chrono::microseconds(5050);
331
332 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
333
334 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
335 drivetrain_right_encoder_;
336
337 ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_;
338
339 ::std::unique_ptr<DigitalInput> pwm_trigger_;
340
341 ::std::atomic<bool> run_{true};
342
Sabina Davisb6b987d2017-10-22 20:50:21 -0700343 DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_,
344 hall_filter_;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700345};
346
347class SolenoidWriter {
348 public:
349 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
350 : pcm_(pcm),
Sabina Davis82b19182017-11-10 09:30:25 -0800351 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
Sabina Davisb6b987d2017-10-22 20:50:21 -0700352 superstructure_(
353 ".y2017_bot3.control_loops.superstructure_queue.output") {}
354
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700355 void set_compressor(::std::unique_ptr<Compressor> compressor) {
Sabina Davisb6b987d2017-10-22 20:50:21 -0700356 compressor_ = ::std::move(compressor);
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700357 }
358
Sabina Davis82b19182017-11-10 09:30:25 -0800359 void set_left_drivetrain_shifter(
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700360 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Sabina Davis82b19182017-11-10 09:30:25 -0800361 left_drivetrain_shifter_ = ::std::move(s);
362 }
363
364 void set_right_drivetrain_shifter(
365 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
366 right_drivetrain_shifter_ = ::std::move(s);
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700367 }
368
Sabina Davisb6b987d2017-10-22 20:50:21 -0700369 void set_fingers(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
370 fingers_ = ::std::move(s);
371 }
372
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700373 void operator()() {
374 compressor_->Start();
375 ::aos::SetCurrentThreadName("Solenoids");
376 ::aos::SetCurrentThreadRealtimePriority(27);
377
378 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
379 ::std::chrono::milliseconds(1));
380
381 while (run_) {
382 {
383 int iterations = phased_loop.SleepUntilNext();
384 if (iterations != 1) {
385 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
386 }
387 }
388
389 {
390 drivetrain_.FetchLatest();
391 if (drivetrain_.get()) {
392 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Sabina Davis82b19182017-11-10 09:30:25 -0800393 left_drivetrain_shifter_->Set(!drivetrain_->left_high);
394 right_drivetrain_shifter_->Set(!drivetrain_->right_high);
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700395 }
396 }
397
398 {
Sabina Davisb6b987d2017-10-22 20:50:21 -0700399 superstructure_.FetchLatest();
400 if (superstructure_.get()) {
401 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
402 fingers_->Set(superstructure_->fingers_out);
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700403 }
Sabina Davisb6b987d2017-10-22 20:50:21 -0700404 }
405
406 {
407 ::frc971::wpilib::PneumaticsToLog to_log;
408 { to_log.compressor_on = compressor_->Enabled(); }
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700409
410 pcm_->Flush();
411 to_log.read_solenoids = pcm_->GetAll();
412 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
413 }
414 }
415 }
416
417 void Quit() { run_ = false; }
418
419 private:
420 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Sabina Davis82b19182017-11-10 09:30:25 -0800421 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid>
422 left_drivetrain_shifter_;
423 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid>
424 right_drivetrain_shifter_;
Sabina Davisb6b987d2017-10-22 20:50:21 -0700425 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> fingers_;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700426
427 ::std::unique_ptr<Compressor> compressor_;
428
429 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Sabina Davisb6b987d2017-10-22 20:50:21 -0700430 ::aos::Queue<::y2017_bot3::control_loops::SuperstructureQueue::Output>
431 superstructure_;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700432
433 ::std::atomic<bool> run_{true};
434};
435
436class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
437 public:
Sabina Davis82b19182017-11-10 09:30:25 -0800438 void set_drivetrain_left0_victor(::std::unique_ptr<::frc::VictorSP> t) {
439 drivetrain_left0_victor_ = ::std::move(t);
440 }
441 void set_drivetrain_left1_victor(::std::unique_ptr<::frc::VictorSP> t) {
442 drivetrain_left1_victor_ = ::std::move(t);
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700443 }
444
Sabina Davis82b19182017-11-10 09:30:25 -0800445 void set_drivetrain_right0_victor(::std::unique_ptr<::frc::VictorSP> t) {
446 drivetrain_right0_victor_ = ::std::move(t);
447 }
448 void set_drivetrain_right1_victor(::std::unique_ptr<::frc::VictorSP> t) {
449 drivetrain_right1_victor_ = ::std::move(t);
450 }
Sabina Davisb6b987d2017-10-22 20:50:21 -0700451
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700452 private:
453 virtual void Read() override {
454 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
455 }
456
457 virtual void Write() override {
458 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
459 LOG_STRUCT(DEBUG, "will output", *queue);
Sabina Davis82b19182017-11-10 09:30:25 -0800460 drivetrain_left0_victor_->SetSpeed(queue->left_voltage / 12.0);
461 drivetrain_left1_victor_->SetSpeed(queue->left_voltage / 12.0);
462 drivetrain_right0_victor_->SetSpeed(-queue->right_voltage / 12.0);
463 drivetrain_right1_victor_->SetSpeed(-queue->right_voltage / 12.0);
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700464 }
465
466 virtual void Stop() override {
467 LOG(WARNING, "drivetrain output too old\n");
Sabina Davis82b19182017-11-10 09:30:25 -0800468 drivetrain_left0_victor_->SetDisabled();
469 drivetrain_left1_victor_->SetDisabled();
470 drivetrain_right0_victor_->SetDisabled();
471 drivetrain_right1_victor_->SetDisabled();
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700472 }
473
Sabina Davis82b19182017-11-10 09:30:25 -0800474 ::std::unique_ptr<::frc::VictorSP> drivetrain_left0_victor_,
475 drivetrain_left1_victor_, drivetrain_right0_victor_,
476 drivetrain_right1_victor_;
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700477};
478
Sabina Davisb6b987d2017-10-22 20:50:21 -0700479class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
480 public:
481 void set_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
482 rollers_victor_ = ::std::move(t);
483 }
484
Sabina Davis82b19182017-11-10 09:30:25 -0800485 void set_hanger0_victor(::std::unique_ptr<::frc::VictorSP> t) {
486 hanger0_victor_ = ::std::move(t);
487 }
488 void set_hanger1_victor(::std::unique_ptr<::frc::VictorSP> t) {
489 hanger1_victor_ = ::std::move(t);
490 }
491
Sabina Davisb6b987d2017-10-22 20:50:21 -0700492 private:
493 virtual void Read() override {
494 ::y2017_bot3::control_loops::superstructure_queue.output.FetchAnother();
495 }
496
497 virtual void Write() override {
498 auto &queue = ::y2017_bot3::control_loops::superstructure_queue.output;
499 LOG_STRUCT(DEBUG, "will output", *queue);
500 rollers_victor_->SetSpeed(queue->voltage_rollers / 12.0);
Sabina Davis82b19182017-11-10 09:30:25 -0800501 hanger0_victor_->SetSpeed(queue->hanger_voltage / 12.0);
502 hanger1_victor_->SetSpeed(queue->hanger_voltage / 12.0);
Sabina Davisb6b987d2017-10-22 20:50:21 -0700503 }
504
505 virtual void Stop() override {
506 LOG(WARNING, "Superstructure output too old.\n");
507 rollers_victor_->SetDisabled();
Sabina Davis82b19182017-11-10 09:30:25 -0800508 hanger0_victor_->SetDisabled();
509 hanger1_victor_->SetDisabled();
Sabina Davisb6b987d2017-10-22 20:50:21 -0700510 }
511
512 ::std::unique_ptr<::frc::VictorSP> rollers_victor_;
Sabina Davis82b19182017-11-10 09:30:25 -0800513 ::std::unique_ptr<::frc::VictorSP> hanger0_victor_, hanger1_victor_;
Sabina Davisb6b987d2017-10-22 20:50:21 -0700514};
515
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700516class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
517 public:
518 ::std::unique_ptr<Encoder> make_encoder(int index) {
519 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
520 Encoder::k4X);
521 }
522
523 void Run() override {
524 ::aos::InitNRT();
525 ::aos::SetCurrentThreadName("StartCompetition");
526
527 ::frc971::wpilib::JoystickSender joystick_sender;
528 ::std::thread joystick_thread(::std::ref(joystick_sender));
529
530 ::frc971::wpilib::PDPFetcher pdp_fetcher;
531 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
532 SensorReader reader;
533
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700534 reader.set_drivetrain_left_encoder(make_encoder(0));
535 reader.set_drivetrain_right_encoder(make_encoder(1));
536 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(0));
537 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(1));
538
Sabina Davisb6b987d2017-10-22 20:50:21 -0700539 reader.set_pwm_trigger(make_unique<DigitalInput>(0));
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700540 reader.set_dma(make_unique<DMA>());
541 ::std::thread reader_thread(::std::ref(reader));
542
543 ::frc971::wpilib::GyroSender gyro_sender;
544 ::std::thread gyro_thread(::std::ref(gyro_sender));
545
546 DrivetrainWriter drivetrain_writer;
Sabina Davis82b19182017-11-10 09:30:25 -0800547 drivetrain_writer.set_drivetrain_left0_victor(
548 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
549 drivetrain_writer.set_drivetrain_left1_victor(
550 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
551 drivetrain_writer.set_drivetrain_right0_victor(
552 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
553 drivetrain_writer.set_drivetrain_right1_victor(
554 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700555 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
556
Sabina Davisb6b987d2017-10-22 20:50:21 -0700557 SuperstructureWriter superstructure_writer;
558 superstructure_writer.set_rollers_victor(
559 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Sabina Davis82b19182017-11-10 09:30:25 -0800560 superstructure_writer.set_hanger0_victor(
561 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)));
562 superstructure_writer.set_hanger1_victor(
563 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
564 ::std::thread superstructure_writer_thread(::std::ref(superstructure_writer));
Sabina Davisb6b987d2017-10-22 20:50:21 -0700565
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700566 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
567 new ::frc971::wpilib::BufferedPcm());
568 SolenoidWriter solenoid_writer(pcm);
Sabina Davis82b19182017-11-10 09:30:25 -0800569 solenoid_writer.set_left_drivetrain_shifter(pcm->MakeSolenoid(3));
570 solenoid_writer.set_right_drivetrain_shifter(pcm->MakeSolenoid(1));
Sabina Davisb6b987d2017-10-22 20:50:21 -0700571 solenoid_writer.set_fingers(pcm->MakeSolenoid(2));
Sabina Davis5ae0c7c2017-10-21 20:51:55 -0700572
573 solenoid_writer.set_compressor(make_unique<Compressor>());
574
575 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
576
577 // Wait forever. Not much else to do...
578 while (true) {
579 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
580 if (r != 0) {
581 PLOG(WARNING, "infinite select failed");
582 } else {
583 PLOG(WARNING, "infinite select succeeded??\n");
584 }
585 }
586
587 LOG(ERROR, "Exiting WPILibRobot\n");
588
589 joystick_sender.Quit();
590 joystick_thread.join();
591 pdp_fetcher.Quit();
592 pdp_fetcher_thread.join();
593 reader.Quit();
594 reader_thread.join();
595 gyro_sender.Quit();
596 gyro_thread.join();
597
598 drivetrain_writer.Quit();
599 drivetrain_writer_thread.join();
600 solenoid_writer.Quit();
601 solenoid_thread.join();
602
603 ::aos::Cleanup();
604 }
605};
606
607} // namespace
608} // namespace wpilib
609} // namespace y2017_bot3
610
611AOS_ROBOT_CLASS(::y2017_bot3::wpilib::WPILibRobot);