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 | |
| 14 | constexpr ::aos::time::Time LoopOutputHandler::kStopTimeout; |
| 15 | |
| 16 | LoopOutputHandler::LoopOutputHandler() : watchdog_(this) {} |
| 17 | |
| 18 | void LoopOutputHandler::operator()() { |
| 19 | ::aos::SetCurrentThreadName("OutputHandler"); |
| 20 | ::std::thread watchdog_thread(::std::ref(watchdog_)); |
| 21 | |
| 22 | ::aos::SetCurrentThreadRealtimePriority(30); |
| 23 | while (run_) { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame^] | 24 | no_joystick_state_.Print(); |
| 25 | fake_joystick_state_.Print(); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 26 | Read(); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame^] | 27 | ::aos::joystick_state.FetchLatest(); |
| 28 | if (!::aos::joystick_state.get()) { |
| 29 | LOG_INTERVAL(no_joystick_state_); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 30 | continue; |
| 31 | } |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame^] | 32 | if (::aos::joystick_state->fake) { |
| 33 | LOG_INTERVAL(fake_joystick_state_); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 34 | continue; |
| 35 | } |
| 36 | |
| 37 | watchdog_.Reset(); |
| 38 | Write(); |
| 39 | } |
| 40 | |
| 41 | Stop(); |
| 42 | |
| 43 | watchdog_.Quit(); |
| 44 | watchdog_thread.join(); |
| 45 | } |
| 46 | |
| 47 | LoopOutputHandler::Watchdog::Watchdog(LoopOutputHandler *handler) |
| 48 | : handler_(handler), |
| 49 | timerfd_(timerfd_create(::aos::time::Time::kDefaultClock, 0)) { |
| 50 | if (timerfd_.get() == -1) { |
| 51 | PLOG(FATAL, "timerfd_create(Time::kDefaultClock, 0)"); |
| 52 | } |
| 53 | ::aos::SetCurrentThreadRealtimePriority(35); |
| 54 | ::aos::SetCurrentThreadName("OutputWatchdog"); |
| 55 | } |
| 56 | |
| 57 | void LoopOutputHandler::Watchdog::operator()() { |
| 58 | uint8_t buf[8]; |
| 59 | while (run_) { |
| 60 | PCHECK(read(timerfd_.get(), buf, sizeof(buf))); |
| 61 | handler_->Stop(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void LoopOutputHandler::Watchdog::Reset() { |
| 66 | itimerspec value = itimerspec(); |
| 67 | value.it_value = kStopTimeout.ToTimespec(); |
| 68 | PCHECK(timerfd_settime(timerfd_.get(), 0, &value, nullptr)); |
| 69 | } |
| 70 | |
| 71 | } // namespace wpilib |
| 72 | } // namespace frc971 |