Rev 4844 Rev 4868
Line 5... Line 5...
5 #logging.basicConfig(level=logging.DEBUG) 5 #logging.basicConfig(level=logging.DEBUG)
6   6  
7 import time 7 import time
8 import datetime 8 import datetime
9 import sys 9 import sys
-   10 import os
-   11  
10 from pymlab import config 12 from pymlab import config
11   13  
12 #### Script Arguments ############################################### 14 #### Script Arguments ###############################################
13   15  
14 if len(sys.argv) != 3: 16 if len(sys.argv) != 3:
15 sys.stderr.write("Invalid number of arguments.\n") 17 sys.stderr.write("Invalid number of arguments.\n")
16 sys.stderr.write("Usage: %s PORT_ADDRESS LOG_FILE.csv\n" % (sys.argv[0], )) 18 sys.stderr.write("Usage: %s PORT_ADDRESS SAMPLE_INTERVAL\n" % (sys.argv[0], ))
17 sys.exit(1) 19 sys.exit(1)
18   20  
-   21  
-   22  
19 port = eval(sys.argv[1]) 23 port = eval(sys.argv[1])
20 log_file = sys.argv[2] 24 interval = eval(sys.argv[2])
-   25  
-   26 if (interval<5) or (interval>3600):
-   27 sys.stderr.write("Invalid sample interval arguments.\n")
-   28 sys.stderr.write("The interval has to be in the range from 5 to 3600 seconds\n")
-   29 sys.exit(2)
21   30  
22 #### Sensor Configuration ########################################### 31 #### Sensor Configuration ###########################################
23   32  
24 cfg = config.Config( 33 cfg = config.Config(
25 i2c = { 34 i2c = {
Line 39... Line 48...
39 ], 48 ],
40 ) 49 )
41 cfg.initialize() 50 cfg.initialize()
42   51  
43 print "Current loop sensor example \r\n" 52 print "Current loop sensor example \r\n"
44 print "Time, channel #1, channel #2, channel #3 , channel #4 \r\n" 53 print "Time, channel #1, channel #2, channel #3 , channel #4, channel #5 \r\n"
45 sensor1 = cfg.get_device("current_sensor1") 54 sensor1 = cfg.get_device("current_sensor1")
-   55 sensor2 = cfg.get_device("current_sensor2")
46 time.sleep(0.5) 56 #time.sleep(0.5)
47   57  
48 #### Data Logging ################################################### 58 #### Data Logging ###################################################
49   59  
50 try: 60 try:
51 with open(log_file, "a") as f: 61 before = time.time()-interval
52 while True: 62 while True:
-   63
-   64 filename = time.strftime("%Y%m%d%H", time.gmtime())+".csv"
-   65 now = time.time()
-   66
-   67 if (now - before >= interval - 2.5): # 0.5*5 channels= 2.5s
-   68 with open(filename, "a") as f:
53   69  
54 sensor1.setADC(channel = 1, gain = 2, sample_rate = 3.75); 70 sensor1.setADC(channel = 1, gain = 1, sample_rate = 3.75);
55 time.sleep(0.5) 71 time.sleep(0.5)
56 channel1 = sensor1.readCurrent(); 72 channel1 = sensor1.readCurrent();
57   73
58 sensor1.setADC(channel = 2, gain = 1, sample_rate = 15); 74 sensor1.setADC(channel = 2, gain = 1, sample_rate = 3.75);
59 time.sleep(0.5) 75 time.sleep(0.5)
60 channel2 = sensor1.readCurrent(); 76 channel2 = sensor1.readCurrent();
61   77
62 sensor1.setADC(channel = 3, gain = 1, sample_rate = 15); 78 sensor1.setADC(channel = 3, gain = 1, sample_rate = 3.75);
63 time.sleep(0.5) 79 time.sleep(0.5)
64 channel3 = sensor1.readCurrent(); 80 channel3 = sensor1.readCurrent();
65   81
66 sensor1.setADC(channel = 4, gain = 1, sample_rate = 15); 82 sensor1.setADC(channel = 4, gain = 1, sample_rate = 3.75);
67 time.sleep(0.5) 83 time.sleep(0.5)
68 channel4 = sensor1.readCurrent(); 84 channel4 = sensor1.readCurrent();
-   85
-   86 sensor2.setADC(channel = 1, gain = 1, sample_rate = 3.75);
-   87 time.sleep(0.5)
-   88 channel5 = sensor2.readCurrent();
-   89
69   90  
70 sys.stdout.write("%s \t %0.3f \t %0.3f \t %0.3f \t %0.3f \n" % (datetime.datetime.now().isoformat(), channel1, channel2, channel3, channel4)) 91 sys.stdout.write("%s \t %0.3f \t %0.3f \t %0.3f \t %0.3f \t %0.3f \n" % (datetime.datetime.now().isoformat(), channel1, channel2, channel3, channel4, channel5))
71   92  
72 f.write("%d\t%d\t%d\t%d\t%d\n" % (time.time(), channel1, channel2, channel3, channel4)) 93 f.write("%d\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\n" % (time.time(), channel1, channel2, channel3, channel4, channel5))
73 f.flush() 94 f.flush()
74   95  
75 sys.stdout.flush() 96 sys.stdout.flush()
-   97 before = time.time()
-   98 else:
-   99 time.sleep(0.1)
76   100  
77 except KeyboardInterrupt: 101 except KeyboardInterrupt:
78 f.close() 102 f.close()
79 sys.exit(0) 103 sys.exit(0)
80   104