MotorSimulator.cs 13 KB

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