How do I properly generate code to set the topic name for a ROS 2 publisher using the value of the getParameter(...) function?

조회 수: 1 (최근 30일)
A basic test example:
function testROS2Node()
% Create ROS 2 Node
nodeParams.topicName = 'test';
node = ros2node("/test", 0, Parameters=nodeParams);
% Get the parameter
topicName = getParameter(node, "topicName", Datatype="string");
% Create the publisher
pub = ros2publisher(node, topicName, "std_msgs/Bool");
% Loop rate to 1 Hz
r = ros2rate(node, 1);
while(1)
% Create and send a message every cycle.
msg = ros2message("std_msgs/Bool");
msg.data = true;
send(pub, msg);
waitfor(r);
end
end
The coder commands:
cfg = coder.config('exe');
cfg.Hardware = coder.hardware('Robot Operating System 2 (ROS 2)');
cfg.Hardware.DeployTo = 'Localhost';
cfg.Hardware.BuildAction = 'Build and Load';
cfg.TargetLang = 'C++';
cfg.InlineBetweenUserFunctions = 'Readability';
codegen('-config', cfg, 'testROS2Node');
The error:
Expression could not be reduced to a constant.
Error in ==> ros2publisher Line: 183 Column: 22
Code generation failed: View Error Report

채택된 답변

Josh Chen
Josh Chen 2023년 8월 9일
Hi Sam,
Unfortunately, the topicName input for ros2publisher has to be a constant. Since parameter value is not constant, you cannot create the publisher with it.
Can you please share more about the workflow? Is there a reason you would like to create a publisher using the parameter?
Thanks,
Josh
  댓글 수: 3
Erwin Mathew Louis
Erwin Mathew Louis 2024년 4월 11일
편집: Erwin Mathew Louis 2024년 4월 11일
Hello,
Just a quick question to this. In case it was a regular parameter and not a topic name, would I be able to change the value from the terminal when the generated C++ code of the node created in matlab is running? There isnt much documentation about how matlab handles such changes to parameters. I guess I am mainly asking about the parameter callback functionality available in ros2. are such callback available in the ROS toolbox?
Josh Chen
Josh Chen 2024년 4월 11일
Hi Erwin,
If I understand you correctly, you are referring to ROS 2 parameters associated with ROS 2 nodes. Yes, this is supported for code generation. You can specify all parameters with the "Parameters" name-value pair when creating the node object.
When generating code from MATLAB, those parameters will be generated as ROS 2 parameters for the associated ROS 2 node.
You may refer to this example for the syntax.
As for parameter callbacks, could you please give some more information about what you are planning to achieve/do inside each callback?
Thanks,
Josh

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 ROS Network Access in MATLAB에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by