Hi Aatif Sulaimani
The differences between receive, callback functions, and LatestMessage when working with subscribers is mentioned below.
receive:
This is a blocking function used to receive the latest message from a specific topic. When you call receive on a subscriber, the function will wait until a message is available on the subscribed topic and return the received message. This function allows you to explicitly control when to receive messages.
cameraSub = rossubscriber('/camera_topic',"DataFormat","struct");
bboxSub = rossubscriber('/bounding_box_topic',"DataFormat","struct");
radarSub = rossubscriber('/radar_point_cloud_topic',"DataFormat","struct");
cameraMsg = receive(cameraSub);
bboxMsg = receive(bboxSub);
radarMsg = receive(radarSub);
In this example, the receive function is used to explicitly wait for and receive the latest messages from each subscriber. The code will block until a new message arrives on each topic, and then the received messages are processed accordingly.
Callback Function:
A callback function is a user-defined function that is automatically called whenever a new message arrives on a subscribed topic. The callback function is associated with the subscriber and is triggered each time a new message arrives. It provides a convenient way to process messages as soon as they are received without explicitly calling a receive function.
cameraSub = rossubscriber('/camera_topic', @processCameraMsg,"DataFormat","struct");
bboxSub = rossubscriber('/bounding_box_topic', @processBboxMsg,"DataFormat","struct");
radarSub = rossubscriber('/radar_point_cloud_topic', @processRadarMsg,"DataFormat","struct");
function processCameraMsg(src, msg)
end
function processBboxMsg(src, msg)
end
function processRadarMsg(src, msg)
end
In this example, callback functions are defined for each subscriber. Whenever a new message arrives on the subscribed topics, the corresponding callback function is automatically called with the received message as an input argument. Inside the callback functions, you can process the messages for tracking purposes.
LatestMessage:
LatestMessage is a property of the subscriber object in MATLAB. It stores the most recent message received on the subscribed topic. You can access this property to retrieve the latest message whenever needed. Unlike the receive function, LatestMessage does not block the execution of your code. It allows you to access the most recent message without waiting for a new one to arrive.
cameraSub = rossubscriber('/camera_topic',"DataFormat","struct");
bboxSub = rossubscriber('/bounding_box_topic',"DataFormat","struct");
radarSub = rossubscriber('/radar_point_cloud_topic',"DataFormat","struct");
cameraMsg = cameraSub.LatestMessage;
bboxMsg = bboxSub.LatestMessage;
radarMsg = radarSub.LatestMessage;
In this example, the LatestMessage property of each subscriber is accessed to retrieve the most recent message without blocking the execution of the code.
When working with multiple subscribers, you can use a combination of these techniques. For example, you can use callback functions to process messages as they arrive, and also access the LatestMessage property to obtain the most recent message at any given time.
-Prabeen