Hier Blocks and Parameters: Difference between revisions

From GNU Radio
Jump to navigation Jump to search
No edit summary
 
(24 intermediate revisions by 4 users not shown)
Line 4: Line 4:
This tutorial describes how to create a hierarchical block, or ''Hier block'', in GRC.
This tutorial describes how to create a hierarchical block, or ''Hier block'', in GRC.


The previous tutorial, [[Streams_and_Vectors|Streams and Vectors]], describes the differences between ''Streams'' and ''Vectors''. The next tutorial, [[Creating_Your_First_Block|Creating Your First Block]], describes how to use the ''Embedded Python Block'' to create your first signal processing block in GNU Radio.
The previous tutorial, [[Streams_and_Vectors|Streams and Vectors]], describes the differences between ''Streams'' and ''Vectors''. The next tutorial, [[Creating_Your_First_Block|Creating Your First Block]], describes how to use the ''Embedded Python Block'' to create a signal processing block in GNU Radio.




== Creating the Initial Flowgraph ==
== Creating the Initial Flowgraph ==


A ''hier block'' is used to encapsulate and simplify multiple GNU Radio blocks into a single block. The example ''hier block'' will be a frequency shifter block which multiplies a ''Signal Source'' against an input signal.
A ''hier block'' is used as a wrapper to simplify multiple GNU Radio blocks into a single block. The example ''hier block'' will be a frequency shifter block which multiplies a ''Signal Source'' against an input signal.
 
[[File:FrequencyShifterBlock.png|500px]]
 


The first step is creating the flowgraph. Drag and drop the following blocks into the workspace:
The first step is creating the flowgraph. Drag and drop the following blocks into the workspace:
Line 36: Line 39:
== Create The Hier Block ==
== Create The Hier Block ==


Click and drag in the workspace window to select the ''Signal Source'' and ''Multiply'' blocks, as well as the connection between them:
Click and drag in the workspace window to select the ''Signal Source'' and ''Multiply'' blocks including the connection between them:


[[File:ClickAndDragSelect.png|700px]]
[[File:ClickAndDragSelect.png|700px]]




Right-click on the highlighted blocks and select ''More > Create Hier'':
Right-click on one of the highlighted blocks and select ''More > Create Hier'':


[[File:ClickCreateHier.png|700px]]
[[File:ClickCreateHier.png|700px]]




A flowgraph will be created in a new GRC tab:
A flowgraph is created in a new GRC tab:


[[File:NewHierBlock.png|500px]]
[[File:NewHierBlock.png|500px]]




Double-click the ''Options'' block and edit the properties. Update the properties:
Double-click the ''Options'' block and edit the properties:
* Id: ''FrequencyShifter''
* Id: ''FrequencyShifter''
* Title: ''Frequency Shifter Block''
* Title: ''Frequency Shifter Block''
Line 68: Line 71:
== Variables vs Parameters ==
== Variables vs Parameters ==


A ''variable'' is different than a ''parameter'' in GNU Radio. A ''parameter'' creates an interface for the ''hier block'' to accept a value from an external source, where as a ''variable'' can only be accessed from within a ''hier block''.
A ''variable'' is different than a ''parameter'' in GNU Radio. A ''parameter'' creates an interface for the ''hier block'' to accept a value from an external source, whereas a ''variable'' only exists internally to the ''hier block'':
 
[[File:HierBlockParameterVariable.png|500px]]
 


For example, the ''samp_rate'' variable can only be accessed from within the ''hier block'':
For example, the ''samp_rate'' variable can only be accessed from within the ''hier block'':
Line 75: Line 81:




The ''samp_rate'' needs to be a parameter that way it can be passed in from another block in the larger flowgraph. Delete the ''samp_rate'' variable and add a ''Parameter'' block into the GRC workspace:
The ''samp_rate'' needs to be converted to a parameter so it can be updated from another block in the larger flowgraph. Delete the ''samp_rate'' variable and add a ''Parameter'' block into the GRC workspace:


[[File:AddParameterToHierBlock.png|500px]]
[[File:AddParameterToHierBlock.png|500px]]
Line 95: Line 101:
[[File:EditFrequencyParameterProperties.png|500px]]
[[File:EditFrequencyParameterProperties.png|500px]]


Add the ''frequency'' parameter to the Signal Source ''Frequency'' property:


Add the ''frequency'' parameter to the Signal Source ''Frequency'' property. The flowgraph should now look like:
[[File:AddFrequencyToSignalSourceProperties.png|500px]]
 
The flowgraph should look like:


[[File:HierBlockSampRateFrequencyParameters.png|500px]]
[[File:HierBlockSampRateFrequencyParameters.png|500px]]


== Input and Output Ports ==
== Input and Output Ports ==
Line 115: Line 123:




A Python ''.py'' file and YAML ''.yml'' file will be created in your home directory:
A Python ''.py'' file and YAML ''.yml'' file will be created. For GNU Radio v3.8 the files will be created in your home directory:


<pre>/home/$USER/.grc_gnuradio/</pre>
<pre>/home/$USER/.grc_gnuradio/</pre>
Line 121: Line 129:
[[File:HierBlockPyYml.png|500px]]
[[File:HierBlockPyYml.png|500px]]


For GNU Radio v3.10, the files will be created in the directory where the ''.grc'' file is saved. Please create the ''.grc_gnuradio'' directory and copy the ''.py'' and ''.yml'' files there:


GRC needs to update it's internal memory of the blocks it has access to before the ''Frequency Shifter'' block can be used in a flowgraph. Click the ''Reload Blocks'' button:
<pre>$ mkdir /home/$USER/.grc_gnuradio
$ cp FrequencyShifter.block.yml /home/$USER/.grc_gnuradio/
$ cp FrequencyShifter.py /home/$USER/.grc_gnuradio/ </pre>
 
GRC needs to update the internal list of the blocks before the ''Frequency Shifter'' block can be used in a flowgraph. Click the ''Reload Blocks'' button:


[[File:ClickReloadBlocks.png|250px]]
[[File:ClickReloadBlocks.png|250px]]




You'll notice that there is a new category ''GRC Hier Blocks'' in the block library below ''Core'', and the ''Frequency Shifter Block'' can be used in flowgraphs:
There is a new category ''GRC Hier Blocks'' in the block library below ''Core'', and the ''Frequency Shifter Block'' can be used in flowgraphs:


[[File:CoreCategoryOnly.png|300px]]
[[File:CoreCategoryOnly.png|300px]]
Line 161: Line 174:
== Deleting a Hier Block ==
== Deleting a Hier Block ==


A hier block can be cleared from GRC memory by removing the files from <code>/home/$USER/.grc_gnuradio</code>. In a terminal, move to the <code>.grc_gnuradio</code> directory:
A hier block can be cleared from GRC memory by removing the files from <code>/home/$USER/.grc_gnuradio</code>.
 
In a terminal, move to the <code>.grc_gnuradio</code> directory:


<pre>cd /home/$USER/.grc_gnuradio</pre>
<pre>cd /home/$USER/.grc_gnuradio</pre>


Then remove the files. Warning! The <code>rm</code> cannot be undone!
Then remove the files.
 
Warning! The <code>rm</code> command cannot be undone!
 
For GNU Radio v3.8 the delete command is:


<pre>rm FrequencyShifter.py FrequencyShifter.py.block.yml</pre>
<pre>rm FrequencyShifter.py FrequencyShifter.py.block.yml</pre>
Line 171: Line 190:
[[File:DeleteHierBlockCommand.png|700px]]
[[File:DeleteHierBlockCommand.png|700px]]


For GNU Radio v3.10 the delete command is:
<pre>rm FrequencyShifter.py FrequencyShifter.block.yml</pre>


Click the ''Reload Blocks'' button to update GRC's memory of blocks, clearing it of the ''Frequency Shifter Block'':
Click the ''Reload Blocks'' button to update GRC's memory of blocks, clearing it of the ''Frequency Shifter'' block:


[[File:ClickReloadBlocks.png|350px]]
[[File:ClickReloadBlocks.png|350px]]




You'll notice that the ''GRC Hier Blocks'' category is gone and only the ''Core'' blocks remain:
The ''GRC Hier Blocks'' category is deleted and only the ''Core'' blocks remain:


[[File:CoreCategoryOnly.png|350px]]
[[File:CoreCategoryOnly.png|350px]]




The next tutorial, [[Creating_Your_First_Block|Creating Your First Block]], describes how to use the ''Embedded Python Block'' to create your first signal processing block in GNU Radio.
The next tutorial, [[Creating_Your_First_Block|Creating Your First Block]], describes how to use the ''Embedded Python Block'' to create a signal processing block in GNU Radio.

Latest revision as of 16:08, 18 May 2023

Beginner Tutorials

Introducing GNU Radio

  1. What is GNU Radio?
  2. Installing GNU Radio
  3. Your First Flowgraph

Flowgraph Fundamentals

  1. Python Variables in GRC
  2. Variables in Flowgraphs
  3. Runtime Updating Variables
  4. Signal Data Types
  5. Converting Data Types
  6. Packing Bits
  7. Streams and Vectors
  8. Hier Blocks and Parameters

Creating and Modifying Python Blocks

  1. Creating Your First Block
  2. Python Block With Vectors
  3. Python Block Message Passing
  4. Python Block Tags

DSP Blocks

  1. Low Pass Filter Example
  2. Designing Filter Taps
  3. Sample Rate Change

This tutorial describes how to create a hierarchical block, or Hier block, in GRC.

The previous tutorial, Streams and Vectors, describes the differences between Streams and Vectors. The next tutorial, Creating Your First Block, describes how to use the Embedded Python Block to create a signal processing block in GNU Radio.


Creating the Initial Flowgraph

A hier block is used as a wrapper to simplify multiple GNU Radio blocks into a single block. The example hier block will be a frequency shifter block which multiplies a Signal Source against an input signal.

FrequencyShifterBlock.png


The first step is creating the flowgraph. Drag and drop the following blocks into the workspace:

  1. Signal Source
  2. Multiply
  3. Noise Source
  4. Low Pass Filter
  5. Throttle
  6. QT GUI Frequency Sink
  7. QT GUI Range

Connect the blocks:

StartingFlowgraphHierBlock.png

Update the QT GUI Range properties:

  • Id: frequency
  • Default Value: 0
  • Start: -samp_rate/2
  • Stop: samp_rate/2

Update the Low Pass Filter properties:

  • Cutoff Freq (Hz): samp_rate/4
  • Transition Width (Hz): samp_rate/8

Create The Hier Block

Click and drag in the workspace window to select the Signal Source and Multiply blocks including the connection between them:

ClickAndDragSelect.png


Right-click on one of the highlighted blocks and select More > Create Hier:

ClickCreateHier.png


A flowgraph is created in a new GRC tab:

NewHierBlock.png


Double-click the Options block and edit the properties:

  • Id: FrequencyShifter
  • Title: Frequency Shifter Block
  • Generate Options: Hier Block

OptionsSelectHierBlock.png

The remaining properties will then change, showing the Category:

ShowGRCHierBlocksCategory.png

The Category is where the block can be found in the block library on the right hand of GRC. The hier block will be located under GRC Hier Blocks, instead of Core where the rest of the GNU Radio blocks are located.

Save the flowgraph.

Variables vs Parameters

A variable is different than a parameter in GNU Radio. A parameter creates an interface for the hier block to accept a value from an external source, whereas a variable only exists internally to the hier block:

HierBlockParameterVariable.png


For example, the samp_rate variable can only be accessed from within the hier block:

HierBlockWithVariable.png


The samp_rate needs to be converted to a parameter so it can be updated from another block in the larger flowgraph. Delete the samp_rate variable and add a Parameter block into the GRC workspace:

AddParameterToHierBlock.png


Edit the Parameter properties:

  • Id: samp_rate
  • Label: Sample Rate
  • Type: Float

EditParameterProperties.png


Add a second Parameter:

  • Id: frequency
  • Label: Frequency
  • Type: float

EditFrequencyParameterProperties.png

Add the frequency parameter to the Signal Source Frequency property:

AddFrequencyToSignalSourceProperties.png

The flowgraph should look like:

HierBlockSampRateFrequencyParameters.png

Input and Output Ports

A pad is used to specify input and output ports on a hier block. Add a Pad Source and Pad Sink to the flowgraph to act as the in and out ports:

AddPadSourcePadSink.png


Generate the Hier Block Code

Click Generate the flow graph to create the hier block source code:

ClickGenerateFlowgraph.png


A Python .py file and YAML .yml file will be created. For GNU Radio v3.8 the files will be created in your home directory:

/home/$USER/.grc_gnuradio/

HierBlockPyYml.png

For GNU Radio v3.10, the files will be created in the directory where the .grc file is saved. Please create the .grc_gnuradio directory and copy the .py and .yml files there:

$ mkdir /home/$USER/.grc_gnuradio
$ cp FrequencyShifter.block.yml /home/$USER/.grc_gnuradio/
$ cp FrequencyShifter.py /home/$USER/.grc_gnuradio/ 

GRC needs to update the internal list of the blocks before the Frequency Shifter block can be used in a flowgraph. Click the Reload Blocks button:

ClickReloadBlocks.png


There is a new category GRC Hier Blocks in the block library below Core, and the Frequency Shifter Block can be used in flowgraphs:

CoreCategoryOnly.png

GRCHierBlocksUpdated.png

Using the Hier Block

The hier block can now be used in a flowgraph. Return the starting flowgraph and delete the Signal Source and Multiply blocks:

DeleteSignalSourceMultiplyBlock.png


Add the Frequency Shifter Block to the workspace and connect it to the rest of the flowgraph:

ConnectFrequencyShifterBlock.png


Edit the Frequency Shifter Block properties by adding the samp_rate and frequency variables:

EditFrequencyShifterProperties.png


Running the flowgraph will bring up the QT GUI Frequency Sink window with the QT QUI Range slider:

HierBlockFreqSink.png


Dragging the frequency slider will pass the value through the Frequency Shifter Block parameter causing the signal's center frequency to be modified:

HierBlockFrequencyShift.png

Deleting a Hier Block

A hier block can be cleared from GRC memory by removing the files from /home/$USER/.grc_gnuradio.

In a terminal, move to the .grc_gnuradio directory:

cd /home/$USER/.grc_gnuradio

Then remove the files.

Warning! The rm command cannot be undone!

For GNU Radio v3.8 the delete command is:

rm FrequencyShifter.py FrequencyShifter.py.block.yml

DeleteHierBlockCommand.png

For GNU Radio v3.10 the delete command is:

rm FrequencyShifter.py FrequencyShifter.block.yml

Click the Reload Blocks button to update GRC's memory of blocks, clearing it of the Frequency Shifter block:

ClickReloadBlocks.png


The GRC Hier Blocks category is deleted and only the Core blocks remain:

CoreCategoryOnly.png


The next tutorial, Creating Your First Block, describes how to use the Embedded Python Block to create a signal processing block in GNU Radio.