ROS Nodelet receive and processing in parallel
Hallo community,
what is the best way to provide a heavy/slow process always the newest message when the process is ready to start the work again with new data?
My scenario is the following: - long term image processing that is slower as the depth and rgb image publishing rate
So how is it now possible to provide the newest combined message ( depth and rgb image) to the processing at any time the image processing has finished the old job.
Is it necessary to write code that handle the receiving part in a own thread and share the newest combined message with the processing thread or is there already a ROS implementation which handles such problems?
Additional Question
Will it be possible to publish in the processing thread or better how is it possible to publish form the processing thread?
Everything is implemented in a nodelet class like as nlamprian suggests.
For example:
std::shared_ptr<image_transport::ImageTransport> it_;
image_transport::Publisher resultPub_;
it_.reset(new image_transport::ImageTransport(nh));
resultPub_ = it_->advertise("result_image", 1);
std::thread processingThread;
processingThread = std::thread(std::bind(&processingLoop, this));
void processingLoop() {
while (ros::ok()) {
std::unique_lock<std::mutex> lock(img_mutex);
if (has_new_data.load()) {
// do heavy processing
resultPub_,publish(result_msg);
}
has_new_data.store(false);
processing_cv.wait(lock, [&] { return has_new_data.load(); });
}
}
Best,
Manuel