M3HController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using PaintDotNet.Base.SettingModel;
  8. namespace StageController
  9. {
  10. public class M3HController : AxisController
  11. {
  12. #region Com Connection
  13. private SerialPort m_serialPort;
  14. private string m_portName;
  15. private int m_baudRate;
  16. #endregion
  17. public override bool IsMoving => StateX > 1 || StateY > 1 || StateZ > 1;
  18. /// <summary>
  19. /// 等待平台停稳
  20. /// </summary>
  21. public override void WaitMoveDone()
  22. {
  23. // Console.WriteLine(string.Format("{0} {1} {2}", StateX, StateY, StateZ));
  24. do Thread.Sleep(30);
  25. while (IsMoving && IsOpen);
  26. Thread.Sleep(100);
  27. }
  28. public M3HController() : base()
  29. {
  30. m_cmdQueueM = new ConcurrentQueue<CommandBase>();
  31. CommandBase.AddReceivedHandle(typeof(CommandAllPosition), new Action<CommandBase>(PositionUpdate));
  32. CommandBase.AddReceivedHandle(typeof(CommandVersion), new Action<CommandBase>(VersionUpdate));
  33. }
  34. public override object LoadingStageModel
  35. {
  36. set
  37. {
  38. var m_loadingStageModel = value as LoadingStageModel;
  39. m_portName = m_loadingStageModel.BaseSetPort;
  40. m_baudRate = m_loadingStageModel.BaseSetBps;
  41. base.LoadingStageModel = value;
  42. m_workSpeedXY = m_loadingStageModel.items[m_loadingStageModel.WorkSpeedSelect].LSpeed;
  43. m_workSpeedZ = m_loadingStageModel.items[m_loadingStageModel.WorkSpeedSelect].ZSpeed;
  44. }
  45. }
  46. #region Open Close
  47. /// <summary>
  48. /// 平台是否正常开启
  49. /// </summary>
  50. public override bool IsOpen
  51. {
  52. get => m_running;
  53. }
  54. public override string Version { get; internal set; }
  55. // TODO: 此函数测试期间使用
  56. public override string Open()
  57. {
  58. try
  59. {
  60. if (!m_running)
  61. Open(m_portName, m_baudRate, Parity.None, 8, StopBits.One);
  62. return "";
  63. }
  64. catch (Exception ex)
  65. {
  66. return ex.Message;
  67. }
  68. }
  69. public override void Open(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
  70. {
  71. if (m_serialPort == null || !m_serialPort.IsOpen)
  72. {
  73. m_serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
  74. m_serialPort.Encoding = Encoding.ASCII;
  75. m_serialPort.ReadTimeout = 500;
  76. m_serialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived);
  77. m_serialPort.Open();
  78. var cmd = new CommandGlobal(M3H.ControlType.F);
  79. cmd.Send();
  80. Thread.Sleep(100);
  81. if (cmd.State == 1)
  82. throw new Exception("Connection Failed!");
  83. CommandBase.SerialPort = m_serialPort;
  84. }
  85. if (!m_running)
  86. {
  87. m_cmdQueueM = new ConcurrentQueue<CommandBase>();
  88. m_curCommand = null;
  89. base.Open();
  90. ResetParameter();
  91. }
  92. }
  93. public override void Close()
  94. {
  95. if (IsOpen)
  96. {
  97. FreeStage();
  98. FreeZ();
  99. _closing = true;
  100. }
  101. }
  102. #endregion
  103. // 接收串口返回数据
  104. private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
  105. {
  106. if (!m_running) return;
  107. string recvData = ((SerialPort)sender).ReadExisting();
  108. m_curCommand.Receive(recvData);
  109. }
  110. #region Command Runtime
  111. private CommandBase m_curCommand = null;
  112. /// <summary>
  113. /// 发送试错次数
  114. /// </summary>
  115. private int m_retryCount = 3;
  116. protected override void Process()
  117. {
  118. base.Process();
  119. #region 执行当前指令
  120. if (m_curCommand != null)
  121. {
  122. switch (m_curCommand.State)
  123. {
  124. case -1: //error:停止当前运动控制服务.
  125. if (m_curCommand.SendCount > m_retryCount) CommandError();
  126. m_curCommand.Send();
  127. return;
  128. case 1: //等待平台应答
  129. if (m_curCommand.IsTimeout)
  130. {
  131. if (_timeoutTimes > 1)
  132. CommandTimeout();
  133. m_curCommand = null;
  134. _timeoutTimes++;
  135. }
  136. break;
  137. case 2: //完成应答处理
  138. m_curCommand = null; _timeoutTimes = 0;
  139. break;
  140. case 0: //初始值, 此处不应该出现0的情况
  141. CommandError();
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. #endregion
  148. #region 开始一个新指令,
  149. if (m_curCommand == null)
  150. {
  151. if (m_cmdQueueM.IsEmpty && _closing)
  152. {
  153. m_running = false;
  154. _closing = false;
  155. return;
  156. }
  157. if (m_cmdQueueM.IsEmpty || !m_cmdQueueM.TryDequeue(out m_curCommand))
  158. { // 空闲时间刷新位置
  159. //m_curCommand = new CommandAllPosition();
  160. }
  161. if (m_curCommand != null && !m_curCommand.Send())
  162. { //指令发送失败
  163. CommandError();
  164. }
  165. }
  166. #endregion
  167. }
  168. protected override void CommandError()
  169. {
  170. Dispose();
  171. base.CommandError();
  172. }
  173. private void Dispose()
  174. {
  175. m_running = false;
  176. m_serialPort.Close();
  177. m_curCommand = null;
  178. m_currentMoveflow?.Stop();
  179. }
  180. int _timeoutTimes = 0;
  181. protected override void CommandTimeout()
  182. {
  183. Dispose();
  184. base.CommandTimeout();
  185. }
  186. public override void RefreshPosition()
  187. {
  188. if (m_cmdQueueM.Any(e => e is CommandAllPosition))
  189. return;
  190. AddCommand(new CommandAllPosition());
  191. }
  192. private void VersionUpdate(CommandBase cmd)
  193. {
  194. Version = (cmd as CommandVersion).Version;
  195. }
  196. #endregion
  197. #region Moveflow
  198. private bool IsSimulation = false;
  199. /// <summary>
  200. /// 平台中心位置
  201. /// </summary>
  202. protected override int _centerX => 106500;
  203. /// <summary>
  204. /// 平台中心位置
  205. /// </summary>
  206. protected override int _centerY => 73500;
  207. /// <summary>
  208. /// 正边界
  209. /// </summary>
  210. const int LIMIT_P = (int)Border.F;
  211. private bool _closing;
  212. /// <summary>
  213. /// 开始中心移动流程
  214. /// </summary>
  215. public override void ToCenter()
  216. {
  217. if (IsSimulation) return;
  218. base.ToCenter();
  219. }
  220. protected override void ClearPosXY()
  221. {
  222. base.ClearPosXY();
  223. AddCommand(new CommandAllPosition());
  224. }
  225. /// <summary>
  226. /// 开始复位流程
  227. /// </summary>
  228. public override void ResetStage(Action actDone)
  229. {
  230. if (IsSimulation)//模拟模式
  231. {
  232. ClearPosXY();
  233. actDone();
  234. return;
  235. }
  236. new System.Threading.Tasks.Task(new Action(() =>
  237. {
  238. while (m_cmdQueueM.Count > 0)
  239. {
  240. Thread.Sleep(100);
  241. }
  242. StartResetStage(actDone);
  243. })).Start();
  244. }
  245. private void StartResetStage(Action actDone)
  246. {
  247. var mf = StartMoveflow();
  248. mf.Add(() =>
  249. {
  250. _LockStage();
  251. },
  252. null
  253. );
  254. mf.Add(() =>
  255. {
  256. this.Slide(AxisType.X, false);
  257. this.Slide(AxisType.Y, false);
  258. },
  259. () =>
  260. {
  261. RefreshPosition();
  262. return IsLimitXY();// m_AxisX.Limit == LIMIT_N && m_AxisY.Limit == LIMIT_N;
  263. });
  264. mf.Add(() =>
  265. {
  266. ClearPosXY();
  267. },
  268. null);
  269. mf.Start();
  270. mf.OnMoveflowDone += actDone;
  271. m_currentMoveflow = mf;
  272. }
  273. public override void ZScan(double start, double steplen, int times, Action shoot, Action package, Action actDone)
  274. {
  275. if (IsSimulation)//模拟模式
  276. return;
  277. base.ZScan(start, steplen, times, shoot, package, actDone);
  278. }
  279. #endregion
  280. #region Speed
  281. int m_workSpeedXY;
  282. int m_workSpeedZ;
  283. public override void SetWorkspeedXY()
  284. {
  285. SetSpeedXY(m_workSpeedXY);
  286. SetSpeedZ(m_workSpeedZ);
  287. }
  288. #endregion
  289. public void ResetParameter()
  290. {
  291. AddCommand(new CommandAllPosition());
  292. AddCommand(new CommandParameter() { AxisSelect = 'x' });
  293. AddCommand(new CommandParameter() { AxisSelect = 'y' });
  294. // AddCommand(new CommandParameter() { AxisSelect = 'z', LockOrFree = 'F' }); // z轴参数下发不识别
  295. AddCommand(new CommandVersion());
  296. }
  297. /// <summary>
  298. /// 拼图模式
  299. /// </summary>
  300. public override void SetMergeMode(bool isMerge)
  301. {
  302. if (isMerge)
  303. AddCommand(new CommandGlobal(M3H.ControlType.P));
  304. else
  305. AddCommand(new CommandGlobal(M3H.ControlType.Q));
  306. }
  307. /// <summary>
  308. /// 启用/禁用摇杆
  309. /// </summary>
  310. /// <param name="isAvailable">摇杆可用</param>
  311. public override void SetRockerEnabel(bool isAvailable)
  312. {
  313. }
  314. }
  315. }