AxisShell.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. private int _state;
  47. /// <summary>
  48. /// 外用
  49. /// </summary>
  50. public int State
  51. {
  52. get => _state;
  53. set
  54. {
  55. if (_state >= 6 && value < 2)
  56. {
  57. _state = value;
  58. }
  59. }
  60. }
  61. public int Limit { get => 0; set { } }
  62. public void To(double value)
  63. {
  64. if (value == _stepCommand) return;
  65. if (!IsStop) return;
  66. //new Task(() =>
  67. //{
  68. // _state = 8;
  69. // DelayStart();
  70. _stepCommand = value;
  71. _state = value > Step ? 2 : 3;
  72. //}).Start();
  73. }
  74. public void Move(int value, object arg = null)
  75. {
  76. if (value == 0) return;
  77. if (!IsStop) return;
  78. //new Task(() =>
  79. //{
  80. // _state = 8;
  81. // DelayStart();
  82. _stepCommand = Step + value;
  83. _state = value > 0 ? 2 : 3;
  84. //}).Start();
  85. }
  86. public void Move(double value, object arg = null)
  87. {
  88. if (value == 0) return;
  89. if (!IsStop) return;
  90. //new Task(() =>
  91. //{
  92. // _state = 8;
  93. // DelayStart();
  94. _stepCommand = Step + value;
  95. _state = value > 0 ? 2 : 3;
  96. //}).Start();
  97. }
  98. public void ResetPosition()
  99. {
  100. _step = 0;
  101. }
  102. public void Slide(bool isPositive, object arg = null)
  103. {
  104. //new Task(() =>
  105. //{
  106. // _state = 8;
  107. // DelayStart();
  108. _state = isPositive ? 4 : 5;
  109. //}).Start();
  110. }
  111. public void Stop(object arg = null)
  112. {
  113. //new Task(() =>
  114. //{
  115. // DelayStart();
  116. _state = 6;
  117. //}).Start();
  118. }
  119. private void DelayStart(int time = 0)
  120. {
  121. if (time == 0)
  122. Thread.Sleep(CommandDelay);
  123. else
  124. Thread.Sleep(time);
  125. }
  126. DateTime _time;
  127. public int CommandDelay = 100;//模拟真实运动的指令延时
  128. public bool IsStop => State < 2;
  129. public void Execute()
  130. {
  131. #if false
  132. m_axisController.RefreshPosition();
  133. #else
  134. var span = (DateTime.Now - _time).TotalMilliseconds;
  135. _time = DateTime.Now;
  136. double step = span / 1000 * Speed;
  137. switch (_state)
  138. {
  139. case 0: /// 停止
  140. case 1:
  141. _stepCommand = _step;
  142. break;
  143. case 2: /// 正向定位
  144. step = _step + step;
  145. if (step >= _stepCommand)
  146. {
  147. _state = 7;
  148. step = _stepCommand;
  149. }
  150. _step = step;
  151. break;
  152. case 3:/// 负向定位
  153. step = _step - step;
  154. if (step <= _stepCommand)
  155. {
  156. _state = 7;
  157. step = _stepCommand;
  158. }
  159. _step = step;
  160. break;
  161. case 4:/// 正向连续
  162. _step += step;
  163. _stepCommand = _step;
  164. break;
  165. case 5:/// 负向连续
  166. _step -= step;
  167. _stepCommand = _step;
  168. break;
  169. case 6:
  170. case 7:
  171. m_axisController.RefreshPosition();
  172. break;
  173. default:
  174. //_state = 0;
  175. break;
  176. }
  177. #endif
  178. }
  179. }
  180. }