From 449f72ed034134bb7ea2ed5b356df8eee887f5a7 Mon Sep 17 00:00:00 2001 From: Sam Perry Date: Sat, 1 Oct 2016 11:23:07 +0100 Subject: [PATCH] Completed step 1 Created plot showing linearity of FSR over increasing pressure. Added live plotting python script. --- .gitignore | 5 +- src/ArduinoPyPlot.py | 112 +++++++++++++++++++++++++++++++++++++++++++ src/sketch.ino | 21 ++++++++ 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 src/ArduinoPyPlot.py diff --git a/.gitignore b/.gitignore index 021b2a4..17e7bb8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ -.build +.DS_Store +bin/*/ +*build* +*~ diff --git a/src/ArduinoPyPlot.py b/src/ArduinoPyPlot.py new file mode 100644 index 0000000..4614e61 --- /dev/null +++ b/src/ArduinoPyPlot.py @@ -0,0 +1,112 @@ +""" +ldr.py +Display analog data from Arduino using Python (matplotlib) +Author: Mahesh Venkitachalam +Website: electronut.in +""" + +import sys, serial, argparse +import numpy as np +from time import sleep +from collections import deque + +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +# plot class +class AnalogPlot: + # constr + def __init__(self, strPort, maxLen): + # open serial port + self.ser = serial.Serial(strPort, 9600) + + self.ax = deque([0.0]*maxLen) + self.ay = deque([0.0]*maxLen) + self.maxLen = maxLen + + # add to buffer + def addToBuf(self, buf, val): + if len(buf) < self.maxLen: + buf.append(val) + else: + buf.pop() + buf.appendleft(val) + + # add data + def add(self, data): + assert(len(data) == 2) + self.addToBuf(self.ax, data[0]) + self.addToBuf(self.ay, data[1]) + + # update plot + def update(self, frameNum, a0, a1): + try: + line = self.ser.readline() + data = [float(val) for val in line.split()] + # print data + if(len(data) == 2 and not pause): + self.add(data) + a0.set_data(range(self.maxLen), self.ax) + a1.set_data(range(self.maxLen), 0) + except KeyboardInterrupt: + print('exiting') + + if not pause: + return a0, + + # clean up + def close(self): + # close serial + self.ser.flush() + self.ser.close() + +pause = False +def onClick(event): + global pause + pause = not pause + +# main() function +def main(): + # create parser + parser = argparse.ArgumentParser(description="LDR serial") + # add expected arguments + parser.add_argument('--port', dest='port', required=True) + + # parse args + args = parser.parse_args() + + #strPort = '/dev/tty.usbserial-A7006Yqh' + strPort = args.port + + print('reading from serial port %s...' % strPort) + + # plot parameters + analogPlot = AnalogPlot(strPort, 300) + + print('plotting data...') + + # set up animation + fig = plt.figure() + + ax = plt.axes(xlim=(0, 300), ylim=(0, 1023)) + a0, = ax.plot([], []) + a1, = ax.plot([], []) + fig.canvas.mpl_connect('button_press_event', onClick) + anim = animation.FuncAnimation(fig, analogPlot.update, + fargs=(a0, a1), + frames=None, + interval=50) + + # show plot + plt.show() + + # clean up + analogPlot.close() + + print('exiting.') + + +# call main +if __name__ == '__main__': + main() diff --git a/src/sketch.ino b/src/sketch.ino index 9f91418..1acd60f 100644 --- a/src/sketch.ino +++ b/src/sketch.ino @@ -1,8 +1,29 @@ +// analog-plot +// +// Read analog values from A0 and A1 and print them to serial port. +// +// electronut.in + +#include "Arduino.h" + void setup() { + // initialize serial comms + Serial.begin(9600); } void loop() { + // read A0 + int val1 = analogRead(0); + // read A1 + int val2 = analogRead(1); + // print to serial + Serial.print(val1); + Serial.print(" "); + Serial.print(val2); + Serial.print("\n"); + // wait + delay(50); }