GalilControllerCfgManager.cs 16 KB

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