IoServoMotor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using Aitex.Core.Common.DeviceData;
  7. using Aitex.Core.RT.DataCenter;
  8. using Aitex.Core.RT.Event;
  9. using Aitex.Core.RT.IOCore;
  10. using Aitex.Core.RT.Log;
  11. using Aitex.Core.Util;
  12. namespace Aitex.Core.RT.Device.Unit
  13. {
  14. public class IoServoMotor : BaseDevice, IDevice
  15. {
  16. [Subscription(AITServoMotorProperty.IsServoOn)]
  17. public bool IsServoOn
  18. {
  19. get
  20. {
  21. return _doServoOn != null && _doServoOn.Value;
  22. }
  23. }
  24. [Subscription(AITServoMotorProperty.IsStopped)]
  25. public bool IsStopped
  26. {
  27. get
  28. {
  29. return _diPulseOutputComplete != null && _diPulseOutputComplete.Value;
  30. }
  31. }
  32. [Subscription(AITServoMotorProperty.IsError)]
  33. public bool IsError
  34. {
  35. get
  36. {
  37. return _diAlarm != null && _diAlarm.Value;
  38. }
  39. }
  40. [Subscription(AITServoMotorProperty.CurrentPosition)]
  41. public float CurrentPosition
  42. {
  43. get
  44. {
  45. return _aiPulseFeedback.Value;
  46. }
  47. }
  48. [Subscription(AITServoMotorProperty.CurrentStatus)]
  49. public string CurrentStatus
  50. {
  51. get
  52. {
  53. return _state.ToString();
  54. }
  55. }
  56. private DIAccessor _diHome;
  57. private DIAccessor _diCWLimit;
  58. private DIAccessor _diCCWLimit;
  59. private DIAccessor _diLocationComplete;
  60. private DIAccessor _diAlarm;
  61. private DIAccessor _diPulseOutputOn;
  62. private DIAccessor _diPulseOutputComplete;
  63. private DIAccessor _diNotInitial;
  64. private DOAccessor _doBreakRelay;
  65. private DOAccessor _doServoOn;
  66. private DOAccessor _doMoveUp;
  67. private DOAccessor _doMoveDown;
  68. private DOAccessor _doMoveToPosition;
  69. private DOAccessor _doDeviationCounterReset;
  70. private DOAccessor _doAlarmReset;
  71. private DOAccessor _doStopMoveUp;
  72. private DOAccessor _doStopMoveDown;
  73. private DOAccessor _doHome;
  74. private AOAccessor _aoServoSpeedSetPoint;
  75. private AOAccessor _aoManualSpeedSetPoint;
  76. private AOAccessor _aoAcceleration;
  77. private AOAccessor _aoDeceleration;
  78. private AOAccessor _aoStartFrequency;
  79. private AOAccessor _aoPositionSetPoint;
  80. private AIAccessor _aiPulseFeedback;
  81. private DeviceTimer _timer = new DeviceTimer();
  82. private R_TRIG _trigError = new R_TRIG();
  83. private ServoState _state = ServoState.NotInitial;
  84. private F_TRIG _trigAlarmRecovered = new F_TRIG();
  85. public IoServoMotor(string module, XmlElement node, string ioModule = "")
  86. {
  87. base.Module = module;
  88. base.Name = node.GetAttribute("id");
  89. base.Display = node.GetAttribute("display");
  90. base.DeviceID = node.GetAttribute("schematicId");
  91. _diHome = ParseDiNode("diHome", node, ioModule);
  92. _diCWLimit = ParseDiNode("diCWLimit", node, ioModule);
  93. _diCCWLimit = ParseDiNode("diCCWLimit", node, ioModule);
  94. _diLocationComplete = ParseDiNode("diLocationComplete", node, ioModule);
  95. _diAlarm = ParseDiNode("diAlarm", node, ioModule);
  96. _diPulseOutputOn = ParseDiNode("diPulseOutputOn", node, ioModule);
  97. _diPulseOutputComplete = ParseDiNode("diPulseOutputComplete", node, ioModule);
  98. _diNotInitial = ParseDiNode("diNotInitial", node, ioModule);
  99. _doBreakRelay = ParseDoNode("doBreakRelay", node, ioModule);
  100. _doServoOn = ParseDoNode("doServoOn", node, ioModule);
  101. _doMoveUp = ParseDoNode("doMoveUp", node, ioModule);
  102. _doMoveDown = ParseDoNode("doMoveDown", node, ioModule);
  103. _doMoveToPosition = ParseDoNode("doMoveToPosition", node, ioModule);
  104. _doDeviationCounterReset = ParseDoNode("doDeviationCounterReset", node, ioModule);
  105. _doAlarmReset = ParseDoNode("doAlarmReset", node, ioModule);
  106. _doStopMoveUp = ParseDoNode("doStopMoveUp", node, ioModule);
  107. _doStopMoveDown = ParseDoNode("doStopMoveDown", node, ioModule);
  108. _doHome = ParseDoNode("doHome", node, ioModule);
  109. _aoServoSpeedSetPoint = ParseAoNode("aoServoSpeedSetPoint", node, ioModule);
  110. _aoManualSpeedSetPoint = ParseAoNode("aoManualSpeedSetPoint", node, ioModule);
  111. _aoAcceleration = ParseAoNode("aoAcceleration", node, ioModule);
  112. _aoDeceleration = ParseAoNode("aoDeceleration", node, ioModule);
  113. _aoStartFrequency = ParseAoNode("aoStartFrequency", node, ioModule);
  114. _aoPositionSetPoint = ParseAoNode("aoPositionSetPoint", node, ioModule);
  115. _aiPulseFeedback = ParseAiNode("aiPulseFeedback", node, ioModule);
  116. }
  117. public bool Initialize()
  118. {
  119. DATA.Subscribe(string.Format("Device.{0}.{1}", Module, Name), () =>
  120. {
  121. AITServoMotorData data = new AITServoMotorData()
  122. {
  123. DeviceName = Name,
  124. DeviceSchematicId = DeviceID,
  125. DisplayName = Display,
  126. IsServoOn = IsServoOn,
  127. IsError = IsError,
  128. IsStopped = IsStopped,
  129. CurrentPosition = CurrentPosition,
  130. CurrentStatus = CurrentStatus,
  131. State = _state,
  132. };
  133. return data;
  134. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  135. DEVICE.Register(string.Format("{0}.{1}", Name, AITServoMotorOperation.Home),
  136. (out string reason, int time, object[] param) =>
  137. {
  138. reason = string.Format("{0} Home", Display);
  139. if (!_doHome.SetValue(true, out reason))
  140. return false;
  141. return true;
  142. });
  143. DEVICE.Register(string.Format("{0}.{1}", Name, AITServoMotorOperation.SetServoOn),
  144. (out string reason, int time, object[] param) =>
  145. {
  146. reason = string.Format("{0} Servo on", Display);
  147. if (!_doServoOn.SetValue(true, out reason))
  148. return false;
  149. return true;
  150. });
  151. DEVICE.Register(string.Format("{0}.{1}", Name, AITServoMotorOperation.SetServoOff),
  152. (out string reason, int time, object[] param) =>
  153. {
  154. reason = string.Format("{0} Servo off", Display);
  155. if (!_doServoOn.SetValue(false, out reason))
  156. return false;
  157. return true;
  158. });
  159. DEVICE.Register(string.Format("{0}.{1}", Name, AITServoMotorOperation.MoveTo),
  160. (out string reason, int time, object[] param) =>
  161. {
  162. double speed = (double) param[0];
  163. double position = (double) param[1];
  164. reason = string.Format("{0} Move to {1} at speed {2}", Display, position, speed);
  165. _aoPositionSetPoint.Value = (short)position;
  166. _aoServoSpeedSetPoint.Value = (short)speed;
  167. return true;
  168. });
  169. DEVICE.Register(string.Format("{0}.{1}", Name, AITServoMotorOperation.MoveBy),
  170. (out string reason, int time, object[] param) =>
  171. {
  172. double speed = (double)param[0];
  173. double by = (double)param[1];
  174. double position = _aiPulseFeedback.Value;
  175. reason = string.Format("{0} Move by {1} at speed {2}, from{3}", Display, @by, speed, position);
  176. _aoPositionSetPoint.Value = (short)position;
  177. _aoServoSpeedSetPoint.Value = (short)speed;
  178. _doMoveToPosition.Value = true;
  179. return true;
  180. });
  181. DEVICE.Register(string.Format("{0}.{1}", Name, AITServoMotorOperation.Reset),
  182. (out string reason, int time, object[] param) =>
  183. {
  184. reason = string.Format("{0} Reset", Display);
  185. if (!_doAlarmReset.SetValue(true, out reason))
  186. return false;
  187. return true;
  188. });
  189. DEVICE.Register(string.Format("{0}.{1}", Name, AITServoMotorOperation.Stop),
  190. (out string reason, int time, object[] param) =>
  191. {
  192. reason = string.Format("{0} Stop", Display);
  193. if (!_doStopMoveDown.SetValue(false, out reason) && !_doStopMoveUp.SetValue(true, out reason))
  194. return false;
  195. return true;
  196. });
  197. return true;
  198. }
  199. public void Terminate()
  200. {
  201. }
  202. public void Monitor()
  203. {
  204. try
  205. {
  206. if (!_diAlarm.Value)
  207. {
  208. _doAlarmReset.Value = false;
  209. }
  210. if (_diNotInitial.Value)
  211. {
  212. _state = ServoState.NotInitial;
  213. }else if (_diAlarm.Value)
  214. {
  215. _state = ServoState.Error;
  216. }else if (_diPulseOutputOn.Value)
  217. {
  218. _state = ServoState.Moving;
  219. }
  220. else
  221. {
  222. _state = ServoState.Idle;
  223. }
  224. }
  225. catch (Exception ex)
  226. {
  227. LOG.Write(ex);
  228. }
  229. }
  230. public void Reset()
  231. {
  232. _doAlarmReset.Value = true;
  233. }
  234. }
  235. }