class Avoider(object): def __init__(self): self.radio = RadioAPI() self.packet = Packet('A') self.data = [] for i in range(50): self.data.append(0xff) self.tx_packet_number = 1 def _configure_radio(self, power, frequency, data_rate, modulation): """ Configure radio for operation. """ self.radio.configure_radio(power, frequency, data_rate, modulation) def _send_packet(self): """ Transmit packet. """ self.packet.set_flags_node_a() # location = self.kb.get_state()['current_location'] location = 1 tx_packet = self.packet.make_packet(self.tx_packet_number, location, self.data) self.radio.transmit(tx_packet) self.tx_packet_number += 1 def _receive_packet(self): """ Receive packet. """ rx_packet = self.radio.receive(rx_fifo_threshold=63, timeout=1.0) if rx_packet == []: # this occurs when timeout has been exceeded return else: packet_number, time_stamp, location, flags, data = self.packet.parse_packet( rx_packet) print "packet_number=%d time_stamp=%f location=%d flags=0x%x" % ( packet_number, time_stamp, location, flags) def _listen(self, threshold): """ Listen before talk. """ status = self.radio.listen(threshold, timeout=1.0) # if status == 'clear': # print "channel clear" return status def run(self): """ Run the radio subsystem. """ parser = argparse.ArgumentParser() parser.add_argument("-f", type=float, default=432e6, metavar='frequency', dest='frequency', help="Transmit frequency (default: %(default)s)") parser.add_argument( "-m", type=str, default='gfsk', metavar='modulation', dest='modulation', choices=['gfsk', 'fsk', 'ask', 'cw'], help="Select modulation from [%(choices)s] (default: %(default)s)") parser.add_argument( "-p", type=int, default=17, metavar='power', dest='power', choices=[1, 2, 5, 8, 11, 14, 17, 20], help= "Select transmit power from [%(choices)s] (default: %(default)s)") parser.add_argument("-r", type=float, default=4.8e3, metavar='bitrate', dest='bitrate', help="Set bitrate (default: %(default)s)") parser.add_argument("-t", type=int, default=150, metavar='threshold', dest='threshold', help="Set rssi threshold (default: %(default)s)") parser.add_argument( "-d", type=str, default="data_file.txt", metavar='data_file', dest='data_file', help="Data file to store results (default: %(default)s)") print "parsing args" args = parser.parse_args() frequency = args.frequency modulation = args.modulation power = args.power bitrate = args.bitrate threshold = args.threshold data_file = args.data_file print "data_file", data_file f = open(data_file, 'r+') print "opened file" l = f.readlines() if l == []: self.first_time = True else: self.first_time = False jump_freq = float(l[0].strip('\n')) f.close() self.radio.startup() self.radio.configure_radio(power, frequency, bitrate, modulation) hop = False while True: status = self._listen(threshold) if self.first_time == True: if status == 'clear': if hop == True: self.toc = time.time() print "arrived at final destination, time to find new channel: ", self.toc - self.tic hop = False s = str(frequency) + "\n" f.write(s) f.close() self._send_packet() else: if hop == False: print "reconfiguring radio, starting timer" hop = True self.tic = time.time() frequency += 2e6 print "changing to new center frequency: %f" % ( frequency, ) self.radio.configure_radio(power, frequency, bitrate, modulation) else: if status == 'clear': if hop == True: self.toc = time.time() print "arrived at final destination, time to find new channel: ", self.toc - self.tic hop = False self._send_packet() else: if hop == False: print "reconfiguring radio, starting timer" hop = True self.tic = time.time() frequency = jump_freq print "changing to new center frequency: %f" % ( frequency, ) self.radio.configure_radio(power, frequency, bitrate, modulation) def shutdown(self): self.radio.shutdown()
class StandAloneRadioA(object): def __init__(self): self.radio = RadioAPI() self.packet = Packet('A') self.data = [] for i in range(50): self.data.append(0xff) self.tx_packet_number = 1 def _configure_radio(self, power, frequency, data_rate, modulation): """ Configure radio for operation. """ self.radio.configure_radio(power, frequency, data_rate, modulation) def _send_packet(self): """ Transmit packet. """ self.packet.set_flags_node_a() # location = self.kb.get_state()['current_location'] location = 1 tx_packet = self.packet.make_packet(self.tx_packet_number, location, self.data) self.radio.transmit(tx_packet) self.tx_packet_number += 1 def _receive_packet(self): """ Receive packet. """ rx_packet = self.radio.receive(rx_fifo_threshold=63, timeout=1.0) if rx_packet == []: # this occurs when timeout has been exceeded return else: packet_number, time_stamp, location, flags, data = self.packet.parse_packet( rx_packet) print "packet_number=%d time_stamp=%f location=%d flags=0x%x" % ( packet_number, time_stamp, location, flags) def _listen(self): """ Listen before talk. """ status = self.radio.listen(rssi_threshold=100, timeout=1.0) if status == 'clear': print "channel clear" def run(self): """ Run the radio subsystem. """ parser = argparse.ArgumentParser() # parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) # parser.add_argument("-f", type=float, default=434e6, metavar='frequency', dest='frequency', # nargs=1, help="Transmit frequency (default: %(default)s)") parser.add_argument("-f", type=float, default=434e6, metavar='frequency', dest='frequency', help="Transmit frequency (default: %(default)s)") parser.add_argument( "-m", type=str, default='gfsk', metavar='modulation', dest='modulation', choices=['gfsk', 'fsk', 'ask', 'cw'], help="Select modulation from [%(choices)s] (default: %(default)s)") parser.add_argument( "-p" "--power", type=int, default=17, metavar='power', dest='power', choices=[1, 2, 5, 8, 11, 14, 17, 20], help= "Select transmit power from [%(choices)s] (default: %(default)s)") parser.add_argument("-r" "--bitrate", type=float, default=4.8e3, metavar='bitrate', dest='bitrate', help="Set bitrate (default: %(default)s)") parser.add_argument("-s", type=bool, default=False, metavar='run_sweep', dest='run_sweep', help="Set sweep mode (default: %(default)s)") parser.add_argument( "-i", type=bool, default=False, metavar='interactive', dest='interactive', help="Use interactive sweep (default: %(default)s)") args = parser.parse_args() self.radio.startup() run_sweep = args.run_sweep interactive = args.interactive frequency = args.frequency modulation = args.modulation power = args.power data_rate = args.bitrate if run_sweep == False: self._configure_radio(power, frequency, data_rate, modulation) state = "listen" while True: self._send_packet() else: f = 295e6 freqs = [] while f < 930e6: freqs.append(f) f += 1e6 for i in freqs: if modulation == 'cw': self._configure_radio(power, i, data_rate, modulation) print "changing to freq: ", i self._send_packet() if interactive == True: s = raw_input('Press enter for next frequency') self.radio.set_ready_mode() else: time.sleep(0.1) else: self._configure_radio(power, i, data_rate, modulation) print "changing to freq: ", i for j in range(2): self._send_packet() # fc, hbsel, fb = freq_utils.carrier_freq(i) # for j in range(10): # self.setup_rf(fc, hbsel, fb) # self.tx_data() # print "transmitted packet" # time.sleep(0.01) # pass # def sweep(self): def shutdown(self): self.radio.shutdown()
class TxOnePacket(object): """ Transmitter. """ def __init__(self): self.data = NodeAData() self.packet = Packet('A') self.radio = RadioAPI() self.tx_packet_number = 1 def _configure_radio(self, power, frequency, data_rate, modulation): """ Configure radio for operation. """ self.radio.configure_radio(power, frequency, data_rate, modulation) def _listen(self): status = self.radio.listen(rssi_threshold=100, timeout=0.1) if status == 'clear': pass # print "channel clear" def _receive_packet(self): """ Receive packet """ rx_packet = self.radio.receive(rx_fifo_threshold=64, timeout=None) pkt_num, t, loc, flags, data = self.packet.parse_packet(rx_packet) return pkt_num, loc, flags, data def _send_packet(self, mode, mod=None, eirp=None, bitrate=None): """ Transmit data. Parameters ---------- mode : str Mode of operation. Used to create appropriate packet header and payload. {`send_reconfig_command` | `request_data` | `stream_data`}. mod : str, opt Modulation, one of {`fsk` | `gfsk` | `ook` }. eirp : int, opt Transmit power, one of { 8 | 11 | 14 | 17 }. bitrate : float, opt Radio bitrate, one of {...} """ location = 1 if mode == 'stream_data': print "streaming data packets" self.packet.set_flags_node_a(send_stream=True) payload = self.data.pack_data(mode) elif mode == 'send_reconfig_command': self.packet.set_flags_node_a(send_command=True) payload = self.data.pack_data(mode, mod, eirp, bitrate) elif mode == 'request_data': self.packet.set_flags_node_a(request_data=True) payload = self.data.pack_data(mode) else: print 'error in _send_packet, no mode specified' raise ValueError tx_packet = self.packet.make_packet(self.tx_packet_number, location, payload) self.tx_packet_number += 1 self.radio.transmit(tx_packet) print "packet sent" def run(self): """ Run transmitter. """ parser = argparse.ArgumentParser() parser.add_argument("-f", type=float, default=434e6, metavar='frequency', dest='frequency', nargs=1, help="Transmit frequency (default: %(default)s)") parser.add_argument("-m", type=str, default='gfsk', metavar='modulation', dest='modulation', choices=['gfsk', 'fsk', 'ask'], help="Select modulation from [%(choices)s] (default: %(default)s)") parser.add_argument("-p", type=int, default=17, metavar='power', dest='power', choices=[8, 11, 14, 17, 20], help="Select transmit power from [%(choices)s] (default: %(default)s)") parser.add_argument("-r", type=float, default=4.8e3, metavar='bitrate', dest='bitrate', help="Set bitrate (default: %(default)s)") parser.add_argument("-n", type=int, default=1, metavar='num_pkts', dest='num_pkts', help="Set number of packets to send (default: %(default)s)") args = parser.parse_args() frequency = args.frequency modulation = args.modulation power = args.power data_rate = args.bitrate num_pkts = args.num_pkts self.radio.startup() self._configure_radio(power, frequency, data_rate, modulation) for i in range(num_pkts): self._send_packet('stream_data')
class TxNPackets(object): """ Transmitter. """ def __init__(self): self.data = NodeAData() self.packet = Packet('A') self.radio = RadioAPI() self.tx_packet_number = 1 def _configure_radio(self, power, frequency, data_rate, modulation): """ Configure radio for operation. """ self.radio.configure_radio(power, frequency, data_rate, modulation) def _listen(self): status = self.radio.listen(rssi_threshold=100, timeout=0.1) if status == 'clear': pass # print "channel clear" def _receive_packet(self): """ Receive packet """ rx_packet = self.radio.receive(rx_fifo_threshold=64, timeout=None) pkt_num, t, loc, flags, data = self.packet.parse_packet(rx_packet) return pkt_num, loc, flags, data def _send_packet(self, mode, mod=None, eirp=None, bitrate=None): """ Transmit data. Parameters ---------- mode : str Mode of operation. Used to create appropriate packet header and payload. {`send_reconfig_command` | `request_data` | `stream_data`}. mod : str, opt Modulation, one of {`fsk` | `gfsk` | `ook` }. eirp : int, opt Transmit power, one of { 8 | 11 | 14 | 17 }. bitrate : float, opt Radio bitrate, one of {...} """ location = 1 if mode == 'stream_data': print "streaming data packets" self.packet.set_flags_node_a(send_stream=True) payload = self.data.pack_data(mode) elif mode == 'send_reconfig_command': self.packet.set_flags_node_a(send_command=True) payload = self.data.pack_data(mode, mod, eirp, bitrate) elif mode == 'request_data': self.packet.set_flags_node_a(request_data=True) payload = self.data.pack_data(mode) else: print 'error in _send_packet, no mode specified' raise ValueError tx_packet = self.packet.make_packet(self.tx_packet_number, location, payload) self.tx_packet_number += 1 self.radio.transmit(tx_packet) print "packet sent" def run(self): """ Run transmitter. """ parser = argparse.ArgumentParser() parser.add_argument("-f", type=float, default=434e6, metavar='frequency', dest='frequency', help="Transmit frequency (default: %(default)s)") parser.add_argument( "-m", type=str, default='gfsk', metavar='modulation', dest='modulation', choices=['gfsk', 'fsk', 'ask'], help="Select modulation from [%(choices)s] (default: %(default)s)") parser.add_argument( "-p", type=int, default=17, metavar='power', dest='power', choices=[8, 11, 14, 17, 20], help= "Select transmit power from [%(choices)s] (default: %(default)s)") parser.add_argument("-r", type=float, default=4.8e3, metavar='bitrate', dest='bitrate', help="Set bitrate (default: %(default)s)") parser.add_argument( "-n", type=int, default=1, metavar='num_pkts', dest='num_pkts', help="Set number of packets to send (default: %(default)s)") args = parser.parse_args() frequency = args.frequency modulation = args.modulation power = args.power data_rate = args.bitrate num_pkts = args.num_pkts self.radio.startup() self._configure_radio(power, frequency, data_rate, modulation) for i in range(num_pkts): self._send_packet('stream_data') time.sleep(0.5) def shutdown(self): # print "\n\n\n\n" # print "% d packets received" %(self.pkts_received,) # print self.rx_packet_list # print "\n\n\n\n" # time.sleep(1) self.radio.shutdown()
class StandAloneRadioA(object): def __init__(self): self.kb = KnowledgeBase() self.lock = Lock() self.data = NodeAData() self.packet = Packet('A') self.radio = RadioAPI() self.tx_packet_number = 1 self.ack_packet_number = 0 self.goodput = 0 # self.data = [] # for i in range(50): # self.data.append(0xff) def _configure_radio(self, power, frequency, data_rate, modulation): """ Configure radio for operation. """ self.radio.configure_radio(power, frequency, data_rate, modulation) def _send_packet(self): """ Transmit packet. """ self.packet.set_flags_node_a() location = self.kb.get_state()['current_location'] data = self.data.pack_data() tx_packet = self.packet.make_packet(self.tx_packet_number, location, data) self.radio.transmit(tx_packet) def _receive_packet(self): """ Receive packet. """ rx_packet = self.radio.receive(rx_fifo_threshold=64, timeout=1.0) if rx_packet == []: # this occurs when timeout has been exceeded return else: packet_number, time_stamp, location, flags, data = self.packet.parse_packet(rx_packet) self.ack_packet_number, self.goodput = self.data.unpack_data(data) # print "packet_number=%d time_stamp=%f location=%d flags=0x%x" %(packet_number, time_stamp, # location, flags) print "goodput for acknowledged packet #%d = %f bits/second" %(self.ack_packet_number, self.goodput) def _listen(self): """ Listen before talk. """ status = self.radio.listen(rssi_threshold=100, timeout=1.0) if status == 'clear': print "channel clear" def _fsm(self): self._listen() time.sleep(0.01) self._send_packet() time.sleep(0.01) self._receive_packet() time.sleep(0.01) def run(self): """ Run the radio subsystem. """ self.radio.startup() default_radio_profile = location = self.kb.get_state()['default_radio_profile'] power = default_radio_profile['power'] frequency = default_radio_profile['frequency'] data_rate = default_radio_profile['data_rate'] modulation = default_radio_profile['modulation'] self._configure_radio(power, frequency, data_rate, modulation) # state = "listen" while True: self._fsm() self.lock.acquire() self.kb.sent_packets.append(self.tx_packet_number) self.kb.ack_packets.append((self.ack_packet_number, self.goodput)) self.lock.release() self.tx_packet_number += 1 def shutdown(self): self.kb.save_kb() self.radio.shutdown()
class StandAloneRadioB(object): def __init__(self): # self.kb = KnowledgeBase() self.radio = RadioAPI() self.packet = Packet('B') self.data = [] for i in range(50): self.data.append(0xff) self.tx_packet_number = 1 self.rssi_level = 0 def _configure_radio(self, power, frequency, data_rate, modulation): """ Configure radio for operation. """ self.radio.configure_radio(power, frequency, data_rate, modulation) def _send_packet(self): """ Transmit packet. """ self.packet.set_flags_node_b() # location = self.kb.get_state()['current_location'] location = 1 tx_packet = self.packet.make_packet(self.tx_packet_number, location, self.data) self.radio.transmit(tx_packet) self.tx_packet_number += 1 def _receive_packet(self): """ Receive packet. """ (self.rssi_level, rx_packet) = self.radio.receive(rx_fifo_threshold=63, timeout=None) if rx_packet == []: # this occurs when timeout has been exceeded return else: packet_number, time_stamp, location, flags, data = self.packet.parse_packet(rx_packet) print "packet_number=%d time_stamp=%f location=%d flags=0x%x" %(packet_number, time_stamp, location, flags) return packet_number, loc, flags, data def _listen(self): """ Listen before talk. """ status = self.radio.listen(rssi_threshold=100, timeout=1.0) if status == 'clear': print "channel clear" def run(self): """ Run the radio subsystem. """ parser = argparse.ArgumentParser() # parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("-f", type=float, default=434e6, metavar='frequency', dest='frequency', nargs=1, help="Transmit frequency (default: %(default)s)") parser.add_argument("-m", type=str, default='gfsk', metavar='modulation', dest='modulation', choices=['gfsk', 'fsk', 'ask'], help="Select modulation from [%(choices)s] (default: %(default)s)") parser.add_argument("-p" "--power", type=int, default=17, metavar='power', dest='power', choices=[8, 11, 14, 17], help="Select transmit power from [%(choices)s] (default: %(default)s)") parser.add_argument("-r" "--bitrate", type=float, default=4.8e3, metavar='bitrate', dest='bitrate', help="Set bitrate (default: %(default)s)") args = parser.parse_args() frequency = args.frequency modulation = args.modulation power = args.power data_rate = args.bitrate self.radio.startup() self._configure_radio(power, frequency, data_rate, modulation) time.sleep(1) # self.i = 0 self.rssi_list = [] # for i in range(100): # self.rssi_list.append(self.radio.get_rssi_dBm()) for i in range(100): # while True: # self.rssi_list.append(self.radio.get_rssi_dBm()) # self._receive_packet() pkt_num, loc, flags, data = self._receive_packet() print bin(flags) self.rssi_list.append(self.rssi_level) print "received packet" # self.i += 1 # print "RSSI (raw) : %f RSSI (dBm) : %f" %(rssi, RSSI) print self.rssi_list def shutdown(self): # print "\n\n\n" # print "%d packets received." %(self.i,) # print "\n\n\n" self.radio.shutdown()
class StandAloneRadioA(object): def __init__(self): self.radio = RadioAPI() self.packet = Packet('A') self.data = [] for i in range(50): self.data.append(0xff) self.tx_packet_number = 1 def _configure_radio(self, power, frequency, data_rate, modulation): """ Configure radio for operation. """ self.radio.configure_radio(power, frequency, data_rate, modulation) def _send_packet(self): """ Transmit packet. """ self.packet.set_flags_node_a() # location = self.kb.get_state()['current_location'] location = 1 tx_packet = self.packet.make_packet(self.tx_packet_number, location, self.data) self.radio.transmit(tx_packet) self.tx_packet_number += 1 def _receive_packet(self): """ Receive packet. """ rx_packet = self.radio.receive(rx_fifo_threshold=63, timeout=1.0) if rx_packet == []: # this occurs when timeout has been exceeded return else: packet_number, time_stamp, location, flags, data = self.packet.parse_packet( rx_packet) print "packet_number=%d time_stamp=%f location=%d flags=0x%x" % ( packet_number, time_stamp, location, flags) def _listen(self): """ Listen before talk. """ status = self.radio.listen(rssi_threshold=100, timeout=1.0) if status == 'clear': print "channel clear" def run(self): """ Run the radio subsystem. """ parser = argparse.ArgumentParser() parser.add_argument("-f", type=float, default=434e6, metavar='frequency', dest='frequency', help="Transmit frequency (default: %(default)s)") parser.add_argument( "-m", type=str, default='gfsk', metavar='modulation', dest='modulation', choices=['gfsk', 'fsk', 'ask', 'cw'], help="Select modulation from [%(choices)s] (default: %(default)s)") parser.add_argument( "-p", type=int, default=17, metavar='power', dest='power', choices=[8, 11, 14, 17], help= "Select transmit power from [%(choices)s] (default: %(default)s)") parser.add_argument("-r", type=float, default=4.8e3, metavar='bitrate', dest='bitrate', help="Set bitrate (default: %(default)s)") parser.add_argument( "-n", type=int, default=1000, metavar='num_packets', dest='num_packets', help="Number of packets to send (default: %(default)s)") args = parser.parse_args() frequency = args.frequency modulation = args.modulation power = args.power data_rate = args.bitrate num_packets = args.num_packets self.radio.startup() self.radio.configure_radio(power, frequency, data_rate, modulation) tic = time.time() i = 0 for i in range(num_packets): self._send_packet() print "packet sent" i += 1 toc = time.time() print "\n\n\n" print "%d packets sent." % (i, ) print "Total time (seconds) = %f" % (toc - tic, ) def shutdown(self): self.radio.shutdown()
class StandAloneRadioB(object): def __init__(self): self.kb = KnowledgeBase() self.radio = RadioAPI() self.packet = Packet('B') self.data = [] for i in range(50): self.data.append(0xff) self.tx_packet_number = 1 def _configure_radio(self, power, frequency, data_rate, modulation): """ Configure radio for operation. """ self.radio.configure_radio(power, frequency, data_rate, modulation) def _send_packet(self): """ Transmit packet. """ self.packet.set_flags_node_b() location = self.kb.get_state()['current_location'] tx_packet = self.packet.make_packet(self.tx_packet_number, location, self.data) self.radio.transmit(tx_packet) self.tx_packet_number += 1 def _receive_packet(self): """ Receive packet. """ rx_packet = self.radio.receive(rx_fifo_threshold=63, timeout=None) if rx_packet == []: # this occurs when timeout has been exceeded return else: packet_number, time_stamp, location, flags, data = self.packet.parse_packet( rx_packet) print "packet_number=%d time_stamp=%f location=%d flags=0x%x" % ( packet_number, time_stamp, location, flags) def _listen(self): """ Listen before talk. """ status = self.radio.listen(rssi_threshold=100, timeout=1.0) if status == 'clear': print "channel clear" def run(self): """ Run the radio subsystem. """ self.radio.startup() default_radio_profile = location = self.kb.get_state( )['default_radio_profile'] power = default_radio_profile['power'] frequency = default_radio_profile['frequency'] data_rate = default_radio_profile['data_rate'] modulation = default_radio_profile['modulation'] self._configure_radio(power, frequency, data_rate, modulation) state = "listen" while True: if state == "listen": self._listen() state = "receive" elif state == "receive": self._receive_packet() state = "send" elif state == "send": self._send_packet() state = "listen" else: print "+++ Melon melon melon +++" state = "listen" def shutdown(self): self.radio.shutdown()
class StandAloneRadioB(object): def __init__(self): # self.kb = KnowledgeBase() self.radio = RadioAPI() self.packet = Packet('B') self.data = [] for i in range(50): self.data.append(0xff) self.tx_packet_number = 1 self.rssi_level = 0 def _configure_radio(self, power, frequency, data_rate, modulation): """ Configure radio for operation. """ self.radio.configure_radio(power, frequency, data_rate, modulation) def _send_packet(self): """ Transmit packet. """ self.packet.set_flags_node_b() # location = self.kb.get_state()['current_location'] location = 1 tx_packet = self.packet.make_packet(self.tx_packet_number, location, self.data) self.radio.transmit(tx_packet) self.tx_packet_number += 1 def _receive_packet(self): """ Receive packet. """ (self.rssi_level, rx_packet) = self.radio.receive(rx_fifo_threshold=63, timeout=None) if rx_packet == []: # this occurs when timeout has been exceeded return else: packet_number, time_stamp, location, flags, data = self.packet.parse_packet( rx_packet) print "packet_number=%d time_stamp=%f location=%d flags=0x%x" % ( packet_number, time_stamp, location, flags) def _listen(self): """ Listen before talk. """ status = self.radio.listen(rssi_threshold=100, timeout=1.0) if status == 'clear': print "channel clear" def run(self): """ Run the radio subsystem. """ parser = argparse.ArgumentParser() # parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("-f", type=float, default=434e6, metavar='frequency', dest='frequency', nargs=1, help="Transmit frequency (default: %(default)s)") parser.add_argument( "-m", type=str, default='gfsk', metavar='modulation', dest='modulation', choices=['gfsk', 'fsk', 'ask'], help="Select modulation from [%(choices)s] (default: %(default)s)") parser.add_argument( "-p" "--power", type=int, default=17, metavar='power', dest='power', choices=[8, 11, 14, 17], help= "Select transmit power from [%(choices)s] (default: %(default)s)") parser.add_argument("-r" "--bitrate", type=float, default=4.8e3, metavar='bitrate', dest='bitrate', help="Set bitrate (default: %(default)s)") args = parser.parse_args() frequency = args.frequency modulation = args.modulation power = args.power data_rate = args.bitrate self.radio.startup() self._configure_radio(power, frequency, data_rate, modulation) time.sleep(1) # self.i = 0 self.rssi_list = [] # for i in range(100): # self.rssi_list.append(self.radio.get_rssi_dBm()) for i in range(100): # while True: # self.rssi_list.append(self.radio.get_rssi_dBm()) self._receive_packet() self.rssi_list.append(self.rssi_level) print "received packet" # self.i += 1 # print "RSSI (raw) : %f RSSI (dBm) : %f" %(rssi, RSSI) print self.rssi_list def shutdown(self): # print "\n\n\n" # print "%d packets received." %(self.i,) # print "\n\n\n" self.radio.shutdown()
class StandAloneRadioB(object): def __init__(self): self.kb = KnowledgeBase() self.data = NodeBData() self.packet = Packet('B') self.radio = RadioAPI() self.tx_packet_number = 1 self.rx_packet_list = [] def _configure_radio(self, power, frequency, data_rate, modulation): """ Configure radio for operation. """ self.radio.configure_radio(power, frequency, data_rate, modulation) def _send_packet(self): """ Transmit packet. """ self.packet.set_flags_node_b(ack_packet=True) location = self.kb.get_state()['current_location'] payload = self.data.pack_data(self.rx_packet_number, self.goodput) tx_packet = self.packet.make_packet(self.tx_packet_number, location, payload) self.tx_packet_number += 1 # print "processing latency = %f" %(time.time()-self.tic,) self.radio.transmit(tx_packet) # print "reply transmitted" def _receive_packet(self): """ Receive packet. """ rx_packet = self.radio.receive(rx_fifo_threshold=64, timeout=None) # self.tic = time.time() if rx_packet == []: # this occurs when timeout has been exceeded return else: self.rx_packet_number, time_stamp, location, flags, data = self.packet.parse_packet( rx_packet) # print "packet_number=%d time_stamp=%f location=%d flags=0x%x" %(packet_number, time_stamp, # location, flags) del_time = time.time() - time_stamp self.goodput = 50 * 8 / del_time print "goodput for packet #%d = %f bits/second" % ( self.rx_packet_number, self.goodput) self.rx_packet_list.append(self.rx_packet_number) def _listen(self): """ Listen before talk. """ status = self.radio.listen(rssi_threshold=100, timeout=1.0) if status == 'clear': print "channel clear" def _fsm(self): self._listen() time.sleep(0.01) self._receive_packet() time.sleep(0.3) # to account for tx->rx processing latency in node_a self._send_packet() time.sleep(0.01) def run(self): """ Run the radio subsystem. """ self.radio.startup() default_radio_profile = location = self.kb.get_state( )['default_radio_profile'] power = default_radio_profile['power'] frequency = default_radio_profile['frequency'] data_rate = default_radio_profile['data_rate'] modulation = default_radio_profile['modulation'] self._configure_radio(power, frequency, data_rate, modulation) state = "listen" while True: self._fsm() # if state == "listen": # self._listen() # state = "receive" # time.sleep(0.1) # elif state == "receive": # self._receive_packet() # state = "send" # time.sleep(0.1) # elif state == "send": # self._send_packet() # state = "listen" # time.sleep(0.1) # else: # print "+++ Melon melon melon +++" # state = "listen" def shutdown(self): print "\n\n\n" print "number of received packets = %d" % (len(self.rx_packet_list), ) print "\n\n\n" self.radio.shutdown()
class NodeA(object): """ Node A radio. """ def __init__(self): self.data = NodeAData() self.packet = Packet('A') self.radio = RadioAPI() self.tx_packet_number = 1 def run(self): """ Run Node A. """ parser = argparse.ArgumentParser() parser.add_argument("-f", type=float, default=434e6, metavar='frequency', dest='frequency', nargs=1, help="Transmit frequency (default: %(default)s)") parser.add_argument( "-m", type=str, default='gfsk', metavar='modulation', dest='modulation', choices=['gfsk', 'fsk', 'ask'], help="Select modulation from [%(choices)s] (default: %(default)s)") parser.add_argument( "-p" "--power", type=int, default=17, metavar='power', dest='power', choices=[8, 11, 14, 17], help= "Select transmit power from [%(choices)s] (default: %(default)s)") parser.add_argument("-r" "--bitrate", type=float, default=4.8e3, metavar='bitrate', dest='bitrate', help="Set bitrate (default: %(default)s)") args = parser.parse_args() self.frequency = args.frequency modulation = args.modulation power = args.power data_rate = args.bitrate self.radio.startup() self.radio.configure_radio(power, self.frequency, data_rate, modulation) self.fsm() def _listen(self): status = self.radio.listen(rssi_threshold=100, timeout=0.1) if status == 'clear': pass # print "channel clear" def _receive_packet(self): """ Receive packet """ rx_packet = self.radio.receive(rx_fifo_threshold=64, timeout=None) pkt_num, t, loc, flags, data = self.packet.parse_packet(rx_packet) return pkt_num, loc, flags, data def _send_packet(self, mode, mod=None, eirp=None, bitrate=None): """ Transmit data. Parameters ---------- mode : str Mode of operation. Used to create appropriate packet header and payload. {`send_reconfig_command` | `request_data` | `stream_data`}. mod : str, opt Modulation, one of {`fsk` | `gfsk` | `ook` }. eirp : int, opt Transmit power, one of { 8 | 11 | 14 | 17 }. bitrate : float, opt Radio bitrate, one of {...} """ location = 1 if mode == 'stream_data': print "streaming data packets" self.packet.set_flags_node_a(send_stream=True) payload = self.data.pack_data(mode) elif mode == 'send_reconfig_command': self.packet.set_flags_node_a(send_command=True) payload = self.data.pack_data(mode, mod, eirp, bitrate) elif mode == 'request_data': self.packet.set_flags_node_a(request_data=True) payload = self.data.pack_data(mode) else: print 'error in _send_packet, no mode specified' raise ValueError tx_packet = self.packet.make_packet(self.tx_packet_number, location, payload) self.tx_packet_number += 1 self.radio.transmit(tx_packet) print "packet sent" def fsm(self): """ Node A finite state machine. """ index = 1 # while True: # self._send_packet('stream_data') # index += 1 while True: if index % 20 == 0: print "requesting data" self._send_packet('request_data') index += 1 pkt_num, loc, flags, data = self._receive_packet() packets_received = self.data.unpack_data(data) print "packets received by Node B", packets_received time.sleep(1) elif index == 35: print "changing waveform" mod = 'gfsk' eirp = 11 bitrate = 57.6e3 self._send_packet('send_reconfig_command', mod, eirp, bitrate) index += 1 pkt_num, loc, flags, data = self._receive_packet() if (flags & 0x04) == 0x04: print "node B acknowledged request, changing to new waveform" time.sleep(1) self.radio.configure_radio(eirp, self.frequency, bitrate, mod) self._send_packet('stream_data') index += 1 continue else: print "node B did not acknowledge request, continuing with current waveform" continue else: self._send_packet('stream_data') index += 1
class StandAloneRadioA(object): def __init__(self): self.kb = KnowledgeBase() self.lock = Lock() self.data = NodeAData() self.packet = Packet('A') self.radio = RadioAPI() self.tx_packet_number = 1 self.ack_packet_number = 0 self.goodput = 0 # self.data = [] # for i in range(50): # self.data.append(0xff) def _configure_radio(self, power, frequency, data_rate, modulation): """ Configure radio for operation. """ self.radio.configure_radio(power, frequency, data_rate, modulation) def _send_packet(self): """ Transmit packet. """ self.packet.set_flags_node_a() location = self.kb.get_state()['current_location'] data = self.data.pack_data() tx_packet = self.packet.make_packet(self.tx_packet_number, location, data) self.radio.transmit(tx_packet) def _receive_packet(self): """ Receive packet. """ rx_packet = self.radio.receive(rx_fifo_threshold=64, timeout=1.0) if rx_packet == []: # this occurs when timeout has been exceeded return else: packet_number, time_stamp, location, flags, data = self.packet.parse_packet( rx_packet) self.ack_packet_number, self.goodput = self.data.unpack_data(data) # print "packet_number=%d time_stamp=%f location=%d flags=0x%x" %(packet_number, time_stamp, # location, flags) print "goodput for acknowledged packet #%d = %f bits/second" % ( self.ack_packet_number, self.goodput) def _listen(self): """ Listen before talk. """ status = self.radio.listen(rssi_threshold=100, timeout=1.0) if status == 'clear': print "channel clear" def _fsm(self): self._listen() time.sleep(0.01) self._send_packet() time.sleep(0.01) self._receive_packet() time.sleep(0.01) def run(self): """ Run the radio subsystem. """ self.radio.startup() default_radio_profile = location = self.kb.get_state( )['default_radio_profile'] power = default_radio_profile['power'] frequency = default_radio_profile['frequency'] data_rate = default_radio_profile['data_rate'] modulation = default_radio_profile['modulation'] self._configure_radio(power, frequency, data_rate, modulation) # state = "listen" while True: self._fsm() self.lock.acquire() self.kb.sent_packets.append(self.tx_packet_number) self.kb.ack_packets.append((self.ack_packet_number, self.goodput)) self.lock.release() self.tx_packet_number += 1 def shutdown(self): self.kb.save_kb() self.radio.shutdown()
class StandAloneRadioB(object): def __init__(self): self.kb = KnowledgeBase() self.radio = RadioAPI() self.packet = Packet('B') self.data = [] for i in range(50): self.data.append(0xff) self.tx_packet_number = 1 def _configure_radio(self, power, frequency, data_rate, modulation): """ Configure radio for operation. """ self.radio.configure_radio(power, frequency, data_rate, modulation) def _send_packet(self): """ Transmit packet. """ self.packet.set_flags_node_b() location = self.kb.get_state()['current_location'] tx_packet = self.packet.make_packet(self.tx_packet_number, location, self.data) self.radio.transmit(tx_packet) self.tx_packet_number += 1 def _receive_packet(self): """ Receive packet. """ rx_packet = self.radio.receive(rx_fifo_threshold=63, timeout=None) if rx_packet == []: # this occurs when timeout has been exceeded return else: packet_number, time_stamp, location, flags, data = self.packet.parse_packet(rx_packet) print "packet_number=%d time_stamp=%f location=%d flags=0x%x" %(packet_number, time_stamp, location, flags) def _listen(self): """ Listen before talk. """ status = self.radio.listen(rssi_threshold=100, timeout=1.0) if status == 'clear': print "channel clear" def run(self): """ Run the radio subsystem. """ self.radio.startup() default_radio_profile = location = self.kb.get_state()['default_radio_profile'] power = default_radio_profile['power'] frequency = default_radio_profile['frequency'] data_rate = default_radio_profile['data_rate'] modulation = default_radio_profile['modulation'] self._configure_radio(power, frequency, data_rate, modulation) state = "listen" while True: if state == "listen": self._listen() state = "receive" elif state == "receive": self._receive_packet() state = "send" elif state == "send": self._send_packet() state = "listen" else: print "+++ Melon melon melon +++" state = "listen" def shutdown(self): self.radio.shutdown()
class StandAloneRadioA(object): def __init__(self): self.radio = RadioAPI() self.packet = Packet('A') self.data = [] for i in range(50): self.data.append(0xff) self.tx_packet_number = 1 def _configure_radio(self, power, frequency, data_rate, modulation): """ Configure radio for operation. """ self.radio.configure_radio(power, frequency, data_rate, modulation) def _send_packet(self): """ Transmit packet. """ self.packet.set_flags_node_a() # location = self.kb.get_state()['current_location'] location = 1 tx_packet = self.packet.make_packet(self.tx_packet_number, location, self.data) self.radio.transmit(tx_packet) self.tx_packet_number += 1 def _receive_packet(self): """ Receive packet. """ rx_packet = self.radio.receive(rx_fifo_threshold=63, timeout=1.0) if rx_packet == []: # this occurs when timeout has been exceeded return else: packet_number, time_stamp, location, flags, data = self.packet.parse_packet(rx_packet) print "packet_number=%d time_stamp=%f location=%d flags=0x%x" %(packet_number, time_stamp, location, flags) def _listen(self): """ Listen before talk. """ status = self.radio.listen(rssi_threshold=100, timeout=1.0) if status == 'clear': print "channel clear" def run(self): """ Run the radio subsystem. """ parser = argparse.ArgumentParser() # parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) # parser.add_argument("-f", type=float, default=434e6, metavar='frequency', dest='frequency', # nargs=1, help="Transmit frequency (default: %(default)s)") parser.add_argument("-f", type=float, default=434e6, metavar='frequency', dest='frequency', help="Transmit frequency (default: %(default)s)") parser.add_argument("-m", type=str, default='gfsk', metavar='modulation', dest='modulation', choices=['gfsk', 'fsk', 'ask', 'cw'], help="Select modulation from [%(choices)s] (default: %(default)s)") parser.add_argument("-p" "--power", type=int, default=17, metavar='power', dest='power', choices=[1, 2, 5, 8, 11, 14, 17, 20], help="Select transmit power from [%(choices)s] (default: %(default)s)") parser.add_argument("-r" "--bitrate", type=float, default=4.8e3, metavar='bitrate', dest='bitrate', help="Set bitrate (default: %(default)s)") parser.add_argument("-s", type=bool, default=False, metavar='run_sweep', dest='run_sweep', help="Set sweep mode (default: %(default)s)") parser.add_argument("-i", type=bool, default=False, metavar='interactive', dest='interactive', help="Use interactive sweep (default: %(default)s)") args = parser.parse_args() self.radio.startup() run_sweep = args.run_sweep interactive = args.interactive frequency = args.frequency modulation = args.modulation power = args.power data_rate = args.bitrate if run_sweep == False: self._configure_radio(power, frequency, data_rate, modulation) state = "listen" while True: self._send_packet() else: f = 295e6 freqs = [] while f < 930e6: freqs.append(f) f += 1e6 for i in freqs: if modulation == 'cw': self._configure_radio(power, i, data_rate, modulation) print "changing to freq: ", i self._send_packet() if interactive == True: s = raw_input('Press enter for next frequency') self.radio.set_ready_mode() else: time.sleep(0.1) else: self._configure_radio(power, i, data_rate, modulation) print "changing to freq: ", i for j in range(2): self._send_packet() # fc, hbsel, fb = freq_utils.carrier_freq(i) # for j in range(10): # self.setup_rf(fc, hbsel, fb) # self.tx_data() # print "transmitted packet" # time.sleep(0.01) # pass # def sweep(self): def shutdown(self): self.radio.shutdown()
class Avoider(object): def __init__(self): self.radio = RadioAPI() self.packet = Packet('A') self.data = [] for i in range(50): self.data.append(0xff) self.tx_packet_number = 1 def _configure_radio(self, power, frequency, data_rate, modulation): """ Configure radio for operation. """ self.radio.configure_radio(power, frequency, data_rate, modulation) def _send_packet(self): """ Transmit packet. """ self.packet.set_flags_node_a() # location = self.kb.get_state()['current_location'] location = 1 tx_packet = self.packet.make_packet(self.tx_packet_number, location, self.data) self.radio.transmit(tx_packet) self.tx_packet_number += 1 def _receive_packet(self): """ Receive packet. """ rx_packet = self.radio.receive(rx_fifo_threshold=63, timeout=1.0) if rx_packet == []: # this occurs when timeout has been exceeded return else: packet_number, time_stamp, location, flags, data = self.packet.parse_packet(rx_packet) print "packet_number=%d time_stamp=%f location=%d flags=0x%x" %(packet_number, time_stamp, location, flags) def _listen(self, threshold): """ Listen before talk. """ status = self.radio.listen(threshold, timeout=1.0) # if status == 'clear': # print "channel clear" return status def run(self): """ Run the radio subsystem. """ parser = argparse.ArgumentParser() parser.add_argument("-f", type=float, default=432e6, metavar='frequency', dest='frequency', help="Transmit frequency (default: %(default)s)") parser.add_argument("-m", type=str, default='gfsk', metavar='modulation', dest='modulation', choices=['gfsk', 'fsk', 'ask', 'cw'], help="Select modulation from [%(choices)s] (default: %(default)s)") parser.add_argument("-p", type=int, default=17, metavar='power', dest='power', choices=[1, 2, 5, 8, 11, 14, 17, 20], help="Select transmit power from [%(choices)s] (default: %(default)s)") parser.add_argument("-r", type=float, default=4.8e3, metavar='bitrate', dest='bitrate', help="Set bitrate (default: %(default)s)") parser.add_argument("-t", type=int, default=150, metavar='threshold', dest='threshold', help="Set rssi threshold (default: %(default)s)") args = parser.parse_args() frequency = args.frequency modulation = args.modulation power = args.power data_rate = args.bitrate threshold = args.threshold self.radio.startup() self.radio.configure_radio(power, frequency, data_rate, modulation) hop = False while True: status = self._listen(threshold) if status == 'clear': if hop == True: self.toc = time.time() print "arrived at final destination, time to find new channel: ", self.toc - self.tic hop = False self._send_packet() else: if hop == False: print "reconfiguring radio, starting timer" hop = True self.tic = time.time() frequency += 2e6 print "changing to new center frequency: %f" %(frequency,) self.radio.configure_radio(power, frequency, data_rate, modulation) def shutdown(self): self.radio.shutdown()