#ifndef V_ROTOR_H
#define V_ROTOR_H

#ifdef __cplusplus
extern "C" {
#endif

#include "params_bsu.h"


// äàò÷èê ïî ÷àñîâîé
#define ROTOR_SENSOR_CODE_CLOCKWISE             2       // ïî ÷àñîâîé
#define ROTOR_SENSOR_CODE_COUNTERCLOCKWISE      1       // ïðîòèâ ÷àñîâîé

// äàò÷èê ïðîòèâ ÷àñîâîé
//#define ROTOR_SENSOR_CODE_CLOCKWISE             1       // ïî ÷àñîâîé
//#define ROTOR_SENSOR_CODE_COUNTERCLOCKWISE      2       // ïðîòèâ ÷àñîâîé


void update_rot_sensors(void);
void RotorMeasure(void);
void RotorMeasurePBus(void);
void rotorInit(void);
void select_values_wrotor(void);


void RotorDirectionFilter(int RotorDirectionIn, int *RotorDirectionOut, int *RotorDirectionOut2, int *count_direction);

int AnalisatorRotorSensorPBus(_iq d1, _iq d2, unsigned int *count_overfull_discret, unsigned int *count_zero_discret, _iq *prev_iqTimeRotor,
								unsigned int *discret_out, unsigned int discret_in, _iq *iqWRotorCalcBeforeRegul, _iq *iqWRotorCalc,
								int modeS1, int modeS2,
								int valid_sensor_direct, int valid_sensor_90,
								unsigned int *error_count);

void RotorMeasureDetectDirection(void);


typedef struct
{
	_iq iqWRotorRaw0;
	_iq iqWRotorRaw1;
	_iq iqWRotorRaw2;
	_iq iqWRotorRaw3;

	_iq iqWRotorFilter0;
	_iq iqWRotorFilter1;
	_iq iqWRotorFilter2;
	_iq iqWRotorFilter3;

	_iq iqWRotorDelta;

	_iq iqTimeSensor1;
	_iq iqTimeSensor2;

	_iq prev_iqWRotorRaw0;
    _iq prev_iqWRotorRaw1;
    _iq prev_iqWRotorRaw2;
    _iq prev_iqWRotorRaw3;

    unsigned int count_zero_discret0;
    unsigned int count_zero_discret1;
    unsigned int count_zero_discret2;
    unsigned int count_zero_discret3;


	_iq iqWRotorCalc1;
	_iq iqWRotorCalcBeforeRegul1;
	_iq iqWRotorCalc2;
	_iq iqWRotorCalcBeforeRegul2;

//	int RotorDirectionInstant;
//	int RotorDirectionSlow;

	_iq iqWRotorImpulses1;
	_iq iqWRotorImpulsesBeforeRegul1;
	_iq iqWRotorImpulses2;
	_iq iqWRotorImpulsesBeforeRegul2;

	_iq iqWRotorSum;
    _iq iqWRotorSumFilter;
    _iq iqWRotorSumFilter2;
    _iq iqWRotorSumFilter3;

    _iq iqWRotorSumRamp;

    _iq iqWRotorCalc1Ramp;
    _iq iqPrevWRotorCalc1;
    _iq iqWRotorCalc2Ramp;
    _iq iqPrevWRotorCalc2;

    int RotorDirectionSlow;

} WRotorValues;

#define WRotorValues_DEFAULTS	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0, 0,0,0,0, 0}

typedef struct
{
	_iq iqWRotorRaw0;
	_iq iqWRotorRaw1;
	_iq iqWRotorRaw2;
	_iq iqWRotorRaw3;


	_iq iqWRotorFilter0;
	_iq iqWRotorFilter1;
	_iq iqWRotorFilter2;
	_iq iqWRotorFilter3;

	_iq iqWRotorDelta;
//	_iq iqWRotorCalcBeforeRegul;

	_iq iqWRotorCalcBeforeRegul1;
	_iq iqWRotorCalcBeforeRegul2;

	_iq iqTimeRotor1;
	_iq iqTimeRotor2;

	_iq iqTimeRotor;


	_iq iqWRotorCalc1;
	_iq iqWRotorCalc2;
//	_iq iqWRotorCalc;

	int ModeAutoDiscret;

	int RotorDirectionInstant;
	int RotorDirectionSlow;
    int RotorDirectionCount;
    int RotorDirectionSlow2;



#if (ENABLE_ROTOR_SENSOR_ZERO_SIGNAL==1)
    _iq iqWRotorRawAngle1F;
    _iq iqWRotorRawAngle1R;
    _iq iqWRotorRawAngle2F;
    _iq iqWRotorRawAngle2R;

    _iq iqAngle1F;
    _iq iqAngle1R;

    _iq iqAngle2F;
    _iq iqAngle2R;

    _iq iqAngleCalc;

#endif

} WRotorValuesAngle;

#if (ENABLE_ROTOR_SENSOR_ZERO_SIGNAL==1)
#define WRotorValuesAngle_DEFAULTS  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
#else
#define WRotorValuesAngle_DEFAULTS  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
#endif

extern WRotorValues WRotor;
extern WRotorValuesAngle WRotorPBus;






//#define NORMA_WROTOR 15

#define IQ_WROTOR_MAX_MIN_DELTA 3744914286  //17920


#define WRMP_COEF 0.001 // 0.24 Hz per sec

#define IQ_CONST_3 50331648

#define COUNT_DECODER_ZERO_WROTORPBus 65535 //0x00fe5000//0x01fca000
#define COUNT_DECODER_ZERO_WROTOR 65500 //0x00fe5000//0x01fca000
#define COUNT_DECODER_MAX_WROTOR 10

#ifdef __cplusplus
}
#endif

#endif