/BittingMachine_V31_5_04_2012/PinMarker/LearnDraw/MotionWrappers/YmcWrapper.cs

https://bitbucket.org/alexioffe/biting · C# · 591 lines · 410 code · 94 blank · 87 comment · 11 complexity · fe8a09f0f6d6caad55868d648d919ea3 MD5 · raw file

  1. #define _REAL
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Threading;
  7. //using YETlib;
  8. using System.Runtime.InteropServices;
  9. namespace PinMarker.MotionWrappers
  10. {
  11. public class YmcWrapper : MotionObject
  12. {
  13. IntPtr gGlobalPtr;
  14. IntPtr hDevice;
  15. IntPtr hMark, hReset, hHome, hAlarm = IntPtr.Zero, hErrorCode;
  16. static readonly int CountsFactor = 2048;
  17. static readonly int SpeedCountsFactor = 2048*60;
  18. int lastError = -1;
  19. //IntPtr hAir, hX, hY, hF;
  20. public virtual event ErrorStatusChanged OnErrorStatusChanged;
  21. private IntPtr hNumMoves;
  22. private IntPtr hRun, hStop;
  23. public int GetCountsFactor(int ax)
  24. {
  25. return CountsFactor;
  26. }
  27. public int GetSpeedCountsFactor(int ax)
  28. {
  29. return SpeedCountsFactor;
  30. }
  31. public void SetResetEvent(AutoResetEvent _stopEvent)
  32. {
  33. }
  34. public YmcWrapper()
  35. {
  36. }
  37. public int Init()
  38. {
  39. #if _REAL
  40. // Logger.GetInstance().Log("Trying to create yetlib.Init()");
  41. try
  42. {
  43. int rc = 0;
  44. unsafe
  45. {
  46. COM_DEVICE* ComDevice = stackalloc COM_DEVICE[1];
  47. ComDevice[0].ComDeviceType = 4; // COMDEVICETYPE_PCI_MODE;
  48. ComDevice[0].PortNumber = 1;
  49. ComDevice[0].CpuNumber = 1;
  50. ComDevice[0].NetworkNumber = 0;
  51. ComDevice[0].StationNumber = 0;
  52. ComDevice[0].UnitNumber = 0;
  53. ComDevice[0].IPAddress = 0;
  54. ComDevice[0].Timeout = 10000;
  55. /*GCHandle arr = GCHandle.Alloc(ComDevice, GCHandleType.Pinned);
  56. rc = fnTestDll((COM_DEVICE*)arr.AddrOfPinnedObject().ToPointer(), out gGlobalPtr);
  57. rc = fnTestDll1((COM_DEVICE*)arr.AddrOfPinnedObject().ToPointer(), out gGlobalPtr);
  58. //rc = ymcOpenController((COM_DEVICE*)arr.AddrOfPinnedObject().ToPointer(), out gGlobalPtr);
  59. */
  60. rc = ymcOpenController(ComDevice, out gGlobalPtr);
  61. Logger.GetInstance().Log("ymcOpenController done: rc=" + rc.ToString());
  62. Logger.GetInstance().Log("ymcOpenController done: gGlobalPtr=" + gGlobalPtr.ToString());
  63. //rc = ymcSetAPITimeoutValue(30000);
  64. Logger.GetInstance().Log("ymcSetAPITimeoutValue done: rc=" + rc.ToString());
  65. rc = ymcClearAllAxes();
  66. Logger.GetInstance().Log("ymcClearAllAxes done: rc=" + rc.ToString());
  67. //rc = InitAxis();
  68. ymcGetRegisterDataHandle("MB100013", out hReset);
  69. ymcGetRegisterDataHandle("MB100010", out hHome);
  70. ymcGetRegisterDataHandle("OB00224", out hMark);
  71. /*ymcGetRegisterDataHandle("ML10050", out hAir);
  72. ymcGetRegisterDataHandle("ML10052", out hX);
  73. ymcGetRegisterDataHandle("ML10054", out hY);
  74. ymcGetRegisterDataHandle("ML10056", out hF);
  75. */
  76. // ymcGetRegisterDataHandle("ML10050", out hMoveBuf);
  77. ymcGetRegisterDataHandle("MW10041", out hNumMoves);
  78. ymcGetRegisterDataHandle("MB100011", out hRun);
  79. ymcGetRegisterDataHandle("MB100012", out hStop);
  80. ymcGetRegisterDataHandle("MB100050", out hAlarm);
  81. ymcGetRegisterDataHandle("MW10000", out hErrorCode);
  82. }
  83. // int rc = yetlib.Init();
  84. }
  85. catch (Exception e)
  86. {
  87. Logger.GetInstance().Log("Init() - Exception: " + e.Message);
  88. }
  89. #endif
  90. // Logger.GetInstance().Log("int rc = yetlib.Init(); done");
  91. return 0;
  92. }
  93. public int Close()
  94. {
  95. #if _REAL
  96. // yetlib.Close();
  97. try
  98. {
  99. ymcCloseController(gGlobalPtr);
  100. }
  101. catch (Exception e)
  102. {
  103. Logger.GetInstance().Log("ymcCloseController - Exception: " + e.Message);
  104. }
  105. #endif
  106. return 0;
  107. }
  108. int GetHandleToRegister(String sRegister, ref IntPtr Handle)
  109. {
  110. #if _REAL
  111. try
  112. {
  113. int rc = ymcGetRegisterDataHandle(sRegister, out Handle);
  114. }
  115. catch (Exception e)
  116. {
  117. Logger.GetInstance().Log("GetHandleToRegister - Exception: " + e.Message);
  118. }
  119. #endif
  120. return 0;
  121. }
  122. int SetMemory(IntPtr Handle, int nCount, int[] values)
  123. {
  124. #if _REAL
  125. try
  126. {
  127. unsafe
  128. {
  129. GCHandle arr = GCHandle.Alloc(values, GCHandleType.Pinned);
  130. int rc = ymcSetRegisterData(Handle, nCount, (int*)arr.AddrOfPinnedObject().ToPointer());
  131. }
  132. }
  133. catch (Exception e)
  134. {
  135. Logger.GetInstance().Log("SetMemory - Exception: " + e.Message);
  136. }
  137. #endif
  138. return 0;
  139. }
  140. int GetMemory(IntPtr Handle, int nCount, ref int[] values, ref int nOutCount)
  141. {
  142. #if _REAL
  143. try
  144. {
  145. unsafe
  146. {
  147. int rc = ymcGetRegisterData(Handle, nCount, out values[0], out nOutCount);
  148. // Logger.GetInstance().Log("GetMemory: " + Handle.ToString() + " rc= " + rc.ToString());
  149. return rc;
  150. }
  151. }
  152. catch (Exception e)
  153. {
  154. Logger.GetInstance().Log("GetMemory - Exception: " + e.Message);
  155. }
  156. #endif
  157. return 0;
  158. }
  159. /*
  160. public int InitAxis()
  161. {
  162. int rc = 0;
  163. try
  164. {
  165. unsafe
  166. {
  167. IntPtr* hAxis = stackalloc IntPtr[2];
  168. rc = ymcDeclareAxis(1, 0, 3, 1, 1, 1, "AXIS001", out hAxis[0]);
  169. rc = ymcDeclareAxis(1, 0, 3, 2, 2, 1, "AXIS002", out hAxis[1]);
  170. rc = ymcDeclareDevice(2, hAxis, out hDevice);
  171. }
  172. }
  173. catch (Exception e)
  174. {
  175. Logger.GetInstance().Log("ymcDeclareAxis - Exception: " + e.Message);
  176. return rc;
  177. }
  178. return rc;
  179. }
  180. */
  181. public long MoveTo(double X, double Y, int Speed, int Acc, int Dec)
  182. {
  183. const int MTYPE_ABSOLUTE = 1;
  184. const int VTYPE_UNIT_PAR = 0;
  185. const int ATYPE_UNIT_PAR = 0;
  186. const int FTYPE_S_CURVE = 0;
  187. //const int PHYSICALAXIS = 0;
  188. const int DATATYPE_IMMEDIATE = 0;
  189. const short DISTRIBUTION_COMPLETED = 0;
  190. //const short COMMAND_STARTED = 2;
  191. long rc = 0;
  192. try
  193. {
  194. unsafe
  195. {
  196. MOTION_DATA[] motData = new MOTION_DATA[2];
  197. POSITION_DATA[] posData = new POSITION_DATA[2];
  198. motData[0].CoordinateSystem = motData[1].CoordinateSystem = 0;
  199. motData[0].MoveType = motData[1].MoveType = MTYPE_ABSOLUTE;
  200. motData[0].VelocityType = motData[1].VelocityType = VTYPE_UNIT_PAR;
  201. motData[0].AccDecType = motData[1].AccDecType = ATYPE_UNIT_PAR;
  202. motData[0].FilterType = motData[1].FilterType = FTYPE_S_CURVE;
  203. motData[0].DataType = motData[1].DataType = 0;
  204. motData[0].MaxVelocity = motData[1].MaxVelocity = 1428800;
  205. motData[0].Acceleration = motData[1].Acceleration = Acc;
  206. motData[0].Deceleration = motData[1].Deceleration = Dec;
  207. motData[0].FilterTime = motData[1].FilterTime = 0;
  208. motData[0].Velocity = motData[1].Velocity = Speed;
  209. //rc = ymcGetAxisHandle(PHYSICALAXIS, 1, 0, 3, 1, 0, null, out hAxis[0]);
  210. //rc = ymcGetAxisHandle(PHYSICALAXIS, 1, 0, 3, 2, 0, null, out hAxis[1]);
  211. posData[0].DataType = DATATYPE_IMMEDIATE;
  212. posData[0].PositionData = Convert.ToInt32(X * CountsFactor);
  213. posData[1].DataType = DATATYPE_IMMEDIATE;
  214. posData[1].PositionData = Convert.ToInt32(Y * CountsFactor);
  215. //Call ymcMoveLinear.
  216. rc = ymcMoveLinear(hDevice, motData, posData, 0, null, DISTRIBUTION_COMPLETED, 0);
  217. Logger.GetInstance().Log("MoveTo - Done, rc= " + rc.ToString());
  218. //rc = ymcMoveLinear(hDevice, motData, posData, 0, null, COMMAND_STARTED, 0);
  219. }
  220. }
  221. catch (Exception e)
  222. {
  223. Logger.GetInstance().Log("MoveTo - Exception: " + e.Message);
  224. return rc;
  225. }
  226. return 0;
  227. }
  228. public long Home()
  229. {
  230. int[] v = new int[1];
  231. v[0] = 1;
  232. SetMemory(hHome, 1, v);
  233. Thread.Sleep(10);
  234. v[0] = 0;
  235. SetMemory(hHome, 1, v);
  236. return 0;
  237. }
  238. public long Reset()
  239. {
  240. int[] v = new int[1];
  241. v[0] = 1;
  242. SetMemory(hReset, 1, v);
  243. Thread.Sleep(10);
  244. v[0] = 0;
  245. SetMemory(hReset, 1, v);
  246. return 0;
  247. }
  248. public long Stop()
  249. {
  250. int[] v = new int[1];
  251. v[0] = 1;
  252. SetMemory(hStop, 1, v);
  253. return 0;
  254. }
  255. public bool IsProgramEnded()
  256. {
  257. int[] d = new int[1];
  258. int nout = 0;
  259. try
  260. {
  261. GetMemory(hRun, 1, ref d, ref nout);
  262. return d[0] == 0;
  263. }
  264. catch (Exception e)
  265. {
  266. MessageBox.Show(e.Message);
  267. return false;
  268. }
  269. return false;
  270. }
  271. public long SendMotionsBuffer(List<Motion> values, int nMotions)
  272. {
  273. // Create the correct buffer
  274. /* double tx, ty;
  275. int[] motBuff = new int[values.Count * 4];
  276. for (int i = 0; i < gpPrev.path.Count; i++)
  277. {
  278. if (backgroundWorker1.CancellationPending)
  279. {
  280. MessageBox.Show("Cancelled");
  281. break;
  282. }
  283. if (gpPrev.path[i].Type == MachLine.LineType.Connection)
  284. motBuff[i * 4] = 0;
  285. else
  286. motBuff[i * 4] = 1;
  287. tx = gpPrev.path[i].Xe;
  288. ty = -gpPrev.path[i].Ye;
  289. //tx = Convert.ToSingle(txtXmm.Text) - gpPrev.path[i].Xe;
  290. //ty = Convert.ToSingle(txtYmm.Text) - gpPrev.path[i].Ye;
  291. //ty = gpPrev.path[i].Ye;
  292. motBuff[i * 4 + 1] = (int)(tx * motion.GetCountsFactor(0));
  293. motBuff[i * 4 + 2] = (int)(ty * motion.GetCountsFactor(1));
  294. motBuff[i * 4 + 3] = (int)(Speed * motion.GetSpeedCountsFactor(0));
  295. //Console.WriteLine(i.ToString() + ":" + tx.ToString() + ", " + ty.ToString());
  296. }
  297. */
  298. return 0;
  299. }
  300. public long SendMotionsBuffer(int[] values, int nMotions)
  301. {
  302. int[] d = new int[1];
  303. const int MaxReg = 100;
  304. d[0] = nMotions;
  305. try
  306. {
  307. SetMemory(hNumMoves, 1, d);
  308. int nFirstReg = 0;
  309. while (nFirstReg < nMotions*4)
  310. {
  311. int count = Math.Min(MaxReg, nMotions*4-nFirstReg);
  312. int[] temp = new int[count];
  313. for (int k = 0; k < count; k++)
  314. temp[k] = values[nFirstReg+k];
  315. String S = "ML" + (10050 + nFirstReg*2).ToString();
  316. IntPtr hMoveBuf;
  317. ymcGetRegisterDataHandle(S, out hMoveBuf);
  318. SetMemory(hMoveBuf, count, temp);
  319. nFirstReg += count;
  320. }
  321. d[0] = 1;
  322. SetMemory(hRun, 1, d);
  323. }
  324. catch (Exception e)
  325. {
  326. MessageBox.Show(e.Message);
  327. }
  328. return 0;
  329. }
  330. public int MarkerUpDown(int nVal)
  331. {
  332. int[] v = new int[1];
  333. v[0] = 0;
  334. return SetMemory(hMark, 1, v);
  335. }
  336. public int ReadError()
  337. {
  338. int[] d = new int[1];
  339. int count = 0;
  340. if (hAlarm != IntPtr.Zero)
  341. GetMemory(hAlarm, 1, ref d, ref count);
  342. else
  343. d[0] = 0; // sim 1 to make read error work
  344. if (d[0] != 0)
  345. {
  346. GetMemory(hErrorCode, 1, ref d, ref count);
  347. if (lastError != d[0])
  348. {
  349. lastError = d[0];
  350. OnErrorStatusChanged(this, lastError);
  351. }
  352. return lastError;
  353. }
  354. else
  355. {
  356. if (lastError != 0)
  357. {
  358. lastError = 0;
  359. OnErrorStatusChanged(this, lastError);
  360. }
  361. }
  362. return 0;
  363. }
  364. #if _REAL
  365. #region Imports
  366. [DllImport("ymcPCAPI.dll")]
  367. public static extern unsafe int ymcSetRegisterData(
  368. IntPtr dh, int N, int* data);
  369. [DllImport("ymcPCAPI.dll")]
  370. public static extern int ymcGetRegisterData(
  371. IntPtr dh, int N, out int data, out int nCount);
  372. [DllImport("ymcPCAPI.dll")]
  373. public static extern int ymcGetRegisterDataHandle(
  374. String pRegisterName,
  375. out IntPtr hRegisterData);
  376. [DllImport("ymcPCAPI.dll")]
  377. public static extern int ymcClearAllAxes();
  378. [DllImport("ymcPCAPI.dll")]
  379. public static extern int ymcCloseController(IntPtr hCont);
  380. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  381. public struct COM_DEVICE
  382. {
  383. [MarshalAs(UnmanagedType.U2, SizeConst = 2)]
  384. public ushort ComDeviceType;
  385. [MarshalAs(UnmanagedType.U2, SizeConst = 2)]
  386. public ushort PortNumber;
  387. [MarshalAs(UnmanagedType.U2, SizeConst = 2)]
  388. public ushort CpuNumber;
  389. [MarshalAs(UnmanagedType.U2, SizeConst = 2)]
  390. public ushort NetworkNumber;
  391. [MarshalAs(UnmanagedType.U2, SizeConst = 2)]
  392. public ushort StationNumber;
  393. [MarshalAs(UnmanagedType.U2, SizeConst = 2)]
  394. public ushort UnitNumber;
  395. [MarshalAs(UnmanagedType.U4, SizeConst = 4)]
  396. public uint IPAddress;
  397. [MarshalAs(UnmanagedType.U4, SizeConst = 4)]
  398. public uint Timeout;
  399. }
  400. /*[DllImport("TestDll.dll")]
  401. public static extern unsafe int fnTestDll1
  402. (COM_DEVICE* data, out IntPtr dh);
  403. [DllImport("TestDll.dll")]
  404. public static extern unsafe int fnTestDll(COM_DEVICE* data, out IntPtr dh);
  405. */
  406. [DllImport("ymcPCAPI.dll")]
  407. public static extern unsafe int ymcOpenController
  408. (COM_DEVICE* data, out IntPtr dh);
  409. [DllImport("ymcPCAPI.dll")]
  410. public static extern unsafe int ymcSetAPITimeoutValue
  411. (long dh);
  412. [DllImport("ymcPCAPI.dll")]
  413. public static extern unsafe int ymcGetAxisHandle
  414. (
  415. ushort SpecifyType,
  416. ushort RackNo,
  417. ushort SlotNo,
  418. ushort SubslotNo,
  419. ushort AxisNo,
  420. ushort LogicalAxisNo,
  421. String pAxisName,
  422. out IntPtr pAxis);
  423. [DllImport("ymcPCAPI.dll")]
  424. public static extern unsafe int ymcDeclareDevice(
  425. Int16 AxisNum,
  426. IntPtr* pAxis,
  427. out IntPtr phDevice);
  428. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  429. public struct MOTION_DATA
  430. {
  431. [MarshalAs(UnmanagedType.I2, SizeConst = 2)]
  432. public Int16 CoordinateSystem;
  433. [MarshalAs(UnmanagedType.I2, SizeConst = 2)]
  434. public Int16 MoveType;
  435. [MarshalAs(UnmanagedType.I2, SizeConst = 2)]
  436. public Int16 VelocityType;
  437. [MarshalAs(UnmanagedType.I2, SizeConst = 2)]
  438. public Int16 AccDecType;
  439. [MarshalAs(UnmanagedType.I2, SizeConst = 2)]
  440. public Int16 FilterType;
  441. [MarshalAs(UnmanagedType.I2, SizeConst = 2)]
  442. public Int16 DataType;
  443. [MarshalAs(UnmanagedType.I4, SizeConst = 4)]
  444. public Int32 MaxVelocity;
  445. [MarshalAs(UnmanagedType.I4, SizeConst = 4)]
  446. public Int32 Acceleration;
  447. [MarshalAs(UnmanagedType.I4, SizeConst = 4)]
  448. public Int32 Deceleration;
  449. [MarshalAs(UnmanagedType.I4, SizeConst = 4)]
  450. public Int32 FilterTime;
  451. [MarshalAs(UnmanagedType.I4, SizeConst = 4)]
  452. public Int32 Velocity;
  453. [MarshalAs(UnmanagedType.I4, SizeConst = 4)]
  454. public Int32 ApproachVelocity;
  455. [MarshalAs(UnmanagedType.I4, SizeConst = 4)]
  456. public Int32 CreepVelocity;
  457. }
  458. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  459. public struct POSITION_DATA
  460. {
  461. [MarshalAs(UnmanagedType.I4, SizeConst = 4)]
  462. public Int32 DataType;
  463. [MarshalAs(UnmanagedType.I4, SizeConst = 4)]
  464. public Int32 PositionData;
  465. }
  466. [DllImport("ymcPCAPI.dll")]
  467. public static extern unsafe int ymcMoveLinear(
  468. IntPtr hDevice,
  469. MOTION_DATA[] lpMotionData,
  470. POSITION_DATA[] lpPos,
  471. Int32 hMoveIO,
  472. byte* pObjectName,
  473. Int16 WaitForCompletion,
  474. Int32 SystemOption
  475. );
  476. [DllImport("ymcPCAPI.dll")]
  477. public static extern unsafe int ymcDeclareAxis(
  478. Int16 RackNo,
  479. Int16 SlotNo,
  480. Int16 SubslotNo,
  481. Int16 AxisNo,
  482. Int16 LogicalAxisNo,
  483. Int16 AxisType,
  484. String pAxisName,
  485. out IntPtr pAxis);
  486. #endregion
  487. #endif
  488. }
  489. }