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