Embedded Python Block: Difference between revisions

From GNU Radio
Jump to navigation Jump to search
No edit summary
No edit summary
Line 2: Line 2:
Allows you to create a new (custom) block in Python without needing to make and install an Out of Tree Module (OOT).  When you add the block to your flowgraph,  the pre-populated code simply takes the input stream and multiplies it by a constant.  Note that the structure of this Python block matches the structure of an Out of Tree Module (OOT) Python block. It's essentially a Python OOT block built into a grc flowgraph.  
Allows you to create a new (custom) block in Python without needing to make and install an Out of Tree Module (OOT).  When you add the block to your flowgraph,  the pre-populated code simply takes the input stream and multiplies it by a constant.  Note that the structure of this Python block matches the structure of an Out of Tree Module (OOT) Python block. It's essentially a Python OOT block built into a grc flowgraph.  


=== Initial code in the Python block ===
= Initial code in the Python block =
Here is the pre-populated code:
Here is the pre-populated code:
<pre>
<pre>
Line 40: Line 40:




=== User notes ===
= User notes =


* To revise the Python code:
* To revise the Python code:

Revision as of 19:04, 17 October 2019

Allows you to create a new (custom) block in Python without needing to make and install an Out of Tree Module (OOT). When you add the block to your flowgraph, the pre-populated code simply takes the input stream and multiplies it by a constant. Note that the structure of this Python block matches the structure of an Out of Tree Module (OOT) Python block. It's essentially a Python OOT block built into a grc flowgraph.

Initial code in the Python block

Here is the pre-populated code:

"""
Embedded Python Blocks:

Each time this file is saved, GRC will instantiate the first class it finds
to get ports and parameters of your block. The arguments to __init__  will
be the parameters. All of them are required to have default values!
"""

import numpy as np
from gnuradio import gr


class blk(gr.sync_block):  # other base classes are basic_block, decim_block, interp_block
    """Embedded Python Block example - a simple multiply const"""

    def __init__(self, example_param=1.0):  # only default arguments here
        """arguments to this function show up as parameters in GRC"""
        gr.sync_block.__init__(
            self,
            name='Embedded Python Block',   # will show up in GRC
            in_sig=[np.complex64],
            out_sig=[np.complex64]
        )
        # if an attribute with the same name as a parameter is found,
        # a callback is registered (properties work, too).
        self.example_param = example_param

    def work(self, input_items, output_items):
        """example: multiply with constant"""
        output_items[0][:] = input_items[0] * self.example_param
        return len(output_items[0])


User notes

  • To revise the Python code:
    • right click on the Embedded Python block
    • select Properties
    • select Open in Editor
    • select Use Default or Choose Editor
    • For some platforms the Use Default button may do nothing, in which case you have to choose the text editor manually.
    • if you select Choose Editor, the /usr/bin folder will open for you to pick one.

Important: It is highly recommended that any flowgraph using an Embedded Python Block be placed in a separate directory (folder); particularly so if more than one Embedded Python Block is used. This is because the generation process creates a file epy_block_0.py from that flowgraph. If you generate a different flowgraph in the same directory, its Embedded Python Block will overwrite the file.

Warning: To make changes to the Python code, you must follow the steps above. If you edit the epy_block_0.py file directly and then generate the flowgraph again, your changes will be wiped out! (The most recent edited code is actually stored in the .grc file.)


Parameters

The parameters for this block are simply those of your embedded Python block itself.

Note that every parameter needs a default value. For example, in the code above, note line:

def __init__(self, example_param=1.0):  # only default arguments here

If you don't have any parameters, change that line of code to

def __init__(self):

If you have a vector length equal to a variable of your block, note that the default value must be "hardcoded".

Example Flowgraph

Insert description of flowgraph here, then show a screenshot of the flowgraph and the output if there is an interesting GUI. Currently we have no standard method of uploading the actual flowgraph to the wiki or git repo, unfortunately. The plan is to have an example flowgraph showing how the block might be used, for every block, and the flowgraphs will live in the git repo.