MainMachine.cs
11.2 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
using OnlineStore;
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;
using System.Windows.Forms;
namespace DeviceLibrary
{
public partial class MainMachine : DeviceBase, IRobot
{
public string Name { get; set; } = "MIMO_PLUS";
private bool _canRunning = true;
public bool canRunning
{
get { return _canRunning; }
set
{
if (_canRunning != value) {
Msg.setlogones();
}
_canRunning = value;
}
}
public bool isBusy { get; set; } = false;
public bool isAlarm { get; set; } = false;
public RunStatus runStatus { get; set; } = RunStatus.Stop;
public Robot_Config Config { get; set; }
public Dictionary<string, DeviceGroup> DeviceGroup { get=>RobotManage.DeviceGroup; }
public bool UserPause { get; set; } = false;
public MoveInfo ResetMoveInfo;
public delegate void ProcessMsg(List<Msg> msg);
public event ProcessMsg ProcessMsgEvent;
ServerCommunication ServerCM = new ServerCommunication();
/// <summary>
/// 开始运行的时间
/// </summary>
public DateTime StartTime { get; set; }
/// <summary>
/// 是否在急停中
/// </summary>
public bool isInSuddenDown = false;
public MainMachine(out string msg) {
msg = "";
Config = RobotManage.Config;
crc.LanguageChangeEvent += Crc_LanguageChangeEvent;
SideMove.Init(Config, DeviceGroup, out string m);
msg += m;
TransplantMove.Init(Config, DeviceGroup, out m);
msg += m;
TrayStop.Init(Config, DeviceGroup, out m);
msg += m;
#region 初始化led灯
RunningLed = new Led(Config.DOList["root"][IO_Type.Run_Led].GetIOAddr(), LedColor.green);
StandbyLed = new Led(Config.DOList["root"][IO_Type.Standby_Led].GetIOAddr(), LedColor.yellow);
AlarmLed = new Led(Config.DOList["root"][IO_Type.Alarm_Led].GetIOAddr(), LedColor.red);
#endregion
AlarmBuzzer.SetOnOffAction(() =>{ IOMove(IO_Type.Alarm_Buzzer, IO_VALUE.HIGH); }, () => { IOMove(IO_Type.Alarm_Buzzer, IO_VALUE.LOW); });
IOMonitor.RegisterIO(IO_Type.Reset_BTN, "root", IO_VALUE.HIGH, Reset_BTN, 2500,100);
IOMonitor.RegisterIO(IO_Type.AutoRun_Single, "root", IO_VALUE.HIGH, Run_BTN, 2500,100);
LedProcessInit();
}
private void Crc_LanguageChangeEvent(object sender, EventArgs e)
{
ResetMoveInfo.Name = crc.GetString("Res0173","重置");
}
public bool hasAlarm = false;
/// <summary>
/// 整机启动变量,设置为false后将退出线程,只在停止时调用
/// </summary>
bool mstart=true;
public void Run() {
mstart = true;
while (mstart) {
try
{
canRunning = DeviceCheck();
if (canRunning)
{
BtnProcess();
canRunning = SafeCheck();
}
Thread.Sleep(200);
if (!canRunning || !mstart)
continue;
if (runStatus == RunStatus.Running)
{
ioMonitor();
}
else if (runStatus == RunStatus.HomeReset)
{
HomeReset();
}
}
catch (Exception ex)
{
Msg.add(ex.ToString(), MsgLevel.warning);
Msg.setlogones();
}
finally {
var m = Msg.get()[Msg.Device];
ProcessMsgEvent?.Invoke(m);
ServerCM.ProcessMsg(m);
StoreStatus currnetstoreStatus= StoreStatus.None;
if (m.Find((aa) => aa.msgLevel == MsgLevel.alarm) == null)
{
hasAlarm = false;
AlarmBuzzer.OFF();
if (ServerCM.storeStatus != StoreStatus.InStoreExecute
&& ServerCM.storeStatus != StoreStatus.OutStoreExecute
&& ServerCM.storeStatus != StoreStatus.ResetMove && ServerCM.storeStatus != StoreStatus.StoreOnline)
{
LogUtil.info($"test storeStatus is {ServerCM.storeStatus} change to StoreOnline");
currnetstoreStatus = StoreStatus.StoreOnline;
}
}
else {
hasAlarm = true;
AlarmBuzzer.ON();
currnetstoreStatus = isInSuddenDown ? StoreStatus.SuddenStop : StoreStatus.Warning;
}
//ProcessMoveinfoEvent?.Invoke(MoveInfo.List);
if (!UserPause)
Msg.clear();
else
currnetstoreStatus = StoreStatus.Debugging;
if (currnetstoreStatus!=StoreStatus.None)
ServerCM.storeStatus = currnetstoreStatus;
}
}
LogUtil.info("主线程已退出.");
}
public void Start() {
ServerCM.StartConnectServer();
Run();
}
public void Stop() {
mstart = false;
ServerCM.StopConnectServer();
Thread.Sleep(300);
Alarm(AlarmType.None);
StopMove(true);
LedProcess(null);
}
public void BeginHomeReset(bool firstRun=false) {
if (!firstRun)
{
StopMove();
Thread.Sleep(500);
}
OpenAllServo();
Alarm(AlarmType.None);
runStatus = RunStatus.HomeReset;
ResetMoveInfo.NewMove(MoveStep.H01_HomeReset);
ResetMoveInfo.log("开始回原");
ResetMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
}
//强制回原
bool forceHome=true;
void HomeReset()
{
if (CheckWait(ResetMoveInfo))
return;
switch (ResetMoveInfo.MoveStep)
{
case MoveStep.H01_HomeReset:
ServerCM.storeStatus = StoreStatus.ResetMove;
break;
case MoveStep.H02_HomeReset:
ResetMoveInfo.NextMoveStep(MoveStep.H03_HomeReset);
ResetMoveInfo.log("夹爪升降上升");
ResetMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(500));
break;
case MoveStep.H03_HomeReset:
ResetMoveInfo.NextMoveStep(MoveStep.H04_HomeReset);
break;
case MoveStep.HEND_HomeReset:
forceHome = false;
ResetMoveInfo.log("回原完成");
ResetMoveInfo.EndMove();
runStatus = RunStatus.Running;
ServerCM.storeStatus = StoreStatus.StoreOnline;
break;
}
}
bool _IgnoreSafecheck = false;
public bool IgnoreSafecheck { get => _IgnoreSafecheck; set {
_IgnoreSafecheck = value;
} }
public bool IgnoreGratingSignal = false;
bool lastSafeCheckStatus = true;
bool SafeCheck() {
bool ok = true;
var ignorestring = "[" + crc.GetString("Res0174","已忽略") + "]";
if (!lastSafeCheckStatus && ok)
{
SafetyDevice.ResumeAll();
}
lastSafeCheckStatus = ok;
return ok;
}
void DeviceSuddenStop() {
if (lastSafeCheckStatus)
{
AxisBean.StopMultiAxis(AxisBean.List[Device]);
MoveInfo.List.ForEach((m) => { m.CanWhileCount = 5; });
SafetyDevice.PauseAll();
if (runStatus == RunStatus.HomeReset)
{
ResetMoveInfo.NewMove(MoveStep.H01_HomeReset);
}
}
}
/// <summary>
/// 最后一次气压检测变为0的时间
/// </summary>
DateTime lastAirCloseTime = DateTime.MinValue;
internal DateTime checkAlarmTime = DateTime.Now;
public bool DeviceCheck() {
bool ok = true;
isInSuddenDown = IOValue(IO_Type.SuddenStop_BTN).Equals(IO_VALUE.LOW);
if (UserPause)
{
Msg.add(crc.GetString("Res0175","系统暂停"), MsgLevel.warning);
DeviceSuddenStop();
lastSafeCheckStatus = false;
ok = false;
return ok;
}
else if (isInSuddenDown)
{
Alarm(AlarmType.SuddenStop);
Msg.add(crc.GetString("Res0176","急停中"), MsgLevel.alarm);
ok = false;
}
else if (alarmType != AlarmType.None) {
//if (IOValue(IO_Type.Right_BTN).Equals(IO_VALUE.HIGH) || IOValue(IO_Type.Left_BTN).Equals(IO_VALUE.HIGH))
//{
// Alarm(AlarmType.None);
//}
//else
{
Msg.add(crc.GetString("Res0177","系统需要重置"), MsgLevel.alarm,ErrInfo.SuddenStop);
ok = false;
}
}
if (IOValue(IO_Type.Airpressure_Check).Equals(IO_VALUE.LOW))
{
if (lastAirCloseTime == DateTime.MinValue)
lastAirCloseTime = DateTime.Now;
TimeSpan span = DateTime.Now - lastAirCloseTime;
if (span.TotalSeconds > RobotManage.Config.AirCheckSeconds)
{
ok = false;
Msg.add(crc.GetString("Res0178","气压不足"), MsgLevel.warning);
}
}
else {
lastAirCloseTime = DateTime.MinValue;
}
if (alarmType!=AlarmType.SuddenStop)
{
TimeSpan span = DateTime.Now - checkAlarmTime;
if (span.TotalSeconds > 2)
{
checkAlarmTime = DateTime.Now;
foreach (ConfigMoveAxis configMoveAxis in Config.moveAxisList)
{
if (AxisManager.GetAlarmStatus(configMoveAxis.DeviceName, configMoveAxis.GetAxisValue()) == 1)
{
Msg.add(crc.GetString(configMoveAxis.ProName, configMoveAxis.Explain) + $"[{configMoveAxis.GetAxisValue()}]:" + crc.GetString("Res0179","运动报警"), MsgLevel.alarm, ErrInfo.SuddenStop);
ok = false;
}
}
}
}
return ok;
}
}
}