LiftMonitor.cs
10.9 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
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 DeviceLibrary
{
public class LiftMonitor : ISafetyDevice
{
/// <summary>
/// 关闭端延时
/// </summary>
public int DownOverTimeMS = 0;
public int UpOverTimeMS = 0;
public bool SlowAftPause = false;
string up;
string down;
string saftylight;
string axisbreak;
public AxisBean axisBean;
int upspeed;
int downspeed;
int StrokeLength = 270000;
MoveInfo moveInfo1;
bool paused = false;
public LiftMonitor(string _up, string _down, string _saftylight, string _break, AxisBean _axisBean, int _StrokeLength, int _upspeed, int _downspeed = 0, string name = "升降机构")
{
moveInfo1 = new MoveInfo(name, false);
up = _up;
down = _down;
saftylight = _saftylight;
axisBean = _axisBean;
upspeed = _upspeed;
axisbreak = _break;
downspeed = _downspeed;
StrokeLength = _StrokeLength;
if (downspeed == 0)
{
downspeed = upspeed;
}
SafetyDevice.AddDevice(this);
}
public void UpdateParam(int length, int upSpeed, int downSpeed = 0)
{
StrokeLength = length;
upspeed = upSpeed;
if (downspeed == 0)
{
downspeed = upspeed;
}
}
public void ClearAlarm()
{
}
public bool isAtTOP
{
get
{
return IOManager.IOValue(up.ToString()).Equals(IO_VALUE.HIGH);
}
}
public bool isAtBOTTOM
{
get
{
return IOManager.IOValue(down.ToString()).Equals(IO_VALUE.HIGH);
}
}
public void LiftUp(MoveInfo moveInfo)
{
if (moveInfo == null)
moveInfo = moveInfo1;
if (IOManager.IOValue(IO_Type.SuddenStop_BTN).Equals(IO_VALUE.LOW))
{
moveInfo.log($"急停未解除");
return;
}
double speed = (double)upspeed;
if (paused && SlowAftPause)
{
speed = speed / 3.0;
moveInfo.log($"上一次有暂停,速度降为{speed}");
}
paused = false;
if (IOManager.IOValue(up.ToString()).Equals(IO_VALUE.HIGH))
{
moveInfo.log($"{axisBean.AxisName},已在位置,无需上升");
return;
}
if (!axisBean.IsServeoOn)
axisBean.Open(true, out string msg);
if (!string.IsNullOrEmpty(axisbreak))
{
IOManager.IOMove(axisbreak, IO_VALUE.HIGH);
Thread.Sleep(200);
}
axisBean.RelMove(StrokeLength, speed);
DateTime d = DateTime.Now;
moveInfo.log($"{axisBean.AxisName},LiftUp");
var wr = WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitUp), $"等待顶升[{axisBean.Config.Explain}]机构上升");
if (moveInfo != null)
moveInfo.WaitList.Add(wr);
Task.Run(() =>
{
while (!IOManager.IOValue(up.ToString()).Equals(IO_VALUE.HIGH))
{
Task.Delay(15).Wait();
if (paused)
{
paused = false;
return;
}
if (IOManager.IOValue(IO_Type.SuddenStop_BTN).Equals(IO_VALUE.LOW))
{
moveInfo.log("设备急停");
emergencyStop();
return;
}
if (!string.IsNullOrEmpty(saftylight) && IOManager.IOValue(saftylight).Equals(IO_VALUE.LOW) && IOManager.IOValue(down.ToString()).Equals(IO_VALUE.LOW))
{
moveInfo.log($"{saftylight}触发停止");
Pause();
return;
}
}
moveInfo1.WaitList.Clear();
moveInfo.WaitList.Remove(wr);
if (UpOverTimeMS > 0)
Task.Delay(UpOverTimeMS).Wait();
axisBean.SuddenStop();
if (!string.IsNullOrEmpty(axisbreak))
{
IOManager.IOMove(axisbreak, IO_VALUE.LOW);
axisBean.ServoOff();
}
var t = (DateTime.Now - d).TotalSeconds;
moveInfo.log($"{axisBean.AxisName},上升到位,s:{t}");
});
}
bool WaitUp(WaitResultInfo w)
{
if ((DateTime.Now - LastResumeTime).TotalSeconds < ResumeWaitTimeSec)
return false;
if (IOManager.IOValue(up.ToString()).Equals(IO_VALUE.HIGH))
{
if (UpOverTimeMS > 0)
Task.Delay(UpOverTimeMS).Wait();
axisBean.SuddenStop();
if (!string.IsNullOrEmpty(axisbreak))
{
IOManager.IOMove(axisbreak, IO_VALUE.LOW);
axisBean.ServoOff();
}
return true;
}
if (!axisBean.IsBusy)
{
LogUtil.info("恢复继续上升");
LiftUp(null);
}
return false;
}
public void LiftDown(MoveInfo moveInfo)
{
if (moveInfo == null)
moveInfo = moveInfo1;
if (IOManager.IOValue(IO_Type.SuddenStop_BTN).Equals(IO_VALUE.LOW))
{
moveInfo.log($"急停未解除");
return;
}
double speed = (double)downspeed;
if (paused && SlowAftPause)
{
speed = speed / 3.0;
moveInfo.log($"上一次有暂停,速度降为{speed}");
}
paused = false;
if (IOManager.IOValue(down.ToString()).Equals(IO_VALUE.HIGH))
{
moveInfo.log($"{axisBean.AxisName},已在位置,无需下降");
return;
}
if (!axisBean.IsServeoOn)
axisBean.Open(true, out string msg);
if (!string.IsNullOrEmpty(axisbreak))
{
IOManager.IOMove(axisbreak, IO_VALUE.HIGH);
Thread.Sleep(200);
}
axisBean.RelMove(-StrokeLength, speed);
DateTime d = DateTime.Now;
moveInfo.log($"{axisBean.AxisName},LiftDown");
var wr = WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitDown), $"等待顶升[{axisBean.Config.Explain}]机构下降");
if (moveInfo != null)
moveInfo.WaitList.Add(wr);
Task.Run(() =>
{
while (!IOManager.IOValue(down.ToString()).Equals(IO_VALUE.HIGH))
{
Task.Delay(15).Wait();
if (paused)
{
paused = false;
return;
}
if (IOManager.IOValue(IO_Type.SuddenStop_BTN).Equals(IO_VALUE.LOW))
{
LogUtil.info("设备急停");
emergencyStop();
return;
}
if (!string.IsNullOrEmpty(saftylight) && IOManager.IOValue(saftylight).Equals(IO_VALUE.LOW))
{
moveInfo.log($"{saftylight}触发停止");
Pause();
return;
}
}
moveInfo1.WaitList.Clear();
moveInfo.WaitList.Remove(wr);
if (DownOverTimeMS > 0)
Task.Delay(DownOverTimeMS).Wait();
axisBean.SuddenStop();
if (!string.IsNullOrEmpty(axisbreak))
{
IOManager.IOMove(axisbreak, IO_VALUE.LOW);
axisBean.ServoOff();
}
var t = (DateTime.Now - d).TotalSeconds;
moveInfo.log($"{axisBean.AxisName},下降到位,s:{t}");
});
}
bool WaitDown(WaitResultInfo w)
{
if ((DateTime.Now - LastResumeTime).TotalSeconds < ResumeWaitTimeSec)
return false;
if (IOManager.IOValue(down.ToString()).Equals(IO_VALUE.HIGH))
{
if (DownOverTimeMS > 0)
Task.Delay(DownOverTimeMS).Wait();
axisBean.SuddenStop();
if (!string.IsNullOrEmpty(axisbreak))
{
IOManager.IOMove(axisbreak, IO_VALUE.LOW);
axisBean.ServoOff();
}
return true;
}
if (!axisBean.IsBusy)
{
LogUtil.info("恢复继续下降");
LiftDown(null);
}
return false;
}
void emergencyStop()
{
paused = true;
axisBean.SuddenStop();
if (!string.IsNullOrEmpty(axisbreak))
{
IOManager.IOMove(axisbreak, IO_VALUE.LOW);
axisBean.ServoOff();
}
}
public void Pause()
{
// if (!isDoorClose())
{
paused = true;
axisBean.SuddenStop();
if (!string.IsNullOrEmpty(axisbreak))
{
IOManager.IOMove(axisbreak, IO_VALUE.LOW);
axisBean.ServoOff();
}
}
}
DateTime LastResumeTime = DateTime.Now;
public int ResumeWaitTimeSec = 0;
public void Resume()
{
ResumeSingle();
}
public void ResumeSingle()
{
LastResumeTime = DateTime.Now;
try
{
if (moveInfo1.WaitList.Count > 0)
{
if (moveInfo1.WaitList[0].WaitType == WaitEnum.W013_Action)
{
var wt = moveInfo1.WaitList[0];
for (int i = 0; i < 100; i++)
{
var w = wt.Action?.Invoke(moveInfo1.WaitList[0]);
if (w == null)
return;
if (w.Value)
break;
Task.Delay(100).Wait();
}
}
}
}
catch { }
}
}
}