From 430b7f52a59d064137d05794c8a49eedc89bbfc9 Mon Sep 17 00:00:00 2001 From: Ertan Onur Date: Thu, 2 Jun 2022 11:33:42 +0300 Subject: [PATCH] bladerf gains corrected --- .../ApplicationLayer/MessageSegmentation.py | 6 +- .../OpenCVVideoStreamingApp.py | 13 +- .../PingPongApplicationLayer.py | 4 +- .../PhysicalLayer/BladeRFOfdmFlexFramePhy.py | 17 +-- .../Networking/PhysicalLayer/BladeRFUtils.py | 103 +++++++++----- .../PhysicalLayer/FrameHandlerBase.py | 6 +- testBladeRF.py | 21 +-- testVideoStreamingBladeRF.py | 115 ++++++++++++++++ testVideoStreamingTwoBladeRFs.py | 126 ++++++++++++++++++ 9 files changed, 347 insertions(+), 64 deletions(-) create mode 100644 testVideoStreamingBladeRF.py create mode 100644 testVideoStreamingTwoBladeRFs.py diff --git a/adhoccomputing/Networking/ApplicationLayer/MessageSegmentation.py b/adhoccomputing/Networking/ApplicationLayer/MessageSegmentation.py index 60e6148..3075d5a 100644 --- a/adhoccomputing/Networking/ApplicationLayer/MessageSegmentation.py +++ b/adhoccomputing/Networking/ApplicationLayer/MessageSegmentation.py @@ -29,7 +29,7 @@ class MessageSegmentationPayload(GenericMessagePayload): class MessageSegmentation(GenericModel): - MSS = 40000 + MSS = 4000 recvmsgs = {} def __init__(self, componentname, componentinstancenumber, context=None, configurationparameters=None, num_worker_threads=1, topology=None, child_conn=None, node_queues=None, channel_queues=None): super().__init__(componentname, componentinstancenumber, context, configurationparameters, num_worker_threads, topology, child_conn, node_queues, channel_queues) @@ -45,8 +45,8 @@ def on_message_from_top(self, eventobj: Event): #self.mutex.acquire(1) try: - #fragmentid = secrets.token_bytes(4) - fragmentid = "DENE" + fragmentid = secrets.token_bytes(4) + #fragmentid = "DENE" msg = eventobj.eventcontent hdr = msg.header msgpickled = pickle.dumps(msg) diff --git a/adhoccomputing/Networking/ApplicationLayer/OpenCVVideoStreamingApp.py b/adhoccomputing/Networking/ApplicationLayer/OpenCVVideoStreamingApp.py index 3d85a91..109b24b 100644 --- a/adhoccomputing/Networking/ApplicationLayer/OpenCVVideoStreamingApp.py +++ b/adhoccomputing/Networking/ApplicationLayer/OpenCVVideoStreamingApp.py @@ -26,8 +26,8 @@ class OpenCVVideoStreamingApp(GenericModel): CV2Timer = 1 #frame=None framerate = 20 - frameheight = 40 - framewidth = 30 + frameheight = 80 + framewidth = 60 def on_init(self, eventobj: Event): self.counter = 0 @@ -46,7 +46,7 @@ def __init__(self, componentname, componentinstancenumber, context=None, configu self.cap = cv2.VideoCapture(0) #self.codec = 0x47504A4D # MJPG #self.cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M','J','P','G')) - self.cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('H','2','6','4')) + self.cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('H','2','6','5')) #self.cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('a','v','c','1')) #self.cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'FMP4')) #self.cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPEG')) @@ -63,7 +63,7 @@ def __init__(self, componentname, componentinstancenumber, context=None, configu #if self.componentinstancenumber == 1: ret, framehighres = self.cap.read() framesmallres = cv2.resize(framehighres, (self.frameheight,self.framewidth)) - self.frame = cv2.cvtColor(framesmallres, cv2.COLOR_BGR2GRAY) + self.frame = cv2.cvtColor(framesmallres, cv2.COLOR_BGRA2YUV_I420) def on_message_from_top(self, eventobj: Event): logger.applog(f"{self.componentname}.{self.componentinstancenumber} RECEIVED {str(eventobj)}") @@ -90,13 +90,14 @@ def send_frame(self): ret, framehighres = self.cap.read() try: framesmallres = cv2.resize(framehighres, (self.frameheight,self.framewidth)) - frame = cv2.cvtColor(framesmallres, cv2.COLOR_BGR2GRAY) + frame = framesmallres #cv2.cvtColor(framesmallres, cv2.COLOR_BGR2GRAY) + #(B,G,R) = cv2.split(frame) payload = pickle.dumps(frame) if self.initframe == True: self.frame = frame ##### LOOPBACK trials self.initframe = False #payload = frame.tobytes() - #logger.applog(f"{self.componentname}-{self.componentinstancenumber}: Payload length {len(payload)}") + logger.applog(f"{self.componentname}-{self.componentinstancenumber}: Payload length {len(payload)}") broadcastmessage = GenericMessage(hdr, payload) evt = Event(self, EventTypes.MFRT, broadcastmessage) #logger.applog(f"{self.componentname}.{self.componentinstancenumber} WILL SEND frame of length {len(payload)}") diff --git a/adhoccomputing/Networking/ApplicationLayer/PingPongApplicationLayer.py b/adhoccomputing/Networking/ApplicationLayer/PingPongApplicationLayer.py index 8c85adc..2faa158 100644 --- a/adhoccomputing/Networking/ApplicationLayer/PingPongApplicationLayer.py +++ b/adhoccomputing/Networking/ApplicationLayer/PingPongApplicationLayer.py @@ -40,7 +40,7 @@ def on_message_from_bottom(self, eventobj: Event): evt.eventcontent.payload = eventobj.eventcontent.payload + "-" + str(self.componentinstancenumber) evt.eventcontent.header.sequencenumber = eventobj.eventcontent.header.sequencenumber evt.eventcontent.header.counter = eventobj.eventcontent.header.counter + 1 - #time.sleep(0.1) # TODO WHAT Should this be? + time.sleep(0.001) # TODO WHAT Should this be? self.send_down(evt) # PINGPONG def on_startbroadcast(self, eventobj: Event): @@ -48,7 +48,7 @@ def on_startbroadcast(self, eventobj: Event): self.counter = self.counter + 1 hdr.sequencenumber = self.counter hdr.counter = 1 - payload = "BMSG-"*10000 + str(self.counter) + ": " + str(self.componentinstancenumber) + payload = "BMSG-" + str(self.counter) + ": " + str(self.componentinstancenumber) broadcastmessage = GenericMessage(hdr, payload) #print(f"Payload length {len(payload)}") evt = Event(self, EventTypes.MFRT, broadcastmessage) diff --git a/adhoccomputing/Networking/PhysicalLayer/BladeRFOfdmFlexFramePhy.py b/adhoccomputing/Networking/PhysicalLayer/BladeRFOfdmFlexFramePhy.py index 991e5f0..1d1eada 100644 --- a/adhoccomputing/Networking/PhysicalLayer/BladeRFOfdmFlexFramePhy.py +++ b/adhoccomputing/Networking/PhysicalLayer/BladeRFOfdmFlexFramePhy.py @@ -5,6 +5,7 @@ from ...Generics import * from .LiquidDspUtils import * import numpy as np +import zlib mutex = Lock() @@ -15,18 +16,18 @@ def ofdm_callback(header:POINTER(c_ubyte), header_valid:c_int, payload:POINTER(c #mutex.acquire(1) try: framer = framers.get_framer_by_id(userdata) - logger.debug(f"Node {framer.componentinstancenumber} RSSI {stats.rssi} {framer.sdrdev.rssi}") + #logger.applog(f"Node {framer.componentinstancenumber} RSSI {stats.rssi} {framer.sdrdev.rssi} {payload_valid}") if payload_valid != 0: #ofdmflexframesync_print(framer.fs) pload = string_at(payload, payload_len) - phymsg = pickle.loads(pload) + phymsg = pickle.loads(zlib.decompress(pload)) msg = GenericMessage(phymsg.header, phymsg.payload) framer.send_self(Event(framer, PhyEventTypes.RECV, msg)) - logger.info(f"Header= {msg.header.messagetype} Payload= {msg.payload} RSSI= {stats.rssi}") + #logger.applog(f"Message= {str(msg)} RSSI= {stats.rssi}") except Exception as ex: logger.critical(f"Exception_ofdm_callback: {ex}") #mutex.release() - #ofdmflexframesync_reset(framer.fs) + ofdmflexframesync_reset(framer.fs) return 0 @@ -59,12 +60,12 @@ def configure(self): self.fgprops.fec0 = LIQUID_FEC_NONE self.fgprops.fec1 = LIQUID_FEC_HAMMING74 self.fgprops.mod_scheme = LIQUID_MODEM_QPSK - self.M = 256 - self.cp_len = 64 - self.taper_len = 64 + self.M = 1024 + self.cp_len = 8 + self.taper_len = 8 self.fg = ofdmflexframegen_create(self.M, self.cp_len, self.taper_len, None, byref(self.fgprops)) - self.fgbuffer_len = 1024 #self.M + self.cp_len + self.fgbuffer_len = 8192 #1024 #(self.M + self.cp_len + self.taper_len)*8 self.fgbuffer = np.zeros(self.fgbuffer_len*self.sdrdev.bytes_per_sample//sizeof(c_int16), dtype=np.int16) # sc16q1 samples #res = ofdmflexframegen_print(self.fg) diff --git a/adhoccomputing/Networking/PhysicalLayer/BladeRFUtils.py b/adhoccomputing/Networking/PhysicalLayer/BladeRFUtils.py index e986119..477b700 100644 --- a/adhoccomputing/Networking/PhysicalLayer/BladeRFUtils.py +++ b/adhoccomputing/Networking/PhysicalLayer/BladeRFUtils.py @@ -3,6 +3,7 @@ import threading import time from bladerf import _bladerf, _tool +from bladerf._bladerf import libbladeRF from ...Generics import * from threading import Thread, Lock from .SDRUtils import SDRUtils @@ -10,6 +11,7 @@ from ...Generics import * from .LiquidDspUtils import * import math +from ...Networking.PhysicalLayer.FrameHandlerBase import PhyEventTypes, PhyFrame class BladeRFUtils(SDRUtils): @@ -135,7 +137,7 @@ def load_fpga(self, device, image ): logger.debug(f"FPGA successfully loaded. Version: {str(fpga_version)}" ) except _bladerf.BladeRFError: - logger.criticial(f"Error loading FPGA image: {str(image )}" ) + logger.critical(f"Error loading FPGA image: {str(image )}" ) return 0 @@ -153,25 +155,22 @@ def configure_tx_channel(self): return -1 # Configure bladeRF + - - if self.componentinstancenumber == 0: - deltafreq = 1000000 - else: - deltafreq = 0 self.bladerfdevice_tx_ch.frequency = self.tx_freq #+ deltafreq - self.bladerfdevice_tx_ch.sample_rate = self.bandwidth #40000000 #self.tx_rate + self.bladerfdevice_tx_ch.sample_rate = self.bandwidth #40000000 #self.tx_rate + #self.bladerfdevice_tx_ch.bandwidth = self.bandwidth self.bladerfdevice_tx_ch.gain = self.tx_gain - self.bladerfdevice_tx_ch.bandwidth = self.bandwidth + #self.bladerfdevice_tx_ch.gain_mode = _bladerf.GainMode.Manual #self.bladerfdevice.set_gain_mode ( self.bladerfdevice_tx_ch , _bladerf.GainMode.Manual) # Setup stream self.bladerfdevice.sync_config(layout=_bladerf.ChannelLayout.TX_X1, fmt=_bladerf.Format.SC16_Q11, - num_buffers=16, - buffer_size=8192*2, - num_transfers=8, - stream_timeout=500) + num_buffers = 256, + buffer_size = 8192, + num_transfers = 32, + stream_timeout=100) # Enable module logger.debug( "TX: Start" ) @@ -194,24 +193,20 @@ def configure_rx_channel(self): # Configure BladeRF - - if self.componentinstancenumber == 0: - deltafreq = 0 - else: - deltafreq = 1000000 + #self.bladerfdevice_rx_ch .enable = True self.bladerfdevice_rx_ch.frequency = self.rx_freq #+ deltafreq - self.bladerfdevice_rx_ch.sample_rate = self.bandwidth #40000000 #self.rx_rate + self.bladerfdevice_rx_ch.sample_rate = self.bandwidth #40000000 #self.rx_rate + #self.bladerfdevice_rx_ch.bandwidth = self.bandwidth self.bladerfdevice_rx_ch.gain = self.rx_gain - self.bladerfdevice_rx_ch.bandwidth = self.bandwidth - #self.bladerfdevice_rx_ch.gain_mode = _bladerf.GainMode.Manual + self.bladerfdevice_rx_ch.gain_mode = _bladerf.GainMode.Manual #self.bladerfdevice.set_gain_mode ( self.bladerfdevice_rx_ch , _bladerf.GainMode.Manual) # Setup synchronous stream self.bladerfdevice.sync_config(layout = _bladerf.ChannelLayout.RX_X1, fmt = _bladerf.Format.SC16_Q11, - num_buffers = 16, - buffer_size = 8192*2, - num_transfers = 8, - stream_timeout = 500) + num_buffers = 256, + buffer_size = 8192, + num_transfers = 32, + stream_timeout = 100) # Enable module @@ -244,6 +239,9 @@ def configureSdr(self, type="x115", sdrconfig=None): self.board_name = self.bladerfdevice.board_name self.fpga_size = self.bladerfdevice.fpga_size + #self.bladerfdevice.device_reset() + #self.bladerfdevice.set_tuning_mode(1) + logger.debug(f"Loading FPGA on {self.devicename} at {self.fpgalocation}" ) try: @@ -270,13 +268,14 @@ def configureSdr(self, type="x115", sdrconfig=None): self.tx_gain = int(self.sdrconfig.hw_tx_gain) self.rx_gain = int(self.sdrconfig.hw_rx_gain) self.bandwidth = self.sdrconfig.bandwidth - + + #self.bladerfdevice.set_frequency(0, self.rx_freq) + self.bladerfdevice_rx_ch = self.bladerfdevice.Channel(self.rx_ch) self.bladerfdevice_tx_ch = self.bladerfdevice.Channel(self.tx_ch) #self.bladerfdevice.set_gain_mode(tx_chan, _bladerf.GainMode.FastAttack_AGC) #self.bladerfdevice.set_gain_mode(rx_chan, _bladerf.GainMode.FastAttack_AGC) - - + #TODO FOR TESTING #lb = _bladerf.Loopback.BB_TXVGA1_RXLPF @@ -289,6 +288,38 @@ def configureSdr(self, type="x115", sdrconfig=None): self.configure_rx_channel() self.configure_tx_channel() + + #timestamp: libbladeRF.bladerf_timestamp = 0 + #bqt = _bladerf.ffi.new("struct bladerf_quick_tune *") + #libbladeRF.bladerf_schedule_retune(self.bladerfdevice.dev[0], 0, timestamp, int(self.rx_freq), bqt) + #print(bqt.freqsel, bqt.vcocap, bqt.nint, bqt.nfrac, bqt.flags) + #libbladeRF.bladerf_schedule_retune(self.bladerfdevice.dev[0], 1, timestamp, int(self.rx_freq), bqt) + #print(bqt.freqsel, bqt.vcocap, bqt.nint, bqt.nfrac, bqt.flags) + +# RX Gain +# Overall: 5 to 66 dB +# LNA: 0 to 6 dB (step of 3 dB) +# VGA1: 5 to 30 dB (step of 1 dB) +# VGA2: 0 to 30 dB (step of 1 dB) +# Stage names: lna, rxvga1, rxvga2 + + libbladeRF.bladerf_log_set_verbosity(3) + + libbladeRF.bladerf_set_lna_gain(self.bladerfdevice.dev[0], 3) + libbladeRF.bladerf_set_rxvga1(self.bladerfdevice.dev[0], 15) + libbladeRF.bladerf_set_rxvga2(self.bladerfdevice.dev[0], 15) +# TX Gain +# Overall: -35 to 21 dB +# VGA1: -35 to -4 dB (step of 1 dB) +# VGA2: 0 to 25 dB (step of 1 dB) +# Stage names: txvga1, txvga2 +# Frequency: 237500000 to 3800000000 Hz +# Bandwidth: 1500000 to 28000000 Hz +# Sample Rate: 80000 to 40000000 Hz (recommended max) + + libbladeRF.bladerf_set_txvga1(self.bladerfdevice.dev[0], -4) + libbladeRF.bladerf_set_txvga2(self.bladerfdevice.dev[0], 10) + logger.info(f"\n===> BLADERF {self.bladerfdevice.get_serial()} CONFIG" + f"\n===> TX_CHAN {self.tx_ch}" + @@ -328,18 +359,26 @@ def start_rx(self, rx_callback, framer): def rx_thread(self): + cnt = 1 + num_samples = self.framer.fgbuffer_len - num_samples = 1024 - buf = bytearray(num_samples*self.bytes_per_sample) num_samples_read = 0 while(self.receiveenabled == True): + cnt += 1 #self.mutex.acquire(1) try: + buf = bytearray(num_samples*self.bytes_per_sample) self.bladerfdevice.sync_rx(buf, num_samples) mybuf2 = np.frombuffer(buf, dtype=np.int16).flatten (order="C") #// int(self.sdrconfig.sw_tx_gain) - self.rx_callback( num_samples, mybuf2) - if num_samples*2 > self.samps_per_est: - self.computeRSSI( self.samps_per_est*2, mybuf2[:self.samps_per_est*2],type="sc16") + #self.rx_callback( num_samples, mybuf2) + if num_samples > 0: + frm = PhyFrame(num_samples, mybuf2) + self.framer.frame_in_queue.put(Event(None, PhyEventTypes.RECV, frm)) + if cnt > 10: + cnt = 1 + if num_samples*2 > self.samps_per_est: + self.computeRSSI( self.samps_per_est*2, mybuf2[:self.samps_per_est*2],type="sc16") + #logger.applog(f"Num samples {len(buf)} {num_samples} {len(mybuf2)}") except RuntimeError as ex: logger.error("Runtime error in rx_thread: {ex}") finally: diff --git a/adhoccomputing/Networking/PhysicalLayer/FrameHandlerBase.py b/adhoccomputing/Networking/PhysicalLayer/FrameHandlerBase.py index 51147db..2bef49b 100644 --- a/adhoccomputing/Networking/PhysicalLayer/FrameHandlerBase.py +++ b/adhoccomputing/Networking/PhysicalLayer/FrameHandlerBase.py @@ -98,13 +98,13 @@ def on_message_from_top(self, eventobj: Event): # Preserve the event id through the pipeline try: header = np.zeros(8, dtype=np.ubyte) - for i in range(8): - header[i] = i + #for i in range(8): + # header[i] = i hdr = PhyMessageHeader(PhyMessageTypes.PHYFRAMEDATA, self.componentinstancenumber,MessageDestinationIdentifiers.LINKLAYERBROADCAST) pld = PhyMessagePayload(eventobj.eventcontent.header, eventobj.eventcontent.payload ) msg = GenericMessage(hdr, pld) + ##### COMPRESS - byte_arr_msg = bytearray(zlib.compress(pickle.dumps(msg))) payload_len = len(byte_arr_msg) payload = np.frombuffer(byte_arr_msg, dtype=np.ubyte) diff --git a/testBladeRF.py b/testBladeRF.py index 943e685..2be5241 100644 --- a/testBladeRF.py +++ b/testBladeRF.py @@ -26,8 +26,8 @@ def __init__(self, componentname, componentinstancenumber, context=None, configu super().__init__(componentname, componentinstancenumber, context, configurationparameters, num_worker_threads, topology, child_conn) # SUBCOMPONENTS - macconfig = MacCsmaPPersistentConfigurationParameters(0.5, -45) - sdrconfig = SDRConfiguration(freq =915000000.0, bandwidth = 2000000, chan = 0, hw_tx_gain = 70, hw_rx_gain = 20, sw_tx_gain = -12.0) + macconfig = MacCsmaPPersistentConfigurationParameters(0.5, -40) + sdrconfig = SDRConfiguration(freq =2484000000, bandwidth = 2000000, chan = 0, hw_tx_gain = 0, hw_rx_gain = 39, sw_tx_gain = -12.0) self.appl = PingPongApplicationLayer("PingPongApplicationLayer", componentinstancenumber, topology=topology) self.phy = BladeRFOfdmFlexFramePhy("BladeRFOfdmFlexFramePhy", componentinstancenumber, usrpconfig=sdrconfig, topology=topology) @@ -56,11 +56,11 @@ def __init__(self, componentname, componentinstancenumber, context=None, configu def main(argv): setAHCLogLevel(logging.INFO) - num_nodes = 3 + num_nodes = 2 # Note that the topology has to specific: usrp winslab_b210_0 is run by instance 0 of the component # Therefore, the usrps have to have names winslab_b210_x where x \in (0 to nodecount-1) topo.construct_winslab_topology_without_channels(num_nodes, BladeRFNode) - # topo.mp_construct_sdr_topology_without_channels(num_nodes, BladeRFNode) + #topo.mp_construct_sdr_topology_without_channels(num_nodes, BladeRFNode) #topo.construct_winslab_topology_with_channels(2, UsrpNode, FIFOBroadcastPerfectChannel) # time.sleep(1) @@ -68,14 +68,15 @@ def main(argv): topo.start() - - while(True): - #topo.nodes[0].appl.send_self(Event(topo.nodes[0], PingPongApplicationLayerEventTypes.STARTBROADCAST, None)) - time.sleep(1) + i = 1 + while(i<10000): + topo.nodes[0].appl.send_self(Event(topo.nodes[0], PingPongApplicationLayerEventTypes.STARTBROADCAST, None)) + i += 1 + time.sleep(0.1) - #time.sleep(30) - #topo.exit() + time.sleep(3) + topo.exit() diff --git a/testVideoStreamingBladeRF.py b/testVideoStreamingBladeRF.py new file mode 100644 index 0000000..e15e6e4 --- /dev/null +++ b/testVideoStreamingBladeRF.py @@ -0,0 +1,115 @@ +import os +import sys +import time +sys.path.insert(0, os.getcwd()) +import time +import cv2 + + +from adhoccomputing.GenericModel import GenericModel +from adhoccomputing.Generics import * +from adhoccomputing.Experimentation.Topology import Topology +from adhoccomputing.Networking.LinkLayer.GenericLinkLayer import GenericLinkLayer +from adhoccomputing.Networking.NetworkLayer.GenericNetworkLayer import GenericNetworkLayer +from adhoccomputing.Networking.LogicalChannels.GenericChannel import GenericChannel +from adhoccomputing.Networking.ApplicationLayer.OpenCVVideoStreamingApp import * +from adhoccomputing.Networking.PhysicalLayer.BladeRFOfdmFlexFramePhy import BladeRFOfdmFlexFramePhy +from adhoccomputing.Networking.MacProtocol.CSMA import MacCsmaPPersistent, MacCsmaPPersistentConfigurationParameters +from adhoccomputing.Networking.ApplicationLayer.MessageSegmentation import * +import logging + + +macconfig = MacCsmaPPersistentConfigurationParameters(0.5, -50) +#sdrconfig = SDRConfiguration(freq =915000000.0, bandwidth = 4000000, chan = 0, hw_tx_gain = 70, hw_rx_gain = 30, sw_tx_gain = -12.0) +sdrconfig = SDRConfiguration(freq =2484000000, bandwidth = 2000000, chan = 0, hw_tx_gain = 0, hw_rx_gain = 39, sw_tx_gain = -12.0) + +appconfig = OpenCVVideoStreamingAppConfig(20) + + +class AdHocNode(GenericModel): + + def on_init(self, eventobj: Event): + logger.applog(f"Initializing {self.componentname}.{self.componentinstancenumber}") + pass + + def on_message_from_top(self, eventobj: Event): + self.send_down(eventobj) + + def on_message_from_bottom(self, eventobj: Event): + self.send_up(eventobj) + + def __init__(self, componentname, componentinstancenumber, context=None, configurationparameters=None, num_worker_threads=1, topology=None): + super().__init__(componentname, componentinstancenumber, context, configurationparameters, num_worker_threads, topology) + # SUBCOMPONENTS + + self.appl = OpenCVVideoStreamingApp("OpenCVVideoStreamingApp", componentinstancenumber, topology=topology, configurationparameters=appconfig) + self.seg = MessageSegmentation("MessageSegmentation", componentinstancenumber, topology=topology) + self.phy = BladeRFOfdmFlexFramePhy("UsrpB210OfdmFlexFramePhy", componentinstancenumber, topology=topology,usrpconfig=sdrconfig, ) + self.mac = MacCsmaPPersistent("MacCsmaPPersistent", componentinstancenumber, configurationparameters=macconfig, sdr=self.phy.sdrdev, topology=topology) + + self.components.append(self.appl) + self.components.append(self.mac) + self.components.append(self.seg) + #self.components.append(self.phy) + + # CONNECTIONS AMONG SUBCOMPONENTS + self.appl.connect_me_to_component(ConnectorTypes.UP, self) #Not required if nodemodel will do nothing + self.appl.connect_me_to_component(ConnectorTypes.DOWN, self.seg) + + self.seg.connect_me_to_component(ConnectorTypes.UP, self.appl) + self.seg.connect_me_to_component(ConnectorTypes.DOWN, self.mac) + + self.mac.connect_me_to_component(ConnectorTypes.UP, self.seg) + self.mac.connect_me_to_component(ConnectorTypes.DOWN, self.phy) + + # Connect the bottom component to the composite component.... + self.phy.connect_me_to_component(ConnectorTypes.UP, self.mac) + self.phy.connect_me_to_component(ConnectorTypes.DOWN, self) + + # self.phy.connect_me_to_component(ConnectorTypes.DOWN, self) + # self.connect_me_to_component(ConnectorTypes.DOWN, self.appl) + + +def main(): + + #NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL + setAHCLogLevel(25) + setAHCLogLevel(logging.INFO) + topo = Topology() + #topo.construct_sender_receiver(AdHocNode, AdHocNode, GenericChannel) + topo.construct_winslab_topology_without_channels_for_docker(AdHocNode, 0) + + + + + cap = cv2.VideoCapture(0) + cap.set(3,640) + cap.set(4,480) + #cv2.startWindowThread() + fourcc = cv2.VideoWriter_fourcc(*'MP4V') + #out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640,480)) + cv2.namedWindow('frame') + logger.applog(f"Frame rate will be {appconfig.framerate}") + topo.start() + time.sleep(3) + topo.nodes[0].phy.trigger_event(Event(None, EventTypes.INIT, "")) + topo.nodes[0].appl.send_self(Event(None, OpenCVVideoStreamingAppEventTypes.STARTSTREAMING, "")) + while(True): + frame = topo.nodes[0].appl.frame + if frame is not None: + #out.write(frame) + #frameresized = cv2.resize(frame, (640,480)) + cv2.imshow('frame', frame) + c = cv2.waitKey(1) + if c & 0xFF == ord('q'): + break + + cap.release() + #out.release() + cv2.destroyAllWindows() + + topo.exit() + time.sleep(5) + +if __name__ == "__main__": + main() diff --git a/testVideoStreamingTwoBladeRFs.py b/testVideoStreamingTwoBladeRFs.py new file mode 100644 index 0000000..89aa77e --- /dev/null +++ b/testVideoStreamingTwoBladeRFs.py @@ -0,0 +1,126 @@ +import os +import sys +import time +sys.path.insert(0, os.getcwd()) +import time +import cv2 + + +from adhoccomputing.GenericModel import GenericModel +from adhoccomputing.Generics import * +from adhoccomputing.Experimentation.Topology import Topology +from adhoccomputing.Networking.LinkLayer.GenericLinkLayer import GenericLinkLayer +from adhoccomputing.Networking.NetworkLayer.GenericNetworkLayer import GenericNetworkLayer +from adhoccomputing.Networking.LogicalChannels.GenericChannel import GenericChannel +from adhoccomputing.Networking.ApplicationLayer.OpenCVVideoStreamingApp import * +from adhoccomputing.Networking.PhysicalLayer.BladeRFOfdmFlexFramePhy import BladeRFOfdmFlexFramePhy +from adhoccomputing.Networking.MacProtocol.CSMA import MacCsmaPPersistent, MacCsmaPPersistentConfigurationParameters +from adhoccomputing.Networking.ApplicationLayer.MessageSegmentation import * +import logging + + +macconfig = MacCsmaPPersistentConfigurationParameters(0.5, -30) +#sdrconfig = SDRConfiguration(freq =915000000.0, bandwidth = 20000000, chan = 0, hw_tx_gain = 76, hw_rx_gain = 20, sw_tx_gain = -12.0) +sdrconfig = SDRConfiguration(freq =2484000000, bandwidth = 2000000, chan = 0, hw_tx_gain = 0, hw_rx_gain = 39, sw_tx_gain = -12.0) +appconfig = OpenCVVideoStreamingAppConfig(20) + + +class AdHocNode(GenericModel): + + def on_init(self, eventobj: Event): + logger.applog(f"Initializing {self.componentname}.{self.componentinstancenumber}") + pass + + def on_message_from_top(self, eventobj: Event): + self.send_down(eventobj) + + def on_message_from_bottom(self, eventobj: Event): + self.send_up(eventobj) + + def __init__(self, componentname, componentinstancenumber, context=None, configurationparameters=None, num_worker_threads=1, topology=None): + super().__init__(componentname, componentinstancenumber, context, configurationparameters, num_worker_threads, topology) + # SUBCOMPONENTS + + self.appl = OpenCVVideoStreamingApp("OpenCVVideoStreamingApp", componentinstancenumber, topology=topology, configurationparameters=appconfig) + self.seg = MessageSegmentation("MessageSegmentation", componentinstancenumber, topology=topology) + self.phy = BladeRFOfdmFlexFramePhy("BladeRFOfdmFlexFramePhy", componentinstancenumber, usrpconfig=sdrconfig, topology=topology) +# self.phy = UsrpB210OfdmFlexFramePhy("UsrpB210OfdmFlexFramePhy", componentinstancenumber, topology=topology,usrpconfig=sdrconfig, ) + self.mac = MacCsmaPPersistent("MacCsmaPPersistent", componentinstancenumber, configurationparameters=macconfig, sdr=self.phy.sdrdev, topology=topology) + + self.components.append(self.appl) + self.components.append(self.mac) + self.components.append(self.seg) + #self.components.append(self.phy) + + # CONNECTIONS AMONG SUBCOMPONENTS + self.appl.connect_me_to_component(ConnectorTypes.UP, self) #Not required if nodemodel will do nothing + self.appl.connect_me_to_component(ConnectorTypes.DOWN, self.seg) + + self.seg.connect_me_to_component(ConnectorTypes.UP, self.appl) + self.seg.connect_me_to_component(ConnectorTypes.DOWN, self.mac) + + self.mac.connect_me_to_component(ConnectorTypes.UP, self.seg) + self.mac.connect_me_to_component(ConnectorTypes.DOWN, self.phy) + + # Connect the bottom component to the composite component.... + self.phy.connect_me_to_component(ConnectorTypes.UP, self.mac) + self.phy.connect_me_to_component(ConnectorTypes.DOWN, self) + + # self.phy.connect_me_to_component(ConnectorTypes.DOWN, self) + # self.connect_me_to_component(ConnectorTypes.DOWN, self.appl) + + +def main(): + + #NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL + setAHCLogLevel(25) + setAHCLogLevel(logging.INFO) + topo = Topology() + #topo.construct_sender_receiver(AdHocNode, AdHocNode, GenericChannel) + num_nodes = 2 + topo.construct_winslab_topology_without_channels(num_nodes, AdHocNode, 0) + + + + + cap = cv2.VideoCapture(0) + cap.set(3,640) + cap.set(4,480) + cv2.startWindowThread() + #fourcc = cv2.VideoWriter_fourcc(*'MP4V') + #out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640,480)) + cv2.namedWindow('frame0') + cv2.namedWindow('frame1') + cv2.namedWindow('frame2') + #ret, frame = cap.read() + #cv2.imshow('frame', frame ) + #c = cv2.waitKey(1) + logger.applog(f"Frame rate will be {appconfig.framerate}") + topo.start() + # + time.sleep(3) + for i in range(num_nodes): + topo.nodes[i].phy.trigger_event(Event(None, EventTypes.INIT, "")) + #topo.nodes[1].phy.trigger_event(Event(None, EventTypes.INIT, "")) + # + topo.nodes[0].appl.send_self(Event(None, OpenCVVideoStreamingAppEventTypes.STARTSTREAMING, "")) + while(True): + for i in range(num_nodes): + frame = topo.nodes[i].appl.frame + if frame is not None: + #out.write(frame) + f = cv2.resize(frame, (640,480)) + cv2.imshow('frame'+str(i), f) + c = cv2.waitKey(1) + if c & 0xFF == ord('q'): + break + + cap.release() + #out.release() + cv2.destroyAllWindows() + + topo.exit() + time.sleep(5) + +if __name__ == "__main__": + main()