Build a new module with python
Build a new module
Build a simple new module, which output = input * 0.7, and types of them are both float.
- In command line, input: gr_modtool newmod tutorial
- cd gr_tutorial/
- gr_modtool add multiply_py_ff
- Enter block type: sync
- Language: python
- holder: just Enter
- Enter valid argument list, including default arguments: multiple
- Add Python QA code? [Y/n] y
Note:
Line 3. _ff means float in, float out
Line 4. sync means input items = output items
Modify the module
1. Modify multiply_py_ff.py
Source 1:
def __init__(self, multiple):
gr.sync_block.__init__(self,
name="multiply_py_ff",
in_sig=[<+numpy.float+>],
out_sig=[<+numpy.float+>])
self.multiple = multiple
After modify:
def __init__(self, multiple):
gr.sync_block.__init__(self,
name="multiply_py_ff",
in_sig=[numpy.float32],
out_sig=[numpy.float32])
self.multiple = multiple
Source 2:
def work(self, input_items, output_items):
in0 = input_items[0]
out = output_items[0]
# <+signal processing here+>
out[:] = in0
return len(output_items[0])
After modify:
def work(self, input_items, output_items):
in0 = input_items[0]
out = output_items[0]
# <+signal processing here+>
out[:] = in0 * self.multiple
return len(output_items[0])
2. Modify qa_multiply_py_ff.py
Source:
def test_001_t (self):
# set up fg
self.tb.run ()
# check data
After modify
def test_001_t (self):
# set up fg
src_data = (0, 1, -1, 3.5, -2.5)
expected_result = (0, 2, -2, 7, -5)
src = blocks.vector_source_f(src_data)
mult = multiply_py_ff(2)
snk = blocks.vector_sink_f()
self.tb.connect(src, mult, snk)
self.tb.run ()
result_data = snk.data()
# check data
self.assertFloatTuplesAlmostEqual(expected_result, result_data, 6)
Note:
- In a new line, we should use space bar to avoid “unexpected indent”
- src = blocks.vector_source_f(src_data)
There are several typies:
b - bytes/ unsigned char/ int 8
c - complex
f - float
i - int
s - short
3. python qa_multiply_py_ff.py
Ran 1 test in 0.003s
OK
4. Modify tutorial_multiply_py_ff.xml in grc fold
source:
<param>
<name>...</name>
<key>...</key>
<type>...</type>
</param>
<sink>
<name>in</name>
<type><!-- e.g. int, float, complex, byte, short, xxx_vector, ...--></type>
</sink>
<source>
<name>out</name>
<type><!-- e.g. int, float, complex, byte, short, xxx_vector, ...--></type>
</source>
After modify
<param>
<name>Multiple</name>
<key>multiple</key>
<type>float</type>
</param>
<sink>
<name>in</name>
<type>float</type>
</sink>
<source>
<name>out</name>
<type>float</type>
</source>
- In gr-tutorial folder
mkdir build
cd build/
cmake ../
make
sudo make install
sudo ldconfig
Open GRC (gnuradio companion in command line), and then can find new module