M3HAxis.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using StageController.M3H;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace StageController
  8. {
  9. internal class M3HAxis : IMotion
  10. {
  11. AxisController m_axisController;
  12. /// <summary>
  13. /// 0-999
  14. /// </summary>
  15. public int Speed { get; set; }
  16. public int Step { get; set; }
  17. /// <summary>
  18. /// 指令位置:用于补偿实际位置
  19. /// </summary>
  20. double Expect { get; set; }
  21. public double Actual
  22. {
  23. get
  24. {
  25. if (Reversed)
  26. return (double)(-Step * Stepping);
  27. return (double)(Step * Stepping);
  28. }
  29. }
  30. /// <summary>
  31. /// 0:停机自由,1:停机锁死,2:正向连续,3:反向连续,4:正向步进,5:反向步进
  32. /// </summary>
  33. public int State { get; set; }
  34. private Border _border;
  35. public int Limit
  36. {
  37. get
  38. {
  39. if (Reversed)
  40. {
  41. switch (_border)
  42. {
  43. case Border.F:
  44. return (int)Border.R;
  45. case Border.R:
  46. return (int)Border.F;
  47. }
  48. }
  49. return (int)_border;
  50. }
  51. set { _border = (Border)value; }
  52. }
  53. public M3HAxisType AxisType { get; private set; }
  54. /// <summary>
  55. /// 步距
  56. /// </summary>
  57. public double Stepping { get; set; }
  58. public bool Reversed { get; set; }
  59. public M3HAxis(M3HAxisType type, AxisController axisController)
  60. {
  61. m_axisController = axisController;
  62. AxisType = type;
  63. if (m_axisController == null)
  64. throw new ArgumentNullException("AxisController is Null!");
  65. }
  66. public void Move(int step, object arg = null)
  67. {
  68. if (Reversed) step = -step;
  69. CommandMoveByStep command = new CommandMoveByStep(AxisType, step, Speed);
  70. m_axisController.AddCommand(command);
  71. }
  72. public void Move(double value, object arg = null)
  73. {
  74. if (Reversed) value = -value;
  75. var step = GetStep(value);
  76. CommandMoveByStep command = new CommandMoveByStep(AxisType, step, Speed);
  77. m_axisController.AddCommand(command);
  78. }
  79. public void To(double value)
  80. {
  81. if (Reversed) value = -value;
  82. var step = GetStep(value);
  83. step -= Step;
  84. CommandMoveByStep command = new CommandMoveByStep(AxisType, step, Speed);
  85. m_axisController.AddCommand(command);
  86. }
  87. private int GetStep(double value)
  88. {
  89. var step = (int)(value / Stepping);
  90. var gap = Expect - Actual;
  91. if (Math.Abs(gap) > 5 * Stepping)
  92. {
  93. Expect = Actual;
  94. }
  95. else if (gap > Stepping)
  96. {
  97. step++;
  98. }
  99. else if (gap < -Stepping)
  100. {
  101. step--;
  102. }
  103. Expect += value;
  104. return step;
  105. }
  106. public void Stop(object arg)
  107. {
  108. HandLock handLock = (HandLock)arg;
  109. CommandStop command = new CommandStop(AxisType, handLock);
  110. m_axisController.AddCommand(command);
  111. }
  112. public void Slide(bool isPositive, object arg = null)
  113. {
  114. if (Reversed) isPositive = !isPositive;
  115. m_axisController.AddCommand(new CommandMoveBorder(AxisType, isPositive ? Direction.A : Direction.B, Speed));
  116. }
  117. public void ResetPosition()
  118. {
  119. m_axisController.AddCommand(new CommandResetPosition(AxisType));
  120. }
  121. }
  122. }