MotorSimulator.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.RT.Routine;
  5. using Aitex.Core.Util;
  6. using CyberX8_Core;
  7. using MECF.Framework.Common.Beckhoff.AxisProvider;
  8. using MECF.Framework.Common.Beckhoff.Station;
  9. using MECF.Framework.Common.Device.Galil;
  10. using MECF.Framework.Common.Equipment;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Runtime.InteropServices.ComTypes;
  15. namespace MECF.Framework.Common.Simulator
  16. {
  17. /// <summary>
  18. /// 电机运动模拟器
  19. /// </summary>
  20. public class MotorSimulator : Singleton<MotorSimulator>
  21. {
  22. #region 常量
  23. private const string TARGET_VELOCITY = "TargetVelocity";
  24. private const string TARGET_ACCEL = "TargetAcceleration";
  25. private const string TARGET_DECEL = "TargetDeceleration";
  26. private const string TARGET_POSITION = "TargetPosition";
  27. private const string SWITCH_SIGNAL = "SwitchSignal";
  28. private const string ACTUAL_POSITION = "ActualPosition";
  29. private const string AUXILIARY_POSITION = "AuxiliaryPosition";
  30. private const string HOMING_SIGNAL = "HomingSignal";
  31. private const string MOTION_SIGNAL = "MotionSignal";
  32. private const string STOP_SIGNAL = "StopSignal";
  33. private const string FESTO_DATABUFFER_TRANSPORT1_LOCK = "ProcessTransporterLock";
  34. private const string FESTO_DATABUFFER_TRANSPORT2_LOCK = "LoaderTransporterLock";
  35. /// <summary>
  36. /// 定时器间隔(ms)
  37. /// </summary>
  38. private const int TIMER_INTERVAL = 50;
  39. /// <summary>
  40. /// motor step factor
  41. /// </summary>
  42. private const int MOTOR_STEP_FACTOR = 10;
  43. #endregion
  44. #region 内部变量
  45. /// <summary>
  46. /// 定时器
  47. /// </summary>
  48. private PeriodicJob _periodicJob;
  49. /// <summary>
  50. /// 电机数据字典(key:Name(module.name),value:Datas)
  51. /// </summary>
  52. private Dictionary<string, SimulatorMotionData> _motorNameDataDic = new Dictionary<string, SimulatorMotionData>();
  53. /// <summary>
  54. /// Key:moduleName, Value:minStep
  55. /// </summary>
  56. private Dictionary<string, int> _motorNameMinStepDic = new Dictionary<string, int>();
  57. /// <summary>
  58. /// Key:stationName, Value:position
  59. /// </summary>
  60. private Dictionary<string, double> _stationPositionDic = new Dictionary<string, double>();
  61. /// <summary>
  62. /// Key:stationName, Value:Tolerance
  63. /// </summary>
  64. private Dictionary<string, double> _toleranceDic = new Dictionary<string, double>();
  65. /// <summary>
  66. /// Key:stationName, Value:BeckhoffProviderAxis
  67. /// </summary>
  68. private Dictionary<string, BeckhoffProviderAxis> _axisDataDic = new Dictionary<string, BeckhoffProviderAxis>();
  69. /// <summary>
  70. /// 其他模块数据
  71. /// </summary>
  72. private Dictionary<string, bool> _otherModuleDataDic = new Dictionary<string, bool>();
  73. #endregion
  74. #region 属性
  75. #endregion
  76. //delegate
  77. #region Delegate
  78. public delegate void UpdateVariableValueMotionDatasChanged(Dictionary<string, SimulatorMotionData> datasDic);
  79. public delegate void UpdateVariableValueInputDatasChanged(string module, string VariableName, bool value);
  80. public delegate void UpdateVariableValueWagoDatasChanged(string name, bool value, bool invert);
  81. #endregion
  82. #region 事件
  83. /// <summary>
  84. /// MotionDatas变更事件
  85. /// </summary>
  86. public event UpdateVariableValueMotionDatasChanged OnUpdateMotionDatasChanged;
  87. /// <summary>
  88. /// InputDatas变更事件
  89. /// </summary>
  90. public event UpdateVariableValueInputDatasChanged OnUpdateInputDatasChanged;
  91. /// <summary>
  92. /// WagoDatas变更事件
  93. /// </summary>
  94. public event UpdateVariableValueWagoDatasChanged OnUpdateWagoDatasChanged;
  95. #endregion
  96. /// <summary>
  97. /// 初始化
  98. /// </summary>
  99. public void Initialize()
  100. {
  101. _periodicJob = new PeriodicJob(TIMER_INTERVAL, OnTimer, "Motor Simulator Timer", true);
  102. Init();
  103. }
  104. /// <summary>
  105. /// 初始化数据
  106. /// </summary>
  107. private void Init()
  108. {
  109. SimulatorCommManager.Instance.OnUpdateVariableValueChanged += UpdataDataCausedByOtherModule;
  110. InitOtherModuleDatas();
  111. //加载对应配置文件 GalilControllerCfg-Simulator.xml,初始化数据字典
  112. try
  113. {
  114. string oldXmlPath = PathManager.GetCfgDir();
  115. string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Devices\\GalilControllerCfg-Simulator.xml";
  116. GalilControllerCfg cfg = CustomXmlSerializer.Deserialize<GalilControllerCfg>(new FileInfo(newXmlPath));
  117. if (cfg != null)
  118. {
  119. foreach (GalilDeviceConfig config in cfg.GalilDeviceConfigs)
  120. {
  121. foreach (GalilAxisConfig item in config.GalilAxises)
  122. {
  123. _motorNameDataDic[$"{config.Module}.{item.Name}"] = new SimulatorMotionData();
  124. _motorNameDataDic[$"{config.Module}.{item.Name}"].FwdSoftLimit = item.ForwardSoftwareLimit;
  125. _motorNameDataDic[$"{config.Module}.{item.Name}"].RevSoftLimit = item.ReverseSoftwareLimit;
  126. _motorNameDataDic[$"{config.Module}.{item.Name}"].NegativeTorqueLimit = item.NegativeTorqueLimit;
  127. _motorNameDataDic[$"{config.Module}.{item.Name}"].PositiveTorqueLimit = item.PositiveTorqueLimit;
  128. _motorNameDataDic[$"{config.Module}.{item.Name}"].SwitchSignal = true;
  129. _motorNameDataDic[$"{config.Module}.{item.Name}"].ModuleName = config.Module;
  130. }
  131. }
  132. }
  133. }
  134. catch
  135. {
  136. LOG.WriteLog(eEvent.ERR_GALIL, "Galil", "Load galil GalilControllerCfg-Simulator.xml failed");
  137. }
  138. //加载AxisProviderCfg.xml
  139. try
  140. {
  141. string oldXmlPath = PathManager.GetCfgDir();
  142. string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Devices\\AxisProviderCfg.xml";
  143. BeckhoffAxisProviderCfg axisProviderCfg = CustomXmlSerializer.Deserialize<BeckhoffAxisProviderCfg>(new FileInfo(newXmlPath));
  144. if (axisProviderCfg != null)
  145. {
  146. foreach (BeckhoffProviderAxis item in axisProviderCfg.Axes)
  147. {
  148. double value = item.ScaleFactor / 30;
  149. _axisDataDic[item.Name] = item;
  150. _motorNameMinStepDic[item.Name] = (int)((value < 100) ? 100 : value);
  151. }
  152. }
  153. }
  154. catch
  155. {
  156. LOG.WriteLog(eEvent.ERR_AXIS, "axisProvider", "Load AxisProviderCfg.xml failed");
  157. }
  158. //加载对应配置文件 StationPositionCfg-Simulator.xml,初始化数据字典
  159. try
  160. {
  161. string oldXmlPath = PathManager.GetCfgDir();
  162. string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Station\\StationPositionsCfg_Simulator.xml";
  163. StationPositionCfg cfg = CustomXmlSerializer.Deserialize<StationPositionCfg>(new FileInfo(newXmlPath));
  164. if (cfg != null)
  165. {
  166. foreach (BeckhoffStationModule config in cfg.Module)
  167. {
  168. if (config.Name.Contains("Transporter"))
  169. {
  170. foreach (BeckhoffStationAxis item in config.Axises)
  171. {
  172. _toleranceDic[item.Name] = item.ToleranceDefault;
  173. foreach(var subItem in item.Stations)
  174. {
  175. _stationPositionDic[subItem.Name.ToLower()] = double.Parse(subItem.Position);
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. catch
  183. {
  184. LOG.WriteLog(eEvent.ERR_GALIL, "StationPosition", "Load StationPositionCfg-Simulator.xml failed");
  185. }
  186. }
  187. private void InitOtherModuleDatas()
  188. {
  189. _otherModuleDataDic[FESTO_DATABUFFER_TRANSPORT1_LOCK] = false;
  190. _otherModuleDataDic[FESTO_DATABUFFER_TRANSPORT2_LOCK] = false;
  191. }
  192. /// <summary>
  193. /// 定时器执行
  194. /// </summary>
  195. /// <returns></returns>
  196. private bool OnTimer()
  197. {
  198. //电机运动模型
  199. foreach(var motorItem in _motorNameDataDic)
  200. {
  201. //对应电机进行模拟
  202. MotorMotionSimulator(motorItem);
  203. //实时更新电机数据
  204. UpdateMotionDatas(_motorNameDataDic);
  205. //更新InputDatas
  206. UpdateInputDatas(motorItem);
  207. }
  208. UpdateDatasByMutiAxisDatas();
  209. return true;
  210. }
  211. /// <summary>
  212. /// 通知Galil模块motion数据变化
  213. /// </summary>
  214. /// <param name="data"></param>
  215. private void UpdateMotionDatas(Dictionary<string, SimulatorMotionData> datasDic)
  216. {
  217. if (OnUpdateMotionDatasChanged != null)
  218. {
  219. OnUpdateMotionDatasChanged(datasDic);
  220. }
  221. }
  222. /// <summary>
  223. /// 通知Galil模块Input数据变化
  224. /// </summary>
  225. /// <param name="module"></param>
  226. /// <param name="value"></param>
  227. private void UpdateInputDatas(KeyValuePair<string, SimulatorMotionData> motorItem)
  228. {
  229. if (OnUpdateInputDatasChanged != null && CheckMotionData(motorItem, out string variableName, out bool value))
  230. {
  231. OnUpdateInputDatasChanged(motorItem.Value.ModuleName, variableName, value);
  232. }
  233. }
  234. private void UpdateDatasByMutiAxisDatas()
  235. {
  236. if (OnUpdateWagoDatasChanged != null && CheckMutiMotionData(out string variableName, out bool value))
  237. {
  238. OnUpdateWagoDatasChanged(variableName, value, false);
  239. }
  240. }
  241. /// <summary>
  242. /// 设置电机数据
  243. /// </summary>
  244. /// <param name="axisName"></param>
  245. /// <param name="type"></param>
  246. /// <param name="value"></param>
  247. public void SetMotionData(string axisName, string type, object value)
  248. {
  249. switch (type)
  250. {
  251. case TARGET_VELOCITY:
  252. _motorNameDataDic[axisName].TargetVelocity = (int)value;
  253. break;
  254. case TARGET_ACCEL:
  255. _motorNameDataDic[axisName].TargetAccel = (int)value;
  256. break;
  257. case TARGET_DECEL:
  258. _motorNameDataDic[axisName].TargetDecel = (int)value;
  259. break;
  260. case TARGET_POSITION:
  261. _motorNameDataDic[axisName].TargetPosition = (int)value;
  262. break;
  263. case ACTUAL_POSITION:
  264. _motorNameDataDic[axisName].ActualPosition = (int)value;
  265. break;
  266. case SWITCH_SIGNAL:
  267. _motorNameDataDic[axisName].SwitchSignal = (bool)value;
  268. break;
  269. case STOP_SIGNAL:
  270. _motorNameDataDic[axisName].StopSignal = true;
  271. break;
  272. case HOMING_SIGNAL:
  273. _motorNameDataDic[axisName].HomingSignal = true;
  274. break;
  275. case MOTION_SIGNAL:
  276. _motorNameDataDic[axisName].MotionSignal = true;
  277. break;
  278. case AUXILIARY_POSITION:
  279. //++
  280. break;
  281. default:
  282. break;
  283. }
  284. }
  285. /// <summary>
  286. /// 运动模拟器
  287. /// </summary>
  288. private void MotorMotionSimulator(KeyValuePair<string, SimulatorMotionData> motor)
  289. {
  290. SimulatorMotionData motionData = motor.Value;
  291. string name = motor.Key;
  292. //上电检查
  293. if (!motionData.SwitchSignal) return;
  294. if (motionData.HomingSignal)
  295. {
  296. HomeOperation(motionData);
  297. }
  298. else
  299. {
  300. PositionOperation(motionData, name);
  301. }
  302. }
  303. /// <summary>
  304. /// Home操作
  305. /// </summary>
  306. /// <param name="data"></param>
  307. private void HomeOperation(SimulatorMotionData motionData)
  308. {
  309. if (motionData.MotionSignal)
  310. {
  311. motionData.StopCode = 10;//HM操作停止码10
  312. motionData.MotionSignal = false;
  313. motionData.HomingSignal = false;
  314. motionData.MotionPhase = MotionPhase.Accelerating;
  315. //motionData.ActualPosition = 0;
  316. motionData.ActualVelocity = 0;
  317. }
  318. }
  319. /// <summary>
  320. /// GoToPosition操作
  321. /// </summary>
  322. /// <param name="data"></param>
  323. private void PositionOperation(SimulatorMotionData motionData, string name)
  324. {
  325. if (motionData.MotionSignal)
  326. {
  327. //正向运动
  328. int motorStep = Math.Abs(motionData.ActualPosition - motionData.TargetPosition);
  329. //motionData.ActualVelocity = TrapezoidalSpeedControl(motionData);
  330. motionData.ActualVelocity = motionData.TargetVelocity;
  331. if (motionData.ActualPosition < motionData.TargetPosition)
  332. {
  333. //motionData.ActualPosition += (motionData.ActualVelocity * TIMER_INTERVAL / 1000);
  334. motionData.ActualPosition += ((motorStep / MOTOR_STEP_FACTOR < _motorNameMinStepDic[name]) ? _motorNameMinStepDic[name] : motorStep / MOTOR_STEP_FACTOR);
  335. bool fwdLimit = motionData.FwdSoftLimit != 0 ? motionData.ActualPosition >= motionData.FwdSoftLimit : false;
  336. if (fwdLimit || motionData.ActualPosition >= motionData.TargetPosition)
  337. {
  338. motionData.StopCode = 1;//正常运动停止码1
  339. motionData.MotionSignal = false;
  340. //motionData.MotionPhase = MotionPhase.Accelerating;
  341. motionData.ActualPosition = fwdLimit ? motionData.FwdSoftLimit : motionData.TargetPosition;
  342. motionData.ActualVelocity = 0;
  343. }
  344. }
  345. //反向运动
  346. else if (motionData.ActualPosition > motionData.TargetPosition)
  347. {
  348. motionData.ActualPosition -= ((motorStep / MOTOR_STEP_FACTOR < _motorNameMinStepDic[name]) ? _motorNameMinStepDic[name] : motorStep / MOTOR_STEP_FACTOR);
  349. //motionData.ActualPosition -= (motionData.ActualVelocity * TIMER_INTERVAL / 1000);
  350. bool revLimit = motionData.RevSoftLimit != 0 ? motionData.ActualPosition <= motionData.RevSoftLimit : false;
  351. if (revLimit || motionData.ActualPosition <= motionData.TargetPosition)
  352. {
  353. motionData.StopCode = 1;//正常运动停止码1
  354. motionData.MotionSignal = false;
  355. //motionData.MotionPhase = MotionPhase.Accelerating;
  356. motionData.ActualPosition = revLimit ? motionData.RevSoftLimit : motionData.TargetPosition;
  357. motionData.ActualVelocity = 0;
  358. }
  359. }
  360. }
  361. //停止信号
  362. if (motionData.StopSignal)
  363. {
  364. motionData.MotionSignal = false;
  365. motionData.StopCode = 4;//ST操作停止码4
  366. motionData.StopSignal = false;
  367. }
  368. }
  369. /// <summary>
  370. /// 梯形加减速运动控制
  371. /// </summary>
  372. private int TrapezoidalSpeedControl(SimulatorMotionData data)
  373. {
  374. int speed = 0;
  375. //制动距离
  376. int brakeDistance = (data.ActualVelocity * data.ActualVelocity) / (2 * data.TargetDecel);
  377. int remainingDistance = Math.Abs(data.ActualPosition - data.TargetPosition);
  378. if (brakeDistance >= remainingDistance)
  379. {
  380. data.MotionPhase = MotionPhase.Decelerating;
  381. }
  382. else if (data.ActualVelocity < data.TargetVelocity && data.MotionPhase != MotionPhase.Decelerating)
  383. {
  384. data.MotionPhase = MotionPhase.Accelerating;
  385. }
  386. else
  387. {
  388. data.MotionPhase = MotionPhase.ConstantSpeed;
  389. }
  390. // 速度更新
  391. switch (data.MotionPhase)
  392. {
  393. case MotionPhase.Accelerating:
  394. speed = Math.Min(data.ActualVelocity + data.TargetAccel * TIMER_INTERVAL / 1000, data.TargetVelocity);
  395. break;
  396. case MotionPhase.ConstantSpeed:
  397. speed = data.TargetVelocity;
  398. break;
  399. case MotionPhase.Decelerating:
  400. //speed = Math.Max(data.ActualVelocity - data.TargetDecel * TIMER_INTERVAL / 1000, 10);
  401. speed = Math.Max(data.ActualVelocity / 100, 10);
  402. break;
  403. }
  404. return speed;
  405. }
  406. /// <summary>
  407. /// 检查单一电机数据
  408. /// </summary>
  409. /// <returns></returns>
  410. private bool CheckMotionData(KeyValuePair<string, SimulatorMotionData> motor, out string name,out bool value)
  411. {
  412. bool result = false;
  413. value = false;
  414. name = "";
  415. switch (motor.Key)
  416. {
  417. case "Transporter1.Elevator":
  418. name = "r_TRANSPORT1_WS_HOLD_PRESENT";
  419. value = _otherModuleDataDic[FESTO_DATABUFFER_TRANSPORT1_LOCK] && CheckAtStation(motor.Key, motor.Value, "up") ? true : false;
  420. result = true;
  421. break;
  422. case "Transporter2.Elevator":
  423. name = "r_TRANSPORT2_WS_HOLD_PRESENT";
  424. value = _otherModuleDataDic[FESTO_DATABUFFER_TRANSPORT2_LOCK] && CheckAtStation(motor.Key, motor.Value, "up") ? true : false;
  425. result = true;
  426. break;
  427. default:
  428. break;
  429. }
  430. return result;
  431. }
  432. /// <summary>
  433. /// 检查多个电机数据
  434. /// </summary>
  435. /// <returns></returns>
  436. private bool CheckMutiMotionData(out string variableName, out bool value)
  437. {
  438. bool result = false;
  439. variableName = "";
  440. value = false;
  441. //Loader WS Present Sensor
  442. if (CheckAtStation("Transporter2.Elevator", _motorNameDataDic["Transporter2.Elevator"], "Loader")
  443. && CheckAtStation("Transporter2.Gantry", _motorNameDataDic["Transporter2.Gantry"], "Loader"))
  444. {
  445. value = !_otherModuleDataDic[FESTO_DATABUFFER_TRANSPORT2_LOCK] ? true : false;
  446. variableName = "r_Cathode_Present";
  447. result = true;
  448. }
  449. return result;
  450. }
  451. /// <summary>
  452. /// 更新其他模块数据
  453. /// </summary>
  454. /// <param name="name"></param>
  455. /// <param name="value"></param>
  456. private void UpdataDataCausedByOtherModule(string name, bool value, bool revert)
  457. {
  458. if (_otherModuleDataDic.ContainsKey(name))
  459. {
  460. _otherModuleDataDic[name] = value;
  461. }
  462. }
  463. private bool CheckAtStation(string name, SimulatorMotionData data, string stationName)
  464. {
  465. double diff = Math.Abs(data.ActualPosition - _stationPositionDic[$"{name.ToLower()}.{stationName.ToLower()}"] * _axisDataDic[name].ScaleFactor);
  466. if (diff <= _toleranceDic[name] * _axisDataDic[name].ScaleFactor)
  467. {
  468. return true;
  469. }
  470. return false;
  471. }
  472. }
  473. }