I'm trying to plot a graph with python using the canvas widget, I'm currently sending data in from an arduino sensor sketch ..does anyone have an idea on how I can plot this graph in real time on Python using canvas. I've read up on it but I'm having problems with unpacking the x , y and z axis data and looping it to updating in real time. Below is the arduino sketch.And also the skeleton of the Tkinter canvas sketch. Thanks!!
AcceleroMMA7361 ski;
int x;
int y;
int z;
void setup(){
Serial.begin(115200);
ski.begin(13, 12,11,10, A0, A1, A2);
ski.setARefVoltage(3.3);
ski.setSensitivity(HIGH);
ski.calibrate();
}
void loop(){
x = ski.getXRaw();
y = ski.getYRaw();
z = ski.getZRaw();
Serial.print("\nx: ");
Serial.println(x);
Serial.print("\ty: ");
Serial.print(y);
Serial.print("\tz: ");
Serial.print(z);
delay(10); //(make it readable)
}//End of Arduino Sketch
from Tkinter import *
import serial #import Serial Library
import numpy as np # Import numpy
ardoData = serial.Serial('COM4', 115200)
accelX1 = [] #to hold the incoming axis data from the arduino
accelY1 = []
accelZ1 = []
class GraphException(Exception):
def __init__(self, string):
Exception.__init__(self, string)
class Smoking(Canvas):
def __init__(self, master, FrameTitle, Col, Row, Height):
Canvas.__init__(self, master)
self.FrameTitle = FrameTitle
self.x1Axis = range(1001)
self.y1Axis = range(1001)
self.z1Axis = range(1001)
self.Line1 = range(1001)
self.Line2 = range(1001)
self.Line3 = range(1001)
self.configure(height=Height, width=750, bg='grey', bd=3, relief=GROOVE)
self.grid()
self.grid_propagate(0)
self.Col = Col
self.Row = Row
self.place(y=Col, x=Row)
self.create_text(380, 20, text=self.FrameTitle, fill = 'black')
for i in range(0, 1001):
self.Line1[i] = self.create_line(-10+(i*10), 90, 0+(i*10), 90, fill='blue', width=0)
self.Line2[i] = self.create_line(-10+(i*10), 90, 0+(i*10), 90, fill='red', width=0)
self.Line3[i] = self.create_line(-10+(i*10), 90, 0+(i*10), 90, fill='yellow', width=0)
def LiveValues(self,accelX1, accelY1, accelZ1, Centerline = False, Dritter = False):
for x in range(1, 59):
for i in np.arange(1, 1001, 1):
while(ardoData.inWaiting() == 0):
pass #do nothing
ardoString = ardoData.readline()
dataArray = ardoString.strip().strip('\n') #Split into an array called dataArray and strip off any spaces
# Ensure that you are not working on empty line
if ardoString:
dataArray = ardoString.split(",")
if len(dataArray) > 1:
self.x1Axis = int(dataArray[0])
self.y1Axis = int(dataArray[1])
self.z1Axis = int(dataArray[2])
accelX1.append(self.x1Axis)
accelY1.append(self.y1Axis)
accelZ1.append(self.z1Axis)
for x in range(0, 1001):
self.coords(self.Line1[x], -10+(x*10), self.accelX1[x], (x*10), self.accelX1[x+1])
self.coords(self.Line2[x], -10+(x*10), self.accelY1[x], (x*10), self.accelY1[x+1])
self.coords(self.Line3[x], -10+(x*10), self.accelZ1[x], (x*10), self.accelZ1[x+1])
# Create and run the GUI
root = Tk()
app = Smoking(root, "Smooth Sailing", 1000, 1000, 1000)
app.mainloop()
So after working with the answer given on the post I ended up with a Line graph that is not reactive, I understand that I have to tweak the code to suit the range of values I am getting which is shown below. I increased the limit as indicated by @patthoyts but I still couldn't get either lines to show up on the canvas and to even be reactive ( what i mean by that is to respond to the movement of the sensor). Also, do bear with me Editors!, if I'm going about editing this question the wrong way, is just that I'm quite desperate to have this issue resolved. Does anyone have any clue as to how I could resolve this issue. Thanks PS: I added both images , that of my data stream and the image on the canvas.
coords
property.