ROS2 Message Received Callback for Code Generation

조회 수: 4 (최근 30일)
Michael
Michael 2022년 11월 13일
댓글: Michael 2022년 11월 16일
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

채택된 답변

Karthik Reddy Vennapureddy
Karthik Reddy Vennapureddy 2022년 11월 14일
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
Karthik Reddy Vennapureddy
Karthik Reddy Vennapureddy 2022년 11월 16일
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
Michael
Michael 2022년 11월 16일
Yes he did. Thanks for reponding.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Specialized Messages에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by