From c10467ea59785e566c81cc6b8f2c32b0573bf73d Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Sun, 2 Oct 2005 00:33:06 +0000 Subject: [PATCH] Agrega SensorDireccionViento. --- src/sensor.py | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/sensor.py diff --git a/src/sensor.py b/src/sensor.py new file mode 100644 index 0000000..e677b6b --- /dev/null +++ b/src/sensor.py @@ -0,0 +1,126 @@ +#!/usr/bin/env python +# -*- coding: iso-8859-1 -*- +# vim: set expandtab tabstop=4 shiftwidth=4 : +#---------------------------------------------------------------------------- +# Weathemulator +#---------------------------------------------------------------------------- +# This file is part of weathemulator. +# +# weathemulator is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the Free +# Software Foundation; either version 2 of the License, or (at your option) +# any later version. +# +# weathemulator is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with weathemulator; if not, write to the Free Software Foundation, Inc., 59 +# Temple Place, Suite 330, Boston, MA 02111-1307 USA +#---------------------------------------------------------------------------- +# Creado: sáb oct 1 18:26:32 ART 2005 +# Autores: Leandro Lucarella +#---------------------------------------------------------------------------- + +import time +import math +import random +import threading +from sync import * + +class Sensor(Synchronizable, threading.Thread): + + valorActual = SyncProp("Valor actualmente medido por el sensor") + + def __init__(self, sigma, periodo): + threading.Thread.__init__(self) + self.rnd = random.Random() + self.sigma = sigma + self.periodo = periodo + self.mu = self.initMu() + self.valorActual = self.getValor() + + def initMu(self): + raise NotImplementedError # Abstracto + + def getValor(self): + "Simula la medición del dispositivo físico." + self.mu = self.rnd.gauss(self.mu, self.sigma) + return self.mu + + def run(self): + while 1: + # Hacer algún signal al monitor de los sensores + self.valorActual = self.getValor() + print self.valorActual + time.sleep(self.periodo) + + def __str__(self): + return self.valorActual + + def __repr__(self): + return "<%s valorActual=%s>" % (self.__class__.__name__, self) + + +class SensorDireccionViento(Sensor): + + N = 0 + NE = 1 + E = 2 + SE = 3 + S = 4 + SO = 5 + O = 6 + NO = 7 + ROSA = (N, NE, E, SE, S, SO, O, NO) + + def __init__(self): + Sensor.__init__(self, sigma=0.01, periodo=0.1) + + def rndAngle(self, base, arco): + return (base + arco * (self.rnd.random() - 0.5)) % math.pi + + def initMu(self): + return self.rndAngle(0, 2 * math.pi) + + def getValor(self): + self.mu = self.rndAngle(self.mu, self.sigma) + return SensorDireccionViento.ROSA[int(round(self.mu))] + + def direccionToString(direccion): + if direccion == 0: + return "Norte" + if direccion == 1: + return "Noreste" + if direccion == 2: + return "Este" + if direccion == 3: + return "Sudeste" + if direccion == 4: + return "Sur" + if direccion == 5: + return "Sudoeste" + if direccion == 6: + return "Oeste" + if direccion == 7: + return "Noroeste" + raise NameError + direccionToString = staticmethod(direccionToString) + + def __str__(self): + return SensorDireccionViento.direccionToString(self.valorActual) + + def __repr__(self): + return "<%s dirección=%s valorActual=%d>" % (self.__class__.__name__, + self, self.valorActual) + + +if __name__ == '__main__': + DEBUG = True + sensor = SensorDireccionViento() + sensor.setDaemon(True) + sensor.start() + time.sleep(3) + -- 2.43.0