Uncategorized

How to change a normal Python‐level GNU Radio variable after the flowgraph has started

You can’t usually change a normal Python‐level GNU Radio variable after the flowgraph has started, but there are workarounds = you can make a variable externally adjustable while the script is running by using one of the mechanisms GNU Radio supports for runtime parameter control.

Here are the practical methods you can use:


✅ 1. Use a QT GUI Variable (for manual on-screen control)

If you’re already using the QT GUI:

self.variable = variable = 42
self._variable_range = Range(0, 100, 1, 42, 200)
self._variable_win = RangeWidget(self._variable_range, self.set_variable, "Variable", "counter", float)

This lets you change the variable during execution with a slider or dial.


✅ 2. Add the “Parameter Block” (GNU Radio 3.10+ / 3.11)

GNU Radio now has an official Parameter block (in GRC under Core → Misc).
This allows runtime changes via:

  • CLI gr-ctrl
  • REST API
  • ZMQ message
  • Native GUI element

You add:

  1. Parameter block
  2. Set its ID (e.g., center_freq)
  3. Bind other blocks to it (e.g. Osmocom Source → Center Frequency parameter becomes center_freq).

Now you can change it at runtime using:

gr-ctrl set center_freq 1420405000

Your running script will update instantly.


✅ 3. Use a Python Setter Method (set_variable()) and message ports

If you create a variable like:

self.my_gain = 20

GRC will auto-generate:

def set_my_gain(self, my_gain):
    self.my_gain = my_gain
    self.some_block.set_gain(self.my_gain)

Then you can attach:

  • a Message Strobe
  • a ZMQ SUB socket
  • a REST interface
  • your own external script

and send a value at any time.

Example: ZMQ PUSH → Message Debug → Variable Setter.


✅ 4. Use ZMQ to push parameters from an external script

Flowgraph side:

ZMQ SUB → Python Block → set_variable()

External script:

import zmq, json
 
ctx = zmq.Context()
s = ctx.socket(zmq.PUSH)
s.connect("tcp://127.0.0.1:5557")
 
s.send_json({"my_gain": 30})

This updates your running flowgraph.


✅ 5. Use GNU Radio’s Python API to expose controls

If running headless and you want a terminal interface:

Add something like this to your script:

def run_controller(self):
    while True:
        text = input("New freq: ")
        self.set_center_freq(float(text))

Run it in a thread:

_thread.start_new_thread(self.run_controller, ())

Now you can type values while the flowgraph runs.


⭐ Recommended (Most Robust): Parameter Block + gr-ctrl

If you want a clean, scalable setup (especially for interferometry work), use:

Parameter block with gr-ctrl.

It gives you:

  • No GUI needed
  • Script remains running
  • Update safely via terminal or another program
  • Works across network
  • Supports many parameters

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.