runtime/plc_common_main.c
author etisserant
Fri, 27 Jun 2008 16:21:22 +0200
changeset 178 2390b409eb93
parent 137 187a4e2412e5
child 203 cb9901076a21
permissions -rw-r--r--
Added PLC tick alignement on external synchronization source feature.
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
     1
/*
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
     2
 * Prototypes for function provided by arch-specific code (main)
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
     3
 * concatained after this template
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
     4
 ** /
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
     5
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
     6
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     7
/*
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     8
 * Functions and variables provied by generated C softPLC
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     9
 **/ 
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    10
extern int common_ticktime__;
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    11
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    12
/*
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    13
 * Functions and variables provied by plc.c
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    14
 **/ 
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    15
void run(long int tv_sec, long int tv_nsec);
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    16
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    17
#define maxval(a,b) ((a>b)?a:b)
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    18
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    19
#include "iec_types.h"
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    20
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    21
/*
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    22
 * Functions and variables provied by generated C softPLC
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    23
 **/ 
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    24
void config_run__(int tick);
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    25
void config_init__(void);
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    26
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    27
/*
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    28
 *  Functions and variables to export to generated C softPLC
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    29
 **/
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    30
 
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    31
IEC_TIME __CURRENT_TIME;
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    32
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    33
static int tick = 0;
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 54
diff changeset
    34
static int init_level=0;
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    35
54
ff82e7088ec5 Comments...
etisserant
parents: 49
diff changeset
    36
/*
137
187a4e2412e5 Modifying svgui plugin for following new SVGUIEditor version
lbessard
parents: 57
diff changeset
    37
 * Prototypes of functions exported by plugins 
54
ff82e7088ec5 Comments...
etisserant
parents: 49
diff changeset
    38
 **/
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    39
%(calls_prototypes)s
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    40
54
ff82e7088ec5 Comments...
etisserant
parents: 49
diff changeset
    41
/*
137
187a4e2412e5 Modifying svgui plugin for following new SVGUIEditor version
lbessard
parents: 57
diff changeset
    42
 * Retrieve input variables, run PLC and publish output variables 
54
ff82e7088ec5 Comments...
etisserant
parents: 49
diff changeset
    43
 **/
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    44
void __run()
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    45
{
137
187a4e2412e5 Modifying svgui plugin for following new SVGUIEditor version
lbessard
parents: 57
diff changeset
    46
    %(retrieve_calls)s
54
ff82e7088ec5 Comments...
etisserant
parents: 49
diff changeset
    47
    
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    48
	/*
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    49
	printf("run tick = %%d\n", tick + 1);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    50
	*/
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    51
    config_run__(tick++);
54
ff82e7088ec5 Comments...
etisserant
parents: 49
diff changeset
    52
    
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    53
    %(publish_calls)s
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    54
}
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    55
54
ff82e7088ec5 Comments...
etisserant
parents: 49
diff changeset
    56
/*
ff82e7088ec5 Comments...
etisserant
parents: 49
diff changeset
    57
 * Initialize variables according to PLC's defalut values,
ff82e7088ec5 Comments...
etisserant
parents: 49
diff changeset
    58
 * and then init plugins with that values  
ff82e7088ec5 Comments...
etisserant
parents: 49
diff changeset
    59
 **/
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 54
diff changeset
    60
int __init(int argc,char **argv)
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    61
{
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 54
diff changeset
    62
    int res;
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    63
    config_init__();
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    64
    %(init_calls)s
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 54
diff changeset
    65
    return 0;
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    66
}
54
ff82e7088ec5 Comments...
etisserant
parents: 49
diff changeset
    67
/*
ff82e7088ec5 Comments...
etisserant
parents: 49
diff changeset
    68
 * Calls plugin cleanup proc.
ff82e7088ec5 Comments...
etisserant
parents: 49
diff changeset
    69
 **/
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    70
void __cleanup()
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    71
{
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    72
    %(cleanup_calls)s
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    73
}
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    74
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    75
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    76
void PLC_GetTime(IEC_TIME *CURRENT_TIME);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    77
void PLC_SetTimer(long long next, long long period);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    78
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    79
#define CALIBRATED -2
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    80
#define NOT_CALIBRATED -1
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    81
static int calibration_count = NOT_CALIBRATED;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    82
static IEC_TIME cal_begin;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    83
static long long Tsync = 0;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    84
static long long FreqCorr = 0;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    85
static int Nticks = 0;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    86
static int  last_tick = 0;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    87
static long long Ttick = 0;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    88
#define mod %%
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    89
/*
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    90
 * Call this on each external sync, 
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    91
 **/
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    92
void align_tick(int calibrate)
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    93
{
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    94
	/*
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    95
	printf("align_tick(%%d)\n", calibrate);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    96
	*/
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    97
	if(calibrate){
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    98
		if(calibration_count == CALIBRATED)
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
    99
			/* Re-calibration*/
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   100
			calibration_count = NOT_CALIBRATED;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   101
		if(calibration_count == NOT_CALIBRATED)
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   102
			/* Calibration start, get time*/
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   103
			PLC_GetTime(&cal_begin);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   104
		calibration_count++;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   105
	}else{
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   106
		if(calibration_count >= 0){
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   107
			/* End of calibration */
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   108
			/* Get final time */
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   109
			IEC_TIME cal_end;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   110
			PLC_GetTime(&cal_end);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   111
			/*adjust calibration_count*/
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   112
			calibration_count++;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   113
			/* compute mean of Tsync, over calibration period */	
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   114
			Tsync = ((long long)(cal_end.tv_sec - cal_begin.tv_sec) * (long long)1000000000 +
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   115
					(cal_end.tv_nsec - cal_begin.tv_nsec)) / calibration_count;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   116
			if( (Nticks = (Tsync / Ttick)) > 0){
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   117
				FreqCorr = (Tsync mod Ttick); /* to be divided by Nticks */
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   118
			}else{
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   119
				FreqCorr = Tsync - (Ttick mod Tsync);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   120
			}
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   121
			/*
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   122
			printf("Tsync = %%ld\n", Tsync);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   123
			printf("calibration_count = %%d\n", calibration_count);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   124
			printf("Nticks = %%d\n", Nticks);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   125
			*/
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   126
			calibration_count = CALIBRATED;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   127
		}
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   128
		if(calibration_count == CALIBRATED){
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   129
			/* Get Elapsed time since last PLC tick (__CURRENT_TIME) */
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   130
			IEC_TIME now;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   131
			long long elapsed;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   132
			long long Tcorr;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   133
			long long PhaseCorr;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   134
			long long PeriodicTcorr;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   135
			PLC_GetTime(&now);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   136
			elapsed = (now.tv_sec - __CURRENT_TIME.tv_sec) * 1000000000 + now.tv_nsec - __CURRENT_TIME.tv_nsec;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   137
			if(Nticks > 0){
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   138
				PhaseCorr = elapsed - (Ttick + FreqCorr/Nticks)*%(sync_align_ratio)d/100; /* to be divided by Nticks */
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   139
				Tcorr = Ttick + (PhaseCorr + FreqCorr) / Nticks;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   140
				if(Nticks < 2){
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   141
					/* When Sync source period is near Tick time */
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   142
					/* PhaseCorr may not be applied to Periodic time given to timer */
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   143
					PeriodicTcorr = Ttick + FreqCorr / Nticks;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   144
				}else{
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   145
					PeriodicTcorr = Tcorr; 
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   146
				}
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   147
			}else if(tick > last_tick){
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   148
				last_tick = tick;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   149
				PhaseCorr = elapsed - (Tsync*%(sync_align_ratio)d/100);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   150
				PeriodicTcorr = Tcorr = Ttick + PhaseCorr + FreqCorr;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   151
			}else{
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   152
				/*PLC did not run meanwhile. Nothing to do*/
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   153
				return;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   154
			}
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   155
			/* DO ALIGNEMENT */
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   156
			PLC_SetTimer(Tcorr - elapsed, PeriodicTcorr);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   157
		}
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   158
	}
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 137
diff changeset
   159
}