#!/usr/bin/env/python from IOPi import IOPi import time import RPi.GPIO as GPIO import piplates.RELAYplate as RELAY #Program Parameters verbose = False noGlue = False loopsForPrint = 10000 #When prining enabled, print after this many loops version = 110 ## 105 Changed AJU2 glue speed to 11 ## 106 R1 sart pos +3mm ## 107 R2 start pos +3 R1 stop pos +3 ## 108 Changed AJU1 glue speed to 11 ## 109 Added Glue Dispense Counter ## 110 Added Performance Tracker #-----------System Level Setup----------------# watchDogState = 0 nextWatchDogState = 0 sysState = 0 nextSysState = 0 recoveryState = 0 #When pause button is pressed, restore the system to this state #-----------AJU Task Setup--------------------# AJUisPresent = [True,True] bus = [IOPi(0x20),IOPi(0x21)] AJUsafeFlags = [True, True] nextAJUState = [0,0] AJUEnableCMD = [False,False] AJUEnableDone =[False,False] AJUisReady = [False,False] AJUfinishCMD = [False,False] stopperIsUp = [False,False] glueStartTime = time.time() glueMove = False glueReady = True glueStart = [True,True] glueClear = [True,True] AJUStopperCMD = [False,False] AJUSwitchTime = [time.time(), time.time()] AJUStateDelays = [1,0,.5,0,0,0,25,.5,0,0,0,0] # Bit Number 1 2 3 4 5 6 7 # | | | | | | | #RELAY 1: Pusher 1+2 _/ | | | | | | #RELAY 2: Holder 1 ___/ | | | | | #RELAY 3: Holder 2 _____/ | | | | #RELAY 4: Stopper _______/ | | | #RELAY 5: Motor Enable _________/ | | #RELAY 6: Motor Position___________/ | #RELAY 7: NOT USED _____________/ busRemap = [12,10,8,6,4,2,7,5,3,1,14,16] for i in range(1,17): #Set all IOPi Plus pins as inputs bus[0].set_pin_direction(i,1) bus[1].set_pin_direction(i,1) #-----------Glue Task Setup-------------------# GPIO.setmode(GPIO.BCM) ready = 4 #GPIO pin number for slide ready input moving = 5 # GPIO pin number for MOVE output freeOut = 6 alarmIn = 27 homeEndIn = 17 pulseCW = 26 #GPIO pin number for CW pulses pulseCCW = 8 #GPIO pin number for CCW pulses dispense = 12 #GPIO pin to send dispense command to Arduino glue2ready = 20 #GPIO pin to send ready command to Arduino glueEnablePin = 16 #GPIO pin to send Enable to Arduino (was physical switch) GPIOZHOME = 13 #GPIO pin to send ZHOME command to slide goButton = 15 #GPIO pin number for 'Go' button pauseButton = 14 #GPIO pin number for 'Stop' button glueReady = 21 #GPIO pin to receive ready signal from Arduino glueReplaceIn = 19 #GPIO pin to receive the low glue warning from the Arduino thisTime = time.time() lastTime = time.time() pulseCheckTime = time.time() cycleStartTime = time.time() cycleStopTime = time.time() sysStateChangeTime = time.time() pausePressTime = time.time() pulseDuration = 100 systemIsPaused = False ignorePause = False alarmActive = False infiniteLoop = True inputs = [ready, moving,alarmIn, homeEndIn,glueReady,glueReplaceIn] # List of all inputs for later initialization outputs = [pulseCCW, pulseCW, freeOut, GPIOZHOME, dispense, glue2ready,glueEnablePin] relayOnTime = 0.1 waitForSlideTime = 0.2 glueDripStartDelay = 1 glueDripStopDelay = 0.5 pressureWarningCooldown = time.time() initializeTime = time.time() last10startTime = time.time() pulseEdgeTime = time.time() glueOnTime = time.time() glueOffTime = time.time() glueTimeout = 5 #Timer to shut off glue automatically after 4 seconds if not already turned off pulseState = 0 #Current State of the pulse PWM signal nextPulseState = 0 #Next State of the pulse PWM signal moveState = 0 nextMoveState = 0 destination = 0 direction = 0 glueLevel = 0 #How much glue is left. 0 = normal, 1 = replace soon, 2 = replace now dispRemaining = 100 #Approximately how many dispenses can be completed before the glue must be replaced glueRefPos = 0 #When passing over AJU, this stores the position for that the glue timing shall be relative to startGlue = 87 #Distance from glueRefPos (mm) to begin gluing stopGlue = 117 #Distance from glueRefPos (mm) to stop gluing allowFlip = 270 #Distance from glueRefPos (mm) to allow flipping startGluePosOffset = 5 stopGluePosOffset = 5 allowFlipRev = 0 startGlueRev = allowFlip-stopGlue-10 stopGlueRev = allowFlip-startGlue-20 #Define the key slide positions based on the positions of each AJU (pos1 and pos3 should change if the AJUs are moved) pos1 = 10 pos1a = pos1+startGlue pos1b = pos1 + stopGlue pos2 = pos1+ allowFlip pos3 = 310 pos3a = pos3+startGlue pos3b = pos3+stopGlue pos4 = pos3+allowFlip testOffset = 0 inPOS = False sendHomeFlag = False inHome = False motionParamsSet = False pulseEntryFlag = False AJUclear = True blockAJU = [True,True] #When slide is moving or planning a move, use these flags to block motion of the individual AJUs dispenseSent = False glueIsEnabled = False glueSentReady = False slideInHome = False pauseRequest = False reverseRequest = False pauseForGlueReplace = False airPressureWarning = False absDispenseCounter = 0 dispenseCounter = 0 AJUreadyCMD = [False,False] gluePosCMD = 0 #Glue Station commanded by scheduler gluePos = 0 #Glue Station reported by moveTask glueHomeCMD = False startGlueCMD = False moveTaskDone = False AJUtaskDone = [False,False] continueAfterGlueReady = False absPulseCount = 0 #Slide Encoder Counts (Relative to Home) loopCount = 1 #moveState=0 glueState = 0 AJUStates = [0,0] pauseStateEntryFlag = False for i in inputs: #Configure Inputs GPIO.setup(i,GPIO.IN) GPIO.setup(goButton,GPIO.IN, pull_up_down =GPIO.PUD_UP) GPIO.setup(pauseButton,GPIO.IN, pull_up_down =GPIO.PUD_UP) for i in outputs: #Configure Outputs GPIO.setup(i,GPIO.OUT) GPIO.output(GPIOZHOME, GPIO.HIGH) GPIO.output(freeOut, GPIO.HIGH) GPIO.output(pulseCCW, GPIO.HIGH) GPIO.output(pulseCW, GPIO.HIGH) #Returns a 16bit integer whose bits correspond with the sensor outputs def readSensorState(AJUAddr): sensorState = 0 for i in range(len(busRemap)): j = busRemap[i] sensorState = sensorState*2 + bus[AJUAddr].read_pin(j) return sensorState #This function will print information to the screen only if the verbose flag is true def printDebug(message): if verbose: print(message) #Sets only the first 6 relays of the designated Relay Plate (leaves relay 7 as is) def setAJURelays(AJUAddr,reState): reState = reState + (RELAY.relaySTATE(AJUAddr) & 0b1000000)*64 RELAY.relayALL(AJUAddr, reState) def timeCheck(AJUAddr,delay): return (time.time()-AJUSwitchTime[AJUAddr]>delay) def AJUTask(AJUAddr): global nextAJUState global AJUEnableCMD global AJUisReady global AJUsafeFlags global glueStartTime global AJUSwitchTime global AJUtaskDone global AJUEnableDone global stopperIsUp global airPressureWarning state = nextAJUState[AJUAddr] if state ==0: #System is in default state (all relays inactive) setAJURelays(AJUAddr, 0b000000) AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 1 elif state ==1: if readSensorState(AJUAddr) ==2729: nextAJUState[AJUAddr] = 2 elif state ==2: #System is waiting for glue to home before starting enable sequence if AJUEnableCMD[AJUAddr]: AJUEnableCMD[AJUAddr] = False setAJURelays(AJUAddr, 0b100000) AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 3 elif state == 3: #Wait 2 second for homing operation to complete if timeCheck(AJUAddr, 1) and (bus[AJUAddr].read_pin(14)): #FLipper not in home print ("\n AJU "+ str(AJUAddr+1)+ " homing") AJUSwitchTime[AJUAddr] =time.time() nextAJUState[AJUAddr] = 4 elif timeCheck(AJUAddr,3) and not(bus[AJUAddr].read_pin(14)): #Two seconds has passed and flipper has not moved. Motor must have homed previously AJUEnableDone[AJUAddr] = True setAJURelays(AJUAddr, 0b000000) nextAJUState[AJUAddr] = 6 elif state ==4: #Wait for homing to complete if timeCheck(AJUAddr,20): #Home sensor triggered or moveDone signal is on AJUSwitchTime[AJUAddr] = time.time() setAJURelays(AJUAddr,0b000000) nextAJUState[AJUAddr] = 5 # elif (readSensorState(AJUAddr)%2)==0: #Motor is telling us that move is AJUtaskDone # setAJURelays(AJUAddr,0b000000) # AJUSwitchTime[AJUAddr] = time.time() # nextAJUState[AJUAddr] = 5 elif state ==5: if not(bus[AJUAddr].read_pin(14)): AJUEnableDone[AJUAddr]=True nextAJUState[AJUAddr] = 6 elif state ==6: if AJUStopperCMD[AJUAddr]: AJUStopperCMD[AJUAddr] = False nextAJUState[AJUAddr] = 7 elif state == 7: #Activate Stopper if readSensorState(AJUAddr) == 2729: 1 setAJURelays(AJUAddr,0b001000) #Activate Stopper AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 8 elif state ==8: if readSensorState(AJUAddr) ==2725: #Stopper is up AJUSwitchTime[AJUAddr] = time.time() stopperIsUp[AJUAddr] = True nextAJUState[AJUAddr] = 10 elif timeCheck(AJUAddr,3): airPressureWarning = True nextAJUState[AJUAddr] = 9 elif state ==9: if readSensorState(AJUAddr) ==2725: #Stopper is up AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 10 elif state ==10: if AJUreadyCMD[AJUAddr]: printDebug( "AJU ready command received on AJU" + str(AJUAddr)) AJUSwitchTime[AJUAddr] = time.time() AJUreadyCMD[AJUAddr] = False nextAJUState[AJUAddr] =20 elif state == 20: #Activate Pushers if timeCheck(AJUAddr,.5) and (readSensorState(AJUAddr) == 2725): setAJURelays(AJUAddr,0b001001) #Activate Pushers AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 30 elif state == 30: #Activate Holders if timeCheck(AJUAddr,.5) and readSensorState(AJUAddr) == 1445: setAJURelays(AJUAddr,0b001111) #Activate Holders AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 40 elif state == 40: #Deactivate Pushers if timeCheck(AJUAddr,.5) and readSensorState(AJUAddr) == 1365: setAJURelays(AJUAddr,0b001110) #Deactivate Pushers AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 50 elif state == 50: #Deactivate Stopper if timeCheck(AJUAddr,.5) and readSensorState(AJUAddr) == 2645: setAJURelays(AJUAddr,0b010110) #Deactivate Stopper and command flipper to up position, but do not enable yet AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 60 elif state == 60: if (readSensorState(AJUAddr) == 2649): AJUisReady[AJUAddr] = True #Tell the scheduler that the AJU is ready AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 70 elif state == 70: #Wait for start glue signal if glueStart[AJUAddr]: glueStartTime = time.time() AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 80 elif state ==80: #Wait for AJU finish command if AJUfinishCMD[AJUAddr]: AJUfinishCMD[AJUAddr] = False setAJURelays(AJUAddr,0b110110) #Command flipper up position #AJUsafeFlags[AJUAddr] = False #Tell system that flipper 1 is up AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 100 # elif state ==90: # if timeCheck(AJUAddr, 2.5): #Enable motor to allow flipper to move up # setAJURelays(AJUAddr, 0b110110) # AJUSwitchTime[AJUAddr] = time.time() # nextAJUState[AJUAddr]=100 elif state ==100: if readSensorState(AJUAddr) ==2651: # Flipper has begun operation AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 105 elif state ==105: if timeCheck(AJUAddr,.5) and readSensorState(AJUAddr) ==2650: # Flipper has completed operation #AJUSwitchTime[AJUAddr] = time.time() setAJURelays(AJUAddr, 0b110111) #Activate Pushers nextAJUState[AJUAddr] = 110 elif state == 110: #Wait 20 seconds if timeCheck(AJUAddr,20): #Glue has been held for 20s setAJURelays(AJUAddr,0b110010) AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 115 elif state == 115: #Wait .5 seconds if timeCheck(AJUAddr,.5): setAJURelays(AJUAddr,0b110010) #Deactivate Pushers and Holder 2 AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 120 elif state == 120: if readSensorState(AJUAddr) == 2666: setAJURelays(AJUAddr,0b100010) #Move Motor to "Flipper Down Position" AJUSwitchTime[AJUAddr] = time.time() nextAJUState[AJUAddr] = 130 elif state ==130: if timeCheck(AJUAddr, 0.5) and readSensorState(AJUAddr) >= 2664 and readSensorState(AJUAddr)<=2666: #Flipper has completed its operation #AJUsafeFlags[AJUAddr] = True #Tell glue that flipper 1 is down setAJURelays(AJUAddr, 0b000000) #Deactivate Holder 1. Also disable motor AJUSwitchTime[AJUAddr] = time.time() AJUtaskDone[AJUAddr] = True nextAJUState[AJUAddr] = 5 def mm2counts(position): # This function converts a desired position in mm to the corresponding # number of pulse counts countsPerRev = 1000 lead_mm = 10 counts = 1.0*position/lead_mm*1000 return counts def glueReadyOn(): global glueSentReady printDebug('GlueReadyOn') glueSentReady = True GPIO.output(glue2ready,GPIO.HIGH) def glueReadyOff(): global glueSentReady printDebug('GlueReadyOff') glueSentReady = False GPIO.output(glue2ready,GPIO.LOW) def glueEnable(): global glueIsEnabled printDebug('GlueEnable') GPIO.output(glueEnablePin,GPIO.HIGH) glueIsEnabled = True def glueDisable(): global glueIsEnabled printDebug('GlueDisable') GPIO.output(glueEnablePin, GPIO.LOW) glueIsEnabled = False def DispenseOn(): global dispenseSent global dispenseCounter # global absDispenseCounter printDebug('DispenseOn') dispenseSent = True if not(noGlue): GPIO.output(dispense, GPIO.HIGH) dispenseCounter = dispenseCounter + 1 # absDispenseCounter = absDispenseCounter + 1 def DispenseOff(): global dispenseSent printDebug('DispenseOff') dispenseSent = False GPIO.output(dispense, GPIO.LOW) def setMotionParams(targetmm,speed): global pulseDuration global destination global direction pps = mm2counts(speed) pulseDuration = 1.0/pps printDebug(str(pulseDuration)) destination = mm2counts(targetmm+testOffset) if destination>58000: destination = 58000 if destination >absPulseCount: direction = 1 elif destinationrelayOnTime: GPIO.output(GPIOZHOME, GPIO.LOW) nextMoveState = 20 elif moveState == 20: #Wait for 'waitforSlideTime' then go to state 3 if time.time()-timeSentHome>waitForSlideTime: nextMoveState = 30 elif moveState ==30: #Wait for the driver to give the 'ready' signal (Home operation is complete). if GPIO.input(homeEndIn): slideInHome = True absPulseCount=0 nextMoveState =100 ##### BEGIN FORWARD 1 elif moveState ==100: #Slide is in Home and ready. Set motion parameters to go to position 1 setMotionParams(pos1,90) nextMoveState = 105 printDebug(' State: ' + str(nextMoveState) + ' Waiting for Go Conditions') elif moveState ==105: #Wait for the 'Ready' signal from the glue gun if continueAfterGlueReady: continueAfterGlueReady = False nextMoveState = 110 glueLevel = 0 # elif pauseForGlueReplace: # glueDisable() # nextMoveState = 107 #Placeholder # timeSentHome = time.time() elif moveState ==107: #Wait for the actuator to go up if time.time()-timeSentHome>4: nextMoveState = 108 elif moveState == 108: if not(pauseForGlueReplace): glueEnable() nextMoveState =105 elif moveState ==110: if gluePosCMD ==1: nextMoveState = 115 elif moveState ==115: #Move slide to position 1 (Just before AJU1) if not(abs(absPulseCount-destination)<1): updateSlide() else: gluePos =1 nextMoveState = 120 elif moveState ==120: #Set motion parameters to glue and move to position 2 (Just before AJU2) glueRefPos = absPulseCount setMotionParams(pos3,90) nextMoveState = 125 printDebug( 'Current Position: ' + str(absPulseCount)) printDebug( 'Destination: ' + str(destination)) printDebug( 'GlueRefPos: ' +str(glueRefPos)) printDebug( 'Glue Starting At: ' + str(glueRefPos + mm2counts(startGlue))) printDebug( 'Glue Stopping At: ' + str(glueRefPos + mm2counts(stopGlue))) printDebug( 'Waiting for Go Conditions') ## STOP. WAIT FOR SCHEDULER ## elif moveState == 125: #Slide is waiting for 'Go' Conditions if startGlueCMD: #Wait for scheduler to check conditions and send the glue command startGlueCMD = False nextMoveState = 130 elif moveState == 130: #Slide is moving over AJU1, but not dispensing glue if (absPulseCount glueDripStartDelay: setMotionParams(pos3,11) nextMoveState = 140 elif moveState == 140: #Slide is moving past AJU1 and dispensing glue if (absPulseCount glueDripStopDelay: setMotionParams(pos3,90) nextMoveState = 150 elif moveState ==150: #Slide is getting out of the way of AJU 1 if absPulseCount1: updateSlide() else: gluePos = 3 #Let the scheduler know that the glue has reached position 3 nextMoveState = 205 elif moveState ==200: if gluePosCMD ==5: nextMoveState = 205 ##### BEGIN FORWARD 2 elif moveState ==205: #Set motion parameters to glue and move to position 5 (End of Slide) glueRefPos = absPulseCount setMotionParams(pos4,90) nextMoveState = 210 printDebug( 'Current Position: ' + str(absPulseCount)) printDebug( 'Destination: ' + str(destination)) printDebug( 'GlueRefPos: ' +str(glueRefPos)) printDebug( 'Glue Starting At: ' + str(glueRefPos + mm2counts(startGlue))) printDebug( 'Glue Stopping At: ' + str(glueRefPos + mm2counts(stopGlue))) printDebug( 'Waiting for Go Conditions') ## STOP. WAIT FOR SCHEDULER ## elif moveState == 210: #Slide is waiting for 'Go' Conditions if startGlueCMD: #Wait for scheduler to check conditions and send the glue command startGlueCMD = False nextMoveState = 215 elif moveState == 215: #Slide is moving over AJU2, but not dispensing glue if absPulseCount glueDripStartDelay: setMotionParams(pos4,11) nextMoveState = 225 elif moveState == 225: #Slide is moving past AJU2 and dispensing glue if (absPulseCount glueDripStopDelay: setMotionParams(pos4,90) nextMoveState = 235 elif moveState ==235: #Slide is getting out of the way of AJU 2 if absPulseCount1: updateSlide() else: gluePos = 4 #Let the scheduler know that the glue has reached position 4 nextMoveState = 300 moveTaskDone = True ##### BEGIN REVERSE 2 elif moveState ==300: #Set motion parameters to glue and move to position 2 (Just before AJU2) glueRefPos = absPulseCount setMotionParams(pos2,90) nextMoveState = 305 printDebug( 'Current Position: ' + str(absPulseCount)) printDebug( 'Destination: ' + str(destination)) printDebug( 'GlueRefPos: ' +str(glueRefPos)) printDebug( 'Glue Starting At: ' + str(glueRefPos - mm2counts(allowFlip - stopGlue))) printDebug( 'Glue Stopping At: ' + str(glueRefPos - mm2counts(allowFlip-startGlue))) printDebug( 'Waiting for Go Conditions') ## STOP. WAIT FOR SCHEDULER ## elif moveState == 305: #Slide is waiting for 'Go' Conditions if startGlueCMD: #Wait for scheduler to check conditions and send the glue command startGlueCMD = False nextMoveState = 310 elif moveState ==310: #Slide is moving to the gluestart position on AJU 2 in reverse direction if absPulseCount>mm2counts(pos3b+startGluePosOffset+1):#abs(absPulseCount-glueRefPos) glueDripStartDelay: setMotionParams(pos2,11) nextMoveState = 320 elif moveState == 320: #Slide is moving past AJU2 in reverse direction and dispensing glue if (absPulseCount>mm2counts(pos3a+stopGluePosOffset-2)) and time.time()-glueOnTime glueDripStopDelay: setMotionParams(pos2,90) nextMoveState = 330 elif moveState ==330: #Slide is getting out of the way of AJU 2 if absPulseCount>mm2counts(pos3):#abs(absPulseCount-glueRefPos)1: updateSlide() else: gluePos = 2 #Let the scheduler know that the glue has reached position 2 nextMoveState = 400 ##### BEGIN REVERSE 1 elif moveState ==400: #Set motion parameters to glue and move to position 1 (Just after AJU1 in reverse direction) glueRefPos = absPulseCount setMotionParams(pos1,90) nextMoveState = 405 printDebug( 'Current Position: ' + str(absPulseCount)) printDebug( 'Destination: ' + str(destination)) printDebug( 'GlueRefPos: ' +str(glueRefPos)) printDebug( 'Glue Starting At: ' + str(glueRefPos - mm2counts(allowFlip - stopGlue))) printDebug( 'Glue Stopping At: ' + str(glueRefPos - mm2counts(allowFlip-startGlue))) printDebug( 'Waiting for Go Conditions') ## STOP. WAIT FOR SCHEDULER ## elif moveState == 405: #Slide is waiting for 'Go' Conditions if startGlueCMD: #Wait for scheduler to check conditions and send the glue command startGlueCMD = False nextMoveState = 410 elif moveState ==410: #Slide is moving to the gluestart position on AJU 1 in reverse direction if absPulseCount>mm2counts(pos1b+startGluePosOffset-5):#abs(absPulseCount-glueRefPos) glueDripStartDelay: setMotionParams(pos1,11) nextMoveState = 420 elif moveState == 420: #Slide is moving past AJU1 in reverse direction and dispensing glue if (absPulseCount>mm2counts(pos1a+stopGluePosOffset-2)) and time.time()-glueOnTime glueDripStopDelay: setMotionParams(10,90) nextMoveState = 430 elif moveState ==430: #Slide is getting out of the way of AJU 1 if abs(absPulseCount-destination)>1:#abs(absPulseCount-glueRefPos)0.5*pulseDuration: pulseEdgeTime = time.time() if pulseState ==0: absPulseCount = absPulseCount + GPIO.input(moving)*(2*direction - 1) GPIO.output(pulsePins[direction],GPIO.LOW) nextPulseState = 1 elif pulseState ==1: GPIO.output(pulsePins[direction],GPIO.HIGH) nextPulseState = 0 def scheduler(): global nextSysState global sysStateChangeTime global blockAJU global AJUreadyCMD global AJUfinishCMD global AJUisReady global gluePosCMD global gluePos global slideInHome global startGlueCMD global glueHomeCMD global AJUEnableCMD global AJUEnableDone global AJUStopperCMD global continueAfterGlueReady global airPressureWarning global pauseRequest global systemIsPaused global ignorePause global stopperIsUp global pauseForGlueReplace global dispenseCounter global initializeTime global last10startTime sysState = nextSysState if sysState ==0:#System has just turned on glueDisable() glueReadyOff() DispenseOff() ignorePause = True nextSysState = 1 print "Version " + str(version) print "*IMPORTANT* VERIFY THE FOLLOWING BEFORE OPERATING:" print "\n Air Pressure Set to 40 psi" print "\n Power Switch Set to All Power" print "\n Glue Gun LED is Solid Green" print "\n Glue Gun Is Oriented Vertically" print "\n AJUs are Down and Clear of All Obstructions" print "\nPress START button to initialize" if sysState ==1: if not(GPIO.input(goButton)): nextSysState = 5 if sysState ==5: #Sends the glue to home if not((AJUisPresent[0] and bus[0].read_pin(14)) or (AJUisPresent[1] and bus[1].read_pin(14))): # printDebug( "glueHomeCMD sent") glueReadyOn() printDebug("Readying glue gun.") glueHomeCMD=True #Needed for move task nextSysState = 10 elif sysState ==10: #Wait for home complete if slideInHome: slideInHome = False #print "AJU Motors Homing. Interference May Impact Position Tracking" AJUEnableCMD = [True,True] #Allow AJU motors to home sysStateChangeTime = time.time() nextSysState =12 # if sysState ==12: #Wait 2 seconds to allow homing to begin if ((not(AJUisPresent[0]) or AJUEnableDone[0]) and (not(AJUisPresent[1]) or AJUEnableDone[1])): AJUEnableDone = [False,False] initializeTime = time.time() nextSysState = 13 ##System will return to this state after the cycle has completed if sysState ==13: #Check if homing has completed (flippers are down, then activate the stoppers) if not((AJUisPresent[0] and bus[0].read_pin(14)) or (AJUisPresent[1] and bus[1].read_pin(14))): AJUStopperCMD = [True,True] nextSysState = 15 elif sysState == 15: #Wait for the glue to be ready if GPIO.input(glueReplaceIn): #Glue is low glueDisable() glueReadyOff() print "\n*Glue Replacement Needed*" print "Wait for glue to finish retracting (GD: " + str(dispenseCounter)+ ")" dispenseCounter = 0 nextSysState=17 elif GPIO.input(glueReady) and all(stopperIsUp): #print "Glue is ready" stopperIsUp = [False,False] printDebug( '...glueReady Signal Received') print '\nLoad bricks on both AJUs, then press START button to begin forward operation (GD: ' + str(dispenseCounter)+ ')' continueAfterGlueReady = True nextSysState = 20 elif sysState ==17: if not(GPIO.input(glueReplaceIn)): #Wait for glue replace input to go low. This indicates the glue is fully retracted print "\nLoad glue, then remove hands from machine and \npress START button " nextSysState =18 elif sysState == 18: #Glue is Low if not(GPIO.input(goButton)): print "\nVerifying glue replacement. Please Wait." glueEnable() glueReadyOn() nextSysState =15 elif sysState ==20: #Wait for user to release the 'Go Button' if GPIO.input(goButton): nextSysState =25 # elif sysState == 16: # if glueLevel ==1: # print "\nReplace glue before prodeceding.Press START button when ready" # DisableGlue() # nextSysState = 17 # else: # nextSysState = 20 # # elif sysState == 17: # if elif sysState ==25: #Wait for user to press the 'Go Button' if not(GPIO.input(goButton)): print "\nStarting Operation. Do not touch machine while in operation." AJUreadyCMD = [True,True] printDebug( "Sending AJUs to ready position...") nextSysState = 40 elif sysState ==40: #Wait for acknowledgements of ready command if (not(AJUisPresent[0]) or AJUisReady[0]) and (not(AJUisPresent[1]) or AJUisReady[1]):#all(AJUisReady): AJUisReady[0] = False AJUisReady[1] = False printDebug( "...AJUs are ready") printDebug( "Sending glue to ready position 1") gluePosCMD = 1 nextSysState =50 elif sysState ==50: if gluePos ==1: printDebug( "Glue is in ready position 1") printDebug( "Verifying AJU1 is in down position") nextSysState = 60 # if pauseRequest: # systemIsPaused ==True # print "System paused (50). Press START button to resume" # pauseRequest = False # nextSysState =55 # else: # nextSysState =60 elif sysState ==55: if not(GPIO.input(goButton)): systemIsPaused = False print "\nSystem resuming after PAUSE operation" nextSysState = 60 elif sysState ==60: if not(bus[0].read_pin(14)): gluePosCMD = 2 startGlueCMD =True nextSysState = 70 printDebug( "Dispensing glue on AJU 1") elif sysState ==70: if gluePos ==2: printDebug( "Glue in Position 2. AJU 1 proceding with assembly") AJUfinishCMD[0] =True ignorePause = False printDebug( 'Waiting for AJU 1 to finish') nextSysState = 80 elif sysState ==80: if gluePos ==3: printDebug( "Glue in ready position 3") printDebug( "Verifying AJU 2 is in down position") if pauseRequest: systemIsPaused = True print "System paused (80). Press START button to resume" pauseRequest = False nextSysState =85 else: nextSysState =90 elif sysState ==85: if not(GPIO.input(goButton)): systemIsPaused = False print "\nSystem resuming after PAUSE operation" nextSysState = 90 elif sysState ==90: if not(bus[1].read_pin(14)): ignorePause = True gluePosCMD = 5 startGlueCMD= True nextSysState =100 printDebug( "Dispensing glue on AJU 2") elif sysState ==100: if gluePos ==4: printDebug( "Glue in Position 4. AJU 2 proceding with assembly") AJUfinishCMD[1] = True printDebug( "Waiting for AJU 2 to finish") nextSysState = 110 elif sysState ==110: if AJUtaskDone[0]: print "\nAJU 1 assembly complete" AJUtaskDone[0] = False if AJUtaskDone[1]: print "\nAJU 2 assembly complete" AJUtaskDone[1] = False AJUStopperCMD = [True,True] print '\nForward operation complete' print "----------------------------------------------" print "" nextSysState = 315 elif sysState == 315: #Wait for the glue to be ready # if glueLevel ==1: # print "\nReplace Glue Now. When finished, clear hands from glue gun and press START button" # pauseForGlueReplace = True # sysStateChangeTime = time.time() # nextSysState = 17 if GPIO.input(glueReplaceIn): #Glue is low glueDisable() glueReadyOff() print "\n*Glue Replacement Needed*" print "Wait for glue to finish retracting (GD: " + str(dispenseCounter)+ ")" dispenseCounter = 0 nextSysState=317 elif GPIO.input(glueReady) and all(stopperIsUp): #print "Glue is ready" stopperIsUp = [False,False] printDebug( '...glueReady Signal Received') print '\nLoad bricks on both AJUs, then press START button to begin reverse operation (GD: ' + str(dispenseCounter)+ ')' # if absDispenseCounter%10 == 0: # print "Rate Since Power On: " + str((time.time()-initializeTime)/absDispenseCounter) + " seconds/brick" # print "(Last 10: " + str((time.time()-last10startTime)/10)+ "seconds/brick)" # last10startTime = time.time() continueAfterGlueReady = True nextSysState = 325 elif sysState ==317: if not(GPIO.input(glueReplaceIn)): print "\nLoad glue, then remove hands from machine and \npress START button " nextSysState =318 elif sysState == 318: #Glue is Low if not(GPIO.input(goButton)): print "\nVerifying glue replacement. Please Wait." glueEnable() glueReadyOn() nextSysState =315 elif sysState ==325: #Wait for user to press the 'Go Button' if not(GPIO.input(goButton)): print "\nStarting reverse operation. Do not touch machine while in operation." nextSysState =330 elif sysState ==330: # Send all AJUs to glue ready position AJUreadyCMD = [True,True] nextSysState = 340 printDebug( "Sending AJUs to ready position...") elif sysState ==340: #Wait for acknowledgements of ready command if (not(AJUisPresent[0]) or AJUisReady[0]) and (not(AJUisPresent[1]) or AJUisReady[1]):#all(AJUisReady): AJUisReady[0] = False AJUisReady[1] = False printDebug( "...AJUs are ready") nextSysState = 360 # if pauseRequest: # systemIsPaused = True # print "System paused (340). Press START button to resume" # pauseRequest = False # nextSysState =355 # else: # nextSysState =360 elif sysState ==355: if not(GPIO.input(goButton)): systemIsPaused = False print "\nSystem Resuming" nextSysState = 90 elif sysState ==360: if not(bus[1].read_pin(14)): ignorePause = False gluePosCMD = 2 startGlueCMD =True nextSysState = 370 printDebug( "Dispensing glue on AJU 2") elif sysState ==370: if gluePos ==3: printDebug( "Glue in Position 2. AJU 2 proceding with assembly") AJUfinishCMD[1] =True printDebug( 'Waiting for AJU 2 to finish') nextSysState = 380 elif sysState ==380: if gluePos ==2: printDebug( "Glue in ready position 2") printDebug( "Verifying AJU 1 is in down position") if pauseRequest: systemIsPaused = True print "\nSystem paused. Press START button to resume" pauseRequest = False nextSysState =385 else: nextSysState =390 elif sysState ==385: if not(GPIO.input(goButton)): systemIsPaused = False print "System Resuming" nextSysState = 390 elif sysState ==390: if not(bus[0].read_pin(14)): ignorePause = True gluePosCMD = 1 startGlueCMD= True nextSysState =400 printDebug( "Dispensing glue on AJU 1") elif sysState ==400: if gluePos ==1: printDebug( "Glue in Position 1. AJU 2 proceding with assembly") AJUfinishCMD[0] = True printDebug( "Waiting for AJU 1 to finish") nextSysState = 410 elif sysState ==410: if AJUtaskDone[1]: print "\nAJU 2 assembly complete" AJUtaskDone[1] = False if AJUtaskDone[0]: print "\nAJU 1 assembly complete" AJUtaskDone[0] = False AJUStopperCMD = [True,True] print '\nReverse operation complete' print "----------------------------------------------" print "" nextSysState = 13 def watchDogTask(): #This task monitors for warnings, alarms, and pause button presses global watchDogState global nextWatchDogState global pauseStateEntryFlag global pausePressTime global pauseRequest global reverseRequest global alarmActive global dispRemaining global glueLevel global airPressureWarning global pressureWarningCooldown watchDogState = nextWatchDogState if airPressureWarning and (time.time()-pressureWarningCooldown>5): pressureWarningCooldown = time.time() print("\n Verify pressurized air supply.") airPressureWarning = False if watchDogState ==0: #system not paused if not(GPIO.input(alarmIn) or alarmActive): #Alarm is active. This is highest priority print("\nSlide alarm active. Remove cause of alarm, then press alarm reset button") alarmActive = True #Disable both AJU motors nextWatchDogState = 100 elif not(GPIO.input(pauseButton)): #Pause button was pressed pausePressTime = time.time() if not (systemIsPaused or pauseRequest or ignorePause): print("\nPause button pressed") nextWatchDogState =20 elif (glueLevel == 0 and GPIO.input(glueReplaceIn)): #Arduino is sending glue low warning. This is the lowest priority of the three # print "\nGlue low. Replace when glue head is at either end" glueLevel = 1 #glue should be replaced soon elif glueLevel == 1 and not(GPIO.input(glueReplaceIn)): glueLevel = 0 elif watchDogState == 20: #Pause Button was pressed if not(GPIO.input(alarmIn) or alarmActive): print("\nSlide alarm active. ") alarmActive = True nextWatchDogState = 10 elif GPIO.input(pauseButton):# and not(systemIsPaused): if not(systemIsPaused or ignorePause): print "\nSystem Will Pause After Current Unit" pauseRequest = True nextWatchDogState = 0 #Request was sent. System will pause before next AJU. Go back to checking for all alarms elif time.time()-pausePressTime > 10: print "\nProgram Restarting in 3 seconds" setAJURelays(0, 0) setAJURelays(1,0) time.sleep(3) raise SystemExit("Program Restarting") reverseRequest = True print "\nSystem reverse requested" nextWatchDogState = 40 elif watchDogState == 100: #Alarm currently active if GPIO.input(alarmIn): #Alarm no longer active print("\nAlarm has been reset") alarmActive = False nextWatchDogState = 10 elif not(GPIO.input(pauseButton)): #Pause button was pressed pausePressTime = time.time() print("Pause button pressed") nextWatchDogState =20 time.sleep(.5) print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" time.sleep(.5) print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" while infiniteLoop: scheduler() moveTask() AJUTask(0) AJUTask(1) watchDogTask()