Filter löschen
Filter löschen

ROS2 Message Received Callback for Code Generation

6 Ansichten (letzte 30 Tage)
Michael am 13 Nov. 2022
Kommentiert: Michael am 16 Nov. 2022
The Question
I'm trying to receive messages on a ROS2 network and exectute a callback function when messages are received. I need to deploy this with Code Generation to a C++ executable. When I run my code below in Matlab I get no errors, but when I try code generation I get the error show at the end of this question. I have included a simplified code and the script I used to deploy this code to my target machine. Any idea what the problem is? It seems there is an issue with the inputs to the callback but I am following the example shown in the section Subscribe Using Callback Functions in the Matlab ROS2 subscriber help file. Thanks in advance for any help with this.
The Program
function [] = multirate_tag_sorter_test_simple()
node = ros2node("myNode",0);
myMsg = ros2message("builtin_interfaces/Time");
mySub = ros2subscriber(node,"/myTopic","builtin_interfaces/Time", @receivedMessageCallback);
while true
fprintf('Waiting for messages to arrive...')
[~,~,~] = receive(mySub);
fprintf('Pulse received and processed.')
function receivedMessageCallback(msg)
The Code Generation Script
cfg = coder.config('exe');
cfg.Hardware = coder.hardware('Robot Operating System 2 (ROS 2)');
%cfg.Hardware.BuildAction = 'Build and run';
cfg.Hardware.BuildAction = 'Build and load';
cfg.Hardware.RemoteDeviceAddress = 'XXX.XXX.XXX.XXX';
cfg.Hardware.RemoteDeviceUsername = 'XXXXX';
cfg.Hardware.RemoteDevicePassword = 'XXXXXXX';
cfg.Hardware.DeployTo = 'Remote Device';
cfg.Hardware.ROS2Folder = '/opt/ros/galactic';
cfg.Hardware.ROS2Workspace = '~/uavrt_ws';
cfg.HardwareImplementation.ProdHWDeviceType = 'Intel->x86-64 (Linux 64)';
cfg.RuntimeChecks = true;%Disable for final deployments.
codegen multirate_tag_sorter_test_simple -args {} -config cfg
Error I am seeing:
??? Error calling
This call-site passes more inputs to this function
than it can accept.
Error in ==> ros2subscriber Line: 288 Column: 21
Code generation failed: View Error Report
Error using codegen
Error in multirate_tag_sorter_test_codegen_script (line 22)
codegen multirate_tag_sorter_test_simple -args {} -config cfg

Akzeptierte Antwort

Karthik Reddy Vennapureddy
Karthik Reddy Vennapureddy am 14 Nov. 2022
Hi Michael,
Thank you providing detailed reproduction steps, the following works in MATLAB simulation, but has restrictions in code-generation:
function receivedMessageCallback(msg)
Could you use the below syntax in callback function for code generation, as the code generation requires source event handle as the first argument of the callback function, as mentioned in the following note: Subscribe to messages on a topic - MATLAB (
function receivedMessageCallback(src, msg)
Karthik Reddy
  4 Kommentare
Karthik Reddy Vennapureddy
Karthik Reddy Vennapureddy am 16 Nov. 2022
Hi Michael,
I see that Josh Chen has responded to your question with a workaround in the same post. We are actively addressing the issues encountered with the subscriber callback and will deliver the fix in future updates.
Karthik Reddy
Michael am 16 Nov. 2022
Yes he did. Thanks for reponding.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)


Mehr zu Publishers and Subscribers finden Sie in Help Center und File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by