ROS2 Message Received Callback for Code Generation

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

 采纳的回答

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 个评论

Thanks! It all works now.
I had seen that note when I first read the documentation, but then forgot because in the example in the documentation it has the callback function below that uses (messge) not (src, mesage). You might want to reach out the the team that does the technical writing/documentation and have them update this section of the help file or clarify that the additional arguments are needed for code generation.
function exampleHelperROS2PoseCallback(message)
% Declare global variables to store position and orientation
global pos
global orient
% Extract position and orientation from the ROS message and assign the
% data to the global variables.
pos = [message.linear.x message.linear.y message.linear.z];
orient = [message.angular.x message.angular.y message.angular.z];
end
@Karthik Reddy Vennapureddy I am having issues with passing additional variables to the callback function and posted that as a separate question. If you could answer that as well, I'd appreciated it. That question can be found here:
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.
Thanks,
Karthik Reddy
Yes he did. Thanks for reponding.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Node Generation and Deployment 的更多信息

产品

版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by