Commit b0f61ccf3f32ec1b7ae895a0fe45bbbb16b39675

Authored by Christopher Stone
1 parent caf04a11
Exists in master

Working demo for plotting a live graph of data recieved over a serial connection!

telemetry/code/monitor/pyglet_test.py renamed to telemetry/code/monitor/graph_plotter.py 100644 → 100755
  1 +#!/usr/bin/env python
  2 +
1 3 import pyglet
2 4 import math
3 5 import random
4 6 import time
  7 +import serial
  8 +
  9 +datafeed = serial.Serial(
  10 + port='/dev/ttyUSB0',
  11 + baudrate = 9600,
  12 + parity=serial.PARITY_NONE,
  13 + stopbits=serial.STOPBITS_ONE,
  14 + bytesize=serial.EIGHTBITS,
  15 + timeout=1
  16 +)
5 17  
6 18 window = pyglet.window.Window()
7 19 window.set_caption("Test graph")
... ... @@ -36,9 +48,15 @@ def plotline(xdata, ydata):
36 48 pyglet.gl.glColor4f(1, 0, 0, 1.0)
37 49 points = []
38 50 for n in range(max(len(xdata), len(ydata))):
39   - xpos = ((xdata[n]-min(xdata))*window.width)/(max(xdata)-min(xdata))
  51 + try:
  52 + xpos = ((xdata[n]-min(xdata))*window.width)/(max(xdata)-min(xdata))
  53 + except:
  54 + xpos = 0
40 55 xpos = int(xpos)
41   - ypos = ((ydata[n]-min(ydata))*window.height)/(max(ydata)-min(ydata))
  56 + try:
  57 + ypos = ((ydata[n]-min(ydata))*window.height)/(max(ydata)-min(ydata))
  58 + except:
  59 + ypos = 0
42 60 ypos = int(ypos)
43 61 points.append([xpos, ypos])
44 62 for n in range(len(points)-1):
... ... @@ -52,14 +70,23 @@ ydata = [0]
52 70 @window.event
53 71 def on_draw():
54 72 window.clear()
55   - ydata.append(random.uniform(0, 10))
56   - if len(ydata) > 100:
57   - del ydata[0]
58   - xdata.append(round(time.time() - starttime, 3))
59   - if len(xdata) > 100:
60   - del xdata[0]
  73 + #ydata.append(random.uniform(0, 10))
  74 + value = datafeed.readline()
  75 + try:
  76 + value = float(value)
  77 + ydata.append(value)
  78 + print("Value: " + str(ydata[-1]))
  79 +
  80 + if len(ydata) > 250:
  81 + del ydata[0]
  82 + xdata.append(round(time.time() - starttime, 3))
  83 + if len(xdata) > 250:
  84 + del xdata[0]
  85 + except:
  86 + pass
61 87 drawgrid(10, 12, [min(xdata), max(xdata)], [min(ydata), max(ydata)])
62 88 plotline(xdata, ydata)
63 89  
  90 +
64 91  
65 92 pyglet.app.run()
66 93 \ No newline at end of file
... ...
telemetry/code/robot/analogread_demo/analogread_demo.ino 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +/*
  2 + * Read analogue data, send it on
  3 + *
  4 + * A starting point for a robot monitoring system
  5 + * By the UoN Robot Wars project, 2018
  6 + * This code is under the GPL
  7 + */
  8 +
  9 +#include <math.h>
  10 +
  11 +void setup() {
  12 + Serial.begin(9600);
  13 + Serial.println("Hello, World");
  14 + pinMode(13, OUTPUT);
  15 +}
  16 +
  17 +void loop() {
  18 + float avalue = analogRead(A0);
  19 + Serial.println(avalue);
  20 +}
... ...
telemetry/code/robot/fading_led_demo/fading_led_demo.ino
... ... @@ -16,12 +16,14 @@ void setup() {
16 16  
17 17 void loop() {
18 18 // PWM an LED, fading sinusoidally
19   - double major_frequency = 0.3;
20   - double minor_frequency = 100;
  19 + double major_frequency = 0.5;
  20 + double minor_frequency = 50;
21 21 double duty = 0.5+(0.5*sin(2*PI*major_frequency*millis()/1000));
22 22 Serial.println(duty);
23 23 digitalWrite(13, HIGH);
24 24 delay(1000 * duty/minor_frequency);
25 25 digitalWrite(13, LOW);
26 26 delay(1000 * (1-duty)/minor_frequency);
  27 + float avalue = analogRead(A0);
  28 + //Serial.println(avalue);
27 29 }
... ...