Use ROS2 Launch File to Run Multiple Instances of Matlab Generated ROS2 Nodes

88 views (last 30 days)
I have generated and succesfully deployed a ROS2 node that publishes simple messages from my Matlab code. I can run this on my target machine with ros2 run ros2publisherexample ros2publisherexample . (I know my package name and executable name are the same - I'll deal with that later).
I would like to use a ROS launch file to be able to launch more than one instance of this node. The Matlab function has no arguments and publishes stamped pose messages with all zeros (for testing purposes).
function [] = ros2publisherexample()
%#codegen
node = ros2node("example_node");
pub = ros2publisher(node,'/example_topic','geometry_msgs/PoseStamped');
msg = ros2message('geometry_msgs/PoseStamped');
while (1)
msg.header.stamp.sec = 0;
msg.header.stamp.nanosec = 0;
msg.pose.position.x = 0;
msg.pose.position.y = 0;
msg.pose.position.z = 0;
msg.pose.orientation.x = 0;
msg.pose.orientation.y = 0;
msg.pose.orientation.z = 0;
msg.pose.orientation.w = 0;
send(pub,msg)
pause(1)
end
end
Using this page and this page I have tried generating both xml and python launch files (see below). Because I couldn't get this to work for two instances of the node, I am trying first to just launch one instance of this node with the launch file.
launch.xml:
<launch>
<node pkg="ros2publisherexample" exec="ros2publisherexample" name="sim" namespace="publisher1"/>
</launch>
launch.py:
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
return LaunchDescription([
Node(
package='ros2publisherexample',
executable='ros2publisherexample',
name='sim',
namespace='publisher1'
)
])
In both cases I get errors when trying to run ros2 launch launch.xml or launch.py about argument inputs.
dasl@dasl-Precision-T5610:~/matlab_ws/launch$ ros2 launch launch.py
[INFO] [launch]: All log files can be found below /home/dasl/.ros/log/2022-05-31-10-08-30-854756-dasl-Precision-T5610-59449
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [ros2publisherexample-1]: process started with pid [59451]
[ros2publisherexample-1] terminate called after throwing an instance of 'rclcpp::exceptions::UnknownROSArgsError'
[ros2publisherexample-1] what(): found unknown ROS arguments: '__log_disable_rosout:=true'
[ERROR] [ros2publisherexample-1]: process has died [pid 59451, exit code -6, cmd '/home/dasl/matlab_ws/install/ros2publisherexample/lib/ros2publisherexample/ros2publisherexample --ros-args -r __node:=sim -r __ns:=/publisher1'].
It appears as though launch is trying to pass arguments into the generated function that it is not prepared to receive. I can see that the arguments appear to be setting up remappings for the node and namespace to those in the launch file, but the Matlab generated code appears not ready to accept thos ros arguments. Additionally, I am not sure what to make of the __log_disable_rosout:=true unknown ROS argument.
My question is this: How might I updated my Matlab function or set up my Matlab Coder configuration/build to allow for launching the nodes in this way (ie. from a ros launch file)? Is it even possible?
FYI: I have tried these launch files with and without the name and namespace arguments. If I don't include them (<node pkg="ros2publisherexample" exec="ros2publisherexample"/>) I get the following in terminal. Notice the persistant error about __log_disable_rosout.
dasl@dasl-Precision-T5610:~/matlab_ws/launch$ ros2 launch launch.py
[INFO] [launch]: All log files can be found below /home/dasl/.ros/log/2022-05-31-09-31-19-850082-dasl-Precision-T5610-58108
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [ros2publisherexample-1]: process started with pid [58110]
[ros2publisherexample-1] terminate called after throwing an instance of 'rclcpp::exceptions::UnknownROSArgsError'
[ros2publisherexample-1] what(): found unknown ROS arguments: '__log_disable_rosout:=true'
[ERROR] [ros2publisherexample-1]: process has died [pid 58110, exit code -6, cmd '/home/dasl/matlab_ws/install/ros2publisherexample/lib/ros2publisherexample/ros2publisherexample --ros-args'].
UPDATE: I originally posted this when I was sourcing a Galactic install of ROS 2. I have since also installed Foxy and rebuilt. The error persisted.
  1 Comment
Michael
Michael on 31 May 2022
@Jagadeesh Konakalla You were so knowledgeable/helpful on my last question regarding deployment, I thought you might know something about the question. Any ideas?

Sign in to comment.

Answers (1)

Karthik Reddy Vennapureddy
Hello Michael,
Thank you for providing the detailed execution steps. To answer your question, yes it is possible to launch the nodes(built from generated source files) from a launch file using "ros2 launch". However, there seems to be an issue in the following lines of generated code in main.cpp:
int main(int argc, char * const argv[])
{
std::vector<char *> args(argv, argv + argc);
char log_disable_rosout[] = "__log_disable_rosout:=true";
args.push_back(log_disable_rosout);
rclcpp::init(static_cast<int>(args.size()), args.data());
gNodePtr = std::make_shared<rclcpp::Node>("example_node","");
...
...
return 0;
}
As a workaround, please run the following steps:
  1. Remove/comment the above bold lines of code in main.cpp(present in the src folder of colcon workspace)
  2. Re-run the colcon build
  3. Execute "ros2 launch launch.py". It should now launch one or more nodes, depending on the configuration in launch file.
Let me know, if you face any issues.
Thanks,
Karthik Reddy
  4 Comments
Iftach Naftaly
Iftach Naftaly on 16 Jun 2022
Hi Karthik,
You are right, my node generated from a Simulink model.
Thanks for the information,
Iftach.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!