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

本文主要是介绍Gnuradio(3.10)创建OOT自定义模块--USRP X410软件无线电平台开发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • 平台环境
  • 一、OOT模块(Out-Of-Tree Module)
  • 二、创建OOT模块
    • 1.利用gr_modtool创建模块框架文件
    • 2.编写一个C++模块(名称为square_ff)
    • 3.在OOT module中继续添加一个模块(名称为square2_ff)
  • 三、运行测试
  • 总结


平台环境

环境平台: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 blockstry:from howto import square_ff
except ImportError:import osimport sysdirname, filename = os.path.split(os.path.abspath(__file__))sys.path.append(os.path.join(dirname, "bindings"))from howto import square_ffclass qa_square_ff(gr_unittest.TestCase):def setUp(self):self.tb = gr.top_block()def tearDown(self):self.tb = Nonedef test_instance(self):# FIXME: Test will fail until you pass sensible arguments to the constructorinstance = 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 dataif __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 happensvoid 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 howtomake: howto.square_ff()
inputs:
- label: indomain: streamdtype: floatmultiplicity: 1 
outputs:
- label: outdomain: streamdtype: floatmultiplicity: 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 osimport sysdirname, filename = os.path.split(os.path.abspath(__file__))sys.path.append(os.path.join(dirname, "bindings"))from howto import square2_ffclass qa_square2_ff(gr_unittest.TestCase):def setUp(self):self.tb = gr.top_block()def tearDown(self):self.tb = Nonedef test_instance(self):# FIXME: Test will fail until you pass sensible arguments to the constructorinstance = 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 dataif __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 howtomake: howto.square2_ff()
inputs:
- label: indomain: streamdtype: floatmultiplicity: 1 
outputs:
- label: outdomain: streamdtype: floatmultiplicity: 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模块时,按上述方法略为复杂,还需继续根据官方步骤完善。

这篇关于Gnuradio(3.10)创建OOT自定义模块--USRP X410软件无线电平台开发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/627749

相关文章

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

python如何创建等差数列

《python如何创建等差数列》:本文主要介绍python如何创建等差数列的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python创建等差数列例题运行代码回车输出结果总结python创建等差数列import numpy as np x=int(in

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

怎么用idea创建一个SpringBoot项目

《怎么用idea创建一个SpringBoot项目》本文介绍了在IDEA中创建SpringBoot项目的步骤,包括环境准备(JDK1.8+、Maven3.2.5+)、使用SpringInitializr... 目录如何在idea中创建一个SpringBoot项目环境准备1.1打开IDEA,点击New新建一个项

如何使用Maven创建web目录结构

《如何使用Maven创建web目录结构》:本文主要介绍如何使用Maven创建web目录结构的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录创建web工程第一步第二步第三步第四步第五步第六步第七步总结创建web工程第一步js通过Maven骨架创pytho

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

MySQL 用户创建与授权最佳实践

《MySQL用户创建与授权最佳实践》在MySQL中,用户管理和权限控制是数据库安全的重要组成部分,下面详细介绍如何在MySQL中创建用户并授予适当的权限,感兴趣的朋友跟随小编一起看看吧... 目录mysql 用户创建与授权详解一、MySQL用户管理基础1. 用户账户组成2. 查看现有用户二、创建用户1. 基