Skip to content

Commit 8fff1f7

Browse files
tonynajjarTesting
andauthored
fix high CPU usage (#247)
Co-authored-by: Testing <[email protected]>
1 parent 25c894c commit 8fff1f7

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

joy/src/joy.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ Joy::Joy(const rclcpp::NodeOptions & options)
105105

106106
future_ = exit_signal_.get_future();
107107

108+
if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) < 0) {
109+
throw std::runtime_error("SDL could not be initialized: " + std::string(SDL_GetError()));
110+
}
108111
// In theory we could do this with just a timer, which would simplify the code
109112
// a bit. But then we couldn't react to "immediate" events, so we stick with
110113
// the thread.
@@ -423,11 +426,6 @@ void Joy::eventThread()
423426
rclcpp::Time last_publish = this->now();
424427

425428
do {
426-
if (joystick_ == nullptr) {
427-
if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) < 0) {
428-
throw std::runtime_error("SDL could not be initialized: " + std::string(SDL_GetError()));
429-
}
430-
}
431429

432430
bool should_publish = false;
433431
SDL_Event e;
@@ -475,10 +473,6 @@ void Joy::eventThread()
475473
pub_->publish(joy_msg_);
476474
}
477475

478-
if (joystick_ == nullptr) {
479-
SDL_Quit();
480-
}
481-
482476
status = future_.wait_for(std::chrono::seconds(0));
483477
} while (status == std::future_status::timeout);
484478
}

0 commit comments

Comments
 (0)