targets/plc_common_main.c
changeset 332 4f0e1d66bba5
parent 289 d17bd2f00a87
child 366 cd90e4c10261
equal deleted inserted replaced
331:191a568a2a17 332:4f0e1d66bba5
     1 /**
     1 /**
     2  * Code common to all C targets
     2  * Code common to all C targets
     3  **/ 
     3  **/
     4 
     4 
       
     5 #include <locale.h>
     5 #include "iec_types.h"
     6 #include "iec_types.h"
     6 
       
     7 /*
     7 /*
     8  * Prototypes of functions provied by generated C softPLC
     8  * Prototypes of functions provied by generated C softPLC
     9  **/ 
     9  **/
    10 void config_run__(int tick);
    10 void config_run__(int tick);
    11 void config_init__(void);
    11 void config_init__(void);
    12 
    12 
    13 /*
    13 /*
    14  * Prototypes of functions provied by generated target C code
    14  * Prototypes of functions provied by generated target C code
    32 
    32 
    33 /* Help to quit cleanly when init fail at a certain level */
    33 /* Help to quit cleanly when init fail at a certain level */
    34 static int init_level = 0;
    34 static int init_level = 0;
    35 
    35 
    36 /*
    36 /*
    37  * Prototypes of functions exported by plugins 
    37  * Prototypes of functions exported by plugins
    38  **/
    38  **/
    39 %(calls_prototypes)s
    39 %(calls_prototypes)s
    40 
    40 
    41 /*
    41 /*
    42  * Retrieve input variables, run PLC and publish output variables 
    42  * Retrieve input variables, run PLC and publish output variables
    43  **/
    43  **/
    44 void __run()
    44 void __run()
    45 {
    45 {
    46     __tick++;
    46     __tick++;
    47 
    47 
    48     %(retrieve_calls)s
    48     %(retrieve_calls)s
    49 
    49 
    50     __retrieve_python();
    50     __retrieve_python();
    51 
    51 
    52     /*__retrieve_debug();*/
    52     /*__retrieve_debug();*/
    53     
    53 
    54     config_run__(__tick);
    54     config_run__(__tick);
    55 
    55 
    56     __publish_python();
    56     __publish_python();
    57 
    57 
    58     __publish_debug();
    58     __publish_debug();
    59     
    59 
    60     %(publish_calls)s
    60     %(publish_calls)s
    61 
    61 
    62 }
    62 }
    63 
    63 
    64 /*
    64 /*
    65  * Initialize variables according to PLC's defalut values,
    65  * Initialize variables according to PLC's defalut values,
    66  * and then init plugins with that values  
    66  * and then init plugins with that values
    67  **/
    67  **/
    68 int __init(int argc,char **argv)
    68 int __init(int argc,char **argv)
    69 {
    69 {
    70     int res;
    70     int res;
       
    71     setlocale(LC_NUMERIC, "C");
    71     config_init__();
    72     config_init__();
    72     __init_debug();
    73     __init_debug();
    73     __init_python();
    74     __init_python();
    74     %(init_calls)s
    75     %(init_calls)s
    75     return 0;
    76     return 0;
    97 static int Nticks = 0;
    98 static int Nticks = 0;
    98 static int  last_tick = 0;
    99 static int  last_tick = 0;
    99 static long long Ttick = 0;
   100 static long long Ttick = 0;
   100 #define mod %%
   101 #define mod %%
   101 /*
   102 /*
   102  * Call this on each external sync, 
   103  * Call this on each external sync,
   103  * @param sync_align_ratio 0->100 : align ratio, < 0 : no align, calibrate period 
   104  * @param sync_align_ratio 0->100 : align ratio, < 0 : no align, calibrate period
   104  **/
   105  **/
   105 void align_tick(int sync_align_ratio)
   106 void align_tick(int sync_align_ratio)
   106 {
   107 {
   107 	/*
   108 	/*
   108 	printf("align_tick(%%d)\n", calibrate);
   109 	printf("align_tick(%%d)\n", calibrate);
   121 			/* Get final time */
   122 			/* Get final time */
   122 			IEC_TIME cal_end;
   123 			IEC_TIME cal_end;
   123 			PLC_GetTime(&cal_end);
   124 			PLC_GetTime(&cal_end);
   124 			/*adjust calibration_count*/
   125 			/*adjust calibration_count*/
   125 			calibration_count++;
   126 			calibration_count++;
   126 			/* compute mean of Tsync, over calibration period */	
   127 			/* compute mean of Tsync, over calibration period */
   127 			Tsync = ((long long)(cal_end.tv_sec - cal_begin.tv_sec) * (long long)1000000000 +
   128 			Tsync = ((long long)(cal_end.tv_sec - cal_begin.tv_sec) * (long long)1000000000 +
   128 					(cal_end.tv_nsec - cal_begin.tv_nsec)) / calibration_count;
   129 					(cal_end.tv_nsec - cal_begin.tv_nsec)) / calibration_count;
   129 			if( (Nticks = (Tsync / Ttick)) > 0){
   130 			if( (Nticks = (Tsync / Ttick)) > 0){
   130 				FreqCorr = (Tsync mod Ttick); /* to be divided by Nticks */
   131 				FreqCorr = (Tsync mod Ttick); /* to be divided by Nticks */
   131 			}else{
   132 			}else{
   153 				if(Nticks < 2){
   154 				if(Nticks < 2){
   154 					/* When Sync source period is near Tick time */
   155 					/* When Sync source period is near Tick time */
   155 					/* PhaseCorr may not be applied to Periodic time given to timer */
   156 					/* PhaseCorr may not be applied to Periodic time given to timer */
   156 					PeriodicTcorr = Ttick + FreqCorr / Nticks;
   157 					PeriodicTcorr = Ttick + FreqCorr / Nticks;
   157 				}else{
   158 				}else{
   158 					PeriodicTcorr = Tcorr; 
   159 					PeriodicTcorr = Tcorr;
   159 				}
   160 				}
   160 			}else if(__tick > last_tick){
   161 			}else if(__tick > last_tick){
   161 				last_tick = __tick;
   162 				last_tick = __tick;
   162 				PhaseCorr = elapsed - (Tsync*sync_align_ratio/100);
   163 				PhaseCorr = elapsed - (Tsync*sync_align_ratio/100);
   163 				PeriodicTcorr = Tcorr = Ttick + PhaseCorr + FreqCorr;
   164 				PeriodicTcorr = Tcorr = Ttick + PhaseCorr + FreqCorr;