Python for Robotics
Python is the dominant programming language for modern robotics. It powers ROS 2 nodes, computer vision pipelines, deep learning inference, and high-level robot behavior. The language itself is straightforward — the key is knowing which libraries to use and how to apply them to physical systems. This guide covers exactly that.
Why Python dominates robotics
Python wasn't always the robotics language — C++ held that title for decades. But Python won for three reasons: deep learning frameworks (PyTorch, TensorFlow) are Python-first; ROS 2's rclpy client library makes writing ROS nodes in Python clean and fast; and rapid prototyping speed matters enormously when you're iterating on robot behavior.
C++ is still used for the inner control loops and hardware drivers that need every microsecond. But the planning, perception, and AI layers — everything you interact with daily as a robotics engineer — is mostly Python.
1. NumPy — The Foundation
NumPy is the numerical computing library for Python. In robotics, you'll use it constantly for sensor data, coordinate transforms, and math operations.
Arrays and matrices
NumPy arrays are the Pythonic equivalent of vectors and matrices. A robot's position is np.array([x, y, z]). A rotation matrix is a np.array([[...], [...], [...]]). Matrix multiplication is A @ B. If you've been doing robotics math by hand, NumPy is how you implement it in code.
Sensor data processing
IMU readings, LiDAR point clouds, and joint encoder values all come in as arrays of numbers. NumPy makes it trivial to filter, transform, and analyze them. A moving average filter over the last 10 IMU samples? np.mean(imu_buffer[-10:]). That's it.
2. Serial Communication — Talking to Hardware
Most microcontrollers (Arduino) talk to a Raspberry Pi or laptop via a USB serial connection. Python's pyserial library makes this straightforward.
Reading sensor data from Arduino
Your Arduino sends comma-separated values over Serial: Serial.println("1023,512,768");. In Python: import serial; ser = serial.Serial('/dev/ttyUSB0', 9600); line = ser.readline().decode().strip(); values = line.split(','). That's the complete pipeline from hardware sensor to Python variable. Build your control logic on top of this.
Sending commands to motors
The reverse: Python sends motor speed commands to Arduino. Define a simple protocol: "L150 R200\n" means "left motor 150, right motor 200". Arduino parses the string and drives the motors accordingly. This two-way serial link is how most hobby robots split intelligence (Pi) from motor control (Arduino).
3. OpenCV — Computer Vision in Python
OpenCV (Open Computer Vision) is the go-to library for image processing and basic computer vision. It's the first tool you reach for when working with camera data.
Reading and displaying frames
Three lines of Python capture and display a live camera feed: cap = cv2.VideoCapture(0), ret, frame = cap.read(), cv2.imshow('frame', frame). Everything else in computer vision builds on this loop.
Color detection and filtering
Convert to HSV color space (cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)) and create a mask for a specific color range (cv2.inRange(hsv, lower, upper)). Find the contours of the mask to locate colored objects. This is how a robot follows a colored line or finds an orange ball — without any deep learning needed.
Connecting OpenCV to ROS
The cv_bridge package converts between ROS image messages (sensor_msgs/Image) and OpenCV NumPy arrays. One function call: cv_image = self.bridge.imgmsg_to_cv2(msg, "bgr8"). Now you can process any ROS camera topic with any OpenCV operation.
4. Control Loops — The Heartbeat of a Robot
A control loop is the fundamental programming pattern of robotics: read sensors → compute output → send to actuators → repeat. Getting this loop right is everything.
The basic loop pattern
In ROS 2, control loops are implemented as timer callbacks in a node. self.create_timer(0.01, self.control_callback) calls your function every 10ms (100Hz). Inside the callback: read the latest sensor data (stored from subscriber callbacks), compute the control output, publish it to the motor topic. Simple, clean, reliable.
Why loop rate matters
A slow control loop means sluggish, unstable behavior. A balancing robot running at 10Hz will fall; at 100Hz it stays upright. Match your loop rate to the dynamics of your system — slower systems (navigation) can run at 10Hz, faster systems (balance, motor control) need 100–1000Hz.
Frequently Asked Questions
How much Python do I need to know before learning ROS?
Comfortable with variables, functions, classes, and basic file I/O. You don't need advanced Python (decorators, metaclasses, async). The ROS patterns (publisher, subscriber, timer callback) are simple and consistent — you'll learn the robotics-specific patterns quickly even with intermediate Python skills.
What is rclpy?
The official Python client library for ROS 2. It provides all the core ROS 2 functionality — creating nodes, publishers, subscribers, services, and timers — in Python. import rclpy; from rclpy.node import Node is the standard opening of every ROS 2 Python script.
Is Python too slow for real-time robot control?
For most use cases, no. Python's overhead is negligible when the bottleneck is sensor I/O or network communication. Where Python is genuinely too slow — very high frequency control loops (1kHz+) or computationally intensive C libraries — you use Cython, ctypes, or move that specific component to C++. The majority of robot code doesn't hit this limit.
What other Python libraries are important for robotics?
SciPy for signal processing and optimization. matplotlib for plotting sensor data and trajectories. PyTorch for running neural networks on the robot. transforms3d or spatialmath for rotation representations. gymnasium (formerly OpenAI Gym) for reinforcement learning environments.
Frequently Asked Questions
What will I learn here?
This page covers the core concepts and techniques you need to understand the topic and progress confidently to the next lesson.
How should I use this page?
Start with the overview, then follow the section links to deepen your understanding. Use the table of contents on the right to jump to specific sections.
What should I read next?
Use the navigation below to continue to the next lesson or explore related topics.