ElectricGripper.cs
5.1 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
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DeviceLibrary
{
public class ElectricGripper
{
private Neotel.Rmaxis axis;
private string Port = "";
public Enum GripperType = GripperTypeE.None;
int clampTimes = 0;
public ElectricGripper(string port) {
Port = port;
}
public bool OpenPort()
{
if(axis==null)
axis = new Neotel.Rmaxis();
if (axis.IsPortOpen)
return true;
bool rtn = axis.OpenPort(Port);
//var s = "版本号\r\n" + axis.GetVersion();
return rtn;
}
public void ClosePort()
{
axis.ClosePort();
}
public bool Clamp(MoveInfo moveInfo = null)
{
OpenPort();
GripperType = GripperTypeE.Gripper;
if (moveInfo != null)
moveInfo.WaitList.Add(WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitAction),"夹爪夹紧"));
LogUtil.info($"ElectricGripper Clamp");
if (axis.ErrorCode > 0)
{
LogUtil.info("ElectricGripper Clamp error:" + axis.ErrorCode.ToString() + axis.ErrorString);
axis.ResetError();
}
if (IsBusy)
{
axis.StopAxis();
Thread.Sleep(500);
}
if (!IsBusy)
{
axis.Push(50, 5.5f, 5);
clampTimes++;
if (moveInfo != null)
moveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));
return true;
}
else {
return false;
}
}
public bool Release(MoveInfo moveInfo = null)
{
OpenPort();
GripperType = GripperTypeE.Release;
LogUtil.info($"ElectricGripper Release");
if (axis.ErrorCode > 0)
{
LogUtil.info("ElectricGripper Release error:" + axis.ErrorCode.ToString() + axis.ErrorString);
axis.ResetError();
}
if (IsBusy)
{
axis.StopAxis();
Thread.Sleep(500);
}
if (!IsBusy)
{
axis.MoveAbsolute(0.2f, 5, 15, 15, 1f);
clampTimes = 0;
if (moveInfo != null)
moveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));
return true;
}
else
{
if (axis.ErrorCode > 0)
{
LogUtil.info($"ElectricGripper ErrCode:{axis.ErrorCode}");
axis.ResetError();
Thread.Sleep(100);
}
if (moveInfo != null)
moveInfo.WaitList.Add(WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitAction), "夹爪放松"));
return false;
}
}
public void HomeReset(MoveInfo moveInfo = null)
{
OpenPort();
GripperType = GripperTypeE.Reset;
//if (!IsBusy)
//{
axis.ResetError();
axis.GoHome();
clampTimes = 0;
// }
//if (moveInfo != null)
// moveInfo.WaitList.Add(WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitAction)));
}
public bool IsBusy
{
get
{
LogUtil.info($"ElectricGripper IsReached:{axis.IsReached},IsMoving:{axis.IsMoving}");
//return false;
return !axis.IsReached;
}
}
public int ErrorCode
{
get
{
return axis.ErrorCode;
}
}
public bool IsClamp
{
get
{
LogUtil.OutputDebugString($"ElectricGripper IsPushEmpty:{axis.IsPushEmpty}");
return !axis.IsPushEmpty;
}
}
bool WaitAction(WaitResultInfo w)
{
if (this.IsBusy)
return false;
if (this.GripperType.Equals(ElectricGripper.GripperTypeE.Gripper))
{
if (this.IsClamp)
return true;
else
{
if (clampTimes<=2)
this.Clamp();
return false; ;
}
}
else if (this.GripperType.Equals(ElectricGripper.GripperTypeE.Release))
{
return this.Release();
}
else if (this.GripperType.Equals(ElectricGripper.GripperTypeE.Reset))
{
return this.IsBusy;
}
else
{
return true;
}
}
public enum GripperTypeE
{
Gripper,
Release,
Reset,
None
}
}
}