How do I properly generate code to set the topic name for a ROS 2 publisher using the value of the getParameter(...) function?
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on ROS Node Generation and Deployment in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!