diff --git a/Inu/MCU.c b/Inu/MCU.c index 6cca00c..62c97ae 100644 --- a/Inu/MCU.c +++ b/Inu/MCU.c @@ -152,7 +152,7 @@ static void mdlInitializeSizes(SimStruct *S) */ static void mdlStart(SimStruct *S) { - SIM_Initialize_Simulation(); + SIM_Initialize_Simulation(S); } #endif // MDL_START @@ -187,7 +187,7 @@ static void mdlTerminate(SimStruct *S) hmcu.fMCU_Stop = 1; ResumeThread(hmcu.hMCUThread); WaitForSingleObject(hmcu.hMCUThread, 10000); - SIM_deInitialize_Simulation(); + SIM_deInitialize_Simulation(S); mexUnlock(); } diff --git a/Inu/app_wrapper/app_init.c b/Inu/app_wrapper/app_init.c index 0087c45..a91845d 100644 --- a/Inu/app_wrapper/app_init.c +++ b/Inu/app_wrapper/app_init.c @@ -41,11 +41,11 @@ void app_init(void) { edrk.zadanie.iq_ZadanieU_Charge = _IQ(2500 / NORMA_ACP); edrk.temper_limit_koeffs.sum_limit = _IQ(1); simple_scalar1.fzad_add_max = _IQ(FZAD_ADD_MAX); - //edrk.Mode_ScalarVectorUFConst = ALG_MODE_SCALAR_OBOROTS; - edrk.Mode_ScalarVectorUFConst = ALG_MODE_FOC_OBOROTS; + edrk.Mode_ScalarVectorUFConst = ALG_MODE_SCALAR_OBOROTS; + //edrk.Mode_ScalarVectorUFConst = ALG_MODE_FOC_OBOROTS; edrk.zadanie.iq_power_zad = _IQ(1); - edrk.zadanie.iq_oborots_zad_hz = _IQ(0.5); + edrk.zadanie.iq_oborots_zad_hz = _IQ(1); edrk.MasterSlave = MODE_MASTER; edrk.master_theta; diff --git a/Inu/app_wrapper/app_io.c b/Inu/app_wrapper/app_io.c index 901fc28..b4ac3c8 100644 --- a/Inu/app_wrapper/app_io.c +++ b/Inu/app_wrapper/app_io.c @@ -7,6 +7,7 @@ #include "app_io.h" #include "pwm_sim.h" +#include "adc_sim.h" int Unites[UNIT_QUA_UNITS][UNIT_LEN]; int CAN_timeout[UNIT_QUA]; @@ -14,17 +15,17 @@ RS_DATA_STRUCT rs_a = RS_DATA_STRUCT_DEFAULT, rs_b = RS_DATA_STRUCT_DEFAULT; // void readInputParameters(const real_T* u) { - int nn = 0; - iq_norm_ADC[0][0] = _IQ(u[nn++] / NORMA_ACP); - iq_norm_ADC[0][1] = _IQ(u[nn++] / NORMA_ACP); - iq_norm_ADC[0][2] = _IQ(u[nn++] / NORMA_ACP); - iq_norm_ADC[0][3] = _IQ(u[nn++] / NORMA_ACP); - iq_norm_ADC[0][4] = _IQ(u[nn++] / NORMA_ACP); - iq_norm_ADC[0][5] = _IQ(u[nn++] / NORMA_ACP); - iq_norm_ADC[0][6] = _IQ(u[nn++] / NORMA_ACP); - iq_norm_ADC[0][7] = _IQ(u[nn++] / NORMA_ACP); + iq_norm_ADC[0][0] = _IQ(AdcSim.udc1.adc_val); + iq_norm_ADC[0][1] = _IQ(AdcSim.udc2.adc_val); + iq_norm_ADC[0][2] = _IQ(AdcSim.ia1.adc_val); + iq_norm_ADC[0][3] = _IQ(AdcSim.ib1.adc_val); + iq_norm_ADC[0][4] = _IQ(AdcSim.ic1.adc_val); + iq_norm_ADC[0][5] = _IQ(AdcSim.ia2.adc_val); + iq_norm_ADC[0][6] = _IQ(AdcSim.ib2.adc_val); + iq_norm_ADC[0][7] = _IQ(AdcSim.ic2.adc_val); + int nn = 8; WRotor.iqWRotorCalcBeforeRegul1 = _IQ(u[nn++] / (PI*2) / NORMA_FROTOR); u[nn++]; @@ -32,6 +33,9 @@ void readInputParameters(const real_T* u) { edrk.Go = u[nn++]; u[nn++]; + + edrk.zadanie.iq_power_zad = _IQmpy(_IQ(u[nn]), (edrk.zadanie.rmp_powers_zad.RampHighLimit)); + edrk.zadanie.iq_oborots_zad_hz = _IQmpy(_IQ(u[nn++]), (edrk.zadanie.rmp_oborots_zad_hz.RampHighLimit)); } //void input_param(unsigned short num, unsigned short val) diff --git a/Inu/mcu_wrapper.c b/Inu/mcu_wrapper.c index 994f0a8..e968d62 100644 --- a/Inu/mcu_wrapper.c +++ b/Inu/mcu_wrapper.c @@ -10,6 +10,7 @@ #include "app_init.h" #include "app_io.h" #include "pwm_sim.h" +#include "adc_sim.h" /** * @addtogroup WRAPPER_CONF @@ -59,7 +60,7 @@ void MCU_Step_Simulation(SimStruct* S, time_T time) MCU_readInputs(S); // считывание портов - MCU_Periph_Simulation(); // simulate peripheral + MCU_Periph_Simulation(S); // simulate peripheral #ifdef RUN_APP_MAIN_FUNC_THREAD ResumeThread(hmcu.hMCUThread); @@ -77,8 +78,9 @@ void MCU_Step_Simulation(SimStruct* S, time_T time) * @brief Симуляция периферии МК * @details Пользовательский код, который симулирует работу периферии МК. */ -void MCU_Periph_Simulation(void) +void MCU_Periph_Simulation(SimStruct* S) { + Simulate_ADC(S); Simulate_PWM(); } @@ -122,7 +124,7 @@ void MCU_writeOutputs(SimStruct* S) * @details Пользовательский код, который создает поток для приложения МК и настраивает симулятор МК для симуляции. */ -void SIM_Initialize_Simulation(void) +void SIM_Initialize_Simulation(SimStruct* S) { #ifdef RUN_APP_MAIN_FUNC_THREAD // инициализация потока, который будет выполнять код МК @@ -130,8 +132,9 @@ void SIM_Initialize_Simulation(void) #endif //RUN_APP_MAIN_FUNC_THREAD /* user initialization */ - Init_PWM_Simulation(); app_init(); + Init_ADC_Simulation(); + Init_PWM_Simulation(); /* clock step initialization */ hmcu.sSystemClock_step = MCU_CORE_CLOCK * hmcu.sSimSampleTime; // set system clock step @@ -141,7 +144,7 @@ void SIM_Initialize_Simulation(void) * @brief Деинициализация симуляции МК. * @details Пользовательский код, который будет очищать все структуры после окончания симуляции. */ -void SIM_deInitialize_Simulation(void) +void SIM_deInitialize_Simulation(SimStruct* S) { //// simulate structures of peripheral deinitialization //deInitialize_Periph_Sim(); diff --git a/Inu/mcu_wrapper_conf.h b/Inu/mcu_wrapper_conf.h index 7ef62bd..8ea4aa8 100644 --- a/Inu/mcu_wrapper_conf.h +++ b/Inu/mcu_wrapper_conf.h @@ -142,13 +142,13 @@ extern SIM__MCUHandleTypeDef hmcu; // extern для видимос void MCU_Step_Simulation(SimStruct *S, time_T time); /* MCU peripheral simulation */ -void MCU_Periph_Simulation(void); +void MCU_Periph_Simulation(SimStruct* S); /* Initialize MCU simulation */ -void SIM_Initialize_Simulation(void); +void SIM_Initialize_Simulation(SimStruct* S); /* Deinitialize MCU simulation */ -void SIM_deInitialize_Simulation(void); +void SIM_deInitialize_Simulation(SimStruct* S); /* Read inputs S-function */ void MCU_readInputs(SimStruct* S); diff --git a/Inu/run_mex.bat b/Inu/run_mex.bat index 02a7494..8c4bc70 100644 --- a/Inu/run_mex.bat +++ b/Inu/run_mex.bat @@ -62,7 +62,8 @@ set code_WRAPPER= .\Inu\MCU.c^ .\Inu\app_wrapper\app_init.c^ .\Inu\app_wrapper\app_io.c^ .\Inu\app_wrapper\app_wrapper.c^ - .\Inu\xilinx_wrapper\pwm_sim.c + .\Inu\xilinx_wrapper\pwm_sim.c^ + .\Inu\xilinx_wrapper\adc_sim.c ::------------------------------------------------------------------------- diff --git a/Inu/xilinx_wrapper/adc_sim.c b/Inu/xilinx_wrapper/adc_sim.c index ef81c81..1f2cfea 100644 --- a/Inu/xilinx_wrapper/adc_sim.c +++ b/Inu/xilinx_wrapper/adc_sim.c @@ -1,3 +1,74 @@ #include "adc_sim.h" -AdcSimHandle adcsim; +AdcSimHandle AdcSim; + + +void Simulate_ADC(SimStruct* S) +{ + real_T* IN = ssGetInputPortRealSignal(S, 0); + + adcMeasure(&AdcSim.Measure, IN, 0); + + adcConvert(&AdcSim.convertion, &AdcSim.udc1, AdcSim.Measure.udc1, 0); + adcConvert(&AdcSim.convertion, &AdcSim.udc2, AdcSim.Measure.udc2, 0); + adcConvert(&AdcSim.convertion, &AdcSim.ia1, AdcSim.Measure.ia1, 0); + adcConvert(&AdcSim.convertion, &AdcSim.ib1, AdcSim.Measure.ib1, 0); + adcConvert(&AdcSim.convertion, &AdcSim.ic1, AdcSim.Measure.ic1, 0); + adcConvert(&AdcSim.convertion, &AdcSim.ia2, AdcSim.Measure.ia2, 0); + adcConvert(&AdcSim.convertion, &AdcSim.ib2, AdcSim.Measure.ib2, 0); + adcConvert(&AdcSim.convertion, &AdcSim.ic2, AdcSim.Measure.ic2, 0); +} + +void Init_ADC_Simulation() +{ + adcInitConvertion(&AdcSim.convertion, NORMA_ACP, 2.5, 4096); + adcInitMeasure(&AdcSim.udc1, K_LEM_ADC[0], R_ADC[0], DEFAULT_ZERO_ADC); + adcInitMeasure(&AdcSim.udc2, K_LEM_ADC[1], R_ADC[1], DEFAULT_ZERO_ADC); + adcInitMeasure(&AdcSim.ia1, K_LEM_ADC[2], R_ADC[2], DEFAULT_ZERO_ADC); + adcInitMeasure(&AdcSim.ib1, K_LEM_ADC[3], R_ADC[3], DEFAULT_ZERO_ADC); + adcInitMeasure(&AdcSim.ic1, K_LEM_ADC[4], R_ADC[4], DEFAULT_ZERO_ADC); + adcInitMeasure(&AdcSim.ia2, K_LEM_ADC[5], R_ADC[5], DEFAULT_ZERO_ADC); + adcInitMeasure(&AdcSim.ib2, K_LEM_ADC[6], R_ADC[6], DEFAULT_ZERO_ADC); + adcInitMeasure(&AdcSim.ic2, K_LEM_ADC[7], R_ADC[7], DEFAULT_ZERO_ADC); + +} + +void adcInitConvertion(AdcConvertionHandle* hconv, int norma_adc, double adc_amplitude, int adc_bit_depth) +{ + hconv->norma_adc = norma_adc; + hconv->adc_amplitude = adc_amplitude; + hconv->adc_bit_depth = adc_bit_depth; +} + +void adcInitMeasure(AdcMeasureHandle* hmeasure, int k_lem_adc, int r_adc, int offset) +{ + hmeasure->k_lem_adc = k_lem_adc; + hmeasure->r_adc = r_adc; + hmeasure->offset = offset; +} + +void adcMeasure(AdcRealMeasureHandle *hrmeasure, const real_T* u, int startind) +{ + hrmeasure->udc1 = u[startind++]; + hrmeasure->udc2 = u[startind++]; + hrmeasure->ia1 = u[startind++]; + hrmeasure->ib1 = u[startind++]; + hrmeasure->ic1 = u[startind++]; + hrmeasure->ia2 = u[startind++]; + hrmeasure->ib2 = u[startind++]; + hrmeasure->ic2 = u[startind++]; +} + +void adcConvert(AdcConvertionHandle* hconv, AdcMeasureHandle* hmeasure, double realMeasure, double MeasureSatur) +{ + if (MeasureSatur != 0) + { + if (realMeasure > MeasureSatur) + realMeasure = MeasureSatur; + else if (realMeasure < -MeasureSatur) + realMeasure = -MeasureSatur; + } + // AdcMirror.ADCRESULT0 = (unsigned short)(realMeasure/MeasureSatur*2048. + (float)offset.Udc1); + hmeasure->adc_val = + (unsigned short)(realMeasure / hmeasure->k_lem_adc * hmeasure->r_adc / hconv->norma_adc / hconv->adc_amplitude * hconv->adc_bit_depth + (float)hmeasure->offset); +} \ No newline at end of file diff --git a/Inu/xilinx_wrapper/adc_sim.h b/Inu/xilinx_wrapper/adc_sim.h index 03b98a2..f4ea857 100644 --- a/Inu/xilinx_wrapper/adc_sim.h +++ b/Inu/xilinx_wrapper/adc_sim.h @@ -1,5 +1,7 @@ -#ifndef PWM_SIM -#define PWM_SIM +#include "mcu_wrapper_conf.h" + +#ifndef ADC_SIM +#define ADC_SIM // ADC typedef struct @@ -7,6 +9,64 @@ typedef struct int tAdc; int Tadc; int nAdc; + + int norma_adc; + double adc_amplitude; + int adc_bit_depth; +}AdcConvertionHandle; + +// +typedef struct +{ + double udc1; + double udc2; + double ia1; + double ib1; + double ic1; + double ia2; + double ib2; + double ic2; + double wm; +}AdcRealMeasureHandle; + + +// +typedef struct +{ + int adc_val; + + int r_adc; + int k_lem_adc; + int offset; + +}AdcMeasureHandle; + + +typedef struct +{ + AdcConvertionHandle convertion; + AdcRealMeasureHandle Measure; + AdcMeasureHandle udc1; + AdcMeasureHandle udc2; + AdcMeasureHandle ia1; + AdcMeasureHandle ib1; + AdcMeasureHandle ic1; + AdcMeasureHandle ia2; + AdcMeasureHandle ib2; + AdcMeasureHandle ic2; + AdcMeasureHandle wm; }AdcSimHandle; +AdcSimHandle AdcSim; + + + +void Simulate_ADC(SimStruct* S); +void Init_ADC_Simulation(); + +void adcInitConvertion(AdcConvertionHandle* hconv, int norma_adc, int adc_amplitude, int adc_bit_depth); +void adcInitMeasure(AdcMeasureHandle* hmeasure, int k_lem_adc, int r_adc, int offset); + +void adcMeasure(AdcRealMeasureHandle* hrmeasure, const real_T* u, int startind); +void adcConvert(AdcConvertionHandle* hconv, AdcMeasureHandle* hmeasure, double realMeasure, double MeasureSatur); #endif //PWM_SIM diff --git a/inu_23550.slx b/inu_23550.slx index 0be3b61..5f1aba3 100644 Binary files a/inu_23550.slx and b/inu_23550.slx differ