GalilControllerCfgManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. //if (SC.GetValue<bool>("System.IsSimulatorMode"))
  183. //{
  184. // LOG.WriteBackgroundLog(eEvent.INFO_AXIS, moduleName, JsonConvert.SerializeObject(galilAxisData));
  185. //}
  186. CheckAxisDataChanged(moduleName, galilAxisData);
  187. }
  188. }
  189. /// <summary>
  190. /// 更新ModuleDI
  191. /// </summary>
  192. /// <param name="module"></param>
  193. /// <param name="diValues"></param>
  194. private void UpdateModuleDI(string module, byte[] diValues)
  195. {
  196. if (!_moduleDeviceConfigDictionary.ContainsKey(module))
  197. {
  198. return;
  199. }
  200. GalilDeviceConfig galilDeviceConfig = _moduleDeviceConfigDictionary[module];
  201. if (galilDeviceConfig.GalilDigIn == null || galilDeviceConfig.GalilDigIn.DIs == null)
  202. {
  203. return;
  204. }
  205. bool isInitial = false;
  206. if (!_moduleDiDictionary.ContainsKey(module))
  207. {
  208. _moduleDiDictionary[module] = new byte[diValues.Length];
  209. isInitial=true;
  210. }
  211. if (_moduleDiDictionary[module].Length == diValues.Length)
  212. {
  213. for(int i=0;i<diValues.Length;i++)
  214. {
  215. byte item= _moduleDiDictionary[module][i];
  216. byte diValue = diValues[i];
  217. if (isInitial)
  218. {
  219. _moduleDiDictionary[module][i]= diValue;
  220. UpdateDIValue(galilDeviceConfig,diValue, i);
  221. }
  222. else if (item != diValue)
  223. {
  224. _moduleDiDictionary[module][i] = diValue;
  225. UpdateDIValue(galilDeviceConfig, diValue, i);
  226. }
  227. }
  228. }
  229. }
  230. /// <summary>
  231. /// 更新DI数值
  232. /// </summary>
  233. /// <param name="byt"></param>
  234. /// <param name="index"></param>
  235. private void UpdateDIValue(GalilDeviceConfig deviceConfig,byte byt,int index)
  236. {
  237. List<GalilDI> dIs = deviceConfig.GalilDigIn.DIs.FindAll(O => O.Address == index);
  238. foreach(GalilDI item in dIs)
  239. {
  240. bool value = false;
  241. if (item.Bit == 0)
  242. {
  243. value = (byt & 0x01) == 0x01;
  244. }
  245. else
  246. {
  247. value = (byt >> item.Bit & 0x01) == 0x01;
  248. }
  249. if (item.Invert)
  250. {
  251. value = !value;
  252. }
  253. IOModuleManager.Instance.UpdateIoValue(item.Name,value );
  254. }
  255. }
  256. /// <summary>
  257. /// 检验电机数据是否发生变化
  258. /// </summary>
  259. /// <param name="moduleName"></param>
  260. /// <param name="axisData"></param>
  261. private void CheckAxisDataChanged(string moduleName,GalilAxisData axisData)
  262. {
  263. if (_moduleNameAxisDataDictionary.ContainsKey(moduleName))
  264. {
  265. NotifyGalilAxisData(moduleName, _moduleNameAxisDataDictionary[moduleName],axisData);
  266. _moduleNameAxisDataDictionary[moduleName].Copy(axisData);
  267. }
  268. else
  269. {
  270. NotifyGalilAxisAllData(moduleName, axisData);
  271. _moduleNameAxisDataDictionary[moduleName] = axisData.Clone();
  272. }
  273. }
  274. /// <summary>
  275. /// 更新Galil电机数据
  276. /// </summary>
  277. /// <param name="axisData"></param>
  278. private void NotifyGalilAxisAllData(string moduleName,GalilAxisData axisData)
  279. {
  280. PropertyInfo[] propertyInfos= axisData.GetType().GetProperties();
  281. foreach(var info in propertyInfos)
  282. {
  283. object value = info.GetValue(axisData);
  284. GalilAxisManager.Instance.UpdateIoValue($"{moduleName}.{info.Name}", value);
  285. }
  286. }
  287. /// <summary>
  288. /// 通知Galil电机数据
  289. /// </summary>
  290. /// <param name="sourceData"></param>
  291. /// <param name="targetData"></param>
  292. private void NotifyGalilAxisData(string moduleName,GalilAxisData sourceData, GalilAxisData targetData)
  293. {
  294. PropertyInfo[] propertyInfos = sourceData.GetType().GetProperties();
  295. foreach (var info in propertyInfos)
  296. {
  297. object sourceValue= info.GetValue(sourceData);
  298. object targetValue = info.GetValue(targetData);
  299. if (sourceValue.ToString() != targetValue.ToString())
  300. {
  301. GalilAxisManager.Instance.UpdateIoValue($"{moduleName}.{info.Name}", targetValue);
  302. }
  303. }
  304. }
  305. /// <summary>
  306. /// 设置
  307. /// </summary>
  308. /// <param name="module"></param>
  309. /// <param name="name"></param>
  310. /// <param name="variable"></param>
  311. /// <param name="value"></param>
  312. /// <returns></returns>
  313. public bool SetSystemCommand(string module, string name, string commad, object value)
  314. {
  315. string str = $"{module}.{name}";
  316. if (!_moduleGalilTcpDeviceDictionary.ContainsKey(str))
  317. {
  318. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{module} does not have tcp device");
  319. return false;
  320. }
  321. if (_moduleNameIndexDictionary.ContainsKey(str))
  322. {
  323. int index = _moduleNameIndexDictionary[str];
  324. if (index >= _axisArray.Length)
  325. {
  326. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
  327. return false;
  328. }
  329. string strCommand = "";
  330. if (value != null)
  331. {
  332. strCommand = $"{commad}{value};";
  333. }
  334. else
  335. {
  336. strCommand = $"{commad};";
  337. }
  338. return _moduleGalilTcpDeviceDictionary[str].SetGalilCommand(strCommand);
  339. }
  340. else
  341. {
  342. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
  343. return false;
  344. }
  345. }
  346. /// <summary>
  347. /// 设置
  348. /// </summary>
  349. /// <param name="module"></param>
  350. /// <param name="name"></param>
  351. /// <param name="variable"></param>
  352. /// <param name="value"></param>
  353. /// <returns></returns>
  354. public bool SetAxisCommand(string module, string name,string commad, object value)
  355. {
  356. string str = $"{module}.{name}";
  357. if (!_moduleGalilTcpDeviceDictionary.ContainsKey(str))
  358. {
  359. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{module} does not have tcp device");
  360. return false;
  361. }
  362. if(_moduleNameIndexDictionary.ContainsKey(str))
  363. {
  364. int index = _moduleNameIndexDictionary[str];
  365. if (index >= _axisArray.Length)
  366. {
  367. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
  368. return false;
  369. }
  370. string strCommand = "";
  371. string axis = _axisArray[index].ToString();
  372. if (value != null)
  373. {
  374. strCommand = $"{commad}{axis}={value};";
  375. }
  376. else
  377. {
  378. strCommand = $"{commad}{axis};";
  379. }
  380. return _moduleGalilTcpDeviceDictionary[str].SetGalilCommand(strCommand);
  381. }
  382. else
  383. {
  384. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
  385. return false;
  386. }
  387. }
  388. /// <summary>
  389. /// 获取Galil配置对象
  390. /// </summary>
  391. /// <param name="module"></param>
  392. /// <param name="name"></param>
  393. /// <returns></returns>
  394. public GalilAxisConfig GetGalilAxisConfig(string module,string name)
  395. {
  396. string str = $"{module}.{name}";
  397. if (_moduleNameAxisConfigDictionary.ContainsKey(str))
  398. {
  399. return _moduleNameAxisConfigDictionary[str];
  400. }
  401. else
  402. {
  403. return null;
  404. }
  405. }
  406. }
  407. }