'**********************************************************************''   MacroPump.mls                                           06DEC010''   1) Updates the VFD Stats (RPM, AMP, and LEDS)'   2) Maps some output pin states to UI LEDS'   3) Looks for PoKeys presses and calls the mapped Macro Code''**********************************************************************'System Run ModeConst runMode_IDLE                  = 0Const runMode_RUN                   = 1Const runMode_ATC                   = 2Const runMode_OFFSET                = 3Const runMode_MPG                   = 4Const screen_RUN                    = 1Const screen_ATC                    = 6Const screen_OFFSET                 = 7Const screen_MPG                    = 7'User DROSConst userDRO_spindleRPM            = 1100Const userDRO_spindleTargetHz       = 1101Const userDRO_spindleHz             = 1102Const userDRO_spindleAmps           = 1103Const userDRO_runMode               = 1400'User LEDsConst userLED_atcInitialized        = 1000Const userLED_spindleForward        = 1104Const userLED_spindleReverse        = 1105Const userLED_toolRam               = 1015Const userLED_airBlast              = 1016Const userLED_drawBar               = 1017'Outputs Pins/PortsConst output_LampSolenoid           = Output16        Const output_spindleFan             = Output17        Const output_drawBarSolenoid        = Output18        Const output_RamSolenoid            = Output19Const output_AirBlastSolenoid       = Output20'PoKeys OutputsConst pokeysOutoput_RESET           = 2053Const pokeysOutoput_INIT            = 2052Const pokeysOutoput_MPGA            = 2051Const pokeysOutoput_MPGZ            = 2050Const pokeysOutoput_MPGY            = 2049Const pokeysOutoput_MPGX            = 2048Const pokeysOutoput_MPG_X1          = 2047Const pokeysOutoput_MPG_X10         = 2046Const pokeysOutoput_MPG_X100        = 2045Const pokeysOutoput_COOLANT         = 2044Const pokeysOutoput_modeRUN         = 2042Const pokeysOutoput_modeATC         = 2040Const pokeysOutoput_modeOFFSET      = 2041Const pokeysOutoput_Lamp            = 2039'PoKeys InputsConst pokeysInput_INIT              = 2000Const pokeysInput_RESET             = 2001Const pokeysInput_LAMP              = 2116Const pokeysInput_COOLANT           = 2108Const pokeysInput_PARK              = 2109Const pokeysInput_XOYOZ1            = 2110Const pokeysInput_XOYO              = 2111Const pokeysInput_RUN               = 2123Const pokeysInput_HOLD              = 2115Const pokeysInput_STOP              = 2107Const pokeysInput_incFEED           = 2120Const pokeysInput_decFEED           = 2122Const pokeysInput_resetFEED         = 2121Const pokeysInput_incRPM            = 2112Const pokeysInput_decRPM            = 2114Const pokeysInput_resetRPM          = 2113Const pokeysInput_incATC            = 2117Const pokeysInput_toolATC           = 2118Const pokeysInput_decATC            = 2119Const pokeysInput_MPGX              = 2103Const pokeysInput_MPGY              = 2006Const pokeysInput_MPGZ              = 2005Const pokeysInput_MPGA              = 2004Const pokeysInput_MPG_1X            = 2102Const pokeysInput_MPG_10X           = 2101Const pokeysInput_MPG_100X          = 2100Const pokeysInput_modeRUN           = 2106Const pokeysInput_modeATC           = 2105Const pokeysInput_modeOFFSET        = 2104'Modbus Register NumbersConst modbusReg_RPM                 = 64Const modbusReg_Status              = 67Const modbusReg_TargetHz            = 68Const modbusReg_Hz                  = 70Const modbusReg_Amps                = 72'OEM Buttons Const oemButton_Coolant             = 113Const oemButton_Start               = 1000Const oemButton_Hold                = 1001Const oemButton_Stop                = 1003Const oemButton_incFEED             = 108Const oemButton_decFEED             = 109Const oemButton_resetFEED           = 1014Const oemButton_incRPM              = 163Const oemButton_decRPM              = 164Const oemButton_resetRPM            = 183Const oemButton_ToggleJog           = 103Const oemButton_MPGselectX          = 185Const oemButton_MPGselectY          = 186Const oemButton_MPGselectZ          = 187Const oemButton_MPGselectA          = 188Const oemButton_MPGstepMulti        = 306Const oemButton_MPGstepVelocity     = 304Const oemButton_eStop               = 1021                  'OEM DROsConst oemDRO_jogIncrement            = 828'OEM LEDsConst oemLED_Coolant                = 13Const oemLED_JogEnable              = 83Const oemLED_MPGX                   = 59Const oemLED_MPGY                   = 60Const oemLED_MPGZ                   = 61Const oemLED_MPGA                   = 62Const oemLED_MPGstepMultiMode       = 120Const oemLED_MPGstepVelocityMode    = 118Const oemLED_RESET                  = 800Const oemLED_START                  = 804'**********************************************************************''   The "MacroPump"''**********************************************************************Sub Main()    ' DEBUG: Indication that the Macro Pump is runing and how fast    'MacroLoopCount = GetUserDro( 1500 )    'MacroLoopCount = MacroLoopCount + 1    'setUserDro( 1500, MacroLoopCount )    ' Update the Spindle Data/Stats    updateSpindleRealtimeDROs    ' Update Screen LEDs based on Output States    mapOututsToLEDS        ' Update PoKeys OUTPUTS for the real LEDS based on the System State    updatePoKeyOutputs        ' Check and Procress PoKeys Inputs/Button Presses    checkForPoKeyInputs    ' DEBUG: Display PoKey Number in real-time    'For x = 2100 to 2123    '    If GetUserLED( x ) Then    '        SetUserLabel( 1, "ULED: " & x )    '    End If    'Next xEnd Sub '**********************************************************************''   checkForPoKeyInputs    ''   Checks for pokeys button presses.  Calles additional Macro code if needed'   and/or OEM Buttons.  ''**********************************************************************Sub checkForPoKeyInputs    ' Set the default that ALL buttons that call Macros code need to    ' wait for the system to be Initialized first.    checkForInit = 1        ' Debounce ALL button pushes.  This prevents mulitple pushed of the     ' same button.  Its also provides a nice button repeat feature    If GetTimer( 0 ) > 0.650 Then        '        ' Simple Buttons        '            ' Check for the 'INII' Button being pressed            If GetUserLED( pokeysInput_INIT ) = 0 Then                checkForInit = 0                        ' Skip the check for INIT, this is the INIT Function                processPoKeysMacro( 1999 )                             SetTimer( 0 )                               End If                 ' Check for the 'RESET' Button being pressed            If GetUserLED( pokeysInput_RESET ) = 0 Then                DoOEMButton( oemButton_eStop )                         SetUserLED( userLED_atcInitialized, 0 )                 SetUserDro( userDRO_runMode, runMode_IDLE )                SetTimer( 0 )            End If             ' Check for the 'LAMP' Button being pressed            If GetUserLED( pokeysInput_LAMP ) Then                If IsOutputActive( output_LampSolenoid ) Then                    DeActivateSignal( output_LampSolenoid )                    Else                    ActivateSignal( output_LampSolenoid )                End If                 SetTimer( 0 )            End If             ' Check for the 'COOL' Button being pressed            If GetUserLED( pokeysInput_COOLANT ) Then                DoOEMButton( oemButton_Coolant )                SetTimer( 0 )            End If         '        ' We can only change states if we are INITIALIZED        '        If getUserLED( userLED_atcInitialized ) = 1 Then                    '            ' MPG State Machine Buttons            '                ' Check for the 'MPG A' Button being pressed                If GetUserLED( pokeysInput_MPGA ) = 0 Then                    If GetOEMLED( oemLED_JogEnable ) Then                        If GetOEMLED( oemLED_MPGA ) Then                            DoOEMButton( oemButton_ToggleJog )                            SetUserDro( userDRO_runMode, 0 )                        Else                            DoOEMButton( oemButton_MPGselectA )                        End If                    Else                                        DoOEMButton( oemButton_ToggleJog )                        DoOEMButton( oemButton_MPGselectA )                    End If                    SetTimer( 0 )                End If                     ' Check for the 'MPG Z' Button being pressed                If GetUserLED( pokeysInput_MPGZ ) = 0 Then                    If GetOEMLED( oemLED_JogEnable ) Then                        If GetOEMLED( oemLED_MPGZ ) Then                            DoOEMButton( oemButton_ToggleJog )                            SetUserDro( userDRO_runMode, 0 )                        Else                            DoOEMButton( oemButton_MPGselectZ )                        End If                    Else                                        DoOEMButton( oemButton_ToggleJog )                        DoOEMButton( oemButton_MPGselectZ )                    End If                    SetTimer( 0 )                End If                     ' Check for the 'MPG Y' Button being pressed                If GetUserLED( pokeysInput_MPGY ) = 0 Then                    If GetOEMLED( oemLED_JogEnable ) Then                        If GetOEMLED( oemLED_MPGY ) Then                            DoOEMButton( oemButton_ToggleJog )                            SetUserDro( userDRO_runMode, 0 )                        Else                            DoOEMButton( oemButton_MPGselectY )                        End If                    Else                                        DoOEMButton( oemButton_ToggleJog )                        DoOEMButton( oemButton_MPGselectY )                    End If                    SetTimer( 0 )                End If                     ' Check for the 'MPG X' Button being pressed                If GetUserLED( pokeysInput_MPGX ) Then                    If GetOEMLED( oemLED_JogEnable ) Then                        If GetOEMLED( oemLED_MPGX ) Then                            DoOEMButton( oemButton_ToggleJog )                            SetUserDro( userDRO_runMode, 0 )                        Else                            DoOEMButton( oemButton_MPGselectX )                        End If                    Else                                        DoOEMButton( oemButton_ToggleJog )                        DoOEMButton( oemButton_MPGselectX )                    End If                    SetTimer( 0 )                End If                         ' Check for the 'MPG 1X' Button being pressed                If GetUserLED( pokeysInput_MPG_1X ) Then                    If GetOEMLED( oemLED_JogEnable ) Then                        DoOEMButton( oemButton_MPGstepMulti )                        SetOEMDRO( oemDRO_jogIncrement, 0.0001 )                                                                   SetTimer( 0 )                    End If                End If                         ' Check for the 'MPG 10X' Button being pressed                If GetUserLED( pokeysInput_MPG_10X ) Then                    If GetOEMLED( oemLED_JogEnable ) Then                        DoOEMButton( oemButton_MPGstepMulti )                        SetOEMDRO( oemDRO_jogIncrement, 0.001 )                                                                   SetTimer( 0 )                    End If                End If                         ' Check for the 'MPG 100X' Button being pressed                If GetUserLED( pokeysInput_MPG_100X ) Then                    If GetOEMLED( oemLED_JogEnable ) Then                        DoOEMButton( oemButton_MPGstepVelocity )                        SetTimer( 0 )                    End If                End If                         '            ' System State Machine Control Buttons            '                ' Check for the 'RUN MODE' Button being pressed                If GetUserLED( pokeysInput_modeRUN ) Then                    If GetUserDro( userDRO_runMode ) = runMode_RUN Then                        setUserDro( userDRO_runMode, runMode_IDLE )                    Else                        setUserDro( userDRO_runMode, runMode_RUN )                        If GetOEMLED( oemLED_JogEnable ) Then                            DoOEMButton( oemButton_ToggleJog )                        End If                     End If                     SetTimer( 0 )                End If                     ' Check for the 'ATC MODE' Button being pressed                If GetUserLED( pokeysInput_modeATC ) Then                    If GetUserDro( userDRO_runMode ) = runMode_ATC Then                        setUserDro( userDRO_runMode, runMode_IDLE )                    Else                        setUserDro( userDRO_runMode, runMode_ATC )                        If GetOEMLED( oemLED_JogEnable ) Then                            DoOEMButton( oemButton_ToggleJog )                        End If                     End If                     SetTimer( 0 )                End If                         ' Check for the 'OFFSET MODE' Button being pressed                If GetUserLED( pokeysInput_modeOFFSET ) Then                    If GetUserDro( userDRO_runMode ) = runMode_OFFSET Then                        setUserDro( userDRO_runMode, runMode_IDLE )                    Else                        setUserDro( userDRO_runMode, runMode_OFFSET )                        If GetOEMLED( oemLED_JogEnable ) Then                            DoOEMButton( oemButton_ToggleJog )                        End If                     End If                     SetTimer( 0 )                End If                             End If             '        ' System State: Run Buttons        '            If GetUserDro( userDRO_runMode ) = runMode_RUN Then                ' Check for the 'RUN' Button being pressed                If GetUserLED( pokeysInput_RUN ) Then                    DoOEMButton( oemButton_Start )                    SetTimer( 0 )                End If                 ' Check for the 'HOLD' Button being pressed                If GetUserLED( pokeysInput_HOLD ) Then                    DoOEMButton( oemButton_Hold )                    SetTimer( 0 )                End If                 ' Check for the 'STOP' Button being pressed                If GetUserLED( pokeysInput_STOP ) Then                    DoOEMButton( oemButton_Stop )                    SetTimer( 0 )                End If                 ' Check for the 'INC FEED' Button being pressed                If GetUserLED( pokeysInput_incFEED ) Then                    DoOEMButton( oemButton_incFEED )                    SetTimer( 0 )                End If                 ' Check for the 'DEC FEED' Button being pressed                If GetUserLED( pokeysInput_decFEED ) Then                    DoOEMButton( oemButton_decFEED )                    SetTimer( 0 )                End If                 ' Check for the 'RESET FEED' Button being pressed                If GetUserLED( pokeysInput_resetFEED ) Then                    DoOEMButton( oemButton_resetFEED )                    SetTimer( 0 )                End If                 ' Check for the 'INC RPM' Button being pressed                If GetUserLED( pokeysInput_incRPM ) Then                    DoOEMButton( oemButton_incRPM )                    SetTimer( 0 )                End If                 ' Check for the 'DEC RPM' Button being pressed                If GetUserLED( pokeysInput_decRPM ) Then                    DoOEMButton( oemButton_decRPM )                    SetTimer( 0 )                End If                 ' Check for the 'RESET RPM' Button being pressed                If GetUserLED( pokeysInput_resetRPM ) Then                    DoOEMButton( oemButton_resetRPM )                    SetTimer( 0 )                End If             End If         '        ' System State: ATC Buttons        '            If GetUserDro( userDRO_runMode ) = runMode_ATC Then                    ' Check for the 'INC ATC' Button being pressed                If GetUserLED( pokeysInput_incATC ) Then                    processPoKeysMacro( 1006 )                       SetTimer( 0 )                End If                     ' Check for the 'DEC ATC' Button being pressed                If GetUserLED( pokeysInput_decATC ) Then                    processPoKeysMacro( 1005 )                       SetTimer( 0 )                End If                     ' Check for the 'TOOL: ATC Drawbar' Button being pressed                If GetUserLED( pokeysInput_toolATC ) Then                    processPoKeysMacro( 1004 )                       SetTimer( 0 )                End If                                 End If                 '        ' System State: TOOL OFFSET Buttons        '            If GetUserDro( userDRO_runMode ) = runMode_OFFSET Then                    ' Check for the 'PARK' Button being pressed                If GetUserLED( pokeysInput_PARK ) Then                    processPoKeysMacro( 1113 )                       SetTimer( 0 )                End If                     ' Check for the 'XOYOZ1' Button being pressed                If GetUserLED( pokeysInput_XOYOZ1 ) Then                    processPoKeysMacro( 1111 )                       SetTimer( 0 )                End If                     ' Check for the 'XOYO' Button being pressed                If GetUserLED( pokeysInput_XOYO ) Then                    processPoKeysMacro( 1110 )                       SetTimer( 0 )                End If                 End If     ' End of debounce             End If End Sub '**********************************************************************''   updatePoKeyOutputs    ''   Updates PoKeys Output Pins based on the status of the System ''**********************************************************************Sub updatePoKeyOutputs            '    ' Simple I/O     '        ' System 'RESET'        If GetOEMLED( oemLED_RESET ) Then            setUserLed( pokeysOutoput_RESET, 0 )        Else            setUserLed( pokeysOutoput_RESET, 1 )        End If                ' ATC/System 'INIT'           setUserLed( pokeysOutoput_INIT, GetUserLED( userLED_atcInitialized ))            ' Coolant        If GetOEMLED( oemLED_Coolant ) Then            setUserLed( pokeysOutoput_COOLANT, 1 )        Else            setUserLed( pokeysOutoput_COOLANT, 0 )        End If        ' Lamp        If IsOutputActive( output_LampSolenoid ) Then            setUserLed( pokeysOutoput_Lamp, 1 )        Else            setUserLed( pokeysOutoput_Lamp, 0 )        End If            ' RUN LED        If GetOEMLED( oemLED_START ) Then            ActivateSignal( output_spindleFan )        Else            DeActivateSignal( output_spindleFan )        End If    '    ' MPG States    '            ' MPG A        If GetOEMLED( oemLED_MPGA ) Then            setUserLed( pokeysOutoput_MPGA, 1 )        Else            setUserLed( pokeysOutoput_MPGA, 0 )        End If            ' MPG Z        If GetOEMLED( oemLED_MPGZ ) Then            setUserLed( pokeysOutoput_MPGZ, 1 )        Else            setUserLed( pokeysOutoput_MPGZ, 0 )        End If            ' MPG Y        If GetOEMLED( oemLED_MPGY ) Then            setUserLed( pokeysOutoput_MPGY, 1 )        Else            setUserLed( pokeysOutoput_MPGY, 0 )        End If            ' MPG X        If GetOEMLED( oemLED_MPGX ) Then            setUserLed( pokeysOutoput_MPGX, 1 )        Else            setUserLed( pokeysOutoput_MPGX, 0 )        End If            ' MPG 1X and 10X modes        If GetOEMLED( oemLED_MPGstepMultiMode ) And GetOEMLED( oemLED_JogEnable ) Then            If GetOEMDRO( oemDRO_jogIncrement ) = 0.0001 Then                setUserLed( pokeysOutoput_MPG_X1, 1 )                setUserLed( pokeysOutoput_MPG_X10, 0 )            End If                        If GetOEMDRO( oemDRO_jogIncrement ) = 0.001 Then                setUserLed( pokeysOutoput_MPG_X1, 0 )                setUserLed( pokeysOutoput_MPG_X10, 1 )            End If        Else            setUserLed( pokeysOutoput_MPG_X1, 0 )            setUserLed( pokeysOutoput_MPG_X10, 0 )        End If            ' MPG 100X (step/Velocity Mode)        If GetOEMLED( oemLED_MPGstepVelocityMode ) And GetOEMLED( oemLED_JogEnable ) Then            setUserLed( pokeysOutoput_MPG_X100, 1 )        Else            setUserLed( pokeysOutoput_MPG_X100, 0 )        End If    '    ' System State/Opperating Mode (Idle, RUN, ATC, OFFSET, MPG )    '            ' Mode LEDS 'NULL MODE'        If GetUserDro( userDRO_runMode ) = runMode_IDLE Then            setUserLed( pokeysOutoput_modeRUN, 1 )            setUserLed( pokeysOutoput_modeATC, 1 )            setUserLed( pokeysOutoput_modeOFFSET, 1 )        End If            ' Mode LEDS 'RUN MODE'        If GetUserDro( userDRO_runMode ) = runMode_RUN Then            setUserLed( pokeysOutoput_modeRUN, 0 )            setUserLed( pokeysOutoput_modeATC, 1 )            setUserLed( pokeysOutoput_modeOFFSET, 1 )                                    If GetPage() <> screen_RUN And getUserLED( userLED_atcInitialized ) = 1 Then                DoOEMButton( screen_RUN )            End If        End If            ' Mode LEDS 'ATC MODE'        If GetUserDro( userDRO_runMode ) = runMode_ATC Then            setUserLed( pokeysOutoput_modeRUN, 1 )            setUserLed( pokeysOutoput_modeATC, 0 )            setUserLed( pokeysOutoput_modeOFFSET, 1 )                        If GetPage() <> screen_ATC And getUserLED( userLED_atcInitialized ) = 1 Then                DoOEMButton( screen_ATC )            End If        End If            ' Mode LEDS 'OFFSET MODE'        If GetUserDro( userDRO_runMode ) = runMode_OFFSET Then            setUserLed( pokeysOutoput_modeRUN, 1 )            setUserLed( pokeysOutoput_modeATC, 1 )            setUserLed( pokeysOutoput_modeOFFSET, 0 )                    If GetPage() <> screen_OFFSET And getUserLED( userLED_atcInitialized ) = 1 Then                DoOEMButton( screen_OFFSET )            End If        End If            ' Mode LEDS 'MPG MODE' Part 1        If GetUserDro( userDRO_runMode ) = runMode_MPG Then            setUserLed( pokeysOutoput_modeRUN, 1 )            setUserLed( pokeysOutoput_modeATC, 1 )            setUserLed( pokeysOutoput_modeOFFSET, 1 )                        If GetPage() <> screen_MPG And getUserLED( userLED_atcInitialized ) = 1 Then                DoOEMButton( screen_MPG )            End If        End If            ' Mode LEDS 'MPG MODE' Part 2        If GetOEMLED( oemLED_JogEnable ) Then            SetUserDro( userDRO_runMode, runMode_MPG )        End IfEnd Sub '**********************************************************************''   mapOututsToLEDS''   Updates UserInterface LEDS based on Output Signal Status''**********************************************************************Sub mapOututsToLEDS()    ' ATC Tool RAM    If IsOutputActive( output_RamSolenoid ) Then        setUserLed( userLED_toolRam, 1 )    Else        setUserLed( userLED_toolRam, 0 )    End If        ' Spindle Air Blast    If IsOutputActive( output_AirBlastSolenoid ) Then        setUserLed( userLED_airBlast, 1 )    Else        setUserLed( userLED_airBlast, 0 )    End If        ' Spindle Draw Bar    If IsOutputActive( output_drawBarSolenoid ) Then        setUserLed( userLED_drawBar, 1 )    Else        setUserLed( userLED_drawBar, 0 )    End If    End Sub '**********************************************************************''   updateSpindleData''   Updates the RPM, HZ and Amps DROs''**********************************************************************Sub updateSpindleRealtimeDROs()        '    ' Update the DROs from the Modbus parameters    '        RPM = GetInput( modbusReg_RPM )        setUserDro( userDRO_spindleRPM, RPM )                TargetHz = GetInput( modbusReg_TargetHz ) / 100        setUserDro( userDRO_spindleTargetHz, TargetHz )                Hz = GetInput( modbusReg_Hz ) / 100        setUserDro( userDRO_spindleHz, Hz )                Amps = GetInput( modbusReg_Amps ) / 10        setUserDro( userDRO_spindleAmps, Amps )    '    ' Update the VFD Status LEDs    '        Status = GetInput( modbusReg_Status )                ' Update the Forward Led        If (( Status And &H0004 ) = &H0000 ) And (( Status And &H0001 ) = &H0001 ) Then            setUserLed( userLED_spindleForward, 1 )        Else            setUserLed( userLED_spindleForward, 0 )        End If                ' Update the Reverse Led        If (( Status And &H0004 ) = &H0004 ) And (( Status And &H0001 ) = &H0001 ) Then            setUserLed( userLED_spindleReverse, 1 )        Else            setUserLed( userLED_spindleReverse, 0 )        End If        End Sub '**********************************************************************''   processPoKeysMacro''   Calls the given code and waits for it to complete''**********************************************************************Sub processPoKeysMacro( ByVal macroCodeNumber As Integer )        ' Check if the semephore is currently pending    If GetVar( macroCodeNumber ) = 1 Then        Exit Sub    End If    ' Make sure we are not in Estop    If isEstop() Then        Exit Sub    End If    ' Make sure the system is Initialized first    If checkForInit = 1 Then        If getUserLED( userLED_atcInitialized ) = 0 Then            Exit Sub        End If    End If    ' Set a Semephore based on the macroFileName Number    SetVar( macroCodeNumber, 1 )        ' Call the sub macro function    Code "M" & macroCodeNumber        ' Wait for the Macro to complete    timeout = 0    While GetVar( macroCodeNumber ) = 1        Sleep 10                ' The INIT can take several seconds, so we do not have a timeout        If checkForInit = 1 Then            ' Check for TimeOut ( 5 Second )            Timeout = Timeout + 1            If Timeout = 500 Then                Message "!!! " & macroCodeNumber & " !!! macoPump Timeout"                Sleep 1000                                DoOEMButton( oemButton_eStop )                Exit Sub            End If        End If    WendEnd Sub  