Welcome to your first project of the course.
Based on what you have learned about publishers, subscribers and interface types, it's time for you
to apply this knowledge in this project.
The project scenario is this You have a mobile robot with wheels of a certain radius.
One of the wheels has a rotary encoder sensor on it, which outputs how many rotations per minute.
The wheel is rotating.
For this project, we will keep it simple and say the robot is going at a constant speed.
The first node I want you to create is a publisher node, which publishes to a topic named RPM with
the sensor value that we are going to be publishing, in this case, a constant value of your choice.
Now, I have no idea how fast the robot is actually going on this.
I take into account the size of its wheels, so I want you to create a second node which subscribes
to the RPM topic calculates the speed the robot is going based on its wheel size and then publishes
a result to the topic called speed.
The speed for most robotic systems are in meters per second, but feel free to make it your own preferred
units of measurement.
So let's review what we are trying to accomplish in this project.
In this project, you will create two nodes, one which publishes a value simulating rotations per minute
of the robot wheel spinning and a second script which subscribes to this value, and then goes ahead
and publishes the calculated speed of the robot.
You should be able to run these two nodes in two separate terminals to get your corresponding output.
Go ahead and pause this video and give yourself about 10 minutes or so to give this project a go.
If you get stuck, you can follow along with me in the remainder of this video lecture.
But I encourage you to try to work through this project on your own.
All right.
How'd it go?
If you got stuck, you can follow along with me to see how I did it.
With my Visual Studio code editor open, I can go ahead and create a new script which I will call RPM
Pub Pie.
This note is just going to publish a constant number simulating the speed of our robot.
So I'll actually go ahead and copy the code from the publisher we made in the previous lectures and
modify it.
You can do this by going to edit and we can go to replace or you can use the control H keyboard shortcut.
And first thing I'm going to do is change the class name from Hello World Publisher to RPM Publisher,
so it makes more sense.
And I'll replace all instances.
Next, I will change the node name to RPM pub node.
And instead of publishing a string message type, I will instead use a float 32.
So I will replace all of our string instances like so.
I will change the topic name we are publishing to to be RPM and change the callback function name to
publish rpm so it makes more sense.
Then within our callback function, rather than setting the float message data to Hello world, I will
instead set it to a constant variable which I will call RPM in all caps, and I will then create this
variable at the top of my file and I'll just set it to ten.
Then to make it a bit clearer, I will print that the RPM publisher node is running.
Great.
That's it for our RPM publisher.
We can save the file and test that It works.
Before we open up a terminal, I'll configure our make list text file to include our new script by going
into the install section and adding the scripts slash rpm underscore pub dot pie node.
With that.
I'll save this file and we can go ahead and run a coal can build in terminal.
Or if you follow along with me in the last lecture, go to terminal and run build task to do it for
you.
So we've gone ahead and successfully built our RPM publisher node so we can go ahead and test it.
So I'll go ahead and open up a terminal source, our install setup dot bash file.
And then I can go ahead and run the node to test it out.
So we see RPM publisher Node running.
And if I go ahead and do Ross two topic list, I can go ahead and echo the contents.
And we see our constant value of ten being published.
So I'll go ahead and stop this.
Great.
The RPM node works, so let's now create our speed publisher node.
The first thing our speed publisher node will have to do is subscribe to the value being published over
the RPM topic so I can go over to my subscriber node so that I can copy it over to my newly created
code.
So I'll go over to scripts, create a new file, and I'll call this one Speed Calc dot pie.
Then I'll go ahead and copy the contents from my subscriber's file.
And I'll go ahead and change the class name to Speed calculator.
And change the node name to a speed calc node.
And then I'll change.
Instead of the string message type, we will be subscribing to a float 32 type.
As we did in our RPM publisher.
And we will also match the RPM publisher's topic name in this case RPM.
And I'll go ahead and leave the slash out of there.
You can leave slashes in the topic names, but for scalability reasons, it's better to keep them without
the slash.
When you define them, you'll see why a bit in future lectures and I will change the callback function
to be called calculate speed.
Down in the main function.
I'll change the print statement to say Speed calculator node started.
And I will change this my sub variable to be something that makes a bit more sense.
I'll just call it Speed Calc.
In our calculate speed callback function.
We currently have it set to just print the data out.
Rather than doing that, we will use this data to calculate the speed our robot is going.
First thing I will do is change our message variable to RPM message.
So it's clear what this message is for.
Then I will create a variable called speed, which I can calculate using the RPM message data multiplied
by the circumference of the wheel, which is the wheel radius multiplied by two multiplied by pi.
Which I will approximate as 3.14159.
Then, since I want my speed in meters per second, I will have to divide by 60 to convert rotations
per minute to rotations per second.
Now I need to create this wheel radius variable, which I will just do near the top of the file.
I will set it equal to 12.5 centimeters.
It is worth noting, though, that I want my speed to be in meters per second, so I'll have to convert
this to meters, which I can do by dividing the 12.5 centimeters by 100.
And I can put a note here explaining why I did this.
OC We calculated speed based off the RPM data.
Now we need to publish this speed data, which means I need to create a publisher.
I can just copy over our publisher code from our RPM pub.
And place it here and we will be publishing to the speed topic for this publisher.
So I'll go ahead and change that and I'll go ahead and copy the usage of the publisher's message data
set up.
And place them in my calculate speed callback function.
And I'll rename this speed message so it makes more sense.
And instead of loading in the RPM, we'll be loading in the calculated speed.
Great.
So let's review what is happening in this code.
I created a subscriber which subscribes to the RPM topic.
I also created a publisher which publishes to the speed topic in my subscriber callback function.
I take the RPM data and calculate the robot's speed.
Then I load the value into the float 32 speed message and publish it.
Great.
I'll save the file and head over to my make list text file to include it.
So I'll go ahead and save this and go ahead and recompile our project.
Now I can go ahead and clear my terminal and rerun my RPM publisher node.
And I'll clear this and I'll go ahead and source it.
And run our speed calc node.
Then we can go ahead and open a third terminal tab.
I'll go ahead and move these a bit.
And we can do Ross to node list and we see our two nodes are indeed running and we do Ross to topic
list.
We can see RPM and speed being published too.
So if I go ahead and echo our RPM one more time.
We get our value of ten.
And if I go ahead and echo our speed.
We get the calculated speed of this particular RPM with the wheel radius size I set in my speed calc
node.
It'll be the speed of this particular robot that has this 12.5 centimeter wheel going at ten rotations
per minute.
Congratulations.
You successfully completed your first Ross to Python project where you are able to take advantage of
Ross publishers and subscribers to process data as you would on a real robotic system.
Now notice we had to hard code the values of the RPM and wheel radius into our code.
So what happens if we wanted to use this code for robots with a different wheel size?
Well, we would need to edit our python script depending on the wheel size and rerun it.
Now that's not very functional.
So in the next lecture we are going to learn how we can use parameters to get around this issue rather
than having to edit our Python scripts every time we want to make changes.