Exemplo n.º 1
0
    def __init__(self, node_type, node_index, demodulator, modulator, rx_callback, options):
        gr.top_block.__init__(self)
		
        # is this node the sub node or head?
        self._node_type = node_type
        self._node_id   = node_index	

        # install the socket control channel
        self._socket_ctrl_chan = socket_ctrl_channel(self._node_type, self)
        # start the socket server to capture the control messages
        self._socket_ctrl_chan._sock_server.start()

        if(options.sx_freq is not None):
            # Work-around to get the modulation's bits_per_symbol
            args = demodulator.extract_kwargs_from_options(options)
            symbol_rate = options.bitrate / demodulator(**args).bits_per_symbol()
            if (options.sx_samprate is None):
                ask_sample_rate = symbol_rate*options.samples_per_symbol
            else:
                ask_sample_rate = options.sx_samprate
			
            self._rx_freq = options.sx_freq
            self._tx_freq = options.sx_freq # use the same frequence 
            self._sample_rate = ask_sample_rate

            # configuration the usrp sensors and transmitters
            # Automatically USRP devices discovery
            devices = uhd.find_devices_raw()
            n_devices = len(devices)
            addrs = []
        
            if (n_devices == 0):
                sys.exit("no connected devices")
            elif (n_devices >= 1):
                for i in range(n_devices):
                        addr_t = devices[i].to_string()  #ex. 'type=usrp2,addr=192.168.10.109,name=,serial=E6R14U3UP'
                        addrs.append(addr_t[11:30]) # suppose the addr is 192.168.10.xxx
                        addrs[i]
                
            if (n_devices == 1 and self._node_type == CLUSTER_NODE):
                sys.exit("only one devices for the node, we need both communicator and sensor for cluster node")
            elif (n_devices > 1 and self._node_type == CLUSTER_HEAD):
                sys.exit("only one devices is need for cluster head")

            # Configure Sensors, with all GPS sync
            self.sensors = []
            for i in range(n_devices):
                self.sensors.append(uhd_sensor(addrs[i], ask_sample_rate,
                                                options.sx_freq, options.sx_gain,
                                                options.sx_spec, options.sx_antenna, 
                                                options.verbose))
                self.sensors[i].u.set_start_on_demand()  # the sensor will start sensing on demand												
                if self.sensors[i].u.get_time_source(0) == "none":
                    self.sensors[i].u.set_time_source("mimo", 0)  # Set the time source without GPS to MIMO cable
                    self.sensors[i].u.set_clock_source("mimo",0) 
					
	        	# file sinks
                filename = "%s_sensed.dat" %(NODES_PC*self._node_id + i)
                self.connect(self.sensors[i].u, gr.file_sink(gr.sizeof_gr_complex, filename))

            # Configure Transmitters	
            self.transmitters = []
            self.txpaths = []
            self.tx_srcs = []
            for i in range(n_devices):	
                self.transmitters.append(uhd_transmitter(addrs[i], symbol_rate,
                                                options.samples_per_symbol,
                                                options.tx_samprate,
                                                options.tx_freq, options.tx_gain,
                                                options.tx_spec, options.tx_antenna,
                                                options.verbose, True))	#TDMA transmitter
                self.txpaths.append(transmit_path(modulator, options))
                self.tx_srcs.append(tx_data_src(self.txpaths[i]))
                #filename = "file%.dat" %(i)
                #self.file_src = gr.file_source(gr.sizeof_gr_complex*1, filename, True)      
                #self.source.u.set_center_freq(uhd.tune_request(options.rx_freq, ask_sample_rate*2), 0)
                #print 'In locking '
                #while (self.source.u.get_sensor("lo_locked").to_bool() == False):
                #    print '.'
        
                 #print 'Locked'

        self.timer =  threading.Timer(1, self.start_streaming)
Exemplo n.º 2
0
    def __init__(self, node_type, node_index, mod_class, demod_class,
                 rx_callback, options):

        gr.top_block.__init__(self)

        # is this node the sub node or head?
        self._node_type = node_type
        self._node_id   = node_index

        # Get the modulation's bits_per_symbol
        args = mod_class.extract_kwargs_from_options(options)
        symbol_rate = options.bitrate / mod_class(**args).bits_per_symbol()

        # Automatically USRP devices discovery
        devices = uhd.find_devices_raw()
        n_devices = len(devices)
        addrs = []
        
        if (n_devices == 0):
            sys.exit("no connected devices")
        elif (n_devices >= 1):
            for i in range(n_devices):
                addr_t = devices[i].to_string()  #ex. 'type=usrp2,addr=192.168.10.109,name=,serial=E6R14U3UP'
                addrs.append(addr_t[11:30]) # suppose the addr is 192.168.10.xxx
                addrs[i]
                
        if (n_devices == 1 and self._node_type == CLUSTER_NODE):
            sys.exit("only one devices for the node, we need both communicator and sensor for cluster node")
        elif (n_devices > 1 and self._node_type == CLUSTER_HEAD):
            sys.exit("only one devices is need for cluster head")
                
        # Configure the devices to 
        # 1 Communicator
        # N Sensors (N >= 1)
        # How to determin the Communicator?
        #       we need to assure all the sensors to be sync with GPS, except the communicator
        #       thus, the device with non-sync time will be assigned as communicator
        # Firstly, instantiate all the USRP receivers (including the sensors and the communicator)
        self.sensors = []
        t = []
        dt = []
        time_src = []
        role = []
        
        for i in range(n_devices):
            self.sensors.append(uhd_sensor(addrs[i], options.sx_samprate,
                                                options.sx_freq, options.sx_gain,
                                                options.sx_spec, options.sx_antenna, 
                                                options.verbose))       
            if self.sensors[i].u.get_time_source(0) == "none":
                self.sensors[i].u.set_time_source("mimo", 0)  # Set the time source without GPS to MIMO cable
                self.sensors[i].u.set_clock_source("mimo",0)                
            time_src.append(self.sensors[i].u.get_time_source(0))

        time.sleep(4)   # time to sync between mimo connected devices
            
        for i in range(n_devices):
            t.append(self.sensors[i].u.get_time_now().get_real_secs())

            role.append("sensor")
            
            dt.append(1)
            if i > 0:
                for j in range(i):
                    if (abs(t[j] - t[i]) < 0.1): # Find a pair of GPS synched devices
                        dt[i] += 1
                        dt[j] += 1 
                        
        # Find the communicator
        found_com = 0
        print t
        print addrs
        print time_src
        print dt
        
        cind = 0
        
        for i in range(n_devices):
            if found_com == 1:
                break
                
            if n_devices == 2:
                if self.sensors[i].u.get_time_source(0) == "mimo" and found_com == 0:
                    cind = i
                    found_com = 1
                elif i == 1 and slef.sensors[i].u.get_time_source(0) == "gpsdo" and found_com == 0:
                    cind = i
                    found_com = 1
                elif i == 1:
                    sys.exit("configure error, no communicaotr found for 2 devices")                    
      
            elif (dt[i] != n_devices - 1 and dt[i] != 1) or (sum(dt) == n_devices):
                sys.exit("configure error or Not sync")
            elif dt[i] == 1 and found_com == 0: # We select this as communicator
                cind = i
                found_com = 1
            
            if found_com == 1:
                del self.sensors[cind] # delete this devices from sensor list
                role[cind] = "com"
                self.source =  uhd_receiver(addrs[cind], symbol_rate,
                                            options.samples_per_symbol,
                                            options.rx_freq, options.rx_gain,
                                            options.rx_spec, options.rx_antenna,
                                            options.verbose)
                self.sink = uhd_transmitter(addrs[cind], symbol_rate,
                                            options.samples_per_symbol,
                                            options.tx_freq, options.tx_gain,
                                            options.rx_spec, options.rx_antenna,
                                            options.verbose)
                del addrs[cind] # Ignore the addrs of communicator                                            

        print role
        
        if found_com == 0: # no communicator found
            sys.exit("Configuration Error")

        # Setup the rest of USRPs as sensors
        if (self._node_type == CLUSTER_NODE and STREAM_OR_FINITE == 0):
            for i in range(n_devices - 1):
                filename = "%s_sensed.dat" %(addrs[i])
                self.connect(self.sensors[i].u, gr.file_sink(gr.sizeof_gr_complex, filename))
        
        options.samples_per_symbol = self.source._sps     
        
        self.txpath = transmit_path(mod_class, options)
        self.rxpath = receive_path(demod_class, rx_callback, options)
        
        self.connect(self.txpath, self.sink)
        self.connect(self.source, self.rxpath)