Commit dc810420199b1cf789b9546533df22f5a11d808c

Authored by Christopher Stone
1 parent d5bc030b
Exists in master

Added comments and docstrings

Showing 1 changed file with 17 additions and 12 deletions   Show diff stats
telemetry/code/monitor/graph_plotter_rewrite.py
1 1 #!/usr/bin/env python
2 2  
  3 +# Tool to draw live graphs of data coming in from serial port
  4 +# Written as a telemetry tool by:
  5 +# The UoN Robot Wars Project, 2018
  6 +
  7 +# This code is incomplete, and is missing core features
  8 +
3 9 import pyglet
4 10 #import math
5 11 #import time
... ... @@ -19,12 +25,14 @@ timeout=1
19 25  
20 26 class Series:
21 27 def __init__(self, points=100, title="Series title", xname="x-axis name", yname="y-axis name"):
  28 + """Set up an object to store a 2D data series"""
22 29 self.title = title
23 30 self.xname = xname
24 31 self.yname = yname
25 32 self.data = []
26 33 self.points = points
27 34 def addpoint(self, point):
  35 + """Add a point to the dataset, and remove the oldest, if necessary"""
28 36 self.data.append(point)
29 37 if len(self.data) > self.points:
30 38 del self.data[0]
... ... @@ -41,6 +49,7 @@ class Plot(pyglet.window.Window):
41 49 self.set_caption(self.series.title)
42 50  
43 51 def on_resize(self, width, height):
  52 + """Handle a resize event from the pyglet event loop"""
44 53 self.bounds = ((int(self.width * self.margins[0]), int(self.width * (1 - self.margins[0]))),
45 54 (int(self.height * self.margins[1]), int(self.height * (1 - self.margins[1]))))
46 55 pyglet.window.Window.on_resize(self, width, height)
... ... @@ -66,6 +75,7 @@ class Plot(pyglet.window.Window):
66 75 heading.draw()
67 76  
68 77 def drawAxis(self, axis): # axis=0 is x, 1 is y
  78 + """Draw the gridlines and labels for one axis, specified in the last argument"""
69 79 limita = self.bounds[1-axis][1]
70 80 limitb = self.bounds[1-axis][0]
71 81 start = self.bounds[axis][0]
... ... @@ -97,23 +107,17 @@ class Plot(pyglet.window.Window):
97 107 font_name=self.font, font_size=self.height*self.margins[axis]*0.3,
98 108 x=0, y=self.height/2,
99 109 anchor_x='center', anchor_y='top')
100   - pyglet.gl.glPushMatrix()
  110 + pyglet.gl.glPushMatrix() # Set up a new context to avoid confusing the main one
  111 + # Tranformation to rotate label, and ensure it ends up in the right place
101 112 pyglet.gl.glTranslatef(self.height//2, self.height//2, 0.0)
102   - pyglet.gl.glRotatef(90.0, 0.0, 0.0, 1.0)
  113 + pyglet.gl.glRotatef(90.0, 0.0, 0.0, 1.0)
  114 + # Draw the axis title using the rotated coordinate system
103 115 axistitle.draw()
  116 + # Return everything to its previous state
104 117 pyglet.gl.glPopMatrix()
105   - #pyglet.gl.glRotatef(-90.0, 0.0, 0.0, 1.0)
106   - #pyglet.gl.glTranslatef(-self.width//2, -self.height//2, 0.0)
107 118  
108 119 tag.draw()
109 120  
110   -
111   -
112   -
113   -
114   -
115   -
116   -
117 121 testseries = Series()
118 122  
119 123 plots = []
... ... @@ -121,10 +125,11 @@ plots.append(Plot(testseries))
121 125  
122 126 def pollSerial(elapsed):
123 127 """Check serial port for incoming data"""
124   - # Note, elapsed is time since last call of this function
  128 + # Note: 'elapsed' is time since last call of this function
125 129 values = datafeed.readline().strip().split(", ")
126 130 testseries.addpoint(values)
127 131  
  132 +# Pyglet looks after the main event loop, but this ensures that data keeps being read in
128 133 pyglet.clock.schedule_interval(pollSerial, 0.1)
129 134  
130 135 pyglet.app.run()
131 136 \ No newline at end of file
... ...