Best practice to check if subscriber has already received data
I want my timer
to start exectuting its callback only after the node's two subscribers received data.
What I am currently doing is setting a bool variable to true in the subscribers' callbacks and then checking if both bools are true in the timer callback:
In the nodes CTOR:
vehicle_position_sub_ =
this->create_subscription<VehicleLocalPosition>(
vehicle_prefix+"fmu/vehicle_local_position/out", 10,
[this](const VehicleLocalPosition::UniquePtr msg) {
vehicle_current_pos_=*msg;
received_vehicle_pos_=true;
});
mission_sub_ =
this->create_subscription<MissionWaypoints>(
"gcs/mission_waypoints", 10,
[this](const MissionWaypoints::UniquePtr msg){
mission_waypoints_=*msg;
received_setpoints_=true;
});
And then in the timer callback:
if(received_vehicle_pos_&&received_setpoints_){
....
}
Now this doesn't seem efficient or clean to me since each subscriber callback will update the bool variables even though it's actually only needed in the very first time. And also the if-statement in the timer callback seems inefficient. Is there a better way to do this?