blob: cb5e4ecba8a5801d5b221156110a8c1389834bed [file] [log] [blame]
Brian Silvermand8f403a2014-12-13 19:12:04 -05001#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
11namespace frc971 {
12namespace wpilib {
13
14constexpr ::aos::time::Time LoopOutputHandler::kStopTimeout;
15
16LoopOutputHandler::LoopOutputHandler() : watchdog_(this) {}
17
18void LoopOutputHandler::operator()() {
19 ::aos::SetCurrentThreadName("OutputHandler");
20 ::std::thread watchdog_thread(::std::ref(watchdog_));
21
22 ::aos::SetCurrentThreadRealtimePriority(30);
23 while (run_) {
Brian Silverman699f0cb2015-02-05 19:45:01 -050024 no_joystick_state_.Print();
25 fake_joystick_state_.Print();
Brian Silvermand8f403a2014-12-13 19:12:04 -050026 Read();
Brian Silverman699f0cb2015-02-05 19:45:01 -050027 ::aos::joystick_state.FetchLatest();
28 if (!::aos::joystick_state.get()) {
29 LOG_INTERVAL(no_joystick_state_);
Brian Silvermand8f403a2014-12-13 19:12:04 -050030 continue;
31 }
Brian Silverman699f0cb2015-02-05 19:45:01 -050032 if (::aos::joystick_state->fake) {
33 LOG_INTERVAL(fake_joystick_state_);
Brian Silvermand8f403a2014-12-13 19:12:04 -050034 continue;
35 }
36
37 watchdog_.Reset();
38 Write();
39 }
40
41 Stop();
42
43 watchdog_.Quit();
44 watchdog_thread.join();
45}
46
47LoopOutputHandler::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
57void 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
65void 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