File: rt_zcfcn.cpp1 /* 2 * rt_zcfcn.cpp 3 * 4 * Code generation for model "Controller". 5 * 6 * Model version : 1.1303 7 * Simulink Coder version : 8.3 (R2012b) 20-Jul-2012 8 * C++ source code generated on : Sun Jun 01 14:58:10 2014 9 * 10 * Target selection: grt.tlc 11 * Note: GRT includes extra infrastructure and instrumentation for prototyping 12 * Embedded hardware selection: Generic->32-bit x86 compatible 13 * Code generation objectives: Unspecified 14 * Validation result: Not run 15 */ 16 17 #include "rt_zcfcn.h" 18 19 extern "C" { 20 /* Detect zero crossings events. */ 21 ZCEventType rt_ZCFcn(ZCDirection zcDir, ZCSigState* prevZc, real_T currValue) 22 { 23 slZcEventType zcsDir; 24 slZcEventType tempEv; 25 ZCEventType zcEvent = NO_ZCEVENT; /* assume */ 26 27 /* zcEvent matrix */ 28 static const slZcEventType eventMatrix[4][4] = { 29 /* ZER POS NEG UNK */ 30 { SL_ZCS_EVENT_NUL, SL_ZCS_EVENT_Z2P, SL_ZCS_EVENT_Z2N, SL_ZCS_EVENT_NUL },/* ZER */ 31 32 { SL_ZCS_EVENT_P2Z, SL_ZCS_EVENT_NUL, SL_ZCS_EVENT_P2N, SL_ZCS_EVENT_NUL },/* POS */ 33 34 { SL_ZCS_EVENT_N2Z, SL_ZCS_EVENT_N2P, SL_ZCS_EVENT_NUL, SL_ZCS_EVENT_NUL },/* NEG */ 35 36 { SL_ZCS_EVENT_NUL, SL_ZCS_EVENT_NUL, SL_ZCS_EVENT_NUL, SL_ZCS_EVENT_NUL }/* UNK */ 37 }; 38 39 /* get prevZcEvent and prevZcSign from prevZc */ 40 slZcEventType prevEv = (slZcEventType)(((uint8_T)(*prevZc)) >> 2); 41 slZcSignalSignType prevSign = (slZcSignalSignType)(((uint8_T)(*prevZc)) & 42 0x03); 43 44 /* get current zcSignal sign from current zcSignal value */ 45 slZcSignalSignType currSign = (slZcSignalSignType)((currValue) > 0.0 ? 46 SL_ZCS_SIGN_POS : 47 ((currValue) < 0.0 ? SL_ZCS_SIGN_NEG : SL_ZCS_SIGN_ZERO)); 48 49 /* get current zcEvent based on prev and current zcSignal value */ 50 slZcEventType currEv = eventMatrix[prevSign][currSign]; 51 52 /* get slZcEventType from ZCDirection */ 53 switch (zcDir) { 54 case ANY_ZERO_CROSSING: 55 zcsDir = SL_ZCS_EVENT_ALL; 56 break; 57 58 case FALLING_ZERO_CROSSING: 59 zcsDir = SL_ZCS_EVENT_ALL_DN; 60 break; 61 62 case RISING_ZERO_CROSSING: 63 zcsDir = SL_ZCS_EVENT_ALL_UP; 64 break; 65 66 default: 67 zcsDir = SL_ZCS_EVENT_NUL; 68 break; 69 } 70 71 /*had event, check if double zc happend remove double detection. */ 72 if (slZcHadEvent(currEv, zcsDir)) { 73 currEv = (slZcEventType)(slZcUnAliasEvents(prevEv, currEv)); 74 } else { 75 currEv = SL_ZCS_EVENT_NUL; 76 } 77 78 /* Update prevZc */ 79 tempEv = currEv << 2; /* shift left by 2 bits */ 80 *prevZc = (ZCSigState)((currSign) | (tempEv)); 81 if (currEv & SL_ZCS_EVENT_ALL_DN) { 82 zcEvent = FALLING_ZCEVENT; 83 } else if (currEv & SL_ZCS_EVENT_ALL_UP) { 84 zcEvent = RISING_ZCEVENT; 85 } else { 86 zcEvent = NO_ZCEVENT; 87 } 88 89 return zcEvent; 90 } /* rt_ZCFcn */ 91 } 92 |