data types and variables - ori-systems/work_experience GitHub Wiki
First, let's launch the robot simulator. We can open a new terminal from the activities (top-left corner or use the window key), the side dock panel, or with the shortcut Ctrl+Alt+T
.
Launch robot simulator
roslaunch turtlebot3_gazebo turtlebot3_stage_1.launch
Operate the robot
In order to teleoperate the robot with the keyboard, launch the teleoperation node with the below command in a new terminal window. You can use Ctrl+Shift+T
to open a new tab in the same window.
roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch
Feel free to play around with the simulator. You can find instructions on how to move the robot in the existing terminal. Once you are done with it, please kill the node with Ctrl+C
.
For an example, have a look at this video:
For resetting the simulation open another terminal and run:
rosservice call /gazebo/reset_simulation "{}"
Note: You may see that the script pyscript1.py
already exists. You can work with the same script or name your file pyscript2.py
for this exercise.
Let's create a new Python script named pyscript1.py
, which we will use for testing
Execute in a terminal:
cd ~/work_experience_ws/src/work_experience
cd robot_control
touch pyscript1.py
NOTE: In linux, the touch command is used to create a new empty file from the command line. Alternatively, if you prefer, you can use an IDE or a text editor to create the file.
After the file creation, you should end up with the following files inside your robot_control
folder.
Now, open the pyscript1.py
file and copy the following contents inside the pyscript1.py
script:
from robot_control_class import RobotControl
rc = RobotControl()
a = rc.get_laser(0)
print ("The distance measured is: ", a, " m.")
Using the python or python3 command is the simplest and most straightforward method for executing a Python script. This command must be used along with the file path and file name of the script. Typically, we tend to navigate to the file's directory and run the command with just the file name instead of providing the full file path. In this case, if you follow along the commands as instructed before you will be already located inside the directory where the pyscript1.py file is located. Therefore, in this case, all you need to do to run the script is to type the following inside a terminal:
python pyscript1.py
You should get an output like this one:
[INFO] [1676363986.715301, 0.000000]: Robot Turtlebot...
[INFO] [1676363986.716817, 0.000000]: Checking Laser...
[INFO] [1676363986.996523, 132.004000]: Checking Laser...DONE
The distance measured is: 2.513871192932129 m.
The first thing we can see in the script is the following line:
from robot_control_class import RobotControl
There, we are importing a Python class named RobotControl
. This class is the one we have created to help you interact with the ROS robot without having to actually use ROS.
What does it mean to import?
Imports are very useful and common in Python. Imports allow you to include in your program the code created in other Python modules. This means that the Python code in your program can execute the Python code defined in another program file (in this case, the code of the RobotControl class), without having to rewrite it.
To import the code of another Python module, indicate the following:
from <name_of_the_module> import <method_or_class_to_be_imported>
In our case, we are importing a Python class named RobotControl
, which is defined in the Python module named robot_control_class
.
Creating a variable
The next line we can see is the following one:
rc = RobotControl()
Here we are creating a variable named rc
, which is of the RobotControl
type. We will use that variable to call the methods of that class that allow us to get robot sensor data.
Calling a method of the class
The next line of the code is:
a = rc.get_laser(0)
Three new things here:
- We are calling the get_laser() method, which is provided by the RobotControl class.
- We are providing a parameter to the method (the number 0).
- Then, we are storing the output of the method into the new variable a.
What is this get_laser() method?
The method get_laser (ray_number)
: this method allows you to get data from the laser of the robot. When you call this method, it will return the distance measured by the laser ray that you specify as the parameter.
The parameter ray_number
: here is where you specify a number between 0
and 359
, which will indicate the ray of the laser reading you want to get the measured distance.
Right now, this may sound a bit confusing, so let's try to explain it. As you can see in the robot in the simulation, it has a laser mounted on its top plate.
This laser is projecting many beams (360 to be precise) in a 2D plane parallel to the ground. Those rays are measuring distance to the closest obstacle that intercepts the ray. Those beams are projected from the laser in all directions, covering a range of 360 degrees
This image represents the laser beams being projected from the laser.
If we pass the number 0
to the get_laser()
method, we will get the reading of the laser beam in front of the robot.
So, by calling the get_laser()
method with the number 0
as argument, the method will return the reading of the laser right in front of the robot. This means that we are measuring the distance to the closest obstacle just in front of the robot. And what do we have right in front of the robot? Well, we have the wall.
Getting the output of the method
When you call the get_laser()
method, the method does its magic and computes the distance detected by the ray you specified. Then, it returns that value to the caller.
a = rc.get_laser(0)
This means that calling the method provides a result that must be stored somewhere. That somewhere is the variable a
.
The type of the variable is automatically assigned by the Python system, and it matches the type of the value that is returned by the get_laser()
method. This means you don't need to know the type before you operate with it.
Showing data on the screen
Finally, in the last line of our script, we have a print()
method.
print ("The distance measured is: ", a)
In this case, we are printing the output provided by the get_laser()
method, that is, the distance measured by the ray.
As you can see, the script you have executed is very simple, but it will allow us to introduce a couple of very important concepts that you will need for all your Python scripts: data types and variables.
A variable can be seen as a container that stores some data: it can be a number, text, or more complex data types.
While our program is being executed, variables can be accessed or even changed, which means that a new value will be assigned to the variable.
In most programming languages, like C++ or Java, you need to declare a variable before being able to use it. But in Python, this is much easier. There's no need to declare a variable in Python. If you want to use a variable, you just think of a name and start assigning a value to it! You can see this in the following example:
a = 5
b = 'This is a string'
c = ['This', 'is', 'a', 'list', 1, 2, 3]
Inside the ~/work_experience_ws/src/robot_control
folder, create a new Python script named variables.py
. Inside this script, add the necessary code that does the following:
- First, call the method
get_laser()
, with any number, and store its response into a variable namedlaser1
. - Second, print this value, in order to see what you get.
- Then, call the
get_laser()
method again, with a different number, and store its response in another variable namedlaser2
. - Print this value, in order to see what you get now.
- Finally, call the
get_laser()
method one last time, with a different number. Store its response in the same variablelaser2
. - Print again the value of this
laser2
variable, to see what you get now.
Please try to do it by yourself unless you get stuck or need some inspiration. You will learn much more if you fight for each exercise.
variables.py
from robot_control_class import RobotControl
robotcontrol = RobotControl()
laser1 = robotcontrol.get_laser(0)
print ("The laser value received is: ", laser1)
laser2 = robotcontrol.get_laser(180)
print ("The laser value received is: ", laser2)
laser2 = robotcontrol.get_laser(359)
print ("The laser value received is: ", laser2)
Execute in a terminal:
cd ~/work_experience_ws/src/work_experience
cd robot_control
python variables.py
Every value in Python has a data type. Basically, the data type of a variable indicates what this variable contains: Is it a number? Is it text?
There are various data types in Python. For this chapter, we are going to introduce just some of the most important ones.
- Numbers
- Strings
- Lists
- Tuples
- Dictionaries
To gain a deeper understanding of data types in Python, please follow the link provided below. By exploring the linked resource, you can explore the various data types supported in Python and enhance your knowledge in this fundamental aspect of the language.
- https://www.digitalocean.com/community/tutorials/python-data-types
- https://www.dataquest.io/blog/data-types-in-python/
Inside the ~/work_experience_ws/src/robot_control
folder, Create a new Python script named lists.py
. Inside this script, add the necessary code that does the following:
- First, call the
get_laser_full()
method, and store its response in a Python list. - Then, print the positions
0
,180
, and359
from the full list of readings.
Please try to do it by yourself unless you get stuck or need some inspiration. You will learn much more if you fight for each exercise.
lists.py
from robot_control_class import RobotControl
rc = RobotControl()
l = rc.get_laser_full()
print ("Position 0: ", l[0])
print ("Position 180: ", l[180])
print ("Position 359: ", l[359])
Execute in a terminal:
cd ~/work_experience_ws/src/work_experience
cd robot_control
python lists.py
Inside the ~/work_experience_ws/src/robot_control
folder, create a new Python script named dictionaries.py
. Inside this script, add the necessary code that does the following:
-
First, call the
get_laser_full()
method, and will store its response in a Python list. -
Then, create a dictionary that will contain the position in the list and its corresponding value as key-value pairs. Check the example below:
- Position 1: 5
- Position 52: 32
- Position 231: 0
- Position 344: 21
Do this for the following positions in the list:
0, 50, 100, 150, 200, 250, 300, 359
. -
Finally, print the resulting dictionary.
Please try to do it by yourself unless you get stuck or need some inspiration. You will learn much more if you fight for each exercise.
dictionaries.py
from robot_control_class import RobotControl
rc = RobotControl()
l = rc.get_laser_full()
dict = {"P0": l[0], "P50": l[50], "P100": l[100], "P150": l[150], "P200": l[200], "P250": l[250], "P300": l[300], "P359": l[359]}
print (dict)
Execute in a terminal:
cd ~/work_experience_ws/src/work_experience
cd robot_control
python dictionaries.py