'**********************************************************************
'
'   M1006.mls   INC ATC Tool Pos                            17OCT2011
'
'   Rotates the ATC Carousel (INC)
'
'   1) Checks the Index Sensor for LOW and HIGH changes
'   2) Checks and Updates the ATC Init State
'
'   17OCT2011: Added support for DM4800 Hardware
'   06DEC2010: Added support for MacroPump
'   04AUG2010: Added debug and status messages
'
'**********************************************************************
Const userLable_atcDebug            = 1
Const userLable_atcStatus           = 2
Const userLED_atcInitialized        = 1000
Const userDRO_atcCurrentPosition    = 1000

Const IndexSW                       = Input2
Const HomeSW                        = Input1
Const MotorEnable                   = Output2
Const MotorDirection                = Output1

Sub Main()
    Dim Timeout As Integer    

    ' Make sure the ATC has been Initialized
    If getUserLED( userLED_atcInitialized ) = 0 Then
        EstopMessage( "M1006.mls ATC must be INITIALIZED before using ATC INC Function" )
        Exit Sub
        
    End If

    ' Set up the direction on Starter Capacitor (INC)
    DeActivateSignal( MotorDirection )
    Sleep 100

    ' Enable Motor
    ActivateSignal( MotorEnable )

    '
    ' Watch the the ATC Carousel move 1 Position
    '
        ' Make sure we clear the Index Switch
        Timeout = 0
        While IsActive( IndexSW )
            Sleep 1
            
            ' Check for TimeOut ( 3 Second )
            Timeout = Timeout + 1
            If Timeout = 3000 Then
                EstopMessage( "M1006.mls Failed to clear ATC Index Sw" )
                Exit Sub
                
            End If
        Wend

        ' Make sure we see the Index Switch
        Timeout = 0
        While Not( IsActive( IndexSW ))
            Sleep 1
            
            ' Check for TimeOut ( 3 Second )
            Timeout = Timeout + 1
            If Timeout = 3000 Then
                EstopMessage( "M1006.mls Failed to detect ATC Index Sw" )
                Exit Sub
                
            End If
        Wend

    ' Stop the Motor
    DeActivateSignal( MotorEnable )
    
    ' Update the UI and Save ATC Position
        currentAtcPos = GetUserDRO( userDRO_atcCurrentPosition )
        currentAtcPos = currentAtcPos + 1
        If currentAtcPos = 25 Then currentAtcPos = 1
        Code "M1101 P" & currentAtcPos

    '
    ' Wait for CAP to discharge before deactivating
    '    
    Sleep 300    
    DeActivateSignal( MotorDirection )

    '
    ' If our new carousel position is 1 (ie:HOME), then make sure the Home Sw is active  
    ' 
        ' Check if we should be homed
        If currentAtcPos = 1 Then
            If Not( IsActive( HomeSW )) Then
                EstopMessage( "M1006.mls ATC Home Sensor was not set when it should have been" )
                Exit Sub
                
            End If
        
        Else
            ' Make sure we are NOT homed
             If IsActive( HomeSW ) Then
                EstopMessage( "M1006.mls ATC Home Sensor was SET when it shouldn't have been" )
                Exit Sub
                
            End If
        End If    

    ' Clear the Processing Semephore
    SetVar( 1006, 0 )
    
End Sub 

'**********************************************************************
'
'   EstopMessage                                            Version 1.2
'
'   1) This additional clearing Carousel Number and LEDS
'
'**********************************************************************
Sub EstopMessage( ByVal errorMessage As String )

    Const eStop                         = 1021                  
    Const userLED_atcInitialized        = 1000
    
    ' Update ATC Carousel Number and LEDS to ZERO
    Code "M1101 P0"

    SetUserLabel( userLable_atcStatus, "Error" )
    SetUserLED( userLED_atcInitialized, 0 )            
    
    SetUserLabel( userLable_atcDebug, errorMessage )
    Message "!!! " & errorMessage & " !!!"
    Sleep 1000
    
    DoOEMButton( eStop )
    
End Sub

