4547 |
kaklik |
1 |
#!/usr/bin/python |
|
|
2 |
|
|
|
3 |
# Python test script for MLAB I2C232V01A translator module with HMC5888L Magnetometer sensor as I2C device. |
|
|
4 |
|
|
|
5 |
#uncomment for debbug purposes |
|
|
6 |
import logging |
|
|
7 |
logging.basicConfig(level=logging.DEBUG) |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
import time |
|
|
11 |
import datetime |
|
|
12 |
import sys |
|
|
13 |
|
|
|
14 |
from pymlab import config |
|
|
15 |
|
|
|
16 |
|
|
|
17 |
#### Script Arguments ############################################### |
|
|
18 |
|
|
|
19 |
if len(sys.argv) != 2: |
|
|
20 |
sys.stderr.write("Invalid number of arguments.\n") |
|
|
21 |
sys.stderr.write("Usage: %s #I2CPORT\n" % (sys.argv[0], )) |
|
|
22 |
sys.exit(1) |
|
|
23 |
|
|
|
24 |
port = sys.argv[1] |
|
|
25 |
|
|
|
26 |
#### Sensor Configuration ########################################### |
|
|
27 |
cfg = config.Config( |
|
|
28 |
i2c = { |
|
|
29 |
"port": port, |
|
|
30 |
"device": 'serial', # here you can explicitly set I2C driver with 'hid', 'smbus', 'serial' |
|
|
31 |
}, |
|
|
32 |
bus = [ |
|
|
33 |
{ |
|
|
34 |
"name": "mag", |
|
|
35 |
"type": "mag01", |
|
|
36 |
"gauss": 0.88, |
|
|
37 |
}, |
|
|
38 |
], |
|
|
39 |
) |
|
|
40 |
|
|
|
41 |
cfg.initialize() |
|
|
42 |
mag = cfg.get_device("mag") |
|
|
43 |
sys.stdout.write(" MLAB magnetometer sensor module example \r\n") |
|
|
44 |
time.sleep(0.5) |
|
|
45 |
|
|
|
46 |
#### Data Logging ################################################### |
|
|
47 |
|
|
|
48 |
sys.stdout.write("Magnetometer data acquisition system started \n") |
|
|
49 |
|
|
|
50 |
try: |
|
|
51 |
while True: |
|
|
52 |
mag.route() |
|
|
53 |
(x, y, z) = mag.axes() |
|
|
54 |
#sys.stdout.write("\rHeading: " + magnetometer.degrees(magnetometer.heading()) + " X: " + str(x) + " Y: " + str(y) + " Z: " + str(z) + " " ) |
|
|
55 |
sys.stdout.write(" X: " + str(x) + " Y: " + str(y) + " Z: " + str(z) + " " + "\r\n") |
|
|
56 |
sys.stdout.flush() |
|
|
57 |
time.sleep(0.5) |
|
|
58 |
except KeyboardInterrupt: |
|
|
59 |
sys.exit(0) |