Rev Author Line No. Line
4413 kaklik 1 #!/usr/bin/python
2  
4452 kaklik 3 # MLAB meteostation wind speed gauge with magnetic rotation sensor.
4 # This simple algorithm calculate difference between five time equidistant points during the rotation. The result is angular speed per time step.
5 # Size of time-step could be varied depending on expected wind speed range to measure.
6 # Algorithm should be expanded by Kalman filtering to minimize dependence on fast reading.
7 # The measuring principle could introduce time-stamped reading to increase precision of measurement. It could be possible because the readings are not exactly time equidistant in real Linux word.
4413 kaklik 8  
9 #uncomment for debbug purposes
10 #import logging
11 #logging.basicConfig(level=logging.DEBUG)
12  
13 import time
14 import datetime
15 import sys
4423 kaklik 16 import numpy as np
4413 kaklik 17 from pymlab import config
18  
19 #### Script Arguments ###############################################
20  
21 if len(sys.argv) != 2:
22 sys.stderr.write("Invalid number of arguments.\n")
23 sys.stderr.write("Usage: %s PORT ADDRESS\n" % (sys.argv[0], ))
24 sys.exit(1)
25  
26 port = eval(sys.argv[1])
27 #### Sensor Configuration ###########################################
28  
29 ''''
30 cfg = config.Config(
31 i2c = {
32 "port": port,
33 },
34  
35 bus = [
36 {
37 "type": "i2chub",
38 "address": 0x72,
39  
40 "children": [
41 {"name": "encoder", "type": "rps01", "channel": 1, }
42 ],
43 },
44 ],
45 )
46  
47 '''
48 cfg = config.Config(
49 i2c = {
50 "port": port,
51 },
52 bus = [
53 {
54 "name": "encoder",
55 "type": "rps01",
56 },
57 ],
58 )
59  
60  
61 cfg.initialize()
62  
63 print "RPS01A magnetic position sensor RPS01 readout example \r\n"
64 sensor = cfg.get_device("encoder")
65  
66 print sensor.get_address()
67 print sensor.get_zero_position()
68  
69 #### Data Logging ###################################################
70  
71 try:
4423 kaklik 72 angles = np.zeros(5)
73 angles[4] = sensor.get_angle(verify = False)
4451 kaklik 74 time.sleep(0.01)
4423 kaklik 75 angles[3] = sensor.get_angle(verify = False)
4451 kaklik 76 time.sleep(0.01)
4423 kaklik 77 angles[2] = sensor.get_angle(verify = False)
4451 kaklik 78 time.sleep(0.01)
4423 kaklik 79 angles[1] = sensor.get_angle(verify = False)
80 n = 0
4451 kaklik 81 speed = 0
82 AVERAGING = 50
4423 kaklik 83  
4413 kaklik 84 while True:
4451 kaklik 85 for i in range(AVERAGING):
86 time.sleep(0.01)
87 angles[0] = sensor.get_angle(verify = False)
88  
89 if (angles[0] + n*360 - angles[1]) > 300:
90 n -= 1
91 angles[0] = angles[0] + n*360
92  
93 elif (angles[0] + n*360 - angles[1]) < -300: # compute angular speed in backward direction.
94 n += 1
95 angles[0] = angles[0] + n*360
96  
97 else:
98 angles[0] = angles[0] + n*360
99  
100 speed += (-angles[4] + 8*angles[3] - 8*angles[1] + angles[0])/12
101 angles = np.roll(angles, 1)
102  
103 speed = speed/AVERAGING # apply averaging on acummulated value.
104 print "Speed: %0.2f \t Total Angle: %0.2f \r\n" % (speed, angles[0])
105  
4413 kaklik 106 except KeyboardInterrupt:
107 sys.exit(0)