Rev 4843 Rev 4844
1 #!/usr/bin/python 1 #!/usr/bin/python
2   2  
3 #uncomment for debbug purposes 3 #uncomment for debbug purposes
4 #import logging 4 #import logging
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 from pymlab import config 10 from pymlab import config
11   11  
12 #### Script Arguments ############################################### 12 #### Script Arguments ###############################################
13   13  
14 if len(sys.argv) != 3: 14 if len(sys.argv) != 3:
15 sys.stderr.write("Invalid number of arguments.\n") 15 sys.stderr.write("Invalid number of arguments.\n")
16 sys.stderr.write("Usage: %s PORT_ADDRESS LOG_FILE.csv\n" % (sys.argv[0], )) 16 sys.stderr.write("Usage: %s PORT_ADDRESS LOG_FILE.csv\n" % (sys.argv[0], ))
17 sys.exit(1) 17 sys.exit(1)
18   18  
19 port = eval(sys.argv[1]) 19 port = eval(sys.argv[1])
20 log_file = sys.argv[2] 20 log_file = sys.argv[2]
21   21  
22 #### Sensor Configuration ########################################### 22 #### Sensor Configuration ###########################################
23   23  
24 cfg = config.Config( 24 cfg = config.Config(
25 i2c = { 25 i2c = {
26 "port": port, 26 "port": port,
27 }, 27 },
28 bus = [ 28 bus = [
29 { 29 {
30 "name": "current_sensor", 30 "name": "current_sensor1",
31 "type": "vcai2c01", 31 "type": "vcai2c01",
-   32 "address": 0x68,
-   33 },
-   34 {
-   35 "name": "current_sensor2",
-   36 "type": "vcai2c01",
-   37 "address": 0x6a,
32 }, 38 },
33 ], 39 ],
34 ) 40 )
35 cfg.initialize() 41 cfg.initialize()
36   42  
37 print "Current loop sensor example \r\n" 43 print "Current loop sensor example \r\n"
38 print "Time, channel #1, channel #2, channel #3 , channel #4 \r\n" 44 print "Time, channel #1, channel #2, channel #3 , channel #4 \r\n"
39 sensor = cfg.get_device("current_sensor") 45 sensor1 = cfg.get_device("current_sensor1")
40 time.sleep(0.5) 46 time.sleep(0.5)
41   47  
42 #### Data Logging ################################################### 48 #### Data Logging ###################################################
43   49  
44 try: 50 try:
45 with open(log_file, "a") as f: 51 with open(log_file, "a") as f:
46 while True: 52 while True:
47   53  
48 sensor.setADC(channel = 1, gain = 2, sample_rate = 3.75); 54 sensor1.setADC(channel = 1, gain = 2, sample_rate = 3.75);
49 time.sleep(0.5) 55 time.sleep(0.5)
50 channel1 = sensor.readCurrent(); 56 channel1 = sensor1.readCurrent();
51   57  
52 sensor.setADC(channel = 2, gain = 1, sample_rate = 15); 58 sensor1.setADC(channel = 2, gain = 1, sample_rate = 15);
53 time.sleep(0.5) 59 time.sleep(0.5)
54 channel2 = sensor.readADC(); 60 channel2 = sensor1.readCurrent();
55   61  
56 sensor.setADC(channel = 3, gain = 1, sample_rate = 15); 62 sensor1.setADC(channel = 3, gain = 1, sample_rate = 15);
57 time.sleep(0.5) 63 time.sleep(0.5)
58 channel3 = sensor.readADC(); 64 channel3 = sensor1.readCurrent();
59   65  
60 sensor.setADC(channel = 4, gain = 1, sample_rate = 15); 66 sensor1.setADC(channel = 4, gain = 1, sample_rate = 15);
61 time.sleep(0.5) 67 time.sleep(0.5)
62 channel4 = sensor.readADC(); 68 channel4 = sensor1.readCurrent();
63   69  
64 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)) 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))
65   71  
66 f.write("%d\t%d\t%d\t%d\t%d\n" % (time.time(), channel1, channel2, channel3, channel4)) 72 f.write("%d\t%d\t%d\t%d\t%d\n" % (time.time(), channel1, channel2, channel3, channel4))
67 f.flush() 73 f.flush()
68   74  
69 sys.stdout.flush() 75 sys.stdout.flush()
70   76  
71 except KeyboardInterrupt: 77 except KeyboardInterrupt:
72 f.close() 78 f.close()
73 sys.exit(0) 79 sys.exit(0)
74   80  
75   81