GalilControllerCfgManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. _moduleDeviceConfigDictionary[deviceConfig.Module] = deviceConfig;
  124. foreach(var item in deviceConfig.GalilAxises)
  125. {
  126. _moduleNameIndexDictionary[item.Name] = item.Index;
  127. _moduleNameAxisConfigDictionary[item.Name]=item;
  128. if (!lst.Contains(item.Name))
  129. {
  130. lst.Add(item.Name);
  131. }
  132. }
  133. if (lst.Count > 0)
  134. {
  135. _moduleNameLstDictionary[deviceConfig.Module] = lst;
  136. }
  137. _axisArray = new char[10];
  138. int a = 'A';
  139. for (int i = 0; i < _axisArray.Length; i++)
  140. {
  141. _axisArray[i] = (char)(a + i);
  142. }
  143. }
  144. /// <summary>
  145. /// 更新模块数据
  146. /// </summary>
  147. /// <param name="controllerData"></param>
  148. public void UpdateModuleData(string module,GalilControllerData controllerData)
  149. {
  150. if (!_moduleGalilDataDictionary.ContainsKey(module))
  151. {
  152. _moduleGalilDataDictionary[module] = controllerData;
  153. }
  154. if (!_moduleNameLstDictionary.ContainsKey(module))
  155. {
  156. return;
  157. }
  158. UpdateModuleDI(module, controllerData.Inputs);
  159. List<string> lst = _moduleNameLstDictionary[module];
  160. foreach (var item in lst)
  161. {
  162. string moduleName = item;
  163. if (!_moduleNameIndexDictionary.ContainsKey(moduleName))
  164. {
  165. continue;
  166. }
  167. int index = _moduleNameIndexDictionary[moduleName];
  168. if(index>=controllerData.GalilAxisDatas.Count)
  169. {
  170. continue;
  171. }
  172. GalilAxisData galilAxisData=controllerData.GalilAxisDatas[index];
  173. CheckAxisDataChanged(moduleName, galilAxisData);
  174. }
  175. }
  176. /// <summary>
  177. /// 更新ModuleDI
  178. /// </summary>
  179. /// <param name="module"></param>
  180. /// <param name="diValues"></param>
  181. private void UpdateModuleDI(string module, byte[] diValues)
  182. {
  183. if (!_moduleDeviceConfigDictionary.ContainsKey(module))
  184. {
  185. return;
  186. }
  187. GalilDeviceConfig galilDeviceConfig = _moduleDeviceConfigDictionary[module];
  188. if (galilDeviceConfig.GalilDigIn == null || galilDeviceConfig.GalilDigIn.DIs == null)
  189. {
  190. return;
  191. }
  192. bool isInitial = false;
  193. if (!_moduleDiDictionary.ContainsKey(module))
  194. {
  195. _moduleDiDictionary[module] = new byte[diValues.Length];
  196. isInitial=true;
  197. }
  198. if (_moduleDiDictionary[module].Length == diValues.Length)
  199. {
  200. for(int i=0;i<diValues.Length;i++)
  201. {
  202. byte item= _moduleDiDictionary[module][i];
  203. byte diValue = diValues[i];
  204. if (isInitial)
  205. {
  206. _moduleDiDictionary[module][i]= diValue;
  207. UpdateDIValue(galilDeviceConfig,diValue, i);
  208. }
  209. else if (item != diValue)
  210. {
  211. _moduleDiDictionary[module][i] = diValue;
  212. UpdateDIValue(galilDeviceConfig, diValue, i);
  213. }
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// 更新DI数值
  219. /// </summary>
  220. /// <param name="byt"></param>
  221. /// <param name="index"></param>
  222. private void UpdateDIValue(GalilDeviceConfig deviceConfig,byte byt,int index)
  223. {
  224. List<GalilDI> dIs = deviceConfig.GalilDigIn.DIs.FindAll(O => O.Address == index);
  225. foreach(GalilDI item in dIs)
  226. {
  227. bool value = false;
  228. if (item.Bit == 0)
  229. {
  230. value = (byt & 0x01) == 0x01;
  231. }
  232. else
  233. {
  234. value = (byt >> item.Bit & 0x01) == 0x01;
  235. }
  236. if (item.Invert)
  237. {
  238. value = !value;
  239. }
  240. IOModuleManager.Instance.UpdateIoValue(item.Name,value );
  241. }
  242. }
  243. /// <summary>
  244. /// 检验电机数据是否发生变化
  245. /// </summary>
  246. /// <param name="moduleName"></param>
  247. /// <param name="axisData"></param>
  248. private void CheckAxisDataChanged(string moduleName,GalilAxisData axisData)
  249. {
  250. if (_moduleNameAxisDataDictionary.ContainsKey(moduleName))
  251. {
  252. NotifyGalilAxisData(moduleName, _moduleNameAxisDataDictionary[moduleName],axisData);
  253. _moduleNameAxisDataDictionary[moduleName].Copy(axisData);
  254. }
  255. else
  256. {
  257. NotifyGalilAxisAllData(moduleName, axisData);
  258. _moduleNameAxisDataDictionary[moduleName] = axisData.Clone();
  259. }
  260. }
  261. /// <summary>
  262. /// 更新Galil电机数据
  263. /// </summary>
  264. /// <param name="axisData"></param>
  265. private void NotifyGalilAxisAllData(string moduleName,GalilAxisData axisData)
  266. {
  267. PropertyInfo[] propertyInfos= axisData.GetType().GetProperties();
  268. foreach(var info in propertyInfos)
  269. {
  270. object value = info.GetValue(axisData);
  271. GalilAxisManager.Instance.UpdateIoValue($"{moduleName}.{info.Name}", value);
  272. }
  273. }
  274. /// <summary>
  275. /// 通知Galil电机数据
  276. /// </summary>
  277. /// <param name="sourceData"></param>
  278. /// <param name="targetData"></param>
  279. private void NotifyGalilAxisData(string moduleName,GalilAxisData sourceData, GalilAxisData targetData)
  280. {
  281. PropertyInfo[] propertyInfos = sourceData.GetType().GetProperties();
  282. foreach (var info in propertyInfos)
  283. {
  284. object sourceValue= info.GetValue(sourceData);
  285. object targetValue = info.GetValue(targetData);
  286. if (sourceValue.ToString() != targetValue.ToString())
  287. {
  288. GalilAxisManager.Instance.UpdateIoValue($"{moduleName}.{info.Name}", targetValue);
  289. }
  290. }
  291. }
  292. /// <summary>
  293. /// 设置
  294. /// </summary>
  295. /// <param name="module"></param>
  296. /// <param name="name"></param>
  297. /// <param name="variable"></param>
  298. /// <param name="value"></param>
  299. /// <returns></returns>
  300. public bool SetSystemCommand(string module, string name, string commad, object value)
  301. {
  302. if (!_moduleGalilTcpDeviceDictionary.ContainsKey(module))
  303. {
  304. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{module} does not have tcp device");
  305. return false;
  306. }
  307. string str = $"{module}.{name}";
  308. if (_moduleNameIndexDictionary.ContainsKey(str))
  309. {
  310. int index = _moduleNameIndexDictionary[str];
  311. if (index >= _axisArray.Length)
  312. {
  313. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
  314. return false;
  315. }
  316. string strCommand = "";
  317. if (value != null)
  318. {
  319. strCommand = $"{commad}{value};";
  320. }
  321. else
  322. {
  323. strCommand = $"{commad};";
  324. }
  325. return _moduleGalilTcpDeviceDictionary[module].SetGalilCommand(strCommand);
  326. }
  327. else
  328. {
  329. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
  330. return false;
  331. }
  332. }
  333. /// <summary>
  334. /// 设置
  335. /// </summary>
  336. /// <param name="module"></param>
  337. /// <param name="name"></param>
  338. /// <param name="variable"></param>
  339. /// <param name="value"></param>
  340. /// <returns></returns>
  341. public bool SetAxisCommand(string module, string name,string commad, object value)
  342. {
  343. if (!_moduleGalilTcpDeviceDictionary.ContainsKey(module))
  344. {
  345. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{module} does not have tcp device");
  346. return false;
  347. }
  348. string str = $"{module}.{name}";
  349. if(_moduleNameIndexDictionary.ContainsKey(str))
  350. {
  351. int index = _moduleNameIndexDictionary[str];
  352. if (index >= _axisArray.Length)
  353. {
  354. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
  355. return false;
  356. }
  357. string strCommand = "";
  358. string axis = _axisArray[index].ToString();
  359. if (value != null)
  360. {
  361. strCommand = $"{commad}{axis}={value};";
  362. }
  363. else
  364. {
  365. strCommand = $"{commad}{axis};";
  366. }
  367. return _moduleGalilTcpDeviceDictionary[module].SetGalilCommand(strCommand);
  368. }
  369. else
  370. {
  371. LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
  372. return false;
  373. }
  374. }
  375. /// <summary>
  376. /// 获取Galil配置对象
  377. /// </summary>
  378. /// <param name="module"></param>
  379. /// <param name="name"></param>
  380. /// <returns></returns>
  381. public GalilAxisConfig GetGalilAxisConfig(string module,string name)
  382. {
  383. string str = $"{module}.{name}";
  384. if (_moduleNameAxisConfigDictionary.ContainsKey(str))
  385. {
  386. return _moduleNameAxisConfigDictionary[str];
  387. }
  388. else
  389. {
  390. return null;
  391. }
  392. }
  393. }
  394. }