AxisController.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Concurrent;
  4. using System.IO.Ports;
  5. using System.Threading;
  6. using StageController.M3H;
  7. using StageController.HDS;
  8. namespace StageController
  9. {
  10. public abstract class AxisController
  11. {
  12. //将流程控制从 M3HController.cs 中提取出来
  13. private Thread m_waitingThread;
  14. #region Open Close
  15. protected bool m_running = false;
  16. private void StopMoveflow()
  17. {
  18. if (m_currentMoveflow == null) return;
  19. m_currentMoveflow.Stop();
  20. m_currentMoveflow = null;
  21. }
  22. #endregion
  23. #region AxisList
  24. private Dictionary<AxisType, M3HAxis> AxisList;
  25. private M3HAxis m_AxisX => AxisList[AxisType.X];
  26. private M3HAxis m_AxisY => AxisList[AxisType.Y];
  27. private M3HAxis m_AxisZ => AxisList[AxisType.Z];
  28. private void InitAxisList()
  29. {
  30. AxisList = new Dictionary<AxisType, M3HAxis>();
  31. AxisList.Add(AxisType.X, new M3HAxis(M3HAxisType.X, this));
  32. AxisList.Add(AxisType.Y, new M3HAxis(M3HAxisType.Y, this));
  33. AxisList.Add(AxisType.Z, new M3HAxis(M3HAxisType.Z, this));
  34. }
  35. #endregion
  36. #region AxisShell List 虚拟轴控制
  37. private Dictionary<AxisType, AxisShell> AxisShellList;
  38. /// <summary>
  39. /// M3H--> 0:停机自由,1:停机锁死,2:正向连续,3:反向连续,4:正向步进,5:反向步进
  40. /// </summary>
  41. public virtual int StateX => AxisShellList[AxisType.X].State;
  42. /// <summary>
  43. /// M3H--> 0:停机自由,1:停机锁死,2:正向连续,3:反向连续,4:正向步进,5:反向步进
  44. /// </summary>
  45. public virtual int StateY => AxisShellList[AxisType.Y].State;
  46. // Add by shayg 20220615 start
  47. public virtual int LimitX => AxisShellList[AxisType.X].Limit;
  48. public virtual int LimitY => AxisShellList[AxisType.Y].Limit;
  49. public virtual int LimitZ => AxisShellList[AxisType.Z].Limit;
  50. public virtual int ExcessStepX => AxisShellList[AxisType.X].ExcessStep;
  51. public virtual int ExcessStepY => AxisShellList[AxisType.Y].ExcessStep;
  52. public virtual int ExcessStepZ => AxisShellList[AxisType.Z].ExcessStep;
  53. // Add by shayg 20220615 end
  54. /// <summary>
  55. /// M3H--> 0:停机自由,1:停机锁死,2:正向连续,3:反向连续,4:正向步进,5:反向步进
  56. /// </summary>
  57. public virtual int StateZ => AxisShellList[AxisType.Z].State;
  58. public virtual double X => AxisShellList[AxisType.X].Actual;
  59. public virtual double Y => AxisShellList[AxisType.Y].Actual;
  60. public virtual double Z => AxisShellList[AxisType.Z].Actual;
  61. private void InitAxisShellList()
  62. {
  63. AxisShellList = new Dictionary<AxisType, AxisShell>();
  64. AxisShellList.Add(AxisType.X, new AxisShell(AxisType.X, this));
  65. AxisShellList.Add(AxisType.Y, new AxisShell(AxisType.Y, this));
  66. AxisShellList.Add(AxisType.Z, new AxisShell(AxisType.Z, this));
  67. }
  68. #endregion
  69. #region Command Runtime
  70. /// <summary>
  71. /// 运行中流程
  72. /// </summary>
  73. protected Moveflow m_currentMoveflow;
  74. protected ConcurrentQueue<CommandBase> m_cmdQueueM;
  75. protected Moveflow StartMoveflow()
  76. {
  77. m_cmdQueueM = new ConcurrentQueue<CommandBase>();
  78. return new Moveflow();
  79. }
  80. public void AddCommand(CommandBase command)
  81. {
  82. m_cmdQueueM.Enqueue(command);
  83. }
  84. private void Runtime()
  85. {
  86. while (m_running)
  87. {
  88. Thread.Sleep(25);
  89. Process();
  90. }
  91. }
  92. protected virtual void Process()
  93. {
  94. if (m_currentMoveflow != null && m_currentMoveflow.Done)
  95. StopMoveflow();
  96. #region 虚拟轴控制
  97. foreach (var axis in AxisShellList.Values)
  98. {
  99. axis.Execute();
  100. }
  101. #endregion
  102. }
  103. #endregion
  104. #region Application Event
  105. private List<IStageEvent> m_listApp = new List<IStageEvent>();
  106. #endregion
  107. //定义接收数据事件
  108. internal AxisController()
  109. {
  110. InitAxisList();
  111. InitAxisShellList();
  112. }
  113. public static string ControllerType = "HDS";
  114. static AxisController _instance;
  115. public static AxisController GetInstance(string type = null)
  116. {
  117. if (_instance == null)
  118. {
  119. if (type != null)
  120. {
  121. ControllerType = type;
  122. }
  123. switch (ControllerType)
  124. {
  125. case "M3H":
  126. _instance = new M3HController();
  127. break;
  128. case "HDS":
  129. _instance = new HDSController();
  130. break;
  131. default:
  132. return null;
  133. }
  134. _instance.SetWorkspeedXY();
  135. }
  136. return _instance;
  137. }
  138. public abstract bool NeedSaveResetStageStatus { get; }
  139. public abstract bool FreeOrLockXYEnabled { get; }
  140. public abstract bool FreeOrLockZEnabled { get; }
  141. public abstract string Version { get; internal set; }
  142. public abstract bool IsOpen { get; }
  143. public virtual object LoadingStageModel
  144. {
  145. set
  146. {
  147. var m_loadingStageModel = value as PaintDotNet.Base.SettingModel.LoadingStageModel;
  148. m_AxisX.Stepping = double.Parse(m_loadingStageModel.SteppingX);
  149. m_AxisX.Reversed = true;
  150. m_AxisY.Stepping = double.Parse(m_loadingStageModel.SteppingY);
  151. m_AxisZ.Stepping = double.Parse(m_loadingStageModel.SteppingZ);
  152. //m_AxisZ.Reversed = true;//九孔台使用
  153. }
  154. }
  155. #region Position and State
  156. public abstract bool IsMoving { get; }
  157. public abstract void WaitMoveDone();
  158. #endregion Position and State
  159. public virtual string Open()
  160. {
  161. if (!m_running)
  162. {
  163. m_waitingThread = new Thread(new ThreadStart(Runtime));
  164. m_waitingThread.Start();
  165. m_running = true;
  166. }
  167. return "";
  168. }
  169. public virtual void Open(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
  170. {//M3H连接控制器的函数
  171. }
  172. public virtual void Close()
  173. {
  174. FreeStage();
  175. FreeZ();
  176. }
  177. public void AddApp(IStageEvent app)
  178. {
  179. var wk = new WeakReference(app);//弱引用
  180. m_listApp.Add((wk.Target as IStageEvent));
  181. }
  182. public void RemoveApp(IStageEvent app)
  183. {
  184. m_listApp.Remove(app);
  185. if (m_listApp.Count < 1)
  186. {
  187. FreeStage();
  188. FreeZ();
  189. }
  190. }
  191. protected virtual void ClearPosXY()
  192. {
  193. m_AxisX.ResetPosition();
  194. m_AxisY.ResetPosition();
  195. }
  196. public virtual void ClearPosZ()
  197. {
  198. m_AxisZ.ResetPosition();
  199. AxisShellList[AxisType.Z].ResetPosition();
  200. }
  201. public virtual void FreeStage()
  202. {
  203. StopMoveflow();
  204. AxisShellList[AxisType.X].Stop();
  205. AxisShellList[AxisType.Y].Stop();
  206. //Stop(AxisType.X, HandLock.F);
  207. //Stop(AxisType.Y, HandLock.F);
  208. }
  209. public virtual void FreeZ()
  210. {
  211. StopMoveflow();
  212. //Stop(AxisType.Z, HandLock.F);
  213. AxisShellList[AxisType.Z].Stop();
  214. }
  215. public virtual void LockStage()
  216. {
  217. StopMoveflow();
  218. _LockStage();
  219. }
  220. protected void _LockStage()
  221. {
  222. AxisShellList[AxisType.X].Stop();
  223. AxisShellList[AxisType.Y].Stop();
  224. Stop(AxisType.X, HandLock.S);
  225. Stop(AxisType.Y, HandLock.S);
  226. }
  227. public virtual void LockZ()
  228. {
  229. Stop(AxisType.Z, HandLock.S);
  230. AxisShellList[AxisType.Z].Stop();
  231. }
  232. public abstract void RefreshPosition();
  233. public abstract void SetWorkspeedXY();
  234. public virtual void SetSpeedXY(int speed)
  235. {
  236. var speed1 = SpeedConvertXY(speed);
  237. m_AxisX.Speed = speed1;
  238. m_AxisY.Speed = speed1;
  239. AxisShellList[AxisType.X].Speed = speed;
  240. AxisShellList[AxisType.Y].Speed = speed;
  241. }
  242. public virtual void SetSpeedZ(int speed)
  243. {
  244. m_AxisZ.Speed = SpeedConvertZ(speed);
  245. AxisShellList[AxisType.Z].Speed = speed;
  246. }
  247. /// <summary>
  248. /// XY速度换算
  249. /// </summary>
  250. /// <param name="">微米每秒</param>
  251. /// <returns>速度映射值</returns>
  252. private int SpeedConvertXY(double umpers)
  253. {
  254. var speed = (1000000 * m_AxisX.Stepping / umpers - 33) / 10;
  255. return (int)speed;
  256. }
  257. /// <summary>
  258. /// Z速度换算
  259. /// </summary>
  260. /// <param name="">微米每秒</param>
  261. /// <returns>速度映射值</returns>
  262. private int SpeedConvertZ(double umpers)
  263. {
  264. var speed = (1000000 * m_AxisZ.Stepping / umpers - 33) / 40;
  265. return (int)speed;
  266. }
  267. public virtual void Move(double x, double y)
  268. {
  269. if (m_currentMoveflow != null) StopMoveflow();
  270. if (IsMoving)
  271. {
  272. Console.WriteLine("XY运动中发送运动指令");
  273. return;
  274. }
  275. if (x != 0)
  276. {
  277. AxisShellList[AxisType.X].Move(x);
  278. m_AxisX.Move(x);
  279. }
  280. if (y != 0)
  281. {
  282. AxisShellList[AxisType.Y].Move(y);
  283. m_AxisY.Move(y);
  284. }
  285. }
  286. public virtual void To(double x, double y)
  287. {
  288. if (m_currentMoveflow != null) StopMoveflow();
  289. if (IsMoving) return;
  290. AxisShellList[AxisType.X].To(x);
  291. AxisShellList[AxisType.Y].To(y);
  292. m_AxisY.To(y);
  293. m_AxisX.To(x);
  294. }
  295. protected virtual void ToAction(AxisType axisType, double start)
  296. {
  297. AxisList[axisType].To(start);
  298. }
  299. protected virtual void MoveAction(AxisType axisType, double steplen)
  300. {
  301. AxisList[axisType].Move(steplen);
  302. }
  303. protected bool IsLimitXY()
  304. {
  305. return m_AxisX.Limit == LIMIT_N && m_AxisY.Limit == LIMIT_N;
  306. }
  307. protected virtual void Slide(AxisType axisType, bool isPositive)
  308. {
  309. switch (axisType)
  310. {
  311. case AxisType.X:
  312. m_AxisX.Slide(isPositive);
  313. break;
  314. case AxisType.Y:
  315. m_AxisY.Slide(isPositive);
  316. break;
  317. case AxisType.Z:
  318. m_AxisZ.Slide(isPositive);
  319. break;
  320. //case AxisType.W:
  321. // m_AxisX.Slide(isPositive);
  322. // break;
  323. default:
  324. break;
  325. }
  326. }
  327. public virtual void Split(int direction)
  328. {
  329. if (m_currentMoveflow != null) StopMoveflow();
  330. switch (direction)
  331. {
  332. case 0:
  333. m_AxisX.Slide(false);
  334. AxisShellList[AxisType.X].Slide(false);
  335. break;
  336. case 1:
  337. AxisShellList[AxisType.X].Slide(false);
  338. AxisShellList[AxisType.Y].Slide(false);
  339. m_AxisX.Slide(false);
  340. m_AxisY.Slide(false);
  341. break;
  342. case 2:
  343. AxisShellList[AxisType.Y].Slide(false);
  344. m_AxisY.Slide(false);
  345. break;
  346. case 3:
  347. AxisShellList[AxisType.X].Slide(true);
  348. AxisShellList[AxisType.Y].Slide(false);
  349. m_AxisX.Slide(true);
  350. m_AxisY.Slide(false);
  351. break;
  352. case 4:
  353. AxisShellList[AxisType.X].Slide(true);
  354. m_AxisX.Slide(true);
  355. break;
  356. case 5:
  357. AxisShellList[AxisType.X].Slide(true);
  358. AxisShellList[AxisType.Y].Slide(true);
  359. m_AxisX.Slide(true);
  360. m_AxisY.Slide(true);
  361. break;
  362. case 6:
  363. AxisShellList[AxisType.Y].Slide(true);
  364. m_AxisY.Slide(true);
  365. break;
  366. case 7:
  367. AxisShellList[AxisType.X].Slide(false);
  368. AxisShellList[AxisType.Y].Slide(true);
  369. m_AxisX.Slide(false);
  370. m_AxisY.Slide(true);
  371. break;
  372. }
  373. }
  374. public virtual void Up(double z)
  375. {
  376. if (m_currentMoveflow != null) StopMoveflow();
  377. if (IsMoving) return;
  378. AxisShellList[AxisType.Z].Move(z);
  379. m_AxisZ.Move(z);
  380. }
  381. public virtual void Up(int z)
  382. {
  383. if (m_currentMoveflow != null) StopMoveflow();
  384. if (IsMoving)
  385. {
  386. Console.WriteLine("Z运动中发送运动指令");
  387. return;
  388. }
  389. AxisShellList[AxisType.Z].Move(z);
  390. m_AxisZ.Move(z);
  391. }
  392. public virtual void UpTo(double z)
  393. {
  394. if (m_currentMoveflow != null) StopMoveflow();
  395. if (IsMoving) return;
  396. AxisShellList[AxisType.Z].To(z);
  397. m_AxisZ.To(z);
  398. }
  399. /// <summary>
  400. /// Z轴连续运动
  401. /// </summary>
  402. /// <param name="isToTop">true:上;false:下</param>
  403. public virtual void GoTop(bool isToTop)
  404. {
  405. if (m_currentMoveflow != null) StopMoveflow();
  406. AxisShellList[AxisType.Z].Slide(isToTop);
  407. m_AxisZ.Slide(isToTop);
  408. }
  409. /// <summary>
  410. /// 平台中心位置
  411. /// </summary>
  412. protected virtual int _centerX => 0;
  413. /// <summary>
  414. /// 平台中心位置
  415. /// </summary>
  416. protected virtual int _centerY => 0;
  417. /// <summary>
  418. /// 轴锁定状态常量
  419. /// </summary>
  420. const int STATE_LOCK = 1;
  421. /// <summary>
  422. /// 负边界
  423. /// </summary>
  424. const int LIMIT_N = (int)Border.R;
  425. /// <summary>
  426. /// 边界内
  427. /// </summary>
  428. const int LIMIT_I = (int)Border.I;
  429. public virtual void ToCenter()
  430. {
  431. var mf = StartMoveflow();
  432. mf.Add(() =>
  433. {
  434. _LockStage();
  435. },
  436. () => { return m_AxisX.State == STATE_LOCK && m_AxisY.State == STATE_LOCK; });
  437. mf.Add(() =>
  438. {
  439. m_AxisX.Slide(false);
  440. m_AxisY.Slide(false);
  441. },
  442. () => { return m_AxisX.Limit == LIMIT_N && m_AxisY.Limit == LIMIT_N; });
  443. mf.Add(
  444. () =>
  445. {
  446. m_AxisY.Move(_centerY);
  447. m_AxisX.Move(_centerX);
  448. },
  449. () => { return m_AxisX.Limit == LIMIT_I || m_AxisY.Limit == LIMIT_I; });
  450. mf.Add(() => { }, () => { return m_AxisX.State == STATE_LOCK && m_AxisY.State == STATE_LOCK; });
  451. mf.Start();
  452. m_currentMoveflow = mf;
  453. }
  454. public virtual void ZScan(double start, double steplen, int times, Action shoot, Action package, Action actDone)
  455. {
  456. var mf = StartMoveflow();
  457. mf.Add(
  458. () => m_AxisZ.Stop(HandLock.S),
  459. null);
  460. mf.Add(() =>
  461. {
  462. m_AxisZ.To(start);
  463. },
  464. () =>
  465. {
  466. RefreshPosition();
  467. return Math.Abs(m_AxisZ.Actual - start) < m_AxisZ.Stepping;
  468. });
  469. mf.Add(shoot, null);
  470. for (int i = 0; i < times - 1; i++)
  471. {
  472. mf.Add(() =>
  473. {
  474. m_AxisZ.Move(steplen);
  475. start += steplen;
  476. },
  477. () =>
  478. {
  479. RefreshPosition();
  480. return Math.Abs(m_AxisZ.Actual - start) < m_AxisZ.Stepping;
  481. });
  482. mf.Add(shoot, null);
  483. }
  484. mf.Add(package, null);
  485. mf.Start();
  486. mf.OnMoveflowDone += actDone;
  487. m_currentMoveflow = mf;
  488. }
  489. public abstract void ResetStage(Action actDone);
  490. public virtual void SetMergeMode(bool isMerge) { }
  491. public virtual void SetRockerEnabel(bool isAvailable) { }
  492. protected virtual void CommandError()
  493. {//M3H连接控制器的函数
  494. Logs.Write("CommandError");
  495. foreach (var app in m_listApp) app.OnErrorSend();
  496. }
  497. protected virtual void CommandTimeout()
  498. {//M3H连接控制器的函数
  499. Logs.Write("CommandTimeout");
  500. Console.Write("CommandTimeout");
  501. try
  502. {
  503. foreach (var app in m_listApp) app.OnTimeoutConnect();
  504. }
  505. catch { }
  506. }
  507. protected void PositionUpdate(CommandBase cmd)
  508. {
  509. var posInfo = (CommandAllPosition)cmd;
  510. m_AxisX.Step = posInfo.XStep;
  511. m_AxisY.Step = posInfo.YStep;
  512. m_AxisZ.Step = posInfo.ZStep;
  513. m_AxisX.Limit = (int)posInfo.XBorder;
  514. m_AxisY.Limit = (int)posInfo.YBorder;
  515. m_AxisZ.Limit = (int)posInfo.ZBorder;
  516. m_AxisX.State = posInfo.XState;
  517. m_AxisY.State = posInfo.YState;
  518. m_AxisZ.State = posInfo.ZState;
  519. AxisShellList[AxisType.X].Step = (m_AxisX.Reversed ? -posInfo.XStep : posInfo.XStep) * m_AxisX.Stepping;
  520. AxisShellList[AxisType.Y].Step = posInfo.YStep * m_AxisY.Stepping;
  521. AxisShellList[AxisType.Z].Step = posInfo.ZStep * m_AxisZ.Stepping;
  522. AxisShellList[AxisType.X].Limit = (int)posInfo.XBorder;
  523. AxisShellList[AxisType.Y].Limit = (int)posInfo.YBorder;
  524. AxisShellList[AxisType.Z].Limit = (int)posInfo.ZBorder;
  525. AxisShellList[AxisType.X].State = posInfo.XState;
  526. AxisShellList[AxisType.Y].State = posInfo.YState;
  527. AxisShellList[AxisType.Z].State = posInfo.ZState;
  528. // Add by shayg 20220615 start
  529. AxisShellList[AxisType.X].ExcessStep = posInfo.XExcessStep;
  530. AxisShellList[AxisType.Y].ExcessStep = posInfo.YExcessStep;
  531. AxisShellList[AxisType.Z].ExcessStep = posInfo.ZExcessStep;
  532. // Add by shayg 20220615 end
  533. for (int i = 0; i < m_listApp.Count; i++)
  534. {
  535. var app = m_listApp[i];
  536. if (app == null)
  537. m_listApp.RemoveAt(i--);
  538. else
  539. app.OnUpdatePosition();
  540. }
  541. }
  542. #region Stop
  543. protected virtual void Stop(AxisType axis, HandLock hlock)
  544. {
  545. AxisList[axis].Stop(hlock);
  546. }
  547. #endregion
  548. }
  549. }