AxisShell.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace StageController
  8. {
  9. class AxisShell
  10. {
  11. AxisController m_axisController;
  12. internal AxisShell(AxisType type, AxisController axisController)
  13. {
  14. m_axisController = axisController;
  15. AxisType = type;
  16. Speed = 2000;
  17. }
  18. /// <summary>
  19. /// X|Y|Z
  20. /// </summary>
  21. public AxisType AxisType { get; set; }
  22. private int _speed;
  23. /// <summary>
  24. /// 脉冲/秒
  25. /// </summary>
  26. public int Speed
  27. {
  28. get => _speed;
  29. set
  30. {
  31. _speed = value;
  32. }
  33. }
  34. private double _step;
  35. public double Step { get => _step; set { if (State < 2 || State == 6) _step = value; } }
  36. //指令位置
  37. private double _stepCommand { get; set; }
  38. public double Actual
  39. {
  40. get
  41. {
  42. return (double)(Step);
  43. }
  44. }
  45. //内用
  46. //Modify by shayg 20220615 start
  47. //private int _state;
  48. private volatile int _state;
  49. //Modify by shayg 20220615 end
  50. /// <summary>
  51. /// 外用
  52. /// </summary>
  53. public int State
  54. {
  55. get => _state;
  56. set
  57. {
  58. if (_state >= 6 && value < 2)
  59. {
  60. _state = value;
  61. }
  62. }
  63. }
  64. // Add by shayg 20220615 start
  65. public int ExcessStep { get; set; }
  66. // Add by shayg 20220615 end
  67. public int Limit { get => 0; set { } }
  68. public void To(double value)
  69. {
  70. if (value == _stepCommand) return;
  71. if (!IsStop) return;
  72. //new Task(() =>
  73. //{
  74. // _state = 8;
  75. // DelayStart();
  76. _stepCommand = value;
  77. _state = value > Step ? 2 : 3;
  78. //}).Start();
  79. }
  80. public void Move(int value, object arg = null)
  81. {
  82. if (value == 0) return;
  83. if (!IsStop) return;
  84. //new Task(() =>
  85. //{
  86. // _state = 8;
  87. // DelayStart();
  88. _stepCommand = Step + value;
  89. _state = value > 0 ? 2 : 3;
  90. //}).Start();
  91. }
  92. public void Move(double value, object arg = null)
  93. {
  94. if (value == 0) return;
  95. if (!IsStop) return;
  96. //new Task(() =>
  97. //{
  98. // _state = 8;
  99. // DelayStart();
  100. _stepCommand = Step + value;
  101. _state = value > 0 ? 2 : 3;
  102. //}).Start();
  103. }
  104. public void ResetPosition()
  105. {
  106. _step = 0;
  107. }
  108. public void Slide(bool isPositive, object arg = null)
  109. {
  110. //new Task(() =>
  111. //{
  112. // _state = 8;
  113. // DelayStart();
  114. _state = isPositive ? 4 : 5;
  115. //}).Start();
  116. }
  117. public void Stop(object arg = null)
  118. {
  119. //new Task(() =>
  120. //{
  121. // DelayStart();
  122. _state = 6;
  123. //}).Start();
  124. }
  125. private void DelayStart(int time = 0)
  126. {
  127. if (time == 0)
  128. Thread.Sleep(CommandDelay);
  129. else
  130. Thread.Sleep(time);
  131. }
  132. DateTime _time;
  133. public int CommandDelay = 100;//模拟真实运动的指令延时
  134. // modify by shayg 20220809 start
  135. //public bool IsStop => State < 2;
  136. public bool IsStop => State < 2 && ExcessStep == 0;
  137. // modify by shayg 20220809 end
  138. public void Execute()
  139. {
  140. #if false
  141. m_axisController.RefreshPosition();
  142. #else
  143. var span = (DateTime.Now - _time).TotalMilliseconds;
  144. _time = DateTime.Now;
  145. double step = span / 1000 * Speed;
  146. switch (_state)
  147. {
  148. case 0: /// 停止
  149. case 1:
  150. _stepCommand = _step;
  151. break;
  152. case 2: /// 正向定位
  153. step = _step + step;
  154. if (step >= _stepCommand)
  155. {
  156. _state = 7;
  157. step = _stepCommand;
  158. }
  159. _step = step;
  160. break;
  161. case 3:/// 负向定位
  162. step = _step - step;
  163. if (step <= _stepCommand)
  164. {
  165. _state = 7;
  166. step = _stepCommand;
  167. }
  168. _step = step;
  169. break;
  170. case 4:/// 正向连续
  171. _step += step;
  172. _stepCommand = _step;
  173. break;
  174. case 5:/// 负向连续
  175. _step -= step;
  176. _stepCommand = _step;
  177. break;
  178. case 6:
  179. case 7:
  180. m_axisController.RefreshPosition();
  181. break;
  182. default:
  183. //_state = 0;
  184. break;
  185. }
  186. #endif
  187. }
  188. }
  189. }