AtlasScrew_Left.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using EIP_Protocol;
  8. using MainForm;
  9. using MathNet.Numerics.RootFinding;
  10. using Newtonsoft.Json;
  11. namespace CommonLib
  12. {
  13. public class AtlasScrew_Left : BaseScrew
  14. {
  15. MTF6KConnStus connFlow = new MTF6KConnStus();
  16. public TightResult tightResult = new TightResult();
  17. string receiveMsg;
  18. //连接状态
  19. //public bool connectStaus = false;
  20. public Thread MTF6KThread;
  21. public AtlasScrew_Left(object ip, object port, int connectTimeOut,int sendDataTimeOut,string direction)
  22. {
  23. Ip = ip.ToString();
  24. Port = port.ToString();
  25. ConnectTimeOut = connectTimeOut;
  26. SendDataTimeOut = sendDataTimeOut;
  27. IsCarriage = true;
  28. IsShield = false;
  29. Direction = direction;
  30. }
  31. /// <summary>
  32. /// MTF6K连接流程结构体
  33. /// </summary>
  34. enum MTF6KConnStus
  35. {
  36. Idle,
  37. Disconnect,
  38. Connected,
  39. CheckConnect,
  40. SelectPSET,
  41. CheckSelectPSET,
  42. SelectBatch,
  43. CheckSelectBatch,
  44. SubscribeResult,
  45. CheckSubscribeResult1,
  46. CheckSubscribeResult2,
  47. CheckSubscribeResult3,
  48. SubscribeCurve,
  49. CheckSubscribeCurve,
  50. Ready,
  51. UnsubcribeResult,
  52. UnsubscribeCurve,
  53. };
  54. /// <summary>
  55. /// 拧紧结果
  56. /// </summary>
  57. public class TightResult
  58. {
  59. /// <summary>
  60. /// 扭矩数组
  61. /// </summary>
  62. public double[] torque;
  63. /// <summary>
  64. /// 角度数组
  65. /// </summary>
  66. public double[] angle;
  67. /// <summary>
  68. ///
  69. /// </summary>
  70. public double torCoef = 0.0;
  71. /// <summary>
  72. ///
  73. /// </summary>
  74. public double angCoef = 0.0;
  75. /// <summary>
  76. /// 峰值扭矩
  77. /// </summary>
  78. public double PearkTorque = 0.0;
  79. /// <summary>
  80. /// 总角度
  81. /// </summary>
  82. public double TotalAngle = 0.0;//总角度
  83. /// <summary>
  84. /// 总持续时间
  85. /// </summary>
  86. public double TotalDuration;//总持续时间
  87. /// <summary>
  88. /// 结果代码
  89. /// </summary>
  90. public int ResultCode = 0;//结果代码
  91. public TightResult()
  92. {
  93. }
  94. public void Reset()
  95. {
  96. torCoef = 0.0;
  97. angCoef = 0.0;
  98. ResultCode = 0;
  99. PearkTorque = 0.0;//峰值扭矩
  100. TotalAngle = 0.0;//总角度
  101. TotalDuration = 0.0;//总持续时间
  102. MTF6K.isDataRecvFinish = false;
  103. }
  104. }
  105. private Communicate TCP;
  106. public override void Initial()
  107. {
  108. base.Initial();
  109. try
  110. {
  111. // 如果线程已经存在,先关闭旧线程
  112. if (MTF6KThread != null && MTF6KThread.IsAlive)
  113. {
  114. MTF6KThread.Abort(); // 强制终止线程(注意:Abort 不推荐使用,建议改用信号机制)
  115. MTF6KThread = null;
  116. }
  117. // 如果 TCP 已经连接,先关闭连接
  118. if (TCP != null)
  119. {
  120. TCP.ConnectedResult -= new Communicate.ConnectResultEventHandler(Comm_ConnectedResult);
  121. TCP.MessageReceived -= new Communicate.MessageReceivedEventHandler(Comm_MessageReceived);
  122. TCP.closeSocket();
  123. TCP = null;
  124. }
  125. TCP = new Communicate("TCP1", Ip, Convert.ToInt32(Port));
  126. TCP.FormOpening = true;
  127. TCP.ConnectedResult += new Communicate.ConnectResultEventHandler(Comm_ConnectedResult);
  128. TCP.MessageReceived += new Communicate.MessageReceivedEventHandler(Comm_MessageReceived);
  129. MTF6KThread = new Thread(MTF6KFlow);
  130. MTF6KThread.IsBackground = true;
  131. MTF6KThread.Start();
  132. IsOk = true;
  133. }
  134. catch (Exception)
  135. {
  136. IsOk = IsShield ? true : false;
  137. }
  138. }
  139. /// <summary>
  140. /// MTF6K -TCP连接状态
  141. /// </summary>
  142. /// <param name="sender"></param>
  143. /// <param name="e"></param>
  144. public void Comm_ConnectedResult(object sender, Communicate.ResultEventArgs e)
  145. {
  146. if (e.conResult)
  147. {
  148. connFlow = MTF6KConnStus.Connected;
  149. connectStaus = true;
  150. }
  151. else
  152. {
  153. connectStaus = false;
  154. }
  155. }
  156. /// <summary>
  157. /// 接收数据-MTF6K
  158. /// </summary>
  159. /// <param name="sender"></param>
  160. /// <param name="e"></param>
  161. public void Comm_MessageReceived(object sender, Communicate.MessageEventArgs e)
  162. {
  163. if (sender is Communicate sender1)
  164. {
  165. ReceiveIP = sender1.IpAddress.ToString();
  166. }
  167. if (connFlow == MTF6KConnStus.Ready)
  168. {
  169. MTF6K.ParseKerwords(e.byteMes, false);
  170. //ReceiveIP = ((CommonLib.Communicate)sender).IpAddress.ToString();
  171. }
  172. else
  173. {
  174. MTF6K.ParseKerwords(e.byteMes, true);
  175. }
  176. }
  177. /// <summary>
  178. /// 连接MTF6K流程
  179. /// </summary>
  180. private void MTF6KFlow()
  181. {
  182. int screwIndex = 0;
  183. while (true)
  184. {
  185. Thread.Sleep(20);
  186. while (connectStaus)
  187. {
  188. Thread.Sleep(20);
  189. try
  190. {
  191. switch (connFlow)
  192. {
  193. case MTF6KConnStus.Idle:
  194. break;
  195. case MTF6KConnStus.Disconnect:
  196. {
  197. TCP.SendMessage(MTF6K.disconnect);
  198. connectStaus = false;
  199. TCP.FormOpening = false;
  200. TCP.ConnectedResult -= new Communicate.ConnectResultEventHandler(Comm_ConnectedResult);
  201. TCP.MessageReceived -= new Communicate.MessageReceivedEventHandler(Comm_MessageReceived);
  202. TCP.closeSocket();
  203. TCP = null;
  204. }
  205. break;
  206. case MTF6KConnStus.Connected:
  207. TCP.SendMessage(MTF6K.disconnect);
  208. Thread.Sleep(20);
  209. TCP.SendMessage(MTF6K.connect);
  210. connFlow = MTF6KConnStus.CheckConnect;
  211. break;
  212. case MTF6KConnStus.CheckConnect:
  213. if (MTF6K.MID == "0002")
  214. {
  215. TCP.SendMessage(MTF6K.keepAlive);
  216. connFlow = MTF6KConnStus.SelectPSET;
  217. }
  218. else
  219. { }
  220. break;
  221. case MTF6KConnStus.SelectPSET:
  222. TCP.SendMessage(MTF6K.switchPSET1);
  223. connFlow = MTF6KConnStus.CheckSelectPSET;
  224. break;
  225. case MTF6KConnStus.CheckSelectPSET:
  226. if (MTF6K.MID == "0005")
  227. {
  228. connFlow = MTF6KConnStus.SubscribeResult;
  229. }
  230. else if (MTF6K.MID == "0004")
  231. {
  232. connFlow = MTF6KConnStus.SubscribeResult;
  233. }
  234. break;
  235. case MTF6KConnStus.SubscribeResult:
  236. TCP.SendMessage(MTF6K.subscribeTightingResult);
  237. connFlow = MTF6KConnStus.CheckSubscribeResult1;
  238. break;
  239. case MTF6KConnStus.CheckSubscribeResult1:
  240. if (MTF6K.MID == "1202")
  241. {
  242. connFlow = MTF6KConnStus.SubscribeCurve;
  243. }
  244. else
  245. { }
  246. break;
  247. case MTF6KConnStus.SubscribeCurve:
  248. TCP.SendMessage(MTF6K.subscribeLastTightingCurve);
  249. connFlow = MTF6KConnStus.CheckSubscribeCurve;
  250. break;
  251. case MTF6KConnStus.CheckSubscribeCurve:
  252. if (MTF6K.MID == "0005")
  253. {
  254. connFlow = MTF6KConnStus.Ready;
  255. }
  256. else
  257. { }
  258. break;
  259. case MTF6KConnStus.Ready:
  260. {
  261. if (MTF6K.MID == "0901" && MTF6K.isDataRecvFinish == true)
  262. {
  263. MTF6K.MID = "error";
  264. MTF6K.TightingResult.Convert(out tightResult.ResultCode, out tightResult.PearkTorque, out tightResult.TotalAngle, out tightResult.TotalDuration);
  265. tightResult.torque = new double[MTF6K.CurveResult.SampleCount];
  266. tightResult.angle = new double[MTF6K.CurveResult.SampleCount];
  267. MTF6K.CurveResult.Convert(out tightResult.torque, out tightResult.torCoef, out tightResult.angle, out tightResult.angCoef);
  268. Form_Home home = new Form_Home();
  269. #region 保存数据
  270. string dic = GlobalContext.AtlasLogDir + "扭力曲线\\";
  271. if (!Directory.Exists(dic))
  272. {
  273. Directory.CreateDirectory(dic);
  274. }
  275. string 扭力曲线文件名 = dic + DateTime.Now.ToString("yyyy_MM_dd") +".csv" ;
  276. string strDirection = "";
  277. string strValue="";
  278. StringBuilder sbStr =new StringBuilder();
  279. if (!File.Exists(扭力曲线文件名))
  280. {
  281. // 记录时间,产品SN,螺丝序号
  282. //sbStr.AppendLine("序号,产品SN,扭力数组,角度数组,峰值扭矩,总角度,总持续时间,结果代码,工位");
  283. sbStr.Append("序号,产品SN,扭力数组,角度数组,峰值扭矩,总角度,总持续时间,结果代码,工位");
  284. File.WriteAllText(扭力曲线文件名, sbStr.ToString());
  285. }
  286. if (ReceiveIP == GlobalContext.AtlasAddressLeft)
  287. {
  288. //左工位
  289. Form_Home.screwIndex_left += 1;//螺丝序号,采集一次加一次,进站时先清0
  290. screwIndex = Form_Home.screwIndex_left;
  291. strDirection = "左工位";
  292. string torqueData = string.Join(",", tightResult.torque);
  293. string angleData = string.Join(",", tightResult.angle);
  294. string sn = Form_Home.barcode_left.strProductBarcode;
  295. strValue=$"{screwIndex},{sn},{torqueData},{angleData},{tightResult.PearkTorque},{tightResult.TotalAngle}" +
  296. $",{tightResult.TotalDuration},{tightResult.ResultCode},{strDirection}";
  297. //写入PLC
  298. double maxfTorque = tightResult.torque.Aggregate((currentMax, next) => currentMax > next ? currentMax : next);
  299. double maxfCircles = tightResult.angle.Aggregate((currentMax, next) => currentMax > next ? currentMax : next);
  300. OP70_ScrewDataSet_t resultToPlC = new OP70_ScrewDataSet_t
  301. {
  302. fTorque = (float)maxfTorque,
  303. fCircles = (float)maxfCircles
  304. };
  305. home.WriteResultToPlc(7, "", "g_OP70_MES.Left.screwDriver", 1, resultToPlC);
  306. }
  307. //else if (ReceiveIP == GlobalContext.AtlasAddressRight)
  308. //{
  309. // Form_Home.screwIndex_right += 1;//螺丝序号,采集一次加一次,进站时先清0
  310. // screwIndex = Form_Home.screwIndex_right;
  311. // strDirection = "右工位";
  312. // string torqueData = string.Join(",", tightResult.torque);
  313. // string angleData = string.Join(",", tightResult.angle);
  314. // string sn = Form_Home.barcode_left.strProductBarcode;
  315. // strValue = $"{screwIndex},{sn},{torqueData},{angleData},{tightResult.PearkTorque},{tightResult.TotalAngle}" +
  316. // $",{tightResult.TotalDuration},{tightResult.ResultCode},{strDirection}";
  317. // //写入PLC
  318. // double maxfTorque = tightResult.torque.Aggregate((currentMax, next) => currentMax > next ? currentMax : next);
  319. // double maxfCircles = tightResult.angle.Aggregate((currentMax, next) => currentMax > next ? currentMax : next);
  320. // OP70_ScrewDataSet_t resultToPlC = new OP70_ScrewDataSet_t
  321. // {
  322. // fTorque = (float)maxfTorque,
  323. // fCircles = (float)maxfCircles
  324. // };
  325. // home.WriteResultToPlc(7, "", "g_OP70_MES.Right.screwDriver", 1, resultToPlC);
  326. //}
  327. using(StreamWriter writer=new StreamWriter(扭力曲线文件名,true)){
  328. writer.WriteLine(strValue);
  329. }
  330. #endregion 保存数据
  331. //tightResult.PearkTorque = tightResult.PearkTorque / 98;
  332. #region 删除
  333. //double preDegree = 0;
  334. //double curDegree = 0;
  335. //bool Step3 = false;
  336. //double PearkTorque = 0.0;
  337. //double Pearkdegree = 0.0;
  338. //double mindegree = 0;
  339. //double minvalue = 0;
  340. //JD.Clear();
  341. //NL.Clear();
  342. //List<double> pearkTorqueList = new List<double>(); // 存储 PearkTorque
  343. //List<double> pearkDegreeList = new List<double>(); // 存储 Pearkdegree
  344. //for (int i = 0; i < tightResult.angle.Length; i++)
  345. //{
  346. // double degree = tightResult.angle[i] * tightResult.angCoef;
  347. // double torque = tightResult.torque[i] * tightResult.torCoef / 98;
  348. // preDegree = curDegree;
  349. // curDegree = degree;
  350. // if (i > tightResult.angle.Length - 5 && torque <= 0)
  351. // {
  352. // continue;
  353. // }
  354. // JD.Add(Math.Round(degree,0).ToString());
  355. // NL.Add(Math.Round(torque,2).ToString());
  356. // //反转
  357. // if (curDegree - preDegree <= 0 && degree < 0)
  358. // {
  359. // if (torque > PearkTorque)
  360. // {
  361. // PearkTorque = torque;
  362. // Pearkdegree = degree;
  363. // }
  364. // if (torque < mindegree)
  365. // {
  366. // mindegree = torque;
  367. // minvalue = degree;
  368. // }
  369. // }
  370. // else if (curDegree - preDegree > 0)//正转
  371. // {
  372. // if (degree < 360 && !Step3)//切换扭矩值
  373. // {
  374. // if (torque > PearkTorque)
  375. // {
  376. // PearkTorque = torque;
  377. // Pearkdegree = degree;
  378. // }
  379. // }
  380. // else
  381. // {
  382. // Step3 = true;
  383. // if (torque > PearkTorque)
  384. // {
  385. // PearkTorque = torque;
  386. // Pearkdegree = degree;
  387. // }
  388. // }
  389. // }
  390. // pearkTorqueList.Add(Math.Round(Pearkdegree)); // 添加到列表
  391. // pearkDegreeList.Add(Math.Round(PearkTorque, 2)); // 添加到列表
  392. //}
  393. ////精度
  394. //JD_MEAN = Math.Round(Pearkdegree);
  395. ////扭力
  396. //NL_MEAN = Math.Round(PearkTorque, 2);
  397. //// 调用 AddData 方法,存储数据
  398. //AddData(JD_MEAN, NL_MEAN, pearkTorqueList.ToArray(), pearkDegreeList.ToArray());
  399. #endregion
  400. }
  401. }
  402. break;
  403. case MTF6KConnStus.UnsubscribeCurve:
  404. TCP.SendMessage(MTF6K.unsubscribeLastTightingCurve);
  405. break;
  406. case MTF6KConnStus.UnsubcribeResult:
  407. TCP.SendMessage(MTF6K.unsubscribeTightingResult);
  408. break;
  409. }
  410. }
  411. catch (Exception e)
  412. {
  413. //logHelper.WriteLog("阿特拉斯通讯异常:" + e.StackTrace);
  414. }
  415. }
  416. }
  417. }
  418. // 线程安全锁对象
  419. private readonly object _queueLock = new object();
  420. // 添加数据到缓存
  421. private readonly Queue<(double JD_MEAN, double NL_MEAN, double[] PearkTorque, double[] Pearkdegree)> _dataQueueLeft = new Queue<(double JD_MEAN, double NL_MEAN, double[] PearkTorque, double[] Pearkdegree)>();
  422. private readonly Queue<(double JD_MEAN, double NL_MEAN, double[] PearkTorque, double[] Pearkdegree)> _dataQueueRight = new Queue<(double JD_MEAN, double NL_MEAN, double[] PearkTorque, double[] Pearkdegree)>();
  423. // 添加数据到缓存
  424. public void AddData(double jdMean, double nlMean, double[] pearkTorque, double[] pearkDegree)
  425. {
  426. lock (_queueLock)
  427. {
  428. if (ReceiveIP == GlobalContext.AtlasAddressLeft)
  429. {
  430. _dataQueueLeft.Enqueue((jdMean, nlMean, pearkTorque, pearkDegree));
  431. }
  432. else if (Direction == GlobalContext.AtlasAddressRight)
  433. {
  434. _dataQueueRight.Enqueue((jdMean, nlMean, pearkTorque, pearkDegree));
  435. }
  436. }
  437. }
  438. // 获取并清空缓存中的所有数据(左侧)
  439. public IEnumerable<(double JD_MEAN, double NL_MEAN, double[] PearkTorque, double[] Pearkdegree)> GetCachedDataLeft()
  440. {
  441. lock (_queueLock)
  442. {
  443. var cachedData = _dataQueueLeft.ToList(); // 将当前队列内容复制到列表
  444. _dataQueueLeft.Clear(); // 清空队列
  445. return cachedData;
  446. }
  447. }
  448. // 获取并清空缓存中的所有数据(右侧)
  449. public IEnumerable<(double JD_MEAN, double NL_MEAN, double[] PearkTorque, double[] Pearkdegree)> GetCachedDataRight()
  450. {
  451. lock (_queueLock)
  452. {
  453. var cachedData = _dataQueueRight.ToList(); // 将当前队列内容复制到列表
  454. _dataQueueRight.Clear(); // 清空队列
  455. return cachedData;
  456. }
  457. }
  458. public void DrawCurveDegTor()
  459. {
  460. double preDegree = 0;
  461. double curDegree = 0;
  462. bool Step3 = false;
  463. double PearkTorque = 0.0;
  464. double Pearkdegree = 0.0;
  465. for (int i = 0; i < tightResult.angle.Length; i++)
  466. {
  467. double degree = tightResult.angle[i] * tightResult.angCoef;
  468. double torque = tightResult.torque[i] * tightResult.torCoef / 98;
  469. preDegree = curDegree;
  470. curDegree = degree;
  471. //反转
  472. if (curDegree - preDegree <= 0 && degree < 0)
  473. {
  474. if (torque > PearkTorque)
  475. {
  476. PearkTorque = torque;
  477. Pearkdegree = degree;
  478. }
  479. }
  480. else if (curDegree - preDegree > 0)//正转
  481. {
  482. if (degree < 360 && !Step3)//切换扭矩值
  483. {
  484. if (torque > PearkTorque)
  485. {
  486. PearkTorque = torque;
  487. Pearkdegree = degree;
  488. }
  489. }
  490. else
  491. {
  492. Step3 = true;
  493. if (torque > PearkTorque)
  494. {
  495. PearkTorque = torque;
  496. Pearkdegree = degree;
  497. }
  498. }
  499. //bianliang.NL_MEAN = PearkTorque;
  500. // bianliang.JD_MEAN = Pearkdegree;
  501. }
  502. }
  503. }
  504. }
  505. }