GNU Radio: 自定义 block 实例
综述
本文通过在GNU Radio 中编写一个block的例子,系统介绍创建一个block的过程。该 block 的功能是可以在GRC中通过滑块(WX GUI Slider)来实时改变信号源(Signal Source)的相位偏移。
步骤详解
1. 使用 gr_modtool 工具创建 block 的框架,GNU Radio 会自动帮我们创建所需要的文件及文件夹。
$ gr_modtool newmod myblk
Creating out-of-tree module in ./gr-myblk... Done.
Use 'gr_modtool add' to add a new block to this currently empty module.
$ cd gr-myblk
/gr-myblk$ ls
apps cmake CMakeLists.txt docs examples grc include lib python swig
2. 使用Git版本控制(可选)
在gr-myblock文件夹下执行以下命令,创建一个Git仓库,可用于版本控制。git常用命令速查点这里。
/gr-myblk$ git init
3. 添加block
执行以下命令添加block。参数类型为double,因为我们要传递的角度是0~360度double类型,不填默认没有参数,但后期可以自己添加。根据是否需要Python或者C++测试代码选择Y或者n,如果不填默认为Y。
/gr-myblk$ gr_modtool add -t sync phase_offset
GNU Radio module name identified: myblk
Language: C++
Block/code identifier: phase_offset
Enter valid argument list, including default arguments: double angle
Add Python QA code? [Y/n] n
Add C++ QA code? [Y/n] n
Adding file 'lib/phase_offset_impl.h'...
Adding file 'lib/phase_offset_impl.cc'...
Adding file 'include/myblk/phase_offset.h'...
Editing swig/myblk_swig.i...
Adding file 'grc/myblk_phase_offset.xml'...
Editing grc/CMakeLists.txt...
4. 编辑代码
从上一条命令的输出可以看出,gr_modtood已经为我们自动创建了4个文件,分别是:
- lib/phase_offset_impl.h
- lib/phase_offset_impl.cc
- include/myblk/phase_offset.h
- grc/myblk_phase_offset.xml
分别修改这4个文件如下:
/* -*- c++ -*- */
/*
* Copyright 2016 <+YOU OR YOUR COMPANY+>.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/ #ifdef HAVE_CONFIG_H
#include "config.h"
#endif #include <gnuradio/io_signature.h>
#include "phase_offset_impl.h" namespace gr {
namespace myblk { phase_offset::sptr
phase_offset::make(double angle)
{
return gnuradio::get_initial_sptr
(new phase_offset_impl(angle));
} /*
* The private constructor
*/
phase_offset_impl::phase_offset_impl(double angle)
: gr::sync_block("phase_offset",
gr::io_signature::make(, , sizeof(gr_complex)),
gr::io_signature::make(, , sizeof(gr_complex)))
, d_offset_angle(angle)
{} /*
* Our virtual destructor.
*/
phase_offset_impl::~phase_offset_impl()
{
} int
phase_offset_impl::work(int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
const gr_complex *in = (const gr_complex *) input_items[];
gr_complex *out = (gr_complex *) output_items[]; double rad = d_offset_angle/*PI;//将角度变成弧度
// Do <+signal processing+>
for (int i = ; i < noutput_items; ++i)
{
out[i] = in[i] * gr_complex(cos(rad), sin(rad));
} std::cout << "d_offset_angle = " << d_offset_angle << std::endl; // Tell runtime system how many output items we produced.
return noutput_items;
} } /* namespace myblk */
} /* namespace gr */
phase_offset_impl.cc
/* -*- c++ -*- */
/*
* Copyright 2016 <+YOU OR YOUR COMPANY+>.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/ #ifndef INCLUDED_MYBLK_PHASE_OFFSET_IMPL_H
#define INCLUDED_MYBLK_PHASE_OFFSET_IMPL_H #include <myblk/phase_offset.h> namespace gr {
namespace myblk { class phase_offset_impl : public phase_offset
{
private:
double d_offset_angle; public:
phase_offset_impl(double angle);
~phase_offset_impl(); void set_angle_offset(double angle)
{
d_offset_angle = angle;
} // Where all the action really happens
int work(int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items);
}; } // namespace myblk
} // namespace gr #endif /* INCLUDED_MYBLK_PHASE_OFFSET_IMPL_H */
phase_offset_impl.h
/* -*- c++ -*- */
/*
* Copyright 2016 <+YOU OR YOUR COMPANY+>.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/ #ifndef INCLUDED_MYBLK_PHASE_OFFSET_H
#define INCLUDED_MYBLK_PHASE_OFFSET_H #include <myblk/api.h>
#include <gnuradio/sync_block.h> #define PI 3.14159265358979323846 namespace gr {
namespace myblk { /*!
* \brief <+description of block+>
* \ingroup myblk
*
*/
class MYBLK_API phase_offset : virtual public gr::sync_block
{
public:
typedef boost::shared_ptr<phase_offset> sptr; /*!
* \brief Return a shared_ptr to a new instance of myblk::phase_offset.
*
* To avoid accidental use of raw pointers, myblk::phase_offset's
* constructor is in a private implementation
* class. myblk::phase_offset::make is the public interface for
* creating new instances.
*/
static sptr make(double angle);
virtual void set_angle_offset(double angle) = ;
}; } // namespace myblk
} // namespace gr #endif /* INCLUDED_MYBLK_PHASE_OFFSET_H */
phase_offset.h
<block>
<name>Phase offset</name>
<key>myblk_phase_offset</key>
<category>MYBLK</category>
<import>import myblk</import>
<make>myblk.phase_offset($angle)</make>
<callback>set_angle_offset($angle)</callback>
<param>
<name>Angle</name>
<key>angle</key>
<value>0</value>
<type>real</type>
</param>
<sink>
<name>in</name>
<type>complex</type>
</sink>
<source>
<name>out</name>
<type>complex</type>
</source>
</block>
myblk_phase_offset.xml
5. 编译
依次执行下面的命令完成编译工作
/gr-myblk$ mkdir build
/gr-myblk$ cd build
/gr-myblk/build$ cmake ../
/gr-myblk/build$ make
/gr-myblk/build$ sudo make install
/gr-myblk/build$ sudo ldconfig
注意:如果出现类似错误提示 AttributeError: 'module' object has no attribute 'xxxx', 运行 sudo ldconfig 即可解决.
6. 在GRC中测试验证
流图与运行结果如下:



注意事项
一般我们在block设置的参数,只有在C++类的构造函数中初始化一次赋值。也就是说我们设置的参数只能在程序运行的一开始将参数传递到C++中。但是如果要想像上面的 block 那样在程序运行过程中实时改变参数的值,就需要在xml文件中调用 <callback>...</callback> 关键字,调用C++的成员函数,修改成员变量的值。如本程序中xml中的语句: <callback>set_angle_offset($angle)</callback>
对应C++中函数如下:
void set_angle_offset(double angle)
{
d_offset_angle = angle;
}
除此之外,还要注意参数类型匹配。WX GUI Slider传递的参数类型为real,因此angle的数据类型也应该为real,如果设置为int则会报错,grc不会像C++一样支持数据类型隐式转换,因此要注意类型匹配。
Slider中输入的是角度,C++运算时需要将角度转化成弧度来计算。
GNU Radio: 自定义 block 实例的更多相关文章
- GNU Radio: Synchronization and MIMO Capability with USRP Devices
Application Note Synchronization and MIMO Capability with USRP Devices Ettus Research Introduction S ...
- 【译】GNU Radio How to write a block 【如何开发用户模块及编写功能块】
本文讲解如何在GNU Radio中添加用户开发的信号处理模块,译文如有不当之处可参考原文地址:http://gnuradio.microembedded.com/outoftreemodules Ou ...
- GNU Radio: USRP2 and N2x0 Series
Comparative features list 相对性能清单 Hardware Capabilities: 1 transceiver card slot External PPS referen ...
- GNU Radio 入门培训
1. GNU Radio介绍 1.1 什么是GNU Radio GNU Radio是一个完全开源的软件无线电结构平台,它可以用来设计和仿真,也可以用来连接真实的无线电系统.GNU Radio是一个高度 ...
- GNU Radio: Overview of the GNU Radio Scheduler
Scetion 1: The Flowgraph The flowgraph moves data from sources into sinks. 一个流图由多个模块组成,其中一般包括信源(Sour ...
- GNU Radio: Multiple USRP configurations 配置多个USRP设备
Introduction 引言 Some USRP devices are capable of being grouped to form a single, virtual device. A s ...
- GNU Radio安装教程: Ubuntu14.04 + uhd3.10.0 + gnuradio3.7.10.1
1. 更新和安装依赖项 在编译安装uhd和gnuradio之前,确保已安装所需依赖项.Ubuntu系统运行: sudo apt-get update 安装UHD和GNURadio所需依赖项: On U ...
- GNU Radio Radar Toolbox
GNU Radio Radar Toolbox Install guide Change to any folder in your home directory and enter followin ...
- Objective-C:自定义Block函数
Block函数是一种类似于函数指针的函数,程序员只需要把需要操作的代码封装到定义的block中即可,以后需要使用时,直接调用,非常方便.... 举例如下: 第一种形式:自定义一个无返回值而且无参数的b ...
随机推荐
- 转:Hive SQL的编译过程
Hive是基于Hadoop的一个数据仓库系统,在各大公司都有广泛的应用.美团数据仓库也是基于Hive搭建,每天执行近万次的Hive ETL计算流程,负责每天数百GB的数据存储和分析.Hive的稳定性和 ...
- 练习-99乘法表 token生成器 翻译小工具
一.99乘法表 1.1 技术点 记住: for 循环的使用,以及for的嵌套使用 range()的使用,掌握sep为负数的使用的使用. print() 函数的使用,默认的结尾的换行符 替换 end= ...
- 剑指offer--44.两个链表的第一个公共结点
@selfboot 牛逼的代码,长度相同,一遍出结果, 长度不同,短的点跑完,变成长的,当长的跑完变成短的链表的时候,较长的链表已经走过了多的结点. ------------------------- ...
- linux和windows双系统设置默认启动系统
1.以root身份登录linux系统: 2.启动终端,输入命令:vi /boot/grub/grub.conf,文件内容如下: #boot=/dev/sda default=0 (默认启动哪个系统的设 ...
- 启动Windows防火墙提示“0x8007042c"
win8.1 启动防火墙是报错:启动Windows防火墙提示“0x8007042c" 一.检查服务 1,右击开始->运行->输入“services.msc” 打开服务 在框中找到 ...
- chrome浏览器使用记录
出现错误 net::ERR_BLOCKED_BY_CLIENT 出现这个错误一般是因为chrome安装了adblocker等这样的插件,这些插件会把路径及文件名中包含广告字样的文字禁止掉,比如:adv ...
- 层序遍历二叉树 完整层序重建二叉树 python
给定一个二叉树的完整的层次遍历序列(包含所有节点,包括空节点),利用这个序列生成一颗二叉树. 我们首先来看怎样对一颗二叉树进行层序遍历,下图所示的二叉树层次遍历的结果为[a,b,c,d,e],在这个过 ...
- 解决visual studio2017没有系统类和方法注释的问题
好几次碰到这种情况了,每次都得稍微查一查才能解决这个问题,相信也有不少人遇到这个问题,在对方法还不是很熟练的时候,将鼠标放置到方法上去,就会有信息提示是一件非常方便的事情,本文的解决方法同样适用于只显 ...
- Webdynpro ABAP 简单剖析
众所周知,WEBDYNPRO是今天来SAP主推的一个面向WEB的MVC编程框架,接触过J2EE的朋友都不会对MVC这种设计模式陌生,WEBDYNPRO ABAP的基本设计思路和很多著名的面向互联网的M ...
- (效果四)jst如何判断对象是否为空?
前言:在实现业务逻辑的过程中,很多工程师都会遇到需要判断一个对象,数组是否为空的情景,很多时候我们在请求数据的时候都需要判断请求的对象数据是否为空,如果直接使用,在数据请求为空时,控制台就会报错.因此 ...