Rev 4140 Rev 4141
1 #!/usr/bin/python 1 #!/usr/bin/python
2 """ 2 """
3 Testbed for the animation. 3 Testbed for the animation.
4   4  
5 It basically produces series of temperatures that get animated on the client 5 It basically produces series of temperatures that get animated on the client
6 browser side. 6 browser side.
7   7  
8 """ 8 """
9   9  
10 import time 10 import time
11 import sys 11 import sys
12 from pymlab import config 12 from pymlab import config
13   13  
14 import matplotlib 14 import matplotlib
15 matplotlib.use('module://mplh5canvas.backend_h5canvas') 15 matplotlib.use('module://mplh5canvas.backend_h5canvas')
16 from pylab import * 16 from pylab import *
17 import time 17 import time
18 import numpy as np 18 import numpy as np
19   19  
20 t = [1 + i for i in range(0, 70)] 20 t = [1 + i for i in range(0, 70)]
21 s = [-30 + i for i in range(0, 70)] 21 s = [-30 + i for i in range(0, 70)]
-   22 s2 = [-30 + i for i in range(0, 70)]
22   23  
23 plot(t, s, linewidth=3.0) 24 plot(t, s, linewidth=3.0)
24 xlabel('time (s)') 25 xlabel('time (s)')
25 ylabel('temperature (C)') 26 ylabel('temperature (C)')
26 title('Temperature') 27 title('Temperature external')
27 f = gcf() 28 f = gcf()
28 ax = f.gca() 29 ax = f.gca()
29   30  
-   31 f2 = figure()
30 show(block=False) 32 ax2 = f2.gca()
-   33 ax2.set_xlabel('time (s)')
-   34 ax2.set_ylabel('temperature (C)')
31 # show the figure manager but don't block script execution so animation works.. 35 ax2.set_title('Temperature internal')
-   36 ax2.plot(t, s, linewidth=3.0)
32   37  
-   38 show(block=False, layout=2, open_plot=True)
-   39 # show the figure manager but don't block script execution so animation works..
33   40  
34 while True: 41 while True:
35 #### Sensor Configuration ########################################### 42 #### Sensor Configuration ###########################################
36 cfg = config.Config( 43 cfg = config.Config(
37 i2c = { 44 i2c = {
38 "port": 0, # I2C bus number 45 "port": 0, # I2C bus number
39 }, 46 },
40   47  
41 bus = [ 48 bus = [
42 { 49 {
43 "name": "adc", 50 "name": "adc",
44 "type": "i2cadc01", 51 "type": "i2cadc01",
45 #"channel": 7, 52 #"channel": 7,
46 }, 53 },
47 ], 54 ],
48 ) 55 )
49   56  
50   57  
51 cfg.initialize() 58 cfg.initialize()
52 adc = cfg.get_device("adc") 59 adc = cfg.get_device("adc")
53   60  
54 n = t[-1] + 1 61 n = t[-1] + 1
55 try: 62 try:
56 while True: 63 while True:
57 # Temperature readout 64 # Temperature readout
58 temperature = adc.readTemp() 65 temperature = adc.readTemp()
59 print "Internal Temperature =", float("{0:.2f}".format(temperature)) 66 print "Internal Temperature =", float("{0:.2f}".format(temperature))
60   67  
61 time.sleep(0.5) 68 time.sleep(0.5)
62   69  
63 # Voltage readout 70 # Voltage readout
64 voltage = adc.readADC() 71 voltage = adc.readADC()
65 temp = (voltage / 0.0000397) + temperature #temperrature calculation for K type thermocouple 72 temp = (voltage / 0.0000397) + temperature #temperrature calculation for K type thermocouple
66 print "Voltage =", voltage, ", K-type thermocouple Temperature =", float("{0:.2f}".format(temp)) 73 print "Voltage =", voltage, ", K-type thermocouple Temperature =", float("{0:.2f}".format(temp))
67   74  
68 time.sleep(0.5) 75 time.sleep(0.5)
69   76  
70 # refresh graph 77 # refresh graph
71 s = s[1:] + [temp] 78 s = s[1:] + [temp]
-   79 s2 = s2[1:] + [temperature]
72 t = t[1:] + [n] 80 t = t[1:] + [n]
73 n += 1 81 n += 1
74 if (n % 10) == 0: 82 if (n % 10) == 0:
75 ax.text(n, temp-5, "{0:.1f}".format(temp), 83 ax.text(n, temp-5, "{0:.1f}".format(temp),
76 verticalalignment='bottom', horizontalalignment='right', 84 verticalalignment='bottom', horizontalalignment='right',
-   85 color='red', fontsize=16)
-   86 ax2.text(n, temperature-5, "{0:.1f}".format(temperature),
-   87 verticalalignment='bottom', horizontalalignment='right',
77 color='green', fontsize=16) 88 color='green', fontsize=16)
78   89  
79 ax.lines[0].set_xdata(t) 90 ax.lines[0].set_xdata(t)
80 ax.lines[0].set_ydata(s) 91 ax.lines[0].set_ydata(s)
81 ax.set_xlim(t[0],t[-1]) 92 ax.set_xlim(t[0],t[-1])
-   93 ax2.lines[0].set_xdata(t)
-   94 ax2.lines[0].set_ydata(s2)
-   95 ax2.set_xlim(t[0],t[-1])
82 f.canvas.draw() 96 f.canvas.draw()
-   97 f2.canvas.draw()
83 time.sleep(0.5) 98 time.sleep(0.5)
84   99  
-   100  
-   101  
85   102  
86 except IOError: 103 except IOError:
87 print "IOError" 104 print "IOError"
88 continue 105 continue
89   106  
90 except KeyboardInterrupt: 107 except KeyboardInterrupt:
91 sys.exit(0) 108 sys.exit(0)
92   109  
93   110