#pragma once

#include "bombax.h"

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

//运动子型号
#define MT_CARD_MODEL_1   1  //io模块,无轴 
#define MT_CARD_MODEL_2   2  //NMC3400/3401/1400/1200/1100(R)       4/4/4/2/1 轴  
#define MT_CARD_MODEL_3   3  //NMC5800/5600/1800/1600(R)            6/8轴  
#define MT_CARD_MODEL_4   4  //NMC5160/5120                         12/16 轴 
#define MT_CARD_MODEL_5   5  //LMC3400R/LMC3100R                    激光卡

//输入信号触发模式：限位，急停，通用信号
#define  MT_DISABLE  0  //不使用紧急停止功能
#define  MT_IMD_LOW  1  //低电平触发紧急停止 
#define  MT_DEC_LOW  2  //低电平触发减速停止
#define  MT_IMD_HI   3  //高电平触发紧急停止
#define  MT_DEC_HI   4  //高电平触发减速停止

 // 
 #define MT_MAX_CARD_COUNT   12
 
//通用输入输出转置
#define  MT_FLAG_INV_GIN    1
#define  MT_FLAG_INV_GOUT   (1<<1)
  

//motion time version : base is motion
class BOMBAX_API CMotime :	public CMotion
{
public:
	CMotime(void);
	virtual ~CMotime(void);

	enum MODEL
	{
		MT1400 = 1400,
		MT3400 = 3400,
	};

	enum PULSE_MODE
	{
		MT_PM_HIGH    =0,//脉冲方向(默认)
		MT_PM_LOW,       //脉冲方向
		MT_PM_CW_CCW,    //双脉冲
		MT_PM_CCW_CW,    //双脉冲
		MT_PM_AB,        //AB相位
		MT_PM_BA,        //AB相位
	};

	//紧急停止
	enum EMG_MODE{
	    MT_EM_CLOSE    =    0,    //不使用紧急停止功能
        MT_EM_LOW_IMD  =    1 ,   //低电平触发紧急停止
        MT_EM_LOW_DEC   =   2 ,   //低电平触发减速停止
        MT_EM_HIGH_IMD =    3 ,   //高电平触发紧急停止
        MT_EM_HIGH_DEC =    4 ,   //高电平触发减速停止
	};

	//回零状态
	enum HOME_STATE{
		HS_COMPLETE =0,  //已经完成
		HS_WRONG = 31,   //回原点错误
        HS_BUZY  = 32,   //正在回原点
	};
	 
	//axisCount为轴的总数量
	virtual BOOL  Init(int  axisCount, const char* strArgs, int extra1/*m_nReverseFlags*/, int  extra2 ,  int* pErrCode) ;
	virtual void  Release() ;
//	virtual BOOL  IsDeviceReady() ;

	virtual BOOL  SetSioBit(WORD axis, int id, int bit, BOOL bReverse, int* pErrCode);
	 
	//轴专用IO输出
	virtual BOOL  SetSioLevel(WORD axis, int id, int level, int* pErrCode) ;
	//轴获取专用IO状态
	virtual BOOL  GetSioLevel(WORD axis, int id, int* pLevel, int* pErrCode);
	 
	//减速比，光栅分辨率,软正负限位等
	virtual BOOL  SetSioValue(WORD axis, int id,   double value,   int* pErrCode) ;
	virtual BOOL  GetSioValue(WORD axis, int id, double* value, int* pErrCode) ;

	//使能SIO,如软限位
	virtual BOOL  EnableSio(WORD axis, int id, BOOL flag,   int* pErrCode) ;
	virtual BOOL  IsSioEnabled(WORD axis, int id,  BOOL* flag, int* pErrCode) ;

	//分配专用io 
	//type: RM_SIO_HOME
	//virtual BOOL  ConfigSio(WORD axis, int type, int bit, BOOL bReverse, int* pErrCode);
	//软限位位置,正负分别配置:, id, sub flag, 
	//virtual BOOL  ConfigSio(WORD axis, int id, int sub, double value,   int* pErrCode);

	//轴专用IO输出
	//virtual BOOL  SetSioLevel(WORD axis, int id, int level, int* pErrCode);
	//轴获取专用IO状态
	//virtual BOOL  GetSioLevel(WORD axis, int id, int* pLevel, 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 GetEmergLevel(int bit, int* level, int* pErrCode);

	//设置脉冲模式: 
	virtual BOOL  SetPulseMode(WORD axis, int mode,  int* pErrCode) ;
	virtual BOOL  GetPulseMode(WORD axis, int* mode,  int* pErrCode) ;

	 
	//脉冲当量: 每毫米多少脉冲 
	virtual BOOL  SetAxisUnits(WORD axis, float units, int* pErrCode);
	virtual BOOL  GetAxisUnits(WORD axis, float* units,  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  Reset(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) {return FALSE; } ;
	 
	virtual COptions* CreateOptions( double speed, double acc, double dec ) ;
	virtual void DestroyOptions(COptions* pOptions) ;

//公用接口
public:
	virtual BOOL   ClearAxisPos(WORD axis,  int posType, 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 posType, 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   EnableAxisServo(WORD axis,   BOOL flag, int* pErrCode);
	//virtual BOOL   IsAxisServoEnabled(WORD axis, BOOL* flag, int* pErrCode) ;

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


	virtual BOOL   Delay(int duration, int* pErrCode);
	 
	//@512  回零位 
	virtual BOOL  Home(WORD axis, COptions* options, int* pErrCode) ; 
	virtual BOOL  StopHome(WORD axis, COptions* options, int* pErrCode) ;
	  
	//@528 连续运动 
	virtual BOOL Jog(WORD axis,  COptions* opts,  int dir, int* pErrCode) ;

	//连续运动停止
	virtual BOOL StopJog(WORD axis,  COptions* opts,  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, double speed, double acc, double acc2, int* pErrCode) ;
	//virtual BOOL MoveCircleAny2(CAxes& axes, double cx, double cy, double sx, double sy, int direct, double speed, double acc, double acc2, int* pErrCode) ;
	//virtual BOOL MoveArcTo(WORD axisX, WORD axisY,  double cx, double cy, double ex, double ey, int direct, COptions* options, int* pErrCode)  ;
	//virtual BOOL MoveLineTo(WORD axisX, WORD axisY, double ex, double ey, 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)  ;
	 

	//任意2轴移动,使用轴标识 
	 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 StopAxisAny4(CAxes& axes,  COptions* options, int* pErrCode);
	// virtual BOOL  StopAxisAny(CAxes&  axes, COptions* options,  int* pErrCode) ;
	virtual BOOL StopAny( DWORD csid, 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 PauseQueue(CQueue* pQueue, BOOL bResume, COptions* options, int* pErrCode) ;
	 

public:
	virtual BOOL SetOutputLevel(int bit, int level, int* pErrCode) ;
    virtual BOOL GetOutputLevel(int bit, int* level, int* pErrCode) ;
    virtual BOOL GetInputLevel(int bit, int* level, int* pErrCode) ; 
	  
	virtual BOOL SetOutputLevel(UINT slave,  int bit, int level, int* pErrCode) ;
	virtual BOOL GetOutputLevel(UINT slave,  int bit, int* level, int* pErrCode) ;
	virtual BOOL GetInputLevel(UINT slave,   int bit, int* level, int* pErrCode)  ; 

	virtual BOOL  AddFio(int id,  int bit, BOOL bReverse, int* pErrCode);
	virtual void  RemoveFio(int id);
public:
	 
	virtual const char* GetVersion()  ;
	virtual const char* GetVendorDesc() ;
    virtual const char* GetErrorDesc(int errCode);
	virtual const char* GetSerialNum() ;

	virtual UINT GetDefaultSlaveId();

protected:
	 
	//运动卡专门函数 
	//bit base 1
	UINT FindSlaveIdOfInputBit(int bit); 
	UINT FindSlaveIdOfOutputBit(int bit); 

	BOOL  MoveAny2(DWORD csid, CAxes& axes,  double dx, double dy, COptions* options, BOOL bAbsolute, int* pErrCode);
	BOOL  MoveAny3(DWORD csid, CAxes& axes, double dx, double dy, double dz, COptions* options, BOOL bAbsolute, int* pErrCode);
	BOOL  MoveAny4(DWORD csid, CAxes& axes, double dx, double dy, double dz, double dr, COptions* options, BOOL bAbsolute, int* pErrCode);

	//批量输入输出
	int  SetOutputBits(UINT slave,  unsigned long levels);
	int  GetInputBits(UINT slave,  unsigned long* low, unsigned long* hi);
	
	int  SetOutputBit(UINT slave,int bit, int leve );
	int  GetInputBit(UINT slave,int bit, int* level, int timeout);
	int  GetOutputBit(UINT slave,int bit, int* level) ;
	//int  SetEmergBit(UINT slave,int bit, int type );
	int  SetPulseMode(UINT slave, WORD axis, int mode);
	int  GetPulseMode(UINT slave, WORD axis, int* mode);
	int  GoHome_(WORD axis, int speed, int creep, int evenTime, int offset );
	int  GetHomeState(UINT slave, WORD axis, int* state) ;
	int  GetMoveState(UINT slave, WORD axis, int* state);
	int  Move(WORD axis, int dist, int profile, int speed, int acc, int acc2, BOOL bAbsolute);
	int  StopAxis(WORD axis, int mode, int dec, int dec2 ,int profile );
	int  StopAxisMulti(DWORD coordId, DWORD slaveId, int mode, int dec, int dec2, int profile);

	//多轴移动
	//任意多轴部支持
	//int MoveAny(int coord, unsigned short* axisList, long* distList, int count, int profile, int speed , int acc, int acc2, BOOL bAbsolute);
	int MoveAny2(int coord, int axis1, int axis2, int x, int y, int profile, int speed , int acc, int acc2, BOOL bAbsolute);
	int MoveAny3(int coord, int axis1, int axis2, int axis3, int x, int y, int z, int profile, int speed , int acc, int acc2, BOOL bAbsolute);
	int MoveAny4(int coord, int axis1, int axis2, int axis3, int axis4, int x, int y, int z, int r, int profile, int speed , int acc, int acc2, BOOL bAbsolute);

	 
	////输入输出
	//int GetInputBit(int bit, int* level, int timeout);
	//int GetOutputBit(int bit, int* level) ;
	//int SetOutputBit(int bit, int level) ;

 private:
	int   m_nReverseFlags;
	// short	  m_nCardId  ;
	// short    m_nCardModel  ; 
	 //int      m_nEmergBit ; //急停输入
};

