Gnuradio(3.10)创建OOT自定义模块--USRP X410软件无线电平台开发


平台环境

环境平台:PC win10 + 虚拟机Vmware15.5(Ubuntu21.04)+ Gnuradio 3.10.0.0git

一、OOT模块(Out-Of-Tree Module)

OOT模块是指Gnuradio中并不包含的模块资源,用户根据需求自己定义来制作。编写OOT模块的方法多种,推荐采用gr_modtool工具(安装Gnuradio时默认已安装)。

二、创建OOT模块

参考链接:https://wiki.gnuradio.org/index.php/OutOfTreeModules

1.利用gr_modtool创建模块框架文件

执行指令如下:
$ gr_modtool newmod howto
在这里插入图片描述
创建文件夹名称为"gr-howto"的模块工程文件如上图,gr-howto文件夹中包含若干个文件,其中lib和include文件存放着C++文件和头文件,python文件夹存放单元测试文件和python模块文件。对于Gnuradio 3.8及以前版本,还会包含swig文件,其内部文件负责着C++和python文件的接口和联合封装,在3.9后的版本中,不再使用swig,而是采用pybind11,相关文件存放在python的binding文件夹里。
app文件夹存放已经装载在Gnuradio和编译完成的模块应用文件;grc文件中存放着一个关键的yml文件(GR3.8版本前为xml文件),是已经编辑好的OOT模块与Gnuradio接口桥梁。
OOT涉及关键文件关系如下图:
在这里插入图片描述

2.编写一个C++模块(名称为square_ff)

(1)通过gr_modtool工具添加block相关文件。
举例设计一个block,作用是将输入的single float数据计算平方,再输出single float数据,block命名为square_ff,尾缀的"ff"表示输入输出参数都是float(‘f’)。该模块作为gr-howto Python module目录下的模块之一,python调用该block时可以如下语句:
Import howto
Sqr = howto.square_ff()
通过 gr_modtool 工具创建模块文件,依次输入指令如下图:
在这里插入图片描述
注意Gnuradio版本不同,文件结构也有区别(左侧图为Gnuradio3.8版本,右侧为3.9及以上版本):
在这里插入图片描述“-t general’'表示block type是通用类型,”-l cpp square_ff"表示文件类型为cpp(c++语言,也可采用python设计),block名称为square_ff。
当前可以没有版权指定名(默认为gr-module的作者),这里输入gnuradio.org,也无默认参数default arguments。选择python QA code而不选择C++ QA code,QA程序用作编写block的功能测试。
(2)编写QA测试文件(GR3.9以上版本)
QA文件在gr-howto/python文件夹下,名称为qa_square_ff.py.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2021 gnuradio.org.
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

from gnuradio import gr, gr_unittest
from gnuradio import blocks
 
try:
    from howto import square_ff
except ImportError:
    import os
    import sys
    dirname, filename = os.path.split(os.path.abspath(__file__))
    sys.path.append(os.path.join(dirname, "bindings"))
    from howto import square_ff
 
class qa_square_ff(gr_unittest.TestCase):
 
    def setUp(self):
        self.tb = gr.top_block()
 
    def tearDown(self):
        self.tb = None
 
    def test_instance(self):
        # FIXME: Test will fail until you pass sensible arguments to the constructor
        instance = square_ff()
 
    def test_001_square_ff(self):      
        src_data = (-3, 4, -5.5, 2, 3)
        expected_result = (9, 16, 30.25, 4, 9)
        src = blocks.vector_source_f(src_data)
        sqr = square_ff()
        dst = blocks.vector_sink_f()
        self.tb.connect(src, sqr)
        self.tb.connect(sqr,dst)
        self.tb.run()
        result_data = dst.data()
        self.assertFloatTuplesAlmostEqual(expected_result, result_data, 6)
        # check data

if __name__ == '__main__':
    gr_unittest.run(qa_square_ff, "qa_square_ff.yaml")

(3) 编写block C++文件
OOT的C++描述文件位于gr-howto/lib文件夹下,名称为square_ff_impl.cc和square_ff_impl.h。其中,.h文件已经生成完备,通常不需要修改。
square_ff_impl.cc修改后完整文件如下:

/* -*- c++ -*- */
/*
 * Copyright 2021 gnuradio.org.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "square_ff_impl.h"
#include <gnuradio/io_signature.h>

namespace gr {
namespace howto {

using input_type = float;
using output_type = float;
square_ff::sptr square_ff::make() { return gnuradio::make_block_sptr<square_ff_impl>(); }

/*
 * The private constructor
 */
square_ff_impl::square_ff_impl()
    : gr::block("square_ff",
                gr::io_signature::make(
                    1 /* min inputs */, 1 /* max inputs */, sizeof(input_type)),
                gr::io_signature::make(
                    1 /* min outputs */, 1 /*max outputs */, sizeof(output_type)))
{
}

/*
 * Our virtual destructor.
 */
square_ff_impl::~square_ff_impl() {}

void square_ff_impl::forecast(int noutput_items, gr_vector_int& ninput_items_required)
{
    /* <+forecast+> e.g. ninput_items_required[0] = noutput_items */
    ninput_items_required[0] = noutput_items;
}

int square_ff_impl::general_work(int noutput_items,
                                 gr_vector_int& ninput_items,
                                 gr_vector_const_void_star& input_items,
                                 gr_vector_void_star& output_items)
{
      const float *in = (const float *) input_items[0];
      float *out = (float *) output_items[0];

      for(int i = 0; i < noutput_items; i++) {
        out[i] = in[i] * in[i];
      }

      // Tell runtime system how many input items we consumed on
      // each input stream.
      consume_each (noutput_items);

      // Tell runtime system how many output items we produced.
      return noutput_items;
}

} /* namespace howto */
} /* namespace gr */

square_ff_impl.h:

/* -*- c++ -*- */
/*
 * Copyright 2021 
gnuradio.org.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#ifndef INCLUDED_HOWTO_SQUARE_FF_IMPL_H
#define INCLUDED_HOWTO_SQUARE_FF_IMPL_H

#include <howto/square_ff.h>

namespace gr {
namespace howto {

class square_ff_impl : public square_ff
{
private:
    // Nothing to declare in this block.

public:
    square_ff_impl();
    ~square_ff_impl();

    // Where all the action really happens
    void forecast(int noutput_items, gr_vector_int& ninput_items_required);

    int general_work(int noutput_items,
                     gr_vector_int& ninput_items,
                     gr_vector_const_void_star& input_items,
                     gr_vector_void_star& output_items);
};

} // namespace howto
} // namespace gr

#endif /* INCLUDED_HOWTO_SQUARE_FF_IMPL_H */

(4) 进行编译和测试:gr-howto module文件夹下依次输入如下指令
$mkdir build
$cd build
$cmake . ./
在这里插入图片描述
再执行make:
$make
make完成后即可进行QA测试文件的测试:
$make test
在这里插入图片描述
一切正常,测试成功,如果出现错误,可使用 $ctest -V,查看具体错误报告。
(5)安装module和block至Gnuradio
$ gr_modtool makeyaml square_ff
运行指令,自动更新yml文件,更新后通常还需要一些修改。
在这里插入图片描述
注意:gr_modtool更新生成的yml文件,其中的注释必须要删除!!!
在这里插入图片描述修改后的yaml文件如下:

id: howto_square_ff
label: square_ff
category: '[howto]'
templates:
  imports: import howto
  make: howto.square_ff()
inputs:
- label: in
  domain: stream
  dtype: float
  multiplicity: 1 
outputs:
- label: out
  domain: stream
  dtype: float
  multiplicity: 1 
file_format: 1

回到build路径下,进行OOT的安装指令(只有安装后才能在Gnuradio界面显示)
$sudo make install
$sudo ldconfig
安装完后打开Gnuradio界面,可以看到右侧module栏增加了howto,包含block square_ff。
在这里插入图片描述
至此单个block的设计和安装过程结束。

3.在OOT module中继续添加一个模块(名称为square2_ff)

(1)在gr-howto文件夹下执行指令:
$ gr_modtool add square2_ff
该模块与上述的square_ff的模块类型不同,是Sync,即输入输出流数据比例是1:1,也可看做general通用类型的子类,在cpp代码上也有一些区别。
执行步骤同上:
在这里插入图片描述
此次添加的新模块需要执行(修改原block的源码也需要):
$gr_modtool bind square2_ff
在这里插入图片描述
补充:当.cc源文件调用一些malloc等std库时,可能存在版本问题报错,如下:
在这里插入图片描述
Ubuntu21.10下,GNU Radio3.10git 在进行OOT block的绑定时,执行gr_modtool bind …指令,报错如上图:
通过终端命令:python3 -c “import pygccxml; print(pygccxml.version)”
可以看到已经pygccxml的版本为2.2.1(最新版),可通过卸载pygccxml,解决上述问题,运行指令:
$ pip uninstall pygccxml
在这里插入图片描述

再次运行gr_modtool指令,成功bind。

(2) 依次修改文件:qa_square2_ff.py,square2_ff_impl.cc,howto_square2_ff.block.yml,完整代码依次如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2021 gnuradio.org.
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
from gnuradio import gr, gr_unittest
from gnuradio import blocks
try:
    from howto import square2_ff
except ImportError:
    import os
    import sys
    dirname, filename = os.path.split(os.path.abspath(__file__))
    sys.path.append(os.path.join(dirname, "bindings"))
    from howto import square2_ff

class qa_square2_ff(gr_unittest.TestCase):

    def setUp(self):
        self.tb = gr.top_block()

    def tearDown(self):
        self.tb = None

    def test_instance(self):
        # FIXME: Test will fail until you pass sensible arguments to the constructor
        instance = square2_ff()

    def test_001_square2_ff(self):      
        src_data = (-3, 4, -5.5, 2, 3)
        expected_result = (9, 16, 30.25, 4, 9)
        src = blocks.vector_source_f(src_data)
        sqr = square2_ff()
        dst = blocks.vector_sink_f()
        self.tb.connect(src, sqr)
        self.tb.connect(sqr,dst)
        self.tb.run()
        result_data = dst.data()
        self.assertFloatTuplesAlmostEqual(expected_result, result_data, 6)
        # check data

if __name__ == '__main__':
    gr_unittest.run(qa_square2_ff)

/* -*- c++ -*- */
/*
 * Copyright 2021 
gnuradio.org.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "square2_ff_impl.h"
#include <gnuradio/io_signature.h>

namespace gr {
namespace howto {

using input_type = float;
using output_type = float;
square2_ff::sptr square2_ff::make() { return gnuradio::make_block_sptr<square2_ff_impl>(); }
/*
 * The private constructor
 */
square2_ff_impl::square2_ff_impl()
    : gr::sync_block("square2_ff",
                     gr::io_signature::make(
                         1 /* min inputs */, 1 /* max inputs */, sizeof(input_type)),
                     gr::io_signature::make(
                         1 /* min outputs */, 1 /*max outputs */, sizeof(output_type)))
{
}

/*
 * Our virtual destructor.
 */
square2_ff_impl::~square2_ff_impl() {}

int square2_ff_impl::work(int noutput_items,
                          gr_vector_const_void_star& input_items,
                          gr_vector_void_star& output_items)
{
      const float *in = (const float *) input_items[0];
      float *out = (float *) output_items[0];

      for(int i = 0; i < noutput_items; i++) {
        out[i] = in[i] * in[i];
      }

    return noutput_items;
}

} /* namespace howto */
} /* namespace gr */

id: howto_square2_ff
label: square2_ff
category: '[howto]'
templates:
  imports: import howto
  make: howto.square2_ff()
inputs:
- label: in
  domain: stream
  dtype: float
  multiplicity: 1 
outputs:
- label: out
  domain: stream
  dtype: float
  multiplicity: 1 
file_format: 1

(3) 进入build文件夹,依次执行命令:
$cmake . ./
$make
执行make过程中就遇到如下错误:
在这里插入图片描述Pydoc.h文件是gr_modtool工具自动产生的,提示#include包含错误,怀疑是square2_ff作为后续block添加,可能与之前已经执行过的make指令冲突,因此想到一种极端方法,清空删除build内的所有文件,再重新执行make的一套指令(因square_ff已经调试确认无误,重新make也不会影响square_ff)。
$cmake . ./
$make
$make test
再次测试,测试通过
在这里插入图片描述
(4) 再进行OOT的安装,因已经修改了yml文件,可以直接执行如下指令:
$sudo make install
$sudo ldconfig
至此完成所有安装工作!!!Gnuradio软件界面可以看到两个block均存在,且可正常使用。
在这里插入图片描述

三、运行测试

流程图及运行结果如下(signal1和2重合,两个block运行结果一致)
在这里插入图片描述

总结

对于OOT模块创建,module下添加单个block很顺利,但添加多个block模块时,按上述方法略为复杂,还需继续根据官方步骤完善。

  • 7
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hongney

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值