Commit 2c1aba27b45acaeed9ee11fe10257d64d378a112

Authored by Hugo Stephens
1 parent 761c7779
Exists in master

Updating the latest version to run on Windows

Added a couple of ifs, requires a new dependency
'platform'
robots/little_john/telemetry/code/monitor/graph_plotter_rewrite.py
@@ -12,52 +12,58 @@ import pyglet @@ -12,52 +12,58 @@ import pyglet
12 import serial 12 import serial
13 import numpy 13 import numpy
14 import os 14 import os
  15 +import platform
15 16
16 from colours import * 17 from colours import *
17 -  
18 -devpatterns = ['ttyACM', 'ttyUSB', 'rfcomm']  
19 -  
20 -alldevs = os.listdir("/dev/")  
21 -targetdevs = []  
22 -for dev in alldevs:  
23 - for pattern in devpatterns:  
24 - if pattern in dev:  
25 - targetdevs.append(dev)  
26 -  
27 -  
28 -if len(targetdevs) == 0:  
29 - print("Sorry, no serial devices found.")  
30 - print("Exiting...")  
31 -elif len(targetdevs) > 1:  
32 - print("Found multiple serial devices: ")  
33 - for i, dev in enumerate(targetdevs):  
34 - print(" " + str(i) + ": /dev/" + dev)  
35 - while True:  
36 - try:  
37 - selection = int(input("Please enter your selection (as a digit):\n > "))  
38 - if selection >= 0 and selection < len(targetdevs):  
39 - break  
40 - except KeyboardInterrupt:  
41 - exit()  
42 - except:  
43 - print("Choice unrecognised: please try again:") 18 +# Who doesn't like cross platform development
  19 +if platform.system()=='Windows':
  20 + serialport='COM5'
  21 + os='Windows'
44 else: 22 else:
45 - print("Only found one likely serial device: /dev/" + dev)  
46 - selection = 0  
47 -  
48 -serialport = "/dev/" + targetdevs[selection] 23 + devpatterns = ['ttyACM', 'ttyUSB', 'rfcomm']
  24 +
  25 + alldevs = os.listdir("/dev/")
  26 + targetdevs = []
  27 + for dev in alldevs:
  28 + for pattern in devpatterns:
  29 + if pattern in dev:
  30 + targetdevs.append(dev)
  31 +
  32 +
  33 + if len(targetdevs) == 0:
  34 + print("Sorry, no serial devices found.")
  35 + print("Exiting...")
  36 + elif len(targetdevs) > 1:
  37 + print("Found multiple serial devices: ")
  38 + for i, dev in enumerate(targetdevs):
  39 + print(" " + str(i) + ": /dev/" + dev)
  40 + while True:
  41 + try:
  42 + selection = int(input("Please enter your selection (as a digit):\n > "))
  43 + if selection >= 0 and selection < len(targetdevs):
  44 + break
  45 + except KeyboardInterrupt:
  46 + exit()
  47 + except:
  48 + print("Choice unrecognised: please try again:")
  49 + else:
  50 + print("Only found one likely serial device: /dev/" + dev)
  51 + selection = 0
  52 +
  53 + serialport = "/dev/" + targetdevs[selection]
  54 + os='Other'
49 55
50 try: 56 try:
51 - datafeed = serial.Serial(  
52 - port=serialport,  
53 - baudrate = 9600,  
54 - parity=serial.PARITY_NONE,  
55 - stopbits=serial.STOPBITS_ONE,  
56 - bytesize=serial.EIGHTBITS,  
57 - timeout=1  
58 - )  
59 -  
60 - print("Sucessfully opened " + serialport + " as data source!") 57 +datafeed = serial.Serial(
  58 +port=serialport,
  59 +baudrate = 9600,
  60 +parity=serial.PARITY_NONE,
  61 +stopbits=serial.STOPBITS_ONE,
  62 +bytesize=serial.EIGHTBITS,
  63 +timeout=1
  64 +)
  65 +
  66 +print("Sucessfully opened " + serialport + " as data source!")
61 except Exception, e: 67 except Exception, e:
62 print("Failed to access " + serialport + " because:") 68 print("Failed to access " + serialport + " because:")
63 print(e) 69 print(e)
@@ -76,7 +82,7 @@ class Series: @@ -76,7 +82,7 @@ class Series:
76 # The latter seta of points much be of like nature and share an axis 82 # The latter seta of points much be of like nature and share an axis
77 # (time, temperature, xaccel) 83 # (time, temperature, xaccel)
78 # would be a meaningless thing to plot on a single graph anyway 84 # would be a meaningless thing to plot on a single graph anyway
79 - 85 +
80 self.title = title 86 self.title = title
81 self.xname = xname 87 self.xname = xname
82 self.yname = yname 88 self.yname = yname
@@ -100,24 +106,24 @@ class Plot(pyglet.window.Window): @@ -100,24 +106,24 @@ class Plot(pyglet.window.Window):
100 self.lines = (10, 8) 106 self.lines = (10, 8)
101 #self.resizable = True 107 #self.resizable = True
102 self.set_caption(self.series.title) 108 self.set_caption(self.series.title)
103 - 109 +
104 def on_resize(self, width, height): 110 def on_resize(self, width, height):
105 """Handle a resize event from the pyglet event loop""" 111 """Handle a resize event from the pyglet event loop"""
106 self.bounds = ((int(self.width * self.margins[0]), int(self.width * (1 - self.margins[0]))), 112 self.bounds = ((int(self.width * self.margins[0]), int(self.width * (1 - self.margins[0]))),
107 (int(self.height * self.margins[1]), int(self.height * (1 - self.margins[1])))) 113 (int(self.height * self.margins[1]), int(self.height * (1 - self.margins[1]))))
108 pyglet.window.Window.on_resize(self, width, height) 114 pyglet.window.Window.on_resize(self, width, height)
109 - 115 +
110 def on_draw(self): 116 def on_draw(self):
111 """Draw all the components of the graph""" 117 """Draw all the components of the graph"""
112 self.drawBackground() 118 self.drawBackground()
113 self.drawHeading() 119 self.drawHeading()
114 self.drawAxis(0) 120 self.drawAxis(0)
115 self.drawAxis(1) 121 self.drawAxis(1)
116 - 122 +
117 def drawBackground(self): 123 def drawBackground(self):
118 """Draw the graph background, currently a plain colour""" 124 """Draw the graph background, currently a plain colour"""
119 pyglet.image.SolidColorImagePattern(WHITE).create_image(self.width, self.height).blit(0, 0) 125 pyglet.image.SolidColorImagePattern(WHITE).create_image(self.width, self.height).blit(0, 0)
120 - 126 +
121 def drawHeading(self): 127 def drawHeading(self):
122 """Draw a title for the graph (duplicated in the window titlebar, if present""" 128 """Draw a title for the graph (duplicated in the window titlebar, if present"""
123 heading = pyglet.text.Label(self.series.title, color=BLACK, 129 heading = pyglet.text.Label(self.series.title, color=BLACK,
@@ -125,12 +131,12 @@ class Plot(pyglet.window.Window): @@ -125,12 +131,12 @@ class Plot(pyglet.window.Window):
125 x=self.width/2, y=self.height-(self.margins[1]), 131 x=self.width/2, y=self.height-(self.margins[1]),
126 anchor_x='center', anchor_y='top') 132 anchor_x='center', anchor_y='top')
127 heading.draw() 133 heading.draw()
128 - 134 +
129 def drawLine(self): 135 def drawLine(self):
130 for n in range(len(data) - 1): 136 for n in range(len(data) - 1):
131 a, b = data[n], data[n+1] 137 a, b = data[n], data[n+1]
132 pass 138 pass
133 - 139 +
134 def drawAxis(self, axis): # axis=0 is x, 1 is y 140 def drawAxis(self, axis): # axis=0 is x, 1 is y
135 """Draw the gridlines and labels for one axis, specified in the last argument""" 141 """Draw the gridlines and labels for one axis, specified in the last argument"""
136 limita = self.bounds[1-axis][1] 142 limita = self.bounds[1-axis][1]
@@ -180,16 +186,20 @@ class Plot(pyglet.window.Window): @@ -180,16 +186,20 @@ class Plot(pyglet.window.Window):
180 pyglet.gl.glPopMatrix() 186 pyglet.gl.glPopMatrix()
181 187
182 tag.draw() 188 tag.draw()
183 - 189 +
184 testseries = Series() 190 testseries = Series()
185 191
186 -plots = [] 192 +plots = []
187 plots.append(Plot(testseries)) 193 plots.append(Plot(testseries))
188 194
189 def pollSerial(elapsed): 195 def pollSerial(elapsed):
190 """Check serial port for incoming data""" 196 """Check serial port for incoming data"""
191 # Note: 'elapsed' is time since last call of this function 197 # Note: 'elapsed' is time since last call of this function
192 - values = datafeed.readline().strip().split(", ") 198 + # This works, but it might be better for performance to have two seperate functions, only one of which is run.
  199 + if os=='Windows':
  200 + values = datafeed.readline().strip().split(b", ")
  201 + else:
  202 + values = datafeed.readline().strip().split(", ")
193 testseries.addpoint(values) 203 testseries.addpoint(values)
194 204
195 # Pyglet looks after the main event loop, but this ensures that data keeps being read in 205 # Pyglet looks after the main event loop, but this ensures that data keeps being read in