#ifndef ESTIM_H
#define ESTIM_H


/**********************************************************************
* © 2012 Microchip Technology Inc.
*
* SOFTWARE LICENSE AGREEMENT:
* Microchip Technology Incorporated ("Microchip") retains all ownership and 
* intellectual property rights in the code accompanying this message and in all 
* derivatives hereto.  You may use this code, and any derivatives created by 
* any person or entity by or on your behalf, exclusively with Microchip's
* proprietary products.  Your acceptance and/or use of this code constitutes 
* agreement to the terms and conditions of this notice.
*
* CODE ACCOMPANYING THIS MESSAGE IS SUPPLIED BY MICROCHIP "AS IS".  NO 
* WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 
* TO, IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A 
* PARTICULAR PURPOSE APPLY TO THIS CODE, ITS INTERACTION WITH MICROCHIP'S 
* PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION. 
*
* YOU ACKNOWLEDGE AND AGREE THAT, IN NO EVENT, SHALL MICROCHIP BE LIABLE, WHETHER 
* IN CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE OR BREACH OF STATUTORY DUTY), 
* STRICT LIABILITY, INDEMNITY, CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, SPECIAL, 
* PUNITIVE, EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, FOR COST OR EXPENSE OF 
* ANY KIND WHATSOEVER RELATED TO THE CODE, HOWSOEVER CAUSED, EVEN IF MICROCHIP HAS BEEN 
* ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT 
* ALLOWABLE BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO 
* THIS CODE, SHALL NOT EXCEED THE PRICE YOU PAID DIRECTLY TO MICROCHIP SPECIFICALLY TO 
* HAVE THIS CODE DEVELOPED.
*
* You agree that you are solely responsible for testing the code and 
* determining its suitability.  Microchip has no obligation to modify, test, 
* certify, or support the code.
*
*******************************************************************************/


typedef struct {
    int   			  qDeltaT;      // Integration constant
    int   			  qRho;    	    // angle of estimation
    long  			  qRhoStateVar; // internal variable for angle
    int   			  qOmegaMr;     // primary speed estimation
    int   			  qLastIalpha;  // last value for Ialpha
    int   			  qLastIbeta;   // last value for Ibeta
    int   			  qDIalpha;     // difference Ialpha
    int   			  qDIbeta;      // difference Ibeta
	int				  qEsa;			// BEMF alpha
	int				  qEsb;			// BEMF beta
	int				  qEsd;			// BEMF d
	int				  qEsq;			// BEMF q
	int				  qDiCounter;	// counter in Last DI tables
	int				  qVIndalpha;   // dI*Ls/dt alpha
	int				  qVIndbeta;    // dI*Ls/dt beta
	int				  qEsdf;        // BEMF d filtered
	long			  qEsdStateVar; // state var for BEMF d Filtered
	int				  qEsqf;        // BEMF q filtered
	long			  qEsqStateVar; // state var for BEMF q Filtered
	int				  qKfilterEsdq; // filter constant for d-q BEMF
	int   			  qVelEstim; 			// Estimated speed 
	int   			  qVelEstimFilterK; 	// Filter Konstant for Estimated speed 
	long   			  qVelEstimStateVar; 	// State Variable for Estimated speed 
    int   			  qLastValpha;  // Value from last control step Ialpha 
    int   			  qLastVbeta;   // Value from last control step Ibeta
	int				  qDIlimitLS;			// dIalphabeta/dt
	int				  qDIlimitHS;			// dIalphabeta/dt
	int				  qLastIalphaHS[8];		//  last  value for Ialpha
	int				  qLastIbetaHS[8];			// last  value for Ibeta
	int               RhoOffset;            // estima angle init offset

    } tEstimParm;


typedef struct {
	int				   qRs;			// Rs value - stator resistance
	int				   qLsDt;		// Ls/dt value - stator inductand / dt - variable with speed
	int				   qLsDtBase;	// Ls/dt value - stator inductand / dt for base speed (nominal)
	int				   qInvKFi;	    // InvKfi constant value ( InvKfi = Omega/BEMF )
	int				   qInvKFiBase; // InvKfi constant - base speed (nominal) value
    } tMotorEstimParm;

	

extern tEstimParm 	EstimParm;
extern tMotorEstimParm 	MotorEstimParm;

//------------------  C API for Control routine ---------------------

void	Estim(void);
void	InitEstimParm(void);

#endif
