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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
using FLY.Thick.Blowing.IService;
using Misc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FLY.Thick.Blowing.Server
{
/// <summary>
/// 处理转向信号
/// </summary>
public class LimitCellCollection:INotifyPropertyChanged
{
#region MARKNO
const int MARKNO_DELAY0 = 1;
const int MARKNO_DELAY1 = 2;
#endregion
/// <summary>
/// 正反信号滤波时间 150ms
/// </summary>
const int SIGN_DELAY_MS = 150;
#region 参数
/// <summary>
/// 转向信号100ms 滤波
/// </summary>
public bool IsSignFilter { get; set; }
/// <summary>
/// 缺少信号模式
/// 正反信号线接得不好,有时候会丢失了。
/// 当前的旋转时间是上一次的1.1倍时,在以前的位置增加信号!!!!
/// 当有连续两次相同的信号,且信号差刚好是旋转时间的1.9~2.1倍时,证明真的是缺少信号
/// </summary>
public bool IsLackSignMode { get; set; }
/// <summary>
/// 信号模式, 短信号模式(只撞一下),长信号模式(只要是转着,信号就长亮,直到换向)
/// </summary>
public BlowingSignType SignType { get; set; } = BlowingSignType.Short;
/// <summary>
/// 信号0 撞2次。 这个能自动设置。 两个撞同一信号 间隔30秒内,算一个换向信号。
/// 旋转架快过头,冲过了转向开关,转向开关回弹了, 旋转架回来时,再撞转向开关肯定是在第1次撞的开始位置。
/// 当为2次撞时: StartTime = 第1次撞的StartTime, EndTime = 第2次撞的StartTime
/// </summary>
public bool IsSign0Double { get; set; }
/// <summary>
/// 信号1 撞2次。 这个能自动设置。
/// </summary>
public bool IsSign1Double { get; set; }
public TimeSpan DefaultRPeriod { get; set; } = TimeSpan.FromMinutes(8);
#endregion
/// <summary>
/// 离开限位 到 撞下一个限位 的 旋转架转动时间, 需要初始值,以后测量出来的
/// </summary>
public TimeSpan RenZiJiaPeriod { get; private set; } = TimeSpan.FromMinutes(8);
/// <summary>
/// 撞限位 到 离开限位 的时间, 需要初始值,以后测量出来的
/// </summary>
public TimeSpan LimitSignTime { get; private set; } = TimeSpan.FromSeconds(4);
/// <summary>
/// 转向信号列表
/// </summary>
public RList<LimitCell> mLimitList { get; private set; }
public event Action<LimitCell> AddEvent;
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// 还没处理的信号0 列表
/// </summary>
List<SignData> mSign0ListNoJudge = new List<SignData>();
/// <summary>
/// 还没处理的信号1 列表
/// </summary>
List<SignData> mSign1ListNoJudge = new List<SignData>();
public LimitCellCollection()
{
mLimitList = new Misc.RList<LimitCell>(20);//人字架转10次的信号
Misc.BindingOperations.SetBinding(this, nameof(DefaultRPeriod), this, nameof(RenZiJiaPeriod));
}
public void Clear()
{
mLimitList.Clear();
mSign0ListNoJudge.Clear();
mSign1ListNoJudge.Clear();
RenZiJiaPeriod = DefaultRPeriod;
LimitSignTime = TimeSpan.FromSeconds(4);
}
public void AddLimit(SignData sd)
{
int no = sd.No;
if (IsSignFilter)
{
//信号滤波
if (no == 0)
{
mSign0ListNoJudge.Add(sd);
//等100ms再触发
FObjBase.PollModule.Current.Poll_Config(FObjBase.PollModule.POLL_CONFIG.ADD,
() =>
{
PopSign(0);
},
TimeSpan.FromMilliseconds(SIGN_DELAY_MS), true, false, this, MARKNO_DELAY0, true);
}
else
{
mSign1ListNoJudge.Add(sd);
//等100ms再触发
FObjBase.PollModule.Current.Poll_Config(FObjBase.PollModule.POLL_CONFIG.ADD,
() =>
{
PopSign(1);
},
TimeSpan.FromMilliseconds(SIGN_DELAY_MS), true, false, this, MARKNO_DELAY1, true);
}
}
else
{
update_limit(sd);
}
}
public void OnPoll()
{
//--------------------------------------------------------------------
//添加虚拟信号
if (IsLackSignMode)
OnPoll_Virtual();
//--------------------------------------------------------------------
//正反向信号撞两次的超时判断
OnPoll_checkDouble();
}
/// <summary>
/// 虚拟信号添加
/// </summary>
void OnPoll_Virtual()
{
DateTime now = DateTime.Now;
//--------------------------------------------------------------------
//正反向信号撞两次的超时判断
if (mLimitList.Count == 0)
return;
LimitCell lc = mLimitList.Last();
if (lc.isVirtual)//上一个不能是虚拟信号
return;
if (lc.dt_end == DateTime.MinValue)
return;//上一个信号还没完成
if (now - lc.dt_end < TimeSpan.FromSeconds(RenZiJiaPeriod.TotalSeconds + 20))
return;//时间还不够长
//已经大于上一次旋转信号+20s,都没有信号了
//添加虚拟信号
int no = (lc.no == 0) ? 1 : 0;
DateTime dt = lc.dt_end + RenZiJiaPeriod;
var cell = new LimitCell()
{
no = no,
dt_begin = dt,
dt_end = dt + LimitSignTime,
isVirtual = true
};
mLimitList.RAdd(cell);
AddEvent?.Invoke(cell);
}
/// <summary>
/// 正反向信号撞两次的超时判断
/// </summary>
void OnPoll_checkDouble()
{
if (mLimitList.Count == 0)
return;
LimitCell lc = mLimitList.Last();
//需要撞两次的超时判断!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (!lc.isDouble)
return;
if (lc.dt_end != DateTime.MinValue)
return;//已经结束了
//第2次还没撞
if (DateTime.Now - lc.dt_begin < TimeSpan.FromSeconds(30))
return;
//长时间第2次都没撞, 看来只需要撞一次而已
lc.isDouble = false;
if (lc.no == 0)
IsSign0Double = lc.isDouble;
else
IsSign1Double = lc.isDouble;
lc.dt_end = lc.dt_mid;
lc.dt_mid = DateTime.MinValue;
limitCellDtEndChanged();
}
/// <summary>
/// 转向信号 的 结束时间被修改了
/// </summary>
void limitCellDtEndChanged()
{
if (mLimitList.Count == 0)
return;
DateTime dt1 = mLimitList[mLimitList.Count - 1].dt_begin;
DateTime dt2 = mLimitList[mLimitList.Count - 1].dt_end;
if (dt1 == DateTime.MinValue || dt2 == DateTime.MinValue)
return;
if (dt2 > dt1) {
var ts = dt2 - dt1;
if (ts > TimeSpan.FromSeconds(30))
{
LimitSignTime = TimeSpan.FromSeconds(4);
}
LimitSignTime = ts;//更新撞信号的耗时
}
}
/// <summary>
/// 转向信号 的 开始时间被修改了
/// </summary>
void limitCellDtBeginChanged()
{
if (mLimitList.Count < 2)
return;
DateTime dt1 = mLimitList[mLimitList.Count - 1].dt_begin;
DateTime dt2 = mLimitList[mLimitList.Count - 2].dt_end;
if (dt1 == DateTime.MinValue || dt2 == DateTime.MinValue)
return;
if (dt1 > dt2)
{
RenZiJiaPeriod = dt1 - dt2;//只有这个是正确的!!!
//触发修正事件!!!!!
}
}
/// <summary>
/// 判断信号是否毛刺,最后会调用update_limit
/// </summary>
/// <param name="no"></param>
void PopSign(int no)
{
List<SignData> noJudgeList;
if (no == 0)
noJudgeList = mSign0ListNoJudge;
else
noJudgeList = mSign1ListNoJudge;
if (noJudgeList.Count() == 0)
{
//没有信号,异常
noJudgeList.Clear();
return;
}
if (noJudgeList.Count() == 1)
{
//只有一个信号,它持续了100ms,是信号不是毛刺
update_limit(noJudgeList.First());
}
else
{
SignData sd0 = noJudgeList.First();
SignData sd1 = noJudgeList.Last();
if (sd0.On == sd1.On)
{
//状态一样, 不应该会发生的情况,AD卡坏了?
//异常处理:两次信号间的都只是毛刺,触发第1个信号
update_limit(sd0);
}
else
{
if (sd1.Time - sd0.Time >= TimeSpan.FromMilliseconds(SIGN_DELAY_MS))
{
//两个信号时间差大于 100ms
//触发它们
update_limit(sd0);
update_limit(sd1);
}
else
{
//时间差在100ms内,只是毛刺而已
}
}
}
//判断完,清空列表!!!
noJudgeList.Clear();
}
/// <summary>
/// 添加正反转信号到 列表
/// </summary>
/// <param name="s"></param>
void update_limit(SignData s)
{
//上一个是虚拟信号
//1.当接收的信号与虚拟信号相同时,把虚拟信号删除!!!!!
//2.当接收的信号与虚拟信号不同时,更新旋转时间!!!!!
if (!update_limit_virtual(s))
{
switch (SignType)
{
case BlowingSignType.Short:
update_limit_short(s);
break;
default:
update_limit_long(s);
break;
}
}
}
/// <summary>
/// 处理上一个是虚拟信号的正反转信号,它可能是多余的,返回true,新来的真实信号已经被处理
/// </summary>
/// <param name="dt"></param>
/// <param name="no"></param>
/// <param name="status"></param>
/// <returns></returns>
bool update_limit_virtual(SignData s)
{
DateTime dt = s.Time;
int no = s.No;
bool status = s.On;
if (mLimitList.Count() == 0)
return false;
LimitCell lc_v = mLimitList.Last();
if (lc_v.isVirtual == false)//不是虚拟信号,不用再判断了
return false;
if (lc_v.no == no)//有真实的信号,与虚拟信号一样
{
if (status != true)
return false;//异常, 不可能在 真实的信号中间插了个虚拟信号
//上一个信号是多余的
lc_v.isVirtual = false;
lc_v.dt_begin = dt;
lc_v.dt_end = DateTime.MinValue;
limitCellDtBeginChanged();
return true;
}
else
{
//更新旋转时间!!!!!!
if (mLimitList.Count() > 1)
{
DateTime dt2 = mLimitList[mLimitList.Count - 2].dt_end;
var renZiJiaPeriod = TimeSpan.FromTicks((dt - dt2 - LimitSignTime).Ticks / 2);//只有这个是正确的!!!
lc_v.dt_begin = dt2 + renZiJiaPeriod;
lc_v.dt_end = lc_v.dt_begin + LimitSignTime;
limitCellDtBeginChanged();
}
}
return false;
}
/// <summary>
/// 处理短信号
/// </summary>
/// <param name="s"></param>
void update_limit_short(SignData s)
{
DateTime dt = s.Time;//信号发生的时间
int no = s.No;//信号序号
bool status = s.On;//信号的状态 true开始, false结束
//把信号加入到列表
if (status == true)//添加,写入开始时间
{
if (mLimitList.Count > 0)
{
LimitCell lc = mLimitList.Last();
if (lc.no == no)//连续撞了两次相同限位
{
if (lc.isDouble)
{
lc.dt_end = dt;//信号结束了
limitCellDtEndChanged();
}
else
{
if (lc.dt_end == DateTime.MinValue)//上一个限位还没离开
{
//异常,AD卡出错了,或者断开连接了,清空!!!
mLimitList.Clear();
LimitList_Add(dt, no);
}
else
{
//判断两次的时间差
if ((dt - lc.dt_end) < TimeSpan.FromSeconds(30))
{
//这是同一个转向信号
lc.isDouble = true;
if (lc.no == 0)
IsSign0Double = lc.isDouble;
else
IsSign1Double = lc.isDouble;
lc.dt_mid = lc.dt_end;
lc.dt_end = dt;
limitCellDtEndChanged();
}
else
{
//时间差很大, 异常,旋转架重新转启
//异常,AD卡出错了,或者断开连接了,清空!!!
mLimitList.Clear();
LimitList_Add(dt, no);
}
}
}
}
else
{
//一个新的 信号
if (lc.dt_end == DateTime.MinValue)//上一个转向还没离开
{
if (lc.isDouble)
{
if (lc.dt_mid != DateTime.MinValue)
{
//没事, 只是从需要撞两次,变成只撞一次就可以了
lc.isDouble = false;
if (lc.no == 0)
IsSign0Double = lc.isDouble;
else
IsSign1Double = lc.isDouble;
lc.dt_end = lc.dt_mid;
lc.dt_mid = DateTime.MinValue;
limitCellDtEndChanged();
}
else
{
//异常 输入口没有结束
//AD卡出错了,或者断开连接了,清空!!!
mLimitList.Clear();
LimitList_Add(dt, no);
}
}
else
{
//异常 输入口没有结束
//AD卡出错了,或者断开连接了,清空!!!
mLimitList.Clear();
LimitList_Add(dt, no);
}
}
else
{
//一个新的信号
LimitList_Add(dt, no);
}
}
}
else
{
//一个新的信号
LimitList_Add(dt, no);
}
}
else //写入 结束时间
{
if (mLimitList.Count > 0)
{
LimitCell lc = mLimitList.Last();
if (lc.no == no)
{
if (lc.isDouble)
{
//需要撞2次
if (lc.dt_mid == DateTime.MinValue)
{
lc.dt_mid = dt;//完成撞第1次
}
else if (lc.dt_end != DateTime.MinValue)
{
//完成撞第2次
}
else
{
//异常, 撞第2次 还没开始,就结束了
//AD卡出错了,或者断开连接了,清空!!!
mLimitList.Clear();
}
}
else
{
if (lc.dt_end == DateTime.MinValue)
{
lc.dt_end = dt;//完成一次信号
limitCellDtEndChanged();
}
else
{
//异常,连续2次结束
mLimitList.Clear();
}
}
}
else
{
//异常,还没开始,就结束了
mLimitList.Clear();
}
}
else
{
//异常,还没开始,就结束了
}
}
}
/// <summary>
/// 处理长信号, 只关心 status=true
/// </summary>
/// <param name="s"></param>
void update_limit_long(SignData s)
{
DateTime dt = s.Time;
int no = s.No;
bool status = s.On;
//把信号加入到列表
if (status == true)//添加,写入开始时间
{
if (mLimitList.Count > 0)
{
LimitCell lc = mLimitList.Last();
if (lc.no == no)//连续撞了两次相同限位, 可能重新在转
{
mLimitList.Clear();
}
}
LimitList_Add(dt, dt + LimitSignTime, no);
}
}
void LimitList_Add(DateTime dt, DateTime dt_end, int no)
{
var cell = new LimitCell()
{
no = no,
dt_begin = dt,
dt_end = dt_end
};
limitCellDtBeginChanged();
limitCellDtEndChanged();
mLimitList.RAdd(cell);
AddEvent?.Invoke(cell);
}
void LimitList_Add(DateTime dt, int no)
{
var cell = new LimitCell()
{
dt_begin = dt,
no = no,
isDouble = (no == 0) ? IsSign0Double : IsSign1Double
};
limitCellDtBeginChanged();
mLimitList.RAdd(cell);
AddEvent?.Invoke(cell);
}
}
}