Now we will try to create a publisher using Python.
Well, when we created the package, we used the build type and went CMake.
Then how we will add the python node into it.
It allows us to just use C plus plus.
Well, there will be a little bit modification needed.
Yeah.
Here have the instruction and we can simply edit our own packets.
So it has stated that you can create a package using CMake and we have done already.
Here it is explaining that how you can add a python node into it.
Make a folder which has the same name as the package here.
Let's ruler new folder.
Now you're inside the package.
Turtle Same port done.
And the second thing is that we have to create a file whose inside that folder and its name should be
double underscore init.
Double underscore dot py.
Where do we need that?
Inside the folder we create an empty PY file and this is something that already present when you create
a python package.
And then we have to make a script folder in which we can keep our python nodes.
Open your terminal City rose to workspace.
For you.
We are into our workspace and now source it Source Install local setup dot Dartfish workspaces Source
code to inside your package src.
And there we have the turtle sync package and then again the turtle sim.
And now we have to create our init file.
It will be like touch.
Underscore.
Underscore and.
Underscore.
Underscore dot pi.
Make it executable C or D plus x and compile.
And now this is executable file.
Now we will have to go back and then make our directory.
There's in this directory.
We can keep our python nodes.
We will go inside that directory.
So career torch.
Using the torch command, we can create a file.
Touch.
You can say my python.
publisher.py.
Okay.
And then we should have to make it executable or d plus x and the file.
Now we have set up the files in our brackets.
Okay.
It says you have to add shebang line first and a python file third.
I'm going back and then we can open up the package in the VS code.
There we go.
And we will have this python file here and we will add the first line.
Every python file should have this first shebang line and this is must.
But you will be wondering that.
Why didn't we add this in the init.py.
Well the init.py case is different because this file and mainly used to let the ros know that in this
package something is related to the python is present.
There are the instructions were added to package our XML file.
Just copy this line.
Okay.
And open our package or animal.
And there you can see the line under it.
We will add our new line and now we can open our CMake list.
And there we have to mention that Findpackage and CMake Python is required.
So we will just copy this line and come here to the list and there we go.
Find package.
We will just add the line under this.
And here here are some part that is related to the python and we will just copy it and come right here.
After the Cplusplus dagger.
Add this line of code here and in these scripts.
I'm sorry.
Our folder name was script.
Just training and anchor and script.
And then what was inside?
They sit here and that's it.
Our package can now run Python and C plus plus notes Q1's back and call convert one final samling minus
install.
We build successful source, install the local setup dot bash, find the executor ros to and run turtle
port and double tab.
We have one more executable file that is my python publisher dot pi.
And this is absolutely empty.
It will do nothing.
My python.
That's it.
It will do nothing because it has nothing inside it.
We will come to the documentation and here we will copy this code.
That is the publisher.
We can paste it here as we have changed the python file.
We do not need to use a call Qualcomm and Source Command again because the python node does not need
to be built every time you can add it and run simply this.
Minimal publisher.
I'm going to change the name to like Python publisher.
That's that's fine.
Okay.
And the topic name were choosing a different topic name.
We will choosing the string message Docker communication and I will just paste it here.
The code is doing the same as T plus nor was doing.
And there you can also find the explanation that what part is doing what?
Well, if we have to note and we will run this.
And now, as you can see, it is publishing the data to the topic.
Now we have a Python code that is publishing on to the topic.
And but we have also another publisher that we created using the C plus plus.
We will also run this.
Ross to run turtle same port.
And there was a toggle mode that will be publishing on the same topic.
And now we have two publisher.
They both are publishing on one topic.
So now we will run a subscriber.
It is receiving the data from the both topics.
Now we will open another terminal rose to topic equal and we have the string message communication topic.
And there you can also see.
Now we will see the connection between the nodes and topics using the archetype graph aka underscore
graph.
There we go.
Minimal publisher and Python publisher are publishing on this topic and the string subscribers CV.
I hope this was a fun activity for you.
And now we can run C plus plus and Python node on inches one package.
All right.