AxisController.cs 18 KB

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