试图控制GNUradio流图图形对象

时间:2017-10-04 11:32:18

标签: python gnuradio gnuradio-companion

美好的一天:

我正在尝试编写一些python代码来控制gnuradio块。为了说明我遇到的问题,我创建了一个简单的流程图,其中包含一个连接到声卡的音频源。有一个gnuradio伴侣WX GUI元素(一个可变滑块)来控制音频。我尝试使用gnuradio companion创建的python代码并在python中创建一个对象,然后创建两个线程。一个线程启动GNUradio对象,第二个线程查询用户输入频率,然后查询对象的频率并将其打印到终端进行确认。

当代码运行时,音频发生器启动,显示WX GUI滑块,终端提示用户输入频率。当通过终端查询输入频率时,该数字按预期回显,但GNUradio块不会改变其频率。通过WX GUI滑块更改频率可以按预期工作。

显然我没有正确地将变量链接到GNUradio块。代码复制如下,任何帮助将不胜感激。谢谢。

-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()

1 个答案:

答案 0 :(得分:0)

我相信我找到了自己问题的答案。在上面的代码示例中,在' userInput()'定义,我试图通过直接设置变量' toneGen.freq'来改变音调发生器块的频率。这没用。为了正确设置频率,方法' set_freq'在toneGen对象中必须使用以下语法:

toneGen.set_freq(INT(频率))

此函数调用将正确设置生成器的音频并更新UI滑块。