MotorSimulator.cs 12 KB

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