RobotManage.cs
7.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
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
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using Robot.UR;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DeviceLibrary
{
public static class RobotManage
{
public static MainMachine mainMachine;
public static Robot_Config Config;
public static Dictionary<string,DeviceGroup> DeviceGroup;
public static bool IsLoadOk = true;
public static bool IsConfigMode = false;
public delegate void LoadFinish(bool state,string msg);
public static event LoadFinish LoadFinishEvent;
public static event EventHandler<bool> UserPauseSet;
public static bool isRunning = false;
static string baseDir = Application.StartupPath;
static Thread mainThread;
internal static bool InoutDebugMode;
public static Dictionary<string, RobotPosition> MI1Postion;
public static Dictionary<string, RobotPosition> MI2Postion;
public static Dictionary<string, RobotPosition> CIPostion;
public static URRobotControl Robot_MI1;
public static URRobotControl Robot_MI2;
public static URRobotControl Robot_CI;
public static VStoreCollection VStoreCollection;
public static ShareFolderWatcher folderWatcher;
public static void Init() {
string msg = "";
try
{
mainMachine = null;
string configFile = "config\\Config.csv";
Config = new Robot_Config(0, "", configFile);
Config = (Robot_Config)CSVConfigReader.LoadConfig(Config);
string DeviceGroupFile = "config\\DeviceGroup.csv";
DeviceGroup = CSVPositionReader<DeviceGroup>.AddCSVFile(DeviceGroupFile);
if (File.Exists(configFile))
{
LogUtil.info("加载位置文件:" + configFile);
}
else
{
IsLoadOk = false;
msg += crc.GetString("Res0180", "找不到库位配置文件") + "\n";
}
//folderWatcher = new ShareFolderWatcher(Setting_Init.Device_VJCounterFolder);
//if (!folderWatcher.Start(out string errmsg))
//{
// IsLoadOk = false;
// msg += errmsg + "\n";
// LogUtil.error("ShareFolderWatcher:"+errmsg);
//}
string MI1PostionFile = "config\\MI1Postion.csv";
string MI2PostionFile = "config\\MI2Postion.csv";
string CIPostionFile = "config\\CIPostion.csv";
MI1Postion = CSVPositionReader<RobotPosition>.LoadCSVFile(MI1PostionFile);
MI2Postion = CSVPositionReader<RobotPosition>.LoadCSVFile(MI2PostionFile);
CIPostion = CSVPositionReader<RobotPosition>.LoadCSVFile(CIPostionFile);
Robot_MI1 = new URRobotControl(Setting_Init.URRobot_MI1_IP, "Robot-R1", Setting_Init.URRobot_MI1_ListenPort);
Robot_MI1.Name = "R1";
Robot_MI1.SetSpeedRate(Setting_Init.URRobot_MI1_Speed_Rate);
Robot_MI2 = new URRobotControl(Setting_Init.URRobot_MI2_IP, "Robot-R2", Setting_Init.URRobot_MI2_ListenPort);
Robot_MI2.Name = "R2";
Robot_MI2.SetSpeedRate(Setting_Init.URRobot_MI2_Speed_Rate);
Robot_CI = new URRobotControl(Setting_Init.URRobot_CI_IP, "Robot-CI", Setting_Init.URRobot_CI_ListenPort);
Robot_CI.SetSpeedRate(Setting_Init.URRobot_CI_Speed_Rate);
VStoreCollection = new VStoreCollection();
CodeManager.LoadConfig();
RemoteService.Init();
mainMachine = new MainMachine(out string m);
if (!string.IsNullOrWhiteSpace(m))
{
IsLoadOk = false;
msg += m;
}
//Thread.Sleep(5000);
if (!IOManager.ConnectionIOList(new List<string>()))
{
IsLoadOk = false;
msg += crc.GetString("Res0181", "IO板卡初始化失败") + "\n";
}
if (!VJCounter.Connect(Setting_Init.VJCounter_COMPORT, Setting_Init.VJCounter_BaudRate, out string msgs)) {
IsLoadOk = false;
msg += crc.GetString("Res0111.43f91cee","VJ点料机连接失败") + "\n";
LogUtil.error(msg + "," + msgs);
}
DeviceRunControl.AddDevice("SideMove", SideMove.DeviceList.Values.ToList<IDevice>());
DeviceRunControl.AddDevice("TransplantMove", TransplantMove.DeviceList.Values.ToList<IDevice>());
//DeviceRunControl.AddDevice("TrayStop", TrayStop.DeviceList.Values.ToList<IDevice>());
DeviceRunControl.AddDevice("MI", MI.DeviceList.Values.ToList<IDevice>());
DeviceRunControl.AddDevice("CI", CI.DeviceList.Values.ToList<IDevice>());
TrayStop.DeviceList.Values.ToList<IDevice>().ForEach(device => { DeviceRunControl.AddDevice(device.GroupName, new List<IDevice>() { device }); });
if (G.simulate)
IsLoadOk = true;
}
catch (Exception ex)
{
LogUtil.error(ex.ToString());
MessageBox.Show(ex.ToString());
LoadFinishEvent?.Invoke(false, ex.Message);
return;
}
LoadFinishEvent?.Invoke(IsConfigMode ? IsConfigMode : IsLoadOk, msg);
}
public static void LoadDebug() {
LoadFinishEvent?.Invoke(true, crc.GetString("Res0182","打开调试模式"));
}
public static void Start() {
//Init();
if (!IsLoadOk)
{
LogUtil.info("系统还未加载完毕,无法启动");
if (!IsConfigMode)
return;
}
LogUtil.info("开始启动设备线程");
mainThread = new Thread(new ThreadStart(mainMachine.Start));
mainThread.Start();
VStoreCollection.Start();
Thread.Sleep(50);
isRunning = true;
GC.KeepAlive(mainThread);
mainMachine.BeginHomeReset(true);
DeviceRunControl.StartAll();
}
public static void Stop()
{
LogUtil.info("开始停止系统.");
if (mainMachine != null)
{
mainMachine.Stop();
mainMachine.UserPause = false;
}
VStoreCollection.Stop();
isRunning = false;
}
public static void ShutDown()
{
LogUtil.info("开始关闭系统.");
IOManager.CloseAllConnection();
VStoreCollection.ShutDown();
Robot_MI1.StopRobot();
Robot_MI2.StopRobot();
Robot_CI.StopRobot();
}
public static void UserPause(bool userpause)
{
UserPause("", userpause);
}
public static void UserPause(string msg="",bool userpause=true) {
UserPauseSet?.Invoke(null, userpause);
mainMachine.UserPause = userpause;
if (userpause)
{
if(string.IsNullOrEmpty(msg))
LogUtil.info("用户暂停");
else
LogUtil.info("系统暂停: "+msg);
mainMachine.DeviceSnapshot();
Thread.Sleep(500);
}
else
LogUtil.info("用户取消暂停:"+ msg);
}
}
public enum StoreType {
TypeA,TypeB
}
}