Dr. Arne Jachens

Set up the Raspberry Pi

no warranty for any of the provided information

Keine Erläuterungen gefunden.

import  RPi.GPIO as  GPIO


class HvcMotorDriver:
    def  __init__(self):
        #
        self.MDPins={}
        self.MDPins['pinOpen'] = 18
        self.MDPins['pinShut'] = 17
        self.MDPos={}
        self.MDPos['Shut'] = 979
        self.MDPos['Open'] = 1450
        self.maxOperationTime = 2 # seconds
        
    def  setMode(self):
        #Setup GPIO
        #GPIO.setwarnings(False) #probably you already use HvcSetGPIO
        GPIO.setmode(GPIO.BCM)
        message = "HvcMotorDriver.setMode: \n"
        actuators={}
        for  pin in self.MDPins:
            GPIO.setup(self.MDPins[pin], GPIO.OUT)
            message = message + "GPIO "+pin+":\t"+str(self.MDPins[pin])+"\t=> OUT \n"
            #            actuators[pin] = 0
            #
            #        return actuators,message
            
        return message

    def  move(self,sensors,opMode):
        """ Enable operation when operationMode changes,
            for  a certain time,
            if the motor position is valid
        """
        if self.MDPos['Open'] > self.MDPos['Shut']: 
            minPos = self.MDPos['Shut']
            maxPos = self.MDPos['Open']
            direction = "asc"
        else:
            maxPos = self.MDPos['Shut']
            minPos = self.MDPos['Open']
            direction = "des"
            
        if sensors['posBp']>0.95*minPos and sensors['posBp']<10.05*maxPos:
            print(opMode['byPs'],self.operationModeLast)
            if opMode['byPs'] != self.operationModeLast:
                self.operationModeLast = opMode['byPs']
                self.operationTimer = self.maxOperationTime
            elif self.operationTimer > 0:
                self.operationTimer = self.operationTimer-1

        message = "opMode: "+opMode['byPs']+" timer= "+str(self.operationTimer)+" sirection= "+direction+"\n"
        # set GPIO pins for  operation
        if opMode['byPs']=="open" and  self.operationTimer>0:
            active = True
            if direction == "asc" and sensors['posBp']<self.MDPos['Open']:
                GPIO.output(self.MDPins['pinOpen'], GPIO.HIGH)
                GPIO.output(self.MDPins['pinShut'], GPIO.LOW)
            elif direction == "des" and sensors['posBp']>self.MDPos['Open']:
                GPIO.output(self.MDPins['pinOpen'], GPIO.HIGH)
                GPIO.output(self.MDPins['pinShut'], GPIO.LOW)
            else:
                # full stop
                GPIO.output(self.MDPins['pinOpen'], GPIO.HIGH)
                GPIO.output(self.MDPins['pinShut'], GPIO.HIGH)
                
        elif opMode['byPs']=="shut" and  self.operationTimer>0:
            active = True
            if direction == "asc" and sensors['posBp']>self.MDPos['Shut']:
                GPIO.output(self.MDPins['pinOpen'], GPIO.LOW)
                GPIO.output(self.MDPins['pinShut'], GPIO.HIGH)
            elif direction == "des" and sensors['posBp']<self.MDPos['Shut']:
                GPIO.output(self.MDPins['pinOpen'], GPIO.LOW)
                GPIO.output(self.MDPins['pinShut'], GPIO.HIGH)
            else:
                # full stop
                GPIO.output(self.MDPins['pinOpen'], GPIO.HIGH)
                GPIO.output(self.MDPins['pinShut'], GPIO.HIGH)
                
        elif opMode['byPs']=="stop":
            active = False
            GPIO.output(self.MDPins['pinOpen'], GPIO.LOW)
            GPIO.output(self.MDPins['pinShut'], GPIO.LOW)
            
        else:
            # inactive
            active = False
            GPIO.output(self.MDPins['pinOpen'], GPIO.LOW)
            GPIO.output(self.MDPins['pinShut'], GPIO.LOW)
            
        print(message)
        return active
    
#persistant values:
HvcMotorDriver.operationTimer=0
HvcMotorDriver.operationModeLast=""


if __name__ == "__main__":
    import  time
    from HvcReadSPI import  HvcReadSPI
    
    myMD = HvcMotorDriver()
    msg = myMD.setMode()
    print(msg)
    
    opMode = {}
    opMode['byPs']="open" #     <<===  set target
    myMD.operationModeLast="shut"
    #opMode['byPs']="shut" #     <<===  set target
    #myMD.operationModeLast="open"
    #opMode['byPs']="stop" #     <<===  set target

    # myIO = HvcSetGPIO()
    mySPI = HvcReadSPI()
    ADC,msg = mySPI.readADCverbose()
    print(msg)
    while True:
        ADC = mySPI.readADC()
        sensors={}
        sensors['posBp']=ADC['posBp']
        active = myMD.move(sensors,opMode)
        for  pin in myMD.MDPins:
            foo = GPIO.input(myMD.MDPins[pin])
            print(pin,foo)
            
        if active:
            print(opMode['byPs'],sensors['posBp'])

        time.sleep(1)

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.