ROS2 Message Received Callback for Code Generation

4 views (last 30 days)
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.')
end
end
function receivedMessageCallback(msg)
fprintf('MESSAGE RECEIVED!');
end
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
'multirate_tag_sorter_test_simple/receivedMessageCallback'.
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

Accepted Answer

Karthik Reddy Vennapureddy
Hi Michael,
Thank you providing detailed reproduction steps, the following works in MATLAB simulation, but has restrictions in code-generation:
function receivedMessageCallback(msg)
fprintf('MESSAGE RECEIVED!');
end
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 (mathworks.com)
function receivedMessageCallback(src, msg)
fprintf('MESSAGE RECEIVED!');
end
Thanks,
Karthik Reddy
  4 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!