I have to make the flowgraph run and stop automatically during a preset period, because I have to make the flowgraph only work when the beacon satellite passes the ground station. I am using USRP1.
This is the sort of task for which it is at least easiest to do outside of the model of GNU Radio Companion. That is, you would take the generated code and instead of executing it directly, modify it — or, perhaps, load it as a module.
First of all, I recommend changing from WX GUI to QT GUI. WX support in GNU Radio Companion is deprecated, and the generated code isn't quite the same as what I'm going to talk about.
If you look at the generated code, you will see this structure (I've removed the irrelevant parts).
class GRBR(gr.top_block, Qt.QWidget): ... def main(top_block_cls=GRBR, options=None): ... tb = top_block_cls() tb.start() tb.show() def quitting(): tb.stop() tb.wait() qapp.connect(qapp, Qt.SIGNAL("aboutToQuit()"), quitting) qapp.exec_() if __name__ == '__main__': main()
As you can see, there are start and stop methods on
tb. All you need to do is call them (well, and also
.wait() immediately after every
main() will be sitting inside
qapp.exec_() for the lifetime of the program. You could use a separate thread, or probably better, use something within QT's event loop, which I hear would be QTimer (I haven't programmed with Qt myself).
I mentioned above using the GRC generated code without modifying it. That would look like this:
import GRBR # GRC-generated python, loaded as a module def main(...): # exact same code as before if __name__ == '__main__': main()
This has the advantage that you can keep developing your flow graph in GRC.
You may also wish to consider not actually stopping your flow graph. This is because stopping and restarting can have some negative effects (it does unnecessary work and some source or sink blocks have had or still have bugs related to stop/start). Instead, gate the signal — that is, drop samples until you need them.
The way to do this on external control would be to use the
copy block, which has a boolean parameter called
enabled. As long as it is not enabled, then it discards samples rather than passing them along.
You could also write a bit of Python code that is a
gr.hier_block2, and an accompanying GRC
.xml file telling GRC how to use it, which bundles up the copy block and the timer control in a unit you can actually use within GRC, and not give up the use of GRC's “run” button.
(In principle, this same approach could be used to do
start control, but that seems unwise to me from a code structure perspective.)