-
Notifications
You must be signed in to change notification settings - Fork 0
/
sliding.py
165 lines (142 loc) · 5.38 KB
/
sliding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 28 13:46:19 2014
@author: pbustos
"""
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Update a simple plot as rapidly as possible to measure speed.
"""
from PySide import QtGui, QtCore
#from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
from pyqtgraph.ptime import time
import blescan
import sys
import bluetooth._bluetooth as bluez
class BTPlotter():
nextColor=0
def __init__(self,app):
self.app = app
pg.setConfigOptions(antialias=True)
self.win = pg.GraphicsWindow(title="Basic plotting examples")
self.win.resize(800,800)
self.win.setWindowTitle('SmartPolyTech Beacon System')
self.p = self.win.addPlot(title='RSSI graph')
self.graphView = self.win.addViewBox(row=1,col=0)
#self.graphView.setLimits(xMin=-5, xMax=5, yMin = -5, yMax=5)
self.graphView.setAspectLocked()
self.graphView.setRange(rect=QtCore.QRectF(-6,6,12,-12))
box = QtGui.QGraphicsRectItem(-10, 5, 20, -10)
box.setPen(pg.mkPen("w"))
box.setBrush(pg.mkBrush(None))
self.graphView.addItem(box)
self.graphView.show()
self.graph = pg.GraphItem()
self.graphView.addItem(self.graph)
self.bufferLength = 20
self.p.setRange(QtCore.QRectF(0, 0, self.bufferLength, 100))
self.p.setLabel('bottom', 'Samples')
self.p.setLabel('left', 'Signal Strength')
self.p.setAutoVisible(y=True)
#cross hair
try:
dev_id = 0
self.sock = bluez.hci_open_dev(dev_id)
print "ble thread started"
except:
print "error accessing bluetooth device..."
sys.exit(1)
#Variables
self.lastTime = time()
self.fps = None
#Timers
self.timerBluetooth = QtCore.QTimer()
self.timerBluetooth.timeout.connect(self.updateBluetooth)
self.timerBluetooth.start(300)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.compute)
self.timer.start(100)
#self.p.scene().sigMouseMoved.connect(self.mouseMoved)
self.signalList = {}
self.curveList = {}
def compute(self):
#self.timeElapsed()
#self.app.processEvents() ## force complete redraw for every plot
pass
# def updateSpace(self, poseList):
#
# ## Define positions of nodes
# poses = np.array()
# edges = np.array()
# np.append(poses, [belX,belY])
# i=1
# for p in poseList:
# np.append(poses, p)
# e = [0,i]
# np.append(edges, e)
# i += 1
#
# self.graph.setData(pos=poses, adj=edges)
#
def updateBluetooth(self):
returnedList = blescan.parse_events(self.sock, 1)
for beacon in returnedList:
words = beacon.split(',')
mac = words[0]
uid = words[1]
major = words[2]
minor = words[3]
power = words[4]
rssi = words[5]
print mac, rssi, minor
#print "dict lenght" ,len(self.signalList)
if mac not in self.signalList:
#self.signalList[mac] = np.zeros(self.bufferLength,'f')
self.signalList[mac] = np.zeros(0,'f')
self.curveList[mac] = self.p.plot(pen=QtGui.QPen(pg.intColor(self.nextColor)))
self.nextColor += 1
s = self.signalList[mac]
val = 100 + float(rssi)
if s.size < self.bufferLength:
s = np.append(s,val)
# if s.size > 4:
# s[-1] = self.runningMeanFast(s,3)[0]
print "append" , val
else:
s = np.roll(s,-1)
s[-1] = val
# s[-1] = self.runningMeanFast(s,3)[0]
self.curveList[mac].setData(s)
self.signalList[mac] = s
self.timeElapsed()
self.app.processEvents() ## force complete redraw for every plot
def runningMeanFast(self,x, N):
return np.convolve(x, np.ones((N,))/N)[(N-1):]
def mouseMoved(self,pos):
if self.p.sceneBoundingRect().contains(pos):
mousePoint = self.p.vb.mapSceneToView(pos)
index = int(mousePoint.x())
if index > 0 and index < 100:
self.label.setText("<span style='font-size: 12pt'>x=%0.1f, <span style='color: red'>y1=%0.1f</span>,<span style='color: green'>y2=%0.1f</span>" % (mousePoint.x(), 50,70))
self.vLine.setPos(mousePoint.x())
self.hLine.setPos(mousePoint.y())
def timeElapsed(self):
now = time()
dt = now - self.lastTime
self.lastTime = now
if self.fps is None:
self.fps = 1.0/dt
else:
s = np.clip(dt*3., 0, 1)
self.fps = self.fps * (1-s) + (1.0/dt) * s
self.p.setTitle('%0.2f fps' % self.fps)
## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
app = QtGui.QApplication([])
bt = BTPlotter(app)
QtGui.QApplication.instance().exec_()