1
00:00:00,720 --> 00:00:03,510
Welcome to your first project of the course.

2
00:00:03,600 --> 00:00:09,390
Based on what you have learned about publishers, subscribers and interface types, it's time for you

3
00:00:09,390 --> 00:00:11,700
to apply this knowledge in this project.

4
00:00:12,120 --> 00:00:17,850
The project scenario is this You have a mobile robot with wheels of a certain radius.

5
00:00:17,940 --> 00:00:23,700
One of the wheels has a rotary encoder sensor on it, which outputs how many rotations per minute.

6
00:00:23,700 --> 00:00:25,170
The wheel is rotating.

7
00:00:25,350 --> 00:00:30,720
For this project, we will keep it simple and say the robot is going at a constant speed.

8
00:00:31,110 --> 00:00:37,590
The first node I want you to create is a publisher node, which publishes to a topic named RPM with

9
00:00:37,590 --> 00:00:42,630
the sensor value that we are going to be publishing, in this case, a constant value of your choice.

10
00:00:42,930 --> 00:00:46,320
Now, I have no idea how fast the robot is actually going on this.

11
00:00:46,320 --> 00:00:51,810
I take into account the size of its wheels, so I want you to create a second node which subscribes

12
00:00:51,810 --> 00:00:57,780
to the RPM topic calculates the speed the robot is going based on its wheel size and then publishes

13
00:00:57,780 --> 00:01:00,780
a result to the topic called speed.

14
00:01:00,780 --> 00:01:06,210
The speed for most robotic systems are in meters per second, but feel free to make it your own preferred

15
00:01:06,210 --> 00:01:07,350
units of measurement.

16
00:01:07,710 --> 00:01:11,100
So let's review what we are trying to accomplish in this project.

17
00:01:12,640 --> 00:01:18,700
In this project, you will create two nodes, one which publishes a value simulating rotations per minute

18
00:01:18,700 --> 00:01:24,700
of the robot wheel spinning and a second script which subscribes to this value, and then goes ahead

19
00:01:24,700 --> 00:01:27,760
and publishes the calculated speed of the robot.

20
00:01:28,270 --> 00:01:33,220
You should be able to run these two nodes in two separate terminals to get your corresponding output.

21
00:01:35,370 --> 00:01:40,920
Go ahead and pause this video and give yourself about 10 minutes or so to give this project a go.

22
00:01:40,980 --> 00:01:45,180
If you get stuck, you can follow along with me in the remainder of this video lecture.

23
00:01:45,180 --> 00:01:48,600
But I encourage you to try to work through this project on your own.

24
00:01:58,330 --> 00:01:58,730
All right.

25
00:01:58,750 --> 00:01:59,500
How'd it go?

26
00:01:59,650 --> 00:02:02,680
If you got stuck, you can follow along with me to see how I did it.

27
00:02:05,750 --> 00:02:12,650
With my Visual Studio code editor open, I can go ahead and create a new script which I will call RPM

28
00:02:12,650 --> 00:02:14,060
Pub Pie.

29
00:02:15,670 --> 00:02:20,230
This note is just going to publish a constant number simulating the speed of our robot.

30
00:02:20,320 --> 00:02:25,660
So I'll actually go ahead and copy the code from the publisher we made in the previous lectures and

31
00:02:25,660 --> 00:02:26,380
modify it.

32
00:02:29,480 --> 00:02:35,660
You can do this by going to edit and we can go to replace or you can use the control H keyboard shortcut.

33
00:02:35,870 --> 00:02:41,300
And first thing I'm going to do is change the class name from Hello World Publisher to RPM Publisher,

34
00:02:41,300 --> 00:02:42,620
so it makes more sense.

35
00:02:44,860 --> 00:02:46,540
And I'll replace all instances.

36
00:02:46,720 --> 00:02:50,710
Next, I will change the node name to RPM pub node.

37
00:02:53,500 --> 00:02:58,410
And instead of publishing a string message type, I will instead use a float 32.

38
00:02:58,420 --> 00:03:01,810
So I will replace all of our string instances like so.

39
00:03:03,140 --> 00:03:09,590
I will change the topic name we are publishing to to be RPM and change the callback function name to

40
00:03:09,590 --> 00:03:12,200
publish rpm so it makes more sense.

41
00:03:14,960 --> 00:03:20,720
Then within our callback function, rather than setting the float message data to Hello world, I will

42
00:03:20,720 --> 00:03:27,290
instead set it to a constant variable which I will call RPM in all caps, and I will then create this

43
00:03:27,290 --> 00:03:30,020
variable at the top of my file and I'll just set it to ten.

44
00:03:30,410 --> 00:03:34,940
Then to make it a bit clearer, I will print that the RPM publisher node is running.

45
00:03:35,970 --> 00:03:36,330
Great.

46
00:03:36,330 --> 00:03:37,740
That's it for our RPM publisher.

47
00:03:37,740 --> 00:03:40,320
We can save the file and test that It works.

48
00:03:40,320 --> 00:03:46,380
Before we open up a terminal, I'll configure our make list text file to include our new script by going

49
00:03:46,380 --> 00:03:52,260
into the install section and adding the scripts slash rpm underscore pub dot pie node.

50
00:03:54,510 --> 00:03:55,050
With that.

51
00:03:55,050 --> 00:03:59,370
I'll save this file and we can go ahead and run a coal can build in terminal.

52
00:03:59,370 --> 00:04:04,590
Or if you follow along with me in the last lecture, go to terminal and run build task to do it for

53
00:04:04,590 --> 00:04:05,040
you.

54
00:04:05,700 --> 00:04:11,460
So we've gone ahead and successfully built our RPM publisher node so we can go ahead and test it.

55
00:04:11,460 --> 00:04:15,450
So I'll go ahead and open up a terminal source, our install setup dot bash file.

56
00:04:18,120 --> 00:04:20,880
And then I can go ahead and run the node to test it out.

57
00:04:23,150 --> 00:04:25,310
So we see RPM publisher Node running.

58
00:04:25,310 --> 00:04:31,190
And if I go ahead and do Ross two topic list, I can go ahead and echo the contents.

59
00:04:35,760 --> 00:04:39,470
And we see our constant value of ten being published.

60
00:04:39,480 --> 00:04:41,340
So I'll go ahead and stop this.

61
00:04:43,540 --> 00:04:43,970
Great.

62
00:04:43,990 --> 00:04:48,250
The RPM node works, so let's now create our speed publisher node.

63
00:04:48,280 --> 00:04:52,840
The first thing our speed publisher node will have to do is subscribe to the value being published over

64
00:04:52,840 --> 00:04:59,890
the RPM topic so I can go over to my subscriber node so that I can copy it over to my newly created

65
00:04:59,890 --> 00:05:00,420
code.

66
00:05:00,430 --> 00:05:06,520
So I'll go over to scripts, create a new file, and I'll call this one Speed Calc dot pie.

67
00:05:08,750 --> 00:05:11,870
Then I'll go ahead and copy the contents from my subscriber's file.

68
00:05:13,870 --> 00:05:17,980
And I'll go ahead and change the class name to Speed calculator.

69
00:05:24,770 --> 00:05:27,710
And change the node name to a speed calc node.

70
00:05:29,230 --> 00:05:30,010
And then I'll change.

71
00:05:30,010 --> 00:05:34,660
Instead of the string message type, we will be subscribing to a float 32 type.

72
00:05:36,250 --> 00:05:38,170
As we did in our RPM publisher.

73
00:05:39,130 --> 00:05:43,810
And we will also match the RPM publisher's topic name in this case RPM.

74
00:05:43,810 --> 00:05:45,720
And I'll go ahead and leave the slash out of there.

75
00:05:45,730 --> 00:05:50,590
You can leave slashes in the topic names, but for scalability reasons, it's better to keep them without

76
00:05:50,590 --> 00:05:50,950
the slash.

77
00:05:50,950 --> 00:05:57,370
When you define them, you'll see why a bit in future lectures and I will change the callback function

78
00:05:57,370 --> 00:05:59,320
to be called calculate speed.

79
00:06:03,610 --> 00:06:04,450
Down in the main function.

80
00:06:04,450 --> 00:06:07,540
I'll change the print statement to say Speed calculator node started.

81
00:06:10,170 --> 00:06:14,580
And I will change this my sub variable to be something that makes a bit more sense.

82
00:06:14,580 --> 00:06:16,140
I'll just call it Speed Calc.

83
00:06:19,060 --> 00:06:20,890
In our calculate speed callback function.

84
00:06:20,890 --> 00:06:23,260
We currently have it set to just print the data out.

85
00:06:23,380 --> 00:06:27,730
Rather than doing that, we will use this data to calculate the speed our robot is going.

86
00:06:27,970 --> 00:06:31,840
First thing I will do is change our message variable to RPM message.

87
00:06:31,840 --> 00:06:33,550
So it's clear what this message is for.

88
00:06:33,640 --> 00:06:39,610
Then I will create a variable called speed, which I can calculate using the RPM message data multiplied

89
00:06:39,610 --> 00:06:45,640
by the circumference of the wheel, which is the wheel radius multiplied by two multiplied by pi.

90
00:06:48,770 --> 00:06:52,370
Which I will approximate as 3.14159.

91
00:06:52,610 --> 00:06:58,850
Then, since I want my speed in meters per second, I will have to divide by 60 to convert rotations

92
00:06:58,850 --> 00:07:01,100
per minute to rotations per second.

93
00:07:04,740 --> 00:07:10,140
Now I need to create this wheel radius variable, which I will just do near the top of the file.

94
00:07:10,170 --> 00:07:12,660
I will set it equal to 12.5 centimeters.

95
00:07:12,810 --> 00:07:17,040
It is worth noting, though, that I want my speed to be in meters per second, so I'll have to convert

96
00:07:17,040 --> 00:07:22,560
this to meters, which I can do by dividing the 12.5 centimeters by 100.

97
00:07:22,560 --> 00:07:25,050
And I can put a note here explaining why I did this.

98
00:07:27,470 --> 00:07:30,720
OC We calculated speed based off the RPM data.

99
00:07:30,750 --> 00:07:36,120
Now we need to publish this speed data, which means I need to create a publisher.

100
00:07:36,270 --> 00:07:40,110
I can just copy over our publisher code from our RPM pub.

101
00:07:42,920 --> 00:07:47,360
And place it here and we will be publishing to the speed topic for this publisher.

102
00:07:47,360 --> 00:07:53,120
So I'll go ahead and change that and I'll go ahead and copy the usage of the publisher's message data

103
00:07:53,120 --> 00:07:53,690
set up.

104
00:07:56,150 --> 00:07:59,480
And place them in my calculate speed callback function.

105
00:08:01,860 --> 00:08:04,860
And I'll rename this speed message so it makes more sense.

106
00:08:08,900 --> 00:08:12,410
And instead of loading in the RPM, we'll be loading in the calculated speed.

107
00:08:14,480 --> 00:08:14,780
Great.

108
00:08:14,780 --> 00:08:16,830
So let's review what is happening in this code.

109
00:08:16,850 --> 00:08:20,480
I created a subscriber which subscribes to the RPM topic.

110
00:08:20,630 --> 00:08:26,940
I also created a publisher which publishes to the speed topic in my subscriber callback function.

111
00:08:26,960 --> 00:08:31,070
I take the RPM data and calculate the robot's speed.

112
00:08:31,340 --> 00:08:36,710
Then I load the value into the float 32 speed message and publish it.

113
00:08:37,490 --> 00:08:37,790
Great.

114
00:08:37,790 --> 00:08:42,590
I'll save the file and head over to my make list text file to include it.

115
00:08:46,850 --> 00:08:51,110
So I'll go ahead and save this and go ahead and recompile our project.

116
00:08:53,530 --> 00:09:00,160
Now I can go ahead and clear my terminal and rerun my RPM publisher node.

117
00:09:01,430 --> 00:09:04,040
And I'll clear this and I'll go ahead and source it.

118
00:09:08,170 --> 00:09:09,790
And run our speed calc node.

119
00:09:15,110 --> 00:09:17,600
Then we can go ahead and open a third terminal tab.

120
00:09:18,830 --> 00:09:20,390
I'll go ahead and move these a bit.

121
00:09:22,000 --> 00:09:28,690
And we can do Ross to node list and we see our two nodes are indeed running and we do Ross to topic

122
00:09:28,690 --> 00:09:29,380
list.

123
00:09:30,010 --> 00:09:32,490
We can see RPM and speed being published too.

124
00:09:32,500 --> 00:09:34,720
So if I go ahead and echo our RPM one more time.

125
00:09:40,100 --> 00:09:41,660
We get our value of ten.

126
00:09:41,660 --> 00:09:43,760
And if I go ahead and echo our speed.

127
00:09:45,110 --> 00:09:53,540
We get the calculated speed of this particular RPM with the wheel radius size I set in my speed calc

128
00:09:53,540 --> 00:09:54,110
node.

129
00:09:55,790 --> 00:10:02,450
It'll be the speed of this particular robot that has this 12.5 centimeter wheel going at ten rotations

130
00:10:02,450 --> 00:10:03,200
per minute.

131
00:10:05,200 --> 00:10:06,220
Congratulations.

132
00:10:06,220 --> 00:10:11,890
You successfully completed your first Ross to Python project where you are able to take advantage of

133
00:10:11,890 --> 00:10:17,020
Ross publishers and subscribers to process data as you would on a real robotic system.

134
00:10:17,410 --> 00:10:22,900
Now notice we had to hard code the values of the RPM and wheel radius into our code.

135
00:10:22,990 --> 00:10:27,010
So what happens if we wanted to use this code for robots with a different wheel size?

136
00:10:27,250 --> 00:10:32,050
Well, we would need to edit our python script depending on the wheel size and rerun it.

137
00:10:32,080 --> 00:10:33,610
Now that's not very functional.

138
00:10:33,610 --> 00:10:37,900
So in the next lecture we are going to learn how we can use parameters to get around this issue rather

139
00:10:37,900 --> 00:10:41,470
than having to edit our Python scripts every time we want to make changes.
