AC_SA_BoxBean_ADIO.cs
2.7 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
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
partial class AC_SA_BoxBean
{
public double GetADIO(int index) {
switch (index)
{
case 1:
if (Ultrasonic1!= null)
return Ultrasonic1.Distance;
else
if (StoreManager.Config.AIDI1_Addr>=0)
return IOManager.GetADIOValue(StoreManager.Config.AIDI1_Addr);
break;
case 2:
if (StoreManager.Config.AIDI2_Addr >= 0)
return IOManager.GetADIOValue(StoreManager.Config.AIDI2_Addr);
break;
case 3:
if (StoreManager.Config.AIDI3_Addr >= 0)
return IOManager.GetADIOValue(StoreManager.Config.AIDI3_Addr);
break;
case 4:
if (StoreManager.Config.AIDI4_Addr >= 0)
return IOManager.GetADIOValue(StoreManager.Config.AIDI4_Addr);
break;
default:
return -1;
}
return -1;
}
DebounceFilter TrayCheck_DoorFilter = new DebounceFilter(10);
IO_VALUE TrayCheck_Door
{
get
{
if (Ultrasonic1 != null)
{
var xx = Math.Abs(Ultrasonic1.Distance - Config.AIDI1_DefaultPosition);
var vv = xx >= 5 ? IO_VALUE.HIGH : IO_VALUE.LOW;
var Filter = TrayCheck_DoorFilter.Filter(vv);
LogUtil.info("Ultrasonic1.Distance:" + Ultrasonic1.Distance + "," + xx + "," + vv + ",Filter=" + Filter);
return Filter;
}
else
return IOManager.IOValue(IO_Type.TrayCheck_Door);
}
}
}
public class DebounceFilter
{
private IO_VALUE _lastValue;
private int _counter;
private readonly int _threshold;
public DebounceFilter(int threshold)
{
_threshold = threshold;
}
public IO_VALUE Filter(IO_VALUE value)
{
if (value == _lastValue)
{
_counter = 0;
return value;
}
_counter++;
if (_counter >= _threshold)
{
_lastValue = value;
_counter = 0;
return value;
}
return _lastValue;
}
}
}