按坐标系正常结束状态确认本次插补完成
This commit is contained in:
parent
599883e247
commit
eaabfd10cd
@ -158,6 +158,9 @@ namespace PLCSharp.VVMs.MotionController
|
||||
set { SetProperty(ref _RelativeDone, value); }
|
||||
}
|
||||
|
||||
internal readonly object InterpolationGate = new();
|
||||
internal InterpolationGroup.InterpolationRun InterpolationResult;
|
||||
internal ushort RunningCoordinate;
|
||||
private bool _InterpolationDone;
|
||||
|
||||
/// <summary>
|
||||
@ -227,7 +230,7 @@ namespace PLCSharp.VVMs.MotionController
|
||||
/// </summary>
|
||||
public void Stop()
|
||||
{
|
||||
MotionFlow.Step = 101;
|
||||
lock (InterpolationGate) MotionFlow.Step = 101;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -365,19 +365,8 @@ namespace PLCSharp.VVMs.MotionController
|
||||
break;
|
||||
|
||||
case 31:
|
||||
axis.ErrCode = SDK.MulticoorMove(axis.Interpolations, axis.InterpolationGroupParams);
|
||||
if (axis.ErrCode != 0)
|
||||
{
|
||||
axis.MotionFlow.Step = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
axis.MotionFlow.Step++;
|
||||
}
|
||||
break;
|
||||
|
||||
case 32:
|
||||
axis.MotionFlow.Step = 0;
|
||||
ScanInterpolation(axis, SDK);
|
||||
break;
|
||||
|
||||
case 101:
|
||||
@ -388,6 +377,48 @@ namespace PLCSharp.VVMs.MotionController
|
||||
}
|
||||
}
|
||||
|
||||
internal void ScanInterpolation(Axis axis, ControllerSDK sdk)
|
||||
{
|
||||
lock (axis.InterpolationGate)
|
||||
{
|
||||
if (axis.MotionFlow.Step == 31)
|
||||
{
|
||||
axis.InterpolationDone = false;
|
||||
var parameters = axis.InterpolationGroupParams;
|
||||
axis.RunningCoordinate = parameters.Coordinate;
|
||||
// 两组不同的轴也可能使用同一坐标系。新启动会停止该坐标系原有的插补。
|
||||
if (Axes.Any(other => other != axis && other.MotionFlow.Step == 32
|
||||
&& other.RunningCoordinate == parameters.Coordinate))
|
||||
{
|
||||
axis.ErrorMessage = $"插补坐标系 {parameters.Coordinate} 正忙";
|
||||
axis.MotionFlow.Step = 0;
|
||||
return;
|
||||
}
|
||||
axis.ErrCode = sdk.MulticoorMove(axis.Interpolations, parameters);
|
||||
axis.MotionFlow.Step = axis.ErrCode == 0 ? 32 : 0;
|
||||
return;
|
||||
}
|
||||
if (axis.MotionFlow.Step != 32) return;
|
||||
var state = sdk.GetInterpolationRunState(axis.RunningCoordinate);
|
||||
switch (state)
|
||||
{
|
||||
case 0: // 运行
|
||||
case 1: // 暂停,恢复后继续监视同一条指令
|
||||
case 3: // 刚打开,等待启动
|
||||
return;
|
||||
case 2: // 已启动的缓冲区正常执行完毕
|
||||
axis.InterpolationDone = true;
|
||||
if (axis.InterpolationResult != null)
|
||||
axis.InterpolationResult.Completed = true;
|
||||
break;
|
||||
default: // 包括空闲、异常减速停止和读取错误
|
||||
axis.ErrorMessage = $"插补未正常结束,坐标系 {axis.RunningCoordinate},状态 {state}";
|
||||
break;
|
||||
}
|
||||
axis.MotionFlow.Step = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private ControllerSDK SDK;
|
||||
private BackgroundWorker bkgWorker;
|
||||
private string _IP;
|
||||
@ -519,7 +550,7 @@ namespace PLCSharp.VVMs.MotionController
|
||||
/// <param name="axis">目标轴</param>
|
||||
internal void Stop(Axis axis)
|
||||
{
|
||||
axis.MotionFlow.Step = 0;
|
||||
lock (axis.InterpolationGate) axis.MotionFlow.Step = 0;
|
||||
SDK.Stop(axis.AxisNo);
|
||||
if (axis.ALM)
|
||||
{
|
||||
|
||||
@ -54,6 +54,10 @@ namespace PLCSharp.VVMs.MotionController
|
||||
|
||||
public abstract short MulticoorMove(List<Interpolation> interpolations, InterpolationGroupParams interpolationParams);
|
||||
|
||||
// conti_get_run_state: 0 运行,1 暂停,2 正常结束,3 未启动,4 空闲。
|
||||
// 未实现查询的驱动返回未知状态,不能作为完成依据。
|
||||
public virtual short GetInterpolationRunState(ushort coordinate) => -1;
|
||||
|
||||
public abstract short Save(ushort axisNo, AxisParams axisParams);
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -327,8 +327,14 @@ namespace PLCSharp.VVMs.MotionController
|
||||
break;
|
||||
|
||||
default:
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
if (ret != 0)
|
||||
{
|
||||
LTDMC.dmc_conti_close_list(ControllerNo, interpolationParams.Coordinate);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
ret = LTDMC.dmc_conti_start_list(ControllerNo,
|
||||
interpolationParams.Coordinate);
|
||||
@ -339,6 +345,9 @@ namespace PLCSharp.VVMs.MotionController
|
||||
return ret;
|
||||
}
|
||||
|
||||
public override short GetInterpolationRunState(ushort coordinate) =>
|
||||
LTDMC.dmc_conti_get_run_state(ControllerNo, coordinate);
|
||||
|
||||
/// <summary>
|
||||
/// 保存
|
||||
/// </summary>
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
using PLCSharp.Core.Common;
|
||||
using PLCSharp.Core.Tools;
|
||||
using PLCSharp.Core.Tools;
|
||||
using Prism.Mvvm;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
@ -164,6 +163,8 @@ namespace PLCSharp.VVMs.MotionController
|
||||
/// </summary>
|
||||
public class InterpolationGroupParams : BindableBase
|
||||
{
|
||||
internal InterpolationGroupParams Snapshot() => (InterpolationGroupParams)MemberwiseClone();
|
||||
|
||||
private string _Prompt;
|
||||
|
||||
/// <summary>
|
||||
@ -292,74 +293,76 @@ namespace PLCSharp.VVMs.MotionController
|
||||
|
||||
#region 方法
|
||||
|
||||
private readonly FlowModel flowModel = new();
|
||||
internal sealed class InterpolationRun(InterpolationGroup group)
|
||||
{
|
||||
internal readonly Axis X = group.AxisX;
|
||||
internal readonly Axis Y = group.AxisY;
|
||||
internal readonly InterpolationGroupParams Parameters = group.Params.Snapshot();
|
||||
internal readonly List<Interpolation> Segments = [.. group.Interpolations];
|
||||
internal volatile bool Completed;
|
||||
}
|
||||
|
||||
private readonly object _gate = new();
|
||||
private InterpolationRun _run;
|
||||
private bool _pending;
|
||||
|
||||
/// <summary>
|
||||
/// 运行
|
||||
/// </summary>
|
||||
public void Run()
|
||||
{
|
||||
if (AxisX == null || AxisY == null) return;
|
||||
switch (flowModel.Step)
|
||||
lock (_gate)
|
||||
{
|
||||
case 1:
|
||||
|
||||
AxisX.Interpolations = [.. Interpolations];
|
||||
AxisX.InterpolationGroupParams = Params;
|
||||
AxisX.MotionFlow.Step = 31;
|
||||
flowModel.Step++;
|
||||
break;
|
||||
|
||||
case 10:
|
||||
if (AxisX.InterpolationDone)
|
||||
if (!_pending) return;
|
||||
_pending = false;
|
||||
var axis = _run.X;
|
||||
lock (axis.InterpolationGate)
|
||||
{
|
||||
if (axis.MotionFlow.Step != 0 || _run.Y.MotionFlow.Step != 0)
|
||||
{
|
||||
flowModel.Step = 0;
|
||||
flowModel.Completed = true;
|
||||
axis.ErrorMessage = "插补轴忙,未启动插补";
|
||||
return;
|
||||
}
|
||||
|
||||
break;
|
||||
axis.Interpolations = _run.Segments;
|
||||
axis.InterpolationGroupParams = _run.Parameters;
|
||||
axis.InterpolationResult = _run;
|
||||
axis.InterpolationDone = false;
|
||||
axis.MotionFlow.Step = 31;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Go
|
||||
/// </summary>
|
||||
/// <returns>返回布尔值</returns>
|
||||
public bool Go()
|
||||
{
|
||||
flowModel.Completed = false;
|
||||
|
||||
if (AxisX == null || AxisY == null)
|
||||
lock (_gate)
|
||||
{
|
||||
flowModel.Step = 0;
|
||||
return false;
|
||||
if (_pending) return false;
|
||||
if (_run != null)
|
||||
{
|
||||
lock (_run.X.InterpolationGate)
|
||||
if (_run.X.InterpolationResult == _run && _run.X.MotionFlow.Step is 31 or 32)
|
||||
return false;
|
||||
}
|
||||
_run = null;
|
||||
if (AxisX == null || AxisY == null || Interpolations.Count == 0 ||
|
||||
AxisX.MotionFlow.Step != 0 || AxisY.MotionFlow.Step != 0) return false;
|
||||
_run = new InterpolationRun(this);
|
||||
_pending = true;
|
||||
return true;
|
||||
}
|
||||
if (Interpolations.Count < 1)
|
||||
{
|
||||
flowModel.Step = 0;
|
||||
return false;
|
||||
}
|
||||
flowModel.Step = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// IsDone
|
||||
/// </summary>
|
||||
/// <returns>返回布尔值</returns>
|
||||
public bool IsDone()
|
||||
{
|
||||
return flowModel.Completed;
|
||||
lock (_gate) return _run?.Completed == true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止
|
||||
/// </summary>
|
||||
public void Stop()
|
||||
{
|
||||
AxisX?.Stop();
|
||||
AxisY?.Stop();
|
||||
flowModel.Reset();
|
||||
lock (_gate)
|
||||
{
|
||||
_pending = false;
|
||||
(_run?.X ?? AxisX)?.Stop();
|
||||
(_run?.Y ?? AxisY)?.Stop();
|
||||
_run = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 方法
|
||||
|
||||
@ -315,8 +315,14 @@ namespace PLCSharp.VVMs.MotionController
|
||||
break;
|
||||
|
||||
default:
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
if (ret != 0)
|
||||
{
|
||||
LTSMC.smc_conti_close_list(ControllerNo, interpolationParams.Coordinate);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
ret = LTSMC.smc_conti_start_list(ControllerNo,
|
||||
interpolationParams.Coordinate);
|
||||
@ -327,6 +333,9 @@ namespace PLCSharp.VVMs.MotionController
|
||||
return ret;
|
||||
}
|
||||
|
||||
public override short GetInterpolationRunState(ushort coordinate) =>
|
||||
LTSMC.smc_conti_get_run_state(ControllerNo, coordinate);
|
||||
|
||||
/// <summary>
|
||||
/// 保存
|
||||
/// </summary>
|
||||
|
||||
142
Tests/PLCSharp.Tests/InterpolationGroupTests.cs
Normal file
142
Tests/PLCSharp.Tests/InterpolationGroupTests.cs
Normal file
@ -0,0 +1,142 @@
|
||||
using PLCSharp.VVMs.MotionController;
|
||||
|
||||
namespace PLCSharp.Tests;
|
||||
|
||||
public class InterpolationGroupTests
|
||||
{
|
||||
private sealed class FakeSdk : SMC
|
||||
{
|
||||
internal short StartError;
|
||||
internal short State;
|
||||
internal List<ushort> Starts { get; } = [];
|
||||
internal List<ushort> Reads { get; } = [];
|
||||
|
||||
public override short MulticoorMove(List<Interpolation> segments,
|
||||
InterpolationGroup.InterpolationGroupParams parameters)
|
||||
{
|
||||
Starts.Add(parameters.Coordinate);
|
||||
return StartError;
|
||||
}
|
||||
|
||||
public override short GetInterpolationRunState(ushort coordinate)
|
||||
{
|
||||
Reads.Add(coordinate);
|
||||
return State;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly Controller _controller = new();
|
||||
private readonly FakeSdk _sdk = new();
|
||||
private readonly InterpolationGroup _group = new()
|
||||
{
|
||||
AxisX = new Axis { AxisNo = 3 },
|
||||
AxisY = new Axis { AxisNo = 6 },
|
||||
Interpolations = [new Interpolation()]
|
||||
};
|
||||
|
||||
public InterpolationGroupTests()
|
||||
{
|
||||
_controller.Axes = [_group.AxisX, _group.AxisY];
|
||||
_group.Params.Coordinate = 1;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Assert.True(_group.Go());
|
||||
Assert.False(_group.IsDone());
|
||||
_group.Run();
|
||||
Assert.False(_group.AxisX.InterpolationDone);
|
||||
_controller.ScanInterpolation(_group.AxisX, _sdk);
|
||||
Assert.False(_group.IsDone());
|
||||
}
|
||||
|
||||
private void Poll(short state)
|
||||
{
|
||||
_sdk.State = state;
|
||||
_controller.ScanInterpolation(_group.AxisX, _sdk);
|
||||
_group.Run();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PausedListCompletesOnlyAfterNormalEndOnOriginalCoordinate()
|
||||
{
|
||||
_group.AxisX.InterpolationDone = true;
|
||||
Start();
|
||||
_group.Params.Coordinate = 3;
|
||||
foreach (short state in new short[] { 3, 0, 1, 1, 0 })
|
||||
{
|
||||
Poll(state);
|
||||
Assert.False(_group.IsDone());
|
||||
Assert.False(_group.Go());
|
||||
}
|
||||
Poll(2);
|
||||
Assert.True(_group.IsDone());
|
||||
Assert.True(_group.AxisX.InterpolationDone);
|
||||
Assert.Equal(new ushort[] { 1 }, _sdk.Starts);
|
||||
Assert.Equal(6, _sdk.Reads.Count);
|
||||
Assert.All(_sdk.Reads, coordinate => Assert.Equal((ushort)1, coordinate));
|
||||
Start();
|
||||
Poll(2); // 很短的轨迹可能在首次查询前已经完成
|
||||
Assert.True(_group.IsDone());
|
||||
Assert.Equal((ushort)3, _sdk.Reads.Last());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(4)]
|
||||
[InlineData(5)]
|
||||
[InlineData(-1)]
|
||||
[InlineData(99)]
|
||||
public void AbortedOrUnknownStateCannotLaterBecomeSuccess(short state)
|
||||
{
|
||||
Start();
|
||||
Poll(state);
|
||||
Poll(2);
|
||||
Assert.False(_group.IsDone());
|
||||
Assert.False(_group.AxisX.InterpolationDone);
|
||||
Assert.True(_group.AxisX.Error);
|
||||
Assert.Single(_sdk.Reads);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RejectedStartDoesNotReadPreviousListCompletion()
|
||||
{
|
||||
_sdk.StartError = 7;
|
||||
Start();
|
||||
Poll(2);
|
||||
Assert.False(_group.IsDone());
|
||||
Assert.Equal(7, _group.AxisX.ErrCode);
|
||||
Assert.Empty(_sdk.Reads);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StoppingBetweenScansDoesNotPublishLateCompletion()
|
||||
{
|
||||
Start();
|
||||
Poll(1);
|
||||
_group.Stop();
|
||||
Poll(2);
|
||||
Assert.False(_group.IsDone());
|
||||
Assert.Equal(101, _group.AxisX.MotionFlow.Step);
|
||||
Assert.Single(_sdk.Reads);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SecondGroupCannotReplaceRunningCoordinate()
|
||||
{
|
||||
Start();
|
||||
var other = new InterpolationGroup
|
||||
{
|
||||
AxisX = new Axis(), AxisY = new Axis(), Interpolations = [new Interpolation()]
|
||||
};
|
||||
other.Params.Coordinate = 1;
|
||||
_controller.Axes.Add(other.AxisX);
|
||||
_controller.Axes.Add(other.AxisY);
|
||||
Assert.True(other.Go());
|
||||
other.Run();
|
||||
_controller.ScanInterpolation(other.AxisX, _sdk);
|
||||
Assert.Single(_sdk.Starts);
|
||||
Poll(2);
|
||||
Assert.True(_group.IsDone());
|
||||
Assert.False(other.IsDone());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user