Filter löschen
Filter löschen

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

4 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Josh Chen
Josh Chen am 9 Aug. 2023
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 Kommentare
Erwin Mathew Louis
Erwin Mathew Louis am 11 Apr. 2024
Bearbeitet: Erwin Mathew Louis am 11 Apr. 2024
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 am 11 Apr. 2024
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by