Commit e9ac5444ee9f596a8a520c43a99de345fac2e12d

Authored by Christopher Stone
1 parent 87acb46a
Exists in master

Sucessful demonstration, plotting a sine wave against time. Code may need some tidying up...

Showing 1 changed file with 28 additions and 10 deletions   Show diff stats
robots/little_john/telemetry/code/monitor/version1/main.py
... ... @@ -7,7 +7,7 @@
7 7  
8 8 # This code is incomplete, and is missing core features
9 9  
10   -#import math
  10 +import math
11 11 import time
12 12 #import serial
13 13  
... ... @@ -24,6 +24,7 @@ from colours import *
24 24  
25 25 logging.basicConfig(format='%(levelname)s:\t%(message)s', level=logging.DEBUG)
26 26 logging.info("Logging system active")
  27 +starttime = time.time()
27 28  
28 29 datafeed = selectserial()
29 30  
... ... @@ -38,7 +39,7 @@ else:
38 39 logging.info('OS = ' + os)
39 40  
40 41 class Series:
41   - def __init__(self, points=100, title="Series title", xname="x-axis name", yname="y-axis name"):
  42 + def __init__(self, points=300, title="Series title", xname="x-axis name", yname="y-axis name"):
42 43 """Set up an object to store a 2D data series"""
43 44 # Proposal:
44 45 # In order to neatly handle multiple lines on the same graph
... ... @@ -54,7 +55,7 @@ class Series:
54 55 self.xname = xname
55 56 self.yname = yname
56 57 self.xlimits = (0, 100)
57   - self.ylimits = (0, 255)
  58 + self.ylimits = (-100, 100)
58 59 self.data = []
59 60 self.points = points
60 61  
... ... @@ -63,6 +64,20 @@ class Series:
63 64 self.data.append(point)
64 65 if len(self.data) > self.points:
65 66 del self.data[0]
  67 + self.autoscale(0)
  68 +
  69 + def autoscale(self, axis): # axis=0 is x, 1 is y
  70 + minval = self.data[0][axis]
  71 + maxval = self.data[0][axis]
  72 + for value in self.data:
  73 + if value[axis] < minval:
  74 + minval = value[axis]
  75 + if value[axis] > maxval:
  76 + maxval = value[axis]
  77 + if axis == 0:
  78 + self.xlimits = (minval, maxval)
  79 + else:
  80 + self.ylimits = (minval, maxval)
66 81  
67 82 class Plot(pyglet.window.Window):
68 83 def __init__(self, series):
... ... @@ -115,15 +130,16 @@ class Plot(pyglet.window.Window):
115 130 xscale = float(self.series.xlimits[1]-self.series.xlimits[0])/(self.bounds[0][1]-self.bounds[0][0])
116 131 yscale = float(self.series.ylimits[1]-self.series.ylimits[0])/(self.bounds[1][1]-self.bounds[1][0])
117 132 logging.debug("xscale = " + str(xscale) + ", yscale = " + str(yscale))
118   - #xscale = 5
119   - #xscale = self.series.xlimits[1] self.series.xlimits[0]
120   - #yscale = 1
121 133 lmar = int(self.width * self.margins[0])
122 134 rmar = int(self.width * self.margins[1])
123 135 tmar = int(self.height * self.margins[0])
124 136 bmar = int(self.height * self.margins[1])
  137 +
  138 + pyglet.gl.glLineWidth(2)
  139 +
125 140 for n in range(len(series.data) - 1):
126   - x1, y1, x2, y2 = series.data[n][0], series.data[n][1], series.data[n+1][0], series.data[n+1][1]
  141 + x1, y1 = series.data[n][0]-series.xlimits[0], series.data[n][1]-series.ylimits[0]
  142 + x2, y2 = series.data[n+1][0]-series.xlimits[0], series.data[n+1][1]-series.ylimits[0]
127 143 x1 = int((x1/xscale)+lmar)
128 144 y1 = int((y1/yscale)+bmar)
129 145 x2 = int((x2/xscale)+lmar)
... ... @@ -131,6 +147,8 @@ class Plot(pyglet.window.Window):
131 147 pyglet.graphics.draw(2, pyglet.gl.GL_LINES,
132 148 ('v2i', (x1, y1, x2, y2)),
133 149 ('c3B', (255, 0, 0, 255, 0, 0)))
  150 + pyglet.gl.glLineWidth(1)
  151 +
134 152  
135 153  
136 154 def drawAxis(self, axis): # axis=0 is x, 1 is y
... ... @@ -204,12 +222,12 @@ def pollSerial(elapsed):
204 222  
205 223 def fakePollSerial(elapsed):
206 224 """This function immitates the behaviour of pollSerial, for testing purposes"""
207   - faketime = time.time() * 10
208   - values = [(faketime%100), faketime%255]
  225 + timefromstart = (time.time()-starttime)
  226 + values = [timefromstart, 100*math.sin(25*math.radians(timefromstart))]
209 227 #logging.info("Generated test data: " + str(values))
210 228 testseries.addpoint(values)
211 229  
212 230 # Pyglet looks after the main event loop, but this ensures that data keeps being read in
213   -pyglet.clock.schedule_interval(fakePollSerial, 0.1)
  231 +pyglet.clock.schedule_interval(fakePollSerial, 0.04)
214 232  
215 233 pyglet.app.run()
... ...