Blame view

telemetry/code/monitor/graph_plotter.py 3 KB
b0f61ccf   Christopher Stone   Working demo for ...
1
2
#!/usr/bin/env python

6afdc6b9   Christopher Stone   Attempt at drawin...
3
4
import pyglet
import math
caf04a11   Christopher Stone   Demo code now dra...
5
6
import random
import time
b0f61ccf   Christopher Stone   Working demo for ...
7
8
9
10
11
12
13
14
15
16
import serial

datafeed = serial.Serial(
 port='/dev/ttyUSB0',
 baudrate = 9600,
 parity=serial.PARITY_NONE,
 stopbits=serial.STOPBITS_ONE,
 bytesize=serial.EIGHTBITS,
 timeout=1
)
be93fe78   Christopher Stone   Wrote smoothing f...
17
18
19

red = [1, 0, 0]
green = [0, 1, 0]
99dd1cb7   Christopher Stone   Preparation for s...
20
blue = [0.2, 0.5, 1]
6afdc6b9   Christopher Stone   Attempt at drawin...
21
 
4cf57310   Christopher Stone   made window resiz...
22
window = pyglet.window.Window(800, 480, resizable=True)
be93fe78   Christopher Stone   Wrote smoothing f...
23
window.set_caption("Raw data")
caf04a11   Christopher Stone   Demo code now dra...
24
25

starttime = time.time()
6afdc6b9   Christopher Stone   Attempt at drawin...
26
27

                 
99dd1cb7   Christopher Stone   Preparation for s...
28
def drawgrid(target, xlines, ylines, xlimits, ylimits):
be93fe78   Christopher Stone   Wrote smoothing f...
29
    pyglet.gl.glColor3f(0.5, 0.5, 0.5) 
99dd1cb7   Christopher Stone   Preparation for s...
30
31
32
    for xpos in range(0, target.width, target.width/xlines):
        pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (xpos, 0, xpos, target.height)))
        tagtext = str(round((xlimits[1]-xlimits[0])*(float(xpos)/target.width) + xlimits[0], 1))
6afdc6b9   Christopher Stone   Attempt at drawin...
33
34
        tag = pyglet.text.Label(tagtext, font_name='Arkhip', font_size=10, x=xpos-2, y=0, anchor_x='right', anchor_y='bottom')
        tag.draw()
99dd1cb7   Christopher Stone   Preparation for s...
35
36
37
    for ypos in range(0, target.width, target.height/ylines):
        pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (0, ypos, target.width, ypos)))
        tagtext = str(round((ylimits[1]-ylimits[0])*(float(ypos)/target.height) + ylimits[0], 1))
6afdc6b9   Christopher Stone   Attempt at drawin...
38
39
40
41
        tag = pyglet.text.Label(tagtext, font_name='Arkhip', font_size=10, x=0, y=ypos-2, anchor_x='left', anchor_y='top')
        tag.draw()


99dd1cb7   Christopher Stone   Preparation for s...
42
def plotline(target, xdata, ydata, colour):
be93fe78   Christopher Stone   Wrote smoothing f...
43
    pyglet.gl.glColor3f(colour[0], colour[1], colour[2])
caf04a11   Christopher Stone   Demo code now dra...
44
45
    points = []
    for n in range(max(len(xdata), len(ydata))):
b0f61ccf   Christopher Stone   Working demo for ...
46
        try:
99dd1cb7   Christopher Stone   Preparation for s...
47
            xpos = ((xdata[n]-min(xdata))*target.width)/(max(xdata)-min(xdata))
b0f61ccf   Christopher Stone   Working demo for ...
48
49
        except:
            xpos = 0
caf04a11   Christopher Stone   Demo code now dra...
50
        xpos = int(xpos)
b0f61ccf   Christopher Stone   Working demo for ...
51
        try:
99dd1cb7   Christopher Stone   Preparation for s...
52
            ypos = ((ydata[n]-min(ydata))*target.height)/(max(ydata)-min(ydata))
b0f61ccf   Christopher Stone   Working demo for ...
53
54
        except:
            ypos = 0
caf04a11   Christopher Stone   Demo code now dra...
55
56
57
58
59
        ypos = int(ypos)
        points.append([xpos, ypos])
    for n in range(len(points)-1):            
        pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (points[n][0], points[n][1], points[n+1][0], points[n+1][1])))

6afdc6b9   Christopher Stone   Attempt at drawin...
60

caf04a11   Christopher Stone   Demo code now dra...
61
62
xdata = [0]
ydata = [0]
be93fe78   Christopher Stone   Wrote smoothing f...
63
ydatasmooth = [0]
6afdc6b9   Christopher Stone   Attempt at drawin...
64

6afdc6b9   Christopher Stone   Attempt at drawin...
65

5503da4d   Christopher Stone   changed data life...
66
def poll_serial(foo):
be93fe78   Christopher Stone   Wrote smoothing f...
67
    max_points = 250
b0f61ccf   Christopher Stone   Working demo for ...
68
69
70
71
    value = datafeed.readline()
    try:
        value = float(value)
        ydata.append(value)
be93fe78   Christopher Stone   Wrote smoothing f...
72
        if len(ydata) > max_points:
b0f61ccf   Christopher Stone   Working demo for ...
73
            del ydata[0]
be93fe78   Christopher Stone   Wrote smoothing f...
74
            
b0f61ccf   Christopher Stone   Working demo for ...
75
        xdata.append(round(time.time() - starttime, 3))
be93fe78   Christopher Stone   Wrote smoothing f...
76
        if len(xdata) > max_points:
b0f61ccf   Christopher Stone   Working demo for ...
77
            del xdata[0]
be93fe78   Christopher Stone   Wrote smoothing f...
78
79
80
81
82
83
84
85
86
87
            
        avg = 0;
        for n in range(0, len(ydata)):
            weight = (1-float(n/len(ydata)))/max_points
            avg += weight * ydata[n]
            
        ydatasmooth.append(avg)
        if len(ydatasmooth) > max_points:
            del ydatasmooth[0]
            
b0f61ccf   Christopher Stone   Working demo for ...
88
89
    except:
        pass
5503da4d   Christopher Stone   changed data life...
90
91
92
    
pyglet.clock.schedule_interval(poll_serial, 0.01)

99dd1cb7   Christopher Stone   Preparation for s...
93
94
95
def drawgraph(target, xpoints, ypoints, colour):
    plotline(target, xpoints, ypoints, colour)
    drawgrid(target, 16, 10, [min(xpoints), max(xpoints)], [min(ypoints), max(ypoints)])
5503da4d   Christopher Stone   changed data life...
96
97
98
99

@window.event
def on_draw():
    window.clear()
99dd1cb7   Christopher Stone   Preparation for s...
100
    drawgraph(window, xdata, ydata, blue)
5503da4d   Christopher Stone   changed data life...
101
    
caf04a11   Christopher Stone   Demo code now dra...
102

b0f61ccf   Christopher Stone   Working demo for ...
103

6afdc6b9   Christopher Stone   Attempt at drawin...
104
105
    
pyglet.app.run()