Back to top

AKD Basic Sample Program for Indexing and Homing with Home Switch and Limit Switches

'-------------- Device Params -----------------------
Params
End Params

'-------------- Define (dim) Global Variables --------
Dim homefound as integer
Dim targetposition as integer
Dim speed as integer 'speed of initial move to sprayposition
Dim accel as integer 'acceleration for all moves
Dim decel as integer 'deceleration for all moves

MBInfo
End
'-------------- Main Program -------------------------
Main
DRV.SWENABLE = 0
homefound = 0

'Enable Interrupts
INTR.HWLS.POSSTATE = 1
INTR.HWLS.NEGSTATE = 1 'used for homing if home input not reached before limit switch
Intr.DRV.FAULTS = 1
INTR.DISABLE = 1
INTR.DRV.HWENABLE = 1

'Set default values
targetposition = 1016 'total distance, in 0.001mm
speed = 200 'speed of initial move to targetposition
accel = 1000
decel = 1000

Move.Acc = accel  ' Acceleration (drive units)
Move.Dec = decel  ' Deceleration (drive units)
While DRV.HWENABLE = 0  : Wend 'wait for hardware enable

'While 1 = 1 'start continuous loop

DRV.SWENABLE = 0 'enable drive

'Homing, homing uses Input 3 to start homing and Input 4 for the position capture
'the negative limit switch inturrupt is also used for homing
If homefound = 0 Then 'If axis has not been homed, then ...
    Print "waiting for input 3"
    While DIN3.STATE = 0 : Wend 'wait for the start home command
    'start the homing routine
    CAP0.MODE = 0 'standard position capture
    CAP0.TRIGGER = 3 'Use input 4 for position capture
    CAP0.EDGE = 1 'positive edge
    CAP0.EVENT = 0 'no pre-conditions must be met
    CAP0.EN = 1 'Start looking for the capture trigger
    MOVE.RUNSPEED = 200
    MOVE.DIR = 1 'negative direction
    MOVE.GOVEL 'start moving
    Print "waiting for input 4"
    While CAP0.STATE = 0 : Wend 'wait for the capture to trigger, if hits neg limit switch then run interrupt
    Print "captured"
    MOVE.RUNSPEED = 0
    MOVE.GOVEL 'stop moving, zero speed
    While MOVE.MOVING = 1 : Wend 'wait for motion to stop
    MOVE.POSCOMMAND = (PL.FB - CAP0.PLFB ) 'set position to the difference between the actual position and captured position
    Pause (.2)
    MOVE.RUNSPEED = 50
    MOVE.TARGETPOS = 0
    MOVE.GOABS 'move back to the captured position
    Pause (1) 'this is the end of the homing routine
    homefound = 1 'set homefound tag
    Print "home found"
    Print "end of homing"
End If

While 1 = 1
While DRV.HWENABLE = 0  : Wend
Move.RunSpeed = speed
Move.TargetPos = targetposition
MOVE.GOREL
Print "moving"
While MOVE.MOVING = 1 : Wend
Print "finished moving"
Pause (1) 'wait a second before looping to move again

Wend 'loops back to "While 1 = 1"
End Main

'-------------- Subroutines and Functions ------------

'-------------- Interrupt Routines -------------------
Interrupt HWLS.NEGSTATE
    If homefound = 0 Then 'move to the positive side of the home switch
        While MOVE.MOVING = 1 : Wend 'wait to stop moving
        MOVE.DIR = 0 'positive direction
        MOVE.RUNSPEED = 50 'mm/s
        MOVE.GOVEL 'start moving
        While DIN4.STATE = 0 : Wend 'wait to get on the home switch
        While DIN4.STATE = 1 : Wend 'find positive edge of home switch
        Pause (0.5)
        MOVE.RUNSPEED = 0
        MOVE.GOVEL 'stop moving, zero speed
        While MOVE.MOVING = 1 : Wend 'wait to stop moving
        CAP0.EN = 1 'Start looking for the capture trigger
        MOVE.DIR = 1 'negative direction
        MOVE.RUNSPEED = 50 'mm/s
        MOVE.GOVEL 'start moving
    ElseIf homefound = 1 Then
        Print "Restarting - Neg Limit"
        Restart
    End If
    INTR.HWLS.NEGSTATE = 1
End Interrupt

Interrupt HWLS.POSSTATE
    Print "Restarting - Pos Limit"
    Restart
End Interrupt

Interrupt DRV.HWENABLE
    Print "Restarting - HW Enable"
    Restart
End Interrupt