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

4 次查看(过去 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