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 |
{ |
|
|
30 |
"name": "current_sensor", |
|
|
31 |
"type": "vcai2c01", |
|
|
32 |
}, |
|
|
33 |
], |
|
|
34 |
) |
|
|
35 |
cfg.initialize() |
|
|
36 |
|
|
|
37 |
print "Current loop sensor example \r\n" |
|
|
38 |
print "Time, channel #1, channel #2, channel #3 , channel #4 \r\n" |
|
|
39 |
sensor = cfg.get_device("current_sensor") |
|
|
40 |
time.sleep(0.5) |
|
|
41 |
|
|
|
42 |
#### Data Logging ################################################### |
|
|
43 |
|
|
|
44 |
try: |
|
|
45 |
with open(log_file, "a") as f: |
|
|
46 |
while True: |
|
|
47 |
|
|
|
48 |
sensor.setADC(channel = 1, gain = 2, sample_rate = 3.75); |
|
|
49 |
time.sleep(0.5) |
|
|
50 |
channel1 = sensor.readCurrent(); |
|
|
51 |
|
|
|
52 |
sensor.setADC(channel = 2, gain = 1, sample_rate = 15); |
|
|
53 |
time.sleep(0.5) |
|
|
54 |
channel2 = sensor.readADC(); |
|
|
55 |
|
|
|
56 |
sensor.setADC(channel = 3, gain = 1, sample_rate = 15); |
|
|
57 |
time.sleep(0.5) |
|
|
58 |
channel3 = sensor.readADC(); |
|
|
59 |
|
|
|
60 |
sensor.setADC(channel = 4, gain = 1, sample_rate = 15); |
|
|
61 |
time.sleep(0.5) |
|
|
62 |
channel4 = sensor.readADC(); |
|
|
63 |
|
|
|
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)) |
|
|
65 |
|
|
|
66 |
f.write("%d\t%d\t%d\t%d\t%d\n" % (time.time(), channel1, channel2, channel3, channel4)) |
|
|
67 |
f.flush() |
|
|
68 |
|
|
|
69 |
sys.stdout.flush() |
|
|
70 |
|
|
|
71 |
except KeyboardInterrupt: |
|
|
72 |
f.close() |
|
|
73 |
sys.exit(0) |
|
|
74 |
|
|
|
75 |
|