The way I've done it is to use a Parameter block in the hierarchical block. I give this an ID of, e.g, freq_ratio
, type float
, default value of 0.5
. Whereever I use a block (e.g. a Signal Source) that has a sample rate parameter (which, as you say, commonly defaults to samp_rate
), I replace that with a 1.0
, and I set the frequency to freq_ratio
.
To use the block in a top level flow chart, the hierarchical block's freq_ratio
has that default value of 0.5
, but here in the top level that can be replaced with 1000.0 / samp_rate
. That will have the Signal Source block inside produce me a signal at 1kHz, assuming the top block's samp_rate
is used in a real device (e.g. an Audio Out block).
The point is that "sample rate" doesn't really mean anything as such in a GNU radio flowchart, except where it's used by a block that represents something physical (e.g. and Audio Out block, or a real SDR). So one can quite happily work with ratios.
Another way you can do it is to use two parameters in the hierarchical block. The first can be called samp_rate
; give it a default value. The second can be called freq
; again give it a default value. The Signal Source block in the hierarchical block can then use those two parameters for its sample rate and frequency. In the top level, you replace the default value for the hierachical block's sample rate parameter with samp_rate
, and the default value for its frequency with what ever you want (a constant, some other variable, a Qt Range Widget's i.d, etc). The samp_rate
you've typed here in the top block will be the top block's sample rate variable.
I've Not Really Answered Your Question Yet
Basically everything above is how it can be done for maximum convenience, but I'm afraid it's not possible to automagically directly use the top block's samp_rate
for a block in hierarchical block. That's the answer, I'm afraid.
This makes sense; if you were writing source code, you'd not expect a function to be able to access a global variable that hadn't been defined in a scope that the function access.
What anerisgreat
was saying in their second para:
You can always access the .py of the block and make it read a global variable.
is that, once GRC has generated all the .py source code files for you, you can then manually edit them so that the hierarchical block does use a samp_rate
global variable instead of the value of one of its Parameter blocks.
That works because, by then, the function (or class + methods) that has been generated from the hierarchical block can now be given (by manually editting the .py files) sight of the scope where the top block's samp_rate
has been defined. But this is a pretty unmaintainable approach to take.