#!/usr/bin/env python from gnuradio import gr, gru from gnuradio import usrp from gnuradio import eng_notation from gnuradio.eng_option import eng_option from optparse import OptionParser import sys import rnm_scopesink def pick_subdevice(u): """ The user didn't specify a subdevice on the command line. If there's a daughterboard on A, select A. If there's a daughterboard on B, select B. Otherwise, select A. """ if u.db[0][0].dbid() >= 0: # dbid is < 0 if there's no d'board or a problem return (0, 0) if u.db[1][0].dbid() >= 0: return (1, 0) return (0, 0) class app_flow_graph(gr.flow_graph): def __init__(self): gr.flow_graph.__init__(self) parser = OptionParser(option_class=eng_option) parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None, help="select USRP Rx side A or B (default=first one with a daughterboard)") parser.add_option("-d", "--decim", type="int", default=16, help="set fgpa decimation rate to DECIM [default=%default]") parser.add_option("-f", "--freq", type="eng_float", default=None, 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("-8", "--width-8", action="store_true", default=False, help="Enable 8-bit samples across USB") parser.add_option("-n", "--frame-decim", type="int", default=1, help="set oscope frame decimation factor to n [default=1]") parser.add_option("-v", "--v-scale", type="eng_float", default=1000, help="set oscope frame decimation factor to n [default=1]") parser.add_option("-v", "--v-scale", type="eng_float", default=1000, help="set oscope initial V/div to SCALE [default=%default]") parser.add_option("-t", "--t-scale", type="eng_float", default=49e-6, help="set oscope initial s/div to SCALE [default=50us]") (options, args) = parser.parse_args() if len(args) != 0: parser.print_help() sys.exit(1) self.show_debug_info = True # build the graph self.u = usrp.source_c(decim_rate=options.decim, nchan=1, fusb_block_size=2048, fusb_nblocks=4) if options.rx_subdev_spec is None: options.rx_subdev_spec = pick_subdevice(self.u) self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec)) if options.width_8: width = 8 shift = 8 format = self.u.make_format(width, shift) print "format =", hex(format) r = self.u.set_format(format) # determine the daughterboard subdevice we're using self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec) input_rate = self.u.adc_freq() / self.u.decim_rate() self.scope = modified_scopesink.scope_sink_c(self, sample_rate=input_rate, frame_decim=options.frame_decim, v_scale=options.v_scale, t_scale=options.t_scale) self.connect(self.u, self.scope) # set initial values 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 options.freq is None: # if no freq was specified, use the mid-point r = self.subdev.freq_range() options.freq = float(r[0]+r[1])/2 self.set_gain(options.gain) if not(self.set_freq(options.freq)): print "Failed to set initial frequency" def set_freq(self, target_freq): """ Set the center frequency we're interested in. @param target_freq: frequency in Hz @rypte: bool Tuning is a two step process. First we ask the front-end to tune as close to the desired frequency as it can. Then we use the result of that operation and our target_frequency to determine the value for the digital down converter. """ r = usrp.tune(self.u, 0, self.subdev, target_freq) if r: # update displayed value #print "Setting freq to " + str(target_freq) # update displayed value #print "Setting freq to " + str(target_freq) return True return False def set_gain(self, gain): #print "Setting gain to " + str(gain) self.subdev.set_gain(gain) def set_decim(self, decim): ok = self.u.set_decim_rate(decim) if not ok: print "set_decim failed" input_rate = self.u.adc_freq() / self.u.decim_rate() self.scope.set_sample_rate(input_rate) if self.show_debug_info: # update displayed values print "decim = " + str(self.u.decim_rate()) print "address@hidden = " + str(self.u.adc_freq() / self.u.decim_rate()) return ok def main(): fg = app_flow_graph() fg.run() if __name__ == '__main__': main()