终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

272 lines
13 KiB

  1. using BPASmartClient.Helper;
  2. using BPASmartClient.JXJFoodSmallStation.Model.HK_PLC;
  3. using BPASmartClient.JXJFoodSmallStation.Model.Siemens;
  4. using BPASmartClient.Modbus;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Configuration;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace BPASmartClient.JXJFoodSmallStation.Model
  15. {
  16. public class ProcessControl
  17. {
  18. private volatile static ProcessControl _Instance;
  19. public static ProcessControl GetInstance => _Instance ?? (_Instance = new ProcessControl());
  20. private ProcessControl() { }
  21. public ObservableCollection<RemoteRecipeRawMaterial> RawMaterials { get; set; } = new ObservableCollection<RemoteRecipeRawMaterial>();
  22. public SiemensDeviceStatus SiemensDevice = new SiemensDeviceStatus();
  23. public HKDeviceStatus HKDevice = new HKDeviceStatus();
  24. GVL_SmallStation SmallStation = new GVL_SmallStation();
  25. /// <summary>
  26. /// 接收原料数据
  27. /// </summary>
  28. public RecipeRawMaterial RawMaterial;
  29. public void Init()
  30. {
  31. ActionManage.GetInstance.Register(new Action(() =>
  32. {
  33. if (SiemensDevice.IsConnected)
  34. {
  35. var res = SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.RecipeName);
  36. var res1 = SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.RecipeID);
  37. int res2 = SiemensDevice.Siemens_PLC_S7.ReadClass(RawMaterial, 0, 0);
  38. if ((res != null && res is string recipeName) &&
  39. (res1 != null && res1 is uint recipeID) &&
  40. (res2 > 0))
  41. {
  42. RawMaterials.Clear();
  43. for (int i = 0; i < 15; i++)
  44. {
  45. RawMaterials.Add(new RemoteRecipeRawMaterial()
  46. {
  47. RawMaterialBarrelNum = RawMaterial.RawMaterialBarrelNum[i],
  48. RawMaterialLocation = RawMaterial.RawMaterialLocation[i],
  49. RawMaterialWeight = RawMaterial.RawMaterialWeight[i]
  50. });
  51. }
  52. Json<RemoteRecipeDataColl>.Data.Recipes.Add(new RemoteRecipeData()
  53. {
  54. RecipeName = recipeName,
  55. RecipeCode = recipeID,
  56. RawMaterial = RawMaterials
  57. });
  58. }
  59. }
  60. }), "西门子下发配方", true);
  61. ActionManage.GetInstance.Register(new Action(() =>
  62. {
  63. if (SiemensDevice.IsConnected)
  64. {
  65. var res = SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.TrayLocationNum);
  66. var res1 = SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.RecipeID);
  67. if (res != null && res is int TrayLocation && res1 != null && res1 is int recipeId)
  68. {
  69. int index = Array.FindIndex(Json<RemoteRecipeDataColl>.Data.Recipes.ToArray(), p => p.RecipeCode == recipeId);
  70. if (index >= 0 && index < Json<RemoteRecipeDataColl>.Data.Recipes.Count)
  71. {
  72. Json<RemoteRecipeDataColl>.Data.Recipes.ElementAt(index).TrayCode = TrayLocation;
  73. }
  74. }
  75. }
  76. }), "AGV到位信号", true);//根据下发的配方ID将 托盘的位置信息添加到配方中
  77. string HK_PLC_IP = ConfigurationManager.AppSettings["HKPlc_IP"];
  78. string Siemens_PLC_IP = ConfigurationManager.AppSettings["Siemens_IP"];
  79. try
  80. {
  81. //HKDevice.HK_PLC_S7.Connect(S7.Net.CpuType.S7200Smart, HK_PLC_IP);
  82. //SiemensDevice.Siemens_PLC_S7.Connect(S7.Net.CpuType.S71500, Siemens_PLC_IP);
  83. if (HKDevice.IsConnected)
  84. {
  85. HKDevice.Init();
  86. }
  87. if (SiemensDevice.IsConnected)
  88. {
  89. SiemensDevice.Init();
  90. }
  91. }
  92. catch(Exception ex)
  93. {
  94. }
  95. SmallStation.RecipeQueue.Clear();
  96. //Json<RemoteRecipeDataColl>.Data.Recipes = TestData.GetInstance.Recipes;//添加测试数据
  97. ThreadManage.GetInstance().StartLong(new Action(() =>
  98. {
  99. ReceviceData();
  100. RecipeInfoToHKPLC();
  101. Thread.Sleep(10);
  102. }), "流程处理", true);
  103. ThreadManage.GetInstance().StartLong(new Action(() =>
  104. {
  105. if (SiemensDevice.IsConnected && HKDevice.IsConnected)
  106. {
  107. AgvGetInDelivery();
  108. AgvGetInPickUp();
  109. }
  110. Thread.Sleep(10);
  111. }), "AGV进站送取货", true);
  112. }
  113. /// <summary>
  114. /// AGV进站送货
  115. /// </summary>
  116. private void AgvGetInDelivery()
  117. {
  118. switch (SmallStation.AgvDeliveryPosition)
  119. {
  120. case 0:
  121. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.DeliveryAGVApply) is bool))
  122. {
  123. SmallStation.AgvDeliveryPosition = 1;
  124. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.DeliveryAGVApply, true);
  125. }
  126. break;
  127. case 1:
  128. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.DeliveryAGVIsApply) is bool))
  129. {
  130. SmallStation.AgvDeliveryPosition = 2;
  131. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.DeliveryAGVIsApply, true);
  132. }
  133. break;
  134. case 2:
  135. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.DeliveryAGVApplyJack) is bool))
  136. {
  137. SmallStation.AgvDeliveryPosition = 3;
  138. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.DeliveryAGVApplyJack, true);
  139. }
  140. break;
  141. case 3:
  142. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.DeliveryAGVIsApplyJack) is bool))
  143. {
  144. SmallStation.AgvDeliveryPosition = 4;
  145. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.DeliveryAGVIsApplyJack, true);
  146. }
  147. break;
  148. case 4:
  149. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.DeliveryAGVFinsih) is bool))
  150. {
  151. SmallStation.AgvDeliveryPosition = 5;
  152. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.DeliveryAGVFinsih, true);
  153. }
  154. break;
  155. case 5:
  156. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.StationHaveCargo) is bool))
  157. {
  158. SmallStation.AgvDeliveryPosition = 0;
  159. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.StationHaveCargo, true);
  160. }
  161. break;
  162. default:
  163. break;
  164. }
  165. //获取工位上是否有小车
  166. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.StationIsExistCar, (bool)
  167. HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.StationIsExistCar));
  168. }
  169. /// <summary>
  170. /// AGV进站取货
  171. /// </summary>
  172. private void AgvGetInPickUp()
  173. {
  174. switch (SmallStation.AgvPickUpPosition)
  175. {
  176. case 0:
  177. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.PickAGVApply) is bool))
  178. {
  179. SmallStation.AgvPickUpPosition = 1;
  180. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.PickAGVApply, true);
  181. }
  182. break;
  183. case 1:
  184. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.PickAGVIsApply) is bool))
  185. {
  186. SmallStation.AgvPickUpPosition = 2;
  187. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.PickAGVIsApply, true);
  188. }
  189. break;
  190. case 2:
  191. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.PickCargoAGVFinish) is bool))
  192. {
  193. SmallStation.AgvPickUpPosition = 3;
  194. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.PickCargoAGVFinish, true);
  195. }
  196. break;
  197. case 3:
  198. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.PickAGVFinish) is bool))
  199. {
  200. SmallStation.AgvPickUpPosition = 0;
  201. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.PickAGVFinish, true);
  202. }
  203. break;
  204. default:
  205. break;
  206. }
  207. }
  208. private void ReceviceData()
  209. {
  210. SmallStation.RemoteRecipes = Json<RemoteRecipeDataColl>.Data.Recipes;
  211. if (Json<RemoteRecipeDataColl>.Data.Recipes.Count > 0)
  212. {
  213. foreach (var data in Json<RemoteRecipeDataColl>.Data.Recipes)
  214. {
  215. if(!(SmallStation.RecipeQueue.Contains(data.RecipeCode)))
  216. SmallStation.RecipeQueue.Enqueue(data.RecipeCode);
  217. }
  218. }
  219. }
  220. private void RecipeInfoToHKPLC()
  221. {
  222. if (SmallStation.RecipeQueue.Count > 0)
  223. {
  224. int index = Array.FindIndex(SmallStation.RemoteRecipes.ToArray(), p => p.RecipeCode == SmallStation.RecipeQueue.ElementAt(0));
  225. if (index >= 0 && index <= SmallStation.RemoteRecipes.Count)
  226. {
  227. long code = SmallStation.RemoteRecipes.ElementAt(index).RecipeCode;
  228. if (RTrig.GetInstance("IsAllowDosing").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.IsAllowDosing) is bool a))
  229. {
  230. foreach (var item in SmallStation.RemoteRecipes.ElementAt(index).RawMaterial)
  231. {
  232. HKDevice.StockBinPar((uint)item.RawMaterialBarrelNum, (ushort)item.RawMaterialLocation);
  233. }
  234. HKDevice.AllowDosingSignReset();
  235. SmallStation.IssueRecipeFinish = true;
  236. }
  237. if (RTrig.GetInstance("StockState").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.StockState) is bool) && SmallStation.IssueRecipeFinish)
  238. {
  239. var res = HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.StockBinLocation);
  240. if (res != null && res is int loc)
  241. {
  242. int loc_index = Array.FindIndex(SmallStation.RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
  243. double weight = SmallStation.RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
  244. if (loc_index >= 1 && loc_index <= 15)
  245. {
  246. //DeviceInquire.GetInstance.GetDeviceObject(loc_index)?.Start((uint)weight);//启动并写入每个原料重量
  247. SmallStation.StockInIsWork = loc_index;
  248. HKDevice.StockStateSignReset();
  249. }
  250. }
  251. }
  252. /*if (RTrig.GetInstance("OutRawMaterialFinish").Start(DeviceInquire.GetInstance.GetDeviceObject(SmallStation.StockInIsWork).modbusTcp.Read(DeviceAddress.OutRawMaterialFinish) is bool))
  253. {
  254. HKDevice.SingleDosing((uint)SmallStation.StockInIsWork);
  255. DeviceInquire.GetInstance.GetDeviceObject(SmallStation.StockInIsWork).OutRawMaterialReset();
  256. }
  257. if (RTrig.GetInstance("StockState").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.RecipeDosingFinish) is bool))
  258. {
  259. HKDevice.RecipeDosingFinishReset();
  260. SmallStation.RecipeQueue.TryDequeue(out code);
  261. Json<RemoteRecipeDataColl>.Data.Recipes.RemoveAt(index);//制作完成,移除当前配方
  262. }*/
  263. }
  264. }
  265. }
  266. }
  267. }