Ok, so I am working on a project that takes in data from a sensor that is attached to a servo.
As one of the servos move it records a value n. n is zero initially, and increases as n = n + 1 each time the servos moves to its next position. n = 0 initally
Now this value n is read by a python script as data[0]. Since I want the value of the sensor which is data[1] at the position of the servo I made a script something like:
n_iteration = 0
n_item = 0
while(1):
if ord(data[0]) == n_iteration:
n_iteration = n_iteration + 1
heat_map_reverse[n_item] = ord(data[1])
n_item = n_item + 1
The tricky part for me is if n_iteration does not equal ord(data[0]). If this condition is not met I want my script to keep waiting in the while loop until it is met.
Also, my actual code is a little more complicated and I keep getting 'list index out of range' on the line where it says "heat_map_reverse[m][n] = yvals[49]" Any ideas why? THANKS!
n=0 #row of matrix
m=0 #column of matrix
row_elements = 5
column_elements = 5
n_iteration = 0
m_iteration = 0
heat_map_reverse = [[0 for x in range(n_iteration)] for x in range(m_iteration)]
print heat_map_reverse
while m<column_elements and n<row_elements: #loop forever
data = ser.read(6) # look for a character from serial port, will wait up to timeout above.
if len(data) == 6: #was there a byte to read? should always be true.
yvals = np.roll(yvals,-1) # shift the values in the array
time_high[49]=ord(data[0])+256*ord(data[1])
period[49]=ord(data[2])+256*ord(data[3])
duty_cycle[49]=time_high[49]/(8*period[49])
yvals[49]=280*(duty_cycle[49])-55
#yvals[49] = 280*((1/(8*(ord(data[2])+256*ord(data[3]))))*(ord(data[0])+256*ord(data[1]))-(125/1000))-20 # take the value of the byte
if (math.isnan(yvals[49]) or ord(data[4]) != 0 or ord(data[5]) !=0 ): #check to see if we are getting temperature readings
continue #if not return to begining of loop
outFile.write(str(time()-start_time)+" "+str(yvals[49])+" " + str(heat_map_reverse) + "\n") #write to file
line.set_ydata(yvals) # draw the line
fig.canvas.draw() # update the Canvas
win.set_title("Temp: "+str(yvals[49])+" deg C")
if (ord(data[5]) == m_iteration and ord(data[4]) == n_iteration):
n_iteration = n_iteration + 1
heat_map_reverse[m][n] = yvals[49]
n = n + 1
if (ord(data[5]) == m_iteration + 1 and ord(data[4]) != n_iteration):
m_iteration = m_iteration + 1
n_iteration = 0
n = 0
m = m + 1
print "n is " ,n
print "m is " ,m
print "MSP M_iteration is " ,ord(data[5])
print "MSP N_iteration is is " ,ord(data[4])
print "m_iteration is " ,m_iteration
print "n_iteration is " ,n_iteration
while gtk.events_pending(): #makes sure the GUI updates
gtk.main_iteration()
Aucun commentaire:
Enregistrer un commentaire