MotorSimulator.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.Beckhoff.AxisProvider;
  5. using MECF.Framework.Common.Beckhoff.Station;
  6. using MECF.Framework.Common.Device.Galil;
  7. using MECF.Framework.Common.Equipment;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. namespace MECF.Framework.Common.Simulator
  12. {
  13. /// <summary>
  14. /// 电机运动模拟器
  15. /// </summary>
  16. public class MotorSimulator : Singleton<MotorSimulator>
  17. {
  18. #region 常量
  19. private const string TARGET_VELOCITY = "TargetVelocity";
  20. private const string TARGET_ACCEL = "TargetAcceleration";
  21. private const string TARGET_DECEL = "TargetDeceleration";
  22. private const string TARGET_POSITION = "TargetPosition";
  23. private const string SWITCH_SIGNAL = "SwitchSignal";
  24. private const string ACTUAL_POSITION = "ActualPosition";
  25. private const string AUXILIARY_POSITION = "AuxiliaryPosition";
  26. private const string HOMING_SIGNAL = "HomingSignal";
  27. private const string MOTION_SIGNAL = "MotionSignal";
  28. private const string STOP_SIGNAL = "StopSignal";
  29. /// <summary>
  30. /// 定时器间隔(ms)
  31. /// </summary>
  32. private const int TIMER_INTERVAL = 50;
  33. /// <summary>
  34. /// motor step factor
  35. /// </summary>
  36. private const int MOTOR_STEP_FACTOR = 10;
  37. #endregion
  38. #region 内部变量
  39. /// <summary>
  40. /// 定时器
  41. /// </summary>
  42. private PeriodicJob _periodicJob;
  43. /// <summary>
  44. /// 电机数据字典(key:Name(module.name),value:Datas)
  45. /// </summary>
  46. private Dictionary<string, SimulatorMotionData> _motorNameDataDic = new Dictionary<string, SimulatorMotionData>();
  47. /// <summary>
  48. /// Key:moduleName, Value:minStep
  49. /// </summary>
  50. private Dictionary<string, int> _motorNameMinStepDic = new Dictionary<string, int>();
  51. #endregion
  52. #region 属性
  53. #endregion
  54. //delegate
  55. #region Delegate
  56. public delegate void UpdateVariableValueChanged(Dictionary<string, SimulatorMotionData> datasDic);
  57. #endregion
  58. #region 事件
  59. /// <summary>
  60. /// 变量变更事件
  61. /// </summary>
  62. public event UpdateVariableValueChanged OnUpdateVariableValueChanged;
  63. #endregion
  64. /// <summary>
  65. /// 初始化
  66. /// </summary>
  67. public void Initialize()
  68. {
  69. _periodicJob = new PeriodicJob(TIMER_INTERVAL, OnTimer, "Motor Simulator Timer", true);
  70. Init();
  71. }
  72. /// <summary>
  73. /// 初始化数据
  74. /// </summary>
  75. private void Init()
  76. {
  77. //加载对应配置文件 GalilControllerCfg-Simulator.xml,初始化数据字典
  78. try
  79. {
  80. string oldXmlPath = PathManager.GetCfgDir();
  81. string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Devices\\GalilControllerCfg-Simulator.xml";
  82. GalilControllerCfg cfg = CustomXmlSerializer.Deserialize<GalilControllerCfg>(new FileInfo(newXmlPath));
  83. if (cfg != null)
  84. {
  85. foreach (GalilDeviceConfig config in cfg.GalilDeviceConfigs)
  86. {
  87. foreach (GalilAxisConfig item in config.GalilAxises)
  88. {
  89. _motorNameDataDic[$"{config.Module}.{item.Name}"] = new SimulatorMotionData();
  90. _motorNameDataDic[$"{config.Module}.{item.Name}"].FwdSoftLimit = item.ForwardSoftwareLimit;
  91. _motorNameDataDic[$"{config.Module}.{item.Name}"].RevSoftLimit = item.ReverseSoftwareLimit;
  92. _motorNameDataDic[$"{config.Module}.{item.Name}"].NegativeTorqueLimit = item.NegativeTorqueLimit;
  93. _motorNameDataDic[$"{config.Module}.{item.Name}"].PositiveTorqueLimit = item.PositiveTorqueLimit;
  94. _motorNameDataDic[$"{config.Module}.{item.Name}"].SwitchSignal = true;
  95. }
  96. }
  97. }
  98. }
  99. catch
  100. {
  101. LOG.WriteLog(eEvent.ERR_GALIL, "Galil", "Load galil GalilControllerCfg-Simulator.xml failed");
  102. }
  103. //加载AxisProviderCfg.xml
  104. try
  105. {
  106. string oldXmlPath = PathManager.GetCfgDir();
  107. string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Devices\\AxisProviderCfg.xml";
  108. BeckhoffAxisProviderCfg axisProviderCfg = CustomXmlSerializer.Deserialize<BeckhoffAxisProviderCfg>(new FileInfo(newXmlPath));
  109. if (axisProviderCfg != null)
  110. {
  111. foreach (BeckhoffProviderAxis item in axisProviderCfg.Axes)
  112. {
  113. double value = item.ScaleFactor / 30;
  114. _motorNameMinStepDic[item.Name] = (int)((value < 100) ? 100 : value);
  115. }
  116. }
  117. }
  118. catch
  119. {
  120. LOG.WriteLog(eEvent.ERR_AXIS, "axisProvider", "Load AxisProviderCfg.xml failed");
  121. }
  122. //加载对应配置文件 StationPositionCfg-Simulator.xml,初始化数据字典
  123. try
  124. {
  125. string oldXmlPath = PathManager.GetCfgDir();
  126. string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Devices\\StationPositionsCfg_Simulator.xml";
  127. StationPositionCfg cfg = CustomXmlSerializer.Deserialize<StationPositionCfg>(new FileInfo(newXmlPath));
  128. if (cfg != null)
  129. {
  130. //foreach (BeckhoffStationModule config in cfg.Module)
  131. //{
  132. // if (config.Name == ModuleName.Transporter1.ToString())
  133. // {
  134. // foreach (BeckhoffStationAxis item in config.Axises)
  135. // {
  136. // ;
  137. // }
  138. // }
  139. // else if(config.Name == ModuleName.Transporter2.ToString())
  140. // {
  141. // foreach (BeckhoffStationAxis item in config.Axises)
  142. // {
  143. // ;
  144. // }
  145. // }
  146. //}
  147. }
  148. }
  149. catch
  150. {
  151. LOG.WriteLog(eEvent.ERR_GALIL, "Galil", "Load galil GalilControllerCfg-Simulator.xml failed");
  152. }
  153. }
  154. /// <summary>
  155. /// 定时器执行
  156. /// </summary>
  157. /// <returns></returns>
  158. private bool OnTimer()
  159. {
  160. //电机运动模型
  161. foreach(var motorItem in _motorNameDataDic)
  162. {
  163. //对应电机进行模拟
  164. MotorMotionSimulator(motorItem);
  165. //实时更新电机数据
  166. UpdateVariableValue(_motorNameDataDic);
  167. }
  168. return true;
  169. }
  170. /// <summary>
  171. /// 通知Galil模块数据变化
  172. /// </summary>
  173. /// <param name="data"></param>
  174. private void UpdateVariableValue(Dictionary<string, SimulatorMotionData> datasDic)
  175. {
  176. if (OnUpdateVariableValueChanged != null)
  177. {
  178. OnUpdateVariableValueChanged(datasDic);
  179. }
  180. }
  181. /// <summary>
  182. /// 设置电机数据
  183. /// </summary>
  184. /// <param name="axisName"></param>
  185. /// <param name="type"></param>
  186. /// <param name="value"></param>
  187. public void SetMotionData(string axisName, string type, object value)
  188. {
  189. switch (type)
  190. {
  191. case TARGET_VELOCITY:
  192. _motorNameDataDic[axisName].TargetVelocity = (int)value;
  193. break;
  194. case TARGET_ACCEL:
  195. _motorNameDataDic[axisName].TargetAccel = (int)value;
  196. break;
  197. case TARGET_DECEL:
  198. _motorNameDataDic[axisName].TargetDecel = (int)value;
  199. break;
  200. case TARGET_POSITION:
  201. _motorNameDataDic[axisName].TargetPosition = (int)value;
  202. break;
  203. case ACTUAL_POSITION:
  204. _motorNameDataDic[axisName].ActualPosition = (int)value;
  205. break;
  206. case SWITCH_SIGNAL:
  207. _motorNameDataDic[axisName].SwitchSignal = (bool)value;
  208. break;
  209. case STOP_SIGNAL:
  210. _motorNameDataDic[axisName].StopSignal = true;
  211. break;
  212. case HOMING_SIGNAL:
  213. _motorNameDataDic[axisName].HomingSignal = true;
  214. break;
  215. case MOTION_SIGNAL:
  216. _motorNameDataDic[axisName].MotionSignal = true;
  217. break;
  218. case AUXILIARY_POSITION:
  219. //++
  220. break;
  221. default:
  222. break;
  223. }
  224. }
  225. /// <summary>
  226. /// 运动模拟器
  227. /// </summary>
  228. private void MotorMotionSimulator(KeyValuePair<string, SimulatorMotionData> motor)
  229. {
  230. SimulatorMotionData motionData = motor.Value;
  231. string name = motor.Key;
  232. //上电检查
  233. if (!motionData.SwitchSignal) return;
  234. if (motionData.HomingSignal)
  235. {
  236. HomeOperation(motionData);
  237. }
  238. else
  239. {
  240. PositionOperation(motionData, name);
  241. }
  242. }
  243. /// <summary>
  244. /// Home操作
  245. /// </summary>
  246. /// <param name="data"></param>
  247. private void HomeOperation(SimulatorMotionData motionData)
  248. {
  249. if (motionData.MotionSignal)
  250. {
  251. motionData.StopCode = 10;//HM操作停止码10
  252. motionData.MotionSignal = false;
  253. motionData.HomingSignal = false;
  254. motionData.MotionPhase = MotionPhase.Accelerating;
  255. //motionData.ActualPosition = 0;
  256. motionData.ActualVelocity = 0;
  257. }
  258. }
  259. /// <summary>
  260. /// GoToPosition操作
  261. /// </summary>
  262. /// <param name="data"></param>
  263. private void PositionOperation(SimulatorMotionData motionData, string name)
  264. {
  265. if (motionData.MotionSignal)
  266. {
  267. //正向运动
  268. int motorStep = Math.Abs(motionData.ActualPosition - motionData.TargetPosition);
  269. //motionData.ActualVelocity = TrapezoidalSpeedControl(motionData);
  270. motionData.ActualVelocity = motionData.TargetVelocity;
  271. if (motionData.ActualPosition < motionData.TargetPosition)
  272. {
  273. //motionData.ActualPosition += (motionData.ActualVelocity * TIMER_INTERVAL / 1000);
  274. motionData.ActualPosition += ((motorStep / MOTOR_STEP_FACTOR < _motorNameMinStepDic[name]) ? _motorNameMinStepDic[name] : motorStep / MOTOR_STEP_FACTOR);
  275. bool fwdLimit = motionData.FwdSoftLimit != 0 ? motionData.ActualPosition >= motionData.FwdSoftLimit : false;
  276. if (fwdLimit || motionData.ActualPosition >= motionData.TargetPosition)
  277. {
  278. motionData.StopCode = 1;//正常运动停止码1
  279. motionData.MotionSignal = false;
  280. //motionData.MotionPhase = MotionPhase.Accelerating;
  281. motionData.ActualPosition = fwdLimit ? motionData.FwdSoftLimit : motionData.TargetPosition;
  282. motionData.ActualVelocity = 0;
  283. }
  284. }
  285. //反向运动
  286. else if (motionData.ActualPosition > motionData.TargetPosition)
  287. {
  288. motionData.ActualPosition -= ((motorStep / MOTOR_STEP_FACTOR < _motorNameMinStepDic[name]) ? _motorNameMinStepDic[name] : motorStep / MOTOR_STEP_FACTOR);
  289. //motionData.ActualPosition -= (motionData.ActualVelocity * TIMER_INTERVAL / 1000);
  290. bool revLimit = motionData.RevSoftLimit != 0 ? motionData.ActualPosition <= motionData.RevSoftLimit : false;
  291. if (revLimit || motionData.ActualPosition <= motionData.TargetPosition)
  292. {
  293. motionData.StopCode = 1;//正常运动停止码1
  294. motionData.MotionSignal = false;
  295. //motionData.MotionPhase = MotionPhase.Accelerating;
  296. motionData.ActualPosition = revLimit ? motionData.RevSoftLimit : motionData.TargetPosition;
  297. motionData.ActualVelocity = 0;
  298. }
  299. }
  300. }
  301. //停止信号
  302. if (motionData.StopSignal)
  303. {
  304. motionData.MotionSignal = false;
  305. motionData.StopCode = 4;//ST操作停止码4
  306. motionData.StopSignal = false;
  307. }
  308. }
  309. /// <summary>
  310. /// 梯形加减速运动控制
  311. /// </summary>
  312. private int TrapezoidalSpeedControl(SimulatorMotionData data)
  313. {
  314. int speed = 0;
  315. //制动距离
  316. int brakeDistance = (data.ActualVelocity * data.ActualVelocity) / (2 * data.TargetDecel);
  317. int remainingDistance = Math.Abs(data.ActualPosition - data.TargetPosition);
  318. if (brakeDistance >= remainingDistance)
  319. {
  320. data.MotionPhase = MotionPhase.Decelerating;
  321. }
  322. else if (data.ActualVelocity < data.TargetVelocity && data.MotionPhase != MotionPhase.Decelerating)
  323. {
  324. data.MotionPhase = MotionPhase.Accelerating;
  325. }
  326. else
  327. {
  328. data.MotionPhase = MotionPhase.ConstantSpeed;
  329. }
  330. // 速度更新
  331. switch (data.MotionPhase)
  332. {
  333. case MotionPhase.Accelerating:
  334. speed = Math.Min(data.ActualVelocity + data.TargetAccel * TIMER_INTERVAL / 1000, data.TargetVelocity);
  335. break;
  336. case MotionPhase.ConstantSpeed:
  337. speed = data.TargetVelocity;
  338. break;
  339. case MotionPhase.Decelerating:
  340. //speed = Math.Max(data.ActualVelocity - data.TargetDecel * TIMER_INTERVAL / 1000, 10);
  341. speed = Math.Max(data.ActualVelocity / 100, 10);
  342. break;
  343. }
  344. return speed;
  345. }
  346. }
  347. }