DiosCtrlBox.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #pragma once
  2. // 下列 ifdef 块是创建使从 DLL 导出更简单的
  3. // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 DIOSCTRLBOX_EXPORTS
  4. // 符号编译的。在使用此 DLL 的
  5. // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
  6. // DIOSCTRLBOX_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
  7. // 符号视为是被导出的。
  8. #ifndef DIOSCTRLBOX_EXPORTS
  9. #ifdef _WIN64
  10. #ifdef _DEBUG
  11. #pragma comment(lib, "DiosCtrlBox64D.lib")
  12. #else
  13. #pragma comment(lib, "DiosCtrlBoxX64.lib")
  14. #endif
  15. #else
  16. #ifdef _DEBUG
  17. #pragma comment(lib, "DiosCtrlBoxD.lib")
  18. #else
  19. #pragma comment(lib, "DiosCtrlBox.lib")
  20. #endif
  21. #endif
  22. #endif
  23. #ifdef DIOSCTRLBOX_EXPORTS
  24. #define DIOSCTRLBOX_API __declspec(dllexport)
  25. #define DIOSCTRLBOX_C_API extern "C" __declspec(dllexport)
  26. #else
  27. #define DIOSCTRLBOX_API __declspec(dllimport)
  28. #define DIOSCTRLBOX_C_API extern "C" __declspec(dllimport)
  29. #endif
  30. #include "DPC.h"
  31. #include "Generator_Model_Def.h"
  32. #include "LogicDriver.h"
  33. #include "SerialSCF.h"
  34. #include "DiosLogicDeviceStructure.h"
  35. #include "Mech_Tomo.h"
  36. #include "Hsw_Tomo.h"
  37. #pragma pack(push, 1)
  38. #define DIOSCTRLCPU_COM_TIMEOUT (20000)
  39. #define PACKET_TITLE1 (0xEE)
  40. #define PACKET_TITLE2 (0x5A)
  41. typedef union _FunctionID {
  42. struct {
  43. unsigned char DEVID : 4;//
  44. unsigned char BLOCKID : 4;//
  45. };
  46. unsigned char CharPart;
  47. } FUNCTIONID;
  48. typedef union _ShortData {
  49. struct {
  50. unsigned char Low;//
  51. unsigned char High;//
  52. };
  53. unsigned short ShortPart;
  54. } SHORTDATA;
  55. typedef struct _CtrlPacketHead {
  56. unsigned char szTitle[2];
  57. unsigned char FrameSize;
  58. unsigned char FrameId;
  59. unsigned char FrameCmd;
  60. unsigned char FuncId;
  61. unsigned char Data1;
  62. unsigned char Data2;
  63. unsigned char Data3;
  64. unsigned char Data4;
  65. }CTRLPACKETHEAD;
  66. typedef struct _TOMONODEINFO {
  67. //unsigned short Pos;先不要位置坐标
  68. unsigned short Angle;
  69. }TOMONODEINFO;
  70. #pragma pack(pop)
  71. #define ROTATE_DIR_UP (0x00)
  72. #define ROTATE_DIR_DOWN (0x01)
  73. //IO ports
  74. //HSW
  75. #define FUNCPORTHSW1 (0x11)
  76. #define FUNCPORTHSW2 (0x12)
  77. //伺服电机状态 1:Ready
  78. //#define FUNCPORTSIFUSTATUS (0x14)
  79. #define FUNCPORTEXPNOTIFY (0x31)
  80. //Mech Move
  81. #define FUNCPORTMECHMOVE (0x15)
  82. #define FUNCPORTMECHMOVEPWM (0x16)
  83. #define FUNCPORTMECHMOVEDIR (0x17)
  84. #define FUNCPORTMECHMOVEPOS (0x21)
  85. //Detector ResetPos
  86. #define FUNCPORTRESETPOS (0x22)
  87. //rotate
  88. #define FUNCPORT_ROTATE_DIR (0x36)
  89. //rotate input
  90. #define FUNCPORT_ROTATE_INPUT (0x37)
  91. #define FUNCPORT_485 (0x43)
  92. //encode
  93. #define ENCODEPORT_A (0x32)
  94. #define ENCODEPORT_B (0x33)
  95. #define ENCODEPORT_Z (0x34)
  96. //MCU Ver
  97. #define MCU_VER (0x48)
  98. //rotate limit
  99. //#define FUNCPORT_ROTATE_HIGHLIMIT (0x13)
  100. #define FUNCPORT_ROTATE_LOWLIMIT (0x13)
  101. #define FUNCPORT_ROTATE_PARKINGPOS (0x14)
  102. #define NOTIFY_POS_MAX_LIMIT (20000)
  103. class DIOSCTRLBOX_API DiosCtrlBoxDPC : public LogicDriver
  104. {
  105. bool m_EncoderReadyStatus;
  106. std::string *m_pWorkpath;
  107. std::string *m_pSTMCMD;
  108. HAND_SWITCH m_CurrentHsw;
  109. EXAM_MODE m_CurrentExamMode;
  110. Mech_Tomo *m_pMech;
  111. Hsw_Tomo *m_pHsw;
  112. bool m_HitPrepPos;
  113. bool m_AngleCorrected;
  114. bool m_RsetPosChanged;
  115. //servo
  116. WORD m_ServoMode;
  117. WORD m_ServoGuanLiangB;
  118. WORD m_ServoRoundPulse;
  119. FLOAT m_TomoPositionConv;
  120. //电机参数
  121. USHORT m_MotorPeriod;
  122. USHORT m_MotorEffectivePeriod;
  123. USHORT m_ServoEffectivePeriod;
  124. //为TOMO算法准备----begin
  125. FLOAT m_TomoAngleReqConvRate;
  126. FLOAT m_TomoAngleResConvRate;
  127. //detector
  128. FLOAT m_DetectorPrepTimePeriod;
  129. FLOAT m_MotorPerAngle;
  130. FLOAT m_TomoMoveConvRateA;
  131. FLOAT m_TomoMoveConvRateB;
  132. FLOAT m_TomoResetConvRateA;
  133. FLOAT m_TomoResetConvRateB;
  134. FLOAT m_TomoMoveSpeed;
  135. FLOAT m_TomoSpeedConv;
  136. FLOAT m_TomoMinDistance;
  137. //gen
  138. FLOAT m_TomoMinAngleLimit;
  139. FLOAT m_TomoMinMoveLimit;
  140. FLOAT m_TomoMoveOffset;// LogicPos = PhyOffset*Ratio + MoveOffset
  141. FLOAT m_SID;
  142. FLOAT m_AccDst;//1
  143. FLOAT m_LatDst;//2
  144. FLOAT m_SusDst;//3
  145. FLOAT m_BrkDst;//4
  146. FLOAT m_TomoWarmAngle;
  147. FLOAT m_TomoAngle;
  148. INT m_TomoStepCorr;
  149. INT m_TomoPeriodCorr;
  150. DWORD m_TomoAlgorythm;
  151. FLOAT m_EncoderConvA;
  152. FLOAT m_EncoderConvB;
  153. FLOAT m_EncoderPrecision;
  154. DWORD m_TomoWorkArea;
  155. INT m_LogicTomoPosParamCount;
  156. INT m_LogicTomoPosParamSize;
  157. FLOAT *m_pLogicTomoPosParams;
  158. INT *m_pLogicTomoRPeriodParams;
  159. INT *m_pLogicTomoRStepParams;
  160. INT m_LogicTomoFixStepCount;
  161. INT *m_pLogicTomoFixStepParams;
  162. DWORD m_NotifyCount;
  163. FLOAT m_pNotifiedTomoAngle[NOTIFY_POS_MAX_LIMIT];
  164. FLOAT m_pNotifiedTomoPos[NOTIFY_POS_MAX_LIMIT];
  165. //为TOMO算法准备----end
  166. FLOAT m_ResetParams[MECH_1D_POS_MAX];
  167. FLOAT m_MoveParams[MECH_1D_POS_MAX];
  168. FLOAT m_RotateParams[MECH_1D_POS_MAX];
  169. SCFPacket *m_pSendPacket;
  170. SCFPacket *m_pReceivePacket;
  171. SCF *m_pSCFInstance;
  172. HMODULE m_DllFileHandle;
  173. unsigned char m_FrameId;
  174. //io status
  175. bool m_Level1HSW;
  176. bool m_Level2HSW;
  177. bool PrepCalculation();
  178. bool Action(DWORD Timeout = DIOSCTRLCPU_COM_TIMEOUT);
  179. bool ReceiveAck(DWORD Timeout = DIOSCTRLCPU_COM_TIMEOUT);
  180. USHORT Distance_GetPhyAbsoluteOffsetOnly(FLOAT TargetLogicPos);
  181. USHORT Distance_GetPhyAbsoluteOffsetEx(FLOAT TargetLogicPos);
  182. USHORT Distance_GetPhyAbsoluteOffset(FLOAT TargetLogicPos);
  183. DWORD Angle_GetPhyRelativeOffset(FLOAT TargetLogicAngle, bool Req);
  184. INT Distance_GetphysicGrainTime();
  185. USHORT Distance_Logic2Phy(FLOAT LogicDistance);
  186. FLOAT Distance_Phy2Logic(USHORT PhyDistance);
  187. FLOAT Detector_Phy2Logic(USHORT PhyDistance);
  188. DWORD Angle_Logic2Phy(FLOAT LogicAngle, bool Req);
  189. FLOAT Angle_Phy2Logic(USHORT PhyAngle, bool Req);
  190. bool IsSameAngle(FLOAT a, FLOAT b);
  191. bool IsSameHeight(FLOAT a, FLOAT b);
  192. bool IsSameHeightTomo(FLOAT a, FLOAT b);
  193. void PrintPacket(bool SendFlag);
  194. void PrintPacketEx(const char *pszTitle, const char *pPacket, int Len);
  195. bool BindRotationAndLimitSwitch();
  196. bool WriteServo(UCHAR Target, UCHAR Cmd, USHORT Addr, USHORT Context);
  197. bool ConfigServo();
  198. bool InitEncoder();
  199. bool UnInitEncoder();
  200. bool AtomRotate2LowLimit(DWORD Timeout = DIOSCTRLCPU_COM_TIMEOUT);
  201. bool AtomBlindRotateBackward(FLOAT BackwardAngle, DWORD Timeout = DIOSCTRLCPU_COM_TIMEOUT);
  202. char GetRotateDir(bool Up);
  203. public:
  204. DiosCtrlBoxDPC(void);
  205. virtual ~DiosCtrlBoxDPC(void);
  206. virtual bool DriverEntry(ResDataObject &Configuration);
  207. virtual bool SYSTEM_CALL Driver_Probe(ResDataObject& PARAM_OUT HardwareInfo);
  208. virtual bool SetDriverWorkPath(const char *pWorkPath);
  209. virtual PVOID SYSTEM_CALL LoadDriver();//无根节点
  210. virtual void SYSTEM_CALL UnloadDriver();
  211. bool LoadConfiguration(ResDataObject &connection);
  212. bool InitIOBOX();
  213. RET_STATUS DATA_ACTION GetTomoResults(ResDataObject &result);
  214. //actions
  215. virtual bool Connect();
  216. virtual void DisConnect();
  217. virtual bool SYSTEM_CALL SetDeviceWorkPath(const char * PARAM_IN pWorkPath);
  218. //逻辑设备树的加载和卸载
  219. virtual PVOID SYSTEM_CALL LoadLogicDevices();
  220. virtual void SYSTEM_CALL UnloadLogicDevices();
  221. virtual bool Device_Probe(ResDataObject &HardwareInfo);
  222. virtual DWORD SYSTEM_CALL OnNotify(HANDLE evtList[], DWORD count);
  223. static PACKET_RET DiosCtrlBoxDPC::EventCallback(const char * pRecData, DWORD nLength, DWORD & PacketLength);
  224. //io board ctrl-------------
  225. bool Action485(const char *pszPacket, DWORD PacketLen, DWORD Timeout);
  226. bool Ioctl_PWM_Simple(FUNCTIONID Id, unsigned int Distance);
  227. bool Ioctl3(FUNCTIONID Id, unsigned char Data1, unsigned char Data2, unsigned char Data3);
  228. bool Ioctl2(FUNCTIONID Id, unsigned char Data1, unsigned char Data2);
  229. bool Ioctl1(FUNCTIONID Id, unsigned char Data1);
  230. bool Ioctl_PWM(FUNCTIONID Id, unsigned char Param1, unsigned short FulPeriod, unsigned short HighPeriod, unsigned int PWM);
  231. bool Ioctl_PWM(FUNCTIONID Id, unsigned char Param1, unsigned short Pos, unsigned short FulPeriod, unsigned short HighPeriod, unsigned int PWM);
  232. bool Ioctl_dAdT(FUNCTIONID Id, unsigned char Param1,unsigned short AdVal);//CMD:0x01
  233. bool Ioctl(FUNCTIONID Id, unsigned char *pDataBuff, unsigned int DataLen);//CMD:0x01
  234. bool Write(FUNCTIONID Id, const char *pszContext, unsigned int PacketLen);
  235. bool WriteCOM(const char *pszContext, unsigned int PacketLen);
  236. bool Read_COMResponse(const char *pszContext, unsigned int PacketLen);
  237. int ReadEx(FUNCTIONID Id, char *pszReq, unsigned int ReqLen, char *pszRes, unsigned int ResLen);
  238. int Read(FUNCTIONID Id, unsigned char *pszContext, unsigned int PacketLen);
  239. //dispatch ctrl-------------
  240. void DispatchPacket(const char *pPacket);
  241. void DispatchNotifyPacket(const char *pPacket);
  242. void Dispatch_HSW(HAND_SWITCH hsw);
  243. void Dispatch_PeriodHit();
  244. void Dispatch_PWMOffset(unsigned short PwmOffset, unsigned short TimeOffset);
  245. void Dispatch_HitPos(unsigned short PosOffset, unsigned short TimeOffset);
  246. void Dispatch_HitAngle(unsigned short AngleOffset);
  247. void Dispatch_StopMove();
  248. void Dispatch_StopRotate();
  249. void Dispatch_StopRotate_InTimeout();
  250. void Dispatch_EncoderReady();
  251. void Dispatch_TomoExpTrigger(bool Exp);
  252. void Dispatch_TomoExpPosTrigger(USHORT Pos);
  253. //base cmd-------------------
  254. bool GetSiFuStatus();
  255. bool GetEncoderAngle(FLOAT &Angle);
  256. bool GetCurMechPos();
  257. bool GetCurMechAngle();
  258. bool GetResetHeight();
  259. bool StopMove();
  260. bool StopRotate();
  261. bool StartMove(bool Up);
  262. bool StartMoveInSpeedMode(bool Up);
  263. bool StartMoveInPositionMode(bool Up);
  264. int MoveMechTo(FLOAT TargetPos);//马上返回.0:Moving,1:AtPos,-1:Error
  265. int RotateMechTo(FLOAT TargetAngle);//马上返回.0:Moving,1:AtPos,-1:Error
  266. bool ResetAngleAtom();
  267. bool ResetAngleAtom_Function();
  268. int MoveMechToTomoStartPos();// 0:Moving,1:AtPos,-1:mechError
  269. int MoveMechToTomoEndPos();// 0:Moving,1:AtPos,-1:mechError
  270. //tomo params
  271. bool SetTomoParams();
  272. bool ClearTomoParams();
  273. bool SetTomoParams_Pos();
  274. bool SetTomoParams_Open();
  275. //open cmd ------------------------------------------------------
  276. bool SwitchExam(EXAM_MODE mode);
  277. //原子操作:MoveMech,RotateMech,ActionMech,RESET
  278. bool MoveMechAtom(FLOAT Pos,DWORD Timeout);
  279. bool BlindRotateMech(FLOAT Angle, DWORD Timeout);
  280. bool AtomBlindRotate(FLOAT Angle, DWORD Timeout);
  281. bool RotateMechAtom(FLOAT Angle, DWORD Timeout);
  282. bool ActionMechAtom(FLOAT Pos, FLOAT Angle,DWORD Timeout);
  283. bool StopMech();
  284. bool RESET(DWORD Timeout);
  285. //状态机相关
  286. bool FramePrep();//check resetpos,make calculation possible.
  287. bool FrameReady();//after ready.wait HSW ACTION.
  288. bool FrameStart();//start move
  289. bool FrameEnd();//Stop Mech
  290. bool FramePost();//ResetMechWithHSW first.and return.
  291. //状态机异常
  292. bool FrameError();
  293. bool FrameRecover();
  294. bool SeqError();
  295. bool SeqEnd();
  296. };
  297. DIOSCTRLBOX_C_API DriverDPC* GetDriverDPC();
  298. DIOSCTRLBOX_C_API void ReleaseDriverDPC(DriverDPC *p);