Learning Python for Robotics
Python is a good choice if you want to become a robotics developer (i.e. program robots), specially if you want to program your robots with ROS. On a previous post, I discussed about whether to learn ROS using C++ or using Python. On that post, I argued that, if you know neither Python nor C++ nor ROS, then your best bet to get faster into robotics is to learn robotics using Python.
However, as a teacher of ROS at the Master of Robotics and Automation of the University of LaSalle in Barcelona, I find the same situation every year: the students that arrive to learn ROS have no knowledge about Python, neither C++. If I have to teach them ROS, I have to start first by teaching one of those languages.
If I convinced you to learn ROS using Python, then your next step must be to learn Python first. Let’s dig into it.
Python 2.7 or Python 3?
Ok, so you are fully into learning Python. Then you will hear that at present, there are two versions of Python available and that they cannot be mixed. That means, programs written for Python 2.7 are not compatible with Python 3, and viceversa. So… which one?
In order to take a decision about this, I think that we have to concentrate on the goal we try to achieve. You are here because you want to become a robotics developer. Furthermore, you want to become a robotics developer based on ROS (here some reasons why you should). So let’s ask ROS about what does it thinks.
Which Python version uses ROS?
All previous ROS distributions until today (ROS Melodic) have been based on Python 2.7. This means that all the code already available that runs for those distributions needs to run on Python 2.7.
However, the next distribution of ROS 1 (ROS Noetic, to be released in 2020) is going to be fully based on Python 3 (that is going to be a lot of fun!), as indicated by main ROS developer Tully Foot in this interview.
Additionally, the next generation of ROS, that is, ROS 2, works only on Python 3, so I think that the answer is clear from the point of view of ROS: learn Python 3.
Which Python version for other important libraries?
Apart from ROS, you will have to use many other libraries to build your robot programs.
For example, if you want to do image manipulation, then you will probably use OpenCV. If you want to use machine learning with the robot, you will probably use Scikit, OpenAI-Baselines or Tensorflow/Keras. If you want to use numerical computation you may use scipy, pandas, or numpy. Except for OpenAI-Baselines, all of them work with both Python versions.
Check this page for a complete list of the most important Python libraries ready for Python 3.
On top of all said, Python 2.7 is going to reach its end of life in 2020. That doesn’t mean that your Python 2.7 programs will stop working on that date! It just means that no additional development will be done by the organization that develops Python (that is, The Python Software Foundation). So all the Python software already existing will continue to work, and you will still be able to develop also for it, but no new bug fixing
Finally, clarify that from a beginner point of view, the differences between Python 2.7 and Python 3 are very small. My advice is, if you are a beginner, it will be a safe bet to start with Python 3, and then, learn the small differences that affect your personal case when they arise.
What to learn of Python
If you want to start quick with robotics and ROS, I would say that you only need to understand the following basic concepts of Python:
1. How to create and execute a Python program
This is the basic step that will allow you to run your Python programs and also the programs of third parties.
- How to create a Python file
- How to include (import) external libraries in your Python programs (and how to use them)
- What is the role of indentation in Python
- How to provide execution permissions to the Python files
An example of code that must understand after this step is the following code:
if __name__ == "__main__":
print ("Keep pushing your ROS learning!")
You must understand how to create that program, how to provide permissions for execution, and how to actually execute it.
2. Basic Python language:
In this step, you must understand the types of variables (including lists and dictionaries), control loops, conditionals, and operators that Python provides to operate.
This is the typical basic knowledge that you must learn for each programming language. Not a lot to say here…
A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. Python provides many built-in functions like print(), etc, but you can also create your own.
The important points to understand about functions are:
- How to create a function: definition,
- How to call a function
- How to provide arguments
- How to return values
After this step, you need to understand the following simple example of a function that receives a parameter and returns a value when called from the main program:
sentence = name + " keep pushing your ROS learning... "
if __name__ == "__main__":
full_sentence = print_message ("Rick")
4. Classes and objects
A Python class (also called, an object), is simply a collection of data (variables) and methods (functions) that act on those data. Classes are the basis of good robotics programming and you must master them if you want to create complex robotics programs that do not become spaguetti. The main concepts about classes that you must master are:
- How to create (define) a class. Understand what are the members of the class and the constructor. Pay special attention to the word self which indicates that that variable/function is a member of the class
- How to instantiate a class
- How to call the members of a class from an external program
After this section, you should be able to understand the following code, which makes a robot named pepito to move forward:
from geometry_msgs.msg import Twist
def __init__(self, name, speed):
self.robot_name = name
self.max_speed = speed
self.current_speed = 0.0
self.wheels = rospy.Publisher ("/cmd_vel", Twist, queue_size=0)
print ("Robot initialised")
def set_speed(self, speed):
print ("Sending speed: "+ str(speed))
self.current_speed = speed
speed_cmd = Twist()
speed_cmd.linear.x = speed
if __name__ == "__main__":
robot = MyRobot ("pepito", 0.5)
Once you know the previous concepts, you are ready to move into learning ROS. Of course, there is a lot more about Python than what I have indicated above, but you don’t need to master the language to start using it to learn ROS. Remember, the goal here is to learn and apply ROS to the control of robots.
Learn Python for robotics
On the previous section, I indicated you must learn Python if you want to become a robotics developer. However, what I really recommend is that you learn Python while applying it to robot control.
I specially do not like learning about programming with simple examples of lists of names, conditionals based on stupid numbers or so. I do prefer to learn those programming concepts with a robotics application in mind. For example, I would like to know that a list allows me to include the full list of ranges obtained from the laser, which indicates distances to the closest obstacles. Or conditionals based on how close the obstacles are to the robot. Or loops for controlling the speed in the wheels of the robot.
What I mean is that the learning of the Python concept has to be done while applying it to a robotics situation. This method makes possible to associate robotics concepts with Python language. You become a robotics developer at the same time that you learn the language.
With this purpose in mind, we have created a specific course in which we teach Python while applying your learning to the control of robots. This is a completely free course where we teach all the Python 3 concepts discussed above while you apply it to simulated robots.
The course is available with the title Python 3 for Robotics, and has the following chapters:
ACCESS THE Python 3 FOR ROBOTICS COURSE HERE (it is fully free, forever)
Some interesting questions before starting the course
Do I have to know about robotics?
No. You will be introduced in the required (simple) concepts of robotics while learning Python.
Do I have to know about ROS?
No. Even if you are going to control (simulated) robots along the course, you are not going to use ROS for it. The robots that you will use run ROS underneath, but it will be hidden from you. You will get data from the sensors and send commands to the wheels using pure Python classes provided by us
Is Python the best language for robotics?
This is a very common question and the answer is that it depends on what you want to do, which type of robotics application you want to build, which type of robot you want to use. In a general sense, I would say that if you are doing robotics research, then Python is your best bet. In case you are working for a company, then maybe C++ is better suited for you, even if using Python is that situation can help you to quickly build prototypes and test your ideas.
- Click to share on Twitter (Opens in new window)
- Click to share on Facebook (Opens in new window)
- Click to share on Reddit (Opens in new window)
- Click to email this to a friend (Opens in new window)
- Click to print (Opens in new window)
- Click to share on Google+ (Opens in new window)
- Click to share on Tumblr (Opens in new window)
- Share on Skype (Opens in new window)
- Click to share on Pinterest (Opens in new window)
- Click to share on WhatsApp (Opens in new window)
- Click to share on Telegram (Opens in new window)