
PROGRAM _INIT
	(* Insert code here *)
	RCP_FILM.CAM[0].MasterCompDistance := 50.0;
	RCP_FILM.CAM[0].SlaveCompDistance  := 30.0;
	RCP_FILM.CAM[0].MasterFactor       := 140.0;
	RCP_FILM.CAM[0].SlaveFactor        := 140.0;
	
	RCP_FILM.CAM[1].MasterCompDistance := 173.6;
	RCP_FILM.CAM[1].SlaveCompDistance  := 278.6;
	RCP_FILM.CAM[1].MasterFactor       := 72.5;
	RCP_FILM.CAM[1].SlaveFactor        := 146.7;

	RCP_FILM.CAM[2].MasterCompDistance := 113.9;
	RCP_FILM.CAM[2].SlaveCompDistance  := 124.7;
	RCP_FILM.CAM[2].MasterFactor       := 1.0;
	RCP_FILM.CAM[2].SlaveFactor        := 0;
	
	RCP_FILM.CAM[3].MasterCompDistance := 143.1;
	RCP_FILM.CAM[3].SlaveCompDistance  := 177.4;
	RCP_FILM.CAM[3].MasterFactor       := 31.3;
	RCP_FILM.CAM[3].SlaveFactor        := 52.5;
	
	RCP_FILM.CAM[4].MasterCompDistance := 245.2;
	RCP_FILM.CAM[4].SlaveCompDistance  := 300.1;
	RCP_FILM.CAM[4].MasterFactor       := 190.0;
	RCP_FILM.CAM[4].SlaveFactor        := 190.0;
	
	
	cmdShow := TRUE;
END_PROGRAM

PROGRAM _CYCLIC
	(* Insert code here *)
//  配方读取成功才会往下执行	
//	IF gRcpLoadValid=FALSE OR gRcpFixCtrl.monitor.initOK=0 THEN
//		RETURN;
//	END_IF;
	
	//送膜轴	
	(*********************************Check refresh time***********************************************)
	IF(current_page = 221)THEN//不是曲线显示界面，不执行

		//防止除零	
		IF RCP_FILM.CAM[0].MasterFactor < 0.1  THEN RCP_FILM.CAM[0].MasterFactor := 1; END_IF;
		IF RCP_FILM.CAM[1].MasterFactor < 0.1  THEN RCP_FILM.CAM[1].MasterFactor := 1; END_IF;
		IF RCP_FILM.CAM[2].MasterFactor < 0.1  THEN RCP_FILM.CAM[2].MasterFactor := 1; END_IF;
		IF RCP_FILM.CAM[3].MasterFactor < 0.1  THEN RCP_FILM.CAM[3].MasterFactor := 1; END_IF;
		IF RCP_FILM.CAM[4].MasterFactor < 0.1  THEN RCP_FILM.CAM[4].MasterFactor := 1; END_IF;
		
		Master_all := RCP_FILM.CAM[0].MasterCompDistance + RCP_FILM.CAM[0].MasterFactor + RCP_FILM.CAM[1].MasterCompDistance + RCP_FILM.CAM[1].MasterFactor + RCP_FILM.CAM[2].MasterCompDistance + RCP_FILM.CAM[2].MasterFactor + RCP_FILM.CAM[3].MasterCompDistance + RCP_FILM.CAM[3].MasterFactor + RCP_FILM.CAM[4].MasterCompDistance + RCP_FILM.CAM[4].MasterFactor;
		
		Slave_all := RCP_FILM.CAM[0].SlaveCompDistance + RCP_FILM.CAM[0].SlaveFactor + RCP_FILM.CAM[1].SlaveCompDistance + RCP_FILM.CAM[1].SlaveFactor + RCP_FILM.CAM[2].SlaveCompDistance + RCP_FILM.CAM[2].SlaveFactor + RCP_FILM.CAM[3].SlaveCompDistance + RCP_FILM.CAM[3].SlaveFactor + RCP_FILM.CAM[4].SlaveCompDistance + RCP_FILM.CAM[4].SlaveFactor;
		
		
		
		//顺序为CAM[0 → 3 → 4 → 1 → 2]
		//CAM[0] 起点**********************************************************************************************************************************
		MC_CAM_SECTIONS_TYP_0[0].Section[0].MasterPosition	:= 0;
		MC_CAM_SECTIONS_TYP_0[0].Section[0].SlavePosition	:= 0;
		MC_CAM_SECTIONS_TYP_0[0].Section[0].FirstDerivative	:= 0;
		MC_CAM_SECTIONS_TYP_0[0].Section[0].Mode			:= 0;//0：绝对模式；1：相对模式
		MC_CAM_SECTIONS_TYP_0[0].Section[0].Type			:= 4;//5阶多项式-根据实际修改
		
		//CAM[0] 补偿段终点
		MC_CAM_SECTIONS_TYP_0[0].Section[1].MasterPosition	:= RCP_FILM.CAM[0].MasterCompDistance*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[1].SlavePosition	:= RCP_FILM.CAM[0].SlaveCompDistance*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[1].FirstDerivative := RCP_FILM.CAM[0].SlaveFactor / RCP_FILM.CAM[0].MasterFactor;//速比
		MC_CAM_SECTIONS_TYP_0[0].Section[1].Mode			:= 1;//0：绝对模式；1：相对模式
		MC_CAM_SECTIONS_TYP_0[0].Section[1].Type			:= 1;//直线
	
		//CAM[0] 同步段终点
		MC_CAM_SECTIONS_TYP_0[0].Section[2].MasterPosition	:= RCP_FILM.CAM[0].MasterFactor*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[2].SlavePosition	:= RCP_FILM.CAM[0].SlaveFactor*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[2].FirstDerivative := RCP_FILM.CAM[0].SlaveFactor / RCP_FILM.CAM[0].MasterFactor;//速比
		MC_CAM_SECTIONS_TYP_0[0].Section[2].Mode			:= 1;//0：绝对模式；1：相对模式
		MC_CAM_SECTIONS_TYP_0[0].Section[2].Type			:= 4;//5阶多项式-根据实际修改
	
		//CAM[3] 补偿段终点**********************************************************************************************************************************	
		MC_CAM_SECTIONS_TYP_0[0].Section[3].MasterPosition	:= RCP_FILM.CAM[3].MasterCompDistance*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[3].SlavePosition	:= RCP_FILM.CAM[3].SlaveCompDistance*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[3].FirstDerivative := RCP_FILM.CAM[3].SlaveFactor / RCP_FILM.CAM[3].MasterFactor;
		MC_CAM_SECTIONS_TYP_0[0].Section[3].Mode		    := 1;//0：绝对模式；1：相对模式
		MC_CAM_SECTIONS_TYP_0[0].Section[3].Type	     	:= 1;//直线
	
	
		//CAM[3] 同步段终点
		MC_CAM_SECTIONS_TYP_0[0].Section[4].MasterPosition	:= RCP_FILM.CAM[3].MasterFactor*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[4].SlavePosition	:= RCP_FILM.CAM[3].SlaveFactor*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[4].FirstDerivative := RCP_FILM.CAM[3].SlaveFactor / RCP_FILM.CAM[3].MasterFactor;//速比
		MC_CAM_SECTIONS_TYP_0[0].Section[4].Mode			:= 1;//0：绝对模式；1：相对模式
		MC_CAM_SECTIONS_TYP_0[0].Section[4].Type			:= 4;//5阶多项式-根据实际修改
		
	
		//CAM[4] 补偿段终点
		MC_CAM_SECTIONS_TYP_0[0].Section[5].MasterPosition	:= RCP_FILM.CAM[4].MasterCompDistance*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[5].SlavePosition	:= RCP_FILM.CAM[4].SlaveCompDistance*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[5].FirstDerivative := RCP_FILM.CAM[4].SlaveFactor / RCP_FILM.CAM[4].MasterFactor;//速比
		MC_CAM_SECTIONS_TYP_0[0].Section[5].Mode			:= 1;//0：绝对模式；1：相对模式
		MC_CAM_SECTIONS_TYP_0[0].Section[5].Type			:= 1;//直线	
		
		
		//CAM[4] 同步段终点
		MC_CAM_SECTIONS_TYP_0[0].Section[6].MasterPosition	:= RCP_FILM.CAM[4].MasterFactor*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[6].SlavePosition	:= RCP_FILM.CAM[4].SlaveFactor*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[6].FirstDerivative := RCP_FILM.CAM[4].SlaveFactor / RCP_FILM.CAM[4].MasterFactor;//速比
		MC_CAM_SECTIONS_TYP_0[0].Section[6].Mode			:= 1;//0：绝对模式；1：相对模式
		MC_CAM_SECTIONS_TYP_0[0].Section[6].Type			:= 4;//5阶多项式-根据实际修改	
	
	
		//CAM[1] 补偿段终点
		MC_CAM_SECTIONS_TYP_0[0].Section[7].MasterPosition	:= RCP_FILM.CAM[1].MasterCompDistance*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[7].SlavePosition	:= RCP_FILM.CAM[1].SlaveCompDistance*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[7].FirstDerivative := RCP_FILM.CAM[1].SlaveFactor / RCP_FILM.CAM[1].MasterFactor;//速比
		MC_CAM_SECTIONS_TYP_0[0].Section[7].Mode		    := 1;//0：绝对模式；1：相对模式
		MC_CAM_SECTIONS_TYP_0[0].Section[7].Type	    	:= 1;//直线
		
		
		//CAM[1] 同步段终点
		MC_CAM_SECTIONS_TYP_0[0].Section[8].MasterPosition	:= RCP_FILM.CAM[1].MasterFactor*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[8].SlavePosition	:= RCP_FILM.CAM[1].SlaveFactor*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[8].FirstDerivative := RCP_FILM.CAM[1].SlaveFactor / RCP_FILM.CAM[1].MasterFactor;//速比
		MC_CAM_SECTIONS_TYP_0[0].Section[8].Mode		    := 1;//0：绝对模式；1：相对模式
		MC_CAM_SECTIONS_TYP_0[0].Section[8].Type	    	:= 4;//5阶多项式-根据实际修改	
		
	
		//CAM[2] 补偿段终点
		MC_CAM_SECTIONS_TYP_0[0].Section[9].MasterPosition	:= RCP_FILM.CAM[2].MasterCompDistance*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[9].SlavePosition	:= RCP_FILM.CAM[2].SlaveCompDistance*10;//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[9].FirstDerivative := RCP_FILM.CAM[2].SlaveFactor / RCP_FILM.CAM[2].MasterFactor;//速比
		MC_CAM_SECTIONS_TYP_0[0].Section[9].Mode			:= 1;//0：绝对模式；1：相对模式
		MC_CAM_SECTIONS_TYP_0[0].Section[9].Type			:= 1;//直线
		
		//CAM[2] 同步段终点
		MC_CAM_SECTIONS_TYP_0[0].Section[10].MasterPosition	:= DINT_TO_REAL(REAL_TO_DINT((Master_all)*10));//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[10].SlavePosition	:= DINT_TO_REAL(REAL_TO_DINT((Slave_all)*10));//最后一点的位置必须是整数
		MC_CAM_SECTIONS_TYP_0[0].Section[10].FirstDerivative := RCP_FILM.CAM[2].SlaveFactor / RCP_FILM.CAM[2].MasterFactor;//速比
		MC_CAM_SECTIONS_TYP_0[0].Section[10].Mode			:= 0;//最后一点的位置必须指定为绝对
		MC_CAM_SECTIONS_TYP_0[0].Section[10].Type			:= 0;//最后一点必须为零


	END_IF
	

	

	
	
	
	
	//切换画面自动执行一次	
	IF EDGEPOS(current_page = 221) THEN
		cmdShow := TRUE;
	END_IF;
	
	
	IF cmdShow THEN
		cmdShow		:= FALSE;
		GetCamPointsFinish	:= 0;
		GetCamPointsStep	:= 5;		
	END_IF;
	
	
	CASE GetCamPointsStep OF
		0:
		
		5:
			IF (current_page = 221) THEN//送膜轴
				//	主轴位置获取
				MasterPositions[i] := 0;
				FOR i:=1 TO 999 DO
					MasterPositions[i] := Master_all*10.0/999+MasterPositions[i-1];//unit，1mm=10nit
				END_FOR;
				GetCamPointsStep		:= 10;					
			END_IF;	
			
		10:	
			//********************************************************************************/
			//* 计算凸轮曲线 *//
			//********************************************************************************/	
			IF (current_page = 221) THEN//送膜轴	
				MC_BR_CalcCamFromSections_0.DataAddress := ADR(MC_CAM_SECTIONS_TYP_0[0]);
				MC_BR_CalcCamFromSections_0.CamProfileAddress := ADR(MC_CAMPROFILE_TYP_0[0]);	
			END_IF
			
			MC_BR_CalcCamFromSections_0.Execute     := 1;	
			IF MC_BR_CalcCamFromSections_0.Busy THEN
				GetCamPointsStep					:= 10;//忙中，不跳转
			ELSIF MC_BR_CalcCamFromSections_0.Done THEN
				GetCamPointsStep					:= 15;//完成，进入10步
				MC_BR_CalcCamFromSections_0.Execute := 0;
			ELSIF MC_BR_CalcCamFromSections_0.Error THEN
				GetCamPointsStep					:= 100;//有报错，进入100步
				CamCalcErrorID						:= MC_BR_CalcCamFromSections_0.ErrorID;
				CamCalcErrorInSection               := MC_BR_CalcCamFromSections_0.ErrorInSection;
			END_IF
			
		15://* 保存凸轮曲线 */	
			IF (current_page = 221) THEN//送膜轴	
				strcpy(ADR(MC_BR_SaveCamProfileObj_0.DataObjectName),ADR('FilmCam'));
				MC_BR_SaveCamProfileObj_0.DataAddress := ADR(MC_CAMPROFILE_TYP_0[0]);	
			END_IF
		
			MC_BR_SaveCamProfileObj_0.DataObjectVersion := 0;
			MC_BR_SaveCamProfileObj_0.Execute     := 1;
			IF MC_BR_SaveCamProfileObj_0.Done THEN
				MC_BR_SaveCamProfileObj_0.Execute	:=FALSE;
				GetCamPointsStep					:=20;
			ELSIF (MC_BR_SaveCamProfileObj_0.ErrorID <> 0) THEN
				CamCalcErrorID						:=	MC_BR_SaveCamProfileObj_0.ErrorID;
				MC_BR_SaveCamProfileObj_0.Execute	:=FALSE;
				GetCamPointsStep					:=100;(*Error Handling 暂时不用*)
			END_IF;
			
		20:	//*画图凸轮曲线数据获取 */
			IF (current_page = 221) THEN//送膜轴	
				strcpy(ADR(MC_BR_CalcPointsFromCam_0.DataObjectName),ADR('FilmCam'));
			END_IF

			MC_BR_CalcPointsFromCam_0.MasterPointsAddress := 0;
			MC_BR_CalcPointsFromCam_0.SlavePointsAddress  := ADR(SlavePositions[0]);//unit
			MC_BR_CalcPointsFromCam_0.NumberOfPoints      := 1000;
			MC_BR_CalcPointsFromCam_0.Execute             := 1;
			IF (MC_BR_CalcPointsFromCam_0.Done )THEN
				MC_BR_CalcPointsFromCam_0.Execute := 0;
				SlaveSpeed[0]       := 0;
				MinSpeedRatio   := 0;
				MaxSpeedRatio   := 0;
				FOR i:=1 TO DISP_POINTS-1 DO	
					MasterSpeed[i]  := MasterPositions[i] - MasterPositions[i-1];
					SlaveSpeed[i]   := SlavePositions[i] - SlavePositions[i-1];	
					IF MasterSpeed[i] < 0.1  THEN
						MasterSpeed[i] := 10;
					END_IF;
					MasterSpeed[0] := MasterSpeed[10];
					IF  SlaveSpeed[i]/MasterSpeed[i] > MaxSpeedRatio THEN
						MaxSpeedRatio := SlaveSpeed[i]/MasterSpeed[i];
					END_IF;
				
					IF  SlaveSpeed[i]/MasterSpeed[i] < MinSpeedRatio THEN
						MinSpeedRatio := SlaveSpeed[i]/MasterSpeed[i];
					END_IF;
				END_FOR;	
				GetCamPointsStep	:= 25;
			ELSIF MC_BR_CalcPointsFromCam_0.ErrorID <> 0 THEN
				CamCalcErrorID      := MC_BR_CalcPointsFromCam_0.ErrorID;
				GetCamPointsStep	:= 100;
			END_IF
			
		25:
			GetCamPointsFinish	:= 1;
			GetCamPointsStep	:= 0;
		100://报错
			MC_BR_CalcCamFromSections_0.Execute     := 0;	
			MC_BR_SaveCamProfileObj_0.Execute	    := 0;
			MC_BR_CalcPointsFromCam_0.Execute       := 0;
			GetCamPointsFinish	                    := 0;
			GetCamPointsStep                    	:= 0;	
		
	END_CASE;
	MC_BR_CalcCamFromSections_0();
	MC_BR_SaveCamProfileObj_0();
	MC_BR_CalcPointsFromCam_0();

END_PROGRAM

PROGRAM _EXIT
	(* Insert code here *)
	 
END_PROGRAM

