Search code examples
pythongnuradiognuradio-companion

Trying to control a GNUradio flow graph object


Good Day:

I am trying to write some python code to control a gnuradio block. To illustrate the issue I encountered, I have created a simple flowgraph consisting of an audio source connected to the sound card. There is a single gnuradio companion WX GUI element (a variable slider) to control the audio frequency. I tried to take the python code created by gnuradio companion and create an object within python, then create two threads. One thread starts the GNUradio object, the second thread queries the user to input a frequency, then queries the object's frequency and prints it to the terminal for confirmation.

When the code is run, the audio generator starts, the WX GUI slider is shown, and the terminal prompts the user to input a frequency. When the frequency is input via the terminal query, that number is echoed back as expected but the GNUradio block does not change its frequency. Changing the frequency via the WX GUI slider works as expected.

Obviously I'm not linking the variable to the GNUradio block correctly. The code is copied below, any assistance would be appreciated. Thank you.

-Ed

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
##################################################
# GNU Radio Python Flow Graph
# Title: Top Block
# Generated: Wed Oct  4 06:04:54 2017
##################################################

if __name__ == '__main__':
    import ctypes
    import sys
    if sys.platform.startswith('linux'):
        try:
            x11 = ctypes.cdll.LoadLibrary('libX11.so')
            x11.XInitThreads()
        except:
            print "Warning: failed to XInitThreads()"

from gnuradio import analog
from gnuradio import audio
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from gnuradio.wxgui import forms
from grc_gnuradio import wxgui as grc_wxgui
from optparse import OptionParser
import wx
import threading
from threading import Thread


class top_block(grc_wxgui.top_block_gui):

    def __init__(self):
        grc_wxgui.top_block_gui.__init__(self, title="Top Block")
        _icon_path = "/usr/share/icons/hicolor/32x32/apps/gnuradio-grc.png"
        self.SetIcon(wx.Icon(_icon_path, wx.BITMAP_TYPE_ANY))

        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 32000
        self.freq = freq = 1000

        ##################################################
        # Blocks
        ##################################################
        _freq_sizer = wx.BoxSizer(wx.VERTICAL)
        self._freq_text_box = forms.text_box(
            parent=self.GetWin(),
            sizer=_freq_sizer,
            value=self.freq,
            callback=self.set_freq,
            label="frequency",
            converter=forms.float_converter(),
            proportion=0,
        )
        self._freq_slider = forms.slider(
            parent=self.GetWin(),
            sizer=_freq_sizer,
            value=self.freq,
            callback=self.set_freq,
            minimum=300,
            maximum=5000,
            num_steps=100,
            style=wx.SL_HORIZONTAL,
            cast=float,
            proportion=1,
        )
        self.Add(_freq_sizer)
        self.audio_sink_0 = audio.sink(samp_rate, "", True)
        self.analog_sig_source_x_0 = analog.sig_source_f(samp_rate, analog.GR_COS_WAVE, freq, .25, 0)

        ##################################################
        # Connections
        ##################################################
        self.connect((self.analog_sig_source_x_0, 0), (self.audio_sink_0, 0))    

    def get_samp_rate(self):
        return self.samp_rate

    def set_samp_rate(self, samp_rate):
        self.samp_rate = samp_rate
        self.analog_sig_source_x_0.set_sampling_freq(self.samp_rate)

    def get_freq(self):
        return self.freq

    def set_freq(self, freq):
        self.freq = freq
        self.analog_sig_source_x_0.set_frequency(self.freq)
        self._freq_slider.set_value(self.freq)
        self._freq_text_box.set_value(self.freq)


toneGen = top_block()

def runToneGen():
    toneGen.Start(True)
    toneGen.Wait()

def userInput():
    while True:
        freq = raw_input("Enter frequency: ")
        toneGen.freq = freq
        print "tone generator freq set to: ",toneGen.freq," Hz"


#initiate thread to query user for freq
uiThread = Thread(target=userInput, args=())
uiThread.start()

#initiate thread to run gnuradio block
gnuThread = Thread(target=runToneGen, args=())
gnuThread.start()

Solution

  • I believe I have found the answer to my own question. In the code sample above, in the 'userInput()' definition, I was trying to change the frequency of the tone generator block by directly setting the variable 'toneGen.freq'. This did not work. In order to correctly set the frequency the method 'set_freq' in the toneGen object must be used with this syntax:

    toneGen.set_freq(int(freq))

    this function call will correctly set the audio frequency of the generator and update the UI slider.