#ifndef _V_PWM24_H
#define _V_PWM24_H

#ifdef __cplusplus
	extern "C" {  
#endif

#include "IQmathLib.h"
// #include "DSP281x_Device.h"
#include "word_structurs.h"
#include "svgen_dq.h"

//#define COUNT_VAR_FREQ 400

//#define IQ_KP_DELTA_T 134217728//0.004
//#define IQ_KP_DELTA_COMP_I 134217728//0.004

//#define PID_KP_DELTA_T 0.5//1//20//2//0.001//11.18 //0.036 //0.018 //0.18 //0.095   // PID Kp
//#define PID_KI_DELTA_T 0.000005//0.01 //0.08 // 0.008   // PID Ki
//#define PID_KD_DELTA_T 2//5//0.01 //0.08 // 0.008   // PID Ki
//#define PID_KC_DELTA_T 0.005 //0.09    // PID Kc

//#define PID_KP_DELTA_KOMP_I 1//0.12//0.06//5//10
//#define PID_KI_DELTA_KOMP_I 0.005//0//0.005//0.01
//#define PID_KC_DELTA_KOMP_I 0.005//0//0.005//0.01
//#define PID_KD_DELTA_KOMP_I 0//0//0.005//0.01
//#define PID_KD_DELTA_T 0.0000  //*100  // PID Kd



//#define DELTA_T_MAX  1258291//15099494//0.9//8388608//0.5//13421772//0.8 //8388608// 0.5//13421772 // 0.8
//#define DELTA_T_MIN  -1258291//-15099494//0.9//-8388608//-0.5//-13421772//0.8 //-8388608// -0.5//-13421772 // -0.8

//#define DELTA_KOMP_I_MAX  1258291//6//1677721//0.1//3355443//0.2//1677721//200 A//4194304// 500 A
//#define DELTA_KOMP_I_MIN -1258291//-6//-1677721//-0.1//-3355443//-0.2//-1677721//-200 A//-4194304// -500 A


//#define INSENSITIVE_LEVEL_DELTA_T  83886 //10 V//167772// 20V //335544//40 V//83886 //10 V//335544//40 V//58720//7V//167772// 20V //83886 //10 V
//#define MAX_LEVEL_DELTA_T  1258291//150V//1677721 //200v//2516582//300 V//4194304//500 V//2516582 // 838860 //100 V


typedef struct 	{ _iq  Ti;					// Output: reference phase-a switching function (pu)
					int  up_or_down;		// Output: reference phase-b switching function (pu)
					int impuls_lenght_max;
					int impuls_lenght_min;
					int counter_pass_max;
					int counter_pass_min;
					} SVGEN_PWM24_TIME;


typedef struct 	{
	_iq  Gain;						// Input: reference gain voltage (pu)
	//_iq  Offset;				// Input: reference offset voltage (pu)
	_iq  Freq;					// Input: reference frequency (pu)
	_iq  FreqMax;				// Parameter: Maximum step angle = 6*base_freq*T (pu)
	_iq  Alpha;					// History: Sector angle (pu)

	_iq delta_U;
	_iq delta_t;
	int XilinxFreq;	// Xilinx freq in TIC

    unsigned int pwm_minimal_impuls_zero_minus;
    unsigned int pwm_minimal_impuls_zero_plus;

    WORD_UINT2BITS_STRUCT saw_direct;

    int prev_level; // предыдущее состояние ШИМа, для перехода из middle или close в рабочее
    unsigned int  Tclosed_high;
    unsigned int Tclosed_saw_direct_0;
    unsigned int Tclosed_saw_direct_1;

	_iq Ia;
	_iq	Ib;
	_iq Ic;
	unsigned int number_svgen;
	SVGEN_PWM24_TIME  Ta_0;		// Output: reference phase-a switching function (pu)
	SVGEN_PWM24_TIME  Ta_1;		// Output: reference phase-a switching function (pu)
	SVGEN_PWM24_TIME  Tb_0;		// Output: reference phase-b switching function (pu)
	SVGEN_PWM24_TIME  Tb_1;		// Output: reference phase-b switching function (pu)
	SVGEN_PWM24_TIME  Tc_0;		// Output: reference phase-c switching function (pu)
	SVGEN_PWM24_TIME  Tc_1;		// Output: reference phase-c switching function (pu)
	void   (*calc)();	    	// Pointer to calculation function
	void   (*calc_dq)();	    // Pointer to calculation function which don`t calculate angle from freq
} SVGEN_PWM24;
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																				
typedef SVGEN_PWM24 *SVGEN_PWM24_handle;


#define SVGEN_PWM24_TIME_DEFAULTS { 0,0,0,0 }

                     
#define SVGEN_PWM24_DEFAULTS { 0,0,0,0,0,0,0,0,0, \
			{0}, 0,0,0,0,0,0,0,0,\
			SVGEN_PWM24_TIME_DEFAULTS,SVGEN_PWM24_TIME_DEFAULTS,SVGEN_PWM24_TIME_DEFAULTS, \
			SVGEN_PWM24_TIME_DEFAULTS,SVGEN_PWM24_TIME_DEFAULTS,SVGEN_PWM24_TIME_DEFAULTS, \
			(void (*)(unsigned int))svgen_pwm24_calc, (void (*)(unsigned int))svgen_pwm24_calc_dq }

// extern int ar_sa_a[3][4][7];

extern SVGEN_PWM24 svgen_pwm24_1; 
extern SVGEN_PWM24 svgen_pwm24_2;

extern SVGENDQ svgen_dq_1;
extern SVGENDQ svgen_dq_2;

// extern _iq pidCur_Kp;
// extern _iq pidCur_Ki;

// extern _iq iq_alfa_coef;

// extern _iq iq_koef_mod_korrect_1;
// extern _iq iq_koef_mod_korrect_2;

//extern int freq_array[COUNT_VAR_FREQ];

void svgen_pwm24_calc(SVGEN_PWM24 *vt);
void svgen_pwm24_calc_dq(SVGEN_PWM24 *vt);

void init_alpha_pwm24(int xFreq);
void test_calc_pwm24(_iq uz1, _iq uz2, _iq fz1/*, _iq fz2, int revers*/);
void calc_arr_tph(int sector,int region, _iq iq_ttt0, _iq iq_ttt1, 
				 _iq iq_ttt2, _iq iq_ttt3, _iq iq_ttt4, _iq iq_ttt5, _iq delta_t, unsigned int number_sv,
				 _iq iqIa, _iq iqIb, _iq iqIc);
_iq calc_delta_t(_iq delta_1, unsigned int number,int region);

//void change_freq_pwm(_iq FreqMax, int freq_pwm_xtics, _iq XilinxFreq);
void change_freq_pwm(_iq freq_pwm_xtics);

//void calc_freq_pwm();

void calc_time_one_tk(_iq gain, _iq teta, _iq delta_U,
					  _iq Ia, _iq Ib, _iq Ic,
					  unsigned int number, 
					  SVGEN_PWM24_TIME *tk0, 
					  SVGEN_PWM24_TIME *tk1,
					  SVGEN_PWM24_TIME *tk2, 
					  SVGEN_PWM24_TIME *tk3,
					  SVGEN_PWM24_TIME *tk4, 
					  SVGEN_PWM24_TIME *tk5);

void test_calc_pwm24_dq(_iq U_zad1, _iq U_zad2,_iq teta);

void svgen_set_time_keys_closed(SVGEN_PWM24 *vt);
void svgen_set_time_middle_keys_open(SVGEN_PWM24 *vt);



void InitVariablesSvgen(unsigned int freq);
//void init_alpha(void);
_iq correct_balance_uzpt_pwm24(_iq Tinput, _iq Kplus);
void recalc_time_pwm_minimal_2_xilinx_pwm24_l(SVGEN_PWM24 *pwm24,
                                            _iq *T0, _iq *T1,
                                            _iq timpuls_corr );
void test_calc_simple_dq_pwm24(_iq uz1, _iq uz2, _iq fz1, _iq fz2, _iq Uzad_max);
void test_calc_dq_pwm24(_iq Ud, _iq Uq, _iq Ud2, _iq Uq2, _iq tetta,_iq Uzad_max);
//void test_calc_simple_uf_pwm24(_iq uz1, _iq uz2, _iq fz1, _iq fz2,_iq Uzad_max);

//void init_freq_array(void);

typedef union {
	unsigned int all;
	struct {
		unsigned int k0;
		unsigned int k1;
		unsigned int k2;
		unsigned int k3;
		unsigned int k4;
		unsigned int k5;
		unsigned int k6;
		unsigned int k7;
		unsigned int k8;
		unsigned int k9;
		unsigned int k10;
		unsigned int k11;
		unsigned int k12;
		unsigned int k13;
		unsigned int k14;
		unsigned int k15;
	}bit;
} UP_OR_DOWN;

extern UP_OR_DOWN up_down;
extern _iq winding_displacement;


#ifdef __cplusplus
	}
#endif

#endif /* _V_PWM24_H */
