Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions joy/src/joy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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_;
Expand Down Expand Up @@ -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);
}
Expand Down