TempMovie.py

#!/usr/bin/env python

'''
TempMovie.py
Eric Ayars
6/13/12

Program to capture and plot data from the 10x10 temperature sensor array.

'''

import serial, sys
from pylab import *

size = 12           # size of display window

try:
    minT = float(sys.argv[1])
    maxT = float(sys.argv[2])
    port = sys.argv[3]
except (ValueError, IndexError):
    print '''
Call program with three or four parameters: At a minimum, give
    minimum expected temperature, 
    maximum expected temperature,
    location of serial port.

If you wish to save your data, add the base filename for saved data
frames as a fourth parameter.
'''
    exit(0)

# start the serial port
try:
    ser = serial.Serial(port, 57600, timeout=2)
except:
    print "Could not open port %s, exiting." % port
    exit(0)

# check to see if data is to be saved
try:
    baseFName = sys.argv[4]
    print "Data will be saved as %sxxxx.txt" %baseFName
    save = True
except IndexError:
    print "Data will not be saved."
    save = False

# allow animation
ion()

# set up matrix for temperature data
grid = zeros([10,10])

# read and discard a line to help get things in sync.
junk = ser.readline()

# open the view window
fig = figure(figsize=(size+1, size))

# define the levels at which to draw contours. The resolution 
# of the sensors at 9-bit (default) sensitivity is about 0.12,
# so setting level spacing below this does not help.
levels = arange(minT, maxT+0.12, 0.12)

FrameNumber = 0
# now the main loop, which continues until an error occurs.
while True:

    try:
        # this is the stuff to do unless a problem occurs.

        # Read a line of temperature data from the Arduino
        line = ser.readline()
    
        # split the line into a list of floating-point values
        temperatures = [float(T) for T in line.split()]

        # sort the list of temperatures into a matrix for plotting
        for j in range(10):
            for k in range(10):
                grid[j,k] = temperatures[j*10+k]

        # Save the data if required
        if save:
            fname = "%s%04d.txt" % (baseFName, FrameNumber)
            savetxt(fname, grid, fmt='%0.2f')

        # plot the data
        contourf(grid, levels)
        if FrameNumber == 0:    # We've just started, so add scale.
            axis('off')
            cbar = colorbar(ticks=linspace(minT, maxT, 11))
            cbar.ax.set_yticklabels(linspace(minT, maxT, 11))
        draw()

        # increment frame number and continue
        FrameNumber += 1

    # Handle problems here
    except ValueError:
        # error at float conversion
        print "Conversion error: frame dropped, continuing collection."
        continue
    except IndexError:
        # not all values received for a measurement set
        print "Partial frame received: frame dropped, continuing collection."
        continue
    except serial.serialutil.SerialException:
        # Someone just unplugged the device, or other loss of communication.
        print "Device disconnected, exiting."
        break
    except IOError:
        # problem saving data
        print "Input/Output Error, exiting."
        break
    except: 
        # Catch-all
        print "Unknown error! Exiting program..."
        break

Generated by GNU enscript 1.6.4.