4843 |
kaklik |
1 |
#!/usr/bin/python |
|
|
2 |
|
|
|
3 |
#uncomment for debbug purposes |
|
|
4 |
#import logging |
|
|
5 |
#logging.basicConfig(level=logging.DEBUG) |
|
|
6 |
|
|
|
7 |
import time |
|
|
8 |
import datetime |
|
|
9 |
import sys |
4868 |
kaklik |
10 |
import os |
|
|
11 |
|
4843 |
kaklik |
12 |
from pymlab import config |
|
|
13 |
|
|
|
14 |
#### Script Arguments ############################################### |
|
|
15 |
|
|
|
16 |
if len(sys.argv) != 3: |
|
|
17 |
sys.stderr.write("Invalid number of arguments.\n") |
4868 |
kaklik |
18 |
sys.stderr.write("Usage: %s PORT_ADDRESS SAMPLE_INTERVAL\n" % (sys.argv[0], )) |
4843 |
kaklik |
19 |
sys.exit(1) |
|
|
20 |
|
4868 |
kaklik |
21 |
|
|
|
22 |
|
4843 |
kaklik |
23 |
port = eval(sys.argv[1]) |
4868 |
kaklik |
24 |
interval = eval(sys.argv[2]) |
4843 |
kaklik |
25 |
|
4868 |
kaklik |
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) |
|
|
30 |
|
4843 |
kaklik |
31 |
#### Sensor Configuration ########################################### |
|
|
32 |
|
|
|
33 |
cfg = config.Config( |
|
|
34 |
i2c = { |
|
|
35 |
"port": port, |
|
|
36 |
}, |
|
|
37 |
bus = [ |
|
|
38 |
{ |
4844 |
kaklik |
39 |
"name": "current_sensor1", |
4843 |
kaklik |
40 |
"type": "vcai2c01", |
4844 |
kaklik |
41 |
"address": 0x68, |
4843 |
kaklik |
42 |
}, |
4844 |
kaklik |
43 |
{ |
|
|
44 |
"name": "current_sensor2", |
|
|
45 |
"type": "vcai2c01", |
|
|
46 |
"address": 0x6a, |
|
|
47 |
}, |
4843 |
kaklik |
48 |
], |
|
|
49 |
) |
|
|
50 |
cfg.initialize() |
|
|
51 |
|
|
|
52 |
print "Current loop sensor example \r\n" |
4868 |
kaklik |
53 |
print "Time, channel #1, channel #2, channel #3 , channel #4, channel #5 \r\n" |
4844 |
kaklik |
54 |
sensor1 = cfg.get_device("current_sensor1") |
4868 |
kaklik |
55 |
sensor2 = cfg.get_device("current_sensor2") |
|
|
56 |
#time.sleep(0.5) |
4843 |
kaklik |
57 |
|
|
|
58 |
#### Data Logging ################################################### |
|
|
59 |
|
4868 |
kaklik |
60 |
try: |
|
|
61 |
before = time.time()-interval |
|
|
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: |
4843 |
kaklik |
69 |
|
4868 |
kaklik |
70 |
sensor1.setADC(channel = 1, gain = 1, sample_rate = 3.75); |
|
|
71 |
time.sleep(0.5) |
|
|
72 |
channel1 = sensor1.readCurrent(); |
|
|
73 |
|
|
|
74 |
sensor1.setADC(channel = 2, gain = 1, sample_rate = 3.75); |
|
|
75 |
time.sleep(0.5) |
|
|
76 |
channel2 = sensor1.readCurrent(); |
|
|
77 |
|
|
|
78 |
sensor1.setADC(channel = 3, gain = 1, sample_rate = 3.75); |
|
|
79 |
time.sleep(0.5) |
|
|
80 |
channel3 = sensor1.readCurrent(); |
|
|
81 |
|
|
|
82 |
sensor1.setADC(channel = 4, gain = 1, sample_rate = 3.75); |
|
|
83 |
time.sleep(0.5) |
|
|
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 |
|
4843 |
kaklik |
90 |
|
4868 |
kaklik |
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)) |
4843 |
kaklik |
92 |
|
4868 |
kaklik |
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)) |
|
|
94 |
f.flush() |
4843 |
kaklik |
95 |
|
4868 |
kaklik |
96 |
sys.stdout.flush() |
|
|
97 |
before = time.time() |
|
|
98 |
else: |
|
|
99 |
time.sleep(0.1) |
4843 |
kaklik |
100 |
|
|
|
101 |
except KeyboardInterrupt: |
|
|
102 |
f.close() |
|
|
103 |
sys.exit(0) |
|
|
104 |
|
|
|
105 |
|