ROS2 - Create a Launch File with Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when you start to have multiple nodes to launch any eros2 application along with many parameters then it becomes quite long for you to do it and the more nodes and parameters the higher the chance of doing something wrong fortunately you can create what is called a launch file where you will put all your nodes and parameters in a single file that you can launch with a single comment so in this tutorial I will show you how to create install and start a launch file written with python code with Rush 2. first of all where to create your launch files well you could technically create a launch file from any package you want okay so if you have an existing package all you need to do is to add a launch folder inside the package and inside this large folder you will add your launch files so this can be quite useful if you want to test some functionalities of one package but if you want to create a launch file to start your entire application or to start node that come from different packages then the best practice is actually to create a dedicated package for the launch files and so let's do that actually so let's go to our ros2 workspace so I already have a workspace and in the source folder of the workspace let's create a new package so let's do Ros 2 PKG create and then how are we going to name the package well a convention that you're gonna see With Many Robots so if you browse code on GitHub for example is to use the robot name so let's call our robots my robot okay so if your robot is named differently just use the robot name with underscore if you have spaces okay so my robot and then underscore bring it all right so if you have ever seen a bring a package somewhere for a robot now you know this is a package to just bring up stuff on the robot so to start launch files with parameters etc etc so we're gonna follow this convention ros2pkg create my robot bring up I'm not going to add a build type or dependencies let's just press enter great now we have a new package here let's go inside this package and what is this package well this package is basically a C plus plus package because when you don't provide any build type the default will be Ament cmake so this is an Ament cmake package which simply means that we have a simiclist.txt and then include and Source folder and I'm going to remove so RM Dash RF with include and also rm-rf with Source because we are not going to write code so we end up with cmacclist.txt and package.xml I'm going however to create a launch folder okay so in this launch folder we are going to write our launch files and oh well let's create a launch file so let's go to launch and we are going to to touch let's name it demo and then you are going to use dot launch dot py okay that's how to create a launch file great and now let's edit this launch file so I'm gonna come back actually to my source folder and launch Visual Studio code from here you can use whatever editor you want of course so in this package we have launch folder with demo.launch.pi and then seem at least the txt and package.xml so let's write our launch file so how to write a rush to launch file with python well there is some syntax that you have to respect so let's start by including some stuff we need from so there is the launch package okay and from that we are going to import so import launch description and then you will do def so create a function which name is generate launch description like that with underscores okay this function needs to have this exact name in generate launch description you are going to create a launch description object so let's just name it LD is equal to launch description like this and then return the launch description so this is actually the minimum code that you can write for a launch file you create a generate launch description function and inside this you return a launch description object that you import from the launch Ros 2 module okay that's very important if you don't have this exact syntax especially the name of the function here that's not gonna work all right and now well here we have a launch file that does nothing so we could install it and launch it but that does nothing what we are going to do is we are going to add a few nodes so to add a node you also need to import from so from launch underscore Ros dots actions import node all right so it doesn't necessarily make sense okay I actually don't know why you have to do from launch ros.actions it's just like that okay it has been designed like that so it doesn't necessarily make sense it's just well just remember you have to use this to import a node and that's it and then it will always be the same thing but I can agree that this can be quite confusing all right so now we have imported node so what we can do is we can create a node that we are gonna add to the launch description so let's create for example let's name it talk node is equal to node and let's go back to a new line here and well why do we need to provide to start a node we will need to provide first the package so what package is this node from demo nodes CBP okay I'm gonna use the demo node CBP package where we have a talker and a listener note okay I'm not going to create new node I'm going to use existing ones and so the package is demo node cpvn it should have been installed when you install Rush 2 anyway okay then comma New line and we will need to provide also execute table is equal to and the name of the executable is docker all right and you can see then you have many other things that you can add so I'm not gonna explain everything here of course this is an introduction I'm going to add a few things later but basically that's what you need to start a node from a launch file okay you create a node object you provide the package and the execute table so basically where can we find the node and then when you have the node what you need to do is to add the node to the launch description so you will do LD dot add action token node so make sure you don't forget this because if you just create the token node here and you don't add it to the launch description then it's not going to be launched so this code will actually start the node so with the Tokyo executable from the demo node CBP and then if you want to add another node well that's quite simple let's add listen and node so listener node is equal to node let's use the same syntax with so package Sears actually demo nodes p y okay to take from the python package okay we have demo nodes with C plus plus and with python so let's use actually two different packages and execute table is equal to so this time it's listener okay let's remove that comma here and now we have a second node that we're gonna simply do LD dot add action wave listener node so we create the launch description here and then we create whatever nodes we want and we just add the nodes to the large description with ADD action and then we'll return the launch description all right and now our launch file is complete so we can start two nodes and just a quick parenthesis here if you like what I do if you like the way I teach and if you are serious about learning ros2 well I have a complete course on roster for beginners which contains more than 10 hours of content including launch files of course but many more things that will take you from zero to a or us to developer in one single course you can check the link in the description all right and now let's come back to the video so now we have this launch file let's save it okay so I'm saving the file with Ctrl s and let's install this we're going to first go to package.xml and here you can see we need the package demo node CPP and we need the package demo nodes py okay so because from this package my robot bring up we use notes from other packages let's add an exact depend tag on the package.xml so here right after build tool depend you can add exec depends with demo nodes CBP exec depend so why exact because it's actually during the execution that you're gonna need the dependency an exact depend with demo nodes py so don't forget to add the packages anytime you add a new dependency to package here you add it also to the package.xml so let's save this and that's pretty much it for package.xml now if we just leave it like this nothing is going to happen so let's go to cmakelists.txt and actually I'm going to remove some stuff okay don't really need that for now and you have so you have fine package payment cmake required and after that you can do install and this is the command that's going to actually install the launch file so install directory and then new line you're gonna put launch so launch is the launch directory here that you have created so the same name and then destination share slash and then with dollar sign curly brackets project name so we are going to install this in the share folder okay in the install folder actually of the rusty workspace we will have a share folder slash project name so this is actually the package and this is where it's going to be installed and because of that when we do rush to launch in the terminal we will be able to find the launch file so let's save the cemetlist.txt and then all we need to do is come back to the terminal let's come back to the Cross to workspace and do call Con build okay you can see build successful and then well let's Source The Bash SEO again why do I switch the bash I see because I have put inside this the sourcing of this workspace actually so I could also source setup.bash from the workspace and then let's do rush to launch so this is how to start the launch file my robot bring up you can see the auto completion and we have demo launch Pi let's press enter let's see and well you can see it is working so I'm gonna leave it a bit and then Ctrl C what happened here so you can see we start with a few info logs from the launch and then we have talker which is actually named talker one and listener 2 okay you can see process started with the PID here so there is a new execute table so the execute table that we have precised here the Tokyo execute table and then the listener execute table and then you can see in the nodes we have the token or The Listener node and everything is working correctly and we have just launched our rust to application from just one single launch file now let's come back to the launch file and let's do just a bit more stuff so you can start node great now you can also do stuff like renaming a node like adding parameters remapping topics while everything you could do on the command line you can also do it in a launch file and thanks to python here you can use Python logic to do basically whatever you want so you can customize your application as much as you want and so here let's say I'm gonna add here name so if I add name I'm gonna rename the node so let's call it my torker okay this one we're gonna keep it like it was but this one we're gonna rename it we can also remap a topic so actually if I let's open a new window here and let's put this one here let's start the launch file again and let's do a rush to topic list you can see we have the chatter topic okay so rqt graph very quickly you can see so let's refresh okay the toggle node is publishing to the chatter topic and The Listener node is subscribing to the chatter topic we can actually remap this topic from the launch file so let's kill this and let's do here comma remappings so remappings with an S is equal and then you will add brackets okay this is a list this is going to be an array let's go back to a new line okay let's go back to a new line without Auto completion and you're going to add a tuple for each remapping and so here I want to remap the chatter topic to let's say my chatter so in this node which is publishing to the chatter topic now it's not going to publish on the chatter topic it's going to publish on my Charter topic so I will also need to do the same thing here and we'll do remappings is equal to then open the brackets and also chatter and my chat so I use the same name as you can see so that they can still communicate between each other so let's save this and then I'm going to go back here and do cold call build and do source so we're going to Source the script so it's setup.bash and then let's start the launch file again and is it working yes it is working and then if I do rqt graph again let's refresh you can see now you have my Tokyo node so the node has been renamed which is publishing to my chatter okay so this is the remapping and then the listener nodes which is the same name because we didn't change anything which is subscribing to my chatter so we have another remapping here okay so you can see you can already customize your application here very easily from the launch file and then you can add for example parameters so let's add parameters after remappings so you can do parameters so parameters is equal to and then again a array let's use this syntax like that and here you will not use tuples but you will use Curry brackets so you will use dictionaries so in a dictionary you have a key and a value so the key is going to be the parameter name and then colon and then the parameter value so let's say it's an integer value so you can put a number like this or if it's a string you can put the value here and then well if you have multiple parameters you just add multiple dictionaries so param two is for example 19 okay etc etc and so when you launch this node through the launch file well those parameters are going to be set and then you can go even further which is to load parameters using a yaml config file okay you can create a configuration file from your package here and load it from the launch file so if you are interested in that let me know in the comments I might actually make another tutorial on that in the near future alright and congratulations now you can start a launch file you can customize your nodes add parameters Etc to launch your complete roster application from just one single command line thank you for watching now subscribe here to get more tutorials in the future also check out my online courses if you like what I teach links in the description and see you in the next one
Info
Channel: Robotics Back-End
Views: 7,507
Rating: undefined out of 5
Keywords: ros2 launch file, ros launch file, ros2 launch, ros2 tutorial, ros2, robotics backend
Id: xJ3WAs8GndA
Channel Id: undefined
Length: 18min 15sec (1095 seconds)
Published: Mon Jan 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.