//home by bove to physical limits and set these to -5000um

define(PLC_Homing='1')

open plc PLC_Homing
define(status='P100',timer='P101')

L20=Motor[2].MaxDac
Motor[2].MaxDac=500
L21=Motor[2].FatalFeLimit
Motor[2].FatalFeLimit=2000
L22=Motor[2].JogSpeed
Motor[2].JogSpeed=1

L30=Motor[3].MaxDac
Motor[3].MaxDac=500
L31=Motor[3].FatalFeLimit
Motor[3].FatalFeLimit=2000
L32=Motor[3].JogSpeed
Motor[3].JogSpeed=1


Motor[2].PhaseFindingStep=1
Motor[3].PhaseFindingStep=1
timer = Sys.RunTime + 1;while (Sys.RunTime < timer){} //wait 1 sec

jog-2; jog-3;

status=1
while(1)
{
  if(Motor[2].FeWarn==1 && (status&2)==0)
  {
    status=status|2
    jog/2
    Motor[2].PhasePos=310
    Motor[2].HomePos=Motor[2].ActPos+5000
    jog2=0
  }
  if(Motor[3].FeWarn==1 && (status&4)==0)
  {
    status=status|4
    jog/3
    Motor[3].PhasePos=1210
    Motor[3].HomePos=Motor[3].ActPos+5000
    jog3=0
  }
  if(status==7)
  {
    status=status|8
    break
  }
}

timer = Sys.RunTime + 2
while (Sys.RunTime < timer){} //wait 2 sec
status=100
timer = Sys.RunTime + 2
status=200
while (Sys.RunTime < timer){} //wait 2 sec
status=201

Motor[2].MaxDac=L20
Motor[2].FatalFeLimit=L21
Motor[2].JogSpeed=L22
Motor[3].MaxDac=L30
Motor[3].FatalFeLimit=L31
Motor[3].JogSpeed=L32

disable plc PLC_Homing
close
