Blame view

telemetry/code/monitor/graph_plotter.py 3.12 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
 
ae0e1c6f   Christopher Stone   Demo with two sim...
22
23
24
25
26
window0 = pyglet.window.Window(800, 480, resizable=True)
window0.set_caption("First graph")

window1 = pyglet.window.Window(800, 480, resizable=True)
window1.set_caption("Second graph")
caf04a11   Christopher Stone   Demo code now dra...
27
28

starttime = time.time()
6afdc6b9   Christopher Stone   Attempt at drawin...
29
30

                 
99dd1cb7   Christopher Stone   Preparation for s...
31
def drawgrid(target, xlines, ylines, xlimits, ylimits):
be93fe78   Christopher Stone   Wrote smoothing f...
32
    pyglet.gl.glColor3f(0.5, 0.5, 0.5) 
99dd1cb7   Christopher Stone   Preparation for s...
33
34
35
    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...
36
37
        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...
38
39
40
    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...
41
42
43
44
        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...
45
def plotline(target, xdata, ydata, colour):
be93fe78   Christopher Stone   Wrote smoothing f...
46
    pyglet.gl.glColor3f(colour[0], colour[1], colour[2])
caf04a11   Christopher Stone   Demo code now dra...
47
48
    points = []
    for n in range(max(len(xdata), len(ydata))):
b0f61ccf   Christopher Stone   Working demo for ...
49
        try:
99dd1cb7   Christopher Stone   Preparation for s...
50
            xpos = ((xdata[n]-min(xdata))*target.width)/(max(xdata)-min(xdata))
b0f61ccf   Christopher Stone   Working demo for ...
51
52
        except:
            xpos = 0
caf04a11   Christopher Stone   Demo code now dra...
53
        xpos = int(xpos)
b0f61ccf   Christopher Stone   Working demo for ...
54
        try:
99dd1cb7   Christopher Stone   Preparation for s...
55
            ypos = ((ydata[n]-min(ydata))*target.height)/(max(ydata)-min(ydata))
b0f61ccf   Christopher Stone   Working demo for ...
56
57
        except:
            ypos = 0
caf04a11   Christopher Stone   Demo code now dra...
58
59
60
61
62
        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...
63

caf04a11   Christopher Stone   Demo code now dra...
64
xdata = [0]
ae0e1c6f   Christopher Stone   Demo with two sim...
65
66
y0data = [0]
y1data = [0]
6afdc6b9   Christopher Stone   Attempt at drawin...
67

5503da4d   Christopher Stone   changed data life...
68
def poll_serial(foo):
ae0e1c6f   Christopher Stone   Demo with two sim...
69
70
71
    max_points = 200
    value0 = datafeed.readline().strip().split(", ")[0]
    value1 = datafeed.readline().strip().split(", ")[1]
b0f61ccf   Christopher Stone   Working demo for ...
72
    try:
ae0e1c6f   Christopher Stone   Demo with two sim...
73
74
75
76
77
78
79
80
81
        value0 = float(value0)
        y0data.append(value0)
        if len(y0data) > max_points:
            del y0data[0]
            
        value1 = float(value1)
        y1data.append(value1)
        if len(y1data) > max_points:
            del y1data[0]
be93fe78   Christopher Stone   Wrote smoothing f...
82
            
b0f61ccf   Christopher Stone   Working demo for ...
83
        xdata.append(round(time.time() - starttime, 3))
be93fe78   Christopher Stone   Wrote smoothing f...
84
        if len(xdata) > max_points:
ae0e1c6f   Christopher Stone   Demo with two sim...
85
            del xdata[0]  
b0f61ccf   Christopher Stone   Working demo for ...
86
87
    except:
        pass
5503da4d   Christopher Stone   changed data life...
88
89
90
    
pyglet.clock.schedule_interval(poll_serial, 0.01)

99dd1cb7   Christopher Stone   Preparation for s...
91
92
93
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...
94

ae0e1c6f   Christopher Stone   Demo with two sim...
95
@window0.event
5503da4d   Christopher Stone   changed data life...
96
def on_draw():
ae0e1c6f   Christopher Stone   Demo with two sim...
97
98
    window0.clear()
    drawgraph(window0, xdata, y0data, red)
5503da4d   Christopher Stone   changed data life...
99
    
ae0e1c6f   Christopher Stone   Demo with two sim...
100
101
102
103
@window1.event
def on_draw():
    window1.clear()
    drawgraph(window1, xdata, y1data, green)
6afdc6b9   Christopher Stone   Attempt at drawin...
104
105
    
pyglet.app.run()