targets/plc_main_tail.c
changeset 1428 e14003eb4d42
parent 1093 b5f78cff4459
child 1479 8f41aa88aa46
equal deleted inserted replaced
1423:4e33f644f7cf 1428:e14003eb4d42
   173 			/*adjust calibration_count*/
   173 			/*adjust calibration_count*/
   174 			calibration_count++;
   174 			calibration_count++;
   175 			/* compute mean of Tsync, over calibration period */
   175 			/* compute mean of Tsync, over calibration period */
   176 			Tsync = ((long long)(cal_end.tv_sec - cal_begin.tv_sec) * (long long)1000000000 +
   176 			Tsync = ((long long)(cal_end.tv_sec - cal_begin.tv_sec) * (long long)1000000000 +
   177 					(cal_end.tv_nsec - cal_begin.tv_nsec)) / calibration_count;
   177 					(cal_end.tv_nsec - cal_begin.tv_nsec)) / calibration_count;
   178 			if( (Nticks = (Tsync / Ttick)) > 0){
   178 			if( (Nticks = (Tsync / common_ticktime__)) > 0){
   179 				FreqCorr = (Tsync % Ttick); /* to be divided by Nticks */
   179 				FreqCorr = (Tsync % common_ticktime__); /* to be divided by Nticks */
   180 			}else{
   180 			}else{
   181 				FreqCorr = Tsync - (Ttick % Tsync);
   181 				FreqCorr = Tsync - (common_ticktime__ % Tsync);
   182 			}
   182 			}
   183 			/*
   183 			/*
   184 			printf("Tsync = %ld\n", Tsync);
   184 			printf("Tsync = %ld\n", Tsync);
   185 			printf("calibration_count = %d\n", calibration_count);
   185 			printf("calibration_count = %d\n", calibration_count);
   186 			printf("Nticks = %d\n", Nticks);
   186 			printf("Nticks = %d\n", Nticks);
   195 			long long PhaseCorr;
   195 			long long PhaseCorr;
   196 			long long PeriodicTcorr;
   196 			long long PeriodicTcorr;
   197 			PLC_GetTime(&now);
   197 			PLC_GetTime(&now);
   198 			elapsed = (now.tv_sec - __CURRENT_TIME.tv_sec) * 1000000000 + now.tv_nsec - __CURRENT_TIME.tv_nsec;
   198 			elapsed = (now.tv_sec - __CURRENT_TIME.tv_sec) * 1000000000 + now.tv_nsec - __CURRENT_TIME.tv_nsec;
   199 			if(Nticks > 0){
   199 			if(Nticks > 0){
   200 				PhaseCorr = elapsed - (Ttick + FreqCorr/Nticks)*sync_align_ratio/100; /* to be divided by Nticks */
   200 				PhaseCorr = elapsed - (common_ticktime__ + FreqCorr/Nticks)*sync_align_ratio/100; /* to be divided by Nticks */
   201 				Tcorr = Ttick + (PhaseCorr + FreqCorr) / Nticks;
   201 				Tcorr = common_ticktime__ + (PhaseCorr + FreqCorr) / Nticks;
   202 				if(Nticks < 2){
   202 				if(Nticks < 2){
   203 					/* When Sync source period is near Tick time */
   203 					/* When Sync source period is near Tick time */
   204 					/* PhaseCorr may not be applied to Periodic time given to timer */
   204 					/* PhaseCorr may not be applied to Periodic time given to timer */
   205 					PeriodicTcorr = Ttick + FreqCorr / Nticks;
   205 					PeriodicTcorr = common_ticktime__ + FreqCorr / Nticks;
   206 				}else{
   206 				}else{
   207 					PeriodicTcorr = Tcorr;
   207 					PeriodicTcorr = Tcorr;
   208 				}
   208 				}
   209 			}else if(__tick > last_tick){
   209 			}else if(__tick > last_tick){
   210 				last_tick = __tick;
   210 				last_tick = __tick;
   211 				PhaseCorr = elapsed - (Tsync*sync_align_ratio/100);
   211 				PhaseCorr = elapsed - (Tsync*sync_align_ratio/100);
   212 				PeriodicTcorr = Tcorr = Ttick + PhaseCorr + FreqCorr;
   212 				PeriodicTcorr = Tcorr = common_ticktime__ + PhaseCorr + FreqCorr;
   213 			}else{
   213 			}else{
   214 				/*PLC did not run meanwhile. Nothing to do*/
   214 				/*PLC did not run meanwhile. Nothing to do*/
   215 				return;
   215 				return;
   216 			}
   216 			}
   217 			/* DO ALIGNEMENT */
   217 			/* DO ALIGNEMENT */