/*
 * sim_model.c
 *
 *  Created on: 30 окт. 2024 г.
 *      Author: yura
 */

#if (_SIMULATE_AC==1)

#ifdef __TI_COMPILER_VERSION__
#pragma SET_DATA_SECTION(".slow_vars")
#endif

//#include "math.h"


#include "V_MotorModel.h"
//#include "V_MotorParams.h"
//#include <V_IQmath.h>
#include "IQmathLib.h"
//#include "main.h"

#include "v_pwm24_v2.h"
#include "edrk_main.h"
#include "params_norma.h"
#include "adc_tools.h"
#include "filter_v1.h"
#include "mathlib.h"
#include "v_rotor_22220.h"

//Модели электродвигателей для отладки в режиме симулятора
//#pragma DATA_SECTION(sim_model, ".slow_vars")
TMotorModel sim_model = MOTOR_MODEL_DEFAULTS;



void sim_model_init(void)
{

    //model.motorInternals.udc = 540; //задается через словарь объектов

    sim_model.motorInternals.udc = 540; //задается через словарь объектов
    sim_model.tpr = svgen_pwm24_1.Tclosed_high;//450; //период частоты ШИМ
    sim_model.cmpr0 = svgen_pwm24_1.Tclosed_high/2;
    sim_model.cmpr1 = svgen_pwm24_1.Tclosed_high/2;
    sim_model.cmpr2 = svgen_pwm24_1.Tclosed_high/2;
    sim_model.cmpr3 = svgen_pwm24_1.Tclosed_high/2;


    sim_model.MotorParametersNum = 10;//
    sim_model.dt = 0;//_IQ4mpy(_IQ4(150 / 4), pwm.DeadBand >> 20) >> 4; //величина мертвого времени

    //Вызов функции инициализации модели двигателя
    sim_model.Init(&sim_model); //Модель двигателя



    sim_model.Init(&sim_model);

}

void sim_model_execute(void)
{
    //Передача текущих скважностей таймеров ШИМ в модель
    sim_model.cmpr0 = svgen_pwm24_1.Ta_imp/2 + svgen_pwm24_1.Tclosed_high/2;//PWM0->CMPA_bit.CMPA;
    sim_model.cmpr1 = svgen_pwm24_1.Tb_imp/2 + svgen_pwm24_1.Tclosed_high/2;//PWM1->CMPA_bit.CMPA;
    sim_model.cmpr2 = svgen_pwm24_1.Tc_imp/2 + svgen_pwm24_1.Tclosed_high/2;;//PWM2->CMPA_bit.CMPA;

    sim_model.InvertorEna = edrk.Go;//Флаг разрешения работы инвертора

    //Вызов функции основного расчета модели двигателя
    sim_model.Execute(&sim_model);

}


void calc_norm_ADC_0_sim(int run_norma)
{
    _iq a1,a2,a3;

#if (1)

#if (_FLOOR6)
    analog.iqU_1 = _IQ(sim_model.motorInternals.udc/NORMA_ACP/2.0);//   iq_norm_ADC[0][0] - analog_zero.iqU_1 + analog.iqU_1_imit;
    analog.iqU_2 = analog.iqU_1;//iq_norm_ADC[0][1] - analog_zero.iqU_2 + analog.iqU_1_imit;
#else
    analog.iqU_1 = iq_norm_ADC[0][0] - analog_zero.iqU_1;
    analog.iqU_2 = iq_norm_ADC[0][1] - analog_zero.iqU_2;
#endif

    analog.iqIu_1 = _IQ(sim_model.motorInternals.isPhaseA/NORMA_ACP/2.0);
    analog.iqIv_1 = _IQ(sim_model.motorInternals.isPhaseB/NORMA_ACP/2.0);
    analog.iqIw_1 = _IQ(sim_model.motorInternals.isPhaseC/NORMA_ACP/2.0);

    analog.iqIu_2 = analog.iqIu_1;
    analog.iqIv_2 = analog.iqIv_1;
    analog.iqIw_2 = analog.iqIw_1;

    analog.iqIin_1 = 0;//_IQ(sim_model.motorInternals.power/sim_model.motorInternals.udc/NORMA_ACP/2.0);   //-iq_norm_ADC[0][9]; // датчик перевернут
    analog.iqIin_2 = analog.iqIin_1;//-iq_norm_ADC[0][9]; // датчик перевернут

    analog.iqUin_A1B1 = 0;//iq_norm_ADC[0][10];

// два варианта подключения датчиков 23550.1 более правильный - по схеме
// 23550.1

    analog.iqUin_B1C1 = 0;//iq_norm_ADC[0][11]; // 23550.1
    analog.iqUin_A2B2 = 0;//iq_norm_ADC[0][12]; // 23550.1

// 23550.3 bs1 bs2

//    analog.iqUin_B1C1 = iq_norm_ADC[0][12]; // 23550.3
//    analog.iqUin_A2B2 = iq_norm_ADC[0][11]; // 23550.3
//
    analog.iqUin_B2C2 = 0;//iq_norm_ADC[0][13];

    analog.iqIbreak_1 = 0;//iq_norm_ADC[0][14];
    analog.iqIbreak_2 = 0;//iq_norm_ADC[0][15];

#else
    analog.iqU_1 = analog.iqIu_1 = analog.iqIu_2 = analog.iqIv_1 = analog.iqIv_2 =
            analog.iqIw_1 = analog.iqIw_2 = analog.iqIin_1 = analog.iqIin_2 = analog.iqUin_A1B1 =
                    analog.iqUin_B1C1 = analog.iqUin_A2B2 = analog.iqUin_B2C2 = analog.iqIbreak_1 = analog.iqIbreak_2
                    = 0;
#endif

    analog.iqUin_C1A1 = -(analog.iqUin_A1B1 + analog.iqUin_B1C1);
    analog.iqUin_C2A2 = -(analog.iqUin_A2B2 + analog.iqUin_B2C2);



    filter.iqU_1_long = exp_regul_iq(koef_Uzpt_long_filter, filter.iqU_1_long, analog.iqU_1);
    filter.iqU_2_long = exp_regul_iq(koef_Uzpt_long_filter, filter.iqU_2_long, analog.iqU_2);


//    analog.iqU_1_fast = filter_U1_3point(analog.iqU_1_fast);
    filter.iqU_1_fast = exp_regul_iq(koef_Uzpt_fast_filter, filter.iqU_1_fast, analog.iqU_1);
    filter.iqU_2_fast = exp_regul_iq(koef_Uzpt_fast_filter, filter.iqU_2_fast, analog.iqU_2);


//    filter.iqUzpt_2_2_fast = exp_regul_iq(koef_Uzpt_fast_filter, filter.iqUzpt_2_2_fast, analog.iqUzpt_2_2);



//15


   analog.iqIm_1 = im_calc(analog.iqIu_1, analog.iqIv_1, analog.iqIw_1);
   analog.iqIm_2 = im_calc(analog.iqIu_2, analog.iqIv_2, analog.iqIw_2);

   analog.iqIu = analog.iqIu_1+analog.iqIu_2;
   analog.iqIv = analog.iqIv_1+analog.iqIv_2;
   analog.iqIw = analog.iqIw_1+analog.iqIw_2;

   analog.iqIm = im_calc(analog.iqIu, analog.iqIv, analog.iqIw);


   analog.iqIin_sum = analog.iqIin_1+analog.iqIin_2;

//   analog.iqIm_3 = im_calc(analog.iqIa1_1_fir_n+analog.iqIa2_1_fir_n, analog.iqIb1_1_fir_n+analog.iqIb2_1_fir_n, analog.iqIc1_1_fir_n+analog.iqIc2_1_fir_n);

   analog.iqUin_m1 = im_calc(analog.iqUin_A1B1, analog.iqUin_B1C1, analog.iqUin_C1A1);
   analog.iqUin_m2 = im_calc(analog.iqUin_A2B2, analog.iqUin_B2C2, analog.iqUin_C2A2);

//   analog.iqUin_m2 = im_calc(analog.UinA2, analog.UinB2, analog.UinC2);

   filter.iqUin_m1 = exp_regul_iq(koef_Uin_filter, filter.iqUin_m1, analog.iqUin_m1);
   filter.iqUin_m2 = exp_regul_iq(koef_Uin_filter, filter.iqUin_m2, analog.iqUin_m2);



//   i_led1_on_off(0);
//   i_led1_on_off(1);

//1

   filter.iqIm_1 = exp_regul_iq(koef_Im_filter, filter.iqIm_1, analog.iqIm_1);
   filter.iqIm_2 = exp_regul_iq(koef_Im_filter, filter.iqIm_2, analog.iqIm_2);
   filter.iqIm = exp_regul_iq(koef_Im_filter, filter.iqIm, analog.iqIm);

   filter.iqIin_sum = exp_regul_iq(koef_Im_filter, filter.iqIin_sum, analog.iqIin_sum);

//3
//   filter_batter2_Iin.InpVarCurr = (analog.iqIin_1)-ZERO_I_IN;
 //  filter_batter2_Iin.calc(&filter_batter2_Iin);

//   filter.iqIin = _IQmpy(filter_batter2_Iin.Out,_IQ_09);


   filter.iqIin_1 = exp_regul_iq(koef_Im_filter, filter.iqIin_1, analog.iqIin_1);
   filter.iqIin_2 = exp_regul_iq(koef_Im_filter, filter.iqIin_2, analog.iqIin_2);

   a1 = analog.iqU_1+analog.iqU_2;
   a2 = analog.iqIin_1;
   a3 = _IQmpy(a1,a2);
   analog.PowerScalar = a3;
//   filter.Power = analog.iqU_1+analog.iqU_2;
   filter.PowerScalar = exp_regul_iq(koef_Power_filter, filter.PowerScalar, analog.PowerScalar);
   filter.PowerScalarFilter2 = exp_regul_iq(koef_Power_filter2, filter.PowerScalarFilter2, filter.PowerScalar);

}



void calc_rotors_sim(void)
{
    rotor_22220.direct_rotor = 1;
    rotor_22220.iqF = _IQ(sim_model.motorInternals.omega_rpm/60.0/NORMA_FROTOR);


    rotor_22220.iqFout = exp_regul_iq(koef_Wout_filter, rotor_22220.iqFout, rotor_22220.iqF);
    rotor_22220.iqFlong = exp_regul_iq(koef_Wout_filter_long, rotor_22220.iqFlong, rotor_22220.iqF);

}



//#ifdef __TI_COMPILER_VERSION__
//#pragma RESET_DATA_SECTION
//#endif

#endif
