Introduction
In the last post the robot got FPGA PWM speed control, and one of the next steps on the list was a depth camera. This post covers adding a RealSense D435i: getting the library running on the KR260, using it from Python, streaming depth to my desktop, and the first attempt at letting the robot drive itself without hitting things. As with the previous posts, I used Claude for research, code generation and debugging. The commands and code below are the versions that worked.
Why compile librealsense at all?
On a desktop PC, installing RealSense support takes a couple of minutes: add the RealSense apt repository, install the packages, done. That's what I did on my desktop. On the KR260 it isn't that simple, and the reason is the Linux kernel.
What the camera needs. The D435i is a USB device. To use it you need librealsense (the RealSense SDK, now developed at github.com/realsenseai/librealsense) plus udev rules that let non root users open the device. Nothing about it touches the FPGA, so xmutil, which loads accelerated applications into the programmable logic, isn't involved at all. That also means PetaLinux isn't required at this stage.
Why the packages don't work. By default librealsense talks to the camera through the kernel's video (V4L) and sensor (HID) drivers. Those need RealSense-specific kernel patches for things like per-frame metadata and the IMU. On a PC, the librealsense2-dkms package applies them, but it only supports the standard Ubuntu LTS kernels on PCs. The KR260's Ubuntu runs AMD's own -xilinx kernel, so neither the DKMS package nor the patch scripts apply.
The fix: the RSUSB backend. librealsense can also be built to talk to the camera directly over USB through libusb, bypassing the kernel drivers completely. That build option is called RSUSB, and it doesn't care which kernel you run. No patches, no DKMS.
Aside: what about
pip install pyrealsense2? PyPI does have an ARM64 wheel for Python 3.12, which is what Ubuntu 24.04 uses. I downloaded it and looked inside: it's built with the kernel (V4L) backend, not RSUSB. Depth and color would probably still work through the standard USB video driver, but per-frame metadata and the IMU depend on kernel support the Kria kernel doesn't have. Compiling with RSUSB avoids the question entirely.
Compiling librealsense on the KR260
Prepare the board. The KR260 has 4 GB of RAM, and compiling librealsense can run out of memory. Adding swap and turning off the desktop as a first step helps:
sudo fallocate -l 4G /swapfile && sudo chmod 600 /swapfile
sudo mkswap /swapfile && sudo swapon /swapfile
sudo xmutil desktop_disable # re-enable later with: sudo xmutil desktop_enable
Install the dependencies and get the source. Check out a release tag rather than master. I used v2.59.1 which is the latest tagged version at the time of writing.
sudo apt install -y git cmake build-essential libssl-dev libusb-1.0-0-dev \
pkg-config libudev-dev libgtk-3-dev libglfw3-dev libgl1-mesa-dev libglu1-mesa-dev \
python3-dev
git clone https://github.com/realsenseai/librealsense.git
cd librealsense
git checkout v2.59.1
./scripts/setup_udev_rules.sh # unplug the camera first
Configure. -DFORCE_RSUSB_BACKEND=ON is the important flag. I also built the examples, the graphical tools (for realsense-viewer) and the Python bindings:
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release \
-DFORCE_RSUSB_BACKEND=ON \
-DBUILD_EXAMPLES=ON \
-DBUILD_GRAPHICAL_EXAMPLES=ON \
-DBUILD_PYTHON_BINDINGS=ON \
-DPython_EXECUTABLE=/usr/bin/python3
CMake confirms that it's building for ARM64 with NEON optimizations, and it downloads pybind11 for the Python bindings.
The bug: Python "not found". After running cmake, my first attempt failed here, even though which python3 showed /usr/bin/python3:
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.12.3", minimum required is "3.7")
CMake Error at /usr/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find Python (missing: Python_EXECUTABLE Python_INCLUDE_DIRS
Python_LIBRARIES Interpreter Development Development.Module
Development.Embed)
The Python program was there, but the development headers (Python.h) and libpython3.12 weren't. Ubuntu ships those in a separate package, and Development in the error is CMake asking for them. The fix is python3-dev (already in the apt line above), plus clearing the cache, because CMake remembers the failed search:
sudo apt install -y python3-dev
rm -rf CMakeCache.txt CMakeFiles
Build and install. Use -j2 rather than -j4, because four parallel compiles can run out of memory. This step will take around 2 hours with -j2.
make -j2
sudo make install && sudo ldconfig
Check it. Plug the camera into one of the USB ports and run:
rs-enumerate-devices
Note that the RealSense requires a USB 3 gen 2 to work at higher resolutions. Most USB A to C cables that come with phones or peripheral devices are actually USB 2 so you might need to get a USB 3 gen 2 USB A to USB C cable to make the D435i work at your required resolution. Once plugged in, it should list the D435i with a USB type of 3.x or 2.1
Using the Python library with and without uv
make install puts the Python bindings here:
/usr/local/lib/python3.12/dist-packages/pyrealsense2/pyrealsense2.cpython-312-aarch64-linux-gnu.so
Without uv. Ubuntu's system Python already searches /usr/local/lib/python3.12/dist-packages, so nothing else is needed:
python3 -c "import pyrealsense2 as rs; print(rs.__file__)"
The same goes for sudo python3, which matters later because the motor driver needs root to reach the FPGA registers through /dev/mem.
With uv. I manage my Python projects with uv, and a uv virtual environment doesn't see the system packages. You can't list a compiled .so as a dependency in pyproject.toml either, since uv only installs real packages. What works is a .pth file, which tells Python to also look in another folder:
cd ~/intel-realsense
uv python pin 3.12
uv venv --python /usr/bin/python3
echo "/usr/local/lib/python3.12/dist-packages" > .venv/lib/python3.12/site-packages/realsense.pth
uv sync
Two details matter here:
- Use Python 3.12. The .so was compiled for CPython 3.12. With requires-python = ">=3.12", uv would happily pick a newer Python, and the import would fail.
- The .pth file survives uv add and uv sync, because uv leaves alone files it didn't install. It's lost only if you delete .venv, and then you rerun the echo line.
One pyproject.toml for both machines. On the desktop, pyrealsense2 should come from PyPI. On the KR260 it comes from the source build. An environment marker handles both:
dependencies = [
"numpy>=2.5.4",
"opencv-python-headless>=5.0.0.93",
# From PyPI on the desktop (x86); on the KR260 (aarch64) it comes from the source build.
"pyrealsense2>=2.59; platform_machine != 'aarch64'",
]
Check which copy is loaded:
uv run python -c "import pyrealsense2 as rs; print(rs.__file__); print(rs.context().query_devices().size())"
The path should start with /usr/local/lib/python3.12/dist-packages/, and with the camera plugged in it prints 1.
Testing on the KR260 with a monitor and realsense-viewer
Because I built with -DBUILD_GRAPHICAL_EXAMPLES=ON, realsense-viewer also runs on the KR260's own desktop (if you disabled the desktop during the compile step run sudo xmutil desktop_enable first, with a DisplayPort monitor attached). It works, but it's very slow, especially the 3D point-cloud view. However, you can test the streaming on the 2D view.
The likely reason is graphics. realsense-viewer renders everything, and especially the 3D view, with OpenGL, and the Zynq UltraScale+ chip on the K26 module has only a small embedded GPU (a Mali-400 MP2). Capturing frames is cheap by comparison: as shown in the next section, the same board streams depth and color at full frame rate once it doesn't have to draw them. So on the robot, realsense-viewer is useful for a quick "is the camera alive" check, and the real viewing happens on the desktop.

Streaming depth from the KR260 to the desktop
Learning on the desktop first. While the CMake and make were running on the Kria, I spent some time with the camera on the desktop, where the standard packages just work and uv add pyrealsense2 is enough. I had Claude generate some examples and worked through the small scripts, each building on the previous one:
| Script | What it teaches |
|---|---|
01_distance.py |
Start a depth stream, read distance in meters, depth as a NumPy array |
02_depth_viewer.py |
Align depth to color, colorize, post-processing filters, click-to-measure, background removal |
03_measure_3d.py |
Turn a pixel plus its depth into a 3D point; measure between two clicked points |
04_pointcloud_obstacle.py |
The full point cloud as an N x 3 array, a simple "nearest obstacle ahead" detector |
05_imu.py |
Accelerometer and gyro readings, tilt from gravity |
06_record.py |
Record to a file for offline development and playback |
Aside: recordings are now
.db3. Older RealSense examples record to.bagfiles. librealsense 2.59 records to.db3(the ROS 2 bag format) and refuses.bagfile names when recording.
Splitting the work between the two machines. The idea is simple: the KR260 only captures and compresses, and the desktop does everything that needs a screen.
KR260 (camera on USB 3) Desktop
rs_server.py -- capture, align, filters ==TCP==> 01_distance_client.py
compress 02_depth_viewer_client.py
The server keeps the camera running and streams to one client at a time over TCP. Each message is a small JSON header followed by the image bytes:
[4 bytes: header length][4 bytes: payload length][JSON header][depth bytes][color bytes]
The first message after connecting carries the camera details: the depth scale (how many meters one depth unit is), the lens intrinsics, and the depth-to-color transform. After that, every message is one frame:
- Depth is sent losslessly, because every millimeter is a measurement. It's either raw or compressed with zlib or PNG.
- Color is sent as JPEG, since a little compression loss doesn't matter for viewing.
The client doesn't use pyrealsense2 at all, only NumPy and OpenCV.
Bandwidth. Uncompressed depth at 640x480 and 30 fps is 640 x 480 x 2 bytes x 30, which is about 147 Mbit/s. That's fine over gigabit Ethernet. Over Wi-Fi, --depth-enc zlib roughly halves it, at the cost of CPU time on the KR260.
Where the KR260's time goes. The server prints its frame rate, network usage and milliseconds of work per frame every 2 seconds, which makes the trade-offs visible:
- Aligning depth to the color camera happens on the KR260 by default.
--no-alignskips it to save CPU, but then background removal in the viewer is turned off. - The smoothing filters (spatial, temporal, hole filling) are by far the most expensive part on the ARM cores. They're off by default, and pressing
fin the viewer toggles them on the KR260, so you can watch the cost in the server's output.
Running it:
# KR260
.venv/bin/python rs_server.py
hostname -I
# desktop
uv run 02_depth_viewer_client.py --host <KR260 IP>
The connection has no password, so only run the server on a network you trust.

First steps toward autonomy: wandering without hitting things
The goal for this step is deliberately small: drive around randomly and turn away from anything closer than a set distance. No map yet. I had Claude generate this script and this one needs more manual review than the desktop play scripts since it makes several assumptions about the geometry of the robot.
How wander.py works. It runs on the KR260 and reads the camera directly. For each depth frame (about 30 a second), it does four things:
- Turn each depth pixel into a 3D point, measured against the floor: its height above the floor, how far ahead it is, and how far to the side.
- Throw away everything that can't hit the robot: the floor itself (anything less than 5 cm above it) and anything higher than the top of the robot.
- Split what's left into three zones, left, center (the robot's width plus a margin) and right, and find the nearest obstacle in each.
- Decide what to do:
| State | What the robot does |
|---|---|
| FORWARD | Drives along a gently changing random curve, slows down as the center gets closer, and steers away from close sides |
| TURN | Something is closer than the threshold: spins in place toward the side with more room, plus a random extra angle |
| STUCK | No gap found after 8 s of turning: stops, waits, then tries the other direction |
Safety first. A robot that drives itself needs more ways to stop than one that's driven by hand:
- Motors are off by default. Without
--drive, the script runs everything and prints its decisions, but the motors never move. - The existing controller is the emergency stop.
robot_client.pyfrom the last post works unchanged. Space or the B button stops the robot untilwander.pyis restarted, and the client's speed bars show what the autonomous code is doing. With--require-link, the robot also stops whenever the client isn't connected. - It stops if anything else goes wrong: depth frames stop arriving, Ctrl+C, or the SSH session drops.
- Ramping is shared with manual driving. It reuses the slew limiting from
robot_server.py, so speed changes ramp up and down exactly as they do under manual control.
The first bug: the floor was an obstacle. The first version measured the camera's tilt with the D435i's accelerometer and took the camera height from the command line. In a dry run on open floor, it immediately decided something was 0.4 m ahead and started turning, forever. Three small errors added up:
- The height was measured to the wrong point. I measured 15 cm from the top of the camera to the bottom of the wheel. What matters is the height of the lens, which is about 1.25 cm lower.
- The camera is slightly rolled (about 3°), and that version ignored roll, so the floor sloped upward on one side.
- The accelerometer's idea of "level" differs slightly from the depth camera's, and a tilt error of a degree or two makes the floor look higher the farther away it is.
Together these pushed the floor over the 5 cm obstacle line at about 0.4 m. The fix was to stop relying on measurements and let the camera find the floor itself. At startup, with the robot still and facing open floor, wander.py fits a plane to the depth points in the lower half of the image. It tries planes through random sets of three points and keeps the one the most points lie on (a method called RANSAC). The accelerometer is now only a hint for which way is down, so a wall can't be mistaken for the floor. The fit gives the lens height, tilt and roll directly:
floor: lens 0.137 m above the floor, pitch +9.8 deg, roll +3.0 deg (...)
A --debug-image option saves a colour-coded picture of what the robot thinks it sees, every second: green for floor, red for an obstacle in the path, orange for an obstacle at the side, and black for no depth. It was the quickest way to see why it was turning.
Chassis issues when driving autonomously
This is where I stopped testing since part of the issue was the chassis itself. The wire for the realsense was slightly tilting the camera. There was no way to mount the camera using the bottom screw since the chassis did not have a comptaible location so I had to use the more unstable screws on the back. Another chassis issue I found was that one of my tires had worn out and kept unscrewing every time it trued to reverse during testing on the ground. This was not a big issue when driving manually since I could make minute adjustments to the speed to prevent this but while driving autonomously it became a bigger issue. Lastly there is no mount for the 12 V Lipo and the large USB power bank on the chassis. I would need to 3d print one that works for the exact size of the two to prevent them from sliding. Again this was not a big issue at slower manual speeds but needs to be fixed for autonomous testing.
Conclusion and next steps
The D435i now works on the KR260 without PetaLinux. It needed librealsense compiled with the RSUSB backend, plus a small .pth trick to use it from a uv project. Splitting the work so the robot captures and the desktop displays gets around the KR260's weak graphics, and streaming depth over the network works at full frame rate. The first autonomous behaviour, random wandering with obstacle avoidance, runs and makes sensible decisions in dry runs and on the bench. Testing it properly on the ground showed that the software is now ahead of the hardware: a camera that shifts, a wheel that comes loose and batteries that slide all matter much more when nobody is correcting the robot by hand.
Next: - Chassis fixes before more autonomous testing: - A rigid camera mount, ideally a 3D-printed bracket that uses the D435i's bottom tripod screw, with strain relief so the cable can't tilt the camera. - Replacing the worn wheel and securing its screw with threadlocker. - 3D-printed holders for the 12 V LiPo and the USB power bank. - Resume testing on different floors, stationary, carpet and hardwood. That includes finding the lowest motor power that actually moves the robot and the power it needs to spin in place on each surface, since turning on carpet takes a lot more than driving straight. - Build an indoor map. To place what it sees on a map, the robot needs to know where it is, so the first step is odometry (tracking how far it has moved): wheel encoders plus the D435i's gyro.
References
- librealsense on GitHub: github.com/realsenseai/librealsense
- RealSense developer documentation: dev.realsenseai.com/docs
- Hackster: RealSense support for Kria KV260 (PetaLinux 2021.1)
- Avnet Japan: Running Intel RealSense on the KR260 (in Japanese)
- Previous posts: Getting started with AMD Kria KR 260 hardware, Driving an AMD Kria KR260 Robot over WiFi in a cardboard chassis, Adding FPGA PWM speed control to an AMD Kria KR260 robot with IBT-2 motor drivers