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