I might have briefly mentioned namespaces a few times during this course, but now is a good time to
explain how to work with them.
This concept doesn't necessarily belong to the launch file section, but as we set them mostly inside
launch files and also with YAML files, I wanted to wait until this point.
So let's start first with the concept of namespaces.
See how to add a namespace to a node.
And finally we will see how to use namespaces in launch files.
So well you can open some terminals.
And when we start a node let's just do a rush to run with my, for example, CBP package.
And let's start the number publisher that we have used quite a lot already.
Well, you know that if I run this and if I do rush to node list, let's also do a rush to topic list.
Okay.
We have this is the node name number publisher.
And we have the number topic published by this one.
Now let's stop And how to add a namespace.
I'm going to add a namespace and show you what it does.
Well, you just do Ross args, just like for everything else, for renaming, for parameters.
And then you add another dash R.
Okay.
So that's going to be considered as a remapping dash R.
And then you have underscore underscore ns for namespace.
So to rename a node you had node like that for namespace you have this and let's say slash test what's
going to happen.
Well first you can see in the logs.
Now we have not just number publisher we have test number publisher okay.
So namespace is going to be added in front of the name.
Well just like you would use a namespace in your code.
If you're already familiar with namespaces you know what it does okay.
It's just adding a name in front of all the other names.
So if I do Ros2 node list now you can see my node is not number publisher.
My node is test number publisher.
And if I do rostopic list you see that.
So we have the result and parameter events.
Those are just kind of on the side.
But you see now my number topic is not number it's test number.
Okay.
So that's one very important thing is that when you add a namespace to a node, when you start, it
is going to change all of the names inside.
So the node name and the topic names, the service name etc..
Let's check the parameters.
Of course two param list.
Well the parameter name of course doesn't change, but the name of the node has changed.
And so let's come back to the code and I'm going to show you the importance of the leading slash.
So well for example in the Python package it's the same code as the C plus plus one.
You can see that when I created here the name of the node, I didn't put any slash.
Okay.
The same for the topic name.
I didn't add any slash.
And what you can see is if I don't add any slash one leading slash is going to be added.
Okay.
I didn't provide that in the code.
It's been added automatically because you're always going to start from let's call it the root namespace,
which is no namespace, but it's going to add a slash anyway.
So we have slash node name slash topic name.
And here I have added that another test namespace.
So now it's slash test slash number publisher okay.
And by providing the name like this.
So you can see if we don't provide any namespace it's going to add a slash automatically.
But if we provide a namespace it's going to add the namespace in front of the name.
Now let's say for example in this publisher let's say that I add a slash okay.
What's going to happen if I add a slash to this publisher?
So now it's slash number.
Let's save and let's build again.
So I'm going to go to Roster Workspace.
Click on Build packages.
Select with my Pi PG.
Let's just build like this.
What.
We have a warning here but it's not a problem.
Okay.
We haven't done anything bad so let's source.
Okay.
And let's run.
Let's do rush to run.
So let's start.
So I have changed the Python one.
I'm going to of course launch with the Python package with rush to run my Pi PG number publisher.
And then I add a namespace.
So of course it's still working.
And let's do Ros2 node list we have slash test slash number publisher.
If I do roster topic list.
You see we don't have slash test slash number.
We just have slash number.
Why is that?
Because you see in the code I have provided a leading slash.
Okay.
So if you provide a leading slash you're going to say that that's not going to change even if you add
a namespace.
Okay.
You've already said that this is going to be leading slash number.
If you don't provide a slash then a slash is going to be added.
And if you provide a namespace the namespace can also be added.
So if you want to make sure that no namespaces can change topic name a node name or whatever, you add
a slash here at the beginning in your code, but if not, you just don't add any slash.
So that's an important difference here.
And by default here in the code I have not added any slash, which means that I can add as many namespaces
as I want, and it's quite modular this way.
Okay, so let's save again and just again let's build.
I didn't use Simulink install but okay let's source and let's run again.
And now you see it's going to be slash test slash number because I didn't provide a slash.
So that's quite important difference here and something to understand.
All right.
And namespaces are quite useful if you want to split your application for example for different robots
let's say you have three different robots.
And each robot has a node to control the wheel.
And that's basically from the same node using the same topics and services, etc..
How can you make sure that each robot is not going to get the comments for another robot, and that
you're not going to just mess up communication between your wheel control nodes?
Well, you can just use a namespace like slash robot one for the first, one, slash robot two, slash
robot three.
And then you make sure that the topics for robot one are just for robot one.
Okay.
So all the nodes for the robot one will have a namespace.
All the nodes for the robot two will have another namespace, etc..
Okay.
So this way you can make your application super dynamic.
You don't even need to change the code at all.
You just add a namespace when you start the node and how to add a namespace.
You do dash r with ns.
So underscore underscore ns and you provide a namespace here.
And now is where I come back to the launch files.
Because usually we're going to start our applications from launch files.
So that's where you are going to add namespaces.
And to add a namespace inside a launch file.
It's very easy.
You see we have our node tag with pkg exec name and namespace namespace.
And we're going to start with a slash and let's say well let's also use test namespace or let's say
ABC.
So let's see what happens if I just add a namespace ABC here for just that node for the number publisher
node only.
So this time I'm going to build the robot bring up and then source and then Rose to launch my robot
bring up with number app dot launch dot XML.
Okay, you can see already the name of the node has changed rose to node list.
So the same thing as we saw previously.
Now it's slash ABC slash my number publisher.
And now if I do rose to topic list you see that the namespace has been applied to the topic.
And here it's kind of weird.
Let's let's see with our cut graph.
And let's understand what's happening.
So if I refresh uh well you can group namespaces here.
You see.
But if I go to zero, maybe it's a bit cleaner here for what we want.
So you see we have our ABC my number publisher node which is publishing to ABC number.
Then we have my number counter which is subscribing to my number.
So of course we have a topic mismatch and those cannot communicate.
But there is one thing that you see.
The topic name here is ABC number in the launch file.
So we have added a namespace ABC.
And then we have say that we want to remap number to my number.
And as you can see that clearly this remapping didn't work.
And that's normal because at this point the topic name is not slash number.
The topic name is slash ABC slash number.
So this remapping is not going to have any effect.
And how to solve this.
Well actually you see I have used the slash here.
So we are just going to remove the slash.
Same thing here in the remap okay.
Because it's going to look for slash number.
Exactly.
If I just put number then it's going to rename number.
Even if it's in slash abt slash number it's going to rename number to my number.
Otherwise I would have needed to do slash ABC slash number to slash ABC slash my number.
But that doesn't make it very scalable okay.
If you add a namespace then you need to change it everywhere.
So in this case I just provide the topic name without the slash.
And well now we're going to have the slash ABC slash my number topic.
We also want to have the same topic name on that side.
So we're also going to add the namespace here.
It's going to make more sense namespace ABC and remove this slash that we've added previously okay.
Now let's run again.
So build and then source and then launch.
And let's uh well let's see with Ross two topic list what's going to happen.
You see we have everything is good.
And we can verify that we've actually graph okay perfect.
Now it's slash ABC my number publisher publishing to slash ABC my number.
And this one is subscribing to the correct topic.
And you see also that topic name here has changed because we've applied the ABC namespace to everything
inside this node.
And for the view here.
Well you see I have put zero for namespaces.
But then you can put one for example.
And it's going to group the nodes inside.
You see boxes that correspond to the namespace.
So depending on some cases sometimes it's going to be more readable like this or more readable.
As you increase this number you can just check on your own.
All right.
And that's going to be it for namespaces and how to apply them in launch files.
So you can see in the end this concept and how to use it is not that complicated.
And just in case you need to add a namespace inside of Python launch file, well, this one is quite
easy.
You see, once you have the structure, you just add a namespace argument here.
Okay, with the namespace.
And what I've done is also remove this slash here.
Same reason as I explained to you in the XML launch file.
Okay.
So just namespace like that is going to do the same thing as this in XML.