/* * File: PMSM_Controller.c * * Code generated for Simulink model 'PMSM_Controller'. * * Model version : 1.1245 * Simulink Coder version : 9.4 (R2020b) 29-Jul-2020 * C/C++ source code generated on : Thu Apr 7 15:23:34 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. */ static uint16_T plook_u16s16_evencka(int16_T u, int16_T bp0, uint16_T bpSpace, uint32_T maxIndex); static uint8_T plook_u8u16_evencka(uint16_T u, uint16_T bp0, uint16_T bpSpace, uint32_T maxIndex); 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 uint8_T plook_u8u16_evencka(uint16_T u, uint16_T bp0, uint16_T bpSpace, uint32_T maxIndex) { uint16_T fbpIndex; uint8_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 { 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; } /* * 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_n; 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_n = 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_n > 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_n = 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_n <= 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_j; /* 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_j = ((int32_T)tmp >> 2) + localDW->ResettableDelay_DSTATE; /* Sum: '/Sum6' incorporates: * DataTypeConversion: '/Data Type Conversion1' * Product: '/Divide4' * Sum: '/Sum1' */ tmp = ((int64_T)(rtb_Sum1_j >> 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_j; } /* 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_jw; int32_T rtb_Sum1_d; /* Product: '/Divide4' */ rtb_Divide4_jw = (rtu_err * rtu_P) >> 2; /* Delay: '/Resettable Delay' incorporates: * DataTypeConversion: '/Data Type Conversion2' */ if ((rtu_reset > 0) && (localZCE->ResettableDelay_Reset_ZCE != POS_ZCSIG)) { localDW->icLoad = 1U; } localZCE->ResettableDelay_Reset_ZCE = (ZCSigState)(rtu_reset > 0); if (localDW->icLoad != 0) { localDW->ResettableDelay_DSTATE = localC->DataTypeConversion2; } /* Product: '/Divide1' incorporates: * Product: '/Divide4' */ tmp = ((int64_T)rtb_Divide4_jw * 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_d = (int32_T)tmp + localDW->ResettableDelay_DSTATE; /* Sum: '/Sum6' incorporates: * DataTypeConversion: '/Data Type Conversion1' * Product: '/Divide4' * Sum: '/Sum1' */ tmp = ((int64_T)(rtb_Sum1_d >> 2) << 4) + rtb_Divide4_jw; 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_d; return rty_pi_out_0; } /* 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; int32_T rtb_Add2_l; int32_T rtb_Divide; int32_T rtb_Gain1; int32_T rtb_MultiportSwitch_idx_0; uint32_T qY; uint32_T tmp; int16_T rtb_DataTypeConversion[2]; int16_T rtb_TmpSignalConversionAtLow_Pa[2]; int16_T rtb_Abs5; int16_T rtb_Abs5_h; int16_T rtb_Divide1_fi; int16_T rtb_Gain4; int16_T rtb_Max; int16_T rtb_Sign; int16_T rtb_Switch2_ip; int16_T rtb_Switch3_c; int16_T rtb_Switch_b; int16_T rtb_Switch_oi; uint16_T rtb_LogicalOperator3; 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_m; uint8_T rtb_Switch2_fu; uint8_T rtb_UnitDelay; uint8_T rtb_z_ctrlMod; boolean_T rtb_Equal_k; boolean_T rtb_LogicalOperator; boolean_T rtb_LogicalOperator2; boolean_T rtb_LogicalOperator4; 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 = (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) { /* 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: '/Switch3' incorporates: * Switch: '/Switch2' * UnitDelay: '/UnitDelay4' */ rtb_Switch3_c = rtDW->UnitDelay4_DSTATE; } else { /* Product: '/Divide13' incorporates: * Sum: '/Sum13' * Switch: '/Switch2' * UnitDelay: '/UnitDelay2' * UnitDelay: '/UnitDelay3' * UnitDelay: '/UnitDelay5' */ tmp = 8000000U / (((rtDW->UnitDelay2_DSTATE + rtDW->UnitDelay3_DSTATE_l) + rtDW->UnitDelay5_DSTATE) + rtDW->z_counterRawPrev); if (tmp > 32767U) { tmp = 32767U; } /* Switch: '/Switch3' incorporates: * Product: '/Divide13' * Switch: '/Switch2' */ rtb_Switch3_c = (int16_T)tmp; } /* 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 = rtU->hw_count; } else { tmp = 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 << 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_ip = 0; } else { rtb_Switch2_ip = rtDW->Divide11; } /* End of Switch: '/Switch2' */ /* Abs: '/Abs5' incorporates: * Switch: '/Switch2' */ if (rtb_Switch2_ip < 0) { rtb_Abs5 = (int16_T)-rtb_Switch2_ip; } else { rtb_Abs5 = rtb_Switch2_ip; } /* End of Abs: '/Abs5' */ /* If: '/If1' */ if (rtb_LogicalOperator) { /* Outputs for IfAction SubSystem: '/Subsystem' incorporates: * ActionPort: '/Action Port' */ /* Relay: '/n_commDeacv' incorporates: * Abs: '/Abs5' */ rtDW->n_commDeacv_Mode = ((rtb_Abs5 >= 120) || ((rtb_Abs5 > 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' */ /* Switch: '/Switch' incorporates: * Inport: '/b_hall_calibrate' * Inport: '/open_theta' * Merge: '/Merge' */ if (rtU->b_hall_calibrate) { rtb_Switch_b = (int16_T)(rtU->open_theta << 4); } else { rtb_Switch_b = rtb_Switch3_c; } /* End of Switch: '/Switch' */ /* Abs: '/Abs2' incorporates: * Switch: '/Switch2' */ if (rtb_Switch2_ip < 0) { rtb_LogicalOperator3 = (uint16_T)((uint32_T)-rtb_Switch2_ip >> 2); } else { rtb_LogicalOperator3 = (uint16_T)((uint32_T)rtb_Switch2_ip >> 2); } /* End of Abs: '/Abs2' */ /* UnitDelay: '/UnitDelay' */ rtb_UnitDelay = 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 & 4U) != 0U) { rtb_LogicalOperator = true; } else { if (rtDW->UnitDelay2_DSTATE_p < 0) { /* Abs: '/Abs4' incorporates: * UnitDelay: '/UnitDelay2' */ rtb_Divide1_fi = (int16_T)-rtDW->UnitDelay2_DSTATE_p; } else { /* Abs: '/Abs4' incorporates: * UnitDelay: '/UnitDelay2' */ rtb_Divide1_fi = rtDW->UnitDelay2_DSTATE_p; } rtb_LogicalOperator = (rtU->b_motEna && (rtb_Abs5 < 12) && (rtb_Divide1_fi > 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 << 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: '/b_hall_calibrate' * Inport: '/n_ctrlModReq' * Logic: '/Logical Operator11' * Logic: '/Logical Operator8' * RelationalOperator: '/Relational Operator10' */ rtb_LogicalOperator4 = (rtU->b_hall_calibrate || (!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 = rtDW->n_SpeedCtrl_Mode; /* Logic: '/Logical Operator10' incorporates: * Inport: '/b_cruiseEna' */ rtb_LogicalOperator = (rtb_LogicalOperator && 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)); /* Logic: '/Logical Operator1' incorporates: * Constant: '/constant1' * Inport: '/n_ctrlModReq' * RelationalOperator: '/Relational Operator1' */ rtb_LogicalOperator = ((rtU->n_ctrlModReq == 1) || rtb_LogicalOperator); /* 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) { 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)) { 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: '/vq_open_target' */ if (rtb_n_commDeacv) { rtb_Abs5_h = rtU->vq_open_target; } else { rtb_Abs5_h = 0; } /* End of Switch: '/Switch3' */ /* If: '/If' incorporates: * DataTypeConversion: '/Data Type Conversion1' * Inport: '/b_hall_calibrate' * Inport: '/vq_in' * Switch: '/Switch3' */ if (rtU->b_hall_calibrate) { /* Switch: '/Switch2' incorporates: * Constant: '/Constant2' * DataTypeConversion: '/Data Type Conversion1' * Inport: '/vd_open_target' * Inport: '/vd_in' */ if (rtb_n_commDeacv) { /* Outputs for IfAction SubSystem: '/If Action Subsystem' incorporates: * ActionPort: '/Action Port' */ rtDW->Merge[0] = rtU->vd_open_target; /* 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_Abs5_h; /* 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 = (rtDW->UnitDelay_DSTATE != rtb_Abs5_h); /* If: '/If' */ if (rtb_LogicalOperator) { /* Outputs for IfAction SubSystem: '/Subsystem' incorporates: * ActionPort: '/Action Port' */ /* Sum: '/Add' incorporates: * Signum: '/Sign' * Switch: '/Switch3' * UnitDelay: '/UnitDelay2' */ rtb_Sign = (int16_T)((rtb_Abs5_h - 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_Abs5_h; /* 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_Abs5_h; } /* 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' */ rtb_Divide = ((rtb_Sign << 1) + rtDW->Divide) >> 1; if (rtb_Divide > 32767) { rtb_Divide = 32767; } else { if (rtb_Divide < -32768) { rtb_Divide = -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)rtb_Divide > rtDW->Switch) { /* Merge: '/Merge' incorporates: * Switch: '/Switch' */ rtDW->Merge[1] = rtDW->Switch; } else if ((int16_T)rtb_Divide < 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)rtb_Divide; } /* 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 = rtb_Abs5_h; /* Switch: '/Switch2' */ if (rtb_LogicalOperator) { /* 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)rtb_Divide; } /* 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' */ /* Product: '/Divide1' incorporates: * Inport: '/i_dc_limit' * Inport: '/speed_limit' * Product: '/Divide4' * Switch: '/Switch' */ rtb_Divide = ((uint16_T)((rtU->i_dc_limit << 8) / rtU->speed_limit) * rtb_Switch_oi) >> 8; if (rtb_Divide > 32767) { rtb_Divide = 32767; } else { if (rtb_Divide < -32768) { rtb_Divide = -32768; } } /* Product: '/Divide1' */ rtDW->Divide1 = (int16_T)rtb_Divide; /* End of Outputs for SubSystem: '/torque_mode' */ } } /* End of If: '/If' */ /* Outputs for Atomic SubSystem: '/either_edge' */ rtb_LogicalOperator = either_edge(rtb_RelationalOperator4_f, &rtDW->either_edge_f); /* End of Outputs for SubSystem: '/either_edge' */ /* Switch: '/Switch1' */ if (rtb_LogicalOperator) { rtb_UnitDelay = rtb_DataTypeConversion1_c; } /* End of Switch: '/Switch1' */ /* Gain: '/Multiply' incorporates: * DataTypeConversion: '/Data Type Conversion' * Inport: '/adc_a' * Inport: '/adc_b' */ rtb_Divide = (12351 * rtU->adc_a) >> 11; if (rtb_Divide > 32767) { rtb_Divide = 32767; } else { if (rtb_Divide < -32768) { rtb_Divide = -32768; } } rtb_DataTypeConversion[0] = (int16_T)rtb_Divide; rtb_Gain1 = (12351 * rtU->adc_b) >> 11; if (rtb_Gain1 > 32767) { rtb_Gain1 = 32767; } else { if (rtb_Gain1 < -32768) { rtb_Gain1 = -32768; } } rtb_DataTypeConversion[1] = (int16_T)rtb_Gain1; /* Sum: '/Add' incorporates: * Gain: '/Multiply' */ rtb_MultiportSwitch_idx_0 = (int16_T)rtb_Divide + (int16_T)rtb_Gain1; if (rtb_MultiportSwitch_idx_0 > 32767) { rtb_MultiportSwitch_idx_0 = 32767; } else { if (rtb_MultiportSwitch_idx_0 < -32768) { rtb_MultiportSwitch_idx_0 = -32768; } } /* Sum: '/Add1' incorporates: * Sum: '/Add' */ rtb_Add2_l = -rtb_MultiportSwitch_idx_0; if (-rtb_MultiportSwitch_idx_0 > 32767) { rtb_Add2_l = 32767; } /* Sum: '/Add3' incorporates: * Gain: '/Multiply' * Sum: '/Add1' */ rtb_MultiportSwitch_idx_0 = (int16_T)rtb_Gain1 + (int16_T)rtb_Add2_l; if (rtb_MultiportSwitch_idx_0 > 32767) { rtb_MultiportSwitch_idx_0 = 32767; } else { if (rtb_MultiportSwitch_idx_0 < -32768) { rtb_MultiportSwitch_idx_0 = -32768; } } /* Sum: '/Add' incorporates: * Gain: '/Multiply' * Sum: '/Add3' */ rtb_Divide = (((int16_T)rtb_Divide << 1) - rtb_MultiportSwitch_idx_0) >> 1; if (rtb_Divide > 32767) { rtb_Divide = 32767; } else { if (rtb_Divide < -32768) { rtb_Divide = -32768; } } /* Gain: '/Gain1' incorporates: * Product: '/Divide1' * Sum: '/Add' */ rtb_Divide1_fi = (int16_T)((21845 * rtb_Divide) >> 15); /* Gain: '/Gain2' incorporates: * Gain: '/Multiply' * Sum: '/Add1' * Sum: '/Add2' */ rtb_Divide = ((((int16_T)rtb_Gain1 - (int16_T)rtb_Add2_l) >> 1) * 18919) >> 14; if (rtb_Divide > 32767) { rtb_Divide = 32767; } else { if (rtb_Divide < -32768) { rtb_Divide = -32768; } } /* PreLookup: '/a_elecAngle_XA' incorporates: * Switch: '/Switch' */ rtb_LogicalOperator3 = plook_u16s16_evencka(rtb_Switch_b, 0, 4U, 1440U); /* Interpolation_n-D: '/r_cos_M1' */ rtb_Sign = rtConstP.r_cos_M1_Table[rtb_LogicalOperator3]; /* Interpolation_n-D: '/r_sin_M1' incorporates: * Product: '/Divide4' */ rtb_Abs5_h = rtConstP.r_sin_M1_Table[rtb_LogicalOperator3]; /* Sum: '/Sum1' incorporates: * Gain: '/Gain2' * Interpolation_n-D: '/r_cos_M1' * Interpolation_n-D: '/r_sin_M1' * Product: '/Divide1' * Product: '/Divide2' * Product: '/Divide3' */ rtb_Gain1 = (int16_T)((rtb_Divide1_fi * rtConstP.r_cos_M1_Table[rtb_LogicalOperator3]) >> 14) + (int16_T)(((int16_T) rtb_Divide * rtConstP.r_sin_M1_Table[rtb_LogicalOperator3]) >> 14); if (rtb_Gain1 > 32767) { rtb_Gain1 = 32767; } else { if (rtb_Gain1 < -32768) { rtb_Gain1 = -32768; } } /* SignalConversion generated from: '/Low_Pass_Filter' incorporates: * Sum: '/Sum1' */ rtb_TmpSignalConversionAtLow_Pa[0] = (int16_T)rtb_Gain1; /* Sum: '/Sum6' incorporates: * Gain: '/Gain2' * Interpolation_n-D: '/r_cos_M1' * Interpolation_n-D: '/r_sin_M1' * Product: '/Divide1' * Product: '/Divide4' */ rtb_Divide = (int16_T)(((int16_T)rtb_Divide * rtConstP.r_cos_M1_Table[rtb_LogicalOperator3]) >> 14) - (int16_T) ((rtb_Divide1_fi * rtConstP.r_sin_M1_Table[rtb_LogicalOperator3]) >> 14); if (rtb_Divide > 32767) { rtb_Divide = 32767; } else { if (rtb_Divide < -32768) { rtb_Divide = -32768; } } /* SignalConversion generated from: '/Low_Pass_Filter' incorporates: * Sum: '/Sum6' */ rtb_TmpSignalConversionAtLow_Pa[1] = (int16_T)rtb_Divide; /* Outputs for Atomic SubSystem: '/Low_Pass_Filter' */ /* Constant: '/Constant' */ Low_Pass_Filter(rtb_TmpSignalConversionAtLow_Pa, 26214, rtb_DataTypeConversion, &rtDW->Low_Pass_Filter_d); /* End of Outputs for SubSystem: '/Low_Pass_Filter' */ /* Outport: '/VdPrev' incorporates: * UnitDelay: '/UnitDelay1' */ rtY->VdPrev = rtDW->UnitDelay1_DSTATE_f; /* Abs: '/Abs5' incorporates: * UnitDelay: '/UnitDelay1' */ if (rtDW->UnitDelay1_DSTATE_f < 0) { rtb_Divide1_fi = (int16_T)-rtDW->UnitDelay1_DSTATE_f; } else { rtb_Divide1_fi = rtDW->UnitDelay1_DSTATE_f; } /* End of Abs: '/Abs5' */ /* PreLookup: '/Vq_max_XA' */ rtb_LogicalOperator3 = plook_u16s16_evencka(rtb_Divide1_fi, 0, 64U, 45U); /* Interpolation_n-D: '/iq_maxSca_M1' incorporates: * Inport: '/i_dc_limit' * Product: '/Divide3' * Product: '/Divide4' */ rtb_Divide = rtDW->Divide3 << 16; rtb_Divide = (rtb_Divide == MIN_int32_T) && (rtU->i_dc_limit == -1) ? MAX_int32_T : rtb_Divide / rtU->i_dc_limit; if (rtb_Divide < 0) { rtb_Divide = 0; } else { if (rtb_Divide > 65535) { rtb_Divide = 65535; } } /* Product: '/Divide1' incorporates: * Inport: '/i_dc_limit' * Interpolation_n-D: '/iq_maxSca_M1' * PreLookup: '/iq_maxSca_XA' * Product: '/Divide4' */ rtb_Divide1_fi = (int16_T)((rtConstP.iq_maxSca_M1_Table[plook_u8u16_evencka ((uint16_T)rtb_Divide, 0U, 1311U, 49U)] * rtU->i_dc_limit) >> 16); /* 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_m = (uint8_T)((rtb_Switch2_fu != 0) && (rtDW->UnitDelay_DSTATE_b != rtb_Switch2_fu)); /* 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' * Product: '/Divide1' * 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_ip), 3174, 10, 20, rtb_Divide1_fi, (int16_T)-rtb_Divide1_fi, 0, rtb_Switch2_fu, &rtConstB.pi_speed_g, &rtDW->pi_speed_g, &rtPrevZCX->pi_speed_g); /* End of Outputs for SubSystem: '/pi_speed' */ /* Merge: '/Merge' incorporates: * Constant: '/Constant1' * Constant: '/Constant11' * Constant: '/Constant2' * Constant: '/Constant4' * Gain: '/Gain1' * Product: '/Divide1' * SignalConversion generated from: '/iq_target' * Sum: '/Add2' * Switch: '/Switch2' * Switch: '/Switch2' */ rtDW->Merge_b = 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' */ rtb_Divide = ((int16_T)(rtb_Switch_oi - rtb_Switch2_ip) * 819) >> 6; if (rtb_Divide > 32767) { rtb_Divide = 32767; } else { if (rtb_Divide < -32768) { rtb_Divide = -32768; } } /* Product: '/Divide1' incorporates: * Sum: '/Sum3' * Switch: '/Switch2' * Switch: '/Switch' */ rtb_Gain1 = ((int16_T)(rtb_Switch2_ip - rtb_Switch_oi) * -51) >> 5; if (rtb_Gain1 > 32767) { rtb_Gain1 = 32767; } else { if (rtb_Gain1 < -32768) { rtb_Gain1 = -32768; } } rtb_Switch_oi = (int16_T)rtb_Gain1; /* End of Product: '/Divide1' */ /* MinMax: '/Max' incorporates: * Product: '/Divide' * Product: '/Divide1' */ if ((int16_T)rtb_Divide > rtb_Switch_oi) { rtb_Max = (int16_T)rtb_Divide; } else { rtb_Max = rtb_Switch_oi; } /* End of MinMax: '/Max' */ /* MinMax: '/Max3' incorporates: * MinMax: '/Max' * Product: '/Divide1' * Switch: '/Switch2' */ if (rtb_Divide1_fi < rtb_Max) { rtb_Max = rtb_Divide1_fi; } /* End of MinMax: '/Max3' */ /* Switch: '/Switch2' incorporates: * Product: '/Divide1' * RelationalOperator: '/LowerRelop1' */ if (rtDW->Divide1 <= rtb_Max) { /* MinMax: '/Max1' incorporates: * Product: '/Divide' * Product: '/Divide1' */ if ((int16_T)rtb_Divide < rtb_Switch_oi) { rtb_Switch_oi = (int16_T)rtb_Divide; } /* End of MinMax: '/Max1' */ /* MinMax: '/Max2' incorporates: * Gain: '/Gain1' * MinMax: '/Max1' * Product: '/Divide1' */ if (rtb_Switch_oi <= (int16_T)-rtb_Divide1_fi) { rtb_Switch_oi = (int16_T)-rtb_Divide1_fi; } /* End of MinMax: '/Max2' */ /* Switch: '/Switch' incorporates: * MinMax: '/Max2' * RelationalOperator: '/UpperRelop' */ if (rtDW->Divide1 < rtb_Switch_oi) { rtb_Max = rtb_Switch_oi; } else { rtb_Max = rtDW->Divide1; } /* End of Switch: '/Switch' */ } /* End of Switch: '/Switch2' */ /* Merge: '/Merge' incorporates: * SignalConversion generated from: '/torque_iq' * Switch: '/Switch2' */ rtDW->Merge_b = rtb_Max; /* End of Outputs for SubSystem: '/torque_mode' */ } } /* End of If: '/If' */ /* 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' * Interpolation_n-D: '/Vq_max_M1' * 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: * Merge: '/Merge' * Product: '/Divide1' * RelationalOperator: '/LowerRelop1' */ if (rtDW->Merge_b <= rtb_Divide1_fi) { /* Switch: '/Switch' incorporates: * Gain: '/Gain1' * RelationalOperator: '/UpperRelop' * Switch: '/Switch2' */ if (rtDW->Merge_b < (int16_T)-rtb_Divide1_fi) { rtb_Divide1_fi = (int16_T)-rtb_Divide1_fi; } else { rtb_Divide1_fi = rtDW->Merge_b; } /* End of Switch: '/Switch' */ } /* End of Switch: '/Switch2' */ /* Outputs for Atomic SubSystem: '/PI_iq' */ PI_backCalc_fixdt((int16_T)(rtb_Divide1_fi - rtb_DataTypeConversion[1]), 4096, 51, 1024, rtConstP.Vq_max_M1_Table[rtb_LogicalOperator3], (int16_T) -rtConstP.Vq_max_M1_Table[rtb_LogicalOperator3], 0, rtb_DataTypeConversion_m, &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' * Gain: '/Gain4' * Gain: '/Gain5' * Inport: '/i_dc_limit' * Interpolation_n-D: '/Vq_max_M1' * Product: '/Divide3' * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Sum: '/Add1' * Switch: '/Switch' * Switch: '/Switch2' */ if (rtDW->Divide3 > rtU->i_dc_limit) { rtb_Switch_oi = rtU->i_dc_limit; } else if (rtDW->Divide3 < (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 = rtDW->Divide3; } /* End of Switch: '/Switch2' */ /* Outputs for Atomic SubSystem: '/PI_id' */ PI_backCalc_fixdt((int16_T)(rtb_Switch_oi - rtb_DataTypeConversion[0]), 4096, 51, 1024, rtU->vbus_voltage, (int16_T)-rtU->vbus_voltage, 0, rtb_DataTypeConversion_m, &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: '/Switch1' incorporates: * Switch: '/Switch2' * Switch: '/Switch2' * Switch: '/Switch' */ if (rtb_z_ctrlMod != 0) { rtb_Switch_oi = rtDW->Switch2_d; rtb_Divide1_fi = rtDW->Switch2; } else { rtb_Switch_oi = rtDW->Merge[1]; rtb_Divide1_fi = rtDW->Merge[0]; } /* End of Switch: '/Switch1' */ /* Sum: '/Sum1' incorporates: * Interpolation_n-D: '/r_cos_M1' * Product: '/Divide2' * Product: '/Divide3' * Product: '/Divide4' * Switch: '/Switch' * Switch: '/Switch1' */ rtb_Divide = (int16_T)((rtb_Divide1_fi * rtb_Abs5_h) >> 14) + (int16_T) ((rtb_Switch_oi * rtb_Sign) >> 14); if (rtb_Divide > 32767) { rtb_Divide = 32767; } else { if (rtb_Divide < -32768) { rtb_Divide = -32768; } } /* Sum: '/Sum6' incorporates: * Interpolation_n-D: '/r_cos_M1' * Product: '/Divide1' * Product: '/Divide4' * Product: '/Divide4' * Switch: '/Switch' * Switch: '/Switch1' */ rtb_Gain1 = (int16_T)((rtb_Divide1_fi * rtb_Sign) >> 14) - (int16_T) ((rtb_Switch_oi * rtb_Abs5_h) >> 14); if (rtb_Gain1 > 32767) { rtb_Gain1 = 32767; } else { if (rtb_Gain1 < -32768) { rtb_Gain1 = -32768; } } /* Product: '/Divide3' incorporates: * Constant: '/Constant1' * Product: '/Divide' * Sum: '/Sum6' */ rtb_Sign = (int16_T)((3547 * (int16_T)rtb_Gain1) >> 12); /* Product: '/Divide2' incorporates: * Constant: '/Constant' * Sum: '/Sum1' */ rtb_Max = (int16_T)((3547 * (int16_T)rtb_Divide) >> 12); /* Product: '/Divide4' incorporates: * Constant: '/Constant2' * Product: '/Divide2' */ rtb_Abs5_h = (int16_T)((2365 * rtb_Max) >> 12); /* Sum: '/Add' incorporates: * Product: '/Divide' * Product: '/Divide4' */ rtb_Gain4 = (int16_T)((rtb_Sign + rtb_Abs5_h) >> 1); /* Sum: '/Add1' incorporates: * Product: '/Divide' * Product: '/Divide4' */ rtb_Abs5_h = (int16_T)((rtb_Abs5_h - rtb_Sign) >> 1); /* Product: '/Divide7' incorporates: * Constant: '/Constant3' * Sum: '/Sum1' */ rtb_Sign = (int16_T)((2365 * (int16_T)rtb_Divide) >> 12); /* MATLAB Function: '/sector_select' incorporates: * Product: '/Divide7' * Sum: '/Sum1' * Sum: '/Sum6' */ if ((int16_T)rtb_Divide >= 0) { if ((int16_T)rtb_Gain1 >= 0) { if (rtb_Sign > (int16_T)rtb_Gain1) { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 2U; } else { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 1U; } } else if (-rtb_Sign > (int16_T)rtb_Gain1) { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 3U; } else { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 2U; } } else if ((int16_T)rtb_Gain1 >= 0) { if (-rtb_Sign > (int16_T)rtb_Gain1) { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 5U; } else { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 6U; } } else if (rtb_Sign > (int16_T)rtb_Gain1) { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 4U; } else { /* DataTypeConversion: '/Data Type Conversion' */ rtb_DataTypeConversion1_c = 5U; } /* End of MATLAB Function: '/sector_select' */ /* Product: '/Divide' incorporates: * Inport: '/vbus_voltage' */ rtb_Sign = (int16_T)(24576000 / rtU->vbus_voltage); /* Product: '/Divide1' incorporates: * Product: '/Divide' * Product: '/Divide2' * Product: '/Divide8' */ rtb_Max = (int16_T)((((2365 * rtb_Max) >> 13) * rtb_Sign) >> 10); /* Product: '/Divide5' incorporates: * Product: '/Divide' * Sum: '/Add' */ rtb_Gain4 = (int16_T)((rtb_Gain4 * rtb_Sign) >> 11); /* Product: '/Divide6' incorporates: * Product: '/Divide' * Sum: '/Add1' */ rtb_Abs5_h = (int16_T)((rtb_Abs5_h * rtb_Sign) >> 11); /* MultiPortSwitch: '/Multiport Switch' incorporates: * DataTypeConversion: '/Data Type Conversion1' * Gain: '/Gain' * Gain: '/Gain' * Gain: '/Gain1' * Product: '/Divide2' * Product: '/Divide2' * Product: '/Divide2' * Product: '/Divide2' * Product: '/Divide2' * Product: '/Divide2' * Sum: '/Add3' * Sum: '/Add3' * Sum: '/Add3' * Sum: '/Add3' * Sum: '/Add3' * Sum: '/Add3' */ switch (rtb_DataTypeConversion1_c) { case 1: /* Product: '/Divide' incorporates: * Gain: '/Gain' * Sum: '/Add' * Sum: '/Add1' */ rtb_Gain1 = (6000 - (rtb_Max - rtb_Abs5_h)) >> 2; /* Sum: '/Add2' incorporates: * Product: '/Divide1' */ rtb_Add2_l = (rtb_Max >> 1) + rtb_Gain1; rtb_MultiportSwitch_idx_0 = (-rtb_Abs5_h >> 1) + rtb_Add2_l; rtb_Divide = rtb_Add2_l; break; case 2: /* Product: '/Divide' incorporates: * Sum: '/Add' * Sum: '/Add1' */ rtb_Sign = (int16_T)((int16_T)(6000 - (int16_T)(rtb_Abs5_h + rtb_Gain4)) >> 2); /* Sum: '/Add2' incorporates: * Product: '/Divide1' */ rtb_Max = (int16_T)((rtb_Gain4 >> 1) + rtb_Sign); rtb_MultiportSwitch_idx_0 = rtb_Max; rtb_Divide = (int16_T)((rtb_Abs5_h >> 1) + rtb_Max); rtb_Gain1 = rtb_Sign; break; case 3: /* Product: '/Divide' incorporates: * Gain: '/Gain' * Sum: '/Add' * Sum: '/Add1' */ rtb_Divide = (6000 - (rtb_Max - rtb_Gain4)) >> 2; /* Sum: '/Add2' incorporates: * Gain: '/Gain' * Product: '/Divide1' */ rtb_Gain1 = (-rtb_Gain4 >> 1) + rtb_Divide; rtb_MultiportSwitch_idx_0 = rtb_Divide; rtb_Divide = (rtb_Max >> 1) + rtb_Gain1; break; case 4: /* Product: '/Divide' incorporates: * Gain: '/Gain' * Sum: '/Add' * Sum: '/Add1' */ rtb_Gain1 = (6000 - (rtb_Abs5_h - rtb_Max)) >> 2; /* Sum: '/Add2' incorporates: * Product: '/Divide1' */ rtb_Add2_l = (rtb_Abs5_h >> 1) + rtb_Gain1; rtb_MultiportSwitch_idx_0 = rtb_Gain1; rtb_Divide = rtb_Add2_l; rtb_Gain1 = (-rtb_Max >> 1) + rtb_Add2_l; break; case 5: /* Product: '/Divide' incorporates: * Gain: '/Gain' * Gain: '/Gain1' * Sum: '/Add1' */ rtb_Gain1 = (6000 - (-rtb_Abs5_h - rtb_Gain4)) >> 2; /* Sum: '/Add2' incorporates: * Gain: '/Gain' * Product: '/Divide1' */ rtb_Add2_l = (-rtb_Abs5_h >> 1) + rtb_Gain1; rtb_MultiportSwitch_idx_0 = rtb_Add2_l; rtb_Divide = rtb_Gain1; rtb_Gain1 = (-rtb_Gain4 >> 1) + rtb_Add2_l; break; default: /* Product: '/Divide' incorporates: * Gain: '/Gain1' * Sum: '/Add' * Sum: '/Add1' */ rtb_Divide = (6000 - (rtb_Gain4 - rtb_Max)) >> 2; /* Sum: '/Add2' incorporates: * Gain: '/Gain1' * Product: '/Divide1' */ rtb_Gain1 = (-rtb_Max >> 1) + rtb_Divide; rtb_MultiportSwitch_idx_0 = (rtb_Gain4 >> 1) + rtb_Gain1; break; } /* End of MultiPortSwitch: '/Multiport Switch' */ /* 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_Abs5; /* Update for UnitDelay: '/UnitDelay' */ rtDW->UnitDelay_DSTATE_j = rtb_UnitDelay; /* Update for UnitDelay: '/UnitDelay2' incorporates: * Switch: '/Switch1' */ rtDW->UnitDelay2_DSTATE_p = rtb_Switch_oi; /* Update for UnitDelay: '/UnitDelay1' incorporates: * Switch: '/Switch' */ rtDW->UnitDelay1_DSTATE_f = rtb_Divide1_fi; /* Update for UnitDelay: '/Unit Delay' */ rtDW->UnitDelay_DSTATE_b = rtb_Switch2_fu; /* Switch: '/Switch2' incorporates: * RelationalOperator: '/LowerRelop1' * RelationalOperator: '/UpperRelop' * Switch: '/Switch' */ if (rtb_MultiportSwitch_idx_0 > 3000) { /* Outport: '/PWM' incorporates: * Constant: '/Constant6' */ 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' */ rtY->PWM[0] = (uint16_T)rtb_MultiportSwitch_idx_0; } if (rtb_Divide > 3000) { /* Outport: '/PWM' incorporates: * Constant: '/Constant6' */ rtY->PWM[1] = 3000U; } else if (rtb_Divide < 0) { /* Switch: '/Switch' incorporates: * Constant: '/Constant5' * Outport: '/PWM' */ rtY->PWM[1] = 0U; } else { /* Outport: '/PWM' */ rtY->PWM[1] = (uint16_T)rtb_Divide; } if (rtb_Gain1 > 3000) { /* Outport: '/PWM' incorporates: * Constant: '/Constant6' */ rtY->PWM[2] = 3000U; } else if (rtb_Gain1 < 0) { /* Switch: '/Switch' incorporates: * Constant: '/Constant5' * Outport: '/PWM' */ rtY->PWM[2] = 0U; } else { /* Outport: '/PWM' */ rtY->PWM[2] = (uint16_T)rtb_Gain1; } /* 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; /* Outport: '/iq' */ rtY->iq = rtb_DataTypeConversion[1]; /* Outport: '/id' */ rtY->id = rtb_DataTypeConversion[0]; /* Outport: '/angle' incorporates: * Switch: '/Switch' */ rtY->angle = rtb_Switch_b; /* Outport: '/rpm' incorporates: * Switch: '/Switch2' */ rtY->rpm = rtb_Switch2_ip; /* 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->pi_speed_g.ResettableDelay_Reset_ZCE = 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' */ /* SystemInitialize for IfAction SubSystem: '/Raw_Motor_Speed_Estimation' */ /* 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_g); /* 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] */