Problem with comparison.
/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 |
---|
0,0 → 1,200 |
{ |
"metadata": { |
"name": "", |
"signature": "sha256:9453f2d297004717b7b0ab5bb80d8d05d3ff319f7e609fa9565e753aa36cef87" |
}, |
"nbformat": 3, |
"nbformat_minor": 0, |
"worksheets": [ |
{ |
"cells": [ |
{ |
"cell_type": "code", |
"collapsed": false, |
"input": [ |
"import h5py\n", |
"import numpy as np\n", |
"import matplotlib.pyplot as plt" |
], |
"language": "python", |
"metadata": {}, |
"outputs": [], |
"prompt_number": 1 |
}, |
{ |
"cell_type": "code", |
"collapsed": false, |
"input": [ |
"file = h5py.File('test_rps.hdf5', 'r') # 'r' means that hdf5 file is open in read-only mode\n", |
"dataset = file['RPS01']" |
], |
"language": "python", |
"metadata": {}, |
"outputs": [], |
"prompt_number": 2 |
}, |
{ |
"cell_type": "code", |
"collapsed": false, |
"input": [ |
"print dataset.value[4,2]" |
], |
"language": "python", |
"metadata": {}, |
"outputs": [ |
{ |
"output_type": "stream", |
"stream": "stdout", |
"text": [ |
"36.7822\n" |
] |
} |
], |
"prompt_number": 3 |
}, |
{ |
"cell_type": "code", |
"collapsed": false, |
"input": [ |
"plt.plot( dataset.value[:,0], dataset.value[:,2], color = 'r')\n", |
"#plt.xlim([1.45415117E9,1.4541513E9])\n", |
"plt.show()" |
], |
"language": "python", |
"metadata": {}, |
"outputs": [], |
"prompt_number": 4 |
}, |
{ |
"cell_type": "code", |
"collapsed": false, |
"input": [ |
"prev_val= dataset.value[0,2]\n", |
"n = 0\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", |
" 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]\n", |
" " |
], |
"language": "python", |
"metadata": {}, |
"outputs": [], |
"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_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": [ |
{ |
"output_type": "stream", |
"stream": "stdout", |
"text": [ |
"[ 3.64746094e+01 3.65405273e+01 3.77929688e+01 ..., -1.31529551e+05\n", |
" -1.31530452e+05 -1.31528452e+05]\n" |
] |
} |
], |
"prompt_number": 10 |
}, |
{ |
"cell_type": "code", |
"collapsed": false, |
"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": [] |
} |
], |
"metadata": {} |
} |
] |
} |
/Modules/Mechanical/WINDGAUGE02A/SW/wind_gauge.py |
---|
0,0 → 1,107 |
#!/usr/bin/python |
# 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 |
#logging.basicConfig(level=logging.DEBUG) |
import time |
import datetime |
import sys |
import numpy as np |
from pymlab import config |
#### Script Arguments ############################################### |
if len(sys.argv) != 2: |
sys.stderr.write("Invalid number of arguments.\n") |
sys.stderr.write("Usage: %s PORT ADDRESS\n" % (sys.argv[0], )) |
sys.exit(1) |
port = eval(sys.argv[1]) |
#### Sensor Configuration ########################################### |
'''' |
cfg = config.Config( |
i2c = { |
"port": port, |
}, |
bus = [ |
{ |
"type": "i2chub", |
"address": 0x72, |
"children": [ |
{"name": "encoder", "type": "rps01", "channel": 1, } |
], |
}, |
], |
) |
''' |
cfg = config.Config( |
i2c = { |
"port": port, |
}, |
bus = [ |
{ |
"name": "encoder", |
"type": "rps01", |
}, |
], |
) |
cfg.initialize() |
print "RPS01A magnetic position sensor RPS01 readout example \r\n" |
sensor = cfg.get_device("encoder") |
print sensor.get_address() |
print sensor.get_zero_position() |
#### 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(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: # 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) |
/Modules/Mechanical/WINDGAUGE02A/SW/test_rps.hdf5 |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Modules/Mechanical/WINDGAUGE02A/SW/test_rps.log.hdf5 |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |