GalilControllerCfgManager.cs 15 KB

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