Creating C++ OOT with gr-modtool
Beginner Tutorials
Introducing GNU Radio Flowgraph Fundamentals
Creating and Modifying Python Blocks DSP Blocks
SDR Hardware |
TODO: intro material
The previous tutorial, Creating Python OOT with gr-modtool, describes how to create a Python block in an OOT module. This c++ OOT tutorial builds upon the previous Python one, so it is is suggested to at least complete the Creating an OOT Module portion of that tutorial before completing this one.
TODO: pick up from python OOT tutorial
TODO: link back to python tutorial
Installation Note
This tutorial was written using GNU Radio v3.10.1.1 on Ubuntu 21.10, installed using the Ubuntu PPA from the Installation Wiki Page. The basic GNU Radio install using:
$ sudo apt-get install gnuradio
does not come with the proper libraries needed to compile and install OOT modules. Consider installing the following packages before continuing:
$ sudo apt-get install gnuradio-dev cmake libspdlog-dev clang-format
Adding a New Block
Move to the gr-customModule directory created in the Creating Python OOT with gr-modtool tutorial:
cd your-path/gr-customModule
Add a new block named multDivSelector:
$ gr_modtool add multDivSelector
The types of blocks will be displayed:
GNU Radio module name identified: customModule ('sink', 'source', 'sync', 'decimator', 'interpolator', 'general', 'tagged_stream', 'hier', 'noblock')
Enter sync as the block type:
Enter block type: sync
Enter cpp as the language:
Language (python/cpp): cpp Language: C++ Block/code identifier: multDivSelector
Enter the name or organization of the copyright holder:
Please specify the copyright holder: YourName
Create a boolean variable selector with a default value of true:
Enter valid argument list, including default arguments: bool selector=true
Select whether or not QA code is desired:
Add Python QA code? [Y/n] n Add C++ QA code? [Y/n] n
Multiple files will then be created or modified:
Adding file 'lib/multDivSelector_impl.h'... Adding file 'lib/multDivSelector_impl.cc'... Adding file 'include/gnuradio/customModule/multDivSelector.h'... Adding file 'python/customModule/bindings/docstrings/multDivSelector_pydoc_template.h'... Adding file 'python/customModule/bindings/multDivSelector_python.cc'... Adding file 'grc/customModule_multDivSelector.block.yml'... Editing grc/CMakeLists.txt...
Editing the c++ Header
Many of the files are automatically generated wrapper code that do not need to be modified. However, the multDivSelector_impl.h and multDivSelector_impl.cc files defines the operation of the block and must be modified. Open the file with a text editor:
$ gedit lib/multDivSelector_impl.h &
The following code will be displayed:
/* -*- c++ -*- */
/*
* Copyright 2022 YourName.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#ifndef INCLUDED_CUSTOMMODULE_MULTDIVSELECTOR_IMPL_H
#define INCLUDED_CUSTOMMODULE_MULTDIVSELECTOR_IMPL_H
#include <gnuradio/customModule/multDivSelector.h>
namespace gr {
namespace customModule {
class multDivSelector_impl : public multDivSelector
{
private:
// Nothing to declare in this block.
public:
multDivSelector_impl(bool selector);
~multDivSelector_impl();
// 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 customModule
} // namespace gr
#endif /* INCLUDED_CUSTOMMODULE_MULTDIVSELECTOR_IMPL_H */
Create a boolean private member _selector which will hold the value of the selector parameter:
class multDivSelector_impl : public multDivSelector
{
private:
bool _selector;
Press CTRL + S to save the file.
Editing the c++ impl
The .cc file needs to be modified to define the desired operation of the block. Open the file with a text editor:
$ gedit multDivSelector_impl.cc &
The code will be as displayed:
/* -*- c++ -*- */
/*
* Copyright 2022 YourName.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "multDivSelector_impl.h"
#include <gnuradio/io_signature.h>
namespace gr {
namespace customModule {
#pragma message("set the following appropriately and remove this warning")
using input_type = float;
#pragma message("set the following appropriately and remove this warning")
using output_type = float;
multDivSelector::sptr multDivSelector::make(bool selector)
{
return gnuradio::make_block_sptr<multDivSelector_impl>(selector);
}
/*
* The private constructor
*/
multDivSelector_impl::multDivSelector_impl(bool selector)
: gr::sync_block("multDivSelector",
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.
*/
multDivSelector_impl::~multDivSelector_impl() {}
int multDivSelector_impl::work(int noutput_items,
gr_vector_const_void_star& input_items,
gr_vector_void_star& output_items)
{
auto in = static_cast<const input_type*>(input_items[0]);
auto out = static_cast<output_type*>(output_items[0]);
#pragma message("Implement the signal processing in your block and remove this warning")
// Do <+signal processing+>
// Tell runtime system how many output items we produced.
return noutput_items;
}
} /* namespace customModule */
} /* namespace gr */
Remove the pragma messages and define the input and output type to be gr_complex:
using input_type = gr_complex;
using output_type = gr_complex;
Update to two inputs and store the value of the selector parameter using the private member _selector as defined in multDivSelector_impl.h:
/*
* The private constructor
*/
multDivSelector_impl::multDivSelector_impl(bool selector)
: gr::sync_block("multDivSelector",
gr::io_signature::make(
2 /* min inputs */, 1 /* max inputs */, sizeof(input_type)),
gr::io_signature::make(
1 /* min outputs */, 1 /*max outputs */, sizeof(output_type)))
{
_selector = selector;
}
Modify the work() function by removing the pragma message, defining the variables in0 and in1 corresponding to the two input ports, and multiply the two inputs if _selector is true and divide them if _selector is false:
int multDivSelector_impl::work(int noutput_items,
gr_vector_const_void_star& input_items,
gr_vector_void_star& output_items)
{
auto in0 = static_cast<const input_type*>(input_items[0]);
auto in1 = static_cast<const input_type*>(input_items[1]);
auto out = static_cast<output_type*>(output_items[0]);
for (int index = 0; index < noutput_items; index++) {
if (_selector) { out[index] = in0[index] * in1[index]; }
else{ out[index] = in0[index] * in1[index]; }
}
// Tell runtime system how many output items we produced.
return noutput_items;
}
Compile and Install
rm -rf build mkdir build cd build cmake ..