From 859445c4425aaf0b96028613459df8a5a2610963 Mon Sep 17 00:00:00 2001 From: Marco Bassa Date: Tue, 19 Mar 2024 17:32:57 +0100 Subject: [PATCH] Revert 'fix high CPU usage (#247)' and adds a sleep instead --- joy/src/joy.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/joy/src/joy.cpp b/joy/src/joy.cpp index c3e8c9df..1cfe9f55 100644 --- a/joy/src/joy.cpp +++ b/joy/src/joy.cpp @@ -105,9 +105,6 @@ Joy::Joy(const rclcpp::NodeOptions & options) future_ = exit_signal_.get_future(); - if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) < 0) { - throw std::runtime_error("SDL could not be initialized: " + std::string(SDL_GetError())); - } // In theory we could do this with just a timer, which would simplify the code // a bit. But then we couldn't react to "immediate" events, so we stick with // the thread. @@ -426,6 +423,13 @@ void Joy::eventThread() rclcpp::Time last_publish = this->now(); do { + if (joystick_ == nullptr) { + std::this_thread::sleep_for(std::chrono::seconds(1)); // Saves CPU time + if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) < 0) { + throw std::runtime_error("SDL could not be initialized: " + std::string(SDL_GetError())); + } + } + bool should_publish = false; SDL_Event e; int wait_time_ms = autorepeat_interval_ms_; @@ -480,6 +484,11 @@ void Joy::eventThread() pub_->publish(joy_msg_); } + if (joystick_ == nullptr) { + std::this_thread::sleep_for(std::chrono::seconds(1)); // Saves CPU time + SDL_Quit(); + } + status = future_.wait_for(std::chrono::seconds(0)); } while (status == std::future_status::timeout); }