Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 1 | #include "frc971/wpilib/loop_output_handler.h" |
| 2 | |
| 3 | #include <sys/timerfd.h> |
| 4 | |
| 5 | #include <thread> |
| 6 | #include <functional> |
| 7 | |
| 8 | #include "aos/linux_code/init.h" |
| 9 | #include "aos/common/messages/robot_state.q.h" |
| 10 | |
| 11 | namespace frc971 { |
| 12 | namespace wpilib { |
| 13 | |
Austin Schuh | bb227f8 | 2015-09-06 15:27:52 -0700 | [diff] [blame] | 14 | LoopOutputHandler::LoopOutputHandler(const ::aos::time::Time &timeout) |
| 15 | : watchdog_(this, timeout) {} |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 16 | |
| 17 | void LoopOutputHandler::operator()() { |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 18 | ::std::thread watchdog_thread(::std::ref(watchdog_)); |
Brian Silverman | 7778fb0 | 2015-12-27 14:08:13 -0800 | [diff] [blame^] | 19 | ::aos::SetCurrentThreadName("OutputHandler"); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 20 | |
| 21 | ::aos::SetCurrentThreadRealtimePriority(30); |
| 22 | while (run_) { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 23 | no_joystick_state_.Print(); |
| 24 | fake_joystick_state_.Print(); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 25 | Read(); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 26 | ::aos::joystick_state.FetchLatest(); |
| 27 | if (!::aos::joystick_state.get()) { |
| 28 | LOG_INTERVAL(no_joystick_state_); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 29 | continue; |
| 30 | } |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 31 | if (::aos::joystick_state->fake) { |
| 32 | LOG_INTERVAL(fake_joystick_state_); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 33 | continue; |
| 34 | } |
| 35 | |
| 36 | watchdog_.Reset(); |
| 37 | Write(); |
| 38 | } |
| 39 | |
| 40 | Stop(); |
| 41 | |
| 42 | watchdog_.Quit(); |
| 43 | watchdog_thread.join(); |
| 44 | } |
| 45 | |
Austin Schuh | bb227f8 | 2015-09-06 15:27:52 -0700 | [diff] [blame] | 46 | LoopOutputHandler::Watchdog::Watchdog(LoopOutputHandler *handler, |
| 47 | const ::aos::time::Time &timeout) |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 48 | : handler_(handler), |
Austin Schuh | bb227f8 | 2015-09-06 15:27:52 -0700 | [diff] [blame] | 49 | timeout_(timeout), |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 50 | timerfd_(timerfd_create(::aos::time::Time::kDefaultClock, 0)) { |
| 51 | if (timerfd_.get() == -1) { |
| 52 | PLOG(FATAL, "timerfd_create(Time::kDefaultClock, 0)"); |
| 53 | } |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void LoopOutputHandler::Watchdog::operator()() { |
Brian Silverman | 7778fb0 | 2015-12-27 14:08:13 -0800 | [diff] [blame^] | 57 | ::aos::SetCurrentThreadRealtimePriority(35); |
| 58 | ::aos::SetCurrentThreadName("OutputWatchdog"); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 59 | uint8_t buf[8]; |
| 60 | while (run_) { |
| 61 | PCHECK(read(timerfd_.get(), buf, sizeof(buf))); |
| 62 | handler_->Stop(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void LoopOutputHandler::Watchdog::Reset() { |
| 67 | itimerspec value = itimerspec(); |
Austin Schuh | bb227f8 | 2015-09-06 15:27:52 -0700 | [diff] [blame] | 68 | value.it_value = timeout_.ToTimespec(); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 69 | PCHECK(timerfd_settime(timerfd_.get(), 0, &value, nullptr)); |
| 70 | } |
| 71 | |
| 72 | } // namespace wpilib |
| 73 | } // namespace frc971 |