#pragma once

#include "bombax.h"
#include "..\pilelibs\includes\robbase\robmot.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                    激光卡



//Motion Time
//速度规划曲线
//速度规划曲线
#define   MT_PT_T  0 //		MO_PT_T=1,
#define   MT_PT_S  1 //		MP_PROFILE_S=1,

//缓冲区运动速度执行规划方式
#define   MT_BM_DEFAULT  0
#define   MT_BM_SMOOTH   1


typedef struct __mt_bank_params_{
	int    profile;
	double speed;
	double acc; 
	double acc2;
	int    runMode;  //MT_BM_DEFAULT

	int    stopMode;     //MT_PT_T
	double dec;
	double dec2;
}MT_BANK_PARAMS;




class BOMBAX_API CMotionTime :	public CRobMot
{
public:
	CMotionTime(void);
	virtual ~CMotionTime(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,   //正在回原点
	};

	virtual BOOL  Init( int* ids, int* models, int count,  int* pErrCode) ;
	virtual BOOL  Init(int id, int model, int frameWork,  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 SetHomeParams(int axis, RM_HOME_PARAMS* pHomeParams, int * pErrCode);

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

	//设置脉冲模式
	int  SetPulseMode(int axis, int mode);
	int  GetPulseMode(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 MoveRaw(int axis, int step, int* pErrCode);
	virtual BOOL HomeRaw(int axis,  int* pErrCode) ;

	virtual BOOL GetTriggerMode(int axis, RM_TRIGGER_PARAMS* pTrigerParams, int paramSize, int * pErrCode){ return FALSE; };
    virtual BOOL SetTriggerMode(int axis, RM_TRIGGER_PARAMS* pTrigerParams, int paramSize, int * pErrCode);
 

//公用接口
public:
	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   GetAxisCurPos(int axis, double *curpos,int * pErrCode =NULL) ;
	virtual BOOL   GetAxisState(int axis, int* state, int* pErrCode);
	virtual BOOL   EnableAxisServo(int axis,  BOOL flag, int* pErrCode);
	virtual BOOL   ClearAxisServoAlarm(int axis,int* pErrCode) ;
	virtual BOOL   GetAxisServoAlarm(int axis,int* state, int* pErrCode) ;
	virtual BOOL   GetAxisServoState(int axis, int* state, int* pErrCode) ;

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

	//virtual BOOL  ActTool(int bit, int* pErrCode) ;
	//virtual BOOL  DeactTool(int bit, int* pErrCode) ;

	//@512  回零位 
	virtual BOOL  GoHome(int axis, double speed, double maxSpeed, double acc, int* pErrCode) ;

	//x,两个y轴
	//virtual BOOL  GoHomeXYY2(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  GoHomeXYZ(double speed,double maxSpeed, double acc, int* pErrCode) ;
	virtual BOOL  GoHomeXYY2(double speed, double maxSpeed,double acc, int* pErrCode) ;
	
	 //4个轴：x, y, y2, z
	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  GoHomeAny3(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  StopHomeXY2(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) ;
	//end of @512

	//@527 点动
	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);
	virtual BOOL StopAxis(int axis, int mode, double dec, double dec2,  int* pErrCode);
	//end of @527

	//@528 连续运动 
	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) ;
	//end of @528

	//@529 插补运动 
	//相对运动
	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 MoveArc(double  ex, double  ey, double radius, BOOL bBigArc, int direct, double speed, double acc, double acc2, int* pErrCode) ;
	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) ;
	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) ;
	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);



	//绝对位置
	virtual BOOL MoveToXY(double x, double y, double speed, double acc, double acc2,int* pErrCode) ;
	virtual BOOL MoveToXY2(double x, double y2, 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) ;

	//任意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  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) ;
	
	//停止任意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);

	//end of @529


	//缓冲区执行
	//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 void CloseBank(int id) ;
	
	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 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) ; 

	//批量设置,可以获得总共64个信号
	//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) ;

public:
	
	//功能项
	virtual BOOL GetAuxState(int axis, int item, 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* low, unsigned long* hi) ; 

	//设置紧急停止位
	///virtual int SetEmgBit(int bit, int type) ; 

	//限位
	//np - 负方向位置， 
	//pp - 正方向位置
	virtual int SetSoftLimit(int axis, long np, long pp) ; 
	virtual int GetSoftLimit(int axis, long* np, long* pp) ; 

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

	//获取原点电平
	virtual int GetHomeBit(int axis, int* level) ;
	 
//motion time封装的接口
protected:
	 //回零
	 int  GoHome(int axis, int minSpeed, int maxSpeed , int offset);

	 //独立运行，用于多轴同时运动
	 int  SetHomeStopTime(int axis, int stopTime );
	 int  SetHomeSpeed(int axis, int minSpeed, int maxSpeed, int offset );
	 int  StartHome(int axis);
	  
	 //伺服相关
	 int SetServoEnabled(int axis, int level );
	 int GetServoState(int axis, unsigned short* state);

	 int GetServoAlarmState(int axis, unsigned short* pState);
	 int ResetServoAlarm(int axis);

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

	 //点动
	 int  Move(int axis, int dist,  int profile, int speed, int acc, int acc2, BOOL bAbsolute);
	 int StopAxis(int axis, int mode, int dec, int dec2, int profile );

	 //连续运动
	 int Jog(int axis,  int speed, int acc ) ;
	 //连续运动停止,直接调用jog,让速度为0
	 // int StopJog(int axis ) ;

	 //插补运动
	 int MoveXY(int coord, int x, int y, int profile, int speed , int acc, int acc2, BOOL bAbsolute=FALSE);
	 int MoveXY2(int coord, int x, int y2, int profile, int speed , int acc, int acc2, BOOL bAbsolute=FALSE);

	 int MoveAny2(int coord, int axis1, int axis2, int x, int y, int profile, int speed , int acc, int acc2, BOOL bAbsolute=FALSE);
	 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=FALSE);


	 int MoveXYZ(int coord, int x, int y, int z, int profile, int speed , int acc, int acc2, BOOL bAbsolute=FALSE);
	 int StopAxisMulti(int coord,int mode, int dec, int dec2, int profile  );
	// int StopAxisXYZ(int coord, int mode, int dec, int dec2, int profile );
	  
	 int MoveArc(int coord, int axis_x, int axis_y, int ex, int ey, int radius, BOOL bBigArc, int dir, int speed , int acc, int acc2, BOOL bAbsolute);
	 int MoveCircle(int coord, int axis_x, int axis_y, int cx, int cy, int sx, int sy, int dir, int speed , int acc, int acc2, BOOL bAbsolute);



	 //缓冲	
	 int  Delay(/*int bankId,*/ int duration);

	 int  StartBank(int bankId);
	 int  EndBank(int bankId, unsigned long* count);
	 int  GetBankState(int bankId, unsigned short* state, unsigned short* linenum);
	 int  ExcuteBank(int bankId, unsigned short mode);
	 int  HaltBank(int bankId, int mode, int dec, int dec2, int profile );
	 int  SetBankProfile(int bankId, int profile, int speed, int acc, int acc2);
	 //MCF_Buffer_Set_Profile_Net(0,0,10000,100000,1000000,0,PROFILE_T);

	 int  GetAxisCurPos(int axis, long* curpos);	  
	 int  GetAxisState(int axis, int* state);

	 
public:
	 
	//回零运动
	//unsigned short m_nStopTime;
	//unsigned short m_nHomeMode;
	//unsigned short m_nLimitLogic;
	//unsigned short m_nHomeLogic;
	//unsigned short m_nIndexLogic;

	 
	 int    m_nStopProfile;
	//int    m_nMoveProfile;
	 int     m_nMoveProfile ;


 private:
	
	//BOOL BeforeJog(int axis,  int* pErrCode);
	BOOL BeforeMove(int axis, double dist  , int* pErrCode);
	//BOOL BeforeHome(int axis,  int* pErrCode);

	  
};

