commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] r3902 - in gnuradio/branches/developers/eb/binstats: g


From: eb
Subject: [Commit-gnuradio] r3902 - in gnuradio/branches/developers/eb/binstats: gnuradio-core/src/lib/general gnuradio-examples/python/usrp
Date: Mon, 30 Oct 2006 15:43:23 -0700 (MST)

Author: eb
Date: 2006-10-30 15:43:22 -0700 (Mon, 30 Oct 2006)
New Revision: 3902

Modified:
   
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.i
   
gnuradio/branches/developers/eb/binstats/gnuradio-examples/python/usrp/usrp_spectrum_sense.py
Log:
work-in-progress

Modified: 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.i
===================================================================
--- 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.i
   2006-10-30 20:30:54 UTC (rev 3901)
+++ 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.i
   2006-10-30 22:43:22 UTC (rev 3902)
@@ -60,6 +60,11 @@
 %rename(feval_ll) gr_py_feval_ll;
 %rename(feval)    gr_py_feval;
 
+%exception {
+  try { $action }
+  catch (Swig::DirectorException &e) { std::cerr << e.getMessage();  
SWIG_fail; }
+}
+
 %{
 
 // class that ensures we acquire and release the Python GIL
@@ -142,7 +147,10 @@
   double calleval(double x)
   {
     ensure_py_gil_state _lock;
-    return eval(x);
+    std::cerr << "gr_py_feval_dd!\n";
+    double t = eval(x);
+    std::cerr << "eval returned " << t << std::endl;
+    return t;
   }
 };
 

Modified: 
gnuradio/branches/developers/eb/binstats/gnuradio-examples/python/usrp/usrp_spectrum_sense.py
===================================================================
--- 
gnuradio/branches/developers/eb/binstats/gnuradio-examples/python/usrp/usrp_spectrum_sense.py
       2006-10-30 20:30:54 UTC (rev 3901)
+++ 
gnuradio/branches/developers/eb/binstats/gnuradio-examples/python/usrp/usrp_spectrum_sense.py
       2006-10-30 22:43:22 UTC (rev 3902)
@@ -11,42 +11,120 @@
 import math
 
 
-class wfm_rx_graph (gr.flow_graph):
+class tune(gr.feval_dd):
+    """
+    This class allows C++ code to callback into python.
+    """
+    def __init__(self, fg):
+        gr.feval_dd.__init__(self)
+        self.fg = fg
+        print "tune: __init__ self =", self
 
+    def eval(self, ignore):
+        """
+        This method is called from gr.bin_statistics_f when it want to change
+        the center frequency.  This method tunes the front end to the new 
center
+        frequency, and returns the new frequency as its result.
+        """
+        #print "tune.eval!" >> sys.stderr
+        print "tune: eval self =", self
+        print "tune.eval!"
+        print "self.fg =", self.fg
+        new_freq = self.fg.set_next_frequency()
+        print "new_freq =", new_freq
+        return new_freq
+        
+
+class parse_msg(object):
+    def __init__(self, msg):
+        self.center_freq = msg.arg1()
+        self.vlen = int(msg.arg2())
+        assert(msg.length() == self.vlen * gr.sizeof_float)
+        # FIXME consider using Numarry vector
+        self.data = struct.unpack('%df' % (self.vlen,), msg.to_string())
+
+
+class my_graph(gr.flow_graph):
+
     def __init__(self):
         gr.flow_graph.__init__(self)
 
-        parser=OptionParser(option_class=eng_option)
+        usage = "usage: %prog [options] min_freq max_freq"
+        parser = OptionParser(option_class=eng_option, usage=usage)
         parser.add_option("-R", "--rx-subdev-spec", type="subdev", 
default=(0,0),
                           help="select USRP Rx side A or B (default=A)")
-        parser.add_option("-f", "--freq", type="eng_float", default=100.1e6,
-                          help="set frequency to FREQ", metavar="FREQ")
         parser.add_option("-g", "--gain", type="eng_float", default=None,
                           help="set gain in dB (default is midpoint)")
+        parser.add_option("", "--tune-delay", type="eng_float", default=1e-3, 
metavar="SECS",
+                          help="time to delay (in seconds) after changing 
frequency [default=%default]")
+        parser.add_option("", "--dwell-delay", type="eng_float", 
default=10e-3, metavar="SECS",
+                          help="time to dwell (in seconds) at a given frequncy 
[default=%default]")
+        parser.add_option("-F", "--fft-size", type="int", default=512,
+                          help="specify number of FFT bins [default=%default]")
+        parser.add_option("-d", "--decim", type="intx", default=8,
+                          help="set decimation to DECIM [default=%default]")
+        parser.add_option("", "--real-time", action="store_true", 
default=False,
+                          help="Attempt to enable real-time scheduling")
+        parser.add_option("-B", "--fusb-block-size", type="int", default=0,
+                          help="specify fast usb block size 
[default=%default]")
+        parser.add_option("-N", "--fusb-nblocks", type="int", default=0,
+                          help="specify number of fast usb blocks 
[default=%default]")
 
         (options, args) = parser.parse_args()
-        if len(args) != 0:
+        if len(args) != 2:
             parser.print_help()
             sys.exit(1)
-        
 
+        self.min_freq = eng_notation.str_to_num(args[0])
+        self.max_freq = eng_notation.str_to_num(args[1])
+
+        if self.min_freq > self.max_freq:
+            self.min_freq, self.max_freq = self.max_freq, self.min_freq   # 
swap them
+
+       self.fft_size = options.fft_size
+
+
+        if not options.real_time:
+            realtime = False
+        else:
+            # Attempt to enable realtime scheduling
+            r = gr.enable_realtime_scheduling()
+            if r == gr.RT_OK:
+                realtime = True
+            else:
+                realtime = False
+                print "Note: failed to enable realtime scheduling"
+
+        # If the user hasn't set the fusb_* parameters on the command line,
+        # pick some values that will reduce latency.
+
+        if options.fusb_block_size == 0 and options.fusb_nblocks == 0:
+            if realtime:                        # be more aggressive
+                options.fusb_block_size = gr.prefs().get_long('fusb', 
'rt_block_size', 1024)
+                options.fusb_nblocks    = gr.prefs().get_long('fusb', 
'rt_nblocks', 16)
+            else:
+                options.fusb_block_size = gr.prefs().get_long('fusb', 
'block_size', 4096)
+                options.fusb_nblocks    = gr.prefs().get_long('fusb', 
'nblocks', 16)
+    
+        #print "fusb_block_size =", options.fusb_block_size
+       #print "fusb_nblocks    =", options.fusb_nblocks
+
         # build graph
         
-        self.u = usrp.source_c()                    # usrp is data source
+        self.u = usrp.source_c(fusb_block_size=options.fusb_block_size,
+                               fusb_nblocks=options.fusb_nblocks)
 
+
         adc_rate = self.u.adc_rate()                # 64 MS/s
-        usrp_decim = 8
-        #usrp_decim = 16
+        usrp_decim = options.decim
         self.u.set_decim_rate(usrp_decim)
-        usrp_rate = adc_rate / usrp_decim           # 8 MS/s
+        usrp_rate = adc_rate / usrp_decim
 
         self.u.set_mux(usrp.determine_rx_mux_value(self.u, 
options.rx_subdev_spec))
         self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
         print "Using RX d'board %s" % (self.subdev.side_and_name(),)
 
 
-       self.fft_size = 512
-
        s2v = gr.stream_to_vector(gr.sizeof_gr_complex, self.fft_size)
 
         mywindow = window.blackmanharris(self.fft_size)
@@ -61,30 +139,46 @@
         log = gr.nlog10_ff(20, self.fft_size,
                            
-20*math.log10(self.fft_size)-10*math.log10(power/self.fft_size))
                
-       sink = gr.null_sink(gr.sizeof_float * self.fft_size)
+        self.freq_step = 0.75 * usrp_rate
+        self.min_center_freq = self.min_freq + self.freq_step/2
+        self.max_center_freq = self.max_freq - self.freq_step/2
 
-       self.connect(self.u, s2v, fft, c2mag, log, sink)
+        self.next_freq = self.min_center_freq
+        
+        tune_delay  = max(0, int(round(options.tune_delay * usrp_rate / 
self.fft_size)))  # in fft_frames
+        dwell_delay = max(1, int(round(options.dwell_delay * usrp_rate / 
self.fft_size))) # in fft_frames
 
+        self.msgq = gr.msg_queue(16)
+        self._tune_callback = tune(self)        # hang on to this to keep it 
from being GC'd
+        stats = gr.bin_statistics_f(self.fft_size, self.msgq,
+                                    self._tune_callback, tune_delay, 
dwell_delay)
+
+       self.connect(self.u, s2v, fft, c2mag, log, stats)
+
         if options.gain is None:
             # if no gain was specified, use the mid-point in dB
             g = self.subdev.gain_range()
             options.gain = float(g[0]+g[1])/2
 
-        if abs(options.freq) < 1e6:
-            options.freq *= 1e6
-
-        # set initial values
-
         self.set_gain(options.gain)
        print "gain =", options.gain
 
-        if not(self.set_freq(options.freq)):
-            self._set_status_msg("Failed to set initial frequency")
 
+    def set_next_freq(self):
+        print "set_next_freq"
+        target_freq = self.next_freq
+        self.next_freq = self.next_freq + self.freq_step
+        if self.next_freq > self.max_center_freq:
+            self.next_freq = self.min_center_freq
 
-       
+        print "target_freq =", target_freq
 
+        if not self.set_freq(target_freq):
+            print "Failed to set frequency to", target_freq
 
+        return target_freq
+                          
+
     def set_freq(self, target_freq):
         """
         Set the center frequency we're interested in.
@@ -97,26 +191,24 @@
         the result of that operation and our target_frequency to
         determine the value for the digital down converter.
         """
-        r = self.u.tune(0, self.subdev, target_freq)
-        
-        if r:
-            self.freq = target_freq
-            self._set_status_msg("OK", 0)
-            return True
+        return self.u.tune(0, self.subdev, target_freq)
 
-        self._set_status_msg("Failed", 0)
-        return False
 
     def set_gain(self, gain):
         self.subdev.set_gain(gain)
 
-    def _set_status_msg(self, msg, which=0):
-        print msg
 
+def main_loop(fg):
+    while 1:
+        m = parse_msg(fg.msgq.delete_head())
+        # do something with the data
+        print m.center_freq
     
 if __name__ == '__main__':
-    fg = wfm_rx_graph()
+    fg = my_graph()
     try:
-        fg.run()
+        fg.start()              # start executing flow graph in another 
thread...
+        main_loop(fg)
+        
     except KeyboardInterrupt:
         pass





reply via email to

[Prev in Thread] Current Thread [Next in Thread]