再会:
我正在尝试编写一些 python 代码来控制 gnuradio 块。为了说明我遇到的问题,我创建了一个简单的流程图,其中包含连接到声卡的音频源。有一个单独的 gnuradio 配套 WX GUI 元素(可变滑块)来控制音频频率。我尝试使用 gnuradio 伴侣创建的 python 代码并在 python 中创建一个对象,然后创建两个线程。一个线程启动 GNUradio 对象,第二个线程查询用户输入频率,然后查询对象的频率并将其打印到终端进行确认。
运行代码时,音频发生器启动,显示 WX GUI 滑块,终端提示用户输入频率。当通过终端查询输入频率时,该数字会按预期回显,但 GNUradio 块不会更改其频率。通过 WX GUI 滑块更改频率按预期工作。
显然我没有正确地将变量链接到 GNUradio 块。代码复制如下,任何帮助将不胜感激。谢谢你。
-埃德
#!/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()