/* ==============================================================================
File Name:	ecs_microstep.h

System Name: Micro stepping bipolar drive for two-phase stepping motor

Description:	Primary header file

Originator:		ECS Development

Target:  TMS320F2808

=====================================================================================
 History:
-------------------------------------------------------------------------------------
 01-15-2005	Release	Rev 1.0 : Original release
=================================================================================  */

#ifndef __ECS_MICROSTEP_H__
#define __ECS_MICROSTEP_H__

#include "IQmathLib.h"
#include "build.h"

#include "park.h"
#include "ipark.h"
#include "pid.h"
#include "ang_gen.h"
#include "ramp.h"
#include "zoh.h"

#ifndef TRUE
#define FALSE 0
#define TRUE  1
#endif

// Define the base quantites
#define BASE_FREQ      	50.0           // Base electrical frequency (Hz)

#define PI 3.14159265358979
/*------------------------------------------------------------------------------
 Define the structure of the MICROSTEP Object. This object has all the data necessary 
 for one instance of a MICROSTEP algorithm.
------------------------------------------------------------------------------*/

// Mode=0 (speed mode), Mode=1 (torque mode), Mode=2 (position mode)
// Direction=0 (Forward), Direction=1 (Reverse)
typedef struct {  unsigned int   Mode; 
                  unsigned int   Dir;    
                           _iq   Spd;
                           _iq   T;
				           _iq   Pos;
				           _iq   Id;
				           _iq   Iq;
				           _iq   Ipeak;
			   } REFERENCE;

typedef struct {  _iq   Spd;
				  _iq   Pos;
				  _iq   Ia;
				  _iq   Ib;
			  } FEEDBACK;

typedef struct {  _iq   In;
				  _iq   Out;
				  _iq   Gain;
			  } LPF;

typedef struct {  _iq   SinDisc;
				  _iq   CosDisc;
			  } VREFENCE;

typedef struct {
              REFERENCE  Ref;
               FEEDBACK  Fdb;        
                    LPF  Lpf_Ipeak;        
               VREFENCE  Vref;    
                   PARK  Park_Idq;
                  IPARK  Ipark_Vdq;
                    PID  PI_Pos;
                    PID  PI_Spd;
                    PID  PI_Id;
                    PID  PI_Iq;
                    PID  PI_Ipeak;
			       RAMP  Ramp;
                 ANGGEN  AngGen;
                    ZOH  Zoh_Ang;
             } MICROSTEP;

#define MICROSTEP_DEFAULTS  { { 0,0,0,0,0,0,0,0 }, { 0,0,0,0 }, \
                              { 0,0,_IQ(0.9) }, { 0,0 }, \
                              PARK_DEFAULTS,  \
                              IPARK_DEFAULTS,  \
                              PID_DEFAULTS,  \
                              PID_DEFAULTS,  \
                              PID_DEFAULTS,  \
                              PID_DEFAULTS,  \
                              PID_DEFAULTS,  \
							  RAMP_DEFAULTS,  \
							  ANGGEN_DEFAULTS,   \
							  ZOH_DEFAULTS  \
                            }

/*------------------------------------------------------------------------------
 Prototypes for functions implemented in ECS_MICROSTEP.C  
------------------------------------------------------------------------------*/
void MICROSTEP_Init(MICROSTEP *);
void MICROSTEP_Update(MICROSTEP *);

#endif /*__ECS_MICROSTEP_H__ */

