#pragma once

#include "bombax.h"
 
#include "..\pilelibs\includes\robbase\motion.h"
//#include "..\pilelibs\includes\robbase\Axes.h"
//#include "..\pilelibs\includes\robbase\Options.h"

//大bit地址中offset
#define OFFSET_BIT_START   10     
//
////#define   HOME_MODE             
//#define   GP_HOME_HIGH_VEL_MODE      1
//#define   GP_HOME_LOW_VEL_MODE       0
//
//#define GN_HOME_ERROR_NONE           100
//#define GN_HOME_ERROR_NOT_TRAP_MODE  101
//#define GN_HOME_ERROR_DISABLE        102
//#define GN_HOME_ERROR_ALARM          103
//#define GN_HOME_ERROR_STOP           104
//#define GN_HOME_ERROR_STAGE          105
//#define GN_HOME_ERROR_HOME_MODE       106
//#define GN_HOME_ERROR_SET_CAPTURE_HOME  107
//#define GN_HOME_ERROR_NO_HOME           108
//#define GN_HOME_ERROR_SET_CAPTURE_INDEX  109
//#define GN_HOME_ERROR_NO_INDEX           110
// 
//
//#define MAX_BANK_COUNT    2
//
//typedef struct __gp_bank_params_{
//	int    logicX, logicY, logicZ;
//	double maxSpeed,   maxAcc;
//	int    evenTime;
//    int    maxCount;   //最大加工路径
//	double T; //前瞻系数,1-10之间,见文档
////	void*  pMotionData; //移动数据
//}GP_BANK_PARAMS;


class BOMBAX_API CGenEcat :	public CMotion
{
public:
	CGenEcat(void);
	virtual ~CGenEcat(void);

	
	virtual BOOL  Init(int  axisCount, const char* strArgs, int extra1, int extra2,  int* pErrCode) ;
	virtual void  Release() ;
	virtual BOOL  IsDeviceReady() ;

	//分配专用io 
	//type: RM_SIO_HOME
	//virtual BOOL  ConfigSio(WORD axis, int type, int bit, BOOL bReverse, int* pErrCode);

	//virtual BOOL  SetSoftLimits(WORD axis, int item, double pos, int* pErrCode) ;
	//virtual BOOL  GetSoftLimits(WORD axis, int item, double* pp, int* pErrCode) ; 

	////获取正负限位电平
	//virtual BOOL GetHardLimitLevel(WORD axis,int item, 	int* level, int* pErrCode) ;
	////virtual BOOL GetNeglmtLevel(WORD axis,int item, 	int* level, int* pErrCode) ;

	//virtual BOOL EnableSoftLimit(WORD axis, int item, BOOL flag, int* pErrCode);
	////virtual BOOL IsSoftLimitEnabled(WORD axis, int item, BOOL* flag, int* pErrCode);

	//virtual BOOL EnableHardLimit(WORD axis, int item, BOOL flag, int* pErrCode);
	////virtual BOOL IsHardLimitEnabled(WORD axis, int item, BOOL* flag, int* pErrCode);



	//获取原点电平
	//virtual BOOL GetHomeLevel(WORD axis, int* level, int* pErrCode) ;
	//virtual BOOL SetHomeParams(WORD axis, RM_HOME_PARAMS* pHomeParams, int * pErrCode);

	//获取急停位电平,紧急停止电平
	//virtual BOOL GetEmergLevel(int bit, int* level, int* pErrCode);
	 

	//设置脉冲模式: 
	//如： mode = 256*100 + 0 表示100微妙的保护时间，模式为0
	//返回0, 表示成功 
	//virtual BOOL  SetPulseMode(WORD axis, int mode,  int* pErrCode) ;
	//virtual BOOL  GetPulseMode(WORD axis, int* mode,  int* pErrCode) ;

	//设置轴类型:0. 虚拟; 1. 脉冲+方向;
	//该类型为厂家自己定义的类型，应用在不同场所 
	//返回0, 表示成功 
	//virtual BOOL  SetAxisVendorType(WORD axis, int type,  int* pErrCode) ;
	//virtual BOOL  GetAxisVendorType(WORD axis, int* type,  int* pErrCode) ;

	//脉冲当量: 每毫米多少脉冲 
	//virtual BOOL  SetAxisUnits(WORD axis, float units, int* pErrCode);
	//virtual BOOL  GetAxisUnits(WORD axis, float* units,  int* pErrCode);
	

	//virtual BOOL   EnableAxisServo(WORD axis,  BOOL flag, int* pErrCode) ;
	//virtual BOOL   IsAxisServoEnabled(WORD axis, BOOL* state, int* pErrCode)  ;

	//
	virtual BOOL   ClearAxisServoAlarm(WORD axis, int duration, int* pErrCode) ;
	//virtual BOOL   IsAxisServoAlarmOn(WORD axis,BOOL* flag, int* pErrCode) ;

	virtual BOOL   EnterEmergState(COptions* options,int *pErrCode);
	virtual BOOL   ClearEmergState(int *pErrCode);
 
	//virtual BOOL   PauseMove(WORD axis,  int mode,  int* pErrCode) ;
	//virtual BOOL   ResumeMove(WORD axis,  int* pErrCode)  ;

	virtual BOOL   StopAll(COptions* options,int* pErrCode);

	virtual BOOL  GetCurSpeed(WORD axis, double* speed, int* pErrCode) ;

	virtual BOOL   ClearAxisState(WORD axis,  int* pErrCode) ;
	

	//獲得軸狀態
	virtual BOOL GetAuxState(WORD axis, /*int item,*/ int* state, int* pErrCode)  ;
	//virtual BOOL SetAuxState(WORD axis,int items, int state, int* pErrCode)   ;
	  
		
	virtual BOOL SetOutputLevel(int slave, int offset, int bit, int level, int* pErrCode) ;
	virtual BOOL GetOutputLevel(int slave, int offset, int bit, int* level, int* pErrCode)  ;
	virtual BOOL GetInputLevel(int slave,  int offset, int bit, int* level, int* pErrCode)  ; 

	virtual BOOL SetBatchOutputLevel(int slave, int offset, __int64 levels,   int* pErrCode)   ;
	virtual BOOL GetBatchOutoutLevel(int slave, int offset, __int64* pLevels, int* pErrCode)   ;
	virtual BOOL GetBatchInputLevel(int slave, int offset,  __int64* pLevels,   int* pErrCode) ;
	
	virtual COptions* CreateOptions(double speed, double acc, double dec) ;
	virtual void DestroyOptions(COptions* pOptions) ;
	 
//公用接口
public:
	virtual BOOL   ClearAxisPos(WORD axis, int type,  int* pErrCode) ;

	//当前位置 ,编码器位置, 光栅尺位置 
	virtual BOOL   GetAxisEncoderPos(WORD axis,   double *curpos, int * pErrCode) ;
    virtual BOOL   GetAxisRasterPos(WORD axis,  double *curpos, int * pErrCode) ;
	virtual BOOL   GetAxisCurPos(WORD axis, int type, double *curpos,int * pErrCode  )  ;
	virtual BOOL   GetAxisRawPos(WORD axis, int type, double *curpos,int * pErrCode  );
	//state: 0-运动中; -1-停止
	//常规运动状态下使用
	virtual BOOL   GetMoveState(WORD axis, int* state, int* pErrCode) ;
	//回零状态下使用
	virtual BOOL   GetHomeState(WORD axis, int* state, int* pErrCode) ;


	virtual BOOL   Delay(int duration, int* pErrCode) ;
	 
	//@512  回零位 
	virtual BOOL  Home(WORD axis, COptions* options, int* pErrCode)  ;
	 
	//任意2轴回零,使用轴标识
	//virtual BOOL  HomeAny2(CAxes& axes, COptions* options, int* pErrCode) ;
	////virtual BOOL  HomeAny4(CAxes& axes, COptions* options, int* pErrCode)  ;
	//virtual BOOL  HomeAny3(CAxes& axes, COptions* options, int* pErrCode) ;
	//virtual BOOL  HomeAny(CAxes& axes, COptions* options, int* pErrCode)  ;
	 
		
	virtual BOOL  StopHome(WORD axis, COptions* options, int* pErrCode) ;
	 
	//停止任意2轴回零。
	//如RM_AF_X | RM_AF_Y
	//virtual BOOL  StopHomeAny2(CAxes& axes, COptions* options, int* pErrCode) ;
	//virtual BOOL  StopHomeAny3(CAxes& axes, COptions* options, int* pErrCode) ;
	//virtual BOOL  StopHomeAny (CAxes& axes, COptions* options, int* pErrCode) ;
	  
	//@528 连续运动 
	virtual BOOL Jog(WORD axis,  COptions* options,  int dir, int* pErrCode)  ;

	//连续运动停止
	virtual BOOL StopJog(WORD axis,  COptions* options,  int* pErrCode)  ;
	//end of @528

	//@527 点动
	virtual BOOL  Move(WORD axis, double step, COptions* options, BOOL bAbsolute, int* pErrCode)  ;  	
	virtual BOOL  StopAxis(WORD axis, COptions* options,  int* pErrCode) ;
	

	//@529 插补运动 	 
	//virtual BOOL MoveArcAny2(CAxes& axes, double  ex, double  ey, double radius, BOOL bBigArc, int direct, COptions* options, int* pErrCode)  ;
	//virtual BOOL MoveCircleAny2(CAxes& axes, double cx, double cy, double sx, double sy, int direct, COptions* options, int* pErrCode) ;
	//virtual BOOL MoveLineAny2(CAxes& axes, double  x0, double  y0, double x1, double y1, COptions* options, int* pErrCode)  ;
	//virtual BOOL MoveArcAny2(CAxes& axes,  double  startX, double  startY, double endX, double endY, double radius, BOOL bBigArc, int direct, COptions* options, int* pErrCode)   ;
	//virtual BOOL MoveCircleAny2(CAxes& axes, double centerX, double centerY, double radius, int direct, COptions* options, int* pErrCode)  ;
	virtual BOOL MoveArcTo( DWORD csid,  WORD axisX, WORD axisY, double cx, double cy, double ex, double ey, int direct, COptions* options, int* pErrCode) ;

	virtual BOOL StopAny( DWORD csid, CAxes&  axes, COptions* options,  int* pErrCode) ;

	//任意2轴移动,使用轴标识
	//如RM_AF_X | RM_AF_Y

//	virtual BOOL  MoveAny2(CAxes& axes, double x, double y,  COptions* options, BOOL bAbsolute,  int* pErrCode ) ;
//	virtual BOOL  MoveAny3(CAxes& axes, double x, double y, double z, COptions* options, BOOL bAbsolute, int* pErrCode) ;
	//不支持4轴以上插补
//	virtual BOOL  MoveAny4(CAxes& axes, double x, double y, double z, double r, COptions* options, BOOL bAbsolute, int* pErrCode) ;
	//virtual BOOL  MoveAny (CAxes& axes, double* posArray, int count, COptions* options, BOOL bAbsolute, int* pErrCode) ;

	 virtual BOOL  MoveAny ( DWORD csid, CAxes& axes, double* posArray, int count, COptions* options, BOOL bAbsolute, int* pErrCode) ;

	//virtual BOOL StopAxisAny2(CAxes& axes,  COptions* options, int* pErrCode) ;
	//virtual BOOL StopAxisAny3(CAxes& axes,  COptions* options, int* pErrCode) ;
	//virtual BOOL StopAxisAny (CAxes& axes,  COptions* options, int* pErrCode) ;
	  

	//缓冲区运动
	virtual BOOL ExecuteQueue(  CQueue* pQueue,int startIndex, int count,  COptions* options,   int* pErrCode )  ;
	virtual BOOL GetQueueState(CQueue* pQueue, int* state, int* linenum, int* pErrCode)  ;
	virtual BOOL StopQueue(CQueue* pQueue, COptions* options,   int* pErrCode)    ; 
	virtual BOOL WaitQueue(CQueue* pQueue,int timeout, int* pErrCode) ;
	  

public:
	
	virtual const char* GetVersion()  ;
	virtual const char* GetVendorDesc() ;
    virtual const char* GetErrorDesc(int errCode);
	virtual const char* GetSerialNum() ;
	  
	int  GetCoreId(){ return m_coreId; };
	  
protected:	
	 int StopAxis(WORD axis, int mode);
	int MoveTrap(WORD axis, double dist,  double speed, double acc );

	double  GetMaxMetricInPulsesAny2(int axisX, int axisY, double metric);
	double  GetMaxMetricInPulsesAny3(int axisX, int axisY, int axisZ, double metric);
	double  GetMaxMetricInPulsesAny4(int axisX, int axisY, int axisZ, int axisR, double metric);

	int     InitCoord(int logicX, int logicY, int logicZ, double speedMax, double accMax, int evenTime, BOOL bInvalidAxisZ);

protected:
    int  m_coreId;
	BOOL m_bDeviceReady;
	BOOL m_bReOpen;  //所谓的双开, 用来加速初始化
	
};

