Python is a good choice if you want to become a robotics developer (i.e. program robots), especially 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 into robotics faster 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 of Python, or C++. If I have to teach them ROS, I have to start first by teaching them one of those languages.
If I convinced you to learn ROS using Python, then your next step must be to learn Python. Let’s dig in.
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 that programs written for Python 2.7 are not compatible with Python 3, and vice versa. So… which one?
In order to make a decision about this, I think that we have to concentrate on the goal we are trying 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 are some reasons why you should). So, let’s ask ROS what it thinks.
Which Python version uses ROS?
All previous ROS distributions up to 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 that, Python 2.7 is going to reach the end of its 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 for it, too, but no new bug fixing.
Finally, to clarify that from a beginner’s 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.
Included here are:
- How to create a Python file
- How to include (import) external libraries into 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 be understood after this step is the following code:
#!/usr/bin/env python import rospy if __name__ == "__main__": rospy.init_node("my_node") 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:
#!/usr/bin/env python import rospy def print_message(name): sentence = name + " keep pushing your ROS learning... " return sentence if __name__ == "__main__": rospy.init_node("my_node") full_sentence = print_message ("Rick") print (full_sentence)
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 spaghetti. The main concepts about classes that you must master are:
- How to create (define) a class. Understand what the members of the class are 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 move forward:
#!/usr/bin/env python import rospy from geometry_msgs.msg import Twist import time class MyRobot(): 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) time.sleep(1) 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 self.wheels.publish(speed_cmd) if __name__ == "__main__": rospy.init_node ("my_node") robot = MyRobot ("pepito", 0.5) robot.set_speed (0.2) rospy.spin()
Once you know the aforementioned concepts, you are ready to move on to 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
In the previous section, I indicated that 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 especially do not like learning about programming with simple examples of lists of names, conditionals based on stupid numbers, and so on. 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 of the wheels on the robot.
What I mean is that the learning of Python concepts has to be done while applying it to a robotics situation. This method makes it possible to associate robotics concepts with the 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, entitled 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 to 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 throughout 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, and 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 in that situation can help you to quickly build prototypes and test your ideas.