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 Center および File ExchangeROS 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