etherlab/plc_ds402node.c
changeset 2032 766078d83e22
child 2034 ae8fecf082a1
equal deleted inserted replaced
2031:c6f32810723e 2032:766078d83e22
       
     1 /*
       
     2  * Ethercat DS402 node execution code
       
     3  *
       
     4  * */
       
     5 
       
     6 #include "ecrt.h"
       
     7 
       
     8 #ifdef _WINDOWS_H
       
     9   #include "iec_types.h"
       
    10 #else
       
    11   #include "iec_std_lib.h"
       
    12 #endif
       
    13 
       
    14 %(MCL_includes)s
       
    15 
       
    16 IEC_UINT __InactiveMask = 0x4f;
       
    17 IEC_UINT __ActiveMask = 0x6f;
       
    18 
       
    19 typedef enum {
       
    20 	__Unknown,
       
    21 	__NotReadyToSwitchOn,
       
    22 	__SwitchOnDisabled,
       
    23 	__ReadyToSwitchOn,
       
    24 	__SwitchedOn,
       
    25 	__OperationEnabled,
       
    26 	__QuickStopActive,
       
    27 	__FaultReactionActive,
       
    28 	__Fault,
       
    29 } __DS402NodeState;
       
    30 
       
    31 typedef struct {
       
    32 %(entry_variables)s
       
    33 	__DS402NodeState state;
       
    34 } __DS402Node;
       
    35 
       
    36 static __DS402Node __DS402Node_%(location)s;
       
    37 
       
    38 %(located_variables_declaration)s
       
    39 
       
    40 extern uint8_t *domain1_pd;
       
    41 %(extern_pdo_entry_configuration)s
       
    42 
       
    43 int __init_%(location)s()
       
    44 {
       
    45 	return 0;
       
    46 }
       
    47 
       
    48 void __cleanup_%(location)s()
       
    49 {
       
    50 }
       
    51 
       
    52 void __retrieve_%(location)s()
       
    53 {
       
    54 	IEC_UINT statusword_inactive = __DS402Node_%(location)s.StatusWord & __InactiveMask;
       
    55 	IEC_UINT statusword_active = __DS402Node_%(location)s.StatusWord & __ActiveMask;
       
    56 
       
    57     // DS402 input entries extraction
       
    58 %(retrieve_variables)s
       
    59 
       
    60 	// DS402 node state computation
       
    61 	__DS402Node_%(location)s.state = __Unknown;
       
    62 	switch (statusword_inactive) {
       
    63 		case 0x00:
       
    64 			__DS402Node_%(location)s.state = __NotReadyToSwitchOn;
       
    65 			break;
       
    66 		case 0x40:
       
    67 			__DS402Node_%(location)s.state = __SwitchOnDisabled;
       
    68 			break;
       
    69 		case 0x0f:
       
    70 			__DS402Node_%(location)s.state = __FaultReactionActive;
       
    71 			break;
       
    72 		case 0x08:
       
    73 			__DS402Node_%(location)s.state = __Fault;
       
    74 			break;
       
    75 		default:
       
    76 			break;
       
    77 	}
       
    78 	switch (statusword_active) {
       
    79 		case 0x21:
       
    80 			__DS402Node_%(location)s.state = __ReadyToSwitchOn;
       
    81 			break;
       
    82 		case 0x23:
       
    83 			__DS402Node_%(location)s.state = __SwitchedOn;
       
    84 			break;
       
    85 		case 0x27:
       
    86 			__DS402Node_%(location)s.state = __OperationEnabled;
       
    87 			break;
       
    88 		case 0x07:
       
    89 			__DS402Node_%(location)s.state = __QuickStopActive;
       
    90 			break;
       
    91 		default:
       
    92 			break;
       
    93 	}
       
    94 	if (__DS402Node_%(location)s.state == __Unknown) {
       
    95 		return;
       
    96 	}
       
    97 
       
    98 }
       
    99 
       
   100 void __publish_%(location)s()
       
   101 {
       
   102 	// DS402 node state transition computation
       
   103 	switch (__DS402Node_%(location)s.state) {
       
   104 	    case __SwitchOnDisabled:
       
   105 	    	__DS402Node_%(location)s.ControlWord = (__DS402Node_%(location)s.ControlWord & ~0x87) | 0x06;
       
   106 	    	break;
       
   107 	    case __ReadyToSwitchOn:
       
   108 	    	__DS402Node_%(location)s.ControlWord = (__DS402Node_%(location)s.ControlWord & ~0x8f) | 0x07;
       
   109 	    	break;
       
   110 	    case __SwitchedOn:
       
   111 	    	// if (POWER) {
       
   112 	    	//      __DS402Node_%(location)s.ControlWord = (__DS402Node_%(location)s.ControlWord & ~0x8f) | 0x0f;
       
   113 	    	// }
       
   114 	    	break;
       
   115 	    case __OperationEnabled:
       
   116 	    	// if (!POWER) {
       
   117 	        //      __DS402Node_%(location)s.ControlWord = (__DS402Node_%(location)s.ControlWord & ~0x8f) | 0x07;
       
   118 	    	// }
       
   119 	    	break;
       
   120 	    case __Fault:
       
   121 	    	__DS402Node_%(location)s.ControlWord = (__DS402Node_%(location)s.ControlWord & ~0x8f) | 0x80;
       
   122 	    	break;
       
   123 	    default:
       
   124 	    	break;
       
   125 	}
       
   126 
       
   127 	// DS402 output entries setting
       
   128 %(publish_variables)s
       
   129 }