/* * File: PMSM_Controller.c * * Code generated for Simulink model 'PMSM_Controller'. * * Model version : 1.1277 * Simulink Coder version : 9.4 (R2020b) 29-Jul-2020 * C/C++ source code generated on : Wed Apr 13 16:49:14 2022 * * Target selection: ert.tlc * Embedded hardware selection: ARM Compatible->ARM Cortex-M * Code generation objectives: * 1. Execution efficiency * 2. RAM efficiency * Validation result: Not run */ #include "PMSM_Controller.h" /* Named constants for Chart: '/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 OPEN_MODE ((uint8_T)0U) #define SPD_MODE ((uint8_T)1U) #define TRQ_MODE ((uint8_T)2U) #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 /* Skipping ulong_long/long_long check: insufficient preprocessor integer range. */ extern int32_T rt_sqrt_Us32En6_Ys32En_dnD5ZXjs(int32_T u); extern int16_T rt_sqrt_Us16En12_Ys16E_cQn1iwAF(int16_T u); static uint16_T plook_u16s16_evencka(int16_T u, int16_T bp0, uint16_T bpSpace, uint32_T maxIndex); static int32_T div_nde_s32_floor(int32_T numerator, int32_T denominator); static void Counter_Init(DW_Counter *localDW, uint16_T rtp_z_cntInit); static uint16_T Counter(uint16_T rtu_inc, uint16_T rtu_max, boolean_T rtu_rst, DW_Counter *localDW); static boolean_T either_edge(boolean_T rtu_u, DW_either_edge *localDW); static void Debounce_Filter_Init(DW_Debounce_Filter *localDW); static void Debounce_Filter(boolean_T rtu_u, uint16_T rtu_tAcv, uint16_T rtu_tDeacv, boolean_T *rty_y, DW_Debounce_Filter *localDW); static void Low_Pass_Filter(const int16_T rtu_u[2], uint16_T rtu_coef, int16_T rty_y[2], DW_Low_Pass_Filter *localDW); static void PI_backCalc_fixdt_Init(DW_PI_backCalc_fixdt *localDW); static void PI_backCalc_fixdt(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T rtu_Kb, int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_ext_limProt, uint8_T rtu_reset, int16_T *rty_pi_out, const ConstB_PI_backCalc_fixdt *localC, DW_PI_backCalc_fixdt *localDW, ZCE_PI_backCalc_fixdt *localZCE); static void pi_speed_Init(DW_pi_speed *localDW); static int16_T pi_speed(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T rtu_Kb, int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_ext_limProt, uint8_T rtu_reset, const ConstB_pi_speed *localC, DW_pi_speed *localDW, ZCE_pi_speed *localZCE); static uint16_T plook_u16s16_evencka(int16_T u, int16_T bp0, uint16_T bpSpace, uint32_T maxIndex) { uint16_T bpIndex; /* 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 { bpIndex = (uint16_T)((uint32_T)(uint16_T)(u - bp0) / bpSpace); if (bpIndex < maxIndex) { } else { bpIndex = (uint16_T)maxIndex; } } return bpIndex; } static int32_T div_nde_s32_floor(int32_T numerator, int32_T denominator) { return (((numerator < 0) != (denominator < 0)) && (numerator % denominator != 0) ? -1 : 0) + numerator / denominator; } /* * System initialize for atomic system: * '/Counter' * '/Counter' */ static void Counter_Init(DW_Counter *localDW, uint16_T rtp_z_cntInit) { /* InitializeConditions for UnitDelay: '/UnitDelay' */ localDW->UnitDelay_DSTATE = rtp_z_cntInit; } /* * Output and update for atomic system: * '/Counter' * '/Counter' */ static uint16_T Counter(uint16_T rtu_inc, uint16_T rtu_max, boolean_T rtu_rst, DW_Counter *localDW) { uint16_T rty_cnt_0; uint16_T rtu_rst_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; } /* * Output and update for atomic system: * '/either_edge' * '/either_edge' */ static 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' */ static void Debounce_Filter_Init(DW_Debounce_Filter *localDW) { /* SystemInitialize for IfAction SubSystem: '/Qualification' */ /* SystemInitialize for Atomic SubSystem: '/Counter' */ Counter_Init(&localDW->Counter_f, 0); /* End of SystemInitialize for SubSystem: '/Counter' */ /* End of SystemInitialize for SubSystem: '/Qualification' */ /* SystemInitialize for IfAction SubSystem: '/Dequalification' */ /* SystemInitialize for Atomic SubSystem: '/Counter' */ Counter_Init(&localDW->Counter_d, 0); /* End of SystemInitialize for SubSystem: '/Counter' */ /* End of SystemInitialize for SubSystem: '/Dequalification' */ } /* Output and update for atomic system: '/Debounce_Filter' */ static 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_jb; boolean_T rtb_RelationalOperator_e; /* Outputs for Atomic SubSystem: '/either_edge' */ rtb_RelationalOperator_e = either_edge(rtu_u, &localDW->either_edge_j); /* 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_jb = Counter(1, rtu_tAcv, rtb_RelationalOperator_e, &localDW->Counter_f); /* End of Outputs for SubSystem: '/Counter' */ /* Switch: '/Switch2' incorporates: * Constant: '/Constant6' * RelationalOperator: '/Relational Operator2' */ *rty_y = ((rtb_Sum1_jb > 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_jb = Counter(1, rtu_tDeacv, rtb_RelationalOperator_e, &localDW->Counter_d); /* End of Outputs for SubSystem: '/Counter' */ /* Switch: '/Switch2' incorporates: * Constant: '/Constant6' * RelationalOperator: '/Relational Operator2' */ *rty_y = ((rtb_Sum1_jb <= 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; } /* Output and update for atomic system: '/Low_Pass_Filter' */ static void Low_Pass_Filter(const int16_T rtu_u[2], uint16_T rtu_coef, int16_T rty_y[2], DW_Low_Pass_Filter *localDW) { int32_T tmp; /* Sum: '/Sum2' incorporates: * UnitDelay: '/UnitDelay1' */ tmp = rtu_u[0] - localDW->UnitDelay1_DSTATE[0]; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* Product: '/Divide3' incorporates: * Sum: '/Sum2' */ rty_y[0] = (int16_T)((rtu_coef * tmp) >> 16); /* Sum: '/Sum3' incorporates: * UnitDelay: '/UnitDelay1' */ rty_y[0] += localDW->UnitDelay1_DSTATE[0]; /* Update for UnitDelay: '/UnitDelay1' incorporates: * Sum: '/Sum3' */ localDW->UnitDelay1_DSTATE[0] = rty_y[0]; /* Sum: '/Sum2' incorporates: * UnitDelay: '/UnitDelay1' */ tmp = rtu_u[1] - localDW->UnitDelay1_DSTATE[1]; if (tmp > 32767) { tmp = 32767; } else { if (tmp < -32768) { tmp = -32768; } } /* Product: '/Divide3' incorporates: * Sum: '/Sum2' */ rty_y[1] = (int16_T)((rtu_coef * tmp) >> 16); /* Sum: '/Sum3' incorporates: * UnitDelay: '/UnitDelay1' */ rty_y[1] += localDW->UnitDelay1_DSTATE[1]; /* Update for UnitDelay: '/UnitDelay1' incorporates: * Sum: '/Sum3' */ localDW->UnitDelay1_DSTATE[1] = rty_y[1]; } /* * System initialize for atomic system: * '/PI_iq' * '/PI_id' */ static void PI_backCalc_fixdt_Init(DW_PI_backCalc_fixdt *localDW) { /* InitializeConditions for Delay: '/Resettable Delay' */ localDW->icLoad = 1U; } /* * Output and update for atomic system: * '/PI_iq' * '/PI_id' */ static void PI_backCalc_fixdt(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T rtu_Kb, int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_ext_limProt, uint8_T rtu_reset, int16_T *rty_pi_out, const ConstB_PI_backCalc_fixdt *localC, DW_PI_backCalc_fixdt *localDW, ZCE_PI_backCalc_fixdt *localZCE) { int64_T tmp; int32_T rtb_Divide4_h; int32_T rtb_Sum1_ae; /* Product: '/Divide4' */ rtb_Divide4_h = (rtu_err * rtu_P) >> 6; /* Delay: '/Resettable Delay' incorporates: * DataTypeConversion: '/Data Type Conversion2' */ if ((rtu_reset > 0) && (localZCE->ResettableDelay_Reset_ZCE_p != POS_ZCSIG)) { localDW->icLoad = 1U; } localZCE->ResettableDelay_Reset_ZCE_p = (ZCSigState)(rtu_reset > 0); if (localDW->icLoad != 0) { localDW->ResettableDelay_DSTATE = localC->DataTypeConversion2; } /* Product: '/Divide1' incorporates: * Product: '/Divide4' */ tmp = ((int64_T)rtb_Divide4_h * rtu_I) >> 10; if (tmp > 2147483647LL) { tmp = 2147483647LL; } else { if (tmp < -2147483648LL) { tmp = -2147483648LL; } } /* Sum: '/Sum2' incorporates: * Product: '/Divide1' * UnitDelay: '/UnitDelay' */ tmp = (((int64_T)rtu_ext_limProt << 3) + (int32_T)tmp) + localDW->UnitDelay_DSTATE; if (tmp > 2147483647LL) { tmp = 2147483647LL; } else { if (tmp < -2147483648LL) { tmp = -2147483648LL; } } /* Sum: '/Sum1' incorporates: * Delay: '/Resettable Delay' * Sum: '/Sum2' */ rtb_Sum1_ae = ((int32_T)tmp >> 2) + localDW->ResettableDelay_DSTATE; /* Sum: '/Sum6' incorporates: * DataTypeConversion: '/Data Type Conversion1' * Product: '/Divide4' * Sum: '/Sum1' */ tmp = ((int64_T)(rtb_Sum1_ae >> 2) << 4) + rtb_Divide4_h; if (tmp > 2147483647LL) { tmp = 2147483647LL; } else { if (tmp < -2147483648LL) { tmp = -2147483648LL; } } /* Switch: '/Switch2' incorporates: * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Sum: '/Sum6' * Switch: '/Switch' */ if ((int32_T)tmp > (rtu_satMax << 4)) { *rty_pi_out = rtu_satMax; } else if ((int32_T)tmp < (rtu_satMin << 4)) { /* Switch: '/Switch' */ *rty_pi_out = rtu_satMin; } else { *rty_pi_out = (int16_T)((int32_T)tmp >> 4); } /* End of Switch: '/Switch2' */ /* Update for UnitDelay: '/UnitDelay' incorporates: * Product: '/Divide2' * Sum: '/Sum3' * Sum: '/Sum6' */ localDW->UnitDelay_DSTATE = (int32_T)(((int64_T)((*rty_pi_out << 4) - (int32_T) tmp) * rtu_Kb) >> 10); /* Update for Delay: '/Resettable Delay' incorporates: * Sum: '/Sum1' */ localDW->icLoad = 0U; localDW->ResettableDelay_DSTATE = rtb_Sum1_ae; } /* System initialize for atomic system: '/pi_speed' */ static void pi_speed_Init(DW_pi_speed *localDW) { /* InitializeConditions for Delay: '/Resettable Delay' */ localDW->icLoad = 1U; } /* Output and update for atomic system: '/pi_speed' */ static int16_T pi_speed(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T rtu_Kb, int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_ext_limProt, uint8_T rtu_reset, const ConstB_pi_speed *localC, DW_pi_speed *localDW, ZCE_pi_speed *localZCE) { int16_T rty_pi_out_0; int64_T tmp; int32_T rtb_Divide4_g; int32_T rtb_Sum1_c; /* Product: '/Divide4' */ rtb_Divide4_g = (rtu_err * rtu_P) >> 2; /* Delay: '/Resettable Delay' incorporates: * DataTypeConversion: '/Data Type Conversion2' */ if ((rtu_reset > 0) && (localZCE->ResettableDelay_Reset_ZCE_a != POS_ZCSIG)) { localDW->icLoad = 1U; } localZCE->ResettableDelay_Reset_ZCE_a = (ZCSigState)(rtu_reset > 0); if (localDW->icLoad != 0) { localDW->ResettableDelay_DSTATE = localC->DataTypeConversion2; } /* Product: '/Divide1' incorporates: * Product: '/Divide4' */ tmp = ((int64_T)rtb_Divide4_g * rtu_I) >> 10; if (tmp > 2147483647LL) { tmp = 2147483647LL; } else { if (tmp < -2147483648LL) { tmp = -2147483648LL; } } /* Sum: '/Sum2' incorporates: * Product: '/Divide1' * UnitDelay: '/UnitDelay' */ tmp = (((int64_T)(int32_T)tmp + rtu_ext_limProt) + ((int64_T) localDW->UnitDelay_DSTATE << 2)) >> 2; if (tmp > 2147483647LL) { tmp = 2147483647LL; } else { if (tmp < -2147483648LL) { tmp = -2147483648LL; } } /* Sum: '/Sum1' incorporates: * Delay: '/Resettable Delay' * Sum: '/Sum2' */ rtb_Sum1_c = (int32_T)tmp + localDW->ResettableDelay_DSTATE; /* Sum: '/Sum6' incorporates: * DataTypeConversion: '/Data Type Conversion1' * Product: '/Divide4' * Sum: '/Sum1' */ tmp = ((int64_T)(rtb_Sum1_c >> 2) << 4) + rtb_Divide4_g; if (tmp > 2147483647LL) { tmp = 2147483647LL; } else { if (tmp < -2147483648LL) { tmp = -2147483648LL; } } /* Switch: '/Switch2' incorporates: * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Sum: '/Sum6' * Switch: '/Switch' */ if ((int32_T)tmp > (rtu_satMax << 4)) { rty_pi_out_0 = rtu_satMax; } else if ((int32_T)tmp < (rtu_satMin << 4)) { /* Switch: '/Switch' */ rty_pi_out_0 = rtu_satMin; } else { rty_pi_out_0 = (int16_T)((int32_T)tmp >> 4); } /* End of Switch: '/Switch2' */ /* Update for UnitDelay: '/UnitDelay' incorporates: * Product: '/Divide2' * Sum: '/Sum3' * Sum: '/Sum6' */ localDW->UnitDelay_DSTATE = (int32_T)(((int64_T)((rty_pi_out_0 << 4) - (int32_T)tmp) * rtu_Kb) >> 12); /* Update for Delay: '/Resettable Delay' incorporates: * Sum: '/Sum1' */ localDW->icLoad = 0U; localDW->ResettableDelay_DSTATE = rtb_Sum1_c; return rty_pi_out_0; } int32_T rt_sqrt_Us32En6_Ys32En_dnD5ZXjs(int32_T u) { int64_T tmp03_u; int32_T iBit; int32_T shiftMask; int32_T tmp01_y; int32_T y; /* Fixed-Point Sqrt Computation by the bisection method. */ if (u > 0) { y = 0; shiftMask = 1073741824; tmp03_u = (int64_T)u << 6; for (iBit = 0; iBit < 31; iBit++) { tmp01_y = y | shiftMask; if ((int64_T)tmp01_y * tmp01_y <= tmp03_u) { y = tmp01_y; } shiftMask = (int32_T)((uint32_T)shiftMask >> 1U); } } else { y = 0; } return y; } int16_T rt_sqrt_Us16En12_Ys16E_cQn1iwAF(int16_T u) { int32_T iBit; int32_T tmp03_u; int16_T shiftMask; int16_T tmp01_y; int16_T y; /* Fixed-Point Sqrt Computation by the bisection method. */ if (u > 0) { y = 0; shiftMask = 16384; tmp03_u = u << 12; for (iBit = 0; iBit < 15; iBit++) { tmp01_y = (int16_T)(y | shiftMask); if (tmp01_y * tmp01_y <= tmp03_u) { y = tmp01_y; } shiftMask = (int16_T)((uint32_T)shiftMask >> 1U); } } else { y = 0; } return y; } /* Model step function */ void PMSM_Controller_step(RT_MODEL *const rtM) { DW *rtDW = rtM->dwork; PrevZCX *rtPrevZCX = rtM->prevZCSigState; ExtU *rtU = (ExtU *) rtM->inputs; ExtY *rtY = (ExtY *) rtM->outputs; int64_T tmp; int32_T rtb_Gain; int32_T rtb_Saturation; int32_T rtb_Sum1; int32_T tmp_0; int32_T tmp_2; uint32_T qY; uint32_T tmp_1; int16_T rtb_DataTypeConversion_k1[2]; int16_T rtb_Switch_m[2]; int16_T rtb_MultiportSwitch_idx_0; int16_T rtb_MultiportSwitch_idx_1; int16_T rtb_Sign; int16_T rtb_Sum; int16_T rtb_Switch2_c; int16_T rtb_Switch3_c; int16_T rtb_Switch_dr; int16_T rtb_Switch_oi; int16_T rtb_r_cos_M1; uint16_T rtb_LogicalOperator3; uint16_T rtb_Switch2_j; int8_T UnitDelay3; int8_T rtb_Sum2; int8_T rtb_Sum2_tmp; uint8_T rtb_Add_cr; uint8_T rtb_DataTypeConversion1_c; uint8_T rtb_DataTypeConversion_i; uint8_T rtb_Switch2_fu; uint8_T rtb_UnitDelay_bc; uint8_T rtb_z_ctrlMod; boolean_T rtb_Equal_k; boolean_T rtb_LogicalOperator2; boolean_T rtb_LogicalOperator4; boolean_T rtb_LogicalOperator_p; boolean_T rtb_RelationalOperator4_f; boolean_T rtb_n_commDeacv; /* Outputs for Atomic SubSystem: '/PMSM_Controller' */ /* Sum: '/Sum3' incorporates: * UnitDelay: '/UnitDelay1' */ qY = rtDW->UnitDelay1_DSTATE + /*MW:OvSatOk*/ 1U; if (rtDW->UnitDelay1_DSTATE + 1U < 1U) { qY = MAX_uint32_T; } /* RelationalOperator: '/Equal' incorporates: * Constant: '/Constant1' * Math: '/Rem' * Sum: '/Sum3' */ rtb_Equal_k = (qY % 20U == 0U); /* Logic: '/Edge_Detect' incorporates: * Delay: '/Delay' * Delay: '/Delay1' * Delay: '/Delay2' * Inport: '/hall_a' * Inport: '/hall_b' * Inport: '/hall_c' */ rtb_LogicalOperator_p = (boolean_T)((rtU->hall_a != 0) ^ (rtDW->Delay_DSTATE != 0) ^ (rtU->hall_b != 0) ^ (rtDW->Delay1_DSTATE != 0) ^ (rtU->hall_c != 0)) ^ (rtDW->Delay2_DSTATE != 0); /* Sum: '/Add' incorporates: * Gain: '/Gain' * Gain: '/Gain1' * Inport: '/hall_a' * Inport: '/hall_b' * Inport: '/hall_c' */ rtb_Add_cr = (uint8_T)((uint32_T)(uint8_T)((uint32_T)(uint8_T)(rtU->hall_c << 2) + (uint8_T)(rtU->hall_b << 1)) + rtU->hall_a); /* If: '/If2' incorporates: * If: '/If2' * Inport: '/z_counterRawPrev' * UnitDelay: '/UnitDelay3' */ if (rtb_LogicalOperator_p) { /* Outputs for IfAction SubSystem: '/Direction_Detection' incorporates: * ActionPort: '/Action Port' */ /* UnitDelay: '/UnitDelay3' */ UnitDelay3 = rtDW->Switch2_i; /* End of Outputs for SubSystem: '/Direction_Detection' */ /* Selector: '/Selector' incorporates: * Constant: '/vec_hallToPos' */ rtb_Sum2_tmp = rtConstP.vec_hallToPos_Value[rtb_Add_cr]; /* Outputs for IfAction SubSystem: '/Direction_Detection' incorporates: * ActionPort: '/Action Port' */ /* Sum: '/Sum2' incorporates: * Constant: '/vec_hallToPos' * Selector: '/Selector' * UnitDelay: '/UnitDelay2' */ rtb_Sum2 = (int8_T)(rtb_Sum2_tmp - rtDW->UnitDelay2_DSTATE_j); /* Switch: '/Switch2' incorporates: * Constant: '/Constant20' * Constant: '/Constant8' * Logic: '/Logical Operator3' * RelationalOperator: '/Relational Operator1' * RelationalOperator: '/Relational Operator6' */ if ((rtb_Sum2 == 1) || (rtb_Sum2 == -5)) { /* Switch: '/Switch2' incorporates: * Constant: '/Constant24' */ rtDW->Switch2_i = 1; } else { /* Switch: '/Switch2' incorporates: * Constant: '/Constant23' */ rtDW->Switch2_i = -1; } /* End of Switch: '/Switch2' */ /* Update for UnitDelay: '/UnitDelay2' */ rtDW->UnitDelay2_DSTATE_j = rtb_Sum2_tmp; /* End of Outputs for SubSystem: '/Direction_Detection' */ /* Outputs for IfAction SubSystem: '/Raw_Motor_Speed_Estimation' incorporates: * ActionPort: '/Action Port' */ /* RelationalOperator: '/Relational Operator4' */ rtb_RelationalOperator4_f = (rtDW->Switch2_i != UnitDelay3); rtDW->z_counterRawPrev = rtDW->UnitDelay3_DSTATE; /* Switch: '/Switch3' incorporates: * Constant: '/Constant4' * Inport: '/z_counterRawPrev' * Logic: '/Logical Operator1' * Switch: '/Switch2' * UnitDelay: '/UnitDelay3' * UnitDelay: '/UnitDelay1' */ if (rtb_RelationalOperator4_f && rtDW->UnitDelay1_DSTATE_i) { rtb_Switch3_c = 0; } else { if (rtb_RelationalOperator4_f) { /* Switch: '/Switch2' incorporates: * UnitDelay: '/UnitDelay4' */ rtb_Switch2_j = rtDW->UnitDelay4_DSTATE; } else { /* Product: '/Divide13' incorporates: * Sum: '/Sum13' * Switch: '/Switch2' * UnitDelay: '/UnitDelay2' * UnitDelay: '/UnitDelay3' * UnitDelay: '/UnitDelay5' */ tmp_1 = 8000000U / (((rtDW->UnitDelay2_DSTATE + rtDW->UnitDelay3_DSTATE_l) + rtDW->UnitDelay5_DSTATE) + rtDW->z_counterRawPrev); if (tmp_1 > 65535U) { tmp_1 = 65535U; } /* Switch: '/Switch2' incorporates: * Product: '/Divide13' */ rtb_Switch2_j = (uint16_T)tmp_1; } rtb_Switch3_c = (int16_T)rtb_Switch2_j; } /* End of Switch: '/Switch3' */ /* Product: '/Divide11' incorporates: * Switch: '/Switch3' */ rtDW->Divide11 = (int16_T)(rtb_Switch3_c * rtDW->Switch2_i); /* Update for UnitDelay: '/UnitDelay1' */ rtDW->UnitDelay1_DSTATE_i = rtb_RelationalOperator4_f; /* Update for UnitDelay: '/UnitDelay2' incorporates: * UnitDelay: '/UnitDelay3' */ rtDW->UnitDelay2_DSTATE = rtDW->UnitDelay3_DSTATE_l; /* Update for UnitDelay: '/UnitDelay3' incorporates: * UnitDelay: '/UnitDelay5' */ rtDW->UnitDelay3_DSTATE_l = rtDW->UnitDelay5_DSTATE; /* Update for UnitDelay: '/UnitDelay5' */ rtDW->UnitDelay5_DSTATE = rtDW->z_counterRawPrev; /* End of Outputs for SubSystem: '/Raw_Motor_Speed_Estimation' */ } /* End of If: '/If2' */ /* Switch: '/Switch3' incorporates: * Constant: '/Constant16' * Constant: '/Constant2' * Constant: '/vec_hallToPos' * RelationalOperator: '/Relational Operator7' * Selector: '/Selector' * Sum: '/Sum1' */ if (rtDW->Switch2_i == 1) { rtb_Sum2 = rtConstP.vec_hallToPos_Value[rtb_Add_cr]; } else { rtb_Sum2 = (int8_T)(rtConstP.vec_hallToPos_Value[rtb_Add_cr] + 1); } /* End of Switch: '/Switch3' */ /* MinMax: '/MinMax' incorporates: * Inport: '/hw_count' */ if (rtU->hw_count < rtDW->z_counterRawPrev) { tmp_1 = rtU->hw_count; } else { tmp_1 = rtDW->z_counterRawPrev; } /* End of MinMax: '/MinMax' */ /* Sum: '/Sum3' incorporates: * Product: '/Divide1' * Product: '/Divide3' */ rtb_Switch3_c = (int16_T)(((int16_T)((int16_T)(((uint64_T)tmp_1 << 14) / rtDW->z_counterRawPrev) * rtDW->Switch2_i) + (rtb_Sum2 << 14)) >> 2); /* MinMax: '/MinMax1' incorporates: * Constant: '/Constant1' * Sum: '/Sum3' * Switch: '/Switch2' */ if (rtb_Switch3_c <= 0) { rtb_Switch3_c = 0; } /* End of MinMax: '/MinMax1' */ /* Sum: '/Add2' incorporates: * Constant: '/Constant2' * Product: '/Divide2' */ rtb_Switch3_c = (int16_T)((((15 * rtb_Switch3_c) >> 4) + 3840) >> 2); /* If: '/If' incorporates: * Constant: '/Constant3' * DataTypeConversion: '/Data Type Conversion' * Inport: '/In1' * Merge: '/Merge' * Sum: '/Add' * Sum: '/Add2' */ if ((int16_T)(rtb_Switch3_c >> 4) >= 360) { /* Outputs for IfAction SubSystem: '/If Action Subsystem' incorporates: * ActionPort: '/Action Port' */ rtb_Switch3_c = (int16_T)(rtb_Switch3_c - 5760); /* End of Outputs for SubSystem: '/If Action Subsystem' */ } /* End of If: '/If' */ /* Switch: '/Switch2' incorporates: * Constant: '/Constant4' * Inport: '/hw_count' * Product: '/Divide11' * RelationalOperator: '/Relational Operator2' */ if (rtU->hw_count >= 400000U) { rtb_Switch2_c = 0; } else { rtb_Switch2_c = rtDW->Divide11; } /* End of Switch: '/Switch2' */ /* Abs: '/Abs5' incorporates: * Switch: '/Switch2' */ if (rtb_Switch2_c < 0) { rtb_Switch2_j = (uint16_T)-rtb_Switch2_c; } else { rtb_Switch2_j = (uint16_T)rtb_Switch2_c; } /* End of Abs: '/Abs5' */ /* If: '/If1' */ if (rtb_LogicalOperator_p) { /* Outputs for IfAction SubSystem: '/Subsystem' incorporates: * ActionPort: '/Action Port' */ /* Relay: '/n_commDeacv' incorporates: * Abs: '/Abs5' */ rtDW->n_commDeacv_Mode = ((rtb_Switch2_j >= 120) || ((rtb_Switch2_j > 60) && rtDW->n_commDeacv_Mode)); /* RelationalOperator: '/Compare' incorporates: * Constant: '/Constant' * Relay: '/n_commDeacv' * Sum: '/Sum13' * UnitDelay: '/UnitDelay2' * UnitDelay: '/UnitDelay3' * UnitDelay: '/UnitDelay5' */ rtDW->Compare = ((uint16_T)((uint32_T)(uint16_T)((uint32_T)(uint16_T) ((uint32_T)rtDW->UnitDelay2_DSTATE_f + rtDW->UnitDelay3_DSTATE_lh) + rtDW->UnitDelay5_DSTATE_f) + rtDW->n_commDeacv_Mode) >= 4); /* Update for UnitDelay: '/UnitDelay2' incorporates: * UnitDelay: '/UnitDelay3' */ rtDW->UnitDelay2_DSTATE_f = rtDW->UnitDelay3_DSTATE_lh; /* Update for UnitDelay: '/UnitDelay3' incorporates: * UnitDelay: '/UnitDelay5' */ rtDW->UnitDelay3_DSTATE_lh = rtDW->UnitDelay5_DSTATE_f; /* Update for UnitDelay: '/UnitDelay5' incorporates: * Logic: '/Logical Operator3' * Relay: '/n_commDeacv' */ rtDW->UnitDelay5_DSTATE_f = rtDW->n_commDeacv_Mode; /* End of Outputs for SubSystem: '/Subsystem' */ } /* End of If: '/If1' */ /* S-Function (sfix_bitop): '/Bitwise Operator1' incorporates: * Inport: '/foc_calibrate' * S-Function (sfix_bitop): '/Bitwise Operator2' */ tmp_2 = rtU->foc_calibrate & 1; /* Switch: '/Switch' incorporates: * Inport: '/open_theta' * Merge: '/Merge' * S-Function (sfix_bitop): '/Bitwise Operator1' */ if (tmp_2 > 0) { rtb_r_cos_M1 = (int16_T)(rtU->open_theta << 4); } else { rtb_r_cos_M1 = rtb_Switch3_c; } /* End of Switch: '/Switch' */ /* Sum: '/Sum' incorporates: * Inport: '/foc_calibrate' * Inport: '/open_theta' * Product: '/Divide' * S-Function (sfix_bitop): '/Bitwise Operator2' */ rtb_Sum = (int16_T)((int16_T)((int16_T)((rtU->foc_calibrate & 2) * rtU->open_theta) << 4) + rtb_r_cos_M1); /* Abs: '/Abs2' incorporates: * Switch: '/Switch2' */ if (rtb_Switch2_c < 0) { rtb_LogicalOperator3 = (uint16_T)((uint32_T)-rtb_Switch2_c >> 2); } else { rtb_LogicalOperator3 = (uint16_T)((uint32_T)rtb_Switch2_c >> 2); } /* End of Abs: '/Abs2' */ /* UnitDelay: '/UnitDelay' */ rtb_UnitDelay_bc = rtDW->UnitDelay_DSTATE_j; /* Outport: '/VqPrev' incorporates: * UnitDelay: '/UnitDelay2' */ rtY->VqPrev = rtDW->UnitDelay2_DSTATE_p; /* Switch: '/Switch3' incorporates: * Abs: '/Abs5' * Abs: '/Abs4' * Constant: '/CTRL_COMM4' * Inport: '/b_motEna' * Logic: '/Logical Operator1' * RelationalOperator: '/Relational Operator9' * RelationalOperator: '/Relational Operator7' * S-Function (sfix_bitop): '/Bitwise Operator1' * UnitDelay: '/UnitDelay2' */ if ((rtb_UnitDelay_bc & 4U) != 0U) { rtb_LogicalOperator_p = true; } else { if (rtDW->UnitDelay2_DSTATE_p < 0) { /* Abs: '/Abs4' incorporates: * UnitDelay: '/UnitDelay2' */ rtb_r_cos_M1 = (int16_T)-rtDW->UnitDelay2_DSTATE_p; } else { /* Abs: '/Abs4' incorporates: * UnitDelay: '/UnitDelay2' */ rtb_r_cos_M1 = rtDW->UnitDelay2_DSTATE_p; } rtb_LogicalOperator_p = (rtU->b_motEna && (rtb_Switch2_j < 12) && (rtb_r_cos_M1 > 960)); } /* 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_DataTypeConversion1_c = (uint8_T)(((uint32_T)((rtb_Add_cr == 7) << 1) + (rtb_Add_cr == 0)) + (rtb_LogicalOperator_p << 2)); /* Outputs for Atomic SubSystem: '/Debounce_Filter' */ /* RelationalOperator: '/Relational Operator2' incorporates: * Constant: '/CTRL_COMM2' * Constant: '/t_errDequal' * Constant: '/t_errQual' */ Debounce_Filter(rtb_DataTypeConversion1_c != 0, 1600, 12000, &rtb_RelationalOperator4_f, &rtDW->Debounce_Filter_i); /* End of Outputs for SubSystem: '/Debounce_Filter' */ /* Logic: '/Logical Operator12' incorporates: * Inport: '/b_motEna' * Logic: '/Logical Operator7' */ rtb_n_commDeacv = ((!rtb_RelationalOperator4_f) && rtU->b_motEna); /* Logic: '/Logical Operator4' incorporates: * Constant: '/constant8' * Inport: '/n_ctrlModReq' * Logic: '/Logical Operator11' * Logic: '/Logical Operator8' * RelationalOperator: '/Relational Operator10' * S-Function (sfix_bitop): '/Bitwise Operator2' */ rtb_LogicalOperator4 = (((uint16_T)tmp_2 != 0) || (!rtDW->Compare) || (!rtb_n_commDeacv) || (rtU->n_ctrlModReq == 0)); /* Relay: '/n_SpeedCtrl' */ rtDW->n_SpeedCtrl_Mode = ((rtb_LogicalOperator3 >= 300) || ((rtb_LogicalOperator3 > 200) && rtDW->n_SpeedCtrl_Mode)); rtb_LogicalOperator_p = rtDW->n_SpeedCtrl_Mode; /* Logic: '/Logical Operator10' incorporates: * Inport: '/b_cruiseEna' */ rtb_LogicalOperator_p = (rtb_LogicalOperator_p && rtU->b_cruiseEna); /* Logic: '/Logical Operator2' incorporates: * Constant: '/constant' * Inport: '/n_ctrlModReq' * Logic: '/Logical Operator5' * RelationalOperator: '/Relational Operator4' */ rtb_LogicalOperator2 = ((rtU->n_ctrlModReq == 2) && (!rtb_LogicalOperator_p)); /* Logic: '/Logical Operator1' incorporates: * Constant: '/constant1' * Inport: '/n_ctrlModReq' * RelationalOperator: '/Relational Operator1' */ rtb_LogicalOperator_p = ((rtU->n_ctrlModReq == 1) || rtb_LogicalOperator_p); /* Chart: '/Control_Mode_Manager' incorporates: * Logic: '/Logical Operator3' * Logic: '/Logical Operator6' * Logic: '/Logical Operator9' */ if (rtDW->is_active_c5_PMSM_Controller == 0U) { rtDW->is_active_c5_PMSM_Controller = 1U; rtDW->is_c5_PMSM_Controller = IN_OPEN; rtb_z_ctrlMod = OPEN_MODE; } else if (rtDW->is_c5_PMSM_Controller == 1) { if (rtb_LogicalOperator4) { rtDW->is_ACTIVE = IN_NO_ACTIVE_CHILD; rtDW->is_c5_PMSM_Controller = IN_OPEN; rtb_z_ctrlMod = OPEN_MODE; } else if (rtDW->is_ACTIVE == 1) { rtb_z_ctrlMod = SPD_MODE; if (!rtb_LogicalOperator_p) { if (rtb_LogicalOperator2) { rtDW->is_ACTIVE = IN_TORQUE_MODE; rtb_z_ctrlMod = TRQ_MODE; } else { rtDW->is_ACTIVE = IN_SPEED_MODE; } } } else { /* case IN_TORQUE_MODE: */ rtb_z_ctrlMod = TRQ_MODE; if (!rtb_LogicalOperator2) { rtDW->is_ACTIVE = IN_SPEED_MODE; rtb_z_ctrlMod = SPD_MODE; } } } else { /* case IN_OPEN: */ rtb_z_ctrlMod = OPEN_MODE; if ((!rtb_LogicalOperator4) && (rtb_LogicalOperator2 || rtb_LogicalOperator_p)) { rtDW->is_c5_PMSM_Controller = IN_ACTIVE; if (rtb_LogicalOperator2) { rtDW->is_ACTIVE = IN_TORQUE_MODE; rtb_z_ctrlMod = TRQ_MODE; } else { rtDW->is_ACTIVE = IN_SPEED_MODE; rtb_z_ctrlMod = SPD_MODE; } } } /* End of Chart: '/Control_Mode_Manager' */ /* Switch: '/Switch' incorporates: * Constant: '/Constant3' * Inport: '/input_target' */ if (rtU->input_target > 60) { /* Switch: '/Switch1' incorporates: * Constant: '/Constant1' * DataTypeConversion: '/Data Type Conversion' * Switch: '/Switch' */ if (rtb_n_commDeacv) { rtb_Switch_oi = rtU->input_target; } else { rtb_Switch_oi = 0; } /* End of Switch: '/Switch1' */ } else { rtb_Switch_oi = 0; } /* End of Switch: '/Switch' */ /* Switch: '/Switch3' incorporates: * Constant: '/Constant4' * DataTypeConversion: '/Data Type Conversion2' * Inport: '/vdq_open_target' */ if (rtb_n_commDeacv) { rtb_r_cos_M1 = rtU->vdq_open_target[1]; } else { rtb_r_cos_M1 = 0; } /* End of Switch: '/Switch3' */ /* If: '/If' incorporates: * DataTypeConversion: '/Data Type Conversion1' * Inport: '/vq_in' * S-Function (sfix_bitop): '/Bitwise Operator2' * Switch: '/Switch3' */ if ((uint16_T)tmp_2 == 1) { /* Switch: '/Switch2' incorporates: * Constant: '/Constant2' * DataTypeConversion: '/Data Type Conversion1' * Inport: '/vdq_open_target' * Inport: '/vd_in' */ if (rtb_n_commDeacv) { /* Outputs for IfAction SubSystem: '/If Action Subsystem' incorporates: * ActionPort: '/Action Port' */ rtDW->Merge[0] = rtU->vdq_open_target[0]; /* End of Outputs for SubSystem: '/If Action Subsystem' */ } else { /* Outputs for IfAction SubSystem: '/If Action Subsystem' incorporates: * ActionPort: '/Action Port' */ rtDW->Merge[0] = 0; /* End of Outputs for SubSystem: '/If Action Subsystem' */ } /* End of Switch: '/Switch2' */ /* Outputs for IfAction SubSystem: '/If Action Subsystem' incorporates: * ActionPort: '/Action Port' */ rtDW->Merge[1] = rtb_r_cos_M1; /* End of Outputs for SubSystem: '/If Action Subsystem' */ } else if ((rtb_z_ctrlMod == 0) && rtb_Equal_k) { /* Outputs for IfAction SubSystem: '/open_mode' incorporates: * ActionPort: '/Action Port' */ /* RelationalOperator: '/Equal1' incorporates: * Switch: '/Switch3' * UnitDelay: '/Unit Delay' */ rtb_LogicalOperator_p = (rtDW->UnitDelay_DSTATE_i != rtb_r_cos_M1); /* If: '/If' */ if (rtb_LogicalOperator_p) { /* Outputs for IfAction SubSystem: '/Subsystem' incorporates: * ActionPort: '/Action Port' */ /* Sum: '/Add' incorporates: * Signum: '/Sign' * Switch: '/Switch3' * UnitDelay: '/UnitDelay2' */ rtb_Sign = (int16_T)((rtb_r_cos_M1 - rtDW->UnitDelay2_DSTATE_p) >> 2); /* Signum: '/Sign' */ if (rtb_Sign < 0) { rtb_Sign = -1; } else { rtb_Sign = (int16_T)(rtb_Sign > 0); } /* End of Signum: '/Sign' */ /* Product: '/Divide' incorporates: * Constant: '/Constant5' */ rtDW->Divide = (int16_T)(rtb_Sign * 6); /* Switch: '/Switch' incorporates: * Switch: '/Switch1' */ if (rtb_Sign > 0) { /* Switch: '/Switch' incorporates: * Switch: '/Switch3' */ rtDW->Switch = rtb_r_cos_M1; /* Switch: '/Switch1' incorporates: * UnitDelay: '/UnitDelay2' */ rtDW->Switch1 = rtDW->UnitDelay2_DSTATE_p; } else { /* Switch: '/Switch' incorporates: * UnitDelay: '/UnitDelay2' */ rtDW->Switch = rtDW->UnitDelay2_DSTATE_p; /* Switch: '/Switch1' incorporates: * Switch: '/Switch3' */ rtDW->Switch1 = rtb_r_cos_M1; } /* End of Switch: '/Switch' */ /* End of Outputs for SubSystem: '/Subsystem' */ /* Switch: '/Switch1' incorporates: * UnitDelay: '/UnitDelay2' */ rtb_Sign = rtDW->UnitDelay2_DSTATE_p; } else { /* Switch: '/Switch1' incorporates: * UnitDelay: '/UnitDelay' */ rtb_Sign = rtDW->UnitDelay_DSTATE_d; } /* End of If: '/If' */ /* Sum: '/Add2' incorporates: * Product: '/Divide' */ tmp_2 = ((rtb_Sign << 1) + rtDW->Divide) >> 1; if (tmp_2 > 32767) { tmp_2 = 32767; } else { if (tmp_2 < -32768) { tmp_2 = -32768; } } /* Switch: '/Switch' incorporates: * Switch: '/Switch' */ if (rtb_Switch_oi > 0) { /* Switch: '/Switch2' incorporates: * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Sum: '/Add2' * Switch: '/Switch' * Switch: '/Switch' * Switch: '/Switch1' */ if ((int16_T)tmp_2 > rtDW->Switch) { /* Merge: '/Merge' incorporates: * Switch: '/Switch' */ rtDW->Merge[1] = rtDW->Switch; } else if ((int16_T)tmp_2 < rtDW->Switch1) { /* Merge: '/Merge' incorporates: * Switch: '/Switch' * Switch: '/Switch' * Switch: '/Switch1' */ rtDW->Merge[1] = rtDW->Switch1; } else { /* Merge: '/Merge' incorporates: * Switch: '/Switch' */ rtDW->Merge[1] = (int16_T)tmp_2; } /* End of Switch: '/Switch2' */ } else { /* Merge: '/Merge' incorporates: * Constant: '/Constant1' */ rtDW->Merge[1] = 0; } /* End of Switch: '/Switch' */ /* Merge: '/Merge' incorporates: * Constant: '/Constant3' * SignalConversion generated from: '/open_voltage' */ rtDW->Merge[0] = 0; /* Update for UnitDelay: '/Unit Delay' incorporates: * Switch: '/Switch3' */ rtDW->UnitDelay_DSTATE_i = rtb_r_cos_M1; /* Switch: '/Switch2' */ if (rtb_LogicalOperator_p) { /* Update for UnitDelay: '/UnitDelay' incorporates: * UnitDelay: '/UnitDelay2' */ rtDW->UnitDelay_DSTATE_d = rtDW->UnitDelay2_DSTATE_p; } else { /* Update for UnitDelay: '/UnitDelay' incorporates: * Sum: '/Add2' */ rtDW->UnitDelay_DSTATE_d = (int16_T)tmp_2; } /* End of Switch: '/Switch2' */ /* End of Outputs for SubSystem: '/open_mode' */ } else { if (rtb_z_ctrlMod == 2) { /* Outputs for IfAction SubSystem: '/torque_mode' incorporates: * ActionPort: '/Action Port' */ /* Switch: '/Switch' incorporates: * Constant: '/Constant' * Inport: '/foc_calibrate' * Inport: '/i_dc_limit' * Inport: '/speed_limit' * Logic: '/Logical Operator' * Product: '/Divide4' * S-Function (sfix_bitop): '/Bitwise Operator1' */ if ((rtU->foc_calibrate & 2U) == 0U) { rtb_LogicalOperator3 = (uint16_T)((rtU->i_dc_limit << 8) / rtU->speed_limit); } else { rtb_LogicalOperator3 = 4096U; } /* End of Switch: '/Switch' */ /* Product: '/Divide1' incorporates: * Switch: '/Switch' * Switch: '/Switch' */ tmp_2 = (rtb_Switch_oi * rtb_LogicalOperator3) >> 8; if (tmp_2 > 32767) { tmp_2 = 32767; } else { if (tmp_2 < -32768) { tmp_2 = -32768; } } /* Product: '/Divide1' */ rtDW->Divide1 = (int16_T)tmp_2; /* End of Outputs for SubSystem: '/torque_mode' */ } } /* End of If: '/If' */ /* Outputs for Atomic SubSystem: '/either_edge' */ rtb_LogicalOperator_p = either_edge(rtb_RelationalOperator4_f, &rtDW->either_edge_f); /* End of Outputs for SubSystem: '/either_edge' */ /* Switch: '/Switch1' */ if (rtb_LogicalOperator_p) { rtb_UnitDelay_bc = rtb_DataTypeConversion1_c; } /* End of Switch: '/Switch1' */ /* Gain: '/Multiply' incorporates: * Inport: '/adc_a' * Inport: '/adc_b' */ tmp_2 = (12351 * rtU->adc_a) >> 11; if (tmp_2 > 32767) { tmp_2 = 32767; } else { if (tmp_2 < -32768) { tmp_2 = -32768; } } tmp_0 = (12351 * rtU->adc_b) >> 11; if (tmp_0 > 32767) { tmp_0 = 32767; } else { if (tmp_0 < -32768) { tmp_0 = -32768; } } /* Sum: '/Add' incorporates: * Gain: '/Multiply' */ rtb_Sum1 = (int16_T)tmp_2 + (int16_T)tmp_0; if (rtb_Sum1 > 32767) { rtb_Sum1 = 32767; } else { if (rtb_Sum1 < -32768) { rtb_Sum1 = -32768; } } /* Sum: '/Add1' incorporates: * Sum: '/Add' */ rtb_Saturation = -rtb_Sum1; if (-rtb_Sum1 > 32767) { rtb_Saturation = 32767; } /* Sum: '/Add3' incorporates: * Gain: '/Multiply' * Sum: '/Add1' */ rtb_Sum1 = (int16_T)tmp_0 + (int16_T)rtb_Saturation; if (rtb_Sum1 > 32767) { rtb_Sum1 = 32767; } else { if (rtb_Sum1 < -32768) { rtb_Sum1 = -32768; } } /* Sum: '/Add' incorporates: * Gain: '/Multiply' * Sum: '/Add3' */ tmp_2 = (((int16_T)tmp_2 << 1) - rtb_Sum1) >> 1; if (tmp_2 > 32767) { tmp_2 = 32767; } else { if (tmp_2 < -32768) { tmp_2 = -32768; } } /* Gain: '/Gain1' incorporates: * Product: '/Divide1' * Sum: '/Add' */ rtb_Sign = (int16_T)((21845 * tmp_2) >> 15); /* Gain: '/Gain2' incorporates: * Gain: '/Multiply' * Sum: '/Add1' * Sum: '/Add2' */ tmp_2 = ((int16_T)(((int16_T)tmp_0 - (int16_T)rtb_Saturation) >> 1) * 18919) >> 14; if (tmp_2 > 32767) { tmp_2 = 32767; } else { if (tmp_2 < -32768) { tmp_2 = -32768; } } /* PreLookup: '/a_elecAngle_XA' incorporates: * Sum: '/Sum' */ rtb_LogicalOperator3 = plook_u16s16_evencka(rtb_Sum, 0, 4U, 1440U); /* Sum: '/Sum1' incorporates: * Gain: '/Gain2' * Interpolation_n-D: '/r_cos_M1' * Interpolation_n-D: '/r_sin_M1' * Product: '/Divide1' * Product: '/Divide2' * Product: '/Divide3' */ tmp_0 = (int16_T)((rtb_Sign * rtConstP.r_cos_M1_Table[rtb_LogicalOperator3]) >> 14) + (int16_T)(((int16_T)tmp_2 * rtConstP.r_sin_M1_Table[rtb_LogicalOperator3]) >> 14); if (tmp_0 > 32767) { tmp_0 = 32767; } else { if (tmp_0 < -32768) { tmp_0 = -32768; } } /* SignalConversion generated from: '/Low_Pass_Filter' incorporates: * Sum: '/Sum1' */ rtb_Switch_m[0] = (int16_T)tmp_0; /* Sum: '/Sum6' incorporates: * Gain: '/Gain2' * Interpolation_n-D: '/r_cos_M1' * Interpolation_n-D: '/r_sin_M1' * Product: '/Divide1' * Product: '/Divide4' */ tmp_2 = (int16_T)(((int16_T)tmp_2 * rtConstP.r_cos_M1_Table[rtb_LogicalOperator3]) >> 14) - (int16_T)((rtb_Sign * rtConstP.r_sin_M1_Table[rtb_LogicalOperator3]) >> 14); if (tmp_2 > 32767) { tmp_2 = 32767; } else { if (tmp_2 < -32768) { tmp_2 = -32768; } } /* SignalConversion generated from: '/Low_Pass_Filter' incorporates: * Sum: '/Sum6' */ rtb_Switch_m[1] = (int16_T)tmp_2; /* Outputs for Atomic SubSystem: '/Low_Pass_Filter' */ /* Constant: '/Constant' */ Low_Pass_Filter(rtb_Switch_m, 26214, rtb_DataTypeConversion_k1, &rtDW->Low_Pass_Filter_d); /* End of Outputs for SubSystem: '/Low_Pass_Filter' */ /* Switch: '/Switch2' */ rtb_Switch2_fu = (uint8_T)(rtb_z_ctrlMod != 0); /* DataTypeConversion: '/Data Type Conversion' incorporates: * Logic: '/Logical Operator' * RelationalOperator: '/Equal' * UnitDelay: '/Unit Delay' */ rtb_DataTypeConversion_i = (uint8_T)((rtb_Switch2_fu != 0) && (rtDW->UnitDelay_DSTATE_b != rtb_Switch2_fu)); /* Delay: '/Resettable Delay' incorporates: * DataTypeConversion: '/Data Type Conversion2' */ if ((rtb_Switch2_fu > 0) && (rtPrevZCX->ResettableDelay_Reset_ZCE != 1)) { rtDW->icLoad = 1U; } rtPrevZCX->ResettableDelay_Reset_ZCE = (ZCSigState)(rtb_Switch2_fu > 0); if (rtDW->icLoad != 0) { rtDW->ResettableDelay_DSTATE = 0; } /* Sum: '/Sum1' incorporates: * Constant: '/Constant3' * Delay: '/Resettable Delay' * Gain: '/Gain' * Gain: '/Gain1' * Sum: '/Sum' * Sum: '/Sum4' * UnitDelay: '/Unit Delay' * UnitDelay: '/Unit Delay' */ rtb_Sum1 = ((((int16_T)((15565 - (rtDW->UnitDelay_DSTATE_e << 2)) >> 2) * 6711) >> 31) + (int32_T)((1374389535LL * rtDW->UnitDelay_DSTATE) >> 37)) + rtDW->ResettableDelay_DSTATE; /* Saturate: '/Saturation' incorporates: * Sum: '/Sum1' */ if (rtb_Sum1 > 0) { rtb_Saturation = 0; } else if (rtb_Sum1 < -1920) { rtb_Saturation = -1920; } else { rtb_Saturation = rtb_Sum1; } /* End of Saturate: '/Saturation' */ /* DataTypeConversion: '/Data Type Conversion1' incorporates: * Logic: '/Logical Operator' */ rtb_DataTypeConversion1_c = (uint8_T)((rtb_Switch2_fu != 0) && rtb_Equal_k); /* If: '/If' incorporates: * Constant: '/Constant1' * Constant: '/Constant11' * Constant: '/Constant2' * Constant: '/Constant4' * Gain: '/Gain1' * Inport: '/i_dc_limit' * Sum: '/Add2' * Switch: '/Switch2' * Switch: '/Switch2' */ if ((rtb_DataTypeConversion1_c > 0) && (rtb_z_ctrlMod == 1)) { /* Outputs for IfAction SubSystem: '/speed_mode' incorporates: * ActionPort: '/Action Port' */ /* Switch: '/Switch2' incorporates: * Inport: '/speed_limit' * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Switch: '/Switch' * Switch: '/Switch' * Switch: '/Switch2' */ if (rtb_Switch_oi > rtU->speed_limit) { rtb_Switch_oi = rtU->speed_limit; } else { if (rtb_Switch_oi < 0) { /* Switch: '/Switch' incorporates: * Constant: '/Constant5' * Switch: '/Switch2' */ rtb_Switch_oi = 0; } } /* End of Switch: '/Switch2' */ /* Outputs for Atomic SubSystem: '/pi_speed' */ rtb_Switch_oi = pi_speed((int16_T)(rtb_Switch_oi - rtb_Switch2_c), 3174, 10, 20, rtU->i_dc_limit, (int16_T)-rtU->i_dc_limit, 0, rtb_Switch2_fu, &rtConstB.pi_speed_d, &rtDW->pi_speed_d, &rtPrevZCX->pi_speed_d); /* End of Outputs for SubSystem: '/pi_speed' */ /* Merge: '/Merge' incorporates: * Constant: '/Constant1' * Constant: '/Constant11' * Constant: '/Constant2' * Constant: '/Constant4' * Gain: '/Gain1' * Inport: '/i_dc_limit' * SignalConversion generated from: '/idq_target' * Sum: '/Add2' * Switch: '/Switch2' * Switch: '/Switch2' */ rtDW->Merge_f = rtb_Switch_oi; /* End of Outputs for SubSystem: '/speed_mode' */ } else { if ((rtb_DataTypeConversion1_c > 0) && (rtb_z_ctrlMod == 2)) { /* Outputs for IfAction SubSystem: '/torque_mode' incorporates: * ActionPort: '/Action Port' */ /* Product: '/Divide' incorporates: * Constant: '/Constant2' * Sum: '/Sum2' * Switch: '/Switch2' * Switch: '/Switch' */ tmp_2 = ((int16_T)(rtb_Switch_oi - rtb_Switch2_c) * 819) >> 6; if (tmp_2 > 32767) { tmp_2 = 32767; } else { if (tmp_2 < -32768) { tmp_2 = -32768; } } /* Product: '/Divide1' incorporates: * Sum: '/Sum3' * Switch: '/Switch2' * Switch: '/Switch' */ tmp_0 = ((int16_T)(rtb_Switch2_c - rtb_Switch_oi) * -51) >> 5; if (tmp_0 > 32767) { tmp_0 = 32767; } else { if (tmp_0 < -32768) { tmp_0 = -32768; } } rtb_Switch_oi = (int16_T)tmp_0; /* End of Product: '/Divide1' */ /* MinMax: '/Max' incorporates: * Product: '/Divide' * Product: '/Divide1' */ if ((int16_T)tmp_2 > rtb_Switch_oi) { rtb_r_cos_M1 = (int16_T)tmp_2; } else { rtb_r_cos_M1 = rtb_Switch_oi; } /* End of MinMax: '/Max' */ /* MinMax: '/Max3' incorporates: * Inport: '/i_dc_limit' * MinMax: '/Max' * Switch: '/Switch2' */ if (rtU->i_dc_limit < rtb_r_cos_M1) { rtb_r_cos_M1 = rtU->i_dc_limit; } /* End of MinMax: '/Max3' */ /* Switch: '/Switch2' incorporates: * Product: '/Divide1' * RelationalOperator: '/LowerRelop1' */ if (rtDW->Divide1 <= rtb_r_cos_M1) { /* MinMax: '/Max1' incorporates: * Product: '/Divide' * Product: '/Divide1' */ if ((int16_T)tmp_2 < rtb_Switch_oi) { rtb_Switch_oi = (int16_T)tmp_2; } /* End of MinMax: '/Max1' */ /* MinMax: '/Max2' incorporates: * Gain: '/Gain1' * Inport: '/i_dc_limit' * MinMax: '/Max1' */ if (rtb_Switch_oi <= (int16_T)-rtU->i_dc_limit) { rtb_Switch_oi = (int16_T)-rtU->i_dc_limit; } /* End of MinMax: '/Max2' */ /* Switch: '/Switch' incorporates: * MinMax: '/Max2' * RelationalOperator: '/UpperRelop' */ if (rtDW->Divide1 < rtb_Switch_oi) { rtb_r_cos_M1 = rtb_Switch_oi; } else { rtb_r_cos_M1 = rtDW->Divide1; } /* End of Switch: '/Switch' */ } /* End of Switch: '/Switch2' */ /* Merge: '/Merge' incorporates: * SignalConversion generated from: '/idq_target' * Switch: '/Switch2' */ rtDW->Merge_f = rtb_r_cos_M1; /* End of Outputs for SubSystem: '/torque_mode' */ } } /* End of If: '/If' */ /* Math: '/Math Function2' incorporates: * Saturate: '/Saturation' * Sum: '/Sum1' */ tmp = ((int64_T)rtb_Saturation * rtb_Saturation) >> 6; if (tmp > 2147483647LL) { tmp = 2147483647LL; } else { if (tmp < -2147483648LL) { tmp = -2147483648LL; } } /* Sqrt: '/Sqrt' incorporates: * Gain: '/Gain' * Math: '/Math Function1' * Math: '/Math Function2' * Sum: '/Sum2' * Switch: '/Switch' */ rtb_Gain = rt_sqrt_Us32En6_Ys32En_dnD5ZXjs(((rtDW->Merge_f * rtDW->Merge_f) >> 6) - (int32_T)tmp); /* If: '/If' incorporates: * Constant: '/Constant3' * Constant: '/Constant4' * Constant: '/Constant6' * Constant: '/Constant9' * Constant: '/Constant1' * Constant: '/Constant7' * Constant: '/Constant8' * Constant: '/Constant9' * Gain: '/Gain3' * Gain: '/Gain5' * If: '/If1' * Inport: '/vbus_voltage' * Sum: '/Add' * Sum: '/Add1' * Switch: '/Switch2' * Switch: '/Switch2' */ if (rtb_Switch2_fu == 1) { /* Outputs for IfAction SubSystem: '/iq_ctrl' incorporates: * ActionPort: '/Action Port' */ /* Switch: '/Switch2' incorporates: * DataTypeConversion: '/Data Type Conversion' * Gain: '/Gain1' * Inport: '/i_dc_limit' * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Switch: '/Switch' */ if ((int16_T)rtb_Gain > rtU->i_dc_limit) { rtb_Switch_oi = rtU->i_dc_limit; } else if ((int16_T)rtb_Gain < (int16_T)-rtU->i_dc_limit) { /* Switch: '/Switch' incorporates: * Gain: '/Gain1' * Switch: '/Switch2' */ rtb_Switch_oi = (int16_T)-rtU->i_dc_limit; } else { rtb_Switch_oi = (int16_T)rtb_Gain; } /* End of Switch: '/Switch2' */ /* Outputs for Atomic SubSystem: '/PI_iq' */ PI_backCalc_fixdt((int16_T)(rtb_Switch_oi - rtb_DataTypeConversion_k1[1]), 4096, 51, 1024, rtU->vbus_voltage, (int16_T) -rtU->vbus_voltage, 0, rtb_DataTypeConversion_i, &rtDW->Switch2_d, &rtConstB.PI_iq, &rtDW->PI_iq, &rtPrevZCX->PI_iq); /* End of Outputs for SubSystem: '/PI_iq' */ /* End of Outputs for SubSystem: '/iq_ctrl' */ /* Outputs for IfAction SubSystem: '/id_ctrl' incorporates: * ActionPort: '/Action Port' */ /* Switch: '/Switch2' incorporates: * Constant: '/Constant1' * Constant: '/Constant7' * Constant: '/Constant8' * Constant: '/Constant9' * DataTypeConversion: '/Data Type Conversion' * Gain: '/Gain4' * Gain: '/Gain5' * Inport: '/i_dc_limit' * Inport: '/vbus_voltage' * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Saturate: '/Saturation' * Sum: '/Add1' * Sum: '/Sum1' * Switch: '/Switch' * Switch: '/Switch2' */ if ((int16_T)rtb_Saturation > rtU->i_dc_limit) { rtb_Switch_oi = rtU->i_dc_limit; } else if ((int16_T)rtb_Saturation < (int16_T)-rtU->i_dc_limit) { /* Switch: '/Switch' incorporates: * Gain: '/Gain4' * Switch: '/Switch2' */ rtb_Switch_oi = (int16_T)-rtU->i_dc_limit; } else { rtb_Switch_oi = (int16_T)rtb_Saturation; } /* End of Switch: '/Switch2' */ /* Outputs for Atomic SubSystem: '/PI_id' */ PI_backCalc_fixdt((int16_T)(rtb_Switch_oi - rtb_DataTypeConversion_k1[0]), 4096, 51, 1024, rtU->vbus_voltage, (int16_T) -rtU->vbus_voltage, 0, rtb_DataTypeConversion_i, &rtDW->Switch2, &rtConstB.PI_id, &rtDW->PI_id, &rtPrevZCX->PI_id); /* End of Outputs for SubSystem: '/PI_id' */ /* End of Outputs for SubSystem: '/id_ctrl' */ } /* End of If: '/If' */ /* Switch: '/Switch' incorporates: * Merge: '/Merge' */ if (rtb_z_ctrlMod != 0) { rtb_Switch_m[0] = rtDW->Switch2; rtb_Switch_m[1] = rtDW->Switch2_d; } else { rtb_Switch_m[0] = rtDW->Merge[0]; rtb_Switch_m[1] = rtDW->Merge[1]; } /* End of Switch: '/Switch' */ /* Gain: '/Gain' incorporates: * Inport: '/vbus_voltage' * Product: '/Product' */ rtb_Switch_oi = (int16_T)((15565 * rtU->vbus_voltage) >> 14); /* Product: '/Divide' incorporates: * Math: '/Math Function' * Math: '/Math Function1' * Product: '/Product' * Sum: '/Sum of Elements' * Switch: '/Switch' */ tmp = ((int64_T)(((rtb_Switch_m[0] * rtb_Switch_m[0]) >> 6) + ((rtb_Switch_m[1] * rtb_Switch_m[1]) >> 6)) << 12) / ((rtb_Switch_oi * rtb_Switch_oi) >> 6); if (tmp > 32767LL) { tmp = 32767LL; } else { if (tmp < -32768LL) { tmp = -32768LL; } } /* Sqrt: '/Sqrt' incorporates: * Product: '/Divide' */ rtb_Switch_oi = rt_sqrt_Us16En12_Ys16E_cQn1iwAF((int16_T)tmp); /* Switch: '/Switch' incorporates: * RelationalOperator: '/Compare' * Sqrt: '/Sqrt' */ if (rtb_Switch_oi > 4096) { /* Switch: '/Switch' incorporates: * Product: '/Divide1' * Switch: '/Switch' */ rtb_Switch_m[0] = (int16_T)div_nde_s32_floor(rtb_Switch_m[0] << 12, rtb_Switch_oi); rtb_Switch_m[1] = (int16_T)div_nde_s32_floor(rtb_Switch_m[1] << 12, rtb_Switch_oi); } /* End of Switch: '/Switch' */ /* Sum: '/Sum1' incorporates: * Interpolation_n-D: '/r_cos_M1' * Interpolation_n-D: '/r_sin_M1' * Product: '/Divide2' * Product: '/Divide3' */ tmp_2 = (int16_T)((rtb_Switch_m[0] * rtConstP.r_sin_M1_Table[rtb_LogicalOperator3]) >> 14) + (int16_T)((rtb_Switch_m[1] * rtConstP.r_cos_M1_Table[rtb_LogicalOperator3]) >> 14); if (tmp_2 > 32767) { tmp_2 = 32767; } else { if (tmp_2 < -32768) { tmp_2 = -32768; } } /* Sum: '/Sum6' incorporates: * Interpolation_n-D: '/r_cos_M1' * Interpolation_n-D: '/r_sin_M1' * Product: '/Divide1' * Product: '/Divide4' */ tmp_0 = (int16_T)((rtb_Switch_m[0] * rtConstP.r_cos_M1_Table[rtb_LogicalOperator3]) >> 14) - (int16_T)((rtb_Switch_m[1] * rtConstP.r_sin_M1_Table[rtb_LogicalOperator3]) >> 14); if (tmp_0 > 32767) { tmp_0 = 32767; } else { if (tmp_0 < -32768) { tmp_0 = -32768; } } /* Product: '/Divide7' incorporates: * Constant: '/Constant3' * Sum: '/Sum1' */ rtb_Switch_dr = (int16_T)((2365 * (int16_T)tmp_2) >> 12); /* MATLAB Function: '/sector_select' incorporates: * Product: '/Divide7' * Sum: '/Sum1' * Sum: '/Sum6' */ if ((int16_T)tmp_2 >= 0) { if ((int16_T)tmp_0 >= 0) { if (rtb_Switch_dr > (int16_T)tmp_0) { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 2U; } else { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 1U; } } else if (-rtb_Switch_dr > (int16_T)tmp_0) { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 3U; } else { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 2U; } } else if ((int16_T)tmp_0 >= 0) { if (-rtb_Switch_dr > (int16_T)tmp_0) { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 5U; } else { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 6U; } } else if (rtb_Switch_dr > (int16_T)tmp_0) { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 4U; } else { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 5U; } /* End of MATLAB Function: '/sector_select' */ /* Gain: '/Gain' incorporates: * Inport: '/vbus_voltage' */ rtb_Gain = 18919 * rtU->vbus_voltage; /* Product: '/Divide' incorporates: * Gain: '/Gain' * Sum: '/Sum6' */ rtb_Sign = (int16_T)(((int64_T)(int16_T)tmp_0 << 26) / rtb_Gain); /* Product: '/Divide1' incorporates: * Gain: '/Gain' * Sum: '/Sum1' */ rtb_r_cos_M1 = (int16_T)(((int64_T)(int16_T)tmp_2 << 26) / rtb_Gain); /* MultiPortSwitch: '/Multiport Switch' incorporates: * DataTypeConversion: '/Data Type Conversion1' * Sum: '/Add4' * Sum: '/Add4' * Sum: '/Add4' * Sum: '/Add4' * Sum: '/Add4' * Sum: '/Add4' */ switch (rtb_DataTypeConversion1_c) { case 1: /* Product: '/Divide3' incorporates: * Product: '/Divide1' * Product: '/Divide2' */ rtb_Switch_dr = (int16_T)(((int16_T)((rtb_r_cos_M1 * 9459) >> 13) * 375) >> 9); /* Product: '/Divide1' incorporates: * Constant: '/Constant' * Product: '/Divide' * Product: '/Divide1' * Product: '/Divide' * Sum: '/Add' */ rtb_r_cos_M1 = (int16_T)(((int16_T)(rtb_Sign - ((rtb_r_cos_M1 * 9459) >> 14)) * 375) >> 9); /* Product: '/Divide4' incorporates: * Sum: '/Add1' * Sum: '/Add2' */ rtb_Sign = (int16_T)((int16_T)(3000 - (int16_T)(rtb_r_cos_M1 + rtb_Switch_dr)) >> 1); /* Sum: '/Add3' */ rtb_Switch_dr += rtb_Sign; rtb_MultiportSwitch_idx_0 = (int16_T)(rtb_Switch_dr + rtb_r_cos_M1); rtb_MultiportSwitch_idx_1 = rtb_Switch_dr; rtb_Switch_dr = rtb_Sign; break; case 2: /* Product: '/Divide1' incorporates: * Constant: '/Constant' * Product: '/Divide' * Product: '/Divide1' * Product: '/Divide' * Sum: '/Add' */ rtb_Switch_dr = (int16_T)(((int16_T)(((rtb_r_cos_M1 * 9459) >> 14) + rtb_Sign) * 375) >> 9); /* Product: '/Divide3' incorporates: * Constant: '/Constant' * Product: '/Divide' * Product: '/Divide1' * Product: '/Divide2' * Sum: '/Add5' */ rtb_r_cos_M1 = (int16_T)(((int16_T)(((rtb_r_cos_M1 * 9459) >> 14) - rtb_Sign) * 375) >> 9); /* Product: '/Divide4' incorporates: * Sum: '/Add1' * Sum: '/Add2' */ rtb_Sign = (int16_T)((int16_T)(3000 - (int16_T)(rtb_r_cos_M1 + rtb_Switch_dr)) >> 1); /* Sum: '/Add3' */ rtb_Switch_dr += rtb_Sign; rtb_MultiportSwitch_idx_0 = rtb_Switch_dr; rtb_MultiportSwitch_idx_1 = (int16_T)(rtb_Switch_dr + rtb_r_cos_M1); rtb_Switch_dr = rtb_Sign; break; case 3: /* Product: '/Divide1' incorporates: * Constant: '/Constant' * Product: '/Divide' * Product: '/Divide1' * Product: '/Divide' * Sum: '/Add' */ rtb_Sign = (int16_T)(((int16_T)(-rtb_Sign - ((rtb_r_cos_M1 * 9459) >> 14)) * 375) >> 9); /* Product: '/Divide3' incorporates: * Product: '/Divide1' * Product: '/Divide2' */ rtb_r_cos_M1 = (int16_T)(((int16_T)((rtb_r_cos_M1 * 9459) >> 13) * 375) >> 9); /* Product: '/Divide4' incorporates: * Sum: '/Add1' * Sum: '/Add2' */ rtb_Switch_dr = (int16_T)((int16_T)(3000 - (int16_T)(rtb_r_cos_M1 + rtb_Sign)) >> 1); /* Sum: '/Add3' */ rtb_Sign += rtb_Switch_dr; rtb_MultiportSwitch_idx_0 = rtb_Switch_dr; rtb_MultiportSwitch_idx_1 = (int16_T)(rtb_Sign + rtb_r_cos_M1); rtb_Switch_dr = rtb_Sign; break; case 4: /* Product: '/Divide1' incorporates: * Constant: '/Constant' * Product: '/Divide' * Product: '/Divide1' * Product: '/Divide' * Sum: '/Add' */ rtb_Sign = (int16_T)(((int16_T)(((rtb_r_cos_M1 * 9459) >> 14) - rtb_Sign) * 375) >> 9); /* Product: '/Divide3' incorporates: * Product: '/Divide1' * Product: '/Divide2' * Sum: '/Add5' */ rtb_r_cos_M1 = (int16_T)(((int16_T)(-((int16_T)((rtb_r_cos_M1 * 9459) >> 13) << 2) >> 2) * 375) >> 9); /* Product: '/Divide4' incorporates: * Sum: '/Add1' * Sum: '/Add2' */ rtb_Switch_dr = (int16_T)((int16_T)(3000 - (int16_T)(rtb_r_cos_M1 + rtb_Sign)) >> 1); /* Sum: '/Add3' */ rtb_Sign += rtb_Switch_dr; rtb_MultiportSwitch_idx_0 = rtb_Switch_dr; rtb_MultiportSwitch_idx_1 = rtb_Sign; rtb_Switch_dr = (int16_T)(rtb_Sign + rtb_r_cos_M1); break; case 5: /* Product: '/Divide3' incorporates: * Constant: '/Constant' * Product: '/Divide' * Product: '/Divide1' * Product: '/Divide2' * Sum: '/Add5' */ rtb_Switch_dr = (int16_T)(((int16_T)(rtb_Sign - ((rtb_r_cos_M1 * 9459) >> 14)) * 375) >> 9); /* Product: '/Divide1' incorporates: * Constant: '/Constant' * Product: '/Divide' * Product: '/Divide1' * Product: '/Divide' * Sum: '/Add' */ rtb_r_cos_M1 = (int16_T)(((int16_T)(-rtb_Sign - ((rtb_r_cos_M1 * 9459) >> 14)) * 375) >> 9); /* Product: '/Divide4' incorporates: * Sum: '/Add1' * Sum: '/Add2' */ rtb_Sign = (int16_T)((int16_T)(3000 - (int16_T)(rtb_r_cos_M1 + rtb_Switch_dr)) >> 1); /* Sum: '/Add3' */ rtb_Switch_dr += rtb_Sign; rtb_MultiportSwitch_idx_0 = rtb_Switch_dr; rtb_MultiportSwitch_idx_1 = rtb_Sign; rtb_Switch_dr += rtb_r_cos_M1; break; default: /* Product: '/Divide3' incorporates: * Product: '/Divide1' * Product: '/Divide2' * Sum: '/Add5' */ rtb_Switch_dr = (int16_T)(((int16_T)(-((int16_T)((rtb_r_cos_M1 * 9459) >> 13) << 2) >> 2) * 375) >> 9); /* Product: '/Divide1' incorporates: * Constant: '/Constant' * Product: '/Divide' * Product: '/Divide1' * Product: '/Divide' * Sum: '/Add' */ rtb_r_cos_M1 = (int16_T)(((int16_T)(((rtb_r_cos_M1 * 9459) >> 14) + rtb_Sign) * 375) >> 9); /* Product: '/Divide4' incorporates: * Sum: '/Add1' * Sum: '/Add2' */ rtb_Sign = (int16_T)((int16_T)(3000 - (int16_T)(rtb_r_cos_M1 + rtb_Switch_dr)) >> 1); /* Sum: '/Add3' */ rtb_Switch_dr += rtb_Sign; rtb_MultiportSwitch_idx_0 = (int16_T)(rtb_Switch_dr + rtb_r_cos_M1); rtb_MultiportSwitch_idx_1 = rtb_Sign; break; } /* End of MultiPortSwitch: '/Multiport Switch' */ /* Outport: '/VdPrev' incorporates: * UnitDelay: '/UnitDelay1' */ rtY->VdPrev = rtDW->UnitDelay1_DSTATE_f; /* Sum: '/Add1' incorporates: * Constant: '/Filter_Constant' * Constant: '/One' * Product: '/Product' * Product: '/Product1' * Sqrt: '/Sqrt' * UnitDelay: '/Unit Delay' */ rtb_Switch_oi = (int16_T)(((rtb_Switch_oi * 41) >> 12) + ((4055 * rtDW->UnitDelay_DSTATE_c) >> 12)); /* Update for UnitDelay: '/UnitDelay1' incorporates: * Sum: '/Sum3' */ rtDW->UnitDelay1_DSTATE = qY; /* Update for Delay: '/Delay' incorporates: * Inport: '/hall_a' */ rtDW->Delay_DSTATE = rtU->hall_a; /* Update for Delay: '/Delay1' incorporates: * Inport: '/hall_b' */ rtDW->Delay1_DSTATE = rtU->hall_b; /* Update for Delay: '/Delay2' incorporates: * Inport: '/hall_c' */ rtDW->Delay2_DSTATE = rtU->hall_c; /* Update for UnitDelay: '/UnitDelay3' incorporates: * Inport: '/hw_count' */ rtDW->UnitDelay3_DSTATE = rtU->hw_count; /* Update for UnitDelay: '/UnitDelay4' incorporates: * Abs: '/Abs5' */ rtDW->UnitDelay4_DSTATE = rtb_Switch2_j; /* Update for UnitDelay: '/UnitDelay' */ rtDW->UnitDelay_DSTATE_j = rtb_UnitDelay_bc; /* Update for UnitDelay: '/UnitDelay2' */ rtDW->UnitDelay2_DSTATE_p = rtb_Switch_m[1]; /* Update for UnitDelay: '/Unit Delay' */ rtDW->UnitDelay_DSTATE_b = rtb_Switch2_fu; /* Update for UnitDelay: '/Unit Delay' incorporates: * Saturate: '/Saturation' * Sum: '/Sum3' * Sum: '/Sum1' */ rtDW->UnitDelay_DSTATE = rtb_Saturation - rtb_Sum1; /* Update for UnitDelay: '/Unit Delay' incorporates: * Sum: '/Add1' */ rtDW->UnitDelay_DSTATE_e = rtb_Switch_oi; /* Update for Delay: '/Resettable Delay' incorporates: * Sum: '/Sum1' */ rtDW->icLoad = 0U; rtDW->ResettableDelay_DSTATE = rtb_Sum1; /* Update for UnitDelay: '/UnitDelay1' */ rtDW->UnitDelay1_DSTATE_f = rtb_Switch_m[0]; /* Update for UnitDelay: '/Unit Delay' incorporates: * Sum: '/Add1' */ rtDW->UnitDelay_DSTATE_c = rtb_Switch_oi; /* Switch: '/Switch2' incorporates: * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Switch: '/Switch' */ if (rtb_MultiportSwitch_idx_0 > 3000) { /* Outport: '/PWM' incorporates: * Constant: '/Constant1' */ rtY->PWM[0] = 3000U; } else if (rtb_MultiportSwitch_idx_0 < 0) { /* Switch: '/Switch' incorporates: * Constant: '/Constant5' * Outport: '/PWM' */ rtY->PWM[0] = 0U; } else { /* Outport: '/PWM' incorporates: * Switch: '/Switch' */ rtY->PWM[0] = (uint16_T)rtb_MultiportSwitch_idx_0; } if (rtb_MultiportSwitch_idx_1 > 3000) { /* Outport: '/PWM' incorporates: * Constant: '/Constant1' */ rtY->PWM[1] = 3000U; } else if (rtb_MultiportSwitch_idx_1 < 0) { /* Switch: '/Switch' incorporates: * Constant: '/Constant5' * Outport: '/PWM' */ rtY->PWM[1] = 0U; } else { /* Outport: '/PWM' incorporates: * Switch: '/Switch' */ rtY->PWM[1] = (uint16_T)rtb_MultiportSwitch_idx_1; } if (rtb_Switch_dr > 3000) { /* Outport: '/PWM' incorporates: * Constant: '/Constant1' */ rtY->PWM[2] = 3000U; } else if (rtb_Switch_dr < 0) { /* Switch: '/Switch' incorporates: * Constant: '/Constant5' * Outport: '/PWM' */ rtY->PWM[2] = 0U; } else { /* Outport: '/PWM' incorporates: * Switch: '/Switch' */ rtY->PWM[2] = (uint16_T)rtb_Switch_dr; } /* End of Switch: '/Switch2' */ /* End of Outputs for SubSystem: '/PMSM_Controller' */ /* Outport: '/sector' */ rtY->sector = rtb_DataTypeConversion1_c; /* Outport: '/n_MotError' */ rtY->n_MotError = rtb_UnitDelay_bc; /* Outport: '/iq' */ rtY->iq = rtb_DataTypeConversion_k1[1]; /* Outport: '/id' */ rtY->id = rtb_DataTypeConversion_k1[0]; /* Outport: '/angle' incorporates: * Sum: '/Sum' */ rtY->angle = rtb_Sum; /* Outport: '/rpm' incorporates: * Switch: '/Switch2' */ rtY->rpm = rtb_Switch2_c; /* Outport: '/hall_angle' incorporates: * Merge: '/Merge' */ rtY->hall_angle = rtb_Switch3_c; /* Outport: '/hall_state' */ rtY->hall_state = rtb_Add_cr; /* Outport: '/running_mode' */ rtY->running_mode = rtb_z_ctrlMod; } /* Model initialize function */ void PMSM_Controller_initialize(RT_MODEL *const rtM) { DW *rtDW = rtM->dwork; PrevZCX *rtPrevZCX = rtM->prevZCSigState; rtPrevZCX->ResettableDelay_Reset_ZCE = POS_ZCSIG; rtPrevZCX->pi_speed_d.ResettableDelay_Reset_ZCE_a = POS_ZCSIG; rtPrevZCX->PI_id.ResettableDelay_Reset_ZCE_p = POS_ZCSIG; rtPrevZCX->PI_iq.ResettableDelay_Reset_ZCE_p = POS_ZCSIG; /* SystemInitialize for Atomic SubSystem: '/PMSM_Controller' */ /* InitializeConditions for Delay: '/Resettable Delay' */ rtDW->icLoad = 1U; /* SystemInitialize for IfAction SubSystem: '/Raw_Motor_Speed_Estimation' */ /* InitializeConditions for UnitDelay: '/UnitDelay2' */ rtDW->UnitDelay2_DSTATE = 200000U; /* SystemInitialize for Outport: '/z_counter' incorporates: * Inport: '/z_counterRawPrev' */ rtDW->z_counterRawPrev = 200000U; /* End of SystemInitialize for SubSystem: '/Raw_Motor_Speed_Estimation' */ /* SystemInitialize for Atomic SubSystem: '/Debounce_Filter' */ Debounce_Filter_Init(&rtDW->Debounce_Filter_i); /* End of SystemInitialize for SubSystem: '/Debounce_Filter' */ /* SystemInitialize for IfAction SubSystem: '/speed_mode' */ /* SystemInitialize for Atomic SubSystem: '/pi_speed' */ pi_speed_Init(&rtDW->pi_speed_d); /* End of SystemInitialize for SubSystem: '/pi_speed' */ /* End of SystemInitialize for SubSystem: '/speed_mode' */ /* SystemInitialize for IfAction SubSystem: '/iq_ctrl' */ /* SystemInitialize for Atomic SubSystem: '/PI_iq' */ PI_backCalc_fixdt_Init(&rtDW->PI_iq); /* End of SystemInitialize for SubSystem: '/PI_iq' */ /* End of SystemInitialize for SubSystem: '/iq_ctrl' */ /* SystemInitialize for IfAction SubSystem: '/id_ctrl' */ /* SystemInitialize for Atomic SubSystem: '/PI_id' */ PI_backCalc_fixdt_Init(&rtDW->PI_id); /* End of SystemInitialize for SubSystem: '/PI_id' */ /* End of SystemInitialize for SubSystem: '/id_ctrl' */ /* End of SystemInitialize for SubSystem: '/PMSM_Controller' */ } /* * File trailer for generated code. * * [EOF] */