Forum: GNU Radio is the example defunct or am I doing something wrong ?

Posted by ronald van Aalst (raalst)
on 2013-02-07 23:09
Hi All,

I'm learning about GnuRadio and Python, and now I try to code my own
block.
The first iteration of the dialtone example works.
now I want to vary the frequency in steps. after an evening of trying
and googling I'm a bit lost. hopefully someone more experienced
would be so kind as to check my code and/or the example provided.

I follow this example snippet in
http://gnuradio.org/redmine/projects/gnuradio/wiki...

 1 class my_top_block(gr.top_block):
 2     def __init__(self):
 3         gr.top_block.__init__(self)
 4     ... # Define some blocks
 5     self.amp = gr.multiply_const_ff(1) # Define multiplier block
 6     ... # Define more blocks
 7
 8         self.connect(..., self.amp, ...) # Connect all blocks
 9
10     def set_volume(self, volume):
11     self.amp.set_k(volume)
12
13 if __name__ == '__main__':
14     my_top_block().start()
15     sleep(2) # Wait 2 secs (assuming sleep was imported!)
16     my_top_block.set_volume(2) # Pump up the volume (by factor 2)
17     sleep(2) # Wait 2 secs (assuming sleep was imported!)
18     my_top_block().stop()

the problem is in referencing the amp block in the way shown above.
In my case I need to access a signal source src0 in the same way.
this is my code.
#!/usr/bin/env python
from gnuradio import gr
from gnuradio import audio
import time

class RvA_top_block(gr.top_block):
  def __init__(self):
    gr.top_block.__init__(self)
    sample_raate = 44100
    ampl = 0.1
    self.src0 = gr.sig_source_f (sample_raate,gr.GR_SIN_WAVE, 350, ampl)
    self.src1 = gr.sig_source_f (sample_raate,gr.GR_SIN_WAVE, 440, ampl)
    self.dst = audio.sink(sample_raate, "")
          self.connect (self.src0, (self.dst,0))
          self.connect (self.src1, (self.dst,1))
  def setfreq(self,fq):
    self.scr0.set_frequency(fq)
  def getfreq(self):
    return self.scr0.frequency()

if __name__ == '__main__':
  try:
    i = 350.0
    while ( i < 1000 ):
      RvA_top_block().start()
      time.sleep(2)
      print i
      i = i * 1.2
      RvA_top_block().stop()
      RvA_top_block().wait()
      RvA_top_block().setfreq(i)
  except [[KeyboardInterrupt]]:
    pass

the result is however a complaint that
Traceback (most recent call last):
  File "./test.py", line 35, in <module>
    RvA_top_block().setfreq(i)
  File "./test.py", line 20, in setfreq
    self.scr0.set_frequency(fq)
  File
"/usr/local/lib/python2.7/dist-packages/gnuradio/gr/top_block.py", line
94, in __getattr__
    return getattr(self._tb, name)
AttributeError: 'gr_top_block_sptr' object has no attribute 'scr0'

any help is greatly appreciated. I'm curious what the difference would
be between the example "amp" and my "src0". I must be stupid, but 'im
not seeing it.

all this runs on an plain ubuntu. other examples work nicely and the
install went smoothly. I took the most current version og gnuradio and
companion about 2 weeks ago.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.