diff --git a/accelerometer.ino b/accelerometer.ino index 63afce1..01fce49 100644 --- a/accelerometer.ino +++ b/accelerometer.ino @@ -13,7 +13,7 @@ long counter = 0; void setup() { Serial.begin(9600); - int led_pins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10}; + //int led_pins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10}; //for (int k = 2;k < 11;k++){ // pinMode(k,OUTPUT); //} @@ -32,17 +32,17 @@ void loop() { //Should proabably switch to Serial.write()??? Serial.print(acc_value_1); - //Serial.print(','); - //Serial.print(acc_value_2); - //Serial.print(','); - //Serial.print(acc_value_3); + Serial.print(','); + Serial.print(acc_value_2); + Serial.print(','); + Serial.print(acc_value_3); Serial.print('\n'); delay(1); //Serial.println("========="); - accel = acc_value_3; + //accel = acc_value_3; //for (long i = 0; i < 250L; i++){ // digitalWrite(9,HIGH); @@ -52,7 +52,7 @@ void loop() { // } //tone(13,accel,100); //led_bank(accel); -counter++; +//counter++; } void led_bank(int accel){ diff --git a/accelerometer_plot.py b/accelerometer_plot.py index 0cdd263..5828418 100644 --- a/accelerometer_plot.py +++ b/accelerometer_plot.py @@ -3,6 +3,7 @@ from matplotlib.lines import Line2D import matplotlib.pyplot as plt import matplotlib.animation as animation +import time class Scope: def __init__(self, ax, maxt=500, dt=1): @@ -13,28 +14,43 @@ def __init__(self, ax, maxt=500, dt=1): #Zero the data self.tdata = [0] + self.xdata = [0] self.ydata = [0] + self.zdata = [0] #Create the line - self.line = Line2D(self.tdata, self.ydata) - self.ax.add_line(self.line) + self.linex = Line2D(self.tdata, self.xdata,color='r') + self.liney = Line2D(self.tdata, self.ydata,color='b') + self.linez = Line2D(self.tdata, self.zdata,color='k') + self.ax.add_line(self.linex) + self.ax.add_line(self.liney) + self.ax.add_line(self.linez) + + + #Create the legend + ax.legend([self.linex,self.liney,self.linez],['X-axis','Y-axis','Z-axis']) #Set the x and y graph limits self.ax.set_ylim(-.1, 1000) self.ax.set_xlim(0, self.maxt) - def update(self, y): + def update(self, new_data): #Update the plot + #Split the string into 3 integers + new_data = map(int,new_data.split(',')) + #Get the last timestep lastt = self.tdata[-1] #If the end of the graph is reached, reset the arrays if lastt > self.tdata[0] + self.maxt: # reset the arrays - self.tdata = [self.tdata[-1]] - self.ydata = [self.ydata[-1]] - self.ax.set_xlim(self.tdata[0], self.tdata[0] + self.maxt) - self.ax.figure.canvas.draw() + self.tdata = [self.tdata[-1]] + self.xdata = [self.xdata[-1]] + self.ydata = [self.ydata[-1]] + self.zdata = [self.zdata[-1]] + self.ax.set_xlim(self.tdata[0], self.tdata[0] + self.maxt) + self.ax.figure.canvas.draw() #Set the new t value to the dt more than the largest existing t value t = self.tdata[-1] + self.dt @@ -43,12 +59,16 @@ def update(self, y): self.tdata.append(t) #Update the existing y array with the value passed in to the function - self.ydata.append(y) + self.xdata.append(new_data[0]) + self.ydata.append(new_data[1]) + self.zdata.append(new_data[2]) #Update the line - self.line.set_data(self.tdata, self.ydata) - return self.line, - + self.linex.set_data(self.tdata, self.xdata) + self.liney.set_data(self.tdata, self.ydata) + self.linez.set_data(self.tdata, self.zdata) + return self.linex,self.liney,self.linez + #return self.linex def emitter(): @@ -57,28 +77,35 @@ def emitter(): #Split them based on new lines lines = a.split('\n') - + #Select the second to last line (sometimes the very last line is empty) #Beware - this throws away all the other data + #print np.size(lines) + #print lines buffer = lines[-2] + #new_data = map(int,buffer.split(',')) + #buffer= new_data[0] yield buffer - + #Set up the serial feed -ser = sr.Serial('/dev/tty.usbmodem621', 9600) +ser = sr.Serial('/dev/tty.usbmodem411', 9600) +time.sleep(1) #Create the figure fig = plt.figure() #Create the main subplot ax = fig.add_subplot(111) +#ax.set_xlabel('Data Sample') +#ax.set_ylabel('G Forces') #Create the scope class scope = Scope(ax) # pass a generator in "emitter" to produce data for the update function every 2 milliseconds -ani = animation.FuncAnimation(fig, scope.update, emitter, interval=2, +ani = animation.FuncAnimation(fig, scope.update, emitter, interval=50, blit=True) #Create the plot window -plt.show() +plt.show() \ No newline at end of file