/* * File: BLDC_controller.c * * Code generated for Simulink model 'BLDC_controller'. * * Model version : 1.1187 * Simulink Coder version : 8.13 (R2017b) 24-Jul-2017 * C/C++ source code generated on : Sun Oct 27 17:31:20 2019 * * Target selection: ert.tlc * Embedded hardware selection: ARM Compatible->ARM Cortex * Emulation hardware selection: * Differs from embedded hardware (MATLAB Host) * Code generation objectives: * 1. Execution efficiency * 2. RAM efficiency * Validation result: Not run */ #include "BLDC_controller.h" /* Named constants for Chart: '/F02_02_Control_Mode_Manager' */ #define IN_ACTIVE ((uint8_T)1U) #define IN_NO_ACTIVE_CHILD ((uint8_T)0U) #define IN_OPEN ((uint8_T)2U) #define IN_SPEED_MODE ((uint8_T)1U) #define IN_TORQUE_MODE ((uint8_T)2U) #define IN_VOLTAGE_MODE ((uint8_T)3U) #define OPEN_MODE ((uint8_T)0U) #define SPD_MODE ((uint8_T)2U) #define TRQ_MODE ((uint8_T)3U) #define VLT_MODE ((uint8_T)1U) #ifndef UCHAR_MAX #include #endif #if ( UCHAR_MAX != (0xFFU) ) || ( SCHAR_MAX != (0x7F) ) #error Code was generated for compiler with different sized uchar/char. \ Consider adjusting Test hardware word size settings on the \ Hardware Implementation pane to match your compiler word sizes as \ defined in limits.h of the compiler. Alternatively, you can \ select the Test hardware is the same as production hardware option and \ select the Enable portable word sizes option on the Code Generation > \ Verification pane for ERT based targets, which will disable the \ preprocessor word size checks. #endif #if ( USHRT_MAX != (0xFFFFU) ) || ( SHRT_MAX != (0x7FFF) ) #error Code was generated for compiler with different sized ushort/short. \ Consider adjusting Test hardware word size settings on the \ Hardware Implementation pane to match your compiler word sizes as \ defined in limits.h of the compiler. Alternatively, you can \ select the Test hardware is the same as production hardware option and \ select the Enable portable word sizes option on the Code Generation > \ Verification pane for ERT based targets, which will disable the \ preprocessor word size checks. #endif #if ( UINT_MAX != (0xFFFFFFFFU) ) || ( INT_MAX != (0x7FFFFFFF) ) #error Code was generated for compiler with different sized uint/int. \ Consider adjusting Test hardware word size settings on the \ Hardware Implementation pane to match your compiler word sizes as \ defined in limits.h of the compiler. Alternatively, you can \ select the Test hardware is the same as production hardware option and \ select the Enable portable word sizes option on the Code Generation > \ Verification pane for ERT based targets, which will disable the \ preprocessor word size checks. #endif #if ( ULONG_MAX != (0xFFFFFFFFU) ) || ( LONG_MAX != (0x7FFFFFFF) ) #error Code was generated for compiler with different sized ulong/long. \ Consider adjusting Test hardware word size settings on the \ Hardware Implementation pane to match your compiler word sizes as \ defined in limits.h of the compiler. Alternatively, you can \ select the Test hardware is the same as production hardware option and \ select the Enable portable word sizes option on the Code Generation > \ Verification pane for ERT based targets, which will disable the \ preprocessor word size checks. #endif #if 0 /* Skip this size verification because of preprocessor limitation */ #if ( ULLONG_MAX != (0xFFFFFFFFFFFFFFFFULL) ) || ( LLONG_MAX != (0x7FFFFFFFFFFFFFFFLL) ) #error Code was generated for compiler with different sized ulong_long/long_long. \ Consider adjusting Test hardware word size settings on the \ Hardware Implementation pane to match your compiler word sizes as \ defined in limits.h of the compiler. Alternatively, you can \ select the Test hardware is the same as production hardware option and \ select the Enable portable word sizes option on the Code Generation > \ Verification pane for ERT based targets, which will disable the \ preprocessor word size checks. #endif #endif uint8_T plook_u8u16_evencka(uint16_T u, uint16_T bp0, uint16_T bpSpace, uint32_T maxIndex); uint8_T plook_u8s16_evencka(int16_T u, int16_T bp0, uint16_T bpSpace, uint32_T maxIndex); uint8_T plook_u8s16u8n6_evenc_s(int16_T u, int16_T bp0, uint16_T bpSpace, uint32_T maxIndex, uint8_T *fraction); int16_T intrp1d_s16s32s32u8u8n6l_s(uint8_T bpIndex, uint8_T frac, const int16_T table[]); extern void Counter_Init(DW_Counter *localDW, int16_T rtp_z_cntInit); extern int16_T Counter(int16_T rtu_inc, int16_T rtu_max, boolean_T rtu_rst, DW_Counter *localDW); extern void Low_Pass_Filter_Reset(DW_Low_Pass_Filter *localDW); extern void Low_Pass_Filter(const int16_T rtu_u[2], uint16_T rtu_coef, int16_T rty_y[2], DW_Low_Pass_Filter *localDW); extern void PI_backCalc_fixdt_Reset(DW_PI_backCalc_fixdt *localDW); extern void PI_backCalc_fixdt(int16_T rtu_err, uint16_T rtu_P, uint16_T rtu_I, uint16_T rtu_Kb, int32_T rtu_ext_limProt, int16_T rtu_satMax, int16_T rtu_satMin, int16_T *rty_out, DW_PI_backCalc_fixdt *localDW); extern void PI_backCalc_fixdt_n_Reset(DW_PI_backCalc_fixdt_f *localDW); extern int16_T PI_backCalc_fixdt_n(int16_T rtu_err, uint16_T rtu_P, uint16_T rtu_I, uint16_T rtu_Kb, int16_T rtu_ext_limProt, int16_T rtu_satMax, int16_T rtu_satMin, DW_PI_backCalc_fixdt_f *localDW); extern void Rate_Limiter_Reset(DW_Rate_Limiter *localDW); extern int32_T Rate_Limiter(int32_T rtu_u, int32_T rtu_initVal, boolean_T rtu_init, int32_T rtu_inc, int32_T rtu_dec, DW_Rate_Limiter *localDW); extern void Counter_b_Init(DW_Counter_l *localDW, uint16_T rtp_z_cntInit); extern uint16_T Counter_i(uint16_T rtu_inc, uint16_T rtu_max, boolean_T rtu_rst, DW_Counter_l *localDW); extern void either_edge_Reset(DW_either_edge *localDW); extern boolean_T either_edge(boolean_T rtu_u, DW_either_edge *localDW); extern void Debounce_Filter_Init(DW_Debounce_Filter *localDW); extern void Debounce_Filter_Reset(DW_Debounce_Filter *localDW); extern void Debounce_Filter(boolean_T rtu_u, uint16_T rtu_tAcv, uint16_T rtu_tDeacv, boolean_T *rty_y, DW_Debounce_Filter *localDW); uint8_T plook_u8u16_evencka(uint16_T u, uint16_T bp0, uint16_T bpSpace, uint32_T maxIndex) { uint8_T bpIndex; uint16_T fbpIndex; /* Prelookup - Index only Index Search method: 'even' Extrapolation method: 'Clip' Use previous index: 'off' Use last breakpoint for index at or above upper limit: 'on' Remove protection against out-of-range input in generated code: 'off' */ if (u <= bp0) { bpIndex = 0U; } else { fbpIndex = (uint16_T)((uint32_T)(uint16_T)((uint32_T)u - bp0) / bpSpace); if (fbpIndex < maxIndex) { bpIndex = (uint8_T)fbpIndex; } else { bpIndex = (uint8_T)maxIndex; } } return bpIndex; } uint8_T plook_u8s16_evencka(int16_T u, int16_T bp0, uint16_T bpSpace, uint32_T maxIndex) { uint8_T bpIndex; uint16_T fbpIndex; /* Prelookup - Index only Index Search method: 'even' Extrapolation method: 'Clip' Use previous index: 'off' Use last breakpoint for index at or above upper limit: 'on' Remove protection against out-of-range input in generated code: 'off' */ if (u <= bp0) { bpIndex = 0U; } else { fbpIndex = (uint16_T)((uint32_T)(uint16_T)(u - bp0) / bpSpace); if (fbpIndex < maxIndex) { bpIndex = (uint8_T)fbpIndex; } else { bpIndex = (uint8_T)maxIndex; } } return bpIndex; } uint8_T plook_u8s16u8n6_evenc_s(int16_T u, int16_T bp0, uint16_T bpSpace, uint32_T maxIndex, uint8_T *fraction) { uint8_T bpIndex; uint16_T uAdjust; uint16_T fbpIndex; /* Prelookup - Index and Fraction Index Search method: 'even' Extrapolation method: 'Clip' Use previous index: 'off' Use last breakpoint for index at or above upper limit: 'off' Remove protection against out-of-range input in generated code: 'off' Rounding mode: 'simplest' */ if (u <= bp0) { bpIndex = 0U; *fraction = 0U; } else { uAdjust = (uint16_T)(u - bp0); fbpIndex = (uint16_T)((uint32_T)uAdjust / bpSpace); if (fbpIndex < maxIndex) { bpIndex = (uint8_T)fbpIndex; *fraction = (uint8_T)(((uint32_T)(uint16_T)((uint32_T)uAdjust - (uint16_T) ((uint32_T)bpIndex * bpSpace)) << 6) / bpSpace); } else { bpIndex = (uint8_T)(maxIndex - 1U); *fraction = 64U; } } return bpIndex; } int16_T intrp1d_s16s32s32u8u8n6l_s(uint8_T bpIndex, uint8_T frac, const int16_T table[]) { uint32_T offset_0d; /* Interpolation 1-D Interpolation method: 'Linear' Use last breakpoint for index at or above upper limit: 'off' Rounding mode: 'simplest' Overflow mode: 'wrapping' */ offset_0d = bpIndex; return (int16_T)((int16_T)(((table[offset_0d + 1U] - table[offset_0d]) * frac) >> 6) + table[offset_0d]); } /* System initialize for atomic system: '/Counter' */ void Counter_Init(DW_Counter *localDW, int16_T rtp_z_cntInit) { /* InitializeConditions for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = rtp_z_cntInit; } /* Output and update for atomic system: '/Counter' */ int16_T Counter(int16_T rtu_inc, int16_T rtu_max, boolean_T rtu_rst, DW_Counter * localDW) { int16_T rtu_rst_0; int16_T rty_cnt_0; /* Switch: '/Switch1' incorporates: * Constant: '/Constant23' * UnitDelay: '/UnitDelay' */ if (rtu_rst) { rtu_rst_0 = 0; } else { rtu_rst_0 = localDW->UnitDelay_DSTATE; } /* End of Switch: '/Switch1' */ /* Sum: '/Sum1' */ rty_cnt_0 = (int16_T)(rtu_inc + rtu_rst_0); /* MinMax: '/MinMax' */ if (rty_cnt_0 < rtu_max) { /* Update for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = rty_cnt_0; } else { /* Update for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = rtu_max; } /* End of MinMax: '/MinMax' */ return rty_cnt_0; } /* System reset for atomic system: '/Low_Pass_Filter' */ void Low_Pass_Filter_Reset(DW_Low_Pass_Filter *localDW) { /* InitializeConditions for UnitDelay: '/UnitDelay3' */ localDW->UnitDelay3_DSTATE[0] = 0; localDW->UnitDelay3_DSTATE[1] = 0; } /* Output and update for atomic system: '/Low_Pass_Filter' */ void Low_Pass_Filter(const int16_T rtu_u[2], uint16_T rtu_coef, int16_T rty_y[2], DW_Low_Pass_Filter *localDW) { uint16_T rtb_Sum5; /* Sum: '/Sum5' */ rtb_Sum5 = (uint16_T)(65535U - rtu_coef); /* Sum: '/Sum1' incorporates: * Product: '/Divide1' * Product: '/Divide2' * UnitDelay: '/UnitDelay3' */ rty_y[0] = (int16_T)(((rtu_u[0] * rtu_coef) >> 16) + ((localDW->UnitDelay3_DSTATE[0] * rtb_Sum5) >> 16)); /* Update for UnitDelay: '/UnitDelay3' */ localDW->UnitDelay3_DSTATE[0] = rty_y[0]; /* Sum: '/Sum1' incorporates: * Product: '/Divide1' * Product: '/Divide2' * UnitDelay: '/UnitDelay3' */ rty_y[1] = (int16_T)(((rtu_u[1] * rtu_coef) >> 16) + ((localDW->UnitDelay3_DSTATE[1] * rtb_Sum5) >> 16)); /* Update for UnitDelay: '/UnitDelay3' */ localDW->UnitDelay3_DSTATE[1] = rty_y[1]; } /* * System reset for atomic system: * '/PI_backCalc_fixdt_Id' * '/PI_backCalc_fixdt_Iq' */ void PI_backCalc_fixdt_Reset(DW_PI_backCalc_fixdt *localDW) { /* InitializeConditions for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = 0; /* InitializeConditions for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE_l = 0; } /* * Output and update for atomic system: * '/PI_backCalc_fixdt_Id' * '/PI_backCalc_fixdt_Iq' */ void PI_backCalc_fixdt(int16_T rtu_err, uint16_T rtu_P, uint16_T rtu_I, uint16_T rtu_Kb, int32_T rtu_ext_limProt, int16_T rtu_satMax, int16_T rtu_satMin, int16_T *rty_out, DW_PI_backCalc_fixdt *localDW) { int32_T rtb_Sum1_i4; int32_T tmp; /* Sum: '/Sum2' incorporates: * Product: '/Divide1' * UnitDelay: '/UnitDelay' */ rtb_Sum1_i4 = rtu_err * rtu_I; if ((rtb_Sum1_i4 < 0) && (rtu_ext_limProt < MIN_int32_T - rtb_Sum1_i4)) { rtb_Sum1_i4 = MIN_int32_T; } else if ((rtb_Sum1_i4 > 0) && (rtu_ext_limProt > MAX_int32_T - rtb_Sum1_i4)) { rtb_Sum1_i4 = MAX_int32_T; } else { rtb_Sum1_i4 += rtu_ext_limProt; } if ((rtb_Sum1_i4 < 0) && (localDW->UnitDelay_DSTATE < MIN_int32_T - rtb_Sum1_i4)) { rtb_Sum1_i4 = MIN_int32_T; } else if ((rtb_Sum1_i4 > 0) && (localDW->UnitDelay_DSTATE > MAX_int32_T - rtb_Sum1_i4)) { rtb_Sum1_i4 = MAX_int32_T; } else { rtb_Sum1_i4 += localDW->UnitDelay_DSTATE; } /* End of Sum: '/Sum2' */ /* Sum: '/Sum1' incorporates: * UnitDelay: '/UnitDelay' */ rtb_Sum1_i4 += localDW->UnitDelay_DSTATE_l; /* Product: '/Divide4' */ tmp = (rtu_err * rtu_P) >> 11; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* Sum: '/Sum6' incorporates: * DataTypeConversion: '/Data Type Conversion1' * Product: '/Divide4' */ tmp = (((rtb_Sum1_i4 >> 16) << 1) + tmp) >> 1; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* Switch: '/Switch2' incorporates: * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Sum: '/Sum6' * Switch: '/Switch' */ if ((int16_T)tmp > rtu_satMax) { *rty_out = rtu_satMax; } else if ((int16_T)tmp < rtu_satMin) { /* Switch: '/Switch' */ *rty_out = rtu_satMin; } else { *rty_out = (int16_T)tmp; } /* End of Switch: '/Switch2' */ /* Update for UnitDelay: '/UnitDelay' incorporates: * Product: '/Divide2' * Sum: '/Sum3' * Sum: '/Sum6' */ localDW->UnitDelay_DSTATE = (int16_T)(*rty_out - (int16_T)tmp) * rtu_Kb; /* Update for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE_l = rtb_Sum1_i4; } /* System reset for atomic system: '/PI_backCalc_fixdt_n' */ void PI_backCalc_fixdt_n_Reset(DW_PI_backCalc_fixdt_f *localDW) { /* InitializeConditions for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = 0; /* InitializeConditions for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE_h = 0; } /* Output and update for atomic system: '/PI_backCalc_fixdt_n' */ int16_T PI_backCalc_fixdt_n(int16_T rtu_err, uint16_T rtu_P, uint16_T rtu_I, uint16_T rtu_Kb, int16_T rtu_ext_limProt, int16_T rtu_satMax, int16_T rtu_satMin, DW_PI_backCalc_fixdt_f *localDW) { int32_T rtb_Sum1_l; int32_T q1; int16_T rty_out_0; /* Sum: '/Sum2' incorporates: * Product: '/Divide1' * UnitDelay: '/UnitDelay' */ rtb_Sum1_l = rtu_err * rtu_I; q1 = rtu_ext_limProt << 10; if ((rtb_Sum1_l < 0) && (q1 < MIN_int32_T - rtb_Sum1_l)) { rtb_Sum1_l = MIN_int32_T; } else if ((rtb_Sum1_l > 0) && (q1 > MAX_int32_T - rtb_Sum1_l)) { rtb_Sum1_l = MAX_int32_T; } else { rtb_Sum1_l += q1; } if ((rtb_Sum1_l < 0) && (localDW->UnitDelay_DSTATE < MIN_int32_T - rtb_Sum1_l)) { rtb_Sum1_l = MIN_int32_T; } else if ((rtb_Sum1_l > 0) && (localDW->UnitDelay_DSTATE > MAX_int32_T - rtb_Sum1_l)) { rtb_Sum1_l = MAX_int32_T; } else { rtb_Sum1_l += localDW->UnitDelay_DSTATE; } /* End of Sum: '/Sum2' */ /* Sum: '/Sum1' incorporates: * UnitDelay: '/UnitDelay' */ rtb_Sum1_l += localDW->UnitDelay_DSTATE_h; /* Product: '/Divide4' */ q1 = (rtu_err * rtu_P) >> 11; if (q1 > 32767) { q1 = 32767; } else { if (q1 < -32768) { q1 = -32768; } } /* Sum: '/Sum6' incorporates: * DataTypeConversion: '/Data Type Conversion1' * Product: '/Divide4' */ q1 = (((rtb_Sum1_l >> 16) << 1) + q1) >> 1; if (q1 > 32767) { q1 = 32767; } else { if (q1 < -32768) { q1 = -32768; } } /* Switch: '/Switch2' incorporates: * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Sum: '/Sum6' * Switch: '/Switch' */ if ((int16_T)q1 > rtu_satMax) { rty_out_0 = rtu_satMax; } else if ((int16_T)q1 < rtu_satMin) { /* Switch: '/Switch' */ rty_out_0 = rtu_satMin; } else { rty_out_0 = (int16_T)q1; } /* End of Switch: '/Switch2' */ /* Update for UnitDelay: '/UnitDelay' incorporates: * Product: '/Divide2' * Sum: '/Sum3' * Sum: '/Sum6' */ localDW->UnitDelay_DSTATE = (int16_T)(rty_out_0 - (int16_T)q1) * rtu_Kb; /* Update for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE_h = rtb_Sum1_l; return rty_out_0; } /* System reset for atomic system: '/Rate_Limiter' */ void Rate_Limiter_Reset(DW_Rate_Limiter *localDW) { /* InitializeConditions for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = 0; } /* Output and update for atomic system: '/Rate_Limiter' */ int32_T Rate_Limiter(int32_T rtu_u, int32_T rtu_initVal, boolean_T rtu_init, int32_T rtu_inc, int32_T rtu_dec, DW_Rate_Limiter *localDW) { int32_T rtb_Switch1_h; int32_T rtb_Sum1_k; int32_T rty_y_0; /* Switch: '/Switch1' incorporates: * UnitDelay: '/UnitDelay' */ if (rtu_init) { rtb_Switch1_h = rtu_initVal; } else { rtb_Switch1_h = localDW->UnitDelay_DSTATE; } /* End of Switch: '/Switch1' */ /* Sum: '/Sum1' */ rtb_Sum1_k = rtu_u - rtb_Switch1_h; rtb_Sum1_k = (rtb_Sum1_k & 134217728) != 0 ? rtb_Sum1_k | -134217728 : rtb_Sum1_k & 134217727; /* Switch: '/Switch2' incorporates: * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Switch: '/Switch' */ if (rtb_Sum1_k > rtu_inc) { rtb_Sum1_k = rtu_inc; } else { if (rtb_Sum1_k < rtu_dec) { /* Switch: '/Switch' */ rtb_Sum1_k = rtu_dec; } } /* End of Switch: '/Switch2' */ /* Sum: '/Sum2' */ rtb_Sum1_k += rtb_Switch1_h; rty_y_0 = (rtb_Sum1_k & 134217728) != 0 ? rtb_Sum1_k | -134217728 : rtb_Sum1_k & 134217727; /* Switch: '/Switch2' */ if (rtu_init) { /* Update for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = rtu_initVal; } else { /* Update for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = rty_y_0; } /* End of Switch: '/Switch2' */ return rty_y_0; } /* * System initialize for atomic system: * '/Counter' * '/Counter' */ void Counter_b_Init(DW_Counter_l *localDW, uint16_T rtp_z_cntInit) { /* InitializeConditions for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = rtp_z_cntInit; } /* * Output and update for atomic system: * '/Counter' * '/Counter' */ uint16_T Counter_i(uint16_T rtu_inc, uint16_T rtu_max, boolean_T rtu_rst, DW_Counter_l *localDW) { uint16_T rtu_rst_0; uint16_T rty_cnt_0; /* Switch: '/Switch1' incorporates: * Constant: '/Constant23' * UnitDelay: '/UnitDelay' */ if (rtu_rst) { rtu_rst_0 = 0U; } else { rtu_rst_0 = localDW->UnitDelay_DSTATE; } /* End of Switch: '/Switch1' */ /* Sum: '/Sum1' */ rty_cnt_0 = (uint16_T)((uint32_T)rtu_inc + rtu_rst_0); /* MinMax: '/MinMax' */ if (rty_cnt_0 < rtu_max) { /* Update for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = rty_cnt_0; } else { /* Update for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = rtu_max; } /* End of MinMax: '/MinMax' */ return rty_cnt_0; } /* * System reset for atomic system: * '/either_edge' * '/either_edge' */ void either_edge_Reset(DW_either_edge *localDW) { /* InitializeConditions for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = false; } /* * Output and update for atomic system: * '/either_edge' * '/either_edge' */ boolean_T either_edge(boolean_T rtu_u, DW_either_edge *localDW) { boolean_T rty_y_0; /* RelationalOperator: '/Relational Operator' incorporates: * UnitDelay: '/UnitDelay' */ rty_y_0 = (rtu_u != localDW->UnitDelay_DSTATE); /* Update for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = rtu_u; return rty_y_0; } /* System initialize for atomic system: '/Debounce_Filter' */ void Debounce_Filter_Init(DW_Debounce_Filter *localDW) { /* SystemInitialize for IfAction SubSystem: '/Qualification' */ /* SystemInitialize for Atomic SubSystem: '/Counter' */ Counter_b_Init(&localDW->Counter_i0, 0U); /* End of SystemInitialize for SubSystem: '/Counter' */ /* End of SystemInitialize for SubSystem: '/Qualification' */ /* SystemInitialize for IfAction SubSystem: '/Dequalification' */ /* SystemInitialize for Atomic SubSystem: '/Counter' */ Counter_b_Init(&localDW->Counter_h, 0U); /* End of SystemInitialize for SubSystem: '/Counter' */ /* End of SystemInitialize for SubSystem: '/Dequalification' */ } /* System reset for atomic system: '/Debounce_Filter' */ void Debounce_Filter_Reset(DW_Debounce_Filter *localDW) { /* InitializeConditions for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = false; /* SystemReset for Atomic SubSystem: '/either_edge' */ either_edge_Reset(&localDW->either_edge_k); /* End of SystemReset for SubSystem: '/either_edge' */ } /* Output and update for atomic system: '/Debounce_Filter' */ void Debounce_Filter(boolean_T rtu_u, uint16_T rtu_tAcv, uint16_T rtu_tDeacv, boolean_T *rty_y, DW_Debounce_Filter *localDW) { uint16_T rtb_Sum1_l; boolean_T rtb_RelationalOperator_f; /* Outputs for Atomic SubSystem: '/either_edge' */ rtb_RelationalOperator_f = either_edge(rtu_u, &localDW->either_edge_k); /* End of Outputs for SubSystem: '/either_edge' */ /* If: '/If2' incorporates: * Constant: '/Constant6' * Constant: '/Constant6' * Inport: '/yPrev' * Logic: '/Logical Operator1' * Logic: '/Logical Operator2' * Logic: '/Logical Operator3' * Logic: '/Logical Operator4' * UnitDelay: '/UnitDelay' */ if (rtu_u && (!localDW->UnitDelay_DSTATE)) { /* Outputs for IfAction SubSystem: '/Qualification' incorporates: * ActionPort: '/Action Port' */ /* Outputs for Atomic SubSystem: '/Counter' */ rtb_Sum1_l = (uint16_T) Counter_i(1U, rtu_tAcv, rtb_RelationalOperator_f, &localDW->Counter_i0); /* End of Outputs for SubSystem: '/Counter' */ /* Switch: '/Switch2' incorporates: * Constant: '/Constant6' * RelationalOperator: '/Relational Operator2' */ *rty_y = ((rtb_Sum1_l > rtu_tAcv) || localDW->UnitDelay_DSTATE); /* End of Outputs for SubSystem: '/Qualification' */ } else if ((!rtu_u) && localDW->UnitDelay_DSTATE) { /* Outputs for IfAction SubSystem: '/Dequalification' incorporates: * ActionPort: '/Action Port' */ /* Outputs for Atomic SubSystem: '/Counter' */ rtb_Sum1_l = (uint16_T) Counter_i(1U, rtu_tDeacv, rtb_RelationalOperator_f, &localDW->Counter_h); /* End of Outputs for SubSystem: '/Counter' */ /* Switch: '/Switch2' incorporates: * Constant: '/Constant6' * RelationalOperator: '/Relational Operator2' */ *rty_y = ((!(rtb_Sum1_l > rtu_tDeacv)) && localDW->UnitDelay_DSTATE); /* End of Outputs for SubSystem: '/Dequalification' */ } else { /* Outputs for IfAction SubSystem: '/Default' incorporates: * ActionPort: '/Action Port' */ *rty_y = localDW->UnitDelay_DSTATE; /* End of Outputs for SubSystem: '/Default' */ } /* End of If: '/If2' */ /* Update for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = *rty_y; } /* Model step function */ void BLDC_controller_step(RT_MODEL *const rtM) { P *rtP = ((P *) rtM->defaultParam); DW *rtDW = ((DW *) rtM->dwork); ExtU *rtU = (ExtU *) rtM->inputs; ExtY *rtY = (ExtY *) rtM->outputs; int32_T tmp; uint8_T rtb_Sum; boolean_T rtb_LogicalOperator; int8_T rtb_Sum2_h; boolean_T rtb_RelationalOperator4_d; boolean_T rtb_RelationalOperator1_m; uint8_T rtb_Sum_l; uint8_T rtb_iq_max_XA; int16_T rtb_Saturation2; int16_T rtb_Merge; int16_T rtb_Switch2_fv; int16_T rtb_Abs5; int16_T rtb_Switch1_a; int16_T rtb_Switch2_d; uint16_T rtb_Divide2_h; int16_T rtb_Saturation; int16_T rtb_Sum6; int16_T rtb_Add; int16_T rtb_TmpSignalConversionAtLow_Pa[2]; uint8_T rtb_r_fieldWeak_XA_o2; int16_T rtb_Gain1; int16_T rtb_Gain6; int32_T rtb_Sum2; int16_T tmp_0[4]; int8_T UnitDelay3; int32_T tmp_1; int32_T tmp_2; int32_T tmp_3; int16_T rtb_Switch2_d_0; /* Outputs for Atomic SubSystem: '/BLDC_controller' */ /* Sum: '/Sum' incorporates: * Gain: '/g_Ha' * Gain: '/g_Hb' * Inport: '/b_hallA ' * Inport: '/b_hallB' * Inport: '/b_hallC' */ rtb_Sum = (uint8_T)((uint32_T)(uint8_T)((uint32_T)(uint8_T)(rtU->b_hallA << 2) + (uint8_T)(rtU->b_hallB << 1)) + rtU->b_hallC); /* Saturate: '/Saturation2' incorporates: * Inport: '/r_inpTgt' */ tmp = rtU->r_inpTgt << 4; if (tmp >= 16000) { rtb_Saturation2 = 16000; } else if (tmp <= -16000) { rtb_Saturation2 = -16000; } else { rtb_Saturation2 = (int16_T)(rtU->r_inpTgt << 4); } /* End of Saturate: '/Saturation2' */ /* If: '/If1' incorporates: * Constant: '/z_ctrlTypSel1' * Inport: '/r_inpTgt' */ if (rtP->z_ctrlTypSel == 0) { /* Outputs for IfAction SubSystem: '/Commutation_Control_Type' incorporates: * ActionPort: '/Action Port' */ rtb_Merge = rtb_Saturation2; /* End of Outputs for SubSystem: '/Commutation_Control_Type' */ } else { /* Outputs for IfAction SubSystem: '/FOC_Control_Type' incorporates: * ActionPort: '/Action Port' */ /* SignalConversion: '/TmpSignal ConversionAtSelectorInport1' incorporates: * Constant: '/Vd_max' * Constant: '/constant1' * Constant: '/i_max' * Constant: '/n_max' */ tmp_0[0] = 0; tmp_0[1] = rtP->Vd_max; tmp_0[2] = rtP->n_max; tmp_0[3] = rtP->i_max; /* Product: '/Divide1' incorporates: * Product: '/Divide4' * Selector: '/Selector' * UnitDelay: '/UnitDelay1' */ rtb_Merge = (int16_T)(((uint16_T)((tmp_0[rtDW->UnitDelay1_DSTATE] << 5) / 125) * rtb_Saturation2) >> 12); /* End of Outputs for SubSystem: '/FOC_Control_Type' */ } /* End of If: '/If1' */ /* Logic: '/Logical Operator' incorporates: * Inport: '/b_hallA ' * Inport: '/b_hallB' * Inport: '/b_hallC' * UnitDelay: '/UnitDelay1' * UnitDelay: '/UnitDelay2' * UnitDelay: '/UnitDelay3' */ rtb_LogicalOperator = (boolean_T)((rtU->b_hallA != 0) ^ (rtU->b_hallB != 0) ^ (rtU->b_hallC != 0) ^ (rtDW->UnitDelay3_DSTATE_fy != 0) ^ (rtDW->UnitDelay1_DSTATE_m != 0)) ^ (rtDW->UnitDelay2_DSTATE_f != 0); /* If: '/If2' incorporates: * If: '/If2' * Inport: '/z_counterRawPrev' * UnitDelay: '/UnitDelay3' */ if (rtb_LogicalOperator) { /* Outputs for IfAction SubSystem: '/F01_04_Direction_Detection' incorporates: * ActionPort: '/Action Port' */ /* UnitDelay: '/UnitDelay3' */ UnitDelay3 = rtDW->Switch2_e; /* Sum: '/Sum2' incorporates: * Constant: '/vec_hallToPos' * Selector: '/Selector' * UnitDelay: '/UnitDelay2' */ rtb_Sum2_h = (int8_T)(rtConstP.vec_hallToPos_Value[rtb_Sum] - rtDW->UnitDelay2_DSTATE_b); /* Switch: '/Switch2' incorporates: * Constant: '/Constant20' * Constant: '/Constant23' * Constant: '/Constant24' * Constant: '/Constant8' * Logic: '/Logical Operator3' * RelationalOperator: '/Relational Operator1' * RelationalOperator: '/Relational Operator6' */ if ((rtb_Sum2_h == 1) || (rtb_Sum2_h == -5)) { rtDW->Switch2_e = 1; } else { rtDW->Switch2_e = -1; } /* End of Switch: '/Switch2' */ /* Update for UnitDelay: '/UnitDelay2' incorporates: * Constant: '/vec_hallToPos' * Selector: '/Selector' */ rtDW->UnitDelay2_DSTATE_b = rtConstP.vec_hallToPos_Value[rtb_Sum]; /* End of Outputs for SubSystem: '/F01_04_Direction_Detection' */ /* Outputs for IfAction SubSystem: '/Raw_Motor_Speed_Estimation' incorporates: * ActionPort: '/Action Port' */ rtDW->z_counterRawPrev = rtDW->UnitDelay3_DSTATE; /* Sum: '/Sum7' incorporates: * Inport: '/z_counterRawPrev' * UnitDelay: '/UnitDelay3' * UnitDelay: '/UnitDelay4' */ rtb_Switch2_fv = (int16_T)(rtDW->z_counterRawPrev - rtDW->UnitDelay4_DSTATE_p); /* Abs: '/Abs2' */ if (rtb_Switch2_fv < 0) { rtb_Switch1_a = (int16_T)-rtb_Switch2_fv; } else { rtb_Switch1_a = rtb_Switch2_fv; } /* End of Abs: '/Abs2' */ /* Relay: '/dz_cntTrnsDet' */ if (rtb_Switch1_a >= rtP->dz_cntTrnsDetHi) { rtDW->dz_cntTrnsDet_Mode = true; } else { if (rtb_Switch1_a <= rtP->dz_cntTrnsDetLo) { rtDW->dz_cntTrnsDet_Mode = false; } } rtDW->dz_cntTrnsDet = rtDW->dz_cntTrnsDet_Mode; /* End of Relay: '/dz_cntTrnsDet' */ /* RelationalOperator: '/Relational Operator4' */ rtb_RelationalOperator4_d = (rtDW->Switch2_e != UnitDelay3); /* Switch: '/Switch3' incorporates: * Constant: '/Constant4' * Logic: '/Logical Operator1' * Switch: '/Switch1' * Switch: '/Switch2' * UnitDelay: '/UnitDelay1' */ if (rtb_RelationalOperator4_d && rtDW->UnitDelay1_DSTATE_n) { rtb_Switch1_a = 0; } else if (rtb_RelationalOperator4_d) { /* Switch: '/Switch2' incorporates: * UnitDelay: '/UnitDelay4' */ rtb_Switch1_a = rtDW->UnitDelay4_DSTATE_e; } else if (rtDW->dz_cntTrnsDet) { /* Switch: '/Switch1' incorporates: * Constant: '/cf_speedCoef' * Product: '/Divide14' * Switch: '/Switch2' */ rtb_Switch1_a = (int16_T)((rtP->cf_speedCoef << 4) / rtDW->z_counterRawPrev); } else { /* Switch: '/Switch1' incorporates: * Constant: '/cf_speedCoef' * Gain: '/g_Ha' * Product: '/Divide13' * Sum: '/Sum13' * Switch: '/Switch2' * UnitDelay: '/UnitDelay2' * UnitDelay: '/UnitDelay3' * UnitDelay: '/UnitDelay5' */ rtb_Switch1_a = (int16_T)(((uint16_T)(rtP->cf_speedCoef << 2) << 4) / (int16_T)(((rtDW->UnitDelay2_DSTATE + rtDW->UnitDelay3_DSTATE_o) + rtDW->UnitDelay5_DSTATE) + rtDW->z_counterRawPrev)); } /* End of Switch: '/Switch3' */ /* Product: '/Divide11' */ rtDW->Divide11 = (int16_T)(rtb_Switch1_a * rtDW->Switch2_e); /* Update for UnitDelay: '/UnitDelay4' */ rtDW->UnitDelay4_DSTATE_p = rtDW->z_counterRawPrev; /* Update for UnitDelay: '/UnitDelay2' incorporates: * UnitDelay: '/UnitDelay3' */ rtDW->UnitDelay2_DSTATE = rtDW->UnitDelay3_DSTATE_o; /* Update for UnitDelay: '/UnitDelay3' incorporates: * UnitDelay: '/UnitDelay5' */ rtDW->UnitDelay3_DSTATE_o = rtDW->UnitDelay5_DSTATE; /* Update for UnitDelay: '/UnitDelay5' */ rtDW->UnitDelay5_DSTATE = rtDW->z_counterRawPrev; /* Update for UnitDelay: '/UnitDelay1' */ rtDW->UnitDelay1_DSTATE_n = rtb_RelationalOperator4_d; /* End of Outputs for SubSystem: '/Raw_Motor_Speed_Estimation' */ } /* End of If: '/If2' */ /* Outputs for Atomic SubSystem: '/Counter' */ /* Constant: '/Constant6' incorporates: * Constant: '/z_maxCntRst2' */ rtb_Switch1_a = (int16_T) Counter(1, rtP->z_maxCntRst, rtb_LogicalOperator, &rtDW->Counter_e); /* End of Outputs for SubSystem: '/Counter' */ /* Switch: '/Switch2' incorporates: * Constant: '/Constant4' * Constant: '/z_maxCntRst' * RelationalOperator: '/Relational Operator2' */ if (rtb_Switch1_a > rtP->z_maxCntRst) { rtb_Switch2_fv = 0; } else { rtb_Switch2_fv = rtDW->Divide11; } /* End of Switch: '/Switch2' */ /* Abs: '/Abs5' */ if (rtb_Switch2_fv < 0) { rtb_Abs5 = (int16_T)-rtb_Switch2_fv; } else { rtb_Abs5 = rtb_Switch2_fv; } /* End of Abs: '/Abs5' */ /* Relay: '/n_commDeacv' */ if (rtb_Abs5 >= rtP->n_commDeacvHi) { rtDW->n_commDeacv_Mode = true; } else { if (rtb_Abs5 <= rtP->n_commAcvLo) { rtDW->n_commDeacv_Mode = false; } } /* Logic: '/Logical Operator2' incorporates: * Constant: '/CTRL_COMM' * Constant: '/z_ctrlTypSel1' * Logic: '/Logical Operator1' * RelationalOperator: '/Relational Operator3' * Relay: '/n_commDeacv' */ rtb_LogicalOperator = ((rtP->z_ctrlTypSel != 0) && rtDW->n_commDeacv_Mode && ( !rtDW->dz_cntTrnsDet)); /* RelationalOperator: '/Relational Operator9' incorporates: * Constant: '/n_stdStillDet' */ rtb_RelationalOperator4_d = (rtb_Abs5 < rtP->n_stdStillDet); /* If: '/If2' incorporates: * Constant: '/b_diagEna' * Constant: '/CTRL_COMM2' * Constant: '/t_errDequal' * Constant: '/t_errQual' * RelationalOperator: '/Relational Operator2' */ rtb_Sum2_h = rtDW->If2_ActiveSubsystem; UnitDelay3 = -1; if (rtP->b_diagEna) { UnitDelay3 = 0; } rtDW->If2_ActiveSubsystem = UnitDelay3; if ((rtb_Sum2_h != UnitDelay3) && (rtb_Sum2_h == 0)) { /* Disable for Outport: '/z_errCode' incorporates: * Outport: '/z_errCode ' */ rtY->z_errCode = 0U; /* Disable for Outport: '/b_errFlag' */ rtDW->Merge_n = false; } if (UnitDelay3 == 0) { if (0 != rtb_Sum2_h) { /* InitializeConditions for IfAction SubSystem: '/F02_Diagnostics' incorporates: * ActionPort: '/Action Port' */ /* InitializeConditions for If: '/If2' incorporates: * UnitDelay: '/UnitDelay' */ rtDW->UnitDelay_DSTATE = 0U; /* End of InitializeConditions for SubSystem: '/F02_Diagnostics' */ /* SystemReset for IfAction SubSystem: '/F02_Diagnostics' incorporates: * ActionPort: '/Action Port' */ /* SystemReset for Atomic SubSystem: '/Debounce_Filter' */ /* SystemReset for If: '/If2' */ Debounce_Filter_Reset(&rtDW->Debounce_Filter_f); /* End of SystemReset for SubSystem: '/Debounce_Filter' */ /* SystemReset for Atomic SubSystem: '/either_edge' */ either_edge_Reset(&rtDW->either_edge_a); /* End of SystemReset for SubSystem: '/either_edge' */ /* End of SystemReset for SubSystem: '/F02_Diagnostics' */ } /* Outputs for IfAction SubSystem: '/F02_Diagnostics' incorporates: * ActionPort: '/Action Port' */ /* Switch: '/Switch3' incorporates: * Abs: '/Abs4' * Constant: '/CTRL_COMM4' * Constant: '/r_errInpTgtThres' * Logic: '/Logical Operator1' * RelationalOperator: '/Relational Operator7' * S-Function (sfix_bitop): '/Bitwise Operator1' * UnitDelay: '/UnitDelay' * UnitDelay: '/UnitDelay4' */ if ((rtDW->UnitDelay_DSTATE & 4) != 0) { rtb_RelationalOperator1_m = true; } else { if (rtDW->UnitDelay4_DSTATE < 0) { /* Abs: '/Abs4' incorporates: * UnitDelay: '/UnitDelay4' */ rtb_Switch2_d_0 = (int16_T)-rtDW->UnitDelay4_DSTATE; } else { /* Abs: '/Abs4' incorporates: * UnitDelay: '/UnitDelay4' */ rtb_Switch2_d_0 = rtDW->UnitDelay4_DSTATE; } rtb_RelationalOperator1_m = ((rtb_Switch2_d_0 > rtP->r_errInpTgtThres) && rtb_RelationalOperator4_d); } /* End of Switch: '/Switch3' */ /* Sum: '/Sum' incorporates: * Constant: '/CTRL_COMM' * Constant: '/CTRL_COMM1' * DataTypeConversion: '/Data Type Conversion3' * Gain: '/g_Hb' * Gain: '/g_Hb1' * RelationalOperator: '/Relational Operator1' * RelationalOperator: '/Relational Operator3' */ rtb_Sum_l = (uint8_T)(((uint32_T)((rtb_Sum == 7) << 1) + (rtb_Sum == 0)) + (rtb_RelationalOperator1_m << 2)); /* Outputs for Atomic SubSystem: '/Debounce_Filter' */ Debounce_Filter(rtb_Sum_l != 0, rtP->t_errQual, rtP->t_errDequal, &rtDW->Merge_n, &rtDW->Debounce_Filter_f); /* End of Outputs for SubSystem: '/Debounce_Filter' */ /* Outputs for Atomic SubSystem: '/either_edge' */ rtb_RelationalOperator1_m = either_edge(rtDW->Merge_n, &rtDW->either_edge_a); /* End of Outputs for SubSystem: '/either_edge' */ /* Switch: '/Switch1' incorporates: * Constant: '/CTRL_COMM2' * Constant: '/t_errDequal' * Constant: '/t_errQual' * RelationalOperator: '/Relational Operator2' */ if (rtb_RelationalOperator1_m) { /* Outport: '/z_errCode' */ rtY->z_errCode = rtb_Sum_l; } else { /* Outport: '/z_errCode' incorporates: * UnitDelay: '/UnitDelay' */ rtY->z_errCode = rtDW->UnitDelay_DSTATE; } /* End of Switch: '/Switch1' */ /* Update for UnitDelay: '/UnitDelay' incorporates: * Outport: '/z_errCode' */ rtDW->UnitDelay_DSTATE = rtY->z_errCode; /* End of Outputs for SubSystem: '/F02_Diagnostics' */ } /* End of If: '/If2' */ /* Logic: '/Logical Operator4' incorporates: * Constant: '/constant2' * Constant: '/constant8' * Inport: '/b_motEna' * Inport: '/z_ctrlModReq' * Logic: '/Logical Operator1' * Logic: '/Logical Operator7' * RelationalOperator: '/Relational Operator10' * RelationalOperator: '/Relational Operator11' * RelationalOperator: '/Relational Operator2' * UnitDelay: '/UnitDelay1' */ rtb_RelationalOperator1_m = ((!rtU->b_motEna) || rtDW->Merge_n || (rtU->z_ctrlModReq == 0) || ((rtU->z_ctrlModReq != rtDW->UnitDelay1_DSTATE) && (rtDW->UnitDelay1_DSTATE != 0))); /* Chart: '/F02_02_Control_Mode_Manager' incorporates: * Constant: '/constant' * Constant: '/constant1' * Constant: '/constant5' * Constant: '/constant6' * Constant: '/constant7' * Inport: '/z_ctrlModReq' * Logic: '/Logical Operator3' * Logic: '/Logical Operator6' * Logic: '/Logical Operator9' * RelationalOperator: '/Relational Operator1' * RelationalOperator: '/Relational Operator3' * RelationalOperator: '/Relational Operator4' * RelationalOperator: '/Relational Operator5' * RelationalOperator: '/Relational Operator6' */ if (rtDW->is_active_c1_BLDC_controller == 0U) { rtDW->is_active_c1_BLDC_controller = 1U; rtDW->is_c1_BLDC_controller = IN_OPEN; rtb_Sum_l = OPEN_MODE; } else if (rtDW->is_c1_BLDC_controller == IN_ACTIVE) { if (rtb_RelationalOperator1_m) { rtDW->is_ACTIVE = IN_NO_ACTIVE_CHILD; rtDW->is_c1_BLDC_controller = IN_OPEN; rtb_Sum_l = OPEN_MODE; } else { switch (rtDW->is_ACTIVE) { case IN_SPEED_MODE: rtb_Sum_l = SPD_MODE; break; case IN_TORQUE_MODE: rtb_Sum_l = TRQ_MODE; break; default: rtb_Sum_l = VLT_MODE; break; } } } else { rtb_Sum_l = OPEN_MODE; if ((!rtb_RelationalOperator1_m) && ((rtU->z_ctrlModReq == 1) || (rtU->z_ctrlModReq == 2) || (rtU->z_ctrlModReq == 3)) && rtb_RelationalOperator4_d) { rtDW->is_c1_BLDC_controller = IN_ACTIVE; if (rtU->z_ctrlModReq == 3) { rtDW->is_ACTIVE = IN_TORQUE_MODE; rtb_Sum_l = TRQ_MODE; } else if (rtU->z_ctrlModReq == 2) { rtDW->is_ACTIVE = IN_SPEED_MODE; rtb_Sum_l = SPD_MODE; } else { rtDW->is_ACTIVE = IN_VOLTAGE_MODE; rtb_Sum_l = VLT_MODE; } } } /* End of Chart: '/F02_02_Control_Mode_Manager' */ /* Switch: '/Switch2' incorporates: * Constant: '/Constant16' * Product: '/Divide1' * Product: '/Divide3' * RelationalOperator: '/Relational Operator7' * Sum: '/Sum3' * Switch: '/Switch3' */ if (rtb_LogicalOperator) { /* MinMax: '/MinMax' */ rtb_Switch2_d = rtb_Switch1_a; if (!(rtb_Switch2_d < rtDW->z_counterRawPrev)) { rtb_Switch2_d = rtDW->z_counterRawPrev; } /* End of MinMax: '/MinMax' */ /* Switch: '/Switch3' incorporates: * Constant: '/Constant16' * Constant: '/vec_hallToPos' * RelationalOperator: '/Relational Operator7' * Selector: '/Selector' * Sum: '/Sum1' */ if (rtDW->Switch2_e == 1) { rtb_Sum2_h = rtConstP.vec_hallToPos_Value[rtb_Sum]; } else { rtb_Sum2_h = (int8_T)(rtConstP.vec_hallToPos_Value[rtb_Sum] + 1); } rtb_Switch2_d = (int16_T)(((int16_T)((int16_T)((rtb_Switch2_d << 14) / rtDW->z_counterRawPrev) * rtDW->Switch2_e) + (rtb_Sum2_h << 14)) >> 2); } else { if (rtDW->Switch2_e == 1) { /* Switch: '/Switch3' incorporates: * Constant: '/vec_hallToPos' * Selector: '/Selector' */ rtb_Sum2_h = rtConstP.vec_hallToPos_Value[rtb_Sum]; } else { /* Switch: '/Switch3' incorporates: * Constant: '/vec_hallToPos' * Selector: '/Selector' * Sum: '/Sum1' */ rtb_Sum2_h = (int8_T)(rtConstP.vec_hallToPos_Value[rtb_Sum] + 1); } rtb_Switch2_d = (int16_T)(rtb_Sum2_h << 12); } /* End of Switch: '/Switch2' */ /* MinMax: '/MinMax1' incorporates: * Constant: '/Constant1' */ if (!(rtb_Switch2_d > 0)) { rtb_Switch2_d = 0; } /* End of MinMax: '/MinMax1' */ /* Product: '/Divide2' */ rtb_Divide2_h = (uint16_T)((15 * rtb_Switch2_d) >> 4); /* Saturate: '/Saturation' incorporates: * Inport: '/i_phaAB' */ tmp = rtU->i_phaAB << 4; if (tmp >= 32000) { rtb_Saturation = 32000; } else if (tmp <= -32000) { rtb_Saturation = -32000; } else { rtb_Saturation = (int16_T)(rtU->i_phaAB << 4); } /* End of Saturate: '/Saturation' */ /* Saturate: '/Saturation1' incorporates: * Inport: '/i_phaBC' */ tmp = rtU->i_phaBC << 4; if (tmp >= 32000) { rtb_Switch2_d = 32000; } else if (tmp <= -32000) { rtb_Switch2_d = -32000; } else { rtb_Switch2_d = (int16_T)(rtU->i_phaBC << 4); } /* End of Saturate: '/Saturation1' */ /* If: '/If1' incorporates: * Constant: '/z_ctrlTypSel1' * Constant: '/cf_currFilt' */ rtb_Sum2_h = rtDW->If1_ActiveSubsystem; UnitDelay3 = -1; if (rtP->z_ctrlTypSel != 0) { UnitDelay3 = 0; } rtDW->If1_ActiveSubsystem = UnitDelay3; if ((rtb_Sum2_h != UnitDelay3) && (rtb_Sum2_h == 0)) { /* Disable for If: '/If1' */ if (rtDW->If1_ActiveSubsystem_h == 0) { /* Disable for Outport: '/Vd' */ rtDW->Switch2 = 0; } rtDW->If1_ActiveSubsystem_h = -1; /* End of Disable for If: '/If1' */ /* Disable for If: '/If1' */ if (rtDW->If1_ActiveSubsystem_f == 0) { /* Disable for Outport: '/iq_limProt' */ rtDW->Divide4 = 0; } rtDW->If1_ActiveSubsystem_f = -1; /* End of Disable for If: '/If1' */ /* Disable for If: '/If2' */ if (rtDW->If2_ActiveSubsystem_c == 0) { /* Disable for Outport: '/n_limProt' */ rtDW->Divide1 = 0; } rtDW->If2_ActiveSubsystem_c = -1; /* End of Disable for If: '/If2' */ /* Disable for SwitchCase: '/Switch Case' */ rtDW->SwitchCase_ActiveSubsystem = -1; /* Disable for Outport: '/r_phaA' */ rtDW->Gain4[0] = 0; /* Disable for Outport: '/r_phaB' */ rtDW->Gain4[1] = 0; /* Disable for Outport: '/r_phaC ' */ rtDW->Gain4[2] = 0; /* Disable for Outport: '/Vq' */ rtDW->Merge = 0; /* Disable for Outport: '/r_devSignal1' */ rtDW->Sum1[0] = 0; /* Disable for Outport: '/r_devSignal2' */ rtDW->Sum1[1] = 0; } if (UnitDelay3 == 0) { if (0 != rtb_Sum2_h) { /* InitializeConditions for IfAction SubSystem: '/F04_Field_Oriented_Control' incorporates: * ActionPort: '/Action Port' */ /* InitializeConditions for If: '/If1' incorporates: * UnitDelay: '/UnitDelay4' */ rtDW->UnitDelay4_DSTATE_er = 0; /* End of InitializeConditions for SubSystem: '/F04_Field_Oriented_Control' */ /* SystemReset for IfAction SubSystem: '/F04_Field_Oriented_Control' incorporates: * ActionPort: '/Action Port' */ /* SystemReset for Atomic SubSystem: '/Low_Pass_Filter' */ /* SystemReset for If: '/If1' */ Low_Pass_Filter_Reset(&rtDW->Low_Pass_Filter_m); /* End of SystemReset for SubSystem: '/Low_Pass_Filter' */ /* End of SystemReset for SubSystem: '/F04_Field_Oriented_Control' */ } /* Outputs for IfAction SubSystem: '/F04_Field_Oriented_Control' incorporates: * ActionPort: '/Action Port' */ /* If: '/If1' incorporates: * Constant: '/b_selPhaABCurrMeas' */ if (rtP->b_selPhaABCurrMeas) { /* Outputs for IfAction SubSystem: '/Clarke_PhasesAB' incorporates: * ActionPort: '/Action Port' */ /* Gain: '/Gain4' */ tmp = 18919 * rtb_Saturation; /* Gain: '/Gain2' */ tmp_3 = 18919 * rtb_Switch2_d; /* Sum: '/Sum1' incorporates: * Gain: '/Gain2' * Gain: '/Gain4' */ tmp = (((tmp < 0 ? 32767 : 0) + tmp) >> 15) + (int16_T)(((tmp_3 < 0 ? 16383 : 0) + tmp_3) >> 14); if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } rtb_Sum6 = (int16_T)tmp; /* End of Sum: '/Sum1' */ /* End of Outputs for SubSystem: '/Clarke_PhasesAB' */ } else { /* Outputs for IfAction SubSystem: '/Clarke_PhasesBC' incorporates: * ActionPort: '/Action Port' */ /* Sum: '/Sum3' */ tmp = rtb_Saturation - rtb_Switch2_d; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* Gain: '/Gain2' incorporates: * Sum: '/Sum3' */ tmp *= 18919; rtb_Sum6 = (int16_T)(((tmp < 0 ? 32767 : 0) + tmp) >> 15); /* Sum: '/Sum1' */ tmp = -rtb_Saturation - rtb_Switch2_d; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } rtb_Saturation = (int16_T)tmp; /* End of Sum: '/Sum1' */ /* End of Outputs for SubSystem: '/Clarke_PhasesBC' */ } /* End of If: '/If1' */ /* PreLookup: '/a_elecAngle_XA' */ rtb_iq_max_XA = plook_u8u16_evencka(rtb_Divide2_h, 0U, 128U, 180U); /* Interpolation_n-D: '/r_sin_M1' */ rtb_Add = rtConstP.r_sin_M1_Table[rtb_iq_max_XA]; /* Interpolation_n-D: '/r_cos_M1' */ rtb_Switch2_d = rtConstP.r_cos_M1_Table[rtb_iq_max_XA]; /* Sum: '/Sum6' incorporates: * Interpolation_n-D: '/r_cos_M1' * Interpolation_n-D: '/r_sin_M1' * Product: '/Divide1' * Product: '/Divide4' */ tmp = (int16_T)((rtb_Sum6 * rtConstP.r_cos_M1_Table[rtb_iq_max_XA]) >> 14) - (int16_T)((rtb_Saturation * rtConstP.r_sin_M1_Table[rtb_iq_max_XA]) >> 14); if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* SignalConversion: '/TmpSignal ConversionAtLow_Pass_FilterInport1' incorporates: * Sum: '/Sum6' */ rtb_TmpSignalConversionAtLow_Pa[0] = (int16_T)tmp; /* Sum: '/Sum1' incorporates: * Interpolation_n-D: '/r_cos_M1' * Interpolation_n-D: '/r_sin_M1' * Product: '/Divide2' * Product: '/Divide3' */ tmp = (int16_T)((rtb_Saturation * rtConstP.r_cos_M1_Table[rtb_iq_max_XA]) >> 14) + (int16_T)((rtb_Sum6 * rtConstP.r_sin_M1_Table[rtb_iq_max_XA]) >> 14); if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* SignalConversion: '/TmpSignal ConversionAtLow_Pass_FilterInport1' incorporates: * Sum: '/Sum1' */ rtb_TmpSignalConversionAtLow_Pa[1] = (int16_T)tmp; /* Outputs for Atomic SubSystem: '/Low_Pass_Filter' */ Low_Pass_Filter(rtb_TmpSignalConversionAtLow_Pa, rtP->cf_currFilt, rtDW->Sum1, &rtDW->Low_Pass_Filter_m); /* End of Outputs for SubSystem: '/Low_Pass_Filter' */ /* Relay: '/n_fieldWeakAuth' incorporates: * Constant: '/cf_currFilt' */ if (rtb_Abs5 >= rtP->n_fieldWeakAuthHi) { rtDW->n_fieldWeakAuth_Mode = true; } else { if (rtb_Abs5 <= rtP->n_fieldWeakAuthLo) { rtDW->n_fieldWeakAuth_Mode = false; } } /* Switch: '/Switch1' incorporates: * Constant: '/a_elecPeriod1' * Constant: '/b_fieldWeakEna' * Logic: '/Logical Operator2' * Relay: '/n_fieldWeakAuth' */ if (rtP->b_fieldWeakEna && rtDW->n_fieldWeakAuth_Mode) { /* Abs: '/Abs5' */ if (rtb_Saturation2 < 0) { rtb_Saturation2 = (int16_T)-rtb_Saturation2; } /* End of Abs: '/Abs5' */ /* PreLookup: '/r_fieldWeak_XA' */ rtb_iq_max_XA = plook_u8s16u8n6_evenc_s(rtb_Saturation2, rtP->r_fieldWeak_XA[0], (uint16_T)(rtP->r_fieldWeak_XA[1] - rtP->r_fieldWeak_XA[0]), 11U, &rtb_r_fieldWeak_XA_o2); /* Interpolation_n-D: '/id_fieldWeak_M1' */ rtb_Saturation = intrp1d_s16s32s32u8u8n6l_s(rtb_iq_max_XA, rtb_r_fieldWeak_XA_o2, rtP->id_fieldWeak_M1); } else { rtb_Saturation = 0; } /* End of Switch: '/Switch1' */ /* Gain: '/toNegative' */ rtb_Saturation2 = (int16_T)-rtb_Saturation; /* Gain: '/Gain4' incorporates: * Constant: '/i_max' */ rtb_Sum6 = (int16_T)-rtP->i_max; /* If: '/If1' incorporates: * Constant: '/Vd_max1' * Constant: '/cf_idKb' * Constant: '/cf_idKi' * Constant: '/cf_idKp' * Constant: '/constant' * Gain: '/Gain3' * Sum: '/Sum3' */ rtb_Sum2_h = rtDW->If1_ActiveSubsystem_h; UnitDelay3 = -1; if (rtb_LogicalOperator) { UnitDelay3 = 0; } rtDW->If1_ActiveSubsystem_h = UnitDelay3; if ((rtb_Sum2_h != UnitDelay3) && (rtb_Sum2_h == 0)) { /* Disable for Outport: '/Vd' */ rtDW->Switch2 = 0; } if (UnitDelay3 == 0) { if (0 != rtb_Sum2_h) { /* SystemReset for IfAction SubSystem: '/Vd_Calculation' incorporates: * ActionPort: '/Action Port' */ /* SystemReset for Atomic SubSystem: '/PI_backCalc_fixdt_Id' */ /* SystemReset for If: '/If1' */ PI_backCalc_fixdt_Reset(&rtDW->PI_backCalc_fixdt_Id); /* End of SystemReset for SubSystem: '/PI_backCalc_fixdt_Id' */ /* End of SystemReset for SubSystem: '/Vd_Calculation' */ } /* Outputs for IfAction SubSystem: '/Vd_Calculation' incorporates: * ActionPort: '/Action Port' */ /* Switch: '/Switch2' incorporates: * Constant: '/i_max' * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Switch: '/Switch' */ if (rtb_Saturation2 > rtP->i_max) { rtb_Saturation2 = rtP->i_max; } else { if (rtb_Saturation2 < rtb_Sum6) { /* Switch: '/Switch' */ rtb_Saturation2 = rtb_Sum6; } } /* End of Switch: '/Switch2' */ /* Sum: '/Sum3' */ tmp = rtb_Saturation2 - rtDW->Sum1[1]; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* Outputs for Atomic SubSystem: '/PI_backCalc_fixdt_Id' */ PI_backCalc_fixdt((int16_T)tmp, rtP->cf_idKp, rtP->cf_idKi, rtP->cf_idKb, 0, rtP->Vd_max, (int16_T)-rtP->Vd_max, &rtDW->Switch2, &rtDW->PI_backCalc_fixdt_Id); /* End of Outputs for SubSystem: '/PI_backCalc_fixdt_Id' */ /* End of Outputs for SubSystem: '/Vd_Calculation' */ } /* End of If: '/If1' */ /* Abs: '/Abs5' */ if (rtDW->Switch2 < 0) { rtb_Switch2_d_0 = (int16_T)-rtDW->Switch2; } else { rtb_Switch2_d_0 = rtDW->Switch2; } /* End of Abs: '/Abs5' */ /* PreLookup: '/Vq_max_XA' */ rtb_iq_max_XA = plook_u8s16_evencka(rtb_Switch2_d_0, rtP->Vq_max_XA[0], (uint16_T)(rtP->Vq_max_XA[1] - rtP->Vq_max_XA[0]), 45U); /* Interpolation_n-D: '/Vq_max_M1' */ rtb_Saturation2 = rtP->Vq_max_M1[rtb_iq_max_XA]; /* Gain: '/Gain5' incorporates: * Interpolation_n-D: '/Vq_max_M1' */ rtb_Sum6 = (int16_T)-rtP->Vq_max_M1[rtb_iq_max_XA]; /* PreLookup: '/iq_max_XA' */ rtb_iq_max_XA = plook_u8s16_evencka(rtb_Saturation, rtP->iq_max_XA[0], (uint16_T)(rtP->iq_max_XA[1] - rtP->iq_max_XA[0]), 50U); /* MinMax: '/MinMax' incorporates: * Constant: '/i_max' * Interpolation_n-D: '/iq_max_M1' */ if (rtP->i_max < rtP->iq_max_M1[rtb_iq_max_XA]) { rtb_Saturation = rtP->i_max; } else { rtb_Saturation = rtP->iq_max_M1[rtb_iq_max_XA]; } /* End of MinMax: '/MinMax' */ /* Gain: '/Gain1' */ rtb_Gain1 = (int16_T)-rtb_Saturation; /* Gain: '/Gain6' incorporates: * Constant: '/n_max1' */ rtb_Gain6 = (int16_T)-rtP->n_max; /* If: '/If1' incorporates: * Constant: '/CTRL_COMM' * Constant: '/CTRL_COMM1' * Logic: '/Logical Operator2' * RelationalOperator: '/Relational Operator1' * RelationalOperator: '/Relational Operator2' */ rtb_Sum2_h = rtDW->If1_ActiveSubsystem_f; UnitDelay3 = -1; if ((rtb_Sum_l == 1) || (rtb_Sum_l == 2)) { UnitDelay3 = 0; } rtDW->If1_ActiveSubsystem_f = UnitDelay3; if ((rtb_Sum2_h != UnitDelay3) && (rtb_Sum2_h == 0)) { /* Disable for Outport: '/iq_limProt' */ rtDW->Divide4 = 0; } if (UnitDelay3 == 0) { /* Outputs for IfAction SubSystem: '/Current_Limit_Protection' incorporates: * ActionPort: '/Action Port' */ /* Switch: '/Switch2' incorporates: * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Switch: '/Switch' */ if (rtDW->Sum1[0] > rtb_Saturation) { rtb_Switch2_d_0 = rtb_Saturation; } else if (rtDW->Sum1[0] < rtb_Gain1) { /* Switch: '/Switch' */ rtb_Switch2_d_0 = rtb_Gain1; } else { rtb_Switch2_d_0 = rtDW->Sum1[0]; } /* End of Switch: '/Switch2' */ /* Product: '/Divide4' incorporates: * Constant: '/cf_iqKpLimProt' * Sum: '/Sum3' */ tmp = ((int16_T)(rtb_Switch2_d_0 - rtDW->Sum1[0]) * rtP->cf_iqKpLimProt) >> 8; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } rtDW->Divide4 = (int16_T)tmp; /* End of Product: '/Divide4' */ /* End of Outputs for SubSystem: '/Current_Limit_Protection' */ } /* End of If: '/If1' */ /* If: '/If2' incorporates: * Constant: '/CTRL_COMM2' * Constant: '/CTRL_COMM3' * Logic: '/Logical Operator1' * RelationalOperator: '/Relational Operator3' * RelationalOperator: '/Relational Operator4' */ rtb_Sum2_h = rtDW->If2_ActiveSubsystem_c; UnitDelay3 = -1; if ((rtb_Sum_l == 1) || (rtb_Sum_l == 3)) { UnitDelay3 = 0; } rtDW->If2_ActiveSubsystem_c = UnitDelay3; if ((rtb_Sum2_h != UnitDelay3) && (rtb_Sum2_h == 0)) { /* Disable for Outport: '/n_limProt' */ rtDW->Divide1 = 0; } if (UnitDelay3 == 0) { /* Outputs for IfAction SubSystem: '/Speed_Limit_Protection' incorporates: * ActionPort: '/Action Port' */ /* Switch: '/Switch2' incorporates: * Constant: '/n_max1' * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Switch: '/Switch' */ if (rtb_Switch2_fv > rtP->n_max) { rtb_Switch2_d_0 = rtP->n_max; } else if (rtb_Switch2_fv < rtb_Gain6) { /* Switch: '/Switch' */ rtb_Switch2_d_0 = rtb_Gain6; } else { rtb_Switch2_d_0 = rtb_Switch2_fv; } /* End of Switch: '/Switch2' */ /* Product: '/Divide1' incorporates: * Constant: '/cf_nKpLimProt' * Sum: '/Sum1' */ tmp = ((int16_T)(rtb_Switch2_d_0 - rtb_Switch2_fv) * rtP->cf_nKpLimProt) >> 8; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } rtDW->Divide1 = (int16_T)tmp; /* End of Product: '/Divide1' */ /* End of Outputs for SubSystem: '/Speed_Limit_Protection' */ } /* End of If: '/If2' */ /* SwitchCase: '/Switch Case' incorporates: * Constant: '/Constant23' * Constant: '/dV_openRate' * Constant: '/cf_iqKiLimProt' * Constant: '/cf_nKb' * Constant: '/cf_nKi' * Constant: '/cf_nKp' * DataTypeConversion: '/Data Type Conversion' * Gain: '/Gain3' * Product: '/Divide1' * SignalConversion: '/Signal Conversion2' * Sum: '/Sum3' */ rtb_Sum2_h = rtDW->SwitchCase_ActiveSubsystem; switch (rtb_Sum_l) { case 1: UnitDelay3 = 0; break; case 2: UnitDelay3 = 1; break; case 3: UnitDelay3 = 2; break; default: UnitDelay3 = 3; break; } rtDW->SwitchCase_ActiveSubsystem = UnitDelay3; switch (UnitDelay3) { case 0: /* Outputs for IfAction SubSystem: '/Voltage_Mode' incorporates: * ActionPort: '/Action Port' */ /* Sum: '/Sum3' */ tmp = (rtb_Merge + rtDW->Divide4) + rtDW->Divide1; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* Switch: '/Switch2' incorporates: * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Sum: '/Sum3' * Switch: '/Switch' */ if ((int16_T)tmp > rtb_Saturation2) { /* SignalConversion: '/Signal Conversion2' */ rtDW->Merge = rtb_Saturation2; } else if ((int16_T)tmp < rtb_Sum6) { /* Switch: '/Switch' incorporates: * SignalConversion: '/Signal Conversion2' */ rtDW->Merge = rtb_Sum6; } else { /* SignalConversion: '/Signal Conversion2' */ rtDW->Merge = (int16_T)tmp; } /* End of Switch: '/Switch2' */ /* End of Outputs for SubSystem: '/Voltage_Mode' */ break; case 1: if (UnitDelay3 != rtb_Sum2_h) { /* SystemReset for IfAction SubSystem: '/Speed_Mode' incorporates: * ActionPort: '/Action Port' */ /* SystemReset for Atomic SubSystem: '/PI_backCalc_fixdt_n' */ /* SystemReset for SwitchCase: '/Switch Case' */ PI_backCalc_fixdt_n_Reset(&rtDW->PI_backCalc_fixdt_n_p); /* End of SystemReset for SubSystem: '/PI_backCalc_fixdt_n' */ /* End of SystemReset for SubSystem: '/Speed_Mode' */ } /* Outputs for IfAction SubSystem: '/Speed_Mode' incorporates: * ActionPort: '/Action Port' */ /* Switch: '/Switch2' incorporates: * Constant: '/n_max1' * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Switch: '/Switch' */ if (rtb_Merge > rtP->n_max) { rtb_Gain6 = rtP->n_max; } else { if (!(rtb_Merge < rtb_Gain6)) { rtb_Gain6 = rtb_Merge; } } /* End of Switch: '/Switch2' */ /* Sum: '/Sum3' */ tmp = rtb_Gain6 - rtb_Switch2_fv; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* Outputs for Atomic SubSystem: '/PI_backCalc_fixdt_n' */ rtDW->Merge = (int16_T) PI_backCalc_fixdt_n((int16_T)tmp, rtP->cf_nKp, rtP->cf_nKi, rtP->cf_nKb, (int16_T)((rtDW->Divide4 * rtP->cf_iqKiLimProt) >> 10), rtb_Saturation2, rtb_Sum6, &rtDW->PI_backCalc_fixdt_n_p); /* End of Outputs for SubSystem: '/PI_backCalc_fixdt_n' */ /* End of Outputs for SubSystem: '/Speed_Mode' */ break; case 2: if (UnitDelay3 != rtb_Sum2_h) { /* SystemReset for IfAction SubSystem: '/Torque_Mode' incorporates: * ActionPort: '/Action Port' */ /* SystemReset for Atomic SubSystem: '/PI_backCalc_fixdt_Iq' */ /* SystemReset for SwitchCase: '/Switch Case' */ PI_backCalc_fixdt_Reset(&rtDW->PI_backCalc_fixdt_Iq); /* End of SystemReset for SubSystem: '/PI_backCalc_fixdt_Iq' */ /* End of SystemReset for SubSystem: '/Torque_Mode' */ } /* Outputs for IfAction SubSystem: '/Torque_Mode' incorporates: * ActionPort: '/Action Port' */ /* Sum: '/Sum2' */ tmp = rtb_Merge + rtDW->Divide1; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* Switch: '/Switch2' incorporates: * RelationalOperator: '/LowerRelop1' * Sum: '/Sum2' */ if (!((int16_T)tmp > rtb_Saturation)) { /* Switch: '/Switch' incorporates: * RelationalOperator: '/UpperRelop' */ if ((int16_T)tmp < rtb_Gain1) { rtb_Saturation = rtb_Gain1; } else { rtb_Saturation = (int16_T)tmp; } /* End of Switch: '/Switch' */ } /* End of Switch: '/Switch2' */ /* Sum: '/Sum1' */ tmp = rtb_Saturation - rtDW->Sum1[0]; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* Outputs for Atomic SubSystem: '/PI_backCalc_fixdt_Iq' */ /* SignalConversion: '/Signal Conversion2' incorporates: * Constant: '/cf_iqKb' * Constant: '/cf_iqKi' * Constant: '/cf_iqKp' * Constant: '/constant' * Sum: '/Sum1' */ PI_backCalc_fixdt((int16_T)tmp, rtP->cf_iqKp, rtP->cf_iqKi, rtP->cf_iqKb, 0, rtb_Saturation2, rtb_Sum6, &rtDW->Merge, &rtDW->PI_backCalc_fixdt_Iq); /* End of Outputs for SubSystem: '/PI_backCalc_fixdt_Iq' */ /* End of Outputs for SubSystem: '/Torque_Mode' */ break; case 3: if (UnitDelay3 != rtb_Sum2_h) { /* SystemReset for IfAction SubSystem: '/Open_Mode' incorporates: * ActionPort: '/Action Port' */ /* SystemReset for Atomic SubSystem: '/rising_edge_init' */ /* SystemReset for SwitchCase: '/Switch Case' incorporates: * UnitDelay: '/UnitDelay' */ rtDW->UnitDelay_DSTATE_g = true; /* End of SystemReset for SubSystem: '/rising_edge_init' */ /* SystemReset for Atomic SubSystem: '/Rate_Limiter' */ Rate_Limiter_Reset(&rtDW->Rate_Limiter_l); /* End of SystemReset for SubSystem: '/Rate_Limiter' */ /* End of SystemReset for SubSystem: '/Open_Mode' */ } /* Outputs for IfAction SubSystem: '/Open_Mode' incorporates: * ActionPort: '/Action Port' */ /* Outputs for Atomic SubSystem: '/rising_edge_init' */ /* UnitDelay: '/UnitDelay' */ rtb_RelationalOperator4_d = rtDW->UnitDelay_DSTATE_g; /* Update for UnitDelay: '/UnitDelay' incorporates: * Constant: '/Constant' */ rtDW->UnitDelay_DSTATE_g = false; /* End of Outputs for SubSystem: '/rising_edge_init' */ /* DataTypeConversion: '/Data Type Conversion' incorporates: * UnitDelay: '/UnitDelay4' */ tmp = rtDW->UnitDelay4_DSTATE_er << 12; /* Gain: '/Gain3' incorporates: * Constant: '/dV_openRate' */ tmp_3 = -rtP->dV_openRate; /* Outputs for Atomic SubSystem: '/Rate_Limiter' */ rtb_Sum2 = Rate_Limiter(0, (tmp & 134217728) != 0 ? tmp | -134217728 : tmp & 134217727, rtb_RelationalOperator4_d, rtP->dV_openRate, (tmp_3 & 134217728) != 0 ? tmp_3 | -134217728 : tmp_3 & 134217727, &rtDW->Rate_Limiter_l); /* End of Outputs for SubSystem: '/Rate_Limiter' */ /* DataTypeConversion: '/Data Type Conversion1' incorporates: * Constant: '/Constant23' * Constant: '/dV_openRate' * DataTypeConversion: '/Data Type Conversion' * Gain: '/Gain3' */ rtDW->Merge = (int16_T)(rtb_Sum2 >> 12); /* End of Outputs for SubSystem: '/Open_Mode' */ break; } /* End of SwitchCase: '/Switch Case' */ /* Sum: '/Sum6' incorporates: * Product: '/Divide1' * Product: '/Divide4' */ tmp = (int16_T)((rtDW->Switch2 * rtb_Switch2_d) >> 14) - (int16_T) ((rtDW->Merge * rtb_Add) >> 14); if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* Sum: '/Sum1' incorporates: * Product: '/Divide2' * Product: '/Divide3' */ tmp_3 = (int16_T)((rtDW->Switch2 * rtb_Add) >> 14) + (int16_T)((rtDW->Merge * rtb_Switch2_d) >> 14); if (tmp_3 > 32767) { tmp_3 = 32767; } else { if (tmp_3 < -32768) { tmp_3 = -32768; } } /* Gain: '/Gain1' incorporates: * Sum: '/Sum1' */ tmp_3 = 14189 * (int16_T)tmp_3; /* Sum: '/Sum6' incorporates: * Gain: '/Gain1' * Gain: '/Gain3' * Sum: '/Sum6' */ tmp_3 = (((tmp_3 < 0 ? 16383 : 0) + tmp_3) >> 14) - ((int16_T)(((int16_T)tmp < 0) + (int16_T)tmp) >> 1); if (tmp_3 > 32767) { tmp_3 = 32767; } else { if (tmp_3 < -32768) { tmp_3 = -32768; } } /* Sum: '/Sum2' incorporates: * Sum: '/Sum6' * Sum: '/Sum6' */ tmp_2 = -(int16_T)tmp - (int16_T)tmp_3; if (tmp_2 > 32767) { tmp_2 = 32767; } else { if (tmp_2 < -32768) { tmp_2 = -32768; } } /* MinMax: '/MinMax1' incorporates: * Sum: '/Sum2' * Sum: '/Sum6' * Sum: '/Sum6' */ rtb_Saturation2 = (int16_T)tmp; if (!((int16_T)tmp < (int16_T)tmp_3)) { rtb_Saturation2 = (int16_T)tmp_3; } if (!(rtb_Saturation2 < (int16_T)tmp_2)) { rtb_Saturation2 = (int16_T)tmp_2; } /* MinMax: '/MinMax2' incorporates: * Sum: '/Sum2' * Sum: '/Sum6' * Sum: '/Sum6' */ rtb_Switch2_d = (int16_T)tmp; if (!((int16_T)tmp > (int16_T)tmp_3)) { rtb_Switch2_d = (int16_T)tmp_3; } if (!(rtb_Switch2_d > (int16_T)tmp_2)) { rtb_Switch2_d = (int16_T)tmp_2; } /* Sum: '/Add' incorporates: * MinMax: '/MinMax1' * MinMax: '/MinMax2' */ tmp_1 = rtb_Saturation2 + rtb_Switch2_d; if (tmp_1 > 32767) { tmp_1 = 32767; } else { if (tmp_1 < -32768) { tmp_1 = -32768; } } /* Gain: '/Gain2' incorporates: * Sum: '/Add' */ rtb_Add = (int16_T)(tmp_1 >> 1); /* Sum: '/Add1' incorporates: * Sum: '/Sum6' */ tmp = (int16_T)tmp - rtb_Add; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* Gain: '/Gain4' incorporates: * Sum: '/Add1' */ rtDW->Gain4[0] = (int16_T)((18919 * tmp) >> 18); /* Sum: '/Add1' incorporates: * Sum: '/Sum6' */ tmp = (int16_T)tmp_3 - rtb_Add; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* Gain: '/Gain4' incorporates: * Sum: '/Add1' */ rtDW->Gain4[1] = (int16_T)((18919 * tmp) >> 18); /* Sum: '/Add1' incorporates: * Sum: '/Sum2' */ tmp = (int16_T)tmp_2 - rtb_Add; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* Gain: '/Gain4' incorporates: * Sum: '/Add1' */ rtDW->Gain4[2] = (int16_T)((18919 * tmp) >> 18); /* Update for UnitDelay: '/UnitDelay4' */ rtDW->UnitDelay4_DSTATE_er = rtDW->Merge; /* End of Outputs for SubSystem: '/F04_Field_Oriented_Control' */ } /* End of If: '/If1' */ /* Switch: '/Switch2' incorporates: * Constant: '/z_ctrlTypSel1' * Constant: '/CTRL_COMM1' * DataTypeConversion: '/Data Type Conversion10' * DataTypeConversion: '/Data Type Conversion8' * RelationalOperator: '/Relational Operator6' */ if (rtP->z_ctrlTypSel == 0) { rtb_Merge = (int16_T)(rtb_Merge >> 4); } else { rtb_Merge = (int16_T)(rtDW->Merge >> 4); } /* End of Switch: '/Switch2' */ /* Switch: '/Switch1' incorporates: * Constant: '/vec_hallToPos' * LookupNDDirect: '/z_commutMap_M1' * Product: '/Divide2' * Selector: '/Selector' * * About '/z_commutMap_M1': * 2-dimensional Direct Look-Up returning a Column */ if (rtb_LogicalOperator) { rtb_Saturation2 = rtDW->Gain4[0]; rtb_Switch2_d = rtDW->Gain4[1]; rtb_Saturation = rtDW->Gain4[2]; } else { if (rtConstP.vec_hallToPos_Value[rtb_Sum] > 5) { /* LookupNDDirect: '/z_commutMap_M1' * * About '/z_commutMap_M1': * 2-dimensional Direct Look-Up returning a Column */ rtb_Sum2_h = 5; } else if (rtConstP.vec_hallToPos_Value[rtb_Sum] < 0) { /* LookupNDDirect: '/z_commutMap_M1' * * About '/z_commutMap_M1': * 2-dimensional Direct Look-Up returning a Column */ rtb_Sum2_h = 0; } else { /* LookupNDDirect: '/z_commutMap_M1' incorporates: * Constant: '/vec_hallToPos' * Selector: '/Selector' * * About '/z_commutMap_M1': * 2-dimensional Direct Look-Up returning a Column */ rtb_Sum2_h = rtConstP.vec_hallToPos_Value[rtb_Sum]; } /* LookupNDDirect: '/z_commutMap_M1' incorporates: * Constant: '/vec_hallToPos' * Selector: '/Selector' * * About '/z_commutMap_M1': * 2-dimensional Direct Look-Up returning a Column */ rtb_Sum2 = rtb_Sum2_h * 3; rtb_Saturation2 = (int16_T)(rtb_Merge * rtConstP.z_commutMap_M1_table[rtb_Sum2]); rtb_Switch2_d = (int16_T)(rtConstP.z_commutMap_M1_table[1 + rtb_Sum2] * rtb_Merge); rtb_Saturation = (int16_T)(rtConstP.z_commutMap_M1_table[2 + rtb_Sum2] * rtb_Merge); } /* End of Switch: '/Switch1' */ /* Update for UnitDelay: '/UnitDelay1' */ rtDW->UnitDelay1_DSTATE = rtb_Sum_l; /* Update for UnitDelay: '/UnitDelay3' incorporates: * Inport: '/b_hallA ' */ rtDW->UnitDelay3_DSTATE_fy = rtU->b_hallA; /* Update for UnitDelay: '/UnitDelay1' incorporates: * Inport: '/b_hallB' */ rtDW->UnitDelay1_DSTATE_m = rtU->b_hallB; /* Update for UnitDelay: '/UnitDelay2' incorporates: * Inport: '/b_hallC' */ rtDW->UnitDelay2_DSTATE_f = rtU->b_hallC; /* Update for UnitDelay: '/UnitDelay3' */ rtDW->UnitDelay3_DSTATE = rtb_Switch1_a; /* Update for UnitDelay: '/UnitDelay4' */ rtDW->UnitDelay4_DSTATE_e = rtb_Abs5; /* Update for UnitDelay: '/UnitDelay4' */ rtDW->UnitDelay4_DSTATE = rtb_Merge; /* End of Outputs for SubSystem: '/BLDC_controller' */ /* Outport: '/DC_phaA' */ rtY->DC_phaA = rtb_Saturation2; /* Outport: '/DC_phaB' */ rtY->DC_phaB = rtb_Switch2_d; /* Outport: '/DC_phaC' */ rtY->DC_phaC = rtb_Saturation; /* Outputs for Atomic SubSystem: '/BLDC_controller' */ /* Outport: '/n_mot' incorporates: * DataTypeConversion: '/Data Type Conversion1' */ rtY->n_mot = (int16_T)(rtb_Switch2_fv >> 4); /* Outport: '/a_elecAngle' incorporates: * DataTypeConversion: '/Data Type Conversion7' */ rtY->a_elecAngle = (int16_T)((uint32_T)rtb_Divide2_h >> 6); /* Outport: '/r_devSignal1' incorporates: * DataTypeConversion: '/Data Type Conversion4' */ rtY->r_devSignal1 = (int16_T)(rtDW->Sum1[0] >> 4); /* Outport: '/r_devSignal2' incorporates: * DataTypeConversion: '/Data Type Conversion5' */ rtY->r_devSignal2 = (int16_T)(rtDW->Sum1[1] >> 4); /* End of Outputs for SubSystem: '/BLDC_controller' */ } /* Model initialize function */ void BLDC_controller_initialize(RT_MODEL *const rtM) { P *rtP = ((P *) rtM->defaultParam); DW *rtDW = ((DW *) rtM->dwork); /* Start for Atomic SubSystem: '/BLDC_controller' */ /* Start for If: '/If2' */ rtDW->If2_ActiveSubsystem = -1; /* Start for If: '/If1' */ rtDW->If1_ActiveSubsystem = -1; /* Start for IfAction SubSystem: '/F04_Field_Oriented_Control' */ /* Start for If: '/If1' */ rtDW->If1_ActiveSubsystem_h = -1; /* Start for If: '/If1' */ rtDW->If1_ActiveSubsystem_f = -1; /* Start for If: '/If2' */ rtDW->If2_ActiveSubsystem_c = -1; /* Start for SwitchCase: '/Switch Case' */ rtDW->SwitchCase_ActiveSubsystem = -1; /* End of Start for SubSystem: '/F04_Field_Oriented_Control' */ /* End of Start for SubSystem: '/BLDC_controller' */ /* SystemInitialize for Atomic SubSystem: '/BLDC_controller' */ /* InitializeConditions for UnitDelay: '/UnitDelay3' */ rtDW->UnitDelay3_DSTATE = rtP->z_maxCntRst; /* SystemInitialize for IfAction SubSystem: '/Raw_Motor_Speed_Estimation' */ /* SystemInitialize for Outport: '/z_counter' */ rtDW->z_counterRawPrev = rtP->z_maxCntRst; /* End of SystemInitialize for SubSystem: '/Raw_Motor_Speed_Estimation' */ /* SystemInitialize for Atomic SubSystem: '/Counter' */ Counter_Init(&rtDW->Counter_e, rtP->z_maxCntRst); /* End of SystemInitialize for SubSystem: '/Counter' */ /* SystemInitialize for IfAction SubSystem: '/F02_Diagnostics' */ /* SystemInitialize for Atomic SubSystem: '/Debounce_Filter' */ Debounce_Filter_Init(&rtDW->Debounce_Filter_f); /* End of SystemInitialize for SubSystem: '/Debounce_Filter' */ /* End of SystemInitialize for SubSystem: '/F02_Diagnostics' */ /* SystemInitialize for IfAction SubSystem: '/F04_Field_Oriented_Control' */ /* SystemInitialize for IfAction SubSystem: '/Open_Mode' */ /* SystemInitialize for Atomic SubSystem: '/rising_edge_init' */ /* InitializeConditions for UnitDelay: '/UnitDelay' */ rtDW->UnitDelay_DSTATE_g = true; /* End of SystemInitialize for SubSystem: '/rising_edge_init' */ /* End of SystemInitialize for SubSystem: '/Open_Mode' */ /* End of SystemInitialize for SubSystem: '/F04_Field_Oriented_Control' */ /* End of SystemInitialize for SubSystem: '/BLDC_controller' */ } /* * File trailer for generated code. * * [EOF] */