4429 |
jacho |
1 |
#!/usr/bin/python |
|
|
2 |
|
|
|
3 |
# Python library for RPS01A MLAB module with AS5048B I2C Magnetic position sensor. |
|
|
4 |
|
|
|
5 |
#uncomment for debbug purposes |
|
|
6 |
#import logging |
|
|
7 |
#logging.basicConfig(level=logging.DEBUG) |
|
|
8 |
|
|
|
9 |
import time |
|
|
10 |
import datetime |
|
|
11 |
import sys |
|
|
12 |
from pymlab import config |
|
|
13 |
|
|
|
14 |
#### Script Arguments ############################################### |
|
|
15 |
|
|
|
16 |
if len(sys.argv) != 2: |
|
|
17 |
sys.stderr.write("Invalid number of arguments.\n") |
|
|
18 |
sys.stderr.write("Usage: %s PORT ADDRESS\n" % (sys.argv[0], )) |
|
|
19 |
sys.exit(1) |
|
|
20 |
|
|
|
21 |
port = eval(sys.argv[1]) |
|
|
22 |
#### Sensor Configuration ########################################### |
|
|
23 |
|
|
|
24 |
'''' |
|
|
25 |
cfg = config.Config( |
|
|
26 |
i2c = { |
|
|
27 |
"port": port, |
|
|
28 |
}, |
|
|
29 |
|
|
|
30 |
bus = [ |
|
|
31 |
{ |
|
|
32 |
"type": "i2chub", |
|
|
33 |
"address": 0x72, |
|
|
34 |
|
|
|
35 |
"children": [ |
|
|
36 |
{"name": "encoder", "type": "rps01", "channel": 1, } |
|
|
37 |
], |
|
|
38 |
}, |
|
|
39 |
], |
|
|
40 |
) |
|
|
41 |
|
|
|
42 |
''' |
|
|
43 |
cfg = config.Config( |
|
|
44 |
i2c = { |
|
|
45 |
"port": port, |
|
|
46 |
}, |
|
|
47 |
bus = [ |
|
|
48 |
{ |
|
|
49 |
"name": "encoder", |
|
|
50 |
"type": "rps01", |
|
|
51 |
}, |
|
|
52 |
], |
|
|
53 |
) |
|
|
54 |
|
|
|
55 |
|
|
|
56 |
cfg.initialize() |
|
|
57 |
|
|
|
58 |
print "RPS01A magnetic position sensor RPS01 readout example \r\n" |
|
|
59 |
sensor = cfg.get_device("encoder") |
|
|
60 |
|
|
|
61 |
print sensor.get_address() |
|
|
62 |
print sensor.get_zero_position() |
|
|
63 |
|
|
|
64 |
#### Data Logging ################################################### |
|
|
65 |
|
|
|
66 |
try: |
|
|
67 |
while True: |
|
|
68 |
# for i in range(10): |
|
|
69 |
angle1 = sensor.get_angle(verify = False) |
|
|
70 |
time.sleep(0.1) |
|
|
71 |
angle2 = sensor.get_angle(verify = False) |
|
|
72 |
time.sleep(0.1) |
|
|
73 |
angle3 = sensor.get_angle(verify = False) |
|
|
74 |
|
|
|
75 |
if (angle1 < angle2): |
|
|
76 |
speed = (angle2 - angle1)/0.01 |
|
|
77 |
else: |
|
|
78 |
speed = (360 - angle1 + angle2)/0.01 |
|
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
sys.stdout.write("Speed: " + str(speed) +"\t"+ str(angle1) +"\t"+ str(angle2) + "\t\tMagnitude: " + str(sensor.get_magnitude()) |
|
|
83 |
+ "\tAGC Value: " + str(sensor.get_agc_value()) + "\tDiagnostics: " + str(sensor.get_diagnostics()) + "\r\n") |
|
|
84 |
sys.stdout.flush() |
|
|
85 |
time.sleep(0.01) |
|
|
86 |
except KeyboardInterrupt: |
|
|
87 |
sys.exit(0) |