In Part 1 of this series, we created a new module called my_module for use in a simple cognitive radio application that required a basic state machine.  In Part 2 of this blog series, we modified the following files:

• Auto-generated header file – Declared a private class variable, which is the state of our state machine
• Auto-generated source file – Implemented the actual state machine

In this blog post, Part 3, we wrap things up by compiling our project using CMake.

The first step is to make sure that the current directory is ‘gr-my_module’. Next, we create the build directory using the following command:

coding

Switch to the new directory:

part3-img2

Now, it’s time to start the CMake compilation process:

This command tells CMake to use the compilation file called CMakeList.txt, which is located one directory above in the hierarchy tree in gr-my_module/.

The last step is to launch the actual CMake compilation process:

The compilation process should not show any errors. If any errors appear, repeat the above steps but as root user instead. Also, if the GNU Radio runtime cannot be found, make sure that the GNU Radio environment variables are correctly instantiated (see Part 1 of this series).

Now that the compilation is done, you can install your module. However, it will not be available yet in the GNU Radio Companion (GRC) software. This is because the gr_modtool command doesn’t create the necessary XML files, it simply adds some skeleton code. To create the XML code for the block, run the following command from the gr-my_module folder:

Note that gr_modtool add creates an invalid GRC file, so this is why we are overwriting it. In most cases, gr_modtool can’t determine all parameters by itself and you will have to edit the appropriate XML file by hand. The GRC wiki page has a description of the parameters. In this case however, because the block is simple, the XML is actually valid.

The last step to make our block available in GRC is to run the following command in the build directory:

That’s it! You should now see in your GRC block library a block called ‘cognitive’, with float input and output ports.

To test and debug a block, you can use the good old printf() function. For example, let’s say want to output at runtime the chosen frequency to transmit/receive. First, add the following line of code after the state verification section in the cognitive_impl.cc file:

printf(“%f, n”, out
[i]);

Save the file and repeat the compilation steps, starting from the CMake compilation process. You don’t need to redo the XML steps. However, you will need to run the make install command again. In your GNU Radio Companion console, you should see the output frequency of the cognitive block.

The implementation of a custom GRC block using C++ code concludes Part 3 of this blog series. The process for creating a custom GRC block using Python is very similar. However, be aware that a Python application typically has lower performance than a C++ version. So, if you are a master of the C++ programming language, I recommend using C++ for your application in the final stage. However, Python can be a very fast prototyping tool for complex digital signal processing applications and is very easy to learn. For this very reason, I will devote some blog time to it in the future. Stay tuned!