Search code examples
pythonbluetooth-lowenergykalman-filter

A way to apply Kalman filtering on each BLE device separately using python


I am trying to get filtered RSSI values of each BLE beacon using Kalman filtering. I cannot use a single instance of kalman filtering on all BLE devices, how to map or assign a instance of kalman filter for each BLE device. I know it has something to do with maps but cannot figure out how to implement it. Any help would be much appreciated.

from bluepy.btle import Scanner
from kalman import KalmanFilter
scanner = Scanner().withDelegate(ScanDelegate())
blemacid = ["d7:b1:41:09:ca:c2","ed:9d:41:19:4c:43","fe:98:f1:d3:85:4f","de:0d:34:4d:66:5e"]

kalmanfilter=map(KalmanFilter(0.008,0.1),blemacid)

while(True):
   devices=scanner.scan(3.0)
   for device in devices:
      if device.addr in blemacid:
         print ("DEV={} RSSI ={}".format(device.addr,kalmanfilter.filter(device.rssi)))

the kalman filter implementation is here kalman filer


Solution

  • I don't think map is the correct solution for this problem. An instance of the filter stored in a dictionary and keyed by the device address would seem a better solution. For example:

    from bluepy.btle import Scanner
    from kalman import KalmanFilter
    scanner = Scanner().withDelegate(ScanDelegate())
    blemacid ={"d7:b1:41:09:ca:c2": KalmanFilter(0.008,0.1),
               "ed:9d:41:19:4c:43": KalmanFilter(0.008,0.1),
               "fe:98:f1:d3:85:4f": KalmanFilter(0.008,0.1),
               "de:0d:34:4d:66:5e": KalmanFilter(0.008,0.1)}
    
    while(True):
       devices=scanner.scan(3.0)
       for device in devices:
          if device.addr in blemacid.keys():
             print("DEV={} RSSI ={}".format(device.addr,blemacid[device.addr].filter(device.rssi)))
    

    I would also be careful that device.addr returns the address in lowercase, the same as you have in blemacid keys so the address comparison works correctly.