using StageController.M3H;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StageController
{
internal class M3HAxis : IMotion
{
AxisController m_axisController;
///
/// 0-999
///
public int Speed { get; set; }
public int Step { get; set; }
///
/// 指令位置:用于补偿实际位置
///
double Expect { get; set; }
public double Actual
{
get
{
if (Reversed)
return (double)(-Step * Stepping);
return (double)(Step * Stepping);
}
}
///
/// 0:停机自由,1:停机锁死,2:正向连续,3:反向连续,4:正向步进,5:反向步进
///
public int State { get; set; }
private Border _border;
public int Limit
{
get
{
if (Reversed)
{
switch (_border)
{
case Border.F:
return (int)Border.R;
case Border.R:
return (int)Border.F;
}
}
return (int)_border;
}
set { _border = (Border)value; }
}
public M3HAxisType AxisType { get; private set; }
///
/// 步距
///
public double Stepping { get; set; }
public bool Reversed { get; set; }
public M3HAxis(M3HAxisType type, AxisController axisController)
{
m_axisController = axisController;
AxisType = type;
if (m_axisController == null)
throw new ArgumentNullException("AxisController is Null!");
}
public void Move(int step, object arg = null)
{
if (Reversed) step = -step;
CommandMoveByStep command = new CommandMoveByStep(AxisType, step, Speed);
m_axisController.AddCommand(command);
}
public void Move(double value, object arg = null)
{
if (Reversed) value = -value;
var step = GetStep(value);
CommandMoveByStep command = new CommandMoveByStep(AxisType, step, Speed);
m_axisController.AddCommand(command);
}
public void To(double value)
{
if (Reversed) value = -value;
var step = GetStep(value);
step -= Step;
CommandMoveByStep command = new CommandMoveByStep(AxisType, step, Speed);
m_axisController.AddCommand(command);
}
private int GetStep(double value)
{
var step = (int)(value / Stepping);
var gap = Expect - Actual;
if (Math.Abs(gap) > 5 * Stepping)
{
Expect = Actual;
}
else if (gap > Stepping)
{
step++;
}
else if (gap < -Stepping)
{
step--;
}
Expect += value;
return step;
}
public void Stop(object arg)
{
HandLock handLock = (HandLock)arg;
CommandStop command = new CommandStop(AxisType, handLock);
m_axisController.AddCommand(command);
}
public void Slide(bool isPositive, object arg = null)
{
if (Reversed) isPositive = !isPositive;
m_axisController.AddCommand(new CommandMoveBorder(AxisType, isPositive ? Direction.A : Direction.B, Speed));
}
public void ResetPosition()
{
m_axisController.AddCommand(new CommandResetPosition(AxisType));
}
}
}