/Modules/Mechanical/WINDGAUGE02A/SW/DataLogger.py |
---|
0,0 → 1,102 |
#!/usr/bin/python |
# plot with >> plot 'last.txt' u 1:2 w l axes x1y1, 'last.txt' u 1:4 w l axes x1y2, 'last.txt' u 1:3 |
import os |
import time |
import datetime |
import sys |
import numpy as np |
from gps import * |
from pymlab import config |
import threading |
gpsd = None |
class GpsPoller(threading.Thread): |
def __init__(self): |
threading.Thread.__init__(self) |
global gpsd #bring it in scope |
gpsd = gps(mode=WATCH_ENABLE) |
self.current_value = None |
self.running = True |
def run(self): |
global gpsd |
while gpsp.running: |
gpsd.next() |
cfg = config.Config( |
i2c = { |
"port": 1, |
}, |
bus = [ |
{ |
"name": "rps", |
"type": "rps01", |
}, |
], |
) |
cfg.initialize() |
print "RPS01A logger" |
sensor = cfg.get_device("rps") |
try: |
angles = np.zeros(5) |
angles[4] = sensor.get_angle(verify = False) |
time.sleep(0.01) |
angles[3] = sensor.get_angle(verify = False) |
time.sleep(0.01) |
angles[2] = sensor.get_angle(verify = False) |
time.sleep(0.01) |
angles[1] = sensor.get_angle(verify = False) |
n = 0 |
speed = 0 |
AVERAGING = 50 |
filen = 'log%0.0f.txt'%time.time() |
f = open(filen,'w') |
os.remove("last.txt") |
os.symlink(filen, "last.txt") |
gpsp = GpsPoller() |
gpsp.start() |
while True: |
for i in range(AVERAGING): |
time.sleep(0.01) |
angles[0] = sensor.get_angle(verify = False) |
if (angles[0] + n*360 - angles[1]) > 300: |
n -= 1 |
angles[0] = angles[0] + n*360 |
elif (angles[0] + n*360 - angles[1]) < -300: |
n += 1 |
angles[0] = angles[0] + n*360 |
else: |
angles[0] = angles[0] + n*360 |
speed += (-angles[4] + 8*angles[3] - 8*angles[1] + angles[0])/12 |
angles = np.roll(angles, 1) |
speed = speed/AVERAGING |
g_spd = gpsd.fix.speed |
print "W_Spd: %0.2f \t Angle: %0.2f \t G_Spd %0.2f" % (speed, angles[0], g_spd) |
f.write("%0.2f %0.2f %0.2f %0.2f\r\n" %(time.time(), abs(speed), angles[0], g_spd)) |
f.flush() |
except KeyboardInterrupt: |
gpsp.running = False |
gpsp.join() |
sys.exit(0) |
/Modules/Mechanical/WINDGAUGE02A/SW/Data_analyser.ipynb |
---|
1,7 → 1,7 |
{ |
"metadata": { |
"name": "", |
"signature": "sha256:c35f5f2963a30c62e2bc2437fc0d2422404aec0dbc3c5526df0be8cecdef27ee" |
"signature": "sha256:9453f2d297004717b7b0ab5bb80d8d05d3ff319f7e609fa9565e753aa36cef87" |
}, |
"nbformat": 3, |
"nbformat_minor": 0, |
50,7 → 50,7 |
] |
} |
], |
"prompt_number": 24 |
"prompt_number": 3 |
}, |
{ |
"cell_type": "code", |
63,7 → 63,7 |
"language": "python", |
"metadata": {}, |
"outputs": [], |
"prompt_number": 14 |
"prompt_number": 4 |
}, |
{ |
"cell_type": "code", |
71,43 → 71,87 |
"input": [ |
"prev_val= dataset.value[0,2]\n", |
"n = 0\n", |
"angle = np.zeros_like(dataset.value)\n", |
"angle = np.zeros((dataset.shape[0]))\n", |
"for i in range(dataset.value.shape[0]):\n", |
" if (dataset.value[i,2] - prev_val) > 300: \n", |
" if (dataset.value[i,2] - prev_val) > 300:\n", |
" n -= 1\n", |
" angle[i] = dataset.value[i,2] + n*360\n", |
" prev_val = dataset.value[i,2]\n", |
" elif -(dataset.value[i,2] - prev_val) > 300: # compute angular speed in backward direction.\n", |
" n += 1\n", |
" angle[i] = dataset.value[i,2] - n*360\n", |
" prev_val = dataset.value[i,2]\n", |
" else:\n", |
" angle[i] = dataset.value[i,2] + n*360\n", |
" prev_val = dataset.value[i,2]" |
" prev_val = dataset.value[i,2]\n", |
" " |
], |
"language": "python", |
"metadata": {}, |
"outputs": [], |
"prompt_number": 19 |
"prompt_number": 6 |
}, |
{ |
"cell_type": "markdown", |
"metadata": {}, |
"source": [ |
"Five point difference numerical calculation. Source: http://mathfun528.blogspot.cz/2011/07/numerical-differentiation.html" |
] |
}, |
{ |
"cell_type": "code", |
"collapsed": false, |
"input": [ |
"angle" |
"angle_speed = np.zeros_like(angle)\n", |
"\n", |
"for i in range(2,angle.shape[0]-2):\n", |
" angle_speed[i] = (-angle[i + 2] + 8*angle[i + 1] - 8*angle[i - 1] + angle[i - 2])/12" |
], |
"language": "python", |
"metadata": {}, |
"outputs": [], |
"prompt_number": 7 |
}, |
{ |
"cell_type": "code", |
"collapsed": false, |
"input": [ |
"fig, ax1 = plt.subplots()\n", |
"\n", |
"ax2 = ax1.twinx()\n", |
"ax1.set_xlabel('Sample #')\n", |
"ax1.set_ylabel('Angle')\n", |
"ax2.set_ylabel('Angular speed')\n", |
"\n", |
"ax1.plot(dataset.value[:,0], angle,'b',dataset.value[:,0], dataset.value[:,2],'r')\n", |
"ax2.plot(dataset.value[:,0], angle_speed,'g')\n", |
"\n", |
"plt.show()" |
], |
"language": "python", |
"metadata": {}, |
"outputs": [], |
"prompt_number": 8 |
}, |
{ |
"cell_type": "code", |
"collapsed": false, |
"input": [ |
"print angle" |
], |
"language": "python", |
"metadata": {}, |
"outputs": [ |
{ |
"metadata": {}, |
"output_type": "pyout", |
"prompt_number": 20, |
"output_type": "stream", |
"stream": "stdout", |
"text": [ |
"array([[ 0., 0., 0.],\n", |
" [ 0., 0., 0.],\n", |
" [ 0., 0., 0.],\n", |
" ..., \n", |
" [ 0., 0., 0.],\n", |
" [ 0., 0., 0.],\n", |
" [ 0., 0., 0.]], dtype=float32)" |
"[ 3.64746094e+01 3.65405273e+01 3.77929688e+01 ..., -1.31529551e+05\n", |
" -1.31530452e+05 -1.31528452e+05]\n" |
] |
} |
], |
"prompt_number": 20 |
"prompt_number": 10 |
}, |
{ |
"cell_type": "code", |
115,6 → 159,38 |
"input": [], |
"language": "python", |
"metadata": {}, |
"outputs": [], |
"prompt_number": 4 |
}, |
{ |
"cell_type": "markdown", |
"metadata": {}, |
"source": [] |
}, |
{ |
"cell_type": "code", |
"collapsed": false, |
"input": [], |
"language": "python", |
"metadata": {}, |
"outputs": [], |
"prompt_number": 45 |
}, |
{ |
"cell_type": "code", |
"collapsed": false, |
"input": [], |
"language": "python", |
"metadata": {}, |
"outputs": [], |
"prompt_number": 45 |
}, |
{ |
"cell_type": "code", |
"collapsed": false, |
"input": [], |
"language": "python", |
"metadata": {}, |
"outputs": [] |
} |
], |
/Modules/Mechanical/WINDGAUGE02A/SW/wind_gauge.py |
---|
1,6 → 1,10 |
#!/usr/bin/python |
# Python library for RPS01A MLAB module with AS5048B I2C Magnetic position sensor. |
# MLAB meteostation wind speed gauge with magnetic rotation sensor. |
# This simple algorithm calculate difference between five time equidistant points during the rotation. The result is angular speed per time step. |
# Size of time-step could be varied depending on expected wind speed range to measure. |
# Algorithm should be expanded by Kalman filtering to minimize dependence on fast reading. |
# 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. |
#uncomment for debbug purposes |
#import logging |
9,6 → 13,7 |
import time |
import datetime |
import sys |
import numpy as np |
from pymlab import config |
#### Script Arguments ############################################### |
64,24 → 69,39 |
#### Data Logging ################################################### |
try: |
angles = np.zeros(5) |
angles[4] = sensor.get_angle(verify = False) |
time.sleep(0.01) |
angles[3] = sensor.get_angle(verify = False) |
time.sleep(0.01) |
angles[2] = sensor.get_angle(verify = False) |
time.sleep(0.01) |
angles[1] = sensor.get_angle(verify = False) |
n = 0 |
speed = 0 |
AVERAGING = 50 |
while True: |
# for i in range(10): |
angle1 = sensor.get_angle(verify = False) |
time.sleep(0.1) |
angle2 = sensor.get_angle(verify = False) |
time.sleep(0.1) |
angle3 = sensor.get_angle(verify = False) |
if (angle1 < angle2): |
speed = (angle2 - angle1)/0.01 |
else: |
speed = (360 - angle1 + angle2)/0.01 |
for i in range(AVERAGING): |
time.sleep(0.01) |
angles[0] = sensor.get_angle(verify = False) |
sys.stdout.write("Speed: " + str(speed) +"\t"+ str(angle1) +"\t"+ str(angle2) + "\t\tMagnitude: " + str(sensor.get_magnitude()) |
+ "\tAGC Value: " + str(sensor.get_agc_value()) + "\tDiagnostics: " + str(sensor.get_diagnostics()) + "\r\n") |
sys.stdout.flush() |
time.sleep(0.01) |
if (angles[0] + n*360 - angles[1]) > 300: |
n -= 1 |
angles[0] = angles[0] + n*360 |
elif (angles[0] + n*360 - angles[1]) < -300: # compute angular speed in backward direction. |
n += 1 |
angles[0] = angles[0] + n*360 |
else: |
angles[0] = angles[0] + n*360 |
speed += (-angles[4] + 8*angles[3] - 8*angles[1] + angles[0])/12 |
angles = np.roll(angles, 1) |
speed = speed/AVERAGING # apply averaging on acummulated value. |
print "Speed: %0.2f \t Total Angle: %0.2f \r\n" % (speed, angles[0]) |
except KeyboardInterrupt: |
sys.exit(0) |