AxisBean.cs
9.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using DeviceLib;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class AxisBean
{
public ConfigMoveAxis Config = null;
public static int TimeoutInterval = 500;
/// <summary>
/// 正常工作过程中判断位置是否到达时使用
/// </summary>
public int LastPosition = 0;
public string AxisName;
public AxisBean(ConfigMoveAxis axisConfig, string deviceName)
{
this.Config = axisConfig;
AxisName = deviceName + " " + Config.Explain + "[" + Config.DeviceName + "-" + Config.GetAxisValue() + "]";
}
private bool IsIntSlvBlock = false;
public bool Open(bool isCheck, out string Msg)
{
IOManager.IOMove(Config.Axis_Run_DO,IO_VALUE.HIGH);
Msg = "";
string portName = Config.DeviceName;
int slvAddr = Config.GetAxisValue();
//打开 轴
int bro = ConfigAppSettings.GetIntValue(Setting_Init.ACBaudRate);
ACServerManager.OpenPort(Config.DeviceName,bro);
Thread.Sleep(50);
//初始化
if (!IsIntSlvBlock)
{
ACServerManager.InitSlvAddr(portName, slvAddr, Config.TargetSpeed, Config.AddSpeed, Config.DelSpeed);
Thread.Sleep(100);
}
ACServerManager.AlarmClear(portName, slvAddr);
Thread.Sleep(50);
ACServerManager.ServoOn(portName, slvAddr);
Thread.Sleep(1000);
//打开所有轴
if (isCheck)
{
if (!OpenAxis(out Msg))
{
return false;
}
}
IOManager.IOMove(Config.Axis_Brake_DO, IO_VALUE.HIGH);
IsIntSlvBlock = true;
return true;
}
/// <summary>
/// 打开所有轴
/// </summary>
/// <returns></returns>
private bool OpenAxis(out string msg)
{
msg = "";
//判断轴是否正常
string portName = Config.DeviceName;
int slvAddr = Config.GetAxisValue();
if (ACServerManager.ServerOnStatus(portName, slvAddr))
{
LogUtil.info(AxisName + "成功打开");
}
else
{
//清理报警,再重新打开一次
LogUtil.info(AxisName + "第一次打开失败,先清理一下报警,再重新打开一次");
ACServerManager.AlarmClear(portName, slvAddr);
System.Threading.Thread.Sleep(1200);
ACServerManager.ServoOn(portName, slvAddr);
System.Threading.Thread.Sleep(100);
if (ACServerManager.ServerOnStatus(portName, slvAddr))
{
LogUtil.info(AxisName + "清理报警后重新打卡轴成功:" + Config.Explain);
}
else
{
ACServerManager.ServoOff(portName, slvAddr);
msg = "打开轴" + Config.Explain + "失败 ";
LogUtil.info(AxisName + msg);
return false;
}
}
return true;
}
public void ServoOff()
{
LogUtil.info("ServoOff【" + AxisName + "】");
IOManager.IOMove(Config.Axis_Brake_DO, IO_VALUE.LOW);
ACServerManager.ServoOff(Config.DeviceName, Config.GetAxisValue());
IOManager.IOMove(Config.Axis_Run_DO, IO_VALUE.LOW);
}
public void HomeMove(StoreMoveInfo MoveInfo)
{
Config.TargetPosition = 0;
LogUtil.info(AxisName + "speed[" + Config.TargetSpeed + "]开始原点返回");
MoveInfo.WaitList.Add(WaitResultInfo.WaitAxis(Config, true));
ACServerManager.HomeMove(Config.DeviceName, (short)Config.GetAxisValue(), Config.HomeHighSpeed);
}
public void AbsMove(StoreMoveInfo MoveInfo, int targetPosition, int targetSpeed)
{
if (MoveInfo == null)
{
AbsMove(targetPosition, targetSpeed);
}
else
{
MoveInfo.WaitList.Add(WaitResultInfo.WaitAxis(Config, targetPosition, targetSpeed));
Config.TargetPosition = targetPosition;
ACServerManager.AbsMove(Config.DeviceName, Config.GetAxisValue(), targetPosition, targetSpeed);
}
}
public static bool ACAxisMoveIsEnd(StoreMoveInfo MoveInfo, ConfigMoveAxis axis, int targetPosition, int targetSpeed, out string msg)
{
msg = "";
string deviceName = axis.DeviceName;
short axisNo = axis.GetAxisValue();
bool isOk = ACServerManager.GetBusyStatus(deviceName, axisNo).Equals(0);
int outCount = ACServerManager.GetActualtPosition(deviceName, axisNo);
int errorCount = Math.Abs(outCount - targetPosition);
if (isOk)
{
if (errorCount <= axis.CanErrorCountMax)
{
return true;
}
//判断是否需要重新运动
if (MoveInfo.CanWhileCount > 0)
{
LogUtil.error(MoveInfo.Name + axis.DisplayStr + "目标位置[" + targetPosition + "]当前位置[" + outCount +
"],误差过大,重新开始运动,剩余[" + MoveInfo.CanWhileCount + "]次");
ACServerManager.SuddenStop(axis.DeviceName, axis.GetAxisValue());
ACServerManager.AbsMove(axis.DeviceName, axis.GetAxisValue(), targetPosition, targetSpeed);
MoveInfo.CanWhileCount--;
}
else
{
msg = " " + MoveInfo.MoveStep + MoveInfo.Name + axis.DisplayStr + ",目标位置[" + targetPosition + "]当前位置[" + outCount
+ "],误差过大,需要报警";
LogUtil.error(msg, 600);
}
}
return false;
}
public static bool HomeMoveIsEnd(StoreMoveInfo MoveInfo, ConfigMoveAxis axis, out string msg)
{
msg = "";
if (ACServerManager.IsHomeMoveEnd(axis.DeviceName, axis.GetAxisValue()))
{
//原点完成并且位置=0
int outCount = ACServerManager.GetActualtPosition(axis.DeviceName, axis.GetAxisValue());
int errorCount = Math.Abs(outCount);
if (errorCount <= axis.CanErrorCountMax)
{
return true;
}
//判断是否需要重新运动
if (MoveInfo.CanWhileCount > 0)
{
LogUtil.error(MoveInfo.Name + axis.DisplayStr + "收到原点完成信号,当前位置[" + outCount + "],重新回原点,剩余[" + MoveInfo.CanWhileCount + "]次");
//LogUtil.error( StoreName + moveAxis.DisplayStr + "重新回原点");
ACServerManager.HomeMove(axis.DeviceName, axis.GetAxisValue(), axis.HomeHighSpeed,true);
MoveInfo.CanWhileCount--;
}
else
{
msg =MoveInfo.Name+ " " + MoveInfo.MoveType + axis.DisplayStr + ",收到原点完成信号,当前位置[" + outCount + "],误差过大,需要报警";
LogUtil.error(msg);
}
}
return false;
}
public int GetAclPosition()
{
int p = ACServerManager.GetActualtPosition(Config.DeviceName,Config.GetAxisValue());
return p;
}
public bool IsInPosition(int targetP)
{
int currp = GetAclPosition();
int chaz = targetP - currp;
if (Math.Abs(chaz) < Config.CanErrorCountMax)
{
return true;
}
return false;
}
/// <summary>
/// 绝对运动至点,不等待结果
/// </summary>
private void AbsMove(int targetPos, double targetSpeed)
{
if (targetPos.Equals(-1))
{
return;
}
LastPosition = -1;
if (targetSpeed > Config.TargetSpeed || targetSpeed <= 0)
{
targetSpeed = Config.TargetSpeed;
}
//小于1,表示是目标速度的百分比
else if (targetSpeed <= 1)
{
targetSpeed = Config.TargetSpeed * targetSpeed;
}
ACServerManager.AbsMove(Config.DeviceName, Config.GetAxisValue(), targetPos, (int)targetSpeed);
}
public void SuddenStop()
{
ACServerManager.SuddenStop(Config.DeviceName, Config.GetAxisValue());
}
}
}