Difference between receive , LatestMessage and callback function in ros subscriber.

14 visualizaciones (últimos 30 días)
Hi,
I would like to know the difference in between receive, Callback function and LatestMessage in ROS subscriber. I am using ROS MATLAB network communication. I am using three subscriber to subscribe to camera image, bounding box and radar point cloud topics for tracking using Multi object trackers.
I want to know the difference between three and which will be the best solution or fast for tracking.
Thank you.

Respuesta aceptada

Prabeen Sahu
Prabeen Sahu el 11 de Sept. de 2023
Editada: Prabeen Sahu el 11 de Sept. de 2023
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");
% Receive the latest camera image
cameraMsg = receive(cameraSub);
% Receive the latest bounding box information
bboxMsg = receive(bboxSub);
% Receive the latest radar point cloud data
radarMsg = receive(radarSub);
% Process the received messages for tracking
% ...
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");
% Define callback functions
function processCameraMsg(src, msg)
% Process camera image message for tracking
% ...
end
function processBboxMsg(src, msg)
% Process bounding box message for tracking
% ...
end
function processRadarMsg(src, msg)
% Process radar point cloud message for tracking
% ...
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");
% Access the latest camera image
cameraMsg = cameraSub.LatestMessage;
% Access the latest bounding box information
bboxMsg = bboxSub.LatestMessage;
% Access the latest radar point cloud data
radarMsg = radarSub.LatestMessage;
% Process the received messages for tracking
% ...
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

Más respuestas (0)

Categorías

Más información sobre Specialized Messages en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by