GalilControllerCfgManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.SCCore;
  4. using Aitex.Core.Util;
  5. using MECF.Framework.Common.Device.Festo;
  6. using MECF.Framework.Common.Equipment;
  7. using MECF.Framework.Common.IOCore;
  8. using MECF.Framework.Common.Net;
  9. using System;
  10. using System.Collections.Concurrent;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Reflection;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. namespace MECF.Framework.Common.Device.Galil
  18. {
  19. public class GalilControllerCfgManager : Singleton<GalilControllerCfgManager>
  20. {
  21. #region 常量
  22. private const string STOPCODE = "StopCode";
  23. private const string REFERENCE_POSITION = "ReferencePosition";
  24. private const string TORQUE = "Torque";
  25. private const string VELOCITY = "Velocity";
  26. private const string POSITION_ERROR = "PositionError";
  27. private const string AUXILIARY_POSITION="AuxiliaryPosition";
  28. private const string GAILI_21 = "Galil21";
  29. private const string GALIL_40 = "Galil40";
  30. #endregion
  31. #region 内部变量
  32. /// <summary>
  33. /// 模块数据字典(key-模块名称,value-模块Galil数据)
  34. /// </summary>
  35. private Dictionary<string, GalilControllerData> _moduleGalilDataDictionary = new Dictionary<string, GalilControllerData>();
  36. /// <summary>
  37. /// 电机数据字典(key-电机名称,value-电机数据)
  38. /// </summary>
  39. private Dictionary<string, GalilAxisData> _moduleNameAxisDataDictionary = new Dictionary<string, GalilAxisData>();
  40. /// <summary>
  41. /// 电机索引字典(key-电机名称,value-索引)
  42. /// </summary>
  43. private Dictionary<string, int> _moduleNameIndexDictionary = new Dictionary<string, int>();
  44. /// <summary>
  45. /// 模块电机集合字典(key-模块名称,value-电机集合)
  46. /// </summary>
  47. private Dictionary<string,List<string>> _moduleNameLstDictionary= new Dictionary<string,List<string>>();
  48. /// <summary>
  49. /// 模块电机配置字典(key-电机名称,value-电机配置)
  50. /// </summary>
  51. private Dictionary<string, GalilAxisConfig> _moduleNameAxisConfigDictionary = new Dictionary<string, GalilAxisConfig>();
  52. /// <summary>
  53. /// 模块设备配置字典
  54. /// </summary>
  55. private Dictionary<string, GalilDeviceConfig> _moduleDeviceConfigDictionary = new Dictionary<string, GalilDeviceConfig>();
  56. /// <summary>
  57. /// 模块电机tcp设备字典(key-模块名称,value-tcp设备)
  58. /// </summary>
  59. private Dictionary<string, IGalilDevice> _moduleGalilTcpDeviceDictionary = new Dictionary<string, IGalilDevice>();
  60. /// <summary>
  61. /// 模块DI数组字典
  62. /// </summary>
  63. private ConcurrentDictionary<string, byte[]> _moduleDiDictionary = new ConcurrentDictionary<string, byte[]>();
  64. /// <summary>
  65. /// 电机
  66. /// </summary>
  67. private char[] _axisArray = null;
  68. #endregion
  69. /// <summary>
  70. /// 初始化
  71. /// </summary>
  72. public void Initialize()
  73. {
  74. bool isSimulate = SC.GetValue<bool>("System.IsSimulatorMode");
  75. string xmlPath = "";
  76. try
  77. {
  78. if (isSimulate)
  79. {
  80. xmlPath = PathManager.GetCfgDir() + "Devices\\GalilControllerCfg-Simulator.xml";
  81. }
  82. else
  83. {
  84. xmlPath = PathManager.GetCfgDir() + "Devices\\GalilControllerCfg.xml";
  85. }
  86. GalilControllerCfg cfg = CustomXmlSerializer.Deserialize<GalilControllerCfg>(new FileInfo(xmlPath));
  87. if (cfg != null)
  88. {
  89. foreach (var config in cfg.GalilDeviceConfigs)
  90. {
  91. InitializeGalilDevice(config);
  92. if (config.GalilType == GAILI_21)
  93. {
  94. Galil21TcpDevice galilDevice = new Galil21TcpDevice(config.Module, config.IpAddress, config.Port);
  95. galilDevice.ReceiveTimeout = config.RecvTimeout;
  96. galilDevice.SendTimeout = config.SendTimeout;
  97. galilDevice.Initialize();
  98. _moduleGalilTcpDeviceDictionary[config.Module] = galilDevice;
  99. }
  100. else
  101. {
  102. Galil40TcpDevice galilDevice = new Galil40TcpDevice(config.Module, config.IpAddress, config.Port);
  103. galilDevice.ReceiveTimeout = config.RecvTimeout;
  104. galilDevice.SendTimeout = config.SendTimeout;
  105. galilDevice.Initialize();
  106. _moduleGalilTcpDeviceDictionary[config.Module] = galilDevice;
  107. }
  108. }
  109. }
  110. }
  111. catch
  112. {
  113. LOG.WriteLog(eEvent.ERR_GALIL, "Galil", "Load galil xml failed");
  114. }
  115. }
  116. /// <summary>
  117. /// 初始化Galil 设备
  118. /// </summary>
  119. /// <param name="deviceConfig"></param>
  120. private void InitializeGalilDevice(GalilDeviceConfig deviceConfig)
  121. {
  122. List<string> lst = new List<string>();
  123. int maxIndex = 0;
  124. _moduleDeviceConfigDictionary[deviceConfig.Module] = deviceConfig;
  125. foreach(var item in deviceConfig.GalilAxises)
  126. {
  127. _moduleNameIndexDictionary[$"{deviceConfig.Module}.{item.Name}"] = item.Index;
  128. _moduleNameAxisConfigDictionary[$"{deviceConfig.Module}.{item.Name}"]=item;
  129. if (!lst.Contains(item.Name))
  130. {
  131. lst.Add(item.Name);
  132. }
  133. if (maxIndex < item.Index)
  134. {
  135. maxIndex= item.Index;
  136. }
  137. }
  138. if (lst.Count > 0)
  139. {
  140. _moduleNameLstDictionary[deviceConfig.Module] = lst;
  141. }
  142. _axisArray = new char[10];
  143. int a = 'A';
  144. for (int i = 0; i < _axisArray.Length; i++)
  145. {
  146. _axisArray[i] = (char)(a + i);
  147. }
  148. }
  149. /// <summary>
  150. /// 更新模块数据
  151. /// </summary>
  152. /// <param name="controllerData"></param>
  153. public void UpdateModuleData(string module,GalilControllerData controllerData)
  154. {
  155. if (!_moduleGalilDataDictionary.ContainsKey(module))
  156. {
  157. _moduleGalilDataDictionary[module] = controllerData;
  158. }
  159. if (!_moduleNameLstDictionary.ContainsKey(module))
  160. {
  161. return;
  162. }
  163. UpdateModuleDI(module, controllerData.Inputs);
  164. List<string> lst = _moduleNameLstDictionary[module];
  165. foreach (var item in lst)
  166. {
  167. string moduleName = $"{module}.{item}";
  168. if (!_moduleNameIndexDictionary.ContainsKey(moduleName))
  169. {
  170. continue;
  171. }
  172. int index = _moduleNameIndexDictionary[moduleName];
  173. if(index>=controllerData.GalilAxisDatas.Count)
  174. {
  175. continue;
  176. }
  177. GalilAxisData galilAxisData=controllerData.GalilAxisDatas[index];
  178. CheckAxisDataChanged(moduleName, galilAxisData);
  179. }
  180. }
  181. /// <summary>
  182. /// 更新ModuleDI
  183. /// </summary>
  184. /// <param name="module"></param>
  185. /// <param name="diValues"></param>
  186. private void UpdateModuleDI(string module, byte[] diValues)
  187. {
  188. if (!_moduleDeviceConfigDictionary.ContainsKey(module))
  189. {
  190. return;
  191. }
  192. GalilDeviceConfig galilDeviceConfig = _moduleDeviceConfigDictionary[module];
  193. if (galilDeviceConfig.GalilDigIn == null || galilDeviceConfig.GalilDigIn.DIs == null)
  194. {
  195. return;
  196. }
  197. bool isInitial = false;
  198. if (!_moduleDiDictionary.ContainsKey(module))
  199. {
  200. _moduleDiDictionary[module] = new byte[diValues.Length];
  201. isInitial=true;
  202. }
  203. if (_moduleDiDictionary[module].Length == diValues.Length)
  204. {
  205. for(int i=0;i<diValues.Length;i++)
  206. {
  207. byte item= _moduleDiDictionary[module][i];
  208. byte diValue = diValues[i];
  209. if (isInitial)
  210. {
  211. _moduleDiDictionary[module][i]= diValue;
  212. UpdateDIValue(galilDeviceConfig,diValue, i);
  213. }
  214. else if (item != diValue)
  215. {
  216. _moduleDiDictionary[module][i] = diValue;
  217. UpdateDIValue(galilDeviceConfig, diValue, i);
  218. }
  219. }
  220. }
  221. }
  222. /// <summary>
  223. /// 更新DI数值
  224. /// </summary>
  225. /// <param name="byt"></param>
  226. /// <param name="index"></param>
  227. private void UpdateDIValue(GalilDeviceConfig deviceConfig,byte byt,int index)
  228. {
  229. List<GalilDI> dIs = deviceConfig.GalilDigIn.DIs.FindAll(O => O.Address == index);
  230. foreach(GalilDI item in dIs)
  231. {
  232. bool value = false;
  233. if (item.Bit == 0)
  234. {
  235. value = (byt & 0x01) == 0x01;
  236. }
  237. else
  238. {
  239. value = (byt >> item.Bit & 0x01) == 0x01;
  240. }
  241. if (item.Invert)
  242. {
  243. value = !value;
  244. }
  245. IOModuleManager.Instance.UpdateIoValue(item.Name,value );
  246. }
  247. }
  248. /// <summary>
  249. /// 检验电机数据是否发生变化
  250. /// </summary>
  251. /// <param name="moduleName"></param>
  252. /// <param name="axisData"></param>
  253. private void CheckAxisDataChanged(string moduleName,GalilAxisData axisData)
  254. {
  255. if (_moduleNameAxisDataDictionary.ContainsKey(moduleName))
  256. {
  257. NotifyGalilAxisData(moduleName, _moduleNameAxisDataDictionary[moduleName],axisData);
  258. _moduleNameAxisDataDictionary[moduleName].Copy(axisData);
  259. }
  260. else
  261. {
  262. NotifyGalilAxisAllData(moduleName, axisData);
  263. _moduleNameAxisDataDictionary[moduleName] = axisData.Clone();
  264. }
  265. }
  266. /// <summary>
  267. /// 更新Galil电机数据
  268. /// </summary>
  269. /// <param name="axisData"></param>
  270. private void NotifyGalilAxisAllData(string moduleName,GalilAxisData axisData)
  271. {
  272. PropertyInfo[] propertyInfos= axisData.GetType().GetProperties();
  273. foreach(var info in propertyInfos)
  274. {
  275. object value = info.GetValue(axisData);
  276. GalilAxisManager.Instance.UpdateIoValue($"{moduleName}.{info.Name}", value);
  277. }
  278. }
  279. /// <summary>
  280. /// 通知Galil电机数据
  281. /// </summary>
  282. /// <param name="sourceData"></param>
  283. /// <param name="targetData"></param>
  284. private void NotifyGalilAxisData(string moduleName,GalilAxisData sourceData, GalilAxisData targetData)
  285. {
  286. PropertyInfo[] propertyInfos = sourceData.GetType().GetProperties();
  287. foreach (var info in propertyInfos)
  288. {
  289. object sourceValue= info.GetValue(sourceData);
  290. object targetValue = info.GetValue(targetData);
  291. if (sourceValue.ToString() != targetValue.ToString())
  292. {
  293. GalilAxisManager.Instance.UpdateIoValue($"{moduleName}.{info.Name}", targetValue);
  294. }
  295. }
  296. }
  297. /// <summary>
  298. /// 设置
  299. /// </summary>
  300. /// <param name="module"></param>
  301. /// <param name="name"></param>
  302. /// <param name="variable"></param>
  303. /// <param name="value"></param>
  304. /// <returns></returns>
  305. public bool SetSystemCommand(string module, string name, string commad, object value)
  306. {
  307. if (!_moduleGalilTcpDeviceDictionary.ContainsKey(module))
  308. {
  309. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{module} does not have tcp device");
  310. return false;
  311. }
  312. string str = $"{module}.{name}";
  313. if (_moduleNameIndexDictionary.ContainsKey(str))
  314. {
  315. int index = _moduleNameIndexDictionary[str];
  316. if (index >= _axisArray.Length)
  317. {
  318. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
  319. return false;
  320. }
  321. string strCommand = "";
  322. if (value != null)
  323. {
  324. strCommand = $"{commad}{value};";
  325. }
  326. else
  327. {
  328. strCommand = $"{commad};";
  329. }
  330. return _moduleGalilTcpDeviceDictionary[module].SetGalilCommand(strCommand);
  331. }
  332. else
  333. {
  334. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
  335. return false;
  336. }
  337. }
  338. /// <summary>
  339. /// 设置
  340. /// </summary>
  341. /// <param name="module"></param>
  342. /// <param name="name"></param>
  343. /// <param name="variable"></param>
  344. /// <param name="value"></param>
  345. /// <returns></returns>
  346. public bool SetAxisCommand(string module, string name,string commad, object value)
  347. {
  348. if (!_moduleGalilTcpDeviceDictionary.ContainsKey(module))
  349. {
  350. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{module} does not have tcp device");
  351. return false;
  352. }
  353. string str = $"{module}.{name}";
  354. if(_moduleNameIndexDictionary.ContainsKey(str))
  355. {
  356. int index = _moduleNameIndexDictionary[str];
  357. if (index >= _axisArray.Length)
  358. {
  359. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
  360. return false;
  361. }
  362. string strCommand = "";
  363. string axis = _axisArray[index].ToString();
  364. if (value != null)
  365. {
  366. strCommand = $"{commad}{axis}={value};";
  367. }
  368. else
  369. {
  370. strCommand = $"{commad}{axis};";
  371. }
  372. return _moduleGalilTcpDeviceDictionary[module].SetGalilCommand(strCommand);
  373. }
  374. else
  375. {
  376. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
  377. return false;
  378. }
  379. }
  380. /// <summary>
  381. /// 获取Galil配置对象
  382. /// </summary>
  383. /// <param name="module"></param>
  384. /// <param name="name"></param>
  385. /// <returns></returns>
  386. public GalilAxisConfig GetGalilAxisConfig(string module,string name)
  387. {
  388. string str = $"{module}.{name}";
  389. if (_moduleNameAxisConfigDictionary.ContainsKey(str))
  390. {
  391. return _moduleNameAxisConfigDictionary[str];
  392. }
  393. else
  394. {
  395. return null;
  396. }
  397. }
  398. }
  399. }