【问题标题】:Is it possible to access work function's variable in __init__ of a GNU Radio block?是否可以在 GNU Radio 块的 __init__ 中访问工作函数的变量?
【发布时间】:2015-06-06 03:12:18
【问题描述】:

在下面的 GNU Radio 处理块中,我无法弄清楚是谁/什么首先将 input_items 的值传递给了 work 函数。是否可以将该值传递给__init__ 函数?

我有一个文件 xyz.py :

class xyz(gr.sync_block):
    """
    docstring for block add_python
    """
    def __init__(self, parent, title, order):
        gr.sync_block.__init__(self,
            name="xyz",
            in_sig=[numpy.float32,numpy.float32],
            out_sig=None)
            ................
           ................
           //I want to access the value of input_items here
           ...............
           ...............


    def work(self, input_items, output_items):
        ................

【问题讨论】:

    标签: python gnuradio


    【解决方案1】:

    __init__ 函数只被调用一次以“初始化”类的新实例。它与通过流程图移动数据无关,除了设置输入和输出类型以便成功连接块。

    所以,在top_block,你可能有:

    proc = xyz() # xyz's __init__ is called here
    self.connect(source, proc, sink) # still no input_items, just connected flowgraph
    

    稍后,您运行流程图:

    tb = top_block()
    tb.run() # 'input_items' are now passed to 'work' of each block in succession
    

    当您运行流程图时,GNU Radio 调度程序会从源块中获取一些样本并将它们放入缓冲区中。然后,它将该缓冲区传递给流程图中下一个块的 work 函数,以及用于输出项的“空”缓冲区。

    所以每个块的work 函数在有数据需要处理时由调度程序自动调用。 __init__ 无法访问work 的任何参数,因为在调用__init__input_items 甚至还没有传递给work

    【讨论】:

    • 那么我绝对没有办法访问sync_block 的__init__ 函数中的数据?那么如何访问 hier_block (github.com/a-w-s/GNURadio-SBHS-Python/blob/master/python/…) __init__ 中的数据呢?我想在顶部块框架上添加一个面板,这只能通过在__init__ 中为self.win 分配一个面板来完成(就像在 hier_block 示例中一样)。如果我尝试在work 函数内为self.win 分配一个面板,则会出现错误:'xyz' object has no attribute 'win'
    • 不,由于我上面提到的原因,在初始化块时绝对无法访问input_itemsoutput_itemshier_block 的行为与您在此问题中询问的同步块完全不同。最好为此创建一个单独的问题。
    【解决方案2】:

    work 是一个完全独立于__init__ 的函数。它的参数不能在该函数之外访问。如果要访问input_items,请将其添加到__init__参数列表中,并在调用__init__时传递。

    【讨论】:

    • 在 gnuradio 中,每个块都有一个 xml 文件。参数从 xml 文件传递​​给 init 函数。我不知道参数从哪里传递给工作函数。我想我在这里过分简化了这个问题。任何对 gnuradio 中的同步块有正确理解的人都可以提供帮助
    猜你喜欢
    • 2013-08-18
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    相关资源
    最近更新 更多