Dr. Arne Jachens

Set up the Raspberry Pi

no warranty for any of the provided information

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=200
        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

1HvcControl.py
2HvcHCSR04ultrasonic.py
3HvcMain.py
4HvcMotorDriver.py
5HvcOneWire.py
6HvcOperationMode.py
7HvcRaw2phys.py
8HvcReadSPI.py
9HvcSendI2C.py
10HvcSetGPIO.py
11HvcTables.py
12HvcWeather.py
13makeDoc.py

php

1HV_Admin_Login.php
2HV_colorMap.php
3HV_composeH2Oplot.php
4HV_config.php
5HV_H2Olevel.php
6HV_readOperationState.php
7HV_restart.php
8HV_serviceLog.php
9HV_setParameters.php
10HV_showHouse.php
11HV_showLog.php
12HV_showWeatherForecast.php
13HV_TempCal.php
14index.php
15readFilenames.php
16readNamedData.php

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