Skip to content

Commit

Permalink
bladerf gains corrected
Browse files Browse the repository at this point in the history
  • Loading branch information
Ertan Onur committed Jun 2, 2022
1 parent bcb9038 commit 430b7f5
Show file tree
Hide file tree
Showing 9 changed files with 347 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'))
Expand All @@ -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)}")
Expand All @@ -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)}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ 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):
hdr = PingPongApplicationLayerMessageHeader(PingPongApplicationLayerMessageTypes.BROADCAST, self.componentinstancenumber, MessageDestinationIdentifiers.LINKLAYERBROADCAST)
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ...Generics import *
from .LiquidDspUtils import *
import numpy as np
import zlib

mutex = Lock()

Expand All @@ -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


Expand Down Expand Up @@ -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)
Expand Down
103 changes: 71 additions & 32 deletions adhoccomputing/Networking/PhysicalLayer/BladeRFUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
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
import numpy as np
from ...Generics import *
from .LiquidDspUtils import *
import math
from ...Networking.PhysicalLayer.FrameHandlerBase import PhyEventTypes, PhyFrame

class BladeRFUtils(SDRUtils):

Expand Down Expand Up @@ -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

Expand All @@ -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" )
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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}" +
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions adhoccomputing/Networking/PhysicalLayer/FrameHandlerBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 11 additions & 10 deletions testBladeRF.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -56,26 +56,27 @@ 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)
# topo.nodes[0].send_self(Event(topo.nodes[0], UsrpNodeEventTypes.STARTBROADCAST, None))

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()



Expand Down
Loading

0 comments on commit 430b7f5

Please sign in to comment.