1
00:00:00,780 --> 00:00:06,210
In the last lecture, we created a ROS to publisher, so let's create a subscriber node to process the

2
00:00:06,210 --> 00:00:08,190
data we sent by our publisher.

3
00:00:08,520 --> 00:00:13,980
In this case, we will just make a simple subscriber which prints the data sent over by the publisher.

4
00:00:15,190 --> 00:00:22,150
So with Vee's code open, let's create a new file in our scripts folder called Subscriber py.

5
00:00:26,900 --> 00:00:27,980
Just like our publisher.

6
00:00:27,980 --> 00:00:34,850
Note we will need to import our ROS client library, our node object and string message type so we can

7
00:00:34,850 --> 00:00:38,930
just copy those over from the import statements from our publisher script.

8
00:00:40,120 --> 00:00:45,100
We can also copy the main function as well as the if name equal to main statement.

9
00:00:45,460 --> 00:00:50,590
From here we're just going to change our my pub variable to my sub.

10
00:00:50,620 --> 00:00:53,800
I will be creating a class called Hello World Subscriber.

11
00:00:54,070 --> 00:00:58,390
Rather than saying our publisher node is running, I'll instead say waiting for data to be published

12
00:00:58,390 --> 00:00:59,500
over the topic.

13
00:00:59,530 --> 00:01:05,260
And then lastly, in our TRI accept statement, we will change the my pub variables to my sub.

14
00:01:14,450 --> 00:01:15,130
All right.

15
00:01:15,140 --> 00:01:19,800
I'll go ahead and close this and let's get to creating our Hello World subscriber class.

16
00:01:19,820 --> 00:01:22,820
Just like our publisher, we will inherit the node class.

17
00:01:24,120 --> 00:01:28,620
Then go ahead and reference the node classes in its statement with our node name.

18
00:01:29,250 --> 00:01:33,780
As you remember, this function takes in our node name, which in this case I will call.

19
00:01:33,780 --> 00:01:35,580
Hello World Sub node.

20
00:01:38,320 --> 00:01:40,500
We can now create our subscriber.

21
00:01:40,510 --> 00:01:46,570
I will just make a variable called sub and use the create subscription function from our node class.

22
00:01:47,110 --> 00:01:49,480
This function takes in four parameters.

23
00:01:49,480 --> 00:01:55,360
The first is the message type of the subscriber, which in this case will be our string message we imported

24
00:01:55,360 --> 00:01:55,990
above.

25
00:01:56,260 --> 00:01:58,810
The second argument is the topic name to subscribe to.

26
00:01:59,230 --> 00:02:03,580
If we look back at our publisher, we can see we set it to the string Hello world.

27
00:02:03,580 --> 00:02:06,160
So you need to make sure that this matches exactly.

28
00:02:06,160 --> 00:02:11,050
If not, your subscriber node will not receive messages from the right topic.

29
00:02:11,410 --> 00:02:13,630
The third argument is a callback function.

30
00:02:13,630 --> 00:02:18,490
The callback function we name here will be run every time we receive a message from our publisher over

31
00:02:18,490 --> 00:02:19,930
this particular topic.

32
00:02:20,020 --> 00:02:24,370
In this case, I will create a callback function called subscriber callback.

33
00:02:26,320 --> 00:02:30,960
Then the last argument is the quality of service profile, which is our subscribers history depth.

34
00:02:30,970 --> 00:02:35,260
I shouldn't have to worry about this for the simple subscriber, but I'll just set it to ten with the

35
00:02:35,260 --> 00:02:36,130
subscriber created.

36
00:02:36,130 --> 00:02:39,010
I'm now going to create the subscriber callback function.

37
00:02:39,190 --> 00:02:43,630
This function will take in self and the message that comes in over the topic.

38
00:02:44,470 --> 00:02:48,370
Once we receive the message, we are just going to print it to the terminal screen.

39
00:02:48,520 --> 00:02:53,830
In this case I will just say received and then append on the content from the messages data attribute.

40
00:02:55,120 --> 00:02:55,510
All right.

41
00:02:55,510 --> 00:02:57,970
With that we have finished creating our subscriber script.

42
00:02:58,060 --> 00:03:03,700
At the top, we imported our Ross Client library, node class and string message type.

43
00:03:04,030 --> 00:03:09,040
Then we created our Hello World Subscriber class, which is based off of the node class.

44
00:03:09,040 --> 00:03:14,440
We initialized the node, then created a subscriber which expects a string message type subscribes to

45
00:03:14,440 --> 00:03:19,480
the hello world topic and triggers our subscriber callback function every time data comes in over the

46
00:03:19,480 --> 00:03:20,110
topic.

47
00:03:20,740 --> 00:03:25,240
In this callback function, we took the message received and printed the data attribute of it to the

48
00:03:25,240 --> 00:03:25,870
terminal.

49
00:03:26,520 --> 00:03:31,350
Then we have our main function where we initialize our communication pipeline, create an instance of

50
00:03:31,350 --> 00:03:36,450
our subscriber class print that we are waiting for data to be published and then keep the node running

51
00:03:36,450 --> 00:03:38,730
until a keyboard interrupt is detected.

52
00:03:38,850 --> 00:03:40,710
In which case we destroy the node.

53
00:03:41,160 --> 00:03:45,960
So with that, I've finished my subscriber, although I just notice I'm on spaces for I'll set that

54
00:03:45,960 --> 00:03:46,680
to tab.

55
00:03:48,530 --> 00:03:53,030
And I'll go ahead and make sure that this is set correctly.

56
00:03:56,340 --> 00:03:56,910
All right.

57
00:03:57,210 --> 00:04:01,740
So now I'm going to go ahead and see what you can do with either the control se keyboard shortcut or

58
00:04:01,740 --> 00:04:04,200
by hitting file and save.

59
00:04:04,410 --> 00:04:06,900
And now we can head over to our terminal.

60
00:04:08,600 --> 00:04:09,910
So let's change it to the directory.

61
00:04:09,920 --> 00:04:15,590
Our script is located and run the Python three command followed by the name of our subscriber script.

62
00:04:21,190 --> 00:04:21,490
All right.

63
00:04:21,490 --> 00:04:25,600
We get the print statement saying that our subscriber node is waiting for data to be published.

64
00:04:25,600 --> 00:04:30,730
So let's go ahead and open a new terminal and start our publisher script that we made in the last lecture.

65
00:04:32,110 --> 00:04:32,410
All right.

66
00:04:32,410 --> 00:04:37,120
So now we see our publisher Node is running and our subscriber node, which is subscribed to that same

67
00:04:37,120 --> 00:04:39,550
topic, is receiving all of the messages.

68
00:04:39,550 --> 00:04:45,700
And if I go ahead and stop my publisher node with control C, we stop receiving new messages from our

69
00:04:45,700 --> 00:04:46,690
publisher node.

70
00:04:47,080 --> 00:04:49,180
Go ahead and stop our subscriber node.

71
00:04:49,810 --> 00:04:56,260
And with that you have successfully created your very own ROS two subscriber in Python, which subscribes

72
00:04:56,260 --> 00:04:58,690
to data sent by our publisher Node.

73
00:04:59,020 --> 00:05:02,680
Let's quickly review what we learned in this lecture.

74
00:05:02,680 --> 00:05:05,560
We learned how to create subscribers in Python.

75
00:05:05,740 --> 00:05:12,100
Within our node class we use the create subscription function to subscribe to a topic with a particular

76
00:05:12,100 --> 00:05:13,120
message type.

77
00:05:13,390 --> 00:05:18,160
Every time data is published over that topic, a subscriber callback function runs.

78
00:05:18,340 --> 00:05:23,790
This callback function takes in the message received over the topic where we can then process it.

79
00:05:23,800 --> 00:05:27,940
In this lecture we just printed the data attribute of our string message, but you're free to process

80
00:05:27,940 --> 00:05:30,820
it however you want within your callback function.
