I will present my problem with an example. I have a callback of Imu messages which I want to integrate on the fly. In a parallel thread I want to access the integrated value from the callback and reset the integration if some condition is met.
Let me present an example.
void Camera::imuCb(const sensor_msgs::Imu::ConstPtr& msg)
{
if(start_integration_) {
// integrate IMU
} else if(reset_integration_) {
// update integrated values in the following variables
Rotation_; // this is Eigen::Matrix3d type and is a member of `camera` class which will be accessed in `camera::foo`
updated_prior_ = true; // set this flag so that we know Rotation matrix is updated in camera::foo
start_integration_ = true; // for next callback in this function
// reset integration and start a fresh
}
}
...
void camera::foo(cv::Mat img)
{
reset_integration_ = true;
start_integration_ = false;
while(ros::ok())
{
// wait until Rotation matrix is updated in the imuCb function
if(prior_updated_) {
prior_updated_ = false; // ok, now it is updated, exit and process the variable
break;
}
}
// do some processing using hte updated matrix `Rotation_`
}
Note that camera::foo
is running in the main thread.
The gist is thus:
Whenever camera::foo
is called I want the integration to stop in the camera::imuCb
and use the integrated Rotation_
in the camera::foo
function.
My doubt is; is what I am doing the right approach? What is the most efficient way to implement this?
Originally posted by anonymous25787 on ROS Answers with karma: 31 on 2020-04-05
Post score: 0
Original comments
Comment by anonymous25787 on 2020-04-05:
Yes, that's something I am trying with right now. Although I don't think memory race would be an issue here since the next time I am going to update Rotation_
is in the next call to camera::foo
.
Comment by anonymous25787 on 2020-04-05:
I just wanted to be sure if what I am implementing is in fact the correct approach or if there were other more efficient approaches.