using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace StageController
{
class AxisShell
{
AxisController m_axisController;
internal AxisShell(AxisType type, AxisController axisController)
{
m_axisController = axisController;
AxisType = type;
Speed = 2000;
}
///
/// X|Y|Z
///
public AxisType AxisType { get; set; }
private int _speed;
///
/// 脉冲/秒
///
public int Speed
{
get => _speed;
set
{
_speed = value;
}
}
private double _step;
public double Step { get => _step; set { if (State < 2 || State == 6) _step = value; } }
//指令位置
private double _stepCommand { get; set; }
public double Actual
{
get
{
return (double)(Step);
}
}
//内用
//Modify by shayg 20220615 start
//private int _state;
private volatile int _state;
//Modify by shayg 20220615 end
///
/// 外用
///
public int State
{
get => _state;
set
{
if (_state >= 6 && value < 2)
{
_state = value;
}
}
}
// Add by shayg 20220615 start
public int ExcessStep { get; set; }
// Add by shayg 20220615 end
public int Limit { get => 0; set { } }
public void To(double value)
{
if (value == _stepCommand) return;
if (!IsStop) return;
//new Task(() =>
//{
// _state = 8;
// DelayStart();
_stepCommand = value;
_state = value > Step ? 2 : 3;
//}).Start();
}
public void Move(int value, object arg = null)
{
if (value == 0) return;
if (!IsStop) return;
//new Task(() =>
//{
// _state = 8;
// DelayStart();
_stepCommand = Step + value;
_state = value > 0 ? 2 : 3;
//}).Start();
}
public void Move(double value, object arg = null)
{
if (value == 0) return;
if (!IsStop) return;
//new Task(() =>
//{
// _state = 8;
// DelayStart();
_stepCommand = Step + value;
_state = value > 0 ? 2 : 3;
//}).Start();
}
public void ResetPosition()
{
_step = 0;
}
public void Slide(bool isPositive, object arg = null)
{
//new Task(() =>
//{
// _state = 8;
// DelayStart();
_state = isPositive ? 4 : 5;
//}).Start();
}
public void Stop(object arg = null)
{
//new Task(() =>
//{
// DelayStart();
_state = 6;
//}).Start();
}
private void DelayStart(int time = 0)
{
if (time == 0)
Thread.Sleep(CommandDelay);
else
Thread.Sleep(time);
}
DateTime _time;
public int CommandDelay = 100;//模拟真实运动的指令延时
// modify by shayg 20220809 start
//public bool IsStop => State < 2;
public bool IsStop => State < 2 && ExcessStep == 0;
// modify by shayg 20220809 end
public void Execute()
{
#if false
m_axisController.RefreshPosition();
#else
var span = (DateTime.Now - _time).TotalMilliseconds;
_time = DateTime.Now;
double step = span / 1000 * Speed;
switch (_state)
{
case 0: /// 停止
case 1:
_stepCommand = _step;
break;
case 2: /// 正向定位
step = _step + step;
if (step >= _stepCommand)
{
_state = 7;
step = _stepCommand;
}
_step = step;
break;
case 3:/// 负向定位
step = _step - step;
if (step <= _stepCommand)
{
_state = 7;
step = _stepCommand;
}
_step = step;
break;
case 4:/// 正向连续
_step += step;
_stepCommand = _step;
break;
case 5:/// 负向连续
_step -= step;
_stepCommand = _step;
break;
case 6:
case 7:
m_axisController.RefreshPosition();
break;
default:
//_state = 0;
break;
}
#endif
}
}
}