targets/plc_python.c
changeset 366 cd90e4c10261
parent 365 a7f58414dea0
child 367 a76ee5307bb7
equal deleted inserted replaced
365:a7f58414dea0 366:cd90e4c10261
     1 /*
       
     2  * Python Asynchronous execution code
       
     3  *
       
     4  * PLC put python commands in a fifo, respecting execution order
       
     5  * with the help of C pragmas inserted in python_eval FB code
       
     6  *
       
     7  * Buffer content is read asynchronously, (from non real time part),
       
     8  * commands are executed and result stored for later use by PLC.
       
     9  *
       
    10  * In this implementation, fifo is a list of pointer to python_eval
       
    11  * function blocks structures. Some local variables have been added in
       
    12  * python_eval interface. We use those local variables as buffer and state
       
    13  * flags.
       
    14  *
       
    15  * */
       
    16 
       
    17 #include "iec_types_all.h"
       
    18 #include "POUS.h"
       
    19 #include <string.h>
       
    20 
       
    21 /* The fifo (fixed size, as number of FB is fixed) */
       
    22 static PYTHON_EVAL* EvalFBs[%(python_eval_fb_count)d];
       
    23 /* Producer and consumer cursors */
       
    24 static int Current_PLC_EvalFB;
       
    25 static int Current_Python_EvalFB;
       
    26 
       
    27 /* A global IEC-Python gateway state, for use inside python_eval FBs*/
       
    28 static int PythonState;
       
    29 #define PYTHON_LOCKED_BY_PYTHON 0
       
    30 #define PYTHON_LOCKED_BY_PLC 1
       
    31 #define PYTHON_MUSTWAKEUP 2
       
    32 #define PYTHON_FINISHED 4
       
    33 
       
    34 /* Each python_eval FunctionBlock have it own state */
       
    35 #define PYTHON_FB_FREE 0
       
    36 #define PYTHON_FB_REQUESTED 1
       
    37 #define PYTHON_FB_PROCESSING 2
       
    38 #define PYTHON_FB_ANSWERED 3
       
    39 
       
    40 int WaitPythonCommands(void);
       
    41 void UnBlockPythonCommands(void);
       
    42 int TryLockPython(void);
       
    43 void UnLockPython(void);
       
    44 void LockPython(void);
       
    45 
       
    46 void __init_python()
       
    47 {
       
    48 	int i;
       
    49 	/* Initialize cursors */
       
    50 	Current_Python_EvalFB = 0;
       
    51 	Current_PLC_EvalFB = 0;
       
    52 	PythonState = PYTHON_LOCKED_BY_PYTHON;
       
    53 	for(i = 0; i < %(python_eval_fb_count)d; i++)
       
    54 		EvalFBs[i] = NULL;
       
    55 }
       
    56 
       
    57 void __cleanup_python()
       
    58 {
       
    59 	PythonState = PYTHON_FINISHED;
       
    60 	UnBlockPythonCommands();
       
    61 }
       
    62 
       
    63 void __retrieve_python()
       
    64 {
       
    65 	/* Check Python thread is not being
       
    66 	 * modifying internal python_eval data */
       
    67 	PythonState = TryLockPython() ?
       
    68 	                PYTHON_LOCKED_BY_PLC :
       
    69 	                PYTHON_LOCKED_BY_PYTHON;
       
    70 	/* If python thread _is_ in, then PythonState remains PYTHON_LOCKED_BY_PYTHON
       
    71 	 * and python_eval will no do anything */
       
    72 }
       
    73 
       
    74 void __publish_python()
       
    75 {
       
    76 	if(PythonState & PYTHON_LOCKED_BY_PLC){
       
    77 		/* If runnig PLC did push something in the fifo*/
       
    78 		if(PythonState & PYTHON_MUSTWAKEUP){
       
    79 			/* WakeUp python thread */
       
    80 			UnBlockPythonCommands();
       
    81 		}
       
    82 		UnLockPython();
       
    83 	}
       
    84 }
       
    85 /**
       
    86  * Called by the PLC, each time a python_eval
       
    87  * FB instance is executed
       
    88  */
       
    89 void __PythonEvalFB(int poll, PYTHON_EVAL* data__)
       
    90 {
       
    91 	/* detect rising edge on TRIG to trigger evaluation */
       
    92 	if(((data__->TRIG && !data__->TRIGM1) ||
       
    93 	   /* polling is equivalent to trig on value rather than on rising edge*/
       
    94 	    (poll && data__->TRIG )) &&
       
    95 	    /* trig only if not already trigged */
       
    96 	   data__->TRIGGED == 0){
       
    97 		/* mark as trigged */
       
    98 		data__->TRIGGED = 1;
       
    99 		/* make a safe copy of the code */
       
   100 		data__->PREBUFFER = data__->CODE;
       
   101 	}
       
   102 	/* retain value for next rising edge detection */
       
   103 	data__->TRIGM1 = data__->TRIG;
       
   104 
       
   105 	/* python thread is not in ? */
       
   106 	if( PythonState & PYTHON_LOCKED_BY_PLC){
       
   107 		/* if some answer are waiting, publish*/
       
   108 		if(data__->STATE == PYTHON_FB_ANSWERED){
       
   109 			/* Copy buffer content into result*/
       
   110 			data__->RESULT = data__->BUFFER;
       
   111 			/* signal result presece to PLC*/
       
   112 			data__->ACK = 1;
       
   113 			/* Mark as free */
       
   114 			data__->STATE = PYTHON_FB_FREE;
       
   115 			/* mark as not trigged */
       
   116 			if(!poll)
       
   117 				data__->TRIGGED = 0;
       
   118 			/*printf("__PythonEvalFB pop %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body);*/
       
   119 		}else if(poll){
       
   120 			/* when in polling, no answer == ack down */
       
   121 			data__->ACK = 0;
       
   122 		}
       
   123 		/* got the order to act ?*/
       
   124 		if(data__->TRIGGED == 1 &&
       
   125 		   /* and not already being processed */
       
   126 		   data__->STATE == PYTHON_FB_FREE)
       
   127 		{
       
   128 			/* Enter the block in the fifo
       
   129 			/* Don't have to check if fifo cell is free
       
   130 			 * as fifo size == FB count, and a FB cannot
       
   131 			 * be requested twice */
       
   132 			EvalFBs[Current_PLC_EvalFB] = data__;
       
   133 			/* copy into BUFFER local*/
       
   134 			data__->BUFFER = data__->PREBUFFER;
       
   135 			/* Set ACK pin to low so that we can set a rising edge on result */
       
   136 			if(!poll){
       
   137 				/* when not polling, a new answer imply reseting ack*/
       
   138 				data__->ACK = 0;
       
   139 			}else{
       
   140 				/* when in polling, acting reset trigger */
       
   141 				data__->TRIGGED = 0;
       
   142 			}
       
   143 			/* Mark FB busy */
       
   144 			data__->STATE = PYTHON_FB_REQUESTED;
       
   145 			/* Have to wakeup python thread in case he was asleep */
       
   146 			PythonState |= PYTHON_MUSTWAKEUP;
       
   147 			/*printf("__PythonEvalFB push %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body);*/
       
   148 			/* Get a new line */
       
   149 			Current_PLC_EvalFB = (Current_PLC_EvalFB + 1) %% %(python_eval_fb_count)d;
       
   150 		}
       
   151 	}
       
   152 }
       
   153 
       
   154 char* PythonIterator(char* result)
       
   155 {
       
   156 	char* next_command;
       
   157 	PYTHON_EVAL* data__;
       
   158 	//printf("PythonIterator result %%s\n", result);
       
   159 	/* take python mutex to prevent changing PLC data while PLC running */
       
   160 	LockPython();
       
   161 	/* Get current FB */
       
   162 	data__ = EvalFBs[Current_Python_EvalFB];
       
   163 	if(data__ && /* may be null at first run */
       
   164 	   data__->STATE == PYTHON_FB_PROCESSING){ /* some answer awaited*/
       
   165 	   	/* If result not None */
       
   166 	   	if(result){
       
   167 			/* Get results len */
       
   168 			data__->BUFFER.len = strlen(result);
       
   169 			/* prevent results overrun */
       
   170 			if(data__->BUFFER.len > STR_MAX_LEN)
       
   171 			{
       
   172 				data__->BUFFER.len = STR_MAX_LEN;
       
   173 				/* TODO : signal error */
       
   174 			}
       
   175 			/* Copy results to buffer */
       
   176 			strncpy(data__->BUFFER.body, result, data__->BUFFER.len);
       
   177 	   	}else{
       
   178 	   		data__->BUFFER.len = 0;
       
   179 	   	}
       
   180 		/* remove block from fifo*/
       
   181 		EvalFBs[Current_Python_EvalFB] = NULL;
       
   182 		/* Mark block as answered */
       
   183 		data__->STATE = PYTHON_FB_ANSWERED;
       
   184 		/* Get a new line */
       
   185 		Current_Python_EvalFB = (Current_Python_EvalFB + 1) %% %(python_eval_fb_count)d;
       
   186 		//printf("PythonIterator ++ Current_Python_EvalFB %%d\n", Current_Python_EvalFB);
       
   187 	}
       
   188 	/* while next slot is empty */
       
   189 	while(((data__ = EvalFBs[Current_Python_EvalFB]) == NULL) ||
       
   190 	 	  /* or doesn't contain command */
       
   191 	      data__->STATE != PYTHON_FB_REQUESTED)
       
   192 	{
       
   193 		UnLockPython();
       
   194 		/* wait next FB to eval */
       
   195 		//printf("PythonIterator wait\n");
       
   196 		if(WaitPythonCommands()) return NULL;
       
   197 		/*emergency exit*/
       
   198 		if(PythonState & PYTHON_FINISHED) return NULL;
       
   199 		LockPython();
       
   200 	}
       
   201 	/* Mark block as processing */
       
   202 	data__->STATE = PYTHON_FB_PROCESSING;
       
   203 	//printf("PythonIterator\n");
       
   204 	/* make BUFFER a null terminated string */
       
   205 	data__->BUFFER.body[data__->BUFFER.len] = 0;
       
   206 	/* next command is BUFFER */
       
   207 	next_command = data__->BUFFER.body;
       
   208 	/* free python mutex */
       
   209 	UnLockPython();
       
   210 	/* return the next command to eval */
       
   211 	return next_command;
       
   212 }
       
   213