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 #!/usr/bin/env python 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 import pyglet 9 import pyglet
4 #import math 10 #import math
5 #import time 11 #import time
@@ -19,12 +25,14 @@ timeout=1 @@ -19,12 +25,14 @@ timeout=1
19 25
20 class Series: 26 class Series:
21 def __init__(self, points=100, title="Series title", xname="x-axis name", yname="y-axis name"): 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 self.title = title 29 self.title = title
23 self.xname = xname 30 self.xname = xname
24 self.yname = yname 31 self.yname = yname
25 self.data = [] 32 self.data = []
26 self.points = points 33 self.points = points
27 def addpoint(self, point): 34 def addpoint(self, point):
  35 + """Add a point to the dataset, and remove the oldest, if necessary"""
28 self.data.append(point) 36 self.data.append(point)
29 if len(self.data) > self.points: 37 if len(self.data) > self.points:
30 del self.data[0] 38 del self.data[0]
@@ -41,6 +49,7 @@ class Plot(pyglet.window.Window): @@ -41,6 +49,7 @@ class Plot(pyglet.window.Window):
41 self.set_caption(self.series.title) 49 self.set_caption(self.series.title)
42 50
43 def on_resize(self, width, height): 51 def on_resize(self, width, height):
  52 + """Handle a resize event from the pyglet event loop"""
44 self.bounds = ((int(self.width * self.margins[0]), int(self.width * (1 - self.margins[0]))), 53 self.bounds = ((int(self.width * self.margins[0]), int(self.width * (1 - self.margins[0]))),
45 (int(self.height * self.margins[1]), int(self.height * (1 - self.margins[1])))) 54 (int(self.height * self.margins[1]), int(self.height * (1 - self.margins[1]))))
46 pyglet.window.Window.on_resize(self, width, height) 55 pyglet.window.Window.on_resize(self, width, height)
@@ -66,6 +75,7 @@ class Plot(pyglet.window.Window): @@ -66,6 +75,7 @@ class Plot(pyglet.window.Window):
66 heading.draw() 75 heading.draw()
67 76
68 def drawAxis(self, axis): # axis=0 is x, 1 is y 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 limita = self.bounds[1-axis][1] 79 limita = self.bounds[1-axis][1]
70 limitb = self.bounds[1-axis][0] 80 limitb = self.bounds[1-axis][0]
71 start = self.bounds[axis][0] 81 start = self.bounds[axis][0]
@@ -97,23 +107,17 @@ class Plot(pyglet.window.Window): @@ -97,23 +107,17 @@ class Plot(pyglet.window.Window):
97 font_name=self.font, font_size=self.height*self.margins[axis]*0.3, 107 font_name=self.font, font_size=self.height*self.margins[axis]*0.3,
98 x=0, y=self.height/2, 108 x=0, y=self.height/2,
99 anchor_x='center', anchor_y='top') 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 pyglet.gl.glTranslatef(self.height//2, self.height//2, 0.0) 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 axistitle.draw() 115 axistitle.draw()
  116 + # Return everything to its previous state
104 pyglet.gl.glPopMatrix() 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 tag.draw() 119 tag.draw()
109 120
110 -  
111 -  
112 -  
113 -  
114 -  
115 -  
116 -  
117 testseries = Series() 121 testseries = Series()
118 122
119 plots = [] 123 plots = []
@@ -121,10 +125,11 @@ plots.append(Plot(testseries)) @@ -121,10 +125,11 @@ plots.append(Plot(testseries))
121 125
122 def pollSerial(elapsed): 126 def pollSerial(elapsed):
123 """Check serial port for incoming data""" 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 values = datafeed.readline().strip().split(", ") 129 values = datafeed.readline().strip().split(", ")
126 testseries.addpoint(values) 130 testseries.addpoint(values)
127 131
  132 +# Pyglet looks after the main event loop, but this ensures that data keeps being read in
128 pyglet.clock.schedule_interval(pollSerial, 0.1) 133 pyglet.clock.schedule_interval(pollSerial, 0.1)
129 134
130 pyglet.app.run() 135 pyglet.app.run()
131 \ No newline at end of file 136 \ No newline at end of file