Blame view

telemetry/code/monitor/graph_plotter_rewrite.py 5.18 KB
001c428d   Christopher Stone   Beginnings of a c...
1
2
3
4
5
#!/usr/bin/env python

import pyglet
#import math
#import time
e4e75d3f   Christopher Stone   Accept data from ...
6
import serial
3ce449c7   Christopher Stone   Added axis title ...
7
import numpy
001c428d   Christopher Stone   Beginnings of a c...
8
9
10

from colours import *

e4e75d3f   Christopher Stone   Accept data from ...
11
12
13
14
15
16
17
18
19
datafeed = serial.Serial(
port='/dev/ttyUSB0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)

6432a3a5   Christopher Stone   Added class to ke...
20
21
22
23
24
25
26
27
28
29
class Series:
    def __init__(self, points=100, title="Series title", xname="x-axis name", yname="y-axis name"):
        self.title = title
        self.xname = xname
        self.yname = yname
        self.data = []
        self.points = points
    def addpoint(self, point):
        self.data.append(point)
        if len(self.data) > self.points:
4863d03c   Christopher Stone   Fixed obvious bug...
30
            del self.data[0]
6432a3a5   Christopher Stone   Added class to ke...
31

d9082b1e   Christopher Stone   Made plot a subcl...
32
33
class Plot(pyglet.window.Window):
    def __init__(self, series):
01bfd701   Christopher Stone   Added docstrings
34
        """Setup a the details of a plot, and create a corresponding window"""
d9082b1e   Christopher Stone   Made plot a subcl...
35
        pyglet.window.Window.__init__(self, resizable=True)
6432a3a5   Christopher Stone   Added class to ke...
36
        self.series = series
948da001   Christopher Stone   More progress on ...
37
        self.font = 'Arkhip'
3ce449c7   Christopher Stone   Added axis title ...
38
        self.margins = (0.08, 0.08) # Fractions of window size
cad30d6c   Christopher Stone   Progress towards ...
39
        self.lines = (12, 8)
d9082b1e   Christopher Stone   Made plot a subcl...
40
        #self.resizable = True
3ce449c7   Christopher Stone   Added axis title ...
41
        self.set_caption(self.series.title)
6dbc3a2c   Christopher Stone   fixed line coordi...
42
43
        
    def on_resize(self, width, height):
d9082b1e   Christopher Stone   Made plot a subcl...
44
        self.bounds = ((int(self.width * self.margins[0]), int(self.width * (1 - self.margins[0]))),
6dbc3a2c   Christopher Stone   fixed line coordi...
45
46
                (int(self.height * self.margins[1]), int(self.height * (1 - self.margins[1]))))
        pyglet.window.Window.on_resize(self, width, height)
d5bc030b   Christopher Stone   Rotated y-axis ti...
47
        print(self.height)
948da001   Christopher Stone   More progress on ...
48
        
d9082b1e   Christopher Stone   Made plot a subcl...
49
    def on_draw(self):
01bfd701   Christopher Stone   Added docstrings
50
        """Draw all the components of the graph"""
948da001   Christopher Stone   More progress on ...
51
52
        self.drawBackground()
        self.drawHeading()
4cab8d4c   Christopher Stone   work towards draw...
53
54
        self.drawAxis(0)
        self.drawAxis(1)
948da001   Christopher Stone   More progress on ...
55
        
001c428d   Christopher Stone   Beginnings of a c...
56
    def drawBackground(self):
01bfd701   Christopher Stone   Added docstrings
57
        """Draw the graph background, currently a plain colour"""
d9082b1e   Christopher Stone   Made plot a subcl...
58
        pyglet.image.SolidColorImagePattern(WHITE).create_image(self.width, self.height).blit(0, 0)
001c428d   Christopher Stone   Beginnings of a c...
59
60
        
    def drawHeading(self):
01bfd701   Christopher Stone   Added docstrings
61
        """Draw a title for the graph (duplicated in the window titlebar, if present"""
3ce449c7   Christopher Stone   Added axis title ...
62
        heading = pyglet.text.Label(self.series.title, color=BLACK,
a1f7d747   Christopher Stone   Improvements to l...
63
64
                            font_name=self.font, font_size=self.height*self.margins[0]*0.5,
                            x=self.width/2, y=self.height-(self.margins[1]),
001c428d   Christopher Stone   Beginnings of a c...
65
66
                            anchor_x='center', anchor_y='top')
        heading.draw()
cad30d6c   Christopher Stone   Progress towards ...
67
     
4cab8d4c   Christopher Stone   work towards draw...
68
    def drawAxis(self, axis): # axis=0 is x, 1 is y
a1f7d747   Christopher Stone   Improvements to l...
69
70
71
72
        limita = self.bounds[1-axis][1]
        limitb = self.bounds[1-axis][0]
        start = self.bounds[axis][0]
        stop = self.bounds[axis][1]
4cab8d4c   Christopher Stone   work towards draw...
73
74
        increment = float(stop-start)/self.lines[axis]
        for pos in numpy.arange(start, stop+1, increment):
3ce449c7   Christopher Stone   Added axis title ...
75
            # Using fp arithmetic to avoid intermittent fencepost errors
4cab8d4c   Christopher Stone   work towards draw...
76
            pos = int(pos)
a1f7d747   Christopher Stone   Improvements to l...
77
78
            if axis==0:   # x axis, vertical lines
                pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (pos, limita, pos, limitb)),
3ce449c7   Christopher Stone   Added axis title ...
79
                                 ('c3B', (0, 0, 0, 0, 0, 0)))
4cab8d4c   Christopher Stone   work towards draw...
80
                tag = pyglet.text.Label("123", color=BLACK,
a1f7d747   Christopher Stone   Improvements to l...
81
                                        font_name=self.font, font_size=self.height*self.margins[1-axis]*0.28,
4cab8d4c   Christopher Stone   work towards draw...
82
83
                                        x=pos, y=self.height*self.margins[axis],
                                        anchor_x='left', anchor_y='top')
a1f7d747   Christopher Stone   Improvements to l...
84
85
86
87
                axistitle = pyglet.text.Label(self.series.xname, color=BLACK,
                                                font_name=self.font, font_size=self.height*self.margins[axis]*0.3,
                                                x=self.width/2, y=0,
                                                anchor_x='center', anchor_y='bottom')
d5bc030b   Christopher Stone   Rotated y-axis ti...
88
                axistitle.draw()
a1f7d747   Christopher Stone   Improvements to l...
89
90
            if axis==1:  # y axis, horizontal lines
                pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (limita, pos, limitb, pos)),
4cab8d4c   Christopher Stone   work towards draw...
91
92
                                 ('c3B', (0, 0, 0, 0, 0, 0)))
                tag = pyglet.text.Label("123", color=BLACK,
a1f7d747   Christopher Stone   Improvements to l...
93
94
                                        font_name=self.font, font_size=self.width*self.margins[axis]*0.22,
                                        x=self.width*self.margins[1-axis]*0.9, y=pos,
4cab8d4c   Christopher Stone   work towards draw...
95
                                        anchor_x='right', anchor_y='center')
a1f7d747   Christopher Stone   Improvements to l...
96
97
98
                axistitle = pyglet.text.Label(self.series.yname, color=BLACK,
                                                font_name=self.font, font_size=self.height*self.margins[axis]*0.3,
                                                x=0, y=self.height/2,
d5bc030b   Christopher Stone   Rotated y-axis ti...
99
100
101
102
103
104
105
106
107
                                                anchor_x='center', anchor_y='top')
                pyglet.gl.glPushMatrix()
                pyglet.gl.glTranslatef(self.height//2, self.height//2, 0.0)
                pyglet.gl.glRotatef(90.0, 0.0, 0.0, 1.0) 
                axistitle.draw()
                pyglet.gl.glPopMatrix()
                #pyglet.gl.glRotatef(-90.0, 0.0, 0.0, 1.0)
                #pyglet.gl.glTranslatef(-self.width//2, -self.height//2, 0.0)

3ce449c7   Christopher Stone   Added axis title ...
108
            tag.draw()
a1f7d747   Christopher Stone   Improvements to l...
109
        
d5bc030b   Christopher Stone   Rotated y-axis ti...
110
111


3ce449c7   Christopher Stone   Added axis title ...
112
            
4cab8d4c   Christopher Stone   work towards draw...
113

cad30d6c   Christopher Stone   Progress towards ...
114
115
        
        
001c428d   Christopher Stone   Beginnings of a c...
116
        
6432a3a5   Christopher Stone   Added class to ke...
117
118
testseries = Series()

001c428d   Christopher Stone   Beginnings of a c...
119
plots = []         
6432a3a5   Christopher Stone   Added class to ke...
120
plots.append(Plot(testseries))
001c428d   Christopher Stone   Beginnings of a c...
121

9159742e   Christopher Stone   Found out what th...
122
def pollSerial(elapsed):
e4e75d3f   Christopher Stone   Accept data from ...
123
    """Check serial port for incoming data"""
9159742e   Christopher Stone   Found out what th...
124
    # Note, elapsed is time since last call of this function
e4e75d3f   Christopher Stone   Accept data from ...
125
126
    values = datafeed.readline().strip().split(", ")
    testseries.addpoint(values)
948da001   Christopher Stone   More progress on ...
127

948da001   Christopher Stone   More progress on ...
128
pyglet.clock.schedule_interval(pollSerial, 0.1)
001c428d   Christopher Stone   Beginnings of a c...
129
130

pyglet.app.run()