FUNCTION "1032_FC Manual function" : Void
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_TEMP 
      wPosition : Word;
      xrtCurrentLimit : Bool;
      g : Int;
      p : Int;
      m : Int;
      b : Int;
   END_VAR


BEGIN
	
	#g := "DB HMI_1".nGWNumber;
	#p := "DB HMI_1".nPortNumber;
	#b := "DB HMI_1".nBoxNumber;
	#m := "DB HMI_1".nMotorNumber;
	
	"rtMotInPos"(CLK:="DB HMI_1".xMotStatInPos );
	
	IF "DB Cycle".Man THEN
	    
	    // Run Forward
	    IF "DB HMI_1".xPBMotMoveFw AND
	        "DB MotorPar".GW[#g].P[#p].Box[#b].Mot[#m].xEnable THEN
	        "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Sign := TRUE;
	        "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].PosType := FALSE;
	        "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Position := "DB HMI_1".nPosition;
	    END_IF;
	    
	    // Run Backward
	    IF "DB HMI_1".xPBMotMoveBw AND
	        "DB MotorPar".GW[#g].P[#p].Box[#b].Mot[#m].xEnable THEN
	        "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Sign := FALSE;
	        "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].PosType := FALSE;
	        "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Position := "DB HMI_1".nPosition;
	    END_IF;
	    
	    // Run Zero/Position
	    IF "DB HMI_1".xPBMotMoveZeroPos AND
	        "DB MotorPar".GW[#g].P[#p].Box[#b].Mot[#m].xEnable THEN
	        "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].PosType := TRUE;
	        "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Position := "DB HMI_1".nPosition;
	    END_IF;
	    
	    // Stop
	    IF NOT "DB Cycle".xZona_MoveManFw AND NOT "DB Cycle".xZona_MoveManBw THEN
	        IF "DB HMI_1".xPBMotStop OR "rtMotInPos".Q OR "DB HMI_1".xMotStatAlarm THEN
	            "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Position    := 0;
	            "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].PosType     := FALSE;
	            "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Reset       := FALSE;
	            "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Sign        := FALSE;
	            "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Stop        := FALSE;
	        END_IF;
	        IF ("DB HMI_1".xPBMotMoveFw OR "DB HMI_1".xPBMotMoveBw OR "DB HMI_1".xPBMotMoveZeroPos) AND
	            "DB MotorPar".GW[#g].P[#p].Box[#b].Mot[#m].xEnable THEN
	            "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Stop := TRUE;
	        END_IF;
	    END_IF;
	    
	END_IF;
	    
	    // Alarm Reset
	IF "DB HMI_1".xPBMotAlarmReset AND "DB Gateway".N[#g].read.P[#p].MotorsBoxStatus[#b].MotorStatus[#m].Alarm THEN
	    "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Reset := TRUE;
	    "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Stop := TRUE;
	    "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Sign := FALSE;
	    "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].PosType := FALSE;
	END_IF;
	
	"rtPB_MotAlmReset"(CLK:= NOT "DB HMI_1".xPBMotAlarmReset);
	
	IF "rtPB_MotAlmReset".Q THEN
	        "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Reset := FALSE;
	        "DB Gateway".N[#g].write.P[#p].MotorBoxCtrl[#b].Mot[#m].Stop := FALSE;
	END_IF;
	    
	    
	    // Motor Current limitation
	"rtPB_CurrentLimit"(CLK:="DB HMI_1".xPopUpCurrentLim,
	                        Q=>#xrtCurrentLimit);
	    IF "rtPB_CurrentLimit".Q THEN
	        "DB HMI_1".snCurrentLimFW := "DB MotorPar".GW[#g].P[#p].Box[#b].Mot[#m].CurrentLimFW;
	        "DB HMI_1".snCurrentLimBW := "DB MotorPar".GW[#g].P[#p].Box[#b].Mot[#m].CurrentLimBW;
	    END_IF;
	    IF "DB HMI_1".xPopUpCurrentLim AND NOT "rtPB_CurrentLimit".Q THEN
	        "DB MotorPar".GW[#g].P[#p].Box[#b].Mot[#m].CurrentLimFW := "DB HMI_1".snCurrentLimFW;
	        "DB MotorPar".GW[#g].P[#p].Box[#b].Mot[#m].CurrentLimBW := "DB HMI_1".snCurrentLimBW;
	    END_IF;
	    IF NOT "DB HMI_1".xPopUpCurrentLim THEN
	        "DB HMI_1".snCurrentLimFW := 0;
	        "DB HMI_1".snCurrentLimBW := 0;
	    END_IF;
	    
	    // HMI Motor Status
	    "DB HMI_1".snGatewayFirmware      := "DB Gateway".N[#g].read.P[#p].Firmware;
	    "DB HMI_1".nBoxInstalled          := USINT_TO_INT ("DB Gateway".N[#g].read.P[#p].MboxNumber);
	    "DB HMI_1".snBoxFirmwareVersion   := "DB Gateway".N[#g].read.P[#p].MotorsBoxStatus[#b].BoxStatus.FirmwareVersion;
	    "DB HMI_1".snBoxFirmwareRevision  := "DB Gateway".N[#g].read.P[#p].MotorsBoxStatus[#b].BoxStatus.FirmwareRevision;
	    "DB HMI_1".xBox_MotEnabled        := "DB MotorPar".GW[#g].P[#p].Box[#b].Mot[#m].xEnable;
	    "DB HMI_1".xMotStatRunningFw      := "DB Gateway".N[#g].read.P[#p].MotorsBoxStatus[#b].MotorStatus[#m].MovingFW;
	    "DB HMI_1".xMotStatRunningBw      := "DB Gateway".N[#g].read.P[#p].MotorsBoxStatus[#b].MotorStatus[#m].MovingBW;
	    "DB HMI_1".xMotStatInPos          := "DB Gateway".N[#g].read.P[#p].MotorsBoxStatus[#b].MotorStatus[#m].InPOS;
	    "DB HMI_1".xMotStatAlarm          := "DB Gateway".N[#g].read.P[#p].MotorsBoxStatus[#b].MotorStatus[#m].Alarm;
	    "DB HMI_1".xMotStatInZeroPos      := "DB Gateway".N[#g].read.P[#p].MotorsBoxStatus[#b].MotorStatus[#m].InZero;
	    "DB HMI_1".xMotStatRunningSlowly  := "DB Gateway".N[#g].read.P[#p].MotorsBoxStatus[#b].MotorStatus[#m].MovingSlowly;
	    "DB HMI_1".xBoxFuseBurned         := "DB Gateway".N[#g].read.P[#p].MotorsBoxStatus[#b].BoxStatus.BurnedFuse;
	    "DB HMI_1".xBoxUndervoltage       := "DB Gateway".N[#g].read.P[#p].MotorsBoxStatus[#b].BoxStatus.Undervoltage;
	    IF ("DB HMI_1".xMotStatRunningFw OR "DB HMI_1".xMotStatRunningBw) AND "DB Cycle".Man THEN
	        "DB HMI_1".xMotStatRunning := 1;
	    ELSE
	        "DB HMI_1".xMotStatRunning := 0;
	    END_IF;
END_FUNCTION

