GNUradio Python Tutorial 20090609
GNUradio Python Tutorial 20090609
KC Huang
WINLAB
Outlines
Python Introduction Understanding & Using GNU Radio
What is GNU Radio architecture? How to program in GNUradio? (Python and C++)
WINLAB
Python - running
Why Python?
Object-oriented Free Mixable (python/c++)
Python scripts can be written in text files with the suffix .py
Example:
$ python script.py This will simply execute the script and return to the terminal afterwards
WINLAB
Python - format
Module: a python file containing definitions and statements
from pick_bitrate import pick_tx_bitrate (from file import function) from gnuradio import gr, (or *) (from package import subpackage or all) Some modules are built-in e.g. sys (import sys)
Example: while b < 10: print b return Body of loop has to be indented by the same amount to indicate statements of the loop WINLAB
WINLAB
Objects
x = ClassName() creates a new instance of this class and assigns the object to the variable x Initial state: for instantiation and parameter pass def __init__(self): <statement-1> WINLAB
WINLAB
GNUradio Architecture
Sender
User-defined Code PC USRP USB FPGA mother board DAC RF Front end daughter board
GNU radio has provided some useful APIs What we are interested in at this time is how to use the existing modules that has been provided in GNU radio project to communicate between two end systems
WINLAB
Sink: No outputs
audio_alsa_sink,usrp_sink
V1 C++
V1 C++ Source
At python level, what we need to do is always just to draw a diagram showing the signal flow from the source to the sink in our mind.
WINLAB
The scheduler is using Pythons built-in module threading, to control the starting, stopping or waiting operations of the signal flow graph.
http://mobiledevices.kom.aau.dk/fileadmin/mobiledevices/teaching/software_testing/Gnu_radio_lecture.pdf
WINLAB
#!/usr/bin/env python from from from from gnuradio import gr gnuradio import audio gnuradio.eng_option import eng_option optparse import OptionParser
Import modules from GNU Radio library 1. The import command is similar to the #include directive in C/C++. 2. gr is the basic GNU Radio module
WINLAB
The class called my_top_block is derived from another class, gr.top_block gr.top_block class maintains the graph and also provides all the functions to build and connect blocks Instantiating a flow graph object and the parent constructor is called
WINLAB
WINLAB
Setting up sinewaves at 350 and 440 Hz and sampling rate by command-line Create signal generating blocks defining destination connecting source and destinations, left and right channel (it specifically connects src0 to port 0 of dst )
WINLAB
WINLAB
GNUradio modules
from gnuradio import MODULENAME
gr usrp audio The main GNU Radio library. You will nearly always need this. USRP sources and sinks and controls. Soundcard controls (sources, sinks). You can use this to send or receive audio to the sound cards, but you can also use your sound card as a narrow band receiver with an external RF frontend. This module contains additional blocks written in Python which include often-used tasks like modulators and demodulators, some extra filter code, resamplers, squelch and so on. Adds some functions to deals with numbers in engineering notation such as `100M' for 100 * 106'.
blks2
Use from gnuradio.eng_options import eng_options to import this feature. This module extends Pythons optparse module to understand engineering notation (see above). Miscellaneous utilities, mathematical and others.
WINLAB
GNUradio scheduler
run() The simplest way to run a flow graph. Calls start(), then wait(). Used to run a flow graph that will stop on its own, or to run a flow graph indefinitely until SIGINT is received. Start the contained flow graph. Returns to the caller once the threads are created. Stop the running flow graph. Notifies each thread created by the scheduler to shutdown, then returns to caller. start()
stop()
wait()
Wait for a flow graph to complete. Flowgraphs complete when either (1) all blocks indicate that they are done, or (2) after stop has been called to request shutdown. Lock a flow graph in preparation for reconfiguration.
lock()
unlock ()
Unlock a flow graph in preparation for reconfiguration. When an equal number of calls to lock() and unlock() have occurred, the flow graph will be restarted automatically.
WINLAB
Three components
my_block_xx.h: Block definition my_block_xx.cc: Block implementation my_block_xx.i: Python bindings (SWIG interface) (SWIG, a tool that generates wrapper code around your C++ functions and classes, so that they are callable from Python)
WINLAB
Useful Resource
GNUradio:
Homepage (download, more links, etc) More comprehensive tutorial
http://gnuradio.org/trac/ http://gnuradio.org/trac/wiki/Tutorials/WritePythonApplications http://gnuradio.org/doc/doxygen/hierarchy.html http://www.gnu.org/software/gnuradio/mailinglists.html https://www.cgran.org/
Python:
http://docs.python.org - online version of built-in Python function documentation http://laurent.pointal.org/python/pqrc - Python Quick Reference Card http://rgruet.free.fr - long version of Python Quick Reference Card http://mail.python.org - extensive Python forum
WINLAB