From d9d096654b60a4186c37e26a4aa4cd8e350f3d61 Mon Sep 17 00:00:00 2001 From: Christopher Stone Date: Fri, 9 Mar 2018 20:54:28 +0000 Subject: [PATCH] Demo reading two values from serial, plotting them on the same graph --- robots/little_john/telemetry/code/monitor/version1/main.py | 25 +++++++++++++++++-------- robots/little_john/telemetry/code/monitor/version1/plot.py | 33 ++++++++++++++++++++------------- robots/little_john/telemetry/code/monitor/version1/series.py | 7 +++++-- robots/little_john/telemetry/code/robot/analogread_demo/analogread_demo.ino | 4 +--- 4 files changed, 43 insertions(+), 26 deletions(-) diff --git a/robots/little_john/telemetry/code/monitor/version1/main.py b/robots/little_john/telemetry/code/monitor/version1/main.py index 4348c0e..d5ad62c 100755 --- a/robots/little_john/telemetry/code/monitor/version1/main.py +++ b/robots/little_john/telemetry/code/monitor/version1/main.py @@ -40,7 +40,7 @@ else: os='Other' logging.info('OS = ' + os) -testseries = Series(points=150, ylimits=(9, 1024), title="Sine wave demo", xname="Time (s)", yname="100sin(t)") +testseries = Series(points=150, ylimits=(0, 1024), title="Data from serial", xname="Time (s)", yname="ADC output") plots = [] plots.append(Plot(testseries)) @@ -48,13 +48,22 @@ def pollSerial(elapsed): """Check serial port for incoming data""" # Note: 'elapsed' is time since last call of this function # This works, but it might be better for performance to have two seperate functions, only one of which is run. - if os=='Windows': - values = datafeed.readline().strip().split(b", ") - else: - values = datafeed.readline().strip() - values = values.split(b', ') + try: + if os=='Windows': + values = datafeed.readline().strip().split(b", ") + else: + values = datafeed.readline().strip() + values = values.split(b', ') + except: + logging.error("Failed to recieve or parse input data") + return + for n, value in enumerate(values): - values[n] = float(value) + try: + values[n] = float(value) + except: + logging.error("Failed to convert input to float") + return #logging.info("Recieved data: " + str(values)) testseries.addpoint([time.time()-starttime] + values) @@ -66,6 +75,6 @@ def fakePollSerial(elapsed): testseries.addpoint(values) # Pyglet looks after the main event loop, but this ensures that data keeps being read in -pyglet.clock.schedule_interval(pollSerial, 0.04) +pyglet.clock.schedule_interval(pollSerial, 0.01) pyglet.app.run() diff --git a/robots/little_john/telemetry/code/monitor/version1/plot.py b/robots/little_john/telemetry/code/monitor/version1/plot.py index 7f00256..2cf0056 100644 --- a/robots/little_john/telemetry/code/monitor/version1/plot.py +++ b/robots/little_john/telemetry/code/monitor/version1/plot.py @@ -40,7 +40,7 @@ class Plot(pyglet.window.Window): self.drawHeading() self.drawAxis(0) self.drawAxis(1) - self.drawLine(self.series) + self.drawLines(self.series) def drawBackground(self): """Draw the graph background, currently a plain colour""" @@ -54,7 +54,7 @@ class Plot(pyglet.window.Window): anchor_x='center', anchor_y='top') heading.draw() - def drawLine(self, series): + def drawLines(self, series): xscale = float(self.series.xlimits[1]-self.series.xlimits[0])/(self.bounds[0][1]-self.bounds[0][0]) yscale = float(self.series.ylimits[1]-self.series.ylimits[0])/(self.bounds[1][1]-self.bounds[1][0]) lmar = int(self.width * self.margins[0]) @@ -63,17 +63,24 @@ class Plot(pyglet.window.Window): bmar = int(self.height * self.margins[1]) pyglet.gl.glLineWidth(2) - - for n in range(len(series.data) - 1): - x1, y1 = series.data[n][0]-series.xlimits[0], series.data[n][1]-series.ylimits[0] - x2, y2 = series.data[n+1][0]-series.xlimits[0], series.data[n+1][1]-series.ylimits[0] - x1 = int((x1/xscale)+lmar) - y1 = int((y1/yscale)+bmar) - x2 = int((x2/xscale)+lmar) - y2 = int((y2/yscale)+bmar) - pyglet.graphics.draw(2, pyglet.gl.GL_LINES, - ('v2i', (x1, y1, x2, y2)), - ('c3B', (255, 0, 0, 255, 0, 0))) + + linecolours = [(255, 0, 0, 255, 0, 0), (0, 0, 255, 0, 0, 255)] + + try: + for m in range(len(series.data[0])-1): + for n in range(len(series.data) - 1): + x1, y1 = series.data[n][0]-series.xlimits[0], series.data[n][m+1]-series.ylimits[0] + x2, y2 = series.data[n+1][0]-series.xlimits[0], series.data[n+1][m+1]-series.ylimits[0] + x1 = int((x1/xscale)+lmar) + y1 = int((y1/yscale)+bmar) + x2 = int((x2/xscale)+lmar) + y2 = int((y2/yscale)+bmar) + pyglet.graphics.draw(2, pyglet.gl.GL_LINES, + ('v2i', (x1, y1, x2, y2)), + ('c3B', linecolours[m])) + except: + logging.error("Failed to plot lines, possibly due to corrupt/missing data. (This happens at startup)") + pyglet.gl.glLineWidth(1) diff --git a/robots/little_john/telemetry/code/monitor/version1/series.py b/robots/little_john/telemetry/code/monitor/version1/series.py index dcbf017..e54210f 100644 --- a/robots/little_john/telemetry/code/monitor/version1/series.py +++ b/robots/little_john/telemetry/code/monitor/version1/series.py @@ -32,8 +32,11 @@ class Series: self.data.append(point) if len(self.data) > self.points: del self.data[0] - self.autoscale(0) - + try: + self.autoscale(0) + except: + logging.error("Series autoscale failed") + def autoscale(self, axis): # axis=0 is x, 1 is y minval = self.data[0][axis] maxval = self.data[0][axis] diff --git a/robots/little_john/telemetry/code/robot/analogread_demo/analogread_demo.ino b/robots/little_john/telemetry/code/robot/analogread_demo/analogread_demo.ino index 36e3b52..ffdbf1a 100644 --- a/robots/little_john/telemetry/code/robot/analogread_demo/analogread_demo.ino +++ b/robots/little_john/telemetry/code/robot/analogread_demo/analogread_demo.ino @@ -6,7 +6,6 @@ * This code is under the GPL */ -#include #include SoftwareSerial Bluetooth(3, 2); // Rx, Tx @@ -14,7 +13,6 @@ SoftwareSerial Bluetooth(3, 2); // Rx, Tx void setup() { Serial.begin(9600); Bluetooth.begin(115200); - //Bluetooth.print("Hello, wireless world!"); pinMode(13, OUTPUT); } @@ -25,5 +23,5 @@ void loop() { Bluetooth.print(a0value); Bluetooth.print(", "); Bluetooth.println(a7value); - delay(500); + delay(100); } -- libgit2 0.21.2