
#ifndef __MOTION_BASE_H_INCLUDED_
#define __MOTION_BASE_H_INCLUDED_

//#include <assert.h>
#include <sst/ucut.h>


#ifdef MOTBASE_EXPORTS
#define ROBBASE_API __declspec(dllexport)
#else
#define ROBBASE_API __declspec(dllimport)
#endif

//最多轴数量
#define ROB_MAX_AXIS_COUNT       64

#define ROB_MAX_SIO_COUNT        (12+12) 
#define ROB_MAX_SYNCOBJ_NAME_LEN        31
 
#define  ROB_WAIT_INFINITE       0x7FFFFFFF



#define ROB_AXIS_1        1 //轴序号从1开始
#define ROB_BIT_1         1 //位序列号从1开始

#define ROB_MAX_DELAY_TIME          8999

//运动方向: 正方向， 负方向
#define ROB_DIR_POS    1
#define ROB_DIR_NEG    (-1)

//Fio类型
#define ROB_FIO_IN     0
#define	ROB_FIO_OUT    1


#define ROB_AH_SERIAL_NUM       (-1)
#define ROB_AH_CAMERA           0
#define ROB_AH_MOTION_MODULE    1
#define ROB_AH_GPIO_MODULE      2

//轴运行状态
#define	ROB_AX_NORMAL    0  //停止状态
#define	ROB_AX_HOMEOK    0  //停止状态
#define	ROB_AX_BUSYHOME  1
#define	ROB_AX_MOVING    1
#define	ROB_AX_STOPPED   2  //缓冲区可能有这种状态，轴停止，但没有错误发生
#define	ROB_AX_ALARM     3  //报警
#define	ROB_AX_FAILURE     3  //错误发生 

//轴停止方式
#define ROB_SM_IMD     0  //立即停止
#define ROB_SM_SMOOTH  1  //平滑停止

//轴暂停模式 pause mode
#define  ROB_PM_IMD   0   //立即暂停
#define  ROB_PM_NEXT  1   //执行下一条指令时暂停
#define  ROB_PM_MARK  2   //执行下一条不一样的mark指令时暂停


//普通轴名称
#define ROB_AXIS_1      1
#define ROB_AXIS_2      2
#define ROB_AXIS_3      3
#define ROB_AXIS_4      4   
#define ROB_AXIS_5      5  
#define ROB_AXIS_6      6  
#define ROB_AXIS_7      7  
#define ROB_AXIS_8      8   
// ..                  N

#define ROB_AXIS(num)   (num)  

//无效的编号
#define ROB_INVALID_NUM  (0x7FFFFFFF)
#define ROB_INVALID_ID   ROB_INVALID_NUM


//一轴
#define ROB_A1_X       1 

//二轴
#define ROB_A2_X       1
#define ROB_A2_Y       2
  
//三轴
#define ROB_A3_X       1
#define ROB_A3_Y       2
#define ROB_A3_Z       3

//四轴
#define ROB_A4_X       1
#define ROB_A4_Y       2
#define ROB_A4_Z       3
#define ROB_A4_Y2      4   
#define ROB_A4_R       4  

//六轴 
#define ROB_A6_X       1
#define ROB_A6_Y       2
#define ROB_A6_Z       3
#define ROB_A6_Y2      4   
#define ROB_A6_R       5   

//8轴
//第1个坐标系
#define ROB_A8_X1       1
#define ROB_A8_Y1       2
#define ROB_A8_Z1       3
#define ROB_A8_R1       4   

//第2个坐标系
#define ROB_A8_X2       5
#define ROB_A8_Y2       6
#define ROB_A8_Z2       7
#define ROB_A8_R2       8  
 

//动作并发行Concurrence
#define ROB_AC_FREE         0   //自由执行
#define ROB_AC_INITIATIVE   1   //主动执行(默认)
#define ROB_AC_SUBMISSIVE   2   //被动执行
#define ROB_AC_HYBRID       3   //混合的执行
#define ROB_AC_EXCLUSIVE    4   //排它主动执行


//robot, action id, control units, units count,  notify wnd, userdata
//返回0表示OK, <0 表示错误返回
typedef int ( *ROB_ACTION_IMPLEMENT_FUNC)(HANDLE, UINT, HANDLE*, int, HWND, void*);
 
#define ROB_CALC_PULSE(n, dpr, mr)   (((n) / (dpr) * (mr)))  //公制mm转成脉冲
#define ROB_CALC_METRIC(n, dpr, mr)  ((n)*1. /(mr) * (dpr) )       //脉冲转成公制mm
#define ROB_PULSE_PER_MM(mr, dpr)  ((mr) /(dpr))   

//ECAT IO OFFSET 
#define GOOGL_ECAT_IO_BIT_OFFSET   10 
#define MAKE_ECAT_BIT(offs, bit)    (((offs) << GOOGL_ECAT_IO_BIT_OFFSET) | (bit))
#define GET_ECAT_OFFSET(Bit)        ((Bit) >> GOOGL_ECAT_IO_BIT_OFFSET)
#define GET_ECAT_BIT(Bit)           ((Bit) & 0x3FF)

//轴运动类型
#define ROB_MT_UNKNOWN      (-1)
#define ROB_MT_UNLIMIT      0   //自由: 无原点限制或限位,
#define ROB_MT_LINEAR       1   //直线运动： 默认
#define ROB_MT_ROTATION     2   //旋转运动

//轴驱动器类型
#define ROB_DT_UNKNOWN      (-1)
#define ROB_DT_STEP         0   //步进
#define ROB_DT_SERVO        1   //伺服
#define ROB_DT_ECAT         2   //EtherCAT

//位置计数器
#define ROB_PC_DEFAULT      0  // 规划位置
#define ROB_PC_ENCODER      1  // 编码器
#define ROB_PC_RASTER       2  // 光栅尺

 
 
//动作类型ID请大于0
#define ROB_ACT_UNKNOWN          (-1)  //未知
#define ROB_ACT_USER_EX           0    //用户自定义动作

 

//辅助状态
//轴辅助状态(报警)
#define		ROB_ALM_POS_LMT  1        //正限位报警
#define		ROB_ALM_NEG_LMT  (1<<1)   //负限位报警
#define	    ROB_ALM_DRV_ALM  (1<<2)   //驱动报警
#define		ROB_ALM_ENA_SRV  (1<<3)   //伺服使能
#define		ROB_ALM_EXC_TOL  (1<<4)   //随动误差超出限制
#define		ROB_ALM_EM_STP   (1<<5)   //急停
#define		ROB_ALM_SM_STP   (1<<6)   //缓停
#define		ROB_ALM_RUN_STA  (1<<7)   //运行状态

#define     ROB_ALM_SOFTLMT  ( ROB_ALM_POS_LMT | ROB_ALM_NEG_LMT)

//常用标识 
#define	    ROB_DISABLE  0 
#define		ROB_ENABLE   1 
#define		ROB_LOW   0 
#define		ROB_HI    1 
#define		ROB_OFF   0 
#define		ROB_ON    1 
#define		ROB_CLOSE 0 
#define		ROB_OPEN  1 
#define		ROB_CW    0         //move direction: 顺时针
#define		ROB_CCW   1         //move direction: 逆时针 
 

//场景类型, 系统内设,实例或派生类自己添加
#define ROB_ST_HOME      0
#define	ROB_ST_EDIT      1
#define	ROB_ST_JOG       2    //连续移动单轴，该场景为系统默认
#define	ROB_ST_ESTOP     3      //急停场景,主要增加减速度
#define	ROB_ST_ANY       (-1)   //变化的场景，里面的参数可变，不支持多线程
#define ROB_ST_MORE      7     //用户定义的场景

 

//返回值
#define ROB_OK            0
#define ROB_FAILED        (-1)

#define ROB_ERR_INVALID_OBJ         (-10084)
#define ROB_ERR_INVALID_HANDLE      ROB_ERR_INVALID_OBJ
#define ROB_ERR_CAMERA_FAILURE      (-10085) 
#define ROB_ERR_BARCODE_NOT_FIND    (-10086) 
#define ROB_ERR_BARCODE_READ        (-10087) 
#define ROB_ERR_MARK_NOT_FIND       (-10088) 
#define ROB_ERR_SHOT_TIMEOUT        (-10089) 
#define ROB_ERR_IN_EMERGENCE        (-10090) 
#define ROB_ERR_REENTRY_CONFLICT    (-10091) 
#define ROB_ERR_SYSERR              (-10092) 
#define ROB_ERR_NOT_ENOUGH          (-10093) 
#define ROB_ERR_ILLEGAL_USE         (-10094) 
#define ROB_ERR_INVALID_AXIS        (-10095)

#define ROB_ERR_UNSUPPORTED         (-10096)
#define ROB_ERR_OUT_OF_RANGE        (-10097)
#define ROB_ERR_NOT_READY           (-10098)
#define ROB_ERR_INPUT_TIME_OUT      (-10099)
#define ROB_ERR_TIME_OUT             ROB_ERR_INPUT_TIME_OUT 
#define ROB_ERR_INVALID_ARGS           (-10100)
#define ROB_ERR_OPERTAION_ABORT        (-10101)
#define ROB_ERR_NOT_IN_STATE           (-10102) 
#define ROB_ERR_OPERTAION_CANCEL       (-10103)
#define ROB_ERR_CONCURRENCE_CONFLICT   ROB_ERR_REENTRY_CONFLICT
#define ROB_ERR_CONFLICT               ROB_ERR_REENTRY_CONFLICT
#define ROB_ERR_CREATION_FAILURE       (-10104) 
#define ROB_ERR_NOT_RUNNING            (-10105)  //robot not running
#define ROB_ERR_NOT_ACTIVE             (-10106)  //action not actve
#define ROB_ERR_OBJECT_NOT_FIND        (-10107) 




//轴状态错误
#define ROB_ERR_BUSY_HOMING            (-10110)
#define ROB_ERR_BUSY_MOVING            (-10111)
#define ROB_ERR_AXIS_ALARM             (-10112)
#define ROB_ERR_AXIS_STOPPED           (-10113)
#define ROB_ERR_AXIS_UNKNOWN           (-10114)

 

 

#define ROB_ERR_USER_EX                (-100000)



#define ROB_TOPPLE_LEVEL(level)      (level) =((level) == ROB_HI? ROB_LOW: ROB_HI) 
#define ROB_IS_FLAG_ON(flags, id)  ( ((flags) & (id)) == (id)  )

ROBBASE_API BOOL robAuthorizeHardware(int deviceType, const char* strModels, const char* strFilePath);
ROBBASE_API BOOL robIsAuthorized();
ROBBASE_API BOOL robIsHardwareAccord();






#endif 


