-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsensor_lacrossTX23.py
160 lines (110 loc) · 4.72 KB
/
sensor_lacrossTX23.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
###########################################################################
# Sint Wind PI
# Copyright 2012 by Tonino Tarsi <[email protected]>
#
# Please refer to the LICENSE file for conditions
# Visit http://www.vololiberomontecucco.it
#
##########################################################################
"""This module defines the base sensor Nevio ."""
import threading
import time
import config
import random
import datetime
import sqlite3
from TTLib import *
import sys
import subprocess
import globalvars
import meteodata
import sensor_thread
import sensor
import RPi.GPIO as GPIO
import TTLib
import thread
from ctypes import *
import intervalmap
def get_wind_dir_text():
"""Return an array to convert wind direction integer to a string."""
return ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW']
class Sensor_LacrossTX23(sensor.Sensor):
# Connections PIN - USING BCM numbering convention !!!!!!
def __init__(self,cfg ):
threading.Thread.__init__(self)
sensor.Sensor.__init__(self,cfg )
self.libTX23 = cdll.LoadLibrary('./TX23/libTX23.so')
if ( self.libTX23.init() != 1 ):
log("Error initializing TX23 library.Try to continue")
self.cfg = cfg
self.bTimerRun = 0
self.rb_WindSpeed = TTLib.RingBuffer(self.cfg.number_of_measure_for_wind_average_gust_calculation)
self.currentiDir = None
self.active = True
self.start()
def run(self):
sleeptime = self.cfg.windmeasureinterval - self.__MEASURETIME
if sleeptime < 0 : sleeptime = 0
iDir = c_int()
iSpeed = c_int()
while self.active :
self.libTX23.getData(byref(iDir), byref(iSpeed),0)
self.currentiDir = iDir
#TTLib.log( "currentWind : " + str(currentWind))
currentSpeed = iSpeed * self.cfg.windspeed_gain + self.cfg.windspeed_offset
self.rb_WindSpeed.append(currentSpeed)
time.sleep(sleeptime)
def Detect(self):
return True
def SetTimer(self):
self.bTimerRun = 0
def GetCurretWindSpeedAndDir(self):
"""Get wind speed pooling __PIN_A ( may be an interrupt version later )."""
iDir = c_int()
iSpeed = c_int()
while self.active :
self.libTX23.getData(byref(iDir), byref(iSpeed),0)
self.currentiDir = iDir
#TTLib.log( "currentWind : " + str(currentWind))
currentSpeed = iSpeed * self.cfg.windspeed_gain + self.cfg.windspeed_offset
self.rb_WindSpeed.append(currentSpeed)
return currentSpeed,self.currentiDir
def GetData(self):
seconds = datetime.datetime.now().second
if ( seconds < 30 ):
time.sleep(30-seconds)
else:
time.sleep(90-seconds)
wind_ave,wind_gust = self.rb_WindSpeed.getMeanMax()
if ( wind_ave != None) :
wind_dir = self.currentiDir * 22.5
wind_dir_code = get_wind_dir_text()[self.currentiDir]
globalvars.meteo_data.status = 0
globalvars.meteo_data.last_measure_time = datetime.datetime.now()
globalvars.meteo_data.idx = globalvars.meteo_data.last_measure_time
globalvars.meteo_data.wind_ave = wind_ave
globalvars.meteo_data.wind_gust = wind_gust
globalvars.meteo_data.wind_dir = wind_dir
globalvars.meteo_data.wind_dir_code = wind_dir_code
if ( self.cfg.use_tmp36 ):
ch1 = self.libMCP.read_channel(1)
v1 = ch1 * (3300.0/1024.0)
temp = (v1 - 500.0) / 10.0
globalvars.meteo_data.temp_out = temp
sensor.Sensor.GetData(self)
if __name__ == '__main__':
configfile = 'swpi.cfg'
cfg = config.config(configfile)
ss = Sensor_LacrossTX23(cfg)
ss.active = False
while ( 1 ) :
speed, dir = ss.GetCurretWindSpeedAndDir()
temp = None
if ( cfg.use_tmp36 ):
ch1 = ss.libMCP.read_channel(1)
v1 = ch1 * (3300.0/1024.0)
temp = (v1 - 500.0) / 10.0
print "Speed:",speed,"Dir:",dir,"Temp;",temp
# ss.GetData()
# log( "Meteo Data - D : " + globalvars.meteo_data.wind_dir_code + " S : " + str(globalvars.meteo_data.wind_ave) + + " G : " + str(globalvars.meteo_data.wind_gust) )
# #print logData("http://localhost/swpi_logger.php")