Commit be93fe7801cf4753661ac468d5b052704fb7cefc
1 parent
4d3a8ce0
Exists in
master
Wrote smoothing function, not yet used
Showing
1 changed file
with
24 additions
and
8 deletions
Show diff stats
telemetry/code/monitor/graph_plotter.py
| ... | ... | @@ -14,15 +14,19 @@ datafeed = serial.Serial( |
| 14 | 14 | bytesize=serial.EIGHTBITS, |
| 15 | 15 | timeout=1 |
| 16 | 16 | ) |
| 17 | + | |
| 18 | +red = [1, 0, 0] | |
| 19 | +green = [0, 1, 0] | |
| 20 | +blue = [0, 0, 1] | |
| 17 | 21 | |
| 18 | 22 | window = pyglet.window.Window() |
| 19 | -window.set_caption("Test graph") | |
| 23 | +window.set_caption("Raw data") | |
| 20 | 24 | |
| 21 | 25 | starttime = time.time() |
| 22 | 26 | |
| 23 | 27 | |
| 24 | 28 | def drawgrid(xlines, ylines, xlimits, ylimits): |
| 25 | - pyglet.gl.glColor4f(0.5, 0.5, 0.5, 1.0) | |
| 29 | + pyglet.gl.glColor3f(0.5, 0.5, 0.5) | |
| 26 | 30 | for xpos in range(0, window.width, window.width/xlines): |
| 27 | 31 | pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (xpos, 0, xpos, window.height))) |
| 28 | 32 | tagtext = str(round((xlimits[1]-xlimits[0])*(float(xpos)/window.width) + xlimits[0], 1)) |
| ... | ... | @@ -35,8 +39,8 @@ def drawgrid(xlines, ylines, xlimits, ylimits): |
| 35 | 39 | tag.draw() |
| 36 | 40 | |
| 37 | 41 | |
| 38 | -def plotline(xdata, ydata): | |
| 39 | - pyglet.gl.glColor4f(1, 0, 0, 1.0) | |
| 42 | +def plotline(xdata, ydata, colour): | |
| 43 | + pyglet.gl.glColor3f(colour[0], colour[1], colour[2]) | |
| 40 | 44 | points = [] |
| 41 | 45 | for n in range(max(len(xdata), len(ydata))): |
| 42 | 46 | try: |
| ... | ... | @@ -56,19 +60,31 @@ def plotline(xdata, ydata): |
| 56 | 60 | |
| 57 | 61 | xdata = [0] |
| 58 | 62 | ydata = [0] |
| 63 | +ydatasmooth = [0] | |
| 59 | 64 | |
| 60 | 65 | |
| 61 | 66 | def poll_serial(foo): |
| 67 | + max_points = 250 | |
| 62 | 68 | value = datafeed.readline() |
| 63 | 69 | try: |
| 64 | 70 | value = float(value) |
| 65 | 71 | ydata.append(value) |
| 66 | - | |
| 67 | - if len(ydata) > 300: | |
| 72 | + if len(ydata) > max_points: | |
| 68 | 73 | del ydata[0] |
| 74 | + | |
| 69 | 75 | xdata.append(round(time.time() - starttime, 3)) |
| 70 | - if len(xdata) > 300: | |
| 76 | + if len(xdata) > max_points: | |
| 71 | 77 | del xdata[0] |
| 78 | + | |
| 79 | + avg = 0; | |
| 80 | + for n in range(0, len(ydata)): | |
| 81 | + weight = (1-float(n/len(ydata)))/max_points | |
| 82 | + avg += weight * ydata[n] | |
| 83 | + | |
| 84 | + ydatasmooth.append(avg) | |
| 85 | + if len(ydatasmooth) > max_points: | |
| 86 | + del ydatasmooth[0] | |
| 87 | + | |
| 72 | 88 | except: |
| 73 | 89 | pass |
| 74 | 90 | |
| ... | ... | @@ -80,7 +96,7 @@ def on_draw(): |
| 80 | 96 | window.clear() |
| 81 | 97 | |
| 82 | 98 | drawgrid(10, 10, [min(xdata), max(xdata)], [min(ydata), max(ydata)]) |
| 83 | - plotline(xdata, ydata) | |
| 99 | + plotline(xdata, ydata, red) | |
| 84 | 100 | |
| 85 | 101 | |
| 86 | 102 | ... | ... |