Hamburger Hamburger

Heating and Ventilation Control

/HvcHCSR04ultrasonic

Keine Erläuterungen gefunden.

import  numpy as  np
import  datetime
import  time
import  RPi.GPIO as  GPIO

class HvcHCSR04ultrasonic:
    def  __init__(self):
        self.GPIO_Trig = 15
        self.GPIO_Echo = 14
        self.speed = 34300 # cm/s
        self.depth = 199   # cm
        self.hMax  = 170   # cm
        # my reservoir:
        # D=2.1m; Hmax=1.7m; A=3.46m^2; V=5.89m^3
        
        GPIO.setwarnings(False) #probably you already use HvcSetGPIO
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(self.GPIO_Trig, GPIO.OUT)
        GPIO.setup(self.GPIO_Echo, GPIO.IN)




        
    def  distance(self):
        # set trigger HIGH
        GPIO.output(self.GPIO_Trig, GPIO.HIGH)
        # set trigger LOW again to initiate measurement
        time.sleep(0.00001)
        GPIO.output(self.GPIO_Trig, GPIO.LOW)
        start = time.time()
        stop = time.time()
        timeOut = time.time()
        failure=False
        # measurement starts, once ECHO is set HIGH
        while GPIO.input(self.GPIO_Echo) == 0:
            start = time.time()
            if start>timeOut+0.3:
                failure=True
                failureType="Trigger"
                break
        # measurement ends, when ECHO is set LOW again
        while GPIO.input(self.GPIO_Echo) == 1:
            stop = time.time()
            if stop>start+0.3:
                failure=True
                failureType="Echo"
                break
            
        # compute the distance, sound travels forth and back
        if failure:
            print("ERROR: ultrasonic",failureType,timeOut,start,stop)
            dist = -99
        else:
            elapsedTime = stop - start
            dist = (elapsedTime * self.speed) / 2.0
        #print(dist)
                
        return dist


    def  filter(self,dist):
        if dist<=0:
            #too short, not plausible
            pass
        elif dist>=self.depth:
            #too long, not plausible
            pass
        elif HvcHCSR04ultrasonic.distFlt == -1.0:
            HvcHCSR04ultrasonic.distFlt = dist
        else:
            w=0.1
            lowPass = (1.0-w)*HvcHCSR04ultrasonic.distFlt + w*dist
            HvcHCSR04ultrasonic.distFlt = lowPass
            
        return HvcHCSR04ultrasonic.distFlt

    def  volume(self,dist):
        #distance to height
        hRel = (self.depth-dist)/self.hMax * 100.0
        return hRel
    
        
#persistant variables:
HvcHCSR04ultrasonic.distFlt = -1.0



if __name__ == "__main__":
    H2O = HvcHCSR04ultrasonic()
    dist = H2O.distance()
    distFlt = H2O.filter(dist)
    fill = H2O.volume(distFlt)
    print(dist,distFlt,fill)

python

1/HvcLightControl.py
2/HvcHCSR04ultrasonic.py
3/HvcPV.py
4/HvcMotorDriver.py
5/HvcRollerShutter.py
6/manGenMqttMap.py
7/HvcReadSPI.py
8/HvcMqtt.py
9/HvcTables.py
10/HvcMain.py
11/HvcSetGPIO.py
12/HvcWifiRelay.py
13/HvcOperationMode.py
14/HvcControl.py
15/HvcRaw2phys.py
16/HvcWeather.py
17/HvcOneWire.py
18/makeDoc.py
19/HvcFronius.py
20/EnergyManager.py
21/HvcSendI2C.py

php

1/HV_colorMap.php
2/HV_Admin_Login.php
3/HV_readOperationState.php
4/HV_setParameters.php
5/HV_config.php
6/EM_handleJSON.php
7/index.php
8/readFilenames.php
9/HV_restart.php
10/HV_moveGate.php
11/HV_showLog.php
12/HV_RollerShutter.php
13/EM_editParameter.php
14/HV_serviceLog.php
15/HV_H2Olevel.php
16/HV_TempCal.php
17/HV_Fronius.php
18/EM_plot.php
19/readNamedData.php
20/HV_composeH2Oplot.php
21/HVdoc.php
22/HV_showWeatherForecast.php
23/HV_showHouse.php

Der gesamte Sourcecode darf gemäß GNU General Public License weiterverbreitet werden.