123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- 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;
- }
- /// <summary>
- /// X|Y|Z
- /// </summary>
- public AxisType AxisType { get; set; }
- private int _speed;
- /// <summary>
- /// 脉冲/秒
- /// </summary>
- 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
- /// <summary>
- /// 外用
- /// </summary>
- 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
- }
- }
- }
|