#pragma once

#include "bombax.h"

#include "..\pilelibs\includes\robbase\robmot.h"


//#define   HOME_MODE             
#define   GP_HOME_HIGH_VEL_MODE      1
#define   GP_HOME_LOW_VEL_MODE       0

#define GP_HOME_ERROR_NONE           100
#define GP_HOME_ERROR_NOT_TRAP_MODE  101
#define GP_HOME_ERROR_DISABLE        102
#define GP_HOME_ERROR_ALARM          103
#define GP_HOME_ERROR_STOP           104
#define GP_HOME_ERROR_STAGE          105
#define GP_HOME_ERROR_HOME_MODE       106
#define GP_HOME_ERROR_SET_CAPTURE_HOME  107
#define GP_HOME_ERROR_NO_HOME           108
#define GP_HOME_ERROR_SET_CAPTURE_INDEX  109
#define GP_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 CGtsPci :	public CRobMot
{
public:
	CGtsPci(void);
	virtual ~CGtsPci(void);

	virtual BOOL  Init(const char* pStrFile, int frameWork, int ctrlMode,   int* pErrCode)  ;
	virtual void  Release();


	//设置软限位
	virtual BOOL  SetSoftLimits(int axis, double neg, double pos, int* pErrCode) ;
	virtual BOOL  GetSoftLimit(int axis, double* np, double* pp, int* pErrCode) ; 

	//获取正负限位电平
	virtual BOOL GetPositiveLimitBit(int axis,	int* level, int* pErrCode) ;
	virtual BOOL GetNegativeLimitbit(int axis,	int* level, int* pErrCode) ;

	//获取原点电平
	virtual BOOL GetHomeBit(int axis, int* level, int* pErrCode) ;

	//急停
	virtual BOOL GetEmergBit(int* level, int* pErrCode);

	//设置脉冲模式
	int  SetPulseMode(int axis, int mode);
	int  GetPulseMode(int axis, int* mode);

	//设置控制模式: 开环， 闭环
	int  SetCtrlMode(int axis, int mode);
	int  GetCtrlMode(int axis, int* mode);


	virtual const char* GetVersion()  ;
	virtual const char* GetVendorDesc() ;
    virtual const char* GetErrorDesc(int errCode);
	virtual const char* GetSerialNum() ;

	virtual BOOL  EnterEmerg(int *pErrCode);
	//virtual BOOL  Reset(int *pErrCode);
	virtual BOOL   ResetAxisState(int axis,  int* pErrCode) ;

	virtual BOOL   EnableAxisServo(int axis, BOOL flag, int* pErrCode);
	virtual BOOL   SetPosCounterType(int axis, int type, int* pErrCode);

	virtual BOOL MoveRaw(int axis, int step, int* pErrCode);
	virtual BOOL HomeRaw(int axis,  int* pErrCode) ;


	//单点设置
    virtual BOOL SetOutputBit(int bit, int level, int* pErrCode) ;
    virtual BOOL GetOutputBit(int bit, int* level, int* pErrCode) ;
    virtual BOOL GetInputBit(int bit, int* level, int* pErrCode) ; 

	//批量设置
	//virtual BOOL SetOutputBits(int lowLevels, int hiLevels, int* pErrCode) ;
 //   virtual BOOL GetOutputBits(int* lowLevels, int* hiLevels, int* pErrCode) ; 
	//virtual BOOL GetInputBits(int* lowLevels, int* hiLevels, int* pErrCode) ; 
	virtual BOOL SetBatchOutBits(int levels, int* pErrCode) ;
    virtual BOOL GetBatchOutBits(int* pLevels, int* pErrCode) ;
	virtual BOOL GetBatchInBits(int* pLevels, int* pErrCode) ;


	//ClearAxisServoAlarm：发送信号给驱动器，清除报警信号
	virtual BOOL   ClearAxisServoAlarm(int axis,int* pErrCode) ;
	//GetAxisServoAlarm： 获取驱动器发来的报警信号
	virtual BOOL   GetAxisServoAlarm(int axis,int* state, int* pErrCode);

	virtual BOOL   GetAxisState(int axis, int* state, int* pErrCode);
	virtual BOOL   GetAxisCurPos(int axis, double *curpos, int * pErrCode);

	virtual BOOL   ClearAxisPos(int axis, int posType, int* pErrCode);
	virtual BOOL   GetAxisRawPos(int axis, int posType, long *curpos, int * pErrCode) ;
    //目前不支持光栅尺
	//virtual BOOL   GetAxisRasterPos(int axis, double *curpos, int * pErrCode);


	//编码器相关
//	virtual BOOL InvertEncoder(int axis, int* pErrCode);
	virtual BOOL SetEncoderLogic(int axis, int level,  int* pErrCode);
	//scalar : alpha << 16 | beta
	virtual BOOL SetEncoderScalar(int axis, int scalar, int* pErrCode);

public:
	
	virtual BOOL  GoHome(int axis, double speed, double maxSpeed, double acc, int* pErrCode) ;
    virtual BOOL  GoHomeXY(double speed, double maxSpeed,double acc, int* pErrCode) ;
    virtual BOOL  GoHomeXY2(double speed, double maxSpeed,double acc, int* pErrCode) ;

    virtual BOOL  GoHomeXYY2(double speed, double maxSpeed,double acc, int* pErrCode) ;
	//x,两个y轴
	//GoHomeX2Y与GoHomeXYY2重复
	//virtual BOOL  GoHomeX2Y(double speed, double maxSpeed,double acc, int* pErrCode) ;
	virtual BOOL  GoHomeXYZ(double speed, double maxSpeed,double acc, int* pErrCode) ;
	//4个轴
	virtual BOOL  GoHome4A(double speed, double maxSpeed,double acc, int* pErrCode) ;

	//任意2轴回零,使用轴标识
	//如RM_AF_X | RM_AF_Y
	virtual BOOL  GoHomeAny2(int axes, double speed, double maxSpeed, double acc, int* pErrCode) ;
	//virtual BOOL  GoHomeAny4(int axes, double speed, double maxSpeed, double acc, int* pErrCode) ;


	virtual BOOL  StopHome(int axis, int mode, double dec, double dec2,int* pErrCode) ;
	virtual BOOL  StopHomeXY( int mode, double dec, double dec2,int* pErrCode) ;
	virtual BOOL  StopHomeXYY2(int mode, double dec, double dec2,  int* pErrCode) ;
	virtual BOOL  StopHomeXYZ(int mode, double dec, double dec2,  int* pErrCode) ;

	//停止任意2轴回零。
	//如RM_AF_X | RM_AF_Y
	virtual BOOL  StopHomeAny2(int axes, int mode, double dec, double dec2,int* pErrCode);

	virtual BOOL  GetHomeState(int axis, int* state, int* pErrCode) ;


	//点动
	//移动到相对位置,dist为负的话，反方向移动
	virtual BOOL Move(int axis, double dist, double speed, double acc, double acc2,int* pErrCode) ;

	//移动到具体位置
	virtual BOOL MoveTo(int axis, double targetPos, double speed, double acc, double acc2,int* pErrCode) ;
	
	//RM_IMD: 立即停止，RM_SMOOTH，平滑停止
	virtual BOOL StopAxis(int axis, int mode, double dec, double dec2, int* pErrCode) ;


	//连续运动
	virtual BOOL Jog(int axis,  double speed, double acc, double acc2, int* pErrCode) ;

	//连续运动停止
	virtual BOOL StopJog(int axis, int mode, double dec, double dec2,  int* pErrCode) ;
	
	//插补
	//相对运动
	virtual BOOL MoveXY(double dx, double dy, double speed, double acc, double acc2, int* pErrCode) ;
	virtual BOOL MoveXYZ(double dx, double dy, double dz, double speed, double acc, double acc2, int* pErrCode) ;
	
	//绝对位置
	virtual BOOL MoveToXY(double x, double y, double speed, double acc, double acc2, int* pErrCode) ;
	virtual BOOL MoveToXY2(double x, double y, double speed, double acc, double acc2, int* pErrCode) ;

	virtual BOOL MoveToXYZ(double x, double y, double z, double speed, double acc, double acc2, int* pErrCode) ;
	virtual BOOL MoveToXY2Z(double x, double y, double z, double speed, double acc, double acc2, int* pErrCode) ;

	virtual BOOL MoveArc(double  ex, double  ey, double radius, BOOL bBigArc,  int direct, double speed, double acc, double acc2, int* pErrCode) ;
	
	//cx, cy为起点的相对位置
	virtual BOOL MoveCircle(double cx, double cy, double sx, double sy, int direct, double speed, double acc, double acc2, int* pErrCode) ;

	virtual BOOL MoveArcXY2(double  ex, double  ey, double radius, BOOL bBigArc, int direct, double speed, double acc, double acc2, int* pErrCode) ;
	//cx, cy为起点的相对位置
	virtual BOOL MoveCircleXY2(double cx, double cy, double sx, double sy, int direct, double speed, double acc, double acc2, int* pErrCode) ;
	
	virtual BOOL MoveArcAny2(int axes, double  ex, double  ey, double radius, BOOL bBigArc, int direct, double speed, double acc, double acc2, int* pErrCode) ;
	
	//cx, cy为起点的相对位置
	virtual BOOL MoveCircleAny2(int axes, double cx, double cy, double sx, double sy, int direct, double speed, double acc, double acc2, int* pErrCode) ;

	
	virtual BOOL MoveArcXYZ(double  ex, double  ey, double z, double radius, BOOL bBigArc, int direct, double speed, double acc, double acc2, int* pErrCode) ;
	virtual BOOL MoveCircleXYZ(double cx, double cy, double z,  double sx, double sy, int direct, double speed, double acc, double acc2, int* pErrCode);

	virtual BOOL MoveArcXY2Z(double  ex, double  ey, double z, double radius, BOOL bBigArc, int direct, double speed, double acc, double acc2, int* pErrCode);
	virtual BOOL MoveCircleXY2Z(double cx, double cy, double z,  double sx, double sy, int direct, double speed, double acc, double acc2, int* pErrCode);


	//任意2轴移动,使用轴标识
	//如RM_AF_X | RM_AF_Y
	virtual BOOL  MoveAny2(int axes,  double dx, double dy, double speed, double acc, double acc2, BOOL bAbsolute, int* pErrCode);
	//virtual BOOL MoveArcAny2(int axes, double  ex, double  ey, double radius, BOOL bBigArc, int direct, double speed, double acc, double acc2, int* pErrCode) ;
	//virtual BOOL MoveCircleAny2(int axes, double cx, double cy, double sx, double sy, int direct, double speed, double acc, double acc2, int* pErrCode) ;

	virtual BOOL  MoveAny3(int axes,  double dx, double dy, double dz, double speed, double acc, double acc2, BOOL bAbsolute, int* pErrCode);


	//停止插补运动
	//
	virtual BOOL StopAxisXY(  int mode, double dec, double dec2, int* pErrCode)  ;
	virtual BOOL StopAxisXYZ(  int mode, double dec, double dec2, int* pErrCode) ;
	virtual BOOL StopAxisXY2(  int mode, double dec, double dec2, int* pErrCode);

	//停止任意2轴移动。
	//如RM_AF_X | RM_AF_Y
	virtual BOOL StopAxisAny2(int axes,  int mode, double dec, double dec2, int* pErrCode);
	virtual BOOL StopAxisAny3(int axes,  int mode, double dec, double dec2, int* pErrCode);

	virtual BOOL StopMultiAxes(int coord,  int mode, double dec, double dec2, int* pErrCode) ;

	virtual BOOL Delay(int duration, int* pErrCode);

	//缓冲区执行
	//virtual BOOL OpenBank(int id, int* pErrCode) ;
	//virtual BOOL SubmitBank(int id,  int* linecount,int* pErrCode) ;
	//virtual BOOL ExecuteBank(int id, int mode, int* pErrCode ) ;
	//virtual BOOL GetBankState(int id, int* state, int* linenum, int* pErrCode)  ;
	//virtual BOOL StopBank(int id, int mode, double dec, double dec2, int* pErrCode) ;
	//virtual BOOL PlanBank(int id, int profile, double speed, double acc, double acc2,  int* pErrCode) ;
	virtual BOOL OpenBank(int id, void* pParams, int paramSize,  int* pErrCode) ;
	virtual BOOL SubmitBank(int id,  int* linecount,int* pErrCode) ;
	virtual BOOL ExecuteBank(int id, /*int mode,*/ int* pErrCode ) ;
	virtual BOOL GetBankState(int id, int* state, int* linenum, int* pErrCode) ;
	virtual BOOL StopBank(int id,  int mode, double dec, double dec2,  int* pErrCode) ;
	virtual BOOL PlanBank(int id,    double speed, double acc, double acc2,   int* pErrCode) ;
	virtual void CloseBank(int id);


public:
	
	//功能项
	virtual BOOL GetAuxState(int axis, int items, int* state, int* pErrCode)  ;
	virtual BOOL SetAuxState(int axis,int items, int state, int* pErrCode) ;

	//单点设置
    int SetOutputBit(int bit, int level) ;
    int GetOutputBit(int bit, int* level) ;

    int  GetInputBit(int bit, int* level, int timeout=1200) ; 

	//批量设置
	int SetOutputBits(unsigned long levels) ;
    int GetInputBits(unsigned long* pLevels) ; 
    int GetOutputBits(unsigned long* pLevels) ; 


	//限位
	 int SetSoftLimit(int axis, long np, long pp) ; 
	 int GetSoftLimit(int axis, long* np, long* pp) ; 

	//获取正负限位电平
	// int GetPositiveLimitBit(int axis,	int* level) ;
	// int GetNegativeLimitbit(int axis,	int* level) ;

	//获取原点电平
	 int GetHomeBit(int axis, int* level) ;

	int EnableSoftLimit(int axis);
	int EnableSoftLimit(int axis, long np, long pp);

	
	int  ClearAxisState(WORD axis);

protected:
	   
	 int GetServoAlarmState(int axis, int* pState);
	 int ResetServoAlarm(int axis);

	 //根据当前位置计数器获取当前轴位置
	 //单位：脉冲
	 //返回：错误码
	 int GetAxisRawPos(int axis, double *curpos ) ;

	 //输入值应该为脉冲，不是毫米.
	 //logicX输入轴号
	 int InitCoord(int logicX, int logicY, int logicZ, double speedMax, double accMax, int evenTime, BOOL bInvalidAxisZ=FALSE);

     //int  StopHome(int axis ) ;
	 //int  GetHomeState(int axis, int* state) ;

	 ////点动
	 int  MoveTrap(int axis, double dist,  double speed, double acc );
	 int  StopAxis(int axis, int mode );
	

	 void* m_MotionData; //缓冲区移动数据


};

