In the last lecture, we created a ROS to publisher, so let's create a subscriber node to process the
data we sent by our publisher.
In this case, we will just make a simple subscriber which prints the data sent over by the publisher.
So with Vee's code open, let's create a new file in our scripts folder called Subscriber py.
Just like our publisher.
Note we will need to import our ROS client library, our node object and string message type so we can
just copy those over from the import statements from our publisher script.
We can also copy the main function as well as the if name equal to main statement.
From here we're just going to change our my pub variable to my sub.
I will be creating a class called Hello World Subscriber.
Rather than saying our publisher node is running, I'll instead say waiting for data to be published
over the topic.
And then lastly, in our TRI accept statement, we will change the my pub variables to my sub.
All right.
I'll go ahead and close this and let's get to creating our Hello World subscriber class.
Just like our publisher, we will inherit the node class.
Then go ahead and reference the node classes in its statement with our node name.
As you remember, this function takes in our node name, which in this case I will call.
Hello World Sub node.
We can now create our subscriber.
I will just make a variable called sub and use the create subscription function from our node class.
This function takes in four parameters.
The first is the message type of the subscriber, which in this case will be our string message we imported
above.
The second argument is the topic name to subscribe to.
If we look back at our publisher, we can see we set it to the string Hello world.
So you need to make sure that this matches exactly.
If not, your subscriber node will not receive messages from the right topic.
The third argument is a callback function.
The callback function we name here will be run every time we receive a message from our publisher over
this particular topic.
In this case, I will create a callback function called subscriber callback.
Then the last argument is the quality of service profile, which is our subscribers history depth.
I shouldn't have to worry about this for the simple subscriber, but I'll just set it to ten with the
subscriber created.
I'm now going to create the subscriber callback function.
This function will take in self and the message that comes in over the topic.
Once we receive the message, we are just going to print it to the terminal screen.
In this case I will just say received and then append on the content from the messages data attribute.
All right.
With that we have finished creating our subscriber script.
At the top, we imported our Ross Client library, node class and string message type.
Then we created our Hello World Subscriber class, which is based off of the node class.
We initialized the node, then created a subscriber which expects a string message type subscribes to
the hello world topic and triggers our subscriber callback function every time data comes in over the
topic.
In this callback function, we took the message received and printed the data attribute of it to the
terminal.
Then we have our main function where we initialize our communication pipeline, create an instance of
our subscriber class print that we are waiting for data to be published and then keep the node running
until a keyboard interrupt is detected.
In which case we destroy the node.
So with that, I've finished my subscriber, although I just notice I'm on spaces for I'll set that
to tab.
And I'll go ahead and make sure that this is set correctly.
All right.
So now I'm going to go ahead and see what you can do with either the control se keyboard shortcut or
by hitting file and save.
And now we can head over to our terminal.
So let's change it to the directory.
Our script is located and run the Python three command followed by the name of our subscriber script.
All right.
We get the print statement saying that our subscriber node is waiting for data to be published.
So let's go ahead and open a new terminal and start our publisher script that we made in the last lecture.
All right.
So now we see our publisher Node is running and our subscriber node, which is subscribed to that same
topic, is receiving all of the messages.
And if I go ahead and stop my publisher node with control C, we stop receiving new messages from our
publisher node.
Go ahead and stop our subscriber node.
And with that you have successfully created your very own ROS two subscriber in Python, which subscribes
to data sent by our publisher Node.
Let's quickly review what we learned in this lecture.
We learned how to create subscribers in Python.
Within our node class we use the create subscription function to subscribe to a topic with a particular
message type.
Every time data is published over that topic, a subscriber callback function runs.
This callback function takes in the message received over the topic where we can then process it.
In this lecture we just printed the data attribute of our string message, but you're free to process
it however you want within your callback function.