IoMfc2.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. using System;
  2. using System.Xml;
  3. using Aitex.Core.Common.DeviceData;
  4. using Aitex.Core.RT.DataCenter;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.IOCore;
  7. using Aitex.Core.RT.OperationCenter;
  8. using Aitex.Core.RT.SCCore;
  9. using Aitex.Core.RT.Tolerance;
  10. using Aitex.Core.Util;
  11. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.MFCs;
  12. namespace Aitex.Core.RT.Device.Unit
  13. {
  14. public class IoMfc2 : BaseDevice, IDevice, IMfc
  15. {
  16. public string Unit
  17. {
  18. get; set;
  19. }
  20. [Subscription(AITMfcDataPropertyName.Scale)]
  21. public double Scale
  22. {
  23. get
  24. {
  25. if (_scN2Scale == null || _scScaleFactor == null)
  26. return 0;
  27. return _scN2Scale.DoubleValue * _scScaleFactor.DoubleValue;
  28. }
  29. }
  30. [Subscription(AITMfcDataPropertyName.SetPoint)]
  31. public double SetPoint
  32. {
  33. get
  34. {
  35. if (_aoFlow != null)
  36. {
  37. return _aoFlow.Value;
  38. }
  39. return 0;
  40. }
  41. set
  42. {
  43. if (_aoFlow != null)
  44. {
  45. _aoFlow.Value = (short)value;
  46. }
  47. }
  48. }
  49. [Subscription(AITMfcDataPropertyName.DefaultSetPoint)]
  50. public double DefaultSetPoint
  51. {
  52. get
  53. {
  54. if (_scDefaultSetPoint != null)
  55. return _scDefaultSetPoint.DoubleValue;
  56. return 0;
  57. }
  58. }
  59. [Subscription(AITMfcDataPropertyName.FeedBack)]
  60. public double FeedBack
  61. {
  62. get
  63. {
  64. if (_aiFlow != null)
  65. return (_scRegulationFactor!=null && _scRegulationFactor.DoubleValue > 0.001) ? _aiFlow.Value / _scRegulationFactor.DoubleValue : _aiFlow.Value;
  66. return 0;
  67. }
  68. }
  69. [Subscription(AITMfcDataPropertyName.IsOutOfTolerance)]
  70. public bool IsOutOfTolerance
  71. {
  72. get
  73. {
  74. return _toleranceChecker.Result;
  75. }
  76. }
  77. [Subscription(AITMfcDataPropertyName.IsEnableAlarm)]
  78. public bool EnableAlarm
  79. {
  80. get
  81. {
  82. if (_scEnableAlarm != null)
  83. return _scEnableAlarm.BoolValue;
  84. return false;
  85. }
  86. }
  87. [Subscription(AITMfcDataPropertyName.AlarmRange)]
  88. public double AlarmRange
  89. {
  90. get
  91. {
  92. if (_scAlarmRange != null)
  93. return _scAlarmRange.DoubleValue;
  94. return 0;
  95. }
  96. }
  97. [Subscription(AITMfcDataPropertyName.AlarmTime)]
  98. public double AlarmTime
  99. {
  100. get
  101. {
  102. if (_scAlarmTime != null)
  103. return _scAlarmTime.IntValue;
  104. return 0;
  105. }
  106. }
  107. [Subscription(AITMfcDataPropertyName.IsOffline)]
  108. public bool IsOffline
  109. {
  110. get
  111. {
  112. if (_diOffline != null)
  113. return _diOffline.Value;
  114. return false;
  115. }
  116. }
  117. public string DisplayName
  118. {
  119. get
  120. {
  121. if (_scGasName != null)
  122. return _scGasName.StringValue;
  123. return Display;
  124. }
  125. }
  126. private AITMfcData DeviceData
  127. {
  128. get
  129. {
  130. AITMfcData data = new AITMfcData()
  131. {
  132. UniqueName = _uniqueName,
  133. Type = "MFC",
  134. DeviceName = Name,
  135. DeviceSchematicId = DeviceID,
  136. DisplayName = DisplayName,
  137. FeedBack = FeedBack,
  138. SetPoint = SetPoint,
  139. Scale = Scale,
  140. IsOffline = IsOffline,
  141. };
  142. return data;
  143. }
  144. }
  145. private DeviceTimer rampTimer = new DeviceTimer();
  146. private double rampTarget;
  147. private double rampInitValue;
  148. private int rampTime;
  149. private ToleranceChecker _toleranceChecker = new ToleranceChecker();
  150. private AIAccessor _aiFlow;
  151. private AOAccessor _aoFlow;
  152. private AOAccessor _aoRange;
  153. private DIAccessor _diOffline;
  154. private SCConfigItem _scGasName;
  155. private SCConfigItem _scEnable;
  156. private SCConfigItem _scN2Scale;
  157. private SCConfigItem _scScaleFactor;
  158. private SCConfigItem _scAlarmRange;
  159. private SCConfigItem _scEnableAlarm;
  160. private SCConfigItem _scAlarmTime;
  161. private SCConfigItem _scDefaultSetPoint;
  162. private SCConfigItem _scRegulationFactor;
  163. private R_TRIG _trigOffline = new R_TRIG();
  164. private string _uniqueName;
  165. public IoMfc2(string module, XmlElement node, string ioModule="")
  166. {
  167. Unit = node.GetAttribute("unit");
  168. base.Module = string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module");
  169. base.Name = node.GetAttribute("id");
  170. base.Display = node.GetAttribute("display");
  171. base.DeviceID = node.GetAttribute("schematicId");
  172. _aoRange = ParseAoNode("aoRange", node, ioModule);
  173. _diOffline = ParseDiNode("diOffline", node, ioModule);
  174. _aiFlow = ParseAiNode("aiFlow", node, ioModule);
  175. _aoFlow = ParseAoNode("aoFlow", node, ioModule);
  176. string scBasePath = node.GetAttribute("scBasePath");
  177. if (string.IsNullOrEmpty(scBasePath))
  178. scBasePath = $"{Module}.{Name}";
  179. else
  180. {
  181. scBasePath = scBasePath.Replace("{module}", Module);
  182. }
  183. _scGasName = ParseScNode("scGasName",node, ioModule, $"{scBasePath}.{Name}.GasName");
  184. _scEnable = ParseScNode("scEnable",node, ioModule, $"{scBasePath}.{Name}.Enable");
  185. _scN2Scale = ParseScNode("scN2Scale",node, ioModule, $"{scBasePath}.{Name}.N2Scale");
  186. _scScaleFactor = ParseScNode("scScaleFactor",node, ioModule, $"{scBasePath}.{Name}.ScaleFactor");
  187. _scAlarmRange = ParseScNode("scAlarmRange",node, ioModule, $"{scBasePath}.{Name}.AlarmRange");
  188. _scEnableAlarm = ParseScNode("scEnableAlarm",node, ioModule, $"{scBasePath}.{Name}.EnableAlarm");
  189. _scAlarmTime = ParseScNode("scAlarmTime",node, ioModule, $"{scBasePath}.{Name}.AlarmTime");
  190. _scDefaultSetPoint = ParseScNode("scDefaultSetPoint",node, ioModule, $"{scBasePath}.{Name}.DefaultSetPoint");
  191. _scRegulationFactor = ParseScNode("scFlowRegulationFactor",node, ioModule, $"{scBasePath}.{Name}.RegulationFactor");
  192. _uniqueName = $"{Module}.{Name}";
  193. }
  194. public bool Initialize()
  195. {
  196. DATA.Subscribe($"{_uniqueName}.DeviceData", () => DeviceData);
  197. DATA.Subscribe($"Device.{Module}.{Name}", () => DeviceData);
  198. OP.Subscribe($"{_uniqueName}.{AITMfcOperation.Ramp}", InvokeRamp);
  199. DEVICE.Register(String.Format("{0}.{1}", Name, AITMfcOperation.Ramp), (out string reason, int time, object[] param) =>
  200. {
  201. double target = Convert.ToDouble((string)param[0]);
  202. target = Math.Min(target, Scale);
  203. target = Math.Max(target, 0);
  204. Ramp(target, time);
  205. reason = string.Format("{0} ramp to {1}{2}", Display, target, Unit);
  206. return true;
  207. });
  208. //@AAA use recipe
  209. DEVICE.Register(String.Format("{0}", Name), (out string reason, int time, object[] param) =>
  210. {
  211. double target = Convert.ToDouble((string)param[0]);
  212. target = Math.Min(target, Scale);
  213. target = Math.Max(target, 0);
  214. Ramp(target, time);
  215. reason = string.Format("{0} ramp to {1}{2}", Display, target, Unit);
  216. return true;
  217. });
  218. return true;
  219. }
  220. private bool InvokeRamp(string method, object[] args)
  221. {
  222. double target = Convert.ToDouble((string)(args[0].ToString()));
  223. target = Math.Min(target, Scale);
  224. target = Math.Max(target, 0);
  225. int time = 0;
  226. if (args.Length >= 2)
  227. time = Convert.ToInt32((string)(args[1].ToString()));
  228. Ramp(target, time);
  229. EV.PostInfoLog(Module, $"{_uniqueName} ramp to {target}{Unit} in {time} seconds");
  230. return true;
  231. }
  232. public void Monitor()
  233. {
  234. Ramping();
  235. CheckTolerance();
  236. if (_aoRange != null)
  237. _aoRange.Value = (short)Scale;
  238. _trigOffline.CLK = IsOffline;
  239. if (_trigOffline.Q)
  240. {
  241. EV.PostMessage(Module, EventEnum.DefaultAlarm, string.Format("{0} is offline", DisplayName));
  242. }
  243. }
  244. public void Reset()
  245. {
  246. _toleranceChecker.Reset(AlarmTime);
  247. _trigOffline.RST = true;
  248. }
  249. public void Terminate()
  250. {
  251. Ramp(DefaultSetPoint, 0);
  252. }
  253. public bool Ramp(double flowSetPoint, int time, out string reason)
  254. {
  255. if (HasAlarm)
  256. {
  257. reason = $"{DisplayName} in error status, can not flow";
  258. return false;
  259. }
  260. if (flowSetPoint < 0 || flowSetPoint > Scale)
  261. {
  262. reason = $"{DisplayName} range is [0, {Scale}], can not flow {flowSetPoint}";
  263. return false;
  264. }
  265. EV.PostInfoLog(Module, $"{DisplayName} flow {flowSetPoint} {Unit} in {time} seconds");
  266. Ramp(flowSetPoint, time);
  267. reason = string.Empty;
  268. return true;
  269. }
  270. public void Ramp(int time)
  271. {
  272. Ramp(DefaultSetPoint, time);
  273. }
  274. public void Ramp(double target, int time)
  275. {
  276. target = Math.Max(0, target);
  277. target = Math.Min(Scale, target);
  278. rampInitValue = SetPoint; //ramp 初始值取当前设定值,而非实际读取值.零漂问题
  279. rampTime = time;
  280. rampTarget = target;
  281. rampTimer.Start(rampTime);
  282. }
  283. public void StopRamp()
  284. {
  285. Ramp(SetPoint, 0);
  286. }
  287. private void Ramping()
  288. {
  289. if (rampTimer.IsTimeout() || rampTime == 0)
  290. {
  291. SetPoint = rampTarget;
  292. }
  293. else
  294. {
  295. SetPoint = rampInitValue + (rampTarget - rampInitValue) * rampTimer.GetElapseTime() / rampTime;
  296. }
  297. }
  298. private void CheckTolerance()
  299. {
  300. if (!EnableAlarm)
  301. {
  302. _toleranceChecker.RST = true;
  303. return;
  304. }
  305. if (SetPoint < 0.1)
  306. {
  307. _toleranceChecker.RST = true;
  308. return;
  309. }
  310. _toleranceChecker.Monitor(FeedBack, (SetPoint-Math.Abs(AlarmRange)), (SetPoint+Math.Abs(AlarmRange)), AlarmTime);
  311. if (_toleranceChecker.Trig)
  312. {
  313. EV.PostAlarmLog(Module, $"{Name} flow out of range {AlarmRange} sccm in {AlarmTime:F0} seconds" );
  314. }
  315. }
  316. }
  317. }