示例#1
0
def main():
    global master
    global cookInfo
    global Heater

    #load settings from file
    cooker = CookProfile()
    with open('cookProfile.csv', 'rb') as cook:
        reader = csv.reader(cook)
        for cookInfo in reader:
            x = 1
    cook.close()
    cooker.setProfile(cookInfo)
    print "Load cookProfile.csv...Completed"
    master = setup()

    Heater = heaterSetup_mtk.HeaterConfig()  # initizalize class instance
    Heater.setTempLimit(master)
    Heater.setTcOffset(master)
    Heater.setColdJunction(master)
    Heater.setTcFilter(master)
    Heater.setPid(master, Heater.RpidReg, 9, Heater.Rpid)
    Heater.setPid(master, Heater.MpidReg, 9, Heater.Mpid)
    Heater.setPid(master, Heater.FpidReg, 9, Heater.Fpid)
    Heater.setPid(master, Heater.UpGratepidReg, 9, Heater.UpGratepid)
    Heater.setPid(master, Heater.LoGratepidReg, 9, Heater.LoGratepid)

    currentTime = 0
    rearTemp, midTemp, frontTemp, UpGrateTemp, LoGrateTemp = readTemp()
    button = 1
    while True:
        print "\n "
        print "Press %r to start | Cook time:%rs Platen Time:%rs RearSP:%r dC MidSP:%r dC" % (
            cooker.KeyControl, cooker.cookTime, cooker.platenTime,
            Heater.TemperatureSP[0], Heater.TemperatureSP[2])
        done = False

        while not done:
            Heater.setTemp(master)
            RearPV, MiddlePV, FrontPV, UpGratePV, LoGratePV = readTemp()
            rearTemp, midTemp = dMod(master, rearTemp, midTemp, RearPV,
                                     MiddlePV, FrontPV)
            UpGrateTemp, LoGrateTemp = dModGrate(master, UpGrateTemp,
                                                 LoGrateTemp, UpGratePV,
                                                 LoGratePV)
            button = master.execute(1, cst.READ_COILS, 2,
                                    1)  # (slave,function,address,output)
            button = button[0]

            done = keyScan(1, 15, button, 1, 1)
            if msvcrt.kbhit():
                Key = msvcrt.getch()
                done = keyScan(2, 15, 1, Key, cooker.KeyControl)

        openName = cooker.file_name + strftime("%Y%b%d_%H.%M.%S",
                                               time.localtime()) + ".csv"
        with open(openName, 'w') as log:
            fieldInfo = ('StartTime', 'CookTime', 'PlatenTime', 'CookZone',
                         'RearSP', 'MidSP', 'FrontSP', 'RearPID', 'MidPID',
                         'FrontPID')
            targetInfo = csv.DictWriter(log,
                                        delimiter=',',
                                        lineterminator='\n',
                                        fieldnames=fieldInfo)
            Infoheaders = dict((n, n) for n in fieldInfo)
            targetInfo.writerow(Infoheaders)
            targetInfo.writerow({
                'StartTime':
                strftime("Time: %Y %b %d %H:%M:%S", time.localtime()),
                'CookTime':
                cooker.cookTime,
                'PlatenTime':
                cooker.platenTime,
                'CookZone':
                cooker.cookZone,
                'RearSP':
                Heater.TemperatureSP[0],
                'MidSP':
                Heater.TemperatureSP[2],
                'FrontSP':
                Heater.TemperatureSP[4],
                'RearPID':
                Heater.Rpid,
                'MidPID':
                Heater.Mpid,
                'FrontPID':
                Heater.Fpid
            })

            fieldnames = ('Time', 'RearTemp', 'MiddleTemp', 'FrontTemp',
                          'UpGrate', 'LoGrate', 'RearPWM', 'UpPWM',
                          'MiddlePWM', 'LoPWM', 'FrontPWM')
            targetWriter = csv.DictWriter(log,
                                          delimiter=',',
                                          lineterminator='\n',
                                          fieldnames=fieldnames)
            headers = dict((n, n) for n in fieldnames)
            targetWriter.writerow(headers)

            flag = 0
            for x in range(0, 3):
                winsound.Beep(1000, 200)
                time.sleep(0.1)
            startTime = time.time()
            done = False
            while timeCal(startTime) < cooker.totalTime:
                if currentTime != int(timeCal(startTime)):
                    currentTime = int(timeCal(startTime))
                    RearPV, MiddlePV, FrontPV, UpGratePV, LoGratePV = readTemp(
                    )
                    pwm = Heater.readPWM(master)
                    targetWriter.writerow({
                        'Time': currentTime,
                        'RearTemp': RearPV,
                        'MiddleTemp': MiddlePV,
                        'FrontTemp': FrontPV,
                        'UpGrate': UpGratePV,
                        'LoGrate': LoGratePV,
                        'RearPWM': pwm[0],
                        'UpPWM': pwm[1],
                        'MiddlePWM': pwm[2],
                        'LoPWM': pwm[3],
                        'FrontPWM': pwm[4]
                    })
                    print "Rear heater: %r, Mid heater: %r, Front heater: %r, time: %r s\n" % (
                        RearPV, MiddlePV, FrontPV,
                        cooker.totalTime - currentTime)
                    if done:
                        break
                if timeCal(startTime) > cooker.totalTime - 6 and flag == 0:
                    flag = 1
                    for x in range(0, 2):
                        winsound.Beep(1000, 200)
                        time.sleep(0.1)
                button = master.execute(1, cst.READ_COILS, 2,
                                        1)  # (slave,function,address,output)
                button = button[0]
                done = keyScan(1, int(timeCal(startTime)), button, 1, 1)
                RearPV, MiddlePV, FrontPV, UpGratePV, LoGratePV = readTemp()
                rearTemp, midTemp = dMod(master, rearTemp, midTemp, RearPV,
                                         MiddlePV, FrontPV)
                UpGrateTemp, LoGrateTemp = dModGrate(master, UpGrateTemp,
                                                     LoGrateTemp, UpGratePV,
                                                     LoGratePV)
        log.close()
        winsound.Beep(1000, 1000)
示例#2
0
def main():
    status = [0, -1, -1]  # [ignore,device1,device2] up = 1, down = 0
    startTime = [0, 0, 0]
    ready = [0, 0, 0]
    sleepTimer = [0, 0, 0]
    motor1 = pinConfig()
    motor1.setGPIO("P9_15", "P9_12", "P9_25", "P9_28", "P9_14")
    motor1.setPin()

    motor2 = pinConfig()
    motor2.setGPIO("P8_17", "P8_15", "P9_29", "P9_31", "P8_13")
    motor2.setPin()

    if GPIO.input(motor1.upperSW):  # if on upperSW move down
        print "Motor1 @Upper switch..."
        status[1], startTime[1] = motor1.moveDOWN(1)
    elif GPIO.input(motor1.lowerSW):  # if on lowerSW move up
        print "Motor1 @Lower switch..."
        status[1], startTime[1] = motor1.moveUP(1)
    else:
        print "Motor1 @Middle..."
        status[1], startTime[1] = motor1.moveUP(1)

    if GPIO.input(motor2.upperSW):
        print "Motor2 @Upper switch..."
        status[2], startTime[2] = motor2.moveDOWN(2)
    elif GPIO.input(motor2.lowerSW):
        print "Motor2 @Lower switch..."
        status[2], startTime[2] = motor2.moveUP(2)
    else:
        print "Motor2 @Middle..."
        status[2], startTime[2] = motor2.moveUP(2)

    print "GPIO event enabled"
    GPIO.add_event_detect(motor1.upperSW, GPIO.RISING, bouncetime=1000)
    GPIO.add_event_detect(motor1.lowerSW, GPIO.RISING, bouncetime=1000)
    GPIO.add_event_detect(motor2.upperSW, GPIO.RISING, bouncetime=1000)
    GPIO.add_event_detect(motor2.lowerSW, GPIO.RISING, bouncetime=1000)

    try:
        while True:
            # print status
            if status[1] == 1:
                if GPIO.input(motor1.upperSW) and GPIO.event_detected(
                        motor1.upperSW):
                    print "Motor1 @Upper switch..."
                    startTime[1], sleepTimer[1] = motor1.moveStop(1)

                if ready[1] == 1:
                    status[1], startTime[1] = motor1.moveDOWN(1)
                    ready[1] = 0

                if timeCal(startTime[1]) > pinConfig.timeout:
                    startTime[1], sleepTimer[1] = motor1.moveStop(1)
                    status[1] = 0

            elif status[1] == 0:
                if GPIO.input(motor1.lowerSW) and GPIO.event_detected(
                        motor1.lowerSW):
                    print "Motor1 @Lower switch..."
                    startTime[1], sleepTimer[1] = motor1.moveStop(1)

                if ready[1] == 1:
                    status[1], startTime[1] = motor1.moveUP(1)
                    ready[1] = 0

                if timeCal(startTime[1]) > pinConfig.timeout:
                    startTime[1], sleepTimer[1] = motor1.moveStop(1)
                    status[1] = 1

            if status[2] == 1:
                if GPIO.input(motor2.upperSW) and GPIO.event_detected(
                        motor2.upperSW):
                    print "Motor2 @Upper switch..."
                    startTime[2], sleepTimer[2] = motor2.moveStop(2)

                if ready[2] == 1:
                    status[2], startTime[2] = motor2.moveDOWN(2)
                    ready[2] = 0

                if timeCal(startTime[2]) > pinConfig.timeout:
                    startTime[2], sleepTimer[2] = motor2.moveStop(2)
                    status[2] = 0

            elif status[2] == 0:
                if GPIO.input(motor2.lowerSW) and GPIO.event_detected(
                        motor2.lowerSW):
                    print "Motor2 @Lower switch..."
                    startTime[2], sleepTimer[2] = motor2.moveStop(2)

                if ready[2] == 1:
                    status[2], startTime[2] = motor2.moveUP(2)
                    ready[2] = 0

                if timeCal(startTime[2]) > pinConfig.timeout:
                    startTime[2], sleepTimer[2] = motor2.moveStop(2)
                    status[2] = 1

            if timeCal(sleepTimer[1]) > pinConfig.restTime:
                ready[1] = 1
            if timeCal(sleepTimer[2]) > pinConfig.restTime:
                ready[2] = 1

    except KeyboardInterrupt:
        motor1.moveStop()
        motor2.moveStop()
        GPIO.cleanup()
示例#3
0
def main():
    global status
    global motor
    status = -1  # up = 1, down = 0
    nextMove = -1
    startTime = 0
    sleepTimer = 1000
    motor = pinConfig()
    motor.setPin()

    if GPIO.input(motor.upperSW):  # if on upperSW move down
        print "@Upper switch..."
        nextMove, startTime = motor.moveDOWN()

    elif GPIO.input(motor.lowerSW):  # if on lowerSW move up
        print "@Lower switch..."
        nextMove, startTime = motor.moveUP()

    else:
        print "@Middle...no switch detected"
        nextMove, startTime = motor.moveUP()

    try:
        while True:
            #stop motor when upper switch trigger
            if GPIO.event_detected(motor.upperSW) and GPIO.input(
                    motor.upperSW):
                sleepTimer = motor.moveStop(motor.upperSW)
                status = 1
                print "stop1"

            #timeout condition @ upperSW
            elif GPIO.input(
                    motor.upperSW) and timeCal(startTime) > pinConfig.timeout:
                print "Motor @Upper Switch timeout..."
                sleepTimer = motor.moveStop(motor.upperSW)
                nextMove, startTime = motor.moveDOWN()
                status = 0

            #stop motor when lower switch trigger
            if GPIO.event_detected(motor.lowerSW) and GPIO.input(
                    motor.lowerSW):
                sleepTimer = motor.moveStop(motor.lowerSW)
                status = 1
                print "stop2"

            #timeout condition @ lowerSW
            elif GPIO.input(
                    motor.lowerSW) and timeCal(startTime) > pinConfig.timeout:
                print "Motor @Lower Switch timeout..."
                sleepTimer = motor.moveStop(motor.lowerSW)
                nextMove, startTime = motor.moveUP()
                status = 0

            # motor timeout without upperSW/lowerSW
            if nextMove == 0 and GPIO.input(motor.lowerSW):
                if timeCal(startTime) > pinConfig.swTime:
                    print "Motor timeout...no movement detected"
                    try:
                        sleepTimer = motor.moveStop(motor.upperSW)
                    except RuntimeError:
                        pass

                    print "stop3"
                    nextMove, startTime = motor.moveUP()
                    status = 0

            elif nextMove == 1 and GPIO.input(motor.upperSW):
                if timeCal(startTime) > pinConfig.swTime:
                    print "Motor timeout...no movement detected"
                    try:
                        sleepTimer = motor.moveStop(motor.lowerSW)
                    except RuntimeError:
                        pass

                    print "stop4"
                    nextMove, startTime = motor.moveDOWN()
                    status = 0

            # motor timeout without upperSW/lowerSW
            if timeCal(startTime) > pinConfig.timeout:
                print "Motor timeout...no switch detected"
                if nextMove == 0:
                    print "stop3"
                    sleepTimer = motor.moveStop(motor.upperSW)
                    print "rest3"
                    time.sleep(pinConfig.restTime)
                    nextMove, startTime = motor.moveDOWN()
                    status = 0

                elif nextMove == 1:
                    print "stop4"
                    sleepTimer = motor.moveStop(motor.lowerSW)
                    print "rest4"
                    time.sleep(pinConfig.restTime)
                    nextMove, startTime = motor.moveUP()
                    status = 0

            #switch direction after rest time lapse
            if nextMove == 0 and status == 1:
                if timeCal(sleepTimer) > pinConfig.restTime:
                    nextMove, startTime = motor.moveDOWN()
                    status = 0

            elif nextMove == 1 and status == 1:
                if timeCal(sleepTimer) > pinConfig.restTime:
                    nextMove, startTime = motor.moveUP()
                    status = 0

    except KeyboardInterrupt:
        motor.eStop()
        GPIO.cleanup()
示例#4
0
def main():
    global status
    global motor
    status = -1  # up = 1, down = 0
    nextMove = -1
    startTime = 0
    sleepTimer = 1000
    motor = pinConfig()
    motor.setPin()

    time.sleep(5)

    if GPIO.input(motor.upperSW):  # if on upperSW move down
        print "@Upper switch..."
        nextMove, startTime = motor.moveDOWN()
    elif GPIO.input(motor.lowerSW):  # if on lowerSW move up
        print "@Lower switch..."
        nextMove, startTime = motor.moveUP()
    else:
        print "@Middle..."
        nextMove, startTime = motor.moveUP()

    print "GPIO event enabled"
    GPIO.add_event_detect(motor.upperSW, GPIO.RISING, bouncetime=2000)
    GPIO.add_event_detect(motor.lowerSW, GPIO.RISING, bouncetime=2000)

    try:
        while True:
            #stop motor when switch trigger
            if GPIO.event_detected(motor.upperSW) and GPIO.input(
                    motor.upperSW):
                sleepTimer = motor.moveStop()
                status = 1

            #timeout condition @ upperSW
            elif GPIO.input(
                    motor.upperSW) and timeCal(startTime) > pinConfig.timeout:
                print "Motor timeout..."
                sleepTimer = motor.moveStop()
                time.sleep(pinConfig.restTime)
                status = 1
                if nextMove == 0:
                    nextMove, startTime = motor.moveDOWN()
                    status = 0
                elif nextMove == 1:
                    nextMove, startTime = motor.moveUP()
                    status = 0

            if GPIO.event_detected(motor.lowerSW) and GPIO.input(
                    motor.lowerSW):
                sleepTimer = motor.moveStop()
                status = 1

            #timeout condition @ lowerSW
            elif GPIO.input(
                    motor.lowerSW) and timeCal(startTime) > pinConfig.timeout:
                print "Motor timeout..."
                sleepTimer = motor.moveStop()
                time.sleep(pinConfig.restTime)
                status = 1
                if nextMove == 0:
                    nextMove, startTime = motor.moveDOWN()
                    status = 0
                elif nextMove == 1:
                    nextMove, startTime = motor.moveUP()
                    status = 0

            # motor timeout without upperSW/lowerSW
            if timeCal(startTime) > pinConfig.timeout:
                print "Motor timeout...no switch detected"
                sleepTimer = motor.moveStop()
                time.sleep(pinConfig.restTime)
                status = 1
                if nextMove == 0:
                    nextMove, startTime = motor.moveDOWN()
                    status = 0
                elif nextMove == 1:
                    nextMove, startTime = motor.moveUP()
                    status = 0

            #switch direction after rest time lapse
            if nextMove == 0 and status == 1:
                if timeCal(sleepTimer) > pinConfig.restTime:
                    nextMove, startTime = motor.moveDOWN()
                    status = 0

            elif nextMove == 1 and status == 1:
                if timeCal(sleepTimer) > pinConfig.restTime:
                    nextMove, startTime = motor.moveUP()
                    status = 0

    except KeyboardInterrupt:
        motor.eStop()
        GPIO.cleanup()
示例#5
0
def main():
    #global instruments
    global master
    global Heater
    master = setup()
    motor = motorSetup_mtk.motorConfig()
    motor.setPIDhalfSpeed(master)
    #motor.setPIDfullSpeed(instruments)
    #motor.setPIDincfullSpeed(instruments)


    Heater = heaterSetup_mtk.HeaterConfig()  #initizalize class instance
    Heater.setTempLimit(master)
    Heater.setTcOffset(master)
    Heater.setColdJunction(master)
    Heater.setTcFilter(master)
    Heater.setPid(master, Heater.RpidReg, 9, Heater.Rpid)
    Heater.setPid(master, Heater.MpidReg, 9, Heater.Mpid)
    Heater.setPid(master, Heater.FpidReg, 9, Heater.Fpid)
    Heater.setPid(master, Heater.UpGratepidReg, 9, Heater.UpGratepid)
    Heater.setPid(master, Heater.LoGratepidReg, 9, Heater.LoGratepid)

    homing_mtk.goHome(master, 1)
    motor.setPIDfullSpeed(master)
    setpoint_wp.movePlaten(master, CookProfile.hoodHeight)


    currentTime = 0
    rearTemp, midTemp, frontTemp, UpGrateTemp, LoGrateTemp = readTemp()
    button = 1
    while True:
        print "\n "
        print "Press %r to start new cycle cook time: %rs RearSP:%r dC MidSP:%r dC" % (
            CookProfile.KeyControl, CookProfile.cookTime, Heater.TemperatureSP[0], Heater.TemperatureSP[2])
        done = False

        while not done:
            Heater.setTemp(master)
            RearPV, MiddlePV, FrontPV, UpGratePV, LoGratePV = readTemp()
            rearTemp, midTemp = dMod(master, rearTemp, midTemp, RearPV, MiddlePV, FrontPV)
            UpGrateTemp, LoGrateTemp = dModGrate(master, UpGrateTemp, LoGrateTemp, UpGratePV, LoGratePV)
            button = master.execute(1,cst.READ_COILS,2,1) #(id, cst.WRITE_SINGLE_COIL, 35, output_value=state) #(slave,function,address,output)
            button = button[0]

            done = keyScan(1,button,1,1)
            if msvcrt.kbhit():
                Key = msvcrt.getch()
                done = keyScan(2,1,Key,CookProfile.KeyControl)


        #moving platen to setpoint
        setpoint_wp.movePlaten(master, CookProfile.stage1)

        filename = strftime(CookProfile.file_name, time.localtime())
        f = open(filename, "w")
        f.write("\n")
        f.write("\n")
        f.write(strftime("Time:%a, %Y%b%d %H:%M:%S\n", time.localtime()))
        f.write("\n")
        f.write("---------------------Cooking Profile------------------\n")
        f.write("\n")
        f.write("Cooktime:%r s RearSP: %r dC MidSP:%r dC \n" % (
            CookProfile.cookTime, Heater.TemperatureSP[0], Heater.TemperatureSP[2]))
        f.write("------------------------------------------------------\n")
        f.write("----Logged Data(Time-RearTemp,MiddleTmp,FrontTemp-----\n")
        f.write("------------------------------------------------------\n")
        gflag = 0
        flag = 0
        for x in range(0, 3):
            winsound.Beep(1000, 200)
            time.sleep(0.1)
        startTime = time.time()
        done = False
        while timeCal(startTime) < CookProfile.cookTime:
            if currentTime != int(timeCal(startTime)):
                currentTime = int(timeCal(startTime))
                RearPV, MiddlePV, FrontPV, UpGratePV, LoGratePV = readTemp()
                f.write("%r,%r,%r,%r,%r,%r\n" % (currentTime, RearPV, MiddlePV, FrontPV, UpGratePV, LoGratePV))
                print "Rear heater: %r, Mid heater: %r, Front heater: %r, time: %r s\n" % (
                    RearPV, MiddlePV, FrontPV, CookProfile.cookTime - currentTime)
                if done:
                    break
            if timeCal(startTime) > CookProfile.gapTime and gflag == 0:
                gflag = 1
                #moving platen to setpoint
                setpoint_wp.movePlaten(master, CookProfile.stage2)
            if timeCal(startTime) > CookProfile.cookTime - 6 and flag == 0:
                flag = 1
                for x in range(0, 2):
                    winsound.Beep(1000, 200)
                    time.sleep(0.1)
            button = master.execute(1,cst.READ_COILS,2,1) #(slave,function,address,output)
            button = button[0]
            done = keyScan(1,button,1,1)
            RearPV, MiddlePV, FrontPV, UpGratePV, LoGratePV = readTemp()
            rearTemp, midTemp = dMod(master, rearTemp, midTemp, RearPV, MiddlePV, FrontPV)
            UpGrateTemp, LoGrateTemp = dModGrate(master, UpGrateTemp, LoGrateTemp, UpGratePV, LoGratePV)
            #f.write("%r,%r,%r,%r,%r,%r\n" % (currentTime, RearPV, MiddlePV, FrontPV, UpGratePV, LoGratePV))
        winsound.Beep(1000, 1000)
        f.close()

        #moving platen to setpoint
        setpoint_wp.movePlaten(master, CookProfile.hoodHeight)
示例#6
0
def main():
    global status
    global motor
    status = -1  # up = 1, down = 0
    nextMove = -1
    startTime = 0
    sleepTimer = 1000
    motor = pinConfig()
    motor.setPin()

    time.sleep(5)

    if GPIO.input(motor.upperSW):  # if on upperSW move down
        print "@Upper switch..."
        nextMove, startTime = motor.moveDOWN()
    elif GPIO.input(motor.lowerSW):  # if on lowerSW move up
        print "@Lower switch..."
        nextMove, startTime = motor.moveUP()
    else:
        print "@Middle..."
        nextMove, startTime = motor.moveUP()

    print "GPIO event enabled"
    GPIO.add_event_detect(motor.upperSW, GPIO.RISING, bouncetime=2000)
    GPIO.add_event_detect(motor.lowerSW, GPIO.RISING, bouncetime=2000)

    try:
        while True:
            #stop motor when switch trigger
            if GPIO.event_detected(motor.upperSW) and GPIO.input(motor.upperSW):
                sleepTimer = motor.moveStop()
                status = 1

            #timeout condition @ upperSW
            elif GPIO.input(motor.upperSW) and timeCal(startTime) > pinConfig.timeout:
                print "Motor timeout..."
                sleepTimer = motor.moveStop()
                time.sleep(pinConfig.restTime)
                status = 1
                if nextMove == 0:
                    nextMove, startTime = motor.moveDOWN()
                    status = 0
                elif nextMove == 1:
                    nextMove, startTime = motor.moveUP()
                    status = 0

            if GPIO.event_detected(motor.lowerSW) and GPIO.input(motor.lowerSW):
                sleepTimer = motor.moveStop()
                status = 1

            #timeout condition @ lowerSW
            elif GPIO.input(motor.lowerSW) and timeCal(startTime) > pinConfig.timeout:
                print "Motor timeout..."
                sleepTimer = motor.moveStop()
                time.sleep(pinConfig.restTime)
                status = 1
                if nextMove == 0:
                    nextMove, startTime = motor.moveDOWN()
                    status = 0
                elif nextMove == 1:
                    nextMove, startTime = motor.moveUP()
                    status = 0

            # motor timeout without upperSW/lowerSW
            if timeCal(startTime) > pinConfig.timeout:
                print "Motor timeout...no switch detected"
                sleepTimer = motor.moveStop()
                time.sleep(pinConfig.restTime)
                status = 1
                if nextMove == 0:
                    nextMove, startTime = motor.moveDOWN()
                    status = 0
                elif nextMove == 1:
                    nextMove, startTime = motor.moveUP()
                    status = 0

            #switch direction after rest time lapse
            if nextMove == 0 and status == 1:
                if timeCal(sleepTimer) > pinConfig.restTime:
                    nextMove, startTime = motor.moveDOWN()
                    status = 0

            elif nextMove == 1 and status == 1:
                if timeCal(sleepTimer) > pinConfig.restTime:
                    nextMove, startTime = motor.moveUP()
                    status = 0


    except KeyboardInterrupt:
        motor.eStop()
        GPIO.cleanup()
示例#7
0
def main():
    status = [0, -1, -1]  # [ignore,device1,device2] up = 1, down = 0
    startTime = [0, 0, 0]
    ready = [0, 0, 0]
    sleepTimer = [0, 0, 0]
    motor1 = pinConfig()
    motor1.setGPIO("P9_15", "P9_12", "P9_25", "P9_28", "P9_14")
    motor1.setPin()

    motor2 = pinConfig()
    motor2.setGPIO("P8_17", "P8_15", "P9_29", "P9_31", "P8_13")
    motor2.setPin()

    if GPIO.input(motor1.upperSW):  # if on upperSW move down
        print "Motor1 @Upper switch..."
        status[1], startTime[1] = motor1.moveDOWN(1)
    elif GPIO.input(motor1.lowerSW):  # if on lowerSW move up
        print "Motor1 @Lower switch..."
        status[1], startTime[1] = motor1.moveUP(1)
    else:
        print "Motor1 @Middle..."
        status[1], startTime[1] = motor1.moveUP(1)

    if GPIO.input(motor2.upperSW):
        print "Motor2 @Upper switch..."
        status[2], startTime[2] = motor2.moveDOWN(2)
    elif GPIO.input(motor2.lowerSW):
        print "Motor2 @Lower switch..."
        status[2], startTime[2] = motor2.moveUP(2)
    else:
        print "Motor2 @Middle..."
        status[2], startTime[2] = motor2.moveUP(2)

    print "GPIO event enabled"
    GPIO.add_event_detect(motor1.upperSW, GPIO.RISING, bouncetime=1000)
    GPIO.add_event_detect(motor1.lowerSW, GPIO.RISING, bouncetime=1000)
    GPIO.add_event_detect(motor2.upperSW, GPIO.RISING, bouncetime=1000)
    GPIO.add_event_detect(motor2.lowerSW, GPIO.RISING, bouncetime=1000)

    try:
        while True:
            # print status
            if status[1] == 1:
                if GPIO.input(motor1.upperSW) and GPIO.event_detected(motor1.upperSW):
                    print "Motor1 @Upper switch..."
                    startTime[1], sleepTimer[1] = motor1.moveStop(1)

                if ready[1] == 1:
                    status[1], startTime[1] = motor1.moveDOWN(1)
                    ready[1] = 0

                if timeCal(startTime[1]) > pinConfig.timeout:
                    startTime[1], sleepTimer[1] = motor1.moveStop(1)
                    status[1] = 0

            elif status[1] == 0:
                if GPIO.input(motor1.lowerSW) and GPIO.event_detected(motor1.lowerSW):
                    print "Motor1 @Lower switch..."
                    startTime[1], sleepTimer[1] = motor1.moveStop(1)

                if ready[1] == 1:
                    status[1], startTime[1] = motor1.moveUP(1)
                    ready[1] = 0

                if timeCal(startTime[1]) > pinConfig.timeout:
                    startTime[1], sleepTimer[1] = motor1.moveStop(1)
                    status[1] = 1

            if status[2] == 1:
                if GPIO.input(motor2.upperSW) and GPIO.event_detected(motor2.upperSW):
                    print "Motor2 @Upper switch..."
                    startTime[2], sleepTimer[2] = motor2.moveStop(2)

                if ready[2] == 1:
                    status[2], startTime[2] = motor2.moveDOWN(2)
                    ready[2] = 0

                if timeCal(startTime[2]) > pinConfig.timeout:
                    startTime[2], sleepTimer[2] = motor2.moveStop(2)
                    status[2] = 0

            elif status[2] == 0:
                if GPIO.input(motor2.lowerSW) and GPIO.event_detected(motor2.lowerSW):
                    print "Motor2 @Lower switch..."
                    startTime[2], sleepTimer[2] = motor2.moveStop(2)

                if ready[2] == 1:
                    status[2], startTime[2] = motor2.moveUP(2)
                    ready[2] = 0

                if timeCal(startTime[2]) > pinConfig.timeout:
                    startTime[2], sleepTimer[2] = motor2.moveStop(2)
                    status[2] = 1

            if timeCal(sleepTimer[1]) > pinConfig.restTime:
                ready[1] = 1
            if timeCal(sleepTimer[2]) > pinConfig.restTime:
                ready[2] = 1


    except KeyboardInterrupt:
        motor1.moveStop()
        motor2.moveStop()
        GPIO.cleanup()
示例#8
0
文件: pidCtl.py 项目: awong36/mtk_pid
def main():
    global master
    global cookInfo
    global Heater

    #load settings from file
    cooker = CookProfile()
    with open('cookProfile.csv', 'rb') as cook:
        reader = csv.reader(cook)
        for cookInfo in reader:
            x = 1
    cook.close()
    cooker.setProfile(cookInfo)
    print "Load cookProfile.csv...Completed"
    master = setup()

    motor = motorSetup_mtk.motorConfig()
    motor.setPIDhalfSpeed(master)

    Heater = heaterSetup_mtk.HeaterConfig()  # initizalize class instance
    Heater.setTempLimit(master)
    Heater.setTcOffset(master)
    Heater.setColdJunction(master)
    Heater.setTcFilter(master)
    Heater.setPid(master, Heater.RpidReg, 9, Heater.Rpid)
    Heater.setPid(master, Heater.MpidReg, 9, Heater.Mpid)
    Heater.setPid(master, Heater.FpidReg, 9, Heater.Fpid)
    Heater.setPid(master, Heater.UpGratepidReg, 9, Heater.UpGratepid)
    Heater.setPid(master, Heater.LoGratepidReg, 9, Heater.LoGratepid)

    homing_mtk.goHome(master, 1)
    motor.setPIDfullSpeed(master)
    setpoint_wp.movePlaten(master, cooker.hoodHeight)

    currentTime = 0
    rearTemp, midTemp, frontTemp, UpGrateTemp, LoGrateTemp = readTemp()
    button = 1
    while True:
        print "\n "
        print "Press %r to start | Cook time:%rs Platen Time:%rs RearSP:%r dC MidSP:%r dC" % (
            cooker.KeyControl, cooker.cookTime, cooker.platenTime, Heater.TemperatureSP[0], Heater.TemperatureSP[2])
        done = False

        while not done:
            Heater.setTemp(master)
            RearPV, MiddlePV, FrontPV, UpGratePV, LoGratePV = readTemp()
            rearTemp, midTemp = dMod(master, rearTemp, midTemp, RearPV, MiddlePV, FrontPV)
            UpGrateTemp, LoGrateTemp = dModGrate(master, UpGrateTemp, LoGrateTemp, UpGratePV, LoGratePV)
            button = master.execute(1, cst.READ_COILS, 2, 1)  # (slave,function,address,output)
            button = button[0]

            done = keyScan(1, 15, button, 1, 1)
            if msvcrt.kbhit():
                Key = msvcrt.getch()
                done = keyScan(2, 15, 1, Key, cooker.KeyControl)


        # moving platen to setpoint
        setpoint_wp.movePlaten(master, cooker.cookZone)
        openName = cooker.file_name + strftime("%Y%b%d_%H.%M.%S", time.localtime()) + ".csv"
        with open(openName, 'w') as log:
            fieldInfo = (
                'StartTime', 'CookTime', 'PlatenTime', 'CookZone', 'RearSP', 'MidSP', 'FrontSP', 'RearPID', 'MidPID',
                'FrontPID')
            targetInfo = csv.DictWriter(log, delimiter=',', lineterminator='\n', fieldnames=fieldInfo)
            Infoheaders = dict((n, n) for n in fieldInfo)
            targetInfo.writerow(Infoheaders)
            targetInfo.writerow(
                {'StartTime': strftime("Time: %Y %b %d %H:%M:%S", time.localtime()), 'CookTime': cooker.cookTime,
                 'PlatenTime': cooker.platenTime, 'CookZone': cooker.cookZone, 'RearSP': Heater.TemperatureSP[0],
                 'MidSP': Heater.TemperatureSP[2], 'FrontSP': Heater.TemperatureSP[4], 'RearPID': Heater.Rpid,
                 'MidPID': Heater.Mpid, 'FrontPID': Heater.Fpid})

            fieldnames = (
                'Time', 'RearTemp', 'MiddleTemp', 'FrontTemp', 'UpGrate', 'LoGrate', 'RearPWM', 'UpPWM', 'MiddlePWM',
                'LoPWM', 'FrontPWM')
            targetWriter = csv.DictWriter(log, delimiter=',', lineterminator='\n', fieldnames=fieldnames)
            headers = dict((n, n) for n in fieldnames)
            targetWriter.writerow(headers)

            flag = 0
            for x in range(0, 3):
                winsound.Beep(1000, 200)
                time.sleep(0.1)
            startTime = time.time()
            done = False
            while timeCal(startTime) < cooker.totalTime:
                if currentTime != int(timeCal(startTime)):
                    currentTime = int(timeCal(startTime))
                    RearPV, MiddlePV, FrontPV, UpGratePV, LoGratePV = readTemp()
                    pwm = Heater.readPWM(master)
                    targetWriter.writerow(
                        {'Time': currentTime, 'RearTemp': RearPV, 'MiddleTemp': MiddlePV, 'FrontTemp': FrontPV,
                         'UpGrate': UpGratePV, 'LoGrate': LoGratePV, 'RearPWM': pwm[0], 'UpPWM': pwm[1],
                         'MiddlePWM': pwm[2], 'LoPWM': pwm[3], 'FrontPWM': pwm[4]})
                    print "Rear heater: %r, Mid heater: %r, Front heater: %r, time: %r s\n" % (
                        RearPV, MiddlePV, FrontPV, cooker.totalTime - currentTime)
                    if done:
                        break
                if timeCal(startTime) > cooker.totalTime - 6 and flag == 0:
                    flag = 1
                    for x in range(0, 2):
                        winsound.Beep(1000, 200)
                        time.sleep(0.1)
                button = master.execute(1, cst.READ_COILS, 2, 1)  # (slave,function,address,output)
                button = button[0]
                done = keyScan(1, int(timeCal(startTime)), button, 1, 1)
                RearPV, MiddlePV, FrontPV, UpGratePV, LoGratePV = readTemp()
                rearTemp, midTemp = dMod(master, rearTemp, midTemp, RearPV, MiddlePV, FrontPV)
                UpGrateTemp, LoGrateTemp = dModGrate(master, UpGrateTemp, LoGrateTemp, UpGratePV, LoGratePV)
        log.close()
        winsound.Beep(1000, 1000)


        # moving platen to setpoint
        setpoint_wp.movePlaten(master, cooker.hoodHeight)
示例#9
0
def main():
    global status
    global motor
    status = -1  # up = 1, down = 0
    nextMove = -1
    startTime = 0
    sleepTimer = 1000
    motor = pinConfig()
    motor.setPin()

    if GPIO.input(motor.upperSW):  # if on upperSW move down
        print "@Upper switch..."
        nextMove, startTime = motor.moveDOWN()

    elif GPIO.input(motor.lowerSW):  # if on lowerSW move up
        print "@Lower switch..."
        nextMove, startTime = motor.moveUP()

    else:
        print "@Middle...no switch detected"
        nextMove, startTime = motor.moveUP()


    try:
        while True:
            #stop motor when upper switch trigger
            if GPIO.event_detected(motor.upperSW) and GPIO.input(motor.upperSW):
                sleepTimer = motor.moveStop(motor.upperSW)
                status = 1
                print "stop1"

            #timeout condition @ upperSW
            elif GPIO.input(motor.upperSW) and timeCal(startTime) > pinConfig.timeout:
                print "Motor @Upper Switch timeout..."
                sleepTimer = motor.moveStop(motor.upperSW)
                nextMove, startTime = motor.moveDOWN()
                status = 0

            #stop motor when lower switch trigger
            if GPIO.event_detected(motor.lowerSW) and GPIO.input(motor.lowerSW):
                sleepTimer = motor.moveStop(motor.lowerSW)
                status = 1
                print "stop2"

            #timeout condition @ lowerSW
            elif GPIO.input(motor.lowerSW) and timeCal(startTime) > pinConfig.timeout:
                print "Motor @Lower Switch timeout..."
                sleepTimer = motor.moveStop(motor.lowerSW)
                nextMove, startTime = motor.moveUP()
                status = 0

            # motor timeout without upperSW/lowerSW
            if nextMove == 0 and GPIO.input(motor.lowerSW):
                if timeCal(startTime) > pinConfig.swTime:
                    print "Motor timeout...no movement detected"
                    try:
                        sleepTimer = motor.moveStop(motor.upperSW)
                    except RuntimeError:
                        pass

                    print "stop3"
                    nextMove, startTime = motor.moveUP()
                    status = 0


            elif nextMove == 1 and GPIO.input(motor.upperSW):
                if timeCal(startTime) > pinConfig.swTime:
                    print "Motor timeout...no movement detected"
                    try:
                        sleepTimer = motor.moveStop(motor.lowerSW)
                    except RuntimeError:
                        pass

                    print "stop4"
                    nextMove, startTime = motor.moveDOWN()
                    status = 0

            # motor timeout without upperSW/lowerSW
            if timeCal(startTime) > pinConfig.timeout:
                print "Motor timeout...no switch detected"
                if nextMove == 0:
                    print "stop3"
                    sleepTimer = motor.moveStop(motor.upperSW)
                    print "rest3"
                    time.sleep(pinConfig.restTime)
                    nextMove, startTime = motor.moveDOWN()
                    status = 0

                elif nextMove == 1:
                    print "stop4"
                    sleepTimer = motor.moveStop(motor.lowerSW)
                    print "rest4"
                    time.sleep(pinConfig.restTime)
                    nextMove, startTime = motor.moveUP()
                    status = 0


            #switch direction after rest time lapse
            if nextMove == 0 and status == 1:
                if timeCal(sleepTimer) > pinConfig.restTime:
                    nextMove, startTime = motor.moveDOWN()
                    status = 0

            elif nextMove == 1 and status == 1:
                if timeCal(sleepTimer) > pinConfig.restTime:
                    nextMove, startTime = motor.moveUP()
                    status = 0


    except KeyboardInterrupt:
        motor.eStop()
        GPIO.cleanup()