test_cdma_tx.py

#!/usr/bin/env python

from gnuradio import gr, gru, modulation_utils, howto
from gnuradio import usrp, usrp2
from gnuradio import eng_notation
from gnuradio.eng_option import eng_option
from optparse import OptionParser
from pick_bitrate import pick_tx_bitrate
import spreading_dbpsk
import random, time, struct, sys, copy
import usrp_options
# from current dir
import usrp_transmit_path

#import os 
#print os.getpid()
#raw_input('Attach and press enter')

class my_top_block(gr.top_block):
   def __init__(self, modulator, options):
       gr.top_block.__init__(self)
       #source
       self.source = gr.glfsr_source_b(4, 0, 9, 1)#(int degree, bool repeat, int mask, int seed) generate puseudo random
       #chunk2byte
       bits_per_chunk = 1
       self.bits2byte = gr.unpacked_to_packed_bb(bits_per_chunk, gr.GR_LSB_FIRST)
       options = copy.copy(options)    # make a copy so we can destructively modify
       self._verbose            = options.verbose
       self._tx_amplitude       = options.tx_amplitude    # digital amplitude sent to USRP
       self._bitrate            = options.bitrate         # desired bit rate
       self._samples_per_symbol = options.samples_per_symbol  # desired samples/baud
       #modulator
       self._modulator_class = modulator# the modulator_class we are using
       mod_kwargs = self._modulator_class.extract_kwargs_from_options(options)# Get mod_kwargs
       self.modulator = self._modulator_class(**mod_kwargs)
       
       #amplifier
       self.amp = gr.multiply_const_cc(1)
       self.set_tx_amplitude(self._tx_amplitude)

       # Display some information about the setup
       if self._verbose:
           self._print_verbage()

       #setup usrp
       self._setup_usrp_sink(options) 
       
       #create flow graph
       self.connect(self.source, self.bits2byte)
       self.connect(self.bits2byte, self.modulator)        
       self.connect(self.modulator, self.amp)
       self.connect(self.amp, self.u)

   def set_tx_amplitude(self, ampl):
       """
       Sets the transmit amplitude sent to the USRP in volts
       @param: ampl 0 <= ampl < 1.
       """
       self._tx_amplitude = max(0.0, min(ampl, 1))
       self.amp.set_k(self._tx_amplitude)
       
   def _setup_usrp_sink(self, options):
       """
       Creates a USRP sink, determines the settings for best bitrate,
       and attaches to the transmitter's subdevice.
       """
       self.u = usrp_options.create_usrp_sink(options)
       dac_rate = self.u.dac_rate()
       print "dac_rate=", eng_notation.num_to_str(dac_rate)
       print "samples/symbol=", options.samples_per_symbol
       if options.verbose:
           print 'USRP Sink:', self.u
       (self._bitrate, self._samples_per_symbol, self._interp) = \
                       pick_tx_bitrate(options.bitrate, self._modulator_class.bits_per_symbol(), \
                                       options.samples_per_symbol, options.interp, dac_rate, \
                                       self.u.get_interp_rates())
       print "interp_rates=", self._interp
       self.u.set_interp(self._interp)
       self.u.set_auto_tr(True)
       if not self.u.set_center_freq(options.tx_freq):
           print "Failed to set Rx frequency to %s" % (eng_notation.num_to_str(options.tx_freq))
           raise ValueError, eng_notation.num_to_str(options.tx_freq)
       

# /////////////////////////////////////////////////////////////////////////////
#                                   main
# /////////////////////////////////////////////////////////////////////////////

def main():

   mods = modulation_utils.type_1_mods()
   parser = OptionParser(option_class=eng_option, conflict_handler="resolve")#parser
   expert_grp = parser.add_option_group("Expert")

   parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
                     default='dbpsk',
                     help="Select modulation from: %s [default=%%default]"
                           % (', '.join(mods.keys()),))#mod
   parser.add_option("-s", "--size", type="eng_float", default=1500,
                     help="set packet size [default=%default]")#packet size
   parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
                     help="set megabytes to transmit [default=%default]")
   parser.add_option("","--discontinuous", action="store_true", default=False,
                     help="enable discontinous transmission (bursts of 5 packets)")#tx timing
   parser.add_option("","--from-file", default=None,
                     help="use file for packet contents")#tx data file

   usrp_transmit_path.add_options(parser, expert_grp)#config for usrp

   for mod in mods.values():
       mod.add_options(expert_grp)#add mod options

   (options, args) = parser.parse_args ()

   if len(args) != 0:# no parser exit
       parser.print_help()
       sys.exit(1)

   if options.tx_freq is None:# no freq parser exit
       sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
       parser.print_help(sys.stderr)
       sys.exit(1)

   if options.from_file is not None:#tx data read
       source_file = open(options.from_file, 'r')

   # build the graph
   
   tb = my_top_block(mods[options.modulation], options)

   r = gr.enable_realtime_scheduling()
   if r != gr.RT_OK:
       print "Warning: failed to enable realtime scheduling"
   
   tb.start()                       # start flow graph

   tb.wait()                       # wait for it to finish
   
if __name__ == '__main__':
   try:
       main()
   except KeyboardInterrupt:
       pass

最終更新:2011年06月29日 02:29