Dr. Arne JachensDr. Arne Jachens

steppermotor

Raspberry_L298.svg

To operate the bypass one pin for opening and one for closing is used. If both pins should be switched on, this indicates a fast stop to the motor, defined by the full-bridge driver L298.

I experienced that the L298 comes with a voltage drop of 3 V. Therefore, to drive the Bosch VMC 24V of my Bypass, I need a voltage supply of 29 V at least.

Python interface

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)