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 12 import serial
13 13 import numpy
14 14 import os
  15 +import platform
15 16  
16 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 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 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 67 except Exception, e:
62 68 print("Failed to access " + serialport + " because:")
63 69 print(e)
... ... @@ -76,7 +82,7 @@ class Series:
76 82 # The latter seta of points much be of like nature and share an axis
77 83 # (time, temperature, xaccel)
78 84 # would be a meaningless thing to plot on a single graph anyway
79   -
  85 +
80 86 self.title = title
81 87 self.xname = xname
82 88 self.yname = yname
... ... @@ -100,24 +106,24 @@ class Plot(pyglet.window.Window):
100 106 self.lines = (10, 8)
101 107 #self.resizable = True
102 108 self.set_caption(self.series.title)
103   -
  109 +
104 110 def on_resize(self, width, height):
105 111 """Handle a resize event from the pyglet event loop"""
106 112 self.bounds = ((int(self.width * self.margins[0]), int(self.width * (1 - self.margins[0]))),
107 113 (int(self.height * self.margins[1]), int(self.height * (1 - self.margins[1]))))
108 114 pyglet.window.Window.on_resize(self, width, height)
109   -
  115 +
110 116 def on_draw(self):
111 117 """Draw all the components of the graph"""
112 118 self.drawBackground()
113 119 self.drawHeading()
114 120 self.drawAxis(0)
115 121 self.drawAxis(1)
116   -
  122 +
117 123 def drawBackground(self):
118 124 """Draw the graph background, currently a plain colour"""
119 125 pyglet.image.SolidColorImagePattern(WHITE).create_image(self.width, self.height).blit(0, 0)
120   -
  126 +
121 127 def drawHeading(self):
122 128 """Draw a title for the graph (duplicated in the window titlebar, if present"""
123 129 heading = pyglet.text.Label(self.series.title, color=BLACK,
... ... @@ -125,12 +131,12 @@ class Plot(pyglet.window.Window):
125 131 x=self.width/2, y=self.height-(self.margins[1]),
126 132 anchor_x='center', anchor_y='top')
127 133 heading.draw()
128   -
  134 +
129 135 def drawLine(self):
130 136 for n in range(len(data) - 1):
131 137 a, b = data[n], data[n+1]
132 138 pass
133   -
  139 +
134 140 def drawAxis(self, axis): # axis=0 is x, 1 is y
135 141 """Draw the gridlines and labels for one axis, specified in the last argument"""
136 142 limita = self.bounds[1-axis][1]
... ... @@ -180,16 +186,20 @@ class Plot(pyglet.window.Window):
180 186 pyglet.gl.glPopMatrix()
181 187  
182 188 tag.draw()
183   -
  189 +
184 190 testseries = Series()
185 191  
186   -plots = []
  192 +plots = []
187 193 plots.append(Plot(testseries))
188 194  
189 195 def pollSerial(elapsed):
190 196 """Check serial port for incoming data"""
191 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 203 testseries.addpoint(values)
194 204  
195 205 # Pyglet looks after the main event loop, but this ensures that data keeps being read in
... ...