TestService.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. #include "TestService.h"
  2. #include "pid.h"
  3. #include "valve.h"
  4. #include "ballvalve.h"
  5. #include "PressureSensor.h"
  6. #include "Calibrationpara.h"
  7. #include<QDateTime>
  8. #include<QTime>
  9. #include<QEventLoop>
  10. #include<QMessageBox>
  11. #include<cmath>
  12. #include <QSignalSpy>
  13. #include <QDebug>
  14. #include <QFile>
  15. #include <QDir>
  16. TestService::TestService(QThread *parent)
  17. :QThread(parent)
  18. ,m_SerialUi3(nullptr)
  19. ,m_SerialUi4(nullptr)
  20. ,m_pTimer_1(nullptr)
  21. ,m_pTimer_2(nullptr)
  22. ,m_compartmentid(0)
  23. {
  24. m_bRunning=false;
  25. m_bRealEnd = true;
  26. for(int i=0; i<MAX_COMPARTMENT_NUM; i++ ){
  27. m_tank.m_comparts[i].m_id = i;
  28. }
  29. //moveToThread(&m_Thread);
  30. //connect(this, SIGNAL(operate()), this, SLOT(doTest()));
  31. }
  32. TestService::~TestService()
  33. {
  34. }
  35. void TestService::init()
  36. {
  37. m_pTimer_1 = new QTimer(this);
  38. connect(m_pTimer_1, SIGNAL(timeout()), this, SLOT(OnTimer1()));
  39. //m_pTimer_2 = new QTimer(this);
  40. //connect(m_pTimer_2, SIGNAL(timeout()), this, SLOT(OnTimer2()));
  41. if(!m_SerialUi3.serialConnect("COM3", "9600", "8", "0", "1")){
  42. QString str = "打开串口COM3失败";
  43. emit onNotice(str);
  44. }
  45. #if 1
  46. if(!m_SerialUi4.serialConnect("COM4", "9600", "8", "2", "1")){
  47. QString str = "打开串口COM4失败";
  48. emit onNotice(str);
  49. }
  50. #endif
  51. connect(&m_report, SIGNAL(onProgress(QString)), this, SLOT(receive_reportprogress(QString)));
  52. }
  53. bool TestService::setTankinfo(const QVariantList& list)
  54. {
  55. QString standard_name;
  56. QString licenseplate_str;
  57. QString companyname_str;
  58. int compartment_num = 0;
  59. int total_volume = 0;
  60. int volume[MAX_COMPARTMENT_NUM] ={0};
  61. for(int i=0; i<list.count(); i++){
  62. QString item_string = list[i].toString();
  63. if(0 == i){
  64. standard_name = item_string;
  65. }else if(1 == i){
  66. licenseplate_str = item_string;
  67. }else if(2 == i){
  68. companyname_str = item_string;
  69. }else if(3 == i){
  70. total_volume = item_string.toInt();
  71. }else if(4 == i){
  72. compartment_num = item_string.toInt();
  73. }else{
  74. if(i< (8+5))
  75. volume[i-5] = item_string.toInt();
  76. }
  77. }
  78. //check invalid
  79. bool ret_bool = true;
  80. if( compartment_num > 0 && compartment_num <= MAX_COMPARTMENT_NUM){
  81. int volume_sum =0;
  82. for(int i=0; i<compartment_num; i++){
  83. volume_sum += volume[i];
  84. }
  85. if(total_volume != volume_sum){
  86. ret_bool = false;
  87. }
  88. }else{
  89. ret_bool = false;
  90. }
  91. if(true == ret_bool){
  92. //m_standardname = standard_name;
  93. qDebug("setTankinfo: standard_name:[%s]", standard_name.toStdString().c_str());
  94. m_tank.licenseplate_str = licenseplate_str;
  95. qDebug("setTankinfo: licenseplate:[%s]", licenseplate_str.toStdString().c_str());
  96. m_tank.companyname_str = companyname_str;
  97. qDebug("setTankinfo: companyname:[%s]", companyname_str.toStdString().c_str());
  98. m_tank.total_volume = total_volume;
  99. qDebug("setTankinfo: total_volume:[%d]", total_volume);
  100. m_tank.compartment_num = compartment_num;
  101. qDebug("setTankinfo: compartment_num:[%d]", compartment_num);
  102. //Standard standard;
  103. if(g_StandardManager->getStandard(standard_name, m_standard)){
  104. for(int i=0; i<compartment_num; i++){
  105. m_tank.m_comparts[i].m_volume = volume[i];
  106. qDebug("setTankinfo: volume [%d]:[%d]", i, volume[i]);
  107. for(int j=0; j<m_standard.item_count; j++){
  108. if(volume[i] >= m_standard.item[j].oilcompartment_volume){
  109. m_tank.m_comparts[i].m_standarditem = m_standard.item[j];
  110. qDebug("setTankinfo: compart [%d]: volume[%d]", i, m_standard.item[j].oilcompartment_volume);
  111. qDebug("setTankinfo: compart [%d]: sys[%f]", i, m_standard.item[j].system_threshold);
  112. qDebug("setTankinfo: compart [%d]: valve[%f]", i, m_standard.item[j].valve_threshold);
  113. break;
  114. }
  115. }
  116. #if 0
  117. ParaItem pid_para;
  118. g_CalibrationPara->get_pidPara(volume[i], pid_para);
  119. qDebug("setTankinfo: pid max_volume:[%d]", pid_para.max_volume);
  120. qDebug("setTankinfo: pid keep_time:[%d]", pid_para.keep_time);
  121. qDebug("setTankinfo: pid outermax:[%f]", pid_para.outermax);
  122. qDebug("setTankinfo: pid outermin:[%f]", pid_para.outermin);
  123. qDebug("setTankinfo: pid outerkp:[%f]", pid_para.outerkp);
  124. qDebug("setTankinfo: pid outerki:[%f]", pid_para.outerki);
  125. qDebug("setTankinfo: pid outerkd:[%f]", pid_para.outerkd);
  126. qDebug("setTankinfo: pid innermax:[%f]", pid_para.innermax);
  127. qDebug("setTankinfo: pid innermin:[%f]", pid_para.innermin);
  128. qDebug("setTankinfo: pid innerkp:[%f]", pid_para.innerkp);
  129. #endif
  130. }
  131. }else{
  132. QString str = "所选标准不存在!";
  133. emit onNotice(str);
  134. ret_bool = false;
  135. }
  136. }
  137. return ret_bool;
  138. }
  139. bool TestService::tstart(int compartmentid, int direction)
  140. {
  141. if(m_bRunning){
  142. QString str = "当前有任务正在进行中...";
  143. emit onNotice(str);
  144. return false;
  145. }
  146. if(false == m_bRealEnd){
  147. QString str = "当前有任务还未完全结束...";
  148. emit onNotice(str);
  149. return false;
  150. }
  151. m_compartmentid = compartmentid-1;
  152. if(0 == direction)
  153. m_direction = Test_Direction_Positive;
  154. else
  155. m_direction = Test_Direction_Negative;
  156. m_stage = Test_Stage_SysTest;
  157. m_step = Test_Step_Start;
  158. m_pressuremutex.lock();
  159. m_pressurelist.clear(); //用来计算平均压力
  160. m_pressuremutex.unlock();
  161. QMessageBox::StandardButton result = QMessageBox::information(NULL, "提示:", "请对油气回收系统相关部件进行检查", QMessageBox::Ok|QMessageBox::Abort, QMessageBox::Ok);
  162. switch (result)
  163. {
  164. case QMessageBox::Ok:
  165. qDebug()<<"Ok";
  166. m_pTimer_1->start(1*500); // 500 ms
  167. m_bRunning = true;
  168. start();
  169. break;
  170. case QMessageBox::Abort:
  171. qDebug()<<"Abort";
  172. return false;
  173. break;
  174. default:
  175. qDebug()<<"default";
  176. return false;
  177. break;
  178. }
  179. return true;
  180. }
  181. bool TestService::tselfstart(int compartmentid, int direction)
  182. {
  183. if(m_bRunning){
  184. QString str = "当前有任务正在进行中...";
  185. emit onNotice(str);
  186. return false;
  187. }
  188. if(false == m_bRealEnd){
  189. QString str = "当前有任务还未完全结束...";
  190. emit onNotice(str);
  191. return false;
  192. }
  193. m_compartmentid = compartmentid-1;
  194. if(0 == direction)
  195. m_direction = Test_Direction_Positive;
  196. else
  197. m_direction = Test_Direction_Negative;
  198. m_stage = Test_Stage_SysTest;
  199. m_step = Test_Step_Start;
  200. m_pressuremutex.lock();
  201. m_pressurelist.clear(); //用来计算平均压力
  202. m_pressuremutex.unlock();
  203. QMessageBox::StandardButton result = QMessageBox::question(NULL, "提示:", "系统即将自检,请确认所有部件都己接好", QMessageBox::Ok|QMessageBox::Abort, QMessageBox::Ok);
  204. switch (result)
  205. {
  206. case QMessageBox::Ok:
  207. qDebug()<<"Ok";
  208. m_pTimer_1->start(1*500); // 1s
  209. m_bRunning = true;
  210. start();
  211. break;
  212. case QMessageBox::Abort:
  213. qDebug()<<"Abort";
  214. return false;
  215. break;
  216. default:
  217. qDebug()<<"default";
  218. return false;
  219. break;
  220. }
  221. return true;
  222. }
  223. bool TestService::tstop(int compartmentid)
  224. {
  225. if(m_compartmentid != compartmentid-1){
  226. return false;
  227. }
  228. if(m_pTimer_1->isActive())
  229. m_pTimer_1->stop();
  230. //if(m_pTimer_2->isActive())
  231. // m_pTimer_2->stop();
  232. if(m_bRunning){
  233. //stop();
  234. m_bRunning = false;
  235. }
  236. return true;
  237. }
  238. bool TestService::reportpic_Ready(int compartmentid)
  239. {
  240. Compartment& com = m_tank.get_compartment(compartmentid-1);
  241. com.m_testfinished = true;
  242. return true;
  243. }
  244. bool TestService::check_alltest_ok()
  245. {
  246. bool ret_flag = true;
  247. for(int i=0; i< m_tank.compartment_num; i++){
  248. Compartment& com = m_tank.get_compartment(i);
  249. if(com.m_testfinished == false ){
  250. ret_flag = false;
  251. break;
  252. }
  253. }
  254. if(false == ret_flag){
  255. QMessageBox::StandardButton result = QMessageBox::question(NULL, "提示:", "有油仓还未进行检测,是否强制生成报告?", QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
  256. switch (result)
  257. {
  258. case QMessageBox::Yes:
  259. ret_flag = true;
  260. break;
  261. case QMessageBox::No:
  262. break;
  263. default:
  264. break;
  265. }
  266. }
  267. return ret_flag;
  268. }
  269. void TestService::receive_reportprogress(QString qstr)
  270. {
  271. qDebug() << "TestService::receive_reportprogress ";
  272. qDebug() << qstr;
  273. emit onGenreportProgress(qstr);
  274. }
  275. bool TestService::genReport(QString word_path, QString pic_dir)
  276. {
  277. bool ret = false;
  278. if(word_path.contains(".doc")){
  279. ret = m_report.make_Word(m_tank, word_path, pic_dir);
  280. }else if(word_path.contains(".pdf")){
  281. ret = m_report.make_PDF(m_tank, word_path, pic_dir);
  282. }else{
  283. QString str = "word_path invalid!";
  284. qDebug() << str;
  285. }
  286. return ret;
  287. }
  288. QJsonObject TestService::get_vrs_items()
  289. {
  290. return m_tank.get_vrs_items();
  291. }
  292. QJsonObject TestService::get_bls_items()
  293. {
  294. return m_tank.get_bls_items();
  295. }
  296. bool TestService::set_item_result(const QString& jsonStr )
  297. {
  298. return m_tank.set_item_result(jsonStr);
  299. }
  300. void TestService::push_pressurelist(float pressure)
  301. {
  302. m_pressuremutex.lock();
  303. if(m_pressurelist.size() == AVERAGE_PRESSURE_COUNT){
  304. m_pressurelist.removeAt(0);
  305. }
  306. m_pressurelist.append(pressure);
  307. m_pressuremutex.unlock();
  308. }
  309. void TestService::clear_pressurelist()
  310. {
  311. m_pressuremutex.lock();
  312. m_pressurelist.clear(); //用来计算平均压力
  313. m_pressuremutex.unlock();
  314. }
  315. float TestService::calculate_averagepressure()
  316. {
  317. float average = 0.0;
  318. m_pressuremutex.lock();
  319. int n = m_pressurelist.size();
  320. if(n>0){
  321. float sum = 0.0;
  322. for(int i=0; i<n; i++){
  323. sum += m_pressurelist.at(i);
  324. //qDebug("push_pressurelist 333 size[%f]", m_pressurelist.at(i));
  325. }
  326. average = sum/n;
  327. }
  328. m_pressuremutex.unlock();
  329. return average;
  330. }
  331. bool TestService::valve_open(Valve_ID id)
  332. {
  333. if(Valve_ID_All == id){
  334. Valve v(&m_SerialUi3, ALL_VALVE);
  335. return v.Open();
  336. }else{
  337. Valve v(&m_SerialUi3, id);
  338. return v.Open();
  339. }
  340. }
  341. bool TestService::valve_close(Valve_ID id)
  342. {
  343. if(Valve_ID_All == id){
  344. Valve v(&m_SerialUi3, ALL_VALVE);
  345. return v.Close();
  346. }else{
  347. Valve v(&m_SerialUi3, id);
  348. return v.Close();
  349. }
  350. }
  351. QString TestService::read_PressureValue()
  352. {
  353. float pressure = 0.0;
  354. static int timeout_count =0;
  355. PressureSensor ps(&m_SerialUi4);
  356. if(ps.Read(pressure)){
  357. push_pressurelist(pressure);
  358. timeout_count=0;
  359. }else{
  360. pressure = calculate_averagepressure();
  361. qDebug("Read_PressureValue failed");
  362. timeout_count++;
  363. if(timeout_count >= AVERAGE_PRESSURE_COUNT){
  364. //data is to old, clear
  365. clear_pressurelist();
  366. qDebug("clear_pressurelist ");
  367. }
  368. }
  369. int _ipressure = pressure*1000;
  370. _ipressure = (_ipressure/PRESSURE_DIVISION_VALUE)*PRESSURE_DIVISION_VALUE;
  371. pressure = _ipressure/1000.0;
  372. QString pressure_str = QString("%1").arg(pressure, 4,'f',2,QLatin1Char('0'));
  373. return pressure_str;
  374. }
  375. bool TestService::pressure_zero()
  376. {
  377. PressureSensor ps(&m_SerialUi4);
  378. return ps.Set_Zero();
  379. }
  380. bool TestService::ballvalve_control(QString position_str)
  381. {
  382. bool ok;
  383. ushort position = position_str.toUShort(&ok, 10);
  384. BallValve ball_v(&m_SerialUi4);
  385. return ball_v.SetPosition(position);
  386. }
  387. QJsonObject TestService::ballvalve_status()
  388. {
  389. unsigned char status;
  390. unsigned short value = 0;
  391. QJsonObject rootObj;
  392. BallValve ball_v(&m_SerialUi4);
  393. if(ball_v.GetSP(status, value)){
  394. switch(status){
  395. case BALLVALVE_STATUS_OPENING:
  396. rootObj.insert("status", "opening");
  397. break;
  398. case BALLVALVE_STATUS_CLOSING:
  399. rootObj.insert("status", "closing");
  400. break;
  401. case BALLVALVE_STATUS_OPENEND:
  402. rootObj.insert("status", "opened");
  403. break;
  404. case BALLVALVE_STATUS_CLOSEEND:
  405. rootObj.insert("status", "closed");
  406. break;
  407. case BALLVALVE_STATUS_BREAK:
  408. rootObj.insert("status", "break");
  409. break;
  410. case BALLVALVE_STATUS_FAULT:
  411. rootObj.insert("status", "fault");
  412. break;
  413. default:
  414. rootObj.insert("status", "unkown");
  415. break;
  416. }
  417. rootObj.insert("position", value);
  418. }else{
  419. rootObj.insert("status", "unkown");
  420. rootObj.insert("position", 0);
  421. }
  422. return rootObj;
  423. }
  424. void TestService::dump_comstatistic()
  425. {
  426. QString filepath = "D:/VaporRecoverySystemTest/comstatistic.txt";
  427. Valve in_v(&m_SerialUi3, VALVE_INTAKE);
  428. in_v.Dump_Statistics(filepath);
  429. PressureSensor ps(&m_SerialUi4);
  430. ps.Dump_Statistics(filepath);
  431. BallValve bv(&m_SerialUi4);
  432. bv.Dump_Statistics(filepath);
  433. }
  434. void TestService::OnTimer1()
  435. {
  436. float pressure = 0.00;
  437. static int timeout_count =0;
  438. static int times_count = 0;
  439. PressureSensor ps(&m_SerialUi4);
  440. if(ps.Read(pressure)){
  441. push_pressurelist(pressure);
  442. timeout_count=0;
  443. }else{
  444. qDebug("Read_PressureValue failed");
  445. pressure = calculate_averagepressure();
  446. timeout_count++;
  447. if(timeout_count >= AVERAGE_PRESSURE_COUNT){
  448. //data is to old, clear
  449. clear_pressurelist();
  450. qDebug("clear_pressurelist ");
  451. }
  452. }
  453. times_count++;
  454. if(0 == times_count%2){
  455. int _ipressure = 0;
  456. _ipressure = pressure*1000;
  457. _ipressure = (_ipressure/PRESSURE_DIVISION_VALUE)*PRESSURE_DIVISION_VALUE;
  458. pressure = _ipressure/1000.0;
  459. QString pressure_str = QString("%1").arg(pressure, 4,'f',2,QLatin1Char('0'));
  460. PressureItem item;
  461. //item.m_pressure = pressure_str;
  462. item.m_testDirection = m_direction;
  463. item.m_testStage = m_stage;
  464. item.m_testStep = m_step;
  465. QVariantMap map;
  466. map.clear();
  467. map.insert("pressure", pressure_str);
  468. map.insert("direction", item.m_testDirection);
  469. map.insert("stage", item.m_testStage);
  470. map.insert("step", item.m_testStep);
  471. //QString pressure_str = QString("%1").arg(pressure, 5,'f',3,QLatin1Char('0'));
  472. //m_tank.m_comparts[m_compartmentid].m_currentpressure= pressure_str.toFloat();
  473. emit sigPressure(m_compartmentid+1, map);
  474. }
  475. }
  476. void TestService::OnTimer2()
  477. {
  478. //qDebug("OnTimer2 >>>enter ");
  479. //float pressure;
  480. //Read_pressure(pressure);
  481. //QThread::sleep(5);
  482. //emit Pressure_changed(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
  483. //qDebug("OnTimer2 >>>leave ");
  484. }
  485. Ret_Value TestService::PositivePressure_Add(const TestExpect& expect, bool bSavedata)
  486. {
  487. Ret_Value ret_v = Ret_OK;
  488. int interval_time = 500; // ms
  489. int count = 0;
  490. int delay_count = 0;
  491. int exceed_times =0;
  492. QElapsedTimer timer;
  493. timer.start();
  494. float prev_pressure = 0;
  495. float current_pressure = 0;
  496. double current_speed = 0;
  497. unsigned char bvstatus;
  498. unsigned short bvposition;
  499. unsigned short bvoffset = 150;
  500. int keep_times = 60;
  501. int stable_count = 0; //稳压计数
  502. PressureSensor ps(&m_SerialUi4);
  503. BallValve bv(&m_SerialUi4);
  504. bool bv_ready = false;
  505. bv.SetPosition(bvoffset);
  506. PID* pPIDouter = NULL;
  507. PID* pPIDinner = NULL;
  508. double speed_target = 0.0;
  509. QList<float> _pressurelist;
  510. ParaItem pid_para;
  511. g_CalibrationPara->get_pidPara(expect.volume, pid_para);
  512. pPIDouter = new PID(0.5, pid_para.outermax, pid_para.outermin, pid_para.outerkp, 0, 0);
  513. //pPIDouter = new PID(0.5, pid_para.outermax, pid_para.outermin, pid_para.outerkp, pid_para.outerkd, pid_para.outerki);
  514. pPIDinner = new PID(0.5, pid_para.innermax, pid_para.innermin, pid_para.innerkp, pid_para.innerkd, pid_para.innerki,pid_para.innermaxI, pid_para.innerIstep, pid_para.innerDstep);
  515. //将稳压时间转换成计数
  516. keep_times = pid_para.keep_time *(1000/interval_time);
  517. prev_pressure = calculate_averagepressure();
  518. Valve in_v(&m_SerialUi3, VALVE_INTAKE);
  519. bool bOpen_intake = false;
  520. //bOpen_intake = in_v.Open();
  521. //
  522. QDateTime datetime = QDateTime::currentDateTime();
  523. QString timestr = datetime.toString("ddHHmmzzz");
  524. QString filepath = "D:/VaporRecoverySystemTest/";
  525. QFile datafile(filepath+timestr+"_PIDdata.csv");
  526. bool datafile_opened = false;
  527. if(true == bSavedata){
  528. datafile_opened = datafile.open(QFile::WriteOnly | QFile::Truncate);
  529. }
  530. //等待压力达到目标值附近
  531. while(m_bRunning){
  532. //等待球阀,到达指定位置 100
  533. if(false == bv_ready){
  534. if(false == bv.GetSP(bvstatus, bvposition)){
  535. qDebug(" PositivePressure_Add, bv.GetSP failed");
  536. }else{
  537. if(BALLVALVE_STATUS_FAULT == bvstatus){
  538. ret_v = Ret_Failed;
  539. break;
  540. }
  541. if((BALLVALVE_STATUS_OPENEND == bvstatus) || (BALLVALVE_STATUS_CLOSEEND == bvstatus) ){
  542. int ps_offset = bvoffset;
  543. int cu_position = bvposition;
  544. if(abs(cu_position-ps_offset) <= 10){
  545. bv_ready=true;
  546. }
  547. }else{
  548. bv.SetPosition(bvoffset);
  549. }
  550. }
  551. }else{
  552. current_pressure = calculate_averagepressure();
  553. current_speed = current_pressure - prev_pressure;
  554. prev_pressure = current_pressure;
  555. qDebug(" PositivePressure_Add, current_pressure : %f target: %f", current_pressure, expect.target);
  556. if(current_pressure >= expect.target+0.01){
  557. count = 0;
  558. stable_count++;
  559. if(bOpen_intake){
  560. qDebug(" PositivePressure_Add, in_v.Close ");
  561. if(in_v.Close()){
  562. bOpen_intake = false;
  563. exceed_times++;
  564. }
  565. //bOpen_intake = false;
  566. }else{
  567. // 过充了,压力下不来。 主要是系统自测会出现这种情况
  568. if((1 == exceed_times) && (stable_count > keep_times)){
  569. count = keep_times+10;
  570. }
  571. }
  572. #if 1
  573. if(exceed_times <= 2){
  574. delay_count = 15*(1000/interval_time);
  575. }else if(3 == exceed_times){
  576. delay_count =10*(1000/interval_time);
  577. }else{
  578. delay_count =5*(1000/interval_time);
  579. }
  580. if(expect.volume < 200){ // 电磁阀与球阀之间的气管内有高压气体,对压力影响较大
  581. if(bvposition != 0){
  582. qDebug(" PositivePressure_Add, position : %d", 0);
  583. if(bv.SetPosition(0))
  584. bvposition = 0;
  585. }
  586. }
  587. #endif
  588. }else{
  589. // stable_count 需要从第一次超出目标压力时,开始增加
  590. if(stable_count > 0) stable_count++;
  591. if(current_pressure >= expect.target && current_pressure < expect.target + 0.01){
  592. count++;
  593. //if(((false == bOpen_intake) && count >=10*(1000/interval_time))||((stable_count > keep_times)&&(false == bOpen_intake))){
  594. // count = keep_times+10;
  595. //}
  596. if(false == bOpen_intake){
  597. //在进气阀关闭,压力回落过程中,两种情况触发加压过程退出
  598. // 1. 压力值连续10秒都没有下降0.01Kpa, 说明压力基本稳定了
  599. // 2. 计数超出稳压所需计数
  600. if((count >=10*(1000/interval_time)) || (stable_count > keep_times) ){
  601. count = keep_times+10;
  602. }
  603. }
  604. // 进行阀门密闭性测试,加压过程中。 只要加压到目标压力就退出
  605. if(m_stage == Test_Stage_ValveTest){
  606. count = keep_times+10;
  607. }
  608. }else{
  609. count=0;
  610. if(delay_count > 0){
  611. delay_count--;
  612. }
  613. if(false == bOpen_intake && 0 == delay_count){
  614. bOpen_intake = in_v.Open();
  615. qDebug(" PositivePressure_Add, in_v.Open 222 ");
  616. pPIDinner->reset();
  617. if(bv.SetPosition(bvoffset))
  618. bvposition = bvoffset;
  619. }
  620. }
  621. if(NULL != pPIDouter && NULL != pPIDinner && 0 == delay_count){
  622. speed_target = pPIDouter->calculate(expect.target, current_pressure);
  623. qDebug(" PositivePressure_Add, pPIDinner speed_target:%f, current_speed:%f", speed_target, current_speed);
  624. double inc = pPIDinner->calculate(speed_target, current_speed);
  625. unsigned short position = bvoffset +inc;
  626. //qDebug(" PositivePressure_Add, position : %d", position);
  627. if(bvposition != position){
  628. qDebug(" PositivePressure_Add, position : %d", position);
  629. if(bv.SetPosition(position))
  630. bvposition = position;
  631. }
  632. }else{
  633. if(0 == delay_count){
  634. qDebug(" PositivePressure_Add, NULL = pPIDouter NULL == pPIDinner ");
  635. }
  636. //qDebug(" PositivePressure_Add, NULL = pPIDouter NULL == pPIDinner ");
  637. }
  638. }
  639. }
  640. //判断是否有泄露(球阀开度值打到最大, 连续20秒压力不上升,切达不到目标压力)
  641. if(bvposition == bvoffset+pid_para.innermax){
  642. _pressurelist.push_back(current_pressure);
  643. if(_pressurelist.size() == 20*(1000/interval_time)){
  644. float first = _pressurelist.front();
  645. if((current_pressure - first) <= 0.2){ //20秒了, 压力上升小于0.2,判为漏气,退出
  646. ret_v = Ret_Excepion;
  647. break;
  648. }
  649. _pressurelist.removeFirst();
  650. }
  651. }else{
  652. if(true != _pressurelist.isEmpty()){
  653. _pressurelist.clear();
  654. }
  655. }
  656. if(count >= keep_times){ //about 60s
  657. break;
  658. }
  659. if(true == bSavedata && true == datafile_opened){
  660. QTextStream out(&datafile); // 输入流
  661. QString str1 = QString("%1").arg(expect.target, 8,'f',6,QLatin1Char('0'));
  662. QString str2 = QString("%1").arg(current_pressure, 8,'f',6,QLatin1Char('0'));
  663. //out << QString::number(expect.target) << "," << QString::number(current_pressure) << ",";
  664. out << str1 << "," << str2 << ",";
  665. str1 = QString("%1").arg(speed_target, 8,'f',6,QLatin1Char('0'));
  666. str2 = QString("%1").arg(current_speed, 8,'f',6,QLatin1Char('0'));
  667. //out << QString::number(speed_target) << "," << QString::number(current_speed) << ",";
  668. out << str1 << "," << str2 << ",";
  669. out << QString::number(bvposition) << "\n";
  670. }
  671. QThread::msleep(interval_time);
  672. int remainingtime = expect.timeout*1000 - timer.elapsed();
  673. if(remainingtime <= 0){
  674. qDebug(" add_pressure timeout, Rough valve will colse");
  675. ret_v = Ret_Timeout;
  676. break;
  677. }
  678. }
  679. if(!m_bRunning){
  680. // be stoped
  681. ret_v = Ret_Exit;
  682. }
  683. if(NULL != pPIDouter){
  684. delete pPIDouter;
  685. }
  686. if(NULL != pPIDinner){
  687. delete pPIDinner;
  688. }
  689. in_v.Close();
  690. bv.SetPosition(0);
  691. if(true == bSavedata && true == datafile_opened){
  692. datafile.close();
  693. }
  694. return ret_v;
  695. }
  696. Ret_Value TestService::PositivePressure_Add_V2(const TestExpect& expect)
  697. {
  698. Ret_Value ret_v = Ret_OK;
  699. int count = 0;
  700. QElapsedTimer timer;
  701. timer.start();
  702. float current_pressure = 0;
  703. unsigned char bvstatus;
  704. unsigned short bvposition;
  705. unsigned short bvoffset = 120;
  706. int holding_time = 60;
  707. PressureSensor ps(&m_SerialUi4);
  708. BallValve bv(&m_SerialUi4);
  709. bool bv_ready = false;
  710. bv.SetPosition(bvoffset);
  711. PID* pPID = NULL;
  712. if(expect.volume <= 200){
  713. pPID = new PID(1, 200, 0, 0, 2, 0.4);
  714. holding_time = 60;
  715. }else if(expect.volume <= 4000){
  716. pPID = new PID(1, 500, 0, 0, 5, 0.6);
  717. holding_time = 60;
  718. }else if(expect.volume <= 6000){
  719. pPID = new PID(1, 800, 0, 0, 6, 1.0);
  720. holding_time = 120;
  721. }else if(expect.volume <= 8000){
  722. pPID = new PID(1, 800, 0, 0, 7, 2);
  723. holding_time = 180;
  724. }else{
  725. pPID = new PID(1, 800, 0, 0, 8, 3);
  726. holding_time = 180;
  727. }
  728. Valve in_v(&m_SerialUi3, VALVE_INTAKE);
  729. bool bOpen_intake = false;
  730. //bOpen_intake = in_v.Open();
  731. //等待压力达到目标值附近
  732. while(m_bRunning){
  733. //等待球阀,到达指定位置 100
  734. if(false == bv_ready){
  735. if(false == bv.GetSP(bvstatus, bvposition)){
  736. qDebug(" PositivePressure_Add, bv.GetSP failed");
  737. }else{
  738. if(BALLVALVE_STATUS_FAULT == bvstatus){
  739. ret_v = Ret_Failed;
  740. break;
  741. }
  742. if((BALLVALVE_STATUS_OPENEND == bvstatus) || (BALLVALVE_STATUS_CLOSEEND == bvstatus) ){
  743. int ps_offset = bvoffset;
  744. int cu_position = bvposition;
  745. if(abs(cu_position-ps_offset) < 10){
  746. bv_ready=true;
  747. }
  748. }else{
  749. bv.SetPosition(bvoffset);
  750. }
  751. }
  752. }else{
  753. current_pressure = calculate_averagepressure();
  754. qDebug(" PositivePressure_Add, current_pressure : %f target: %f", current_pressure, expect.target);
  755. if(current_pressure >= expect.target+0.05){
  756. count++;
  757. if(bOpen_intake){
  758. qDebug(" PositivePressure_Add, in_v.Close ");
  759. if(in_v.Close())
  760. bOpen_intake = false;
  761. }
  762. //系统自测及容积较小时,球阀最好归零,否则因球阀不太紧漏气, 进气阀与球阀之间的气体会压向系统,导致压力升高
  763. //当容器过大时, 如果压力超出后又降下来或系统存在漏气,需要重新加压,设置 bvoffset,能让重新加压速度快起来。
  764. if(expect.volume < 100){
  765. if(bv.SetPosition(0))
  766. bvposition = 0;
  767. qDebug(" PositivePressure_Add, bv.SetPosition 0");
  768. }else {
  769. if(bvoffset != bvposition){
  770. if(bv.SetPosition(bvoffset))
  771. bvposition = bvoffset;
  772. qDebug(" PositivePressure_Add, bv.SetPosition bvoffset");
  773. }
  774. }
  775. }else if((current_pressure < expect.target+0.05) & (current_pressure >= expect.target) ){
  776. count++;
  777. }else{
  778. count = 0;
  779. if(false == bOpen_intake){
  780. bOpen_intake = in_v.Open();
  781. qDebug(" PositivePressure_Add, in_v.Open ");
  782. pPID->reset();
  783. }else{
  784. if(NULL != pPID){
  785. double inc = pPID->calculate_v2(expect.target+0.05, current_pressure);
  786. unsigned short position = bvoffset +inc;
  787. //qDebug(" PositivePressure_Add, position : %d", position);
  788. if(bvposition != position){
  789. qDebug(" PositivePressure_Add, position : %d", position);
  790. if(bv.SetPosition(position))
  791. bvposition = position;
  792. }
  793. }else{
  794. qDebug(" PositivePressure_Add, NULL = pPID ");
  795. }
  796. }
  797. }
  798. }
  799. if(count >= holding_time){ //about 60s
  800. break;
  801. }
  802. QThread::msleep(1000);
  803. int remainingtime = expect.timeout*1000 - timer.elapsed();
  804. if(remainingtime <= 0){
  805. qDebug(" add_pressure timeout, Rough valve will colse");
  806. ret_v = Ret_Timeout;
  807. break;
  808. }
  809. }
  810. if(!m_bRunning){
  811. // be stoped
  812. ret_v = Ret_Exit;
  813. }
  814. if(NULL != pPID){
  815. delete pPID;
  816. }
  817. in_v.Close();
  818. bv.SetPosition(0);
  819. return ret_v;
  820. }
  821. Ret_Value TestService::Pressure_Relief(const TestExpect& expect)
  822. {
  823. Ret_Value ret_v = Ret_OK;
  824. bool bOpen_ventv = false;
  825. Valve vent_v(&m_SerialUi3, VALVE_VENT);
  826. float current_pressure;
  827. float last_pressure = calculate_averagepressure();
  828. QElapsedTimer timer;
  829. timer.start();
  830. int count = 0;
  831. //打开泄压阀
  832. bOpen_ventv = vent_v.Open();
  833. //last_pressure = calculate_averagepressure();
  834. //等待压力清零
  835. while(m_bRunning){
  836. if(false == bOpen_ventv){
  837. bOpen_ventv = vent_v.Open();
  838. }
  839. current_pressure = calculate_averagepressure();
  840. if(fabs(current_pressure - last_pressure) <= 0.01){
  841. qDebug(" Pressure_Relief 111 finished");
  842. count++;
  843. }else{
  844. count=0;
  845. }
  846. if((count > 5) ||(current_pressure < 0.01)){
  847. //关闭泄压阀
  848. vent_v.Close();
  849. break;
  850. }
  851. QThread::msleep(1000);
  852. int remainingtime = expect.timeout*1000 - timer.elapsed();
  853. if(remainingtime < 0){
  854. ret_v = Ret_Timeout;
  855. qDebug("timeout, Pressure_Relief Timeout");
  856. break;
  857. }
  858. }
  859. if(!m_bRunning){
  860. ret_v = Ret_Exit;
  861. }
  862. vent_v.Close();
  863. return ret_v;
  864. }
  865. Ret_Value TestService::Pressure_Relief()
  866. {
  867. Ret_Value ret_v = Ret_OK;
  868. bool bOpen_ventv = false;
  869. Valve vent_v(&m_SerialUi3, VALVE_VENT);
  870. float current_pressure;
  871. QElapsedTimer timer;
  872. timer.start();
  873. int count = 0;
  874. //打开泄压阀
  875. bOpen_ventv = vent_v.Open();
  876. //等待压力清零
  877. while(1){
  878. if(false == bOpen_ventv){
  879. bOpen_ventv = vent_v.Open();
  880. }
  881. if(false == m_pTimer_1->isActive())
  882. OnTimer1();
  883. current_pressure = calculate_averagepressure();
  884. if(current_pressure < 0.1){
  885. qDebug(" Pressure_Relief finished 222");
  886. count++;
  887. }else{
  888. count=0;
  889. }
  890. if(count > 0){
  891. //关闭泄压阀
  892. vent_v.Close();
  893. break;
  894. }
  895. QThread::msleep(1000);
  896. int remainingtime = 60*1000 - timer.elapsed();
  897. if(remainingtime < 0){
  898. ret_v = Ret_Timeout;
  899. qDebug("timeout, Pressure_Relief Timeout");
  900. break;
  901. }
  902. }
  903. vent_v.Close();
  904. qDebug(" Pressure_Relief leave");
  905. return ret_v;
  906. }
  907. Ret_Value TestService::Pressure_Keep(int keeptime, float& start_value, float& end_value)
  908. {
  909. int count = 0;
  910. Ret_Value ret_v = Ret_OK;
  911. start_value = calculate_averagepressure();
  912. int _ipressure = start_value*100;
  913. start_value = _ipressure/100.0;
  914. while((count < keeptime) && m_bRunning){
  915. QThread::msleep(1000);
  916. count++;
  917. }
  918. end_value = calculate_averagepressure();
  919. _ipressure = end_value*100;
  920. end_value = _ipressure/100.0;
  921. if(!m_bRunning){
  922. // be stoped
  923. ret_v = Ret_Exit;
  924. }
  925. return ret_v;
  926. }
  927. bool TestService::self_checking()
  928. {
  929. //读继电器状态
  930. Valve all_v(&m_SerialUi3, ALL_VALVE);
  931. if(false == all_v.Close()){
  932. return false;
  933. }
  934. //读取球阀状态
  935. BallValve ball_v(&m_SerialUi4);
  936. unsigned char status;
  937. unsigned short value;
  938. if(false == ball_v.GetSP(status, value)){
  939. return false;
  940. }
  941. if(status == BALLVALVE_STATUS_FAULT){
  942. return false;
  943. }
  944. ball_v.SetPosition(0);
  945. PressureSensor ps(&m_SerialUi4);
  946. float pressure = 0.0;
  947. if(false == ps.Read(pressure)){
  948. return false;
  949. }
  950. return true;
  951. }
  952. bool TestService::test_init()
  953. {
  954. //bool ret_v=true;
  955. Valve all_v(&m_SerialUi3, ALL_VALVE);
  956. all_v.Close();
  957. BallValve ball_v(&m_SerialUi4);
  958. unsigned char status;
  959. unsigned short value;
  960. if(ball_v.GetSP(status, value)){
  961. if(value > 20){
  962. ball_v.SetPosition(0);
  963. int wait_time = (1+value/100);
  964. QThread::msleep(wait_time*1000);
  965. }
  966. }else{
  967. return false;
  968. }
  969. return true;
  970. }
  971. int TestService::make_messagebox(Test_Direction direction, Test_Step step, Ret_Value ret_v)
  972. {
  973. int ret_btn = QMessageBox::NoButton;
  974. QString notice_str;
  975. if(Test_Direction_Positive == direction){
  976. switch(step){
  977. case Test_Step_AddPressure:
  978. if(Ret_Timeout == ret_v){
  979. notice_str = "加压超时, 是否重试?";
  980. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  981. }else if(Ret_Failed == ret_v){
  982. notice_str = "继电器通信失败, 和序终止";
  983. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  984. }else if(Ret_Excepion == ret_v){
  985. notice_str = "持续性压力不上升,请检测是否有漏气或阀门未关";
  986. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  987. }
  988. break;
  989. case Test_Step_AdjustPressure:
  990. if( Ret_Timeout == ret_v ){
  991. notice_str = "比例阀调压精调超时, 是否重试?";
  992. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  993. //emit onNotice(notice_str);
  994. }else if(Ret_Failed == ret_v){
  995. notice_str = "继电器通信失败, 程序终止";
  996. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  997. //emit onNotice(notice_str);
  998. }
  999. break;
  1000. default:
  1001. break;
  1002. }
  1003. }else {
  1004. }
  1005. return ret_btn;
  1006. }
  1007. void TestService::run()
  1008. {
  1009. qDebug("doTest >>>enter ");
  1010. QString notice_str;
  1011. //QElapsedTimer q_time;
  1012. int ret_btn = QMessageBox::NoButton;
  1013. TestExpect texpect;
  1014. bool bQuit = false;
  1015. //q_time.start();
  1016. Ret_Value ret_v = Ret_OK;
  1017. m_bRealEnd = false;
  1018. if(m_compartmentid >= 0){
  1019. if(m_direction == Test_Direction_Positive){
  1020. while(m_bRunning && (bQuit == false)){
  1021. switch(m_step){
  1022. case Test_Step_Start:
  1023. {
  1024. m_tank.get_compartment(m_compartmentid).clear_result();
  1025. if(false == self_checking()){
  1026. ret_btn = m_msgbox.information(NULL, "提示:", "通信故障,请终止程序", QMessageBox::Abort, QMessageBox::Abort);
  1027. }else{
  1028. test_init();
  1029. ret_btn = m_msgbox.information(NULL, "提示:", "请确认已经打开旋拧阀", QMessageBox::Ok, QMessageBox::Ok);
  1030. }
  1031. switch (ret_btn)
  1032. {
  1033. case QMessageBox::Ok:
  1034. m_step = Test_Step_AddPressure;
  1035. break;
  1036. case QMessageBox::Abort:
  1037. bQuit = true;
  1038. break;
  1039. default:
  1040. break;
  1041. }
  1042. }
  1043. break;
  1044. case Test_Step_AddPressure:
  1045. {
  1046. if(m_stage == Test_Stage_SysTest){
  1047. emit sigState(m_compartmentid+1, "回收系统测试,开始加压", "");
  1048. }else{
  1049. emit sigState(m_compartmentid+1, "回收阀测试,开始加压", "");
  1050. }
  1051. texpect.target = m_standard.positive_target;
  1052. texpect.volume = m_tank.m_comparts[m_compartmentid].m_volume;
  1053. texpect.timeout = m_tank.m_comparts[m_compartmentid].get_addpressure_timeout();
  1054. ret_v = PositivePressure_Add(texpect);
  1055. if(Ret_OK == ret_v){
  1056. if(m_stage == Test_Stage_SysTest){
  1057. m_step = Test_Step_KeepPressure;
  1058. }else if(m_stage == Test_Stage_ValveTest){
  1059. m_msgbox.information(NULL, "提示:", "即将泄压, 请确认已经关闭油气回收阀。", QMessageBox::Ok, QMessageBox::Ok);
  1060. m_step = Test_Step_reliefPressure;
  1061. }
  1062. }else if(Ret_Exit == ret_v){
  1063. bQuit = true;
  1064. }else{
  1065. ret_btn = make_messagebox(m_direction, m_step, ret_v);
  1066. switch (ret_btn)
  1067. {
  1068. case QMessageBox::Retry:
  1069. qDebug()<<"Retry";
  1070. break;
  1071. case QMessageBox::Abort:
  1072. qDebug()<<"Abort";
  1073. bQuit = true;
  1074. break;
  1075. case QMessageBox::Ok:
  1076. qDebug()<<"OK";
  1077. bQuit = true;
  1078. break;
  1079. default:
  1080. break;
  1081. }
  1082. }
  1083. }
  1084. break;
  1085. case Test_Step_KeepPressure:
  1086. {
  1087. if(m_stage == Test_Stage_SysTest){
  1088. emit sigState(m_compartmentid+1, "油气回收系统测试,开始保压", "");
  1089. }else{
  1090. emit sigState(m_compartmentid+1, "油气回收阀测试,开始保压", "");
  1091. }
  1092. // 检测 5分钟都没超限,就是通过了
  1093. //float delta = 0;
  1094. //float threshold = 0.0;
  1095. //bool bPassed = true;
  1096. // 检测 5分钟都没超限,就是通过了
  1097. float delta = 0;
  1098. float start_value = 0.0;
  1099. float end_value = 0.0;
  1100. bool bPassed = true;
  1101. //ret_v = Pressure_Keep2(5*60, start_value, end_value);
  1102. if(m_stage == Test_Stage_SysTest){
  1103. ret_v = Pressure_Keep(5*60, start_value, end_value);
  1104. m_tank.m_comparts[m_compartmentid].m_result.sysstart_pressure = start_value;
  1105. m_tank.m_comparts[m_compartmentid].m_result.sysend_pressure = end_value;
  1106. delta = start_value - end_value;
  1107. m_tank.m_comparts[m_compartmentid].m_result.delta_sys_pressure = delta;
  1108. if(Ret_Exit == ret_v){
  1109. bQuit = true;
  1110. } else {
  1111. if(delta < m_tank.m_comparts[m_compartmentid].m_standarditem.system_threshold){
  1112. bPassed = true;
  1113. m_tank.m_comparts[m_compartmentid].m_result.pass_sys_pressure = true;
  1114. }else{
  1115. bPassed = false;
  1116. m_tank.m_comparts[m_compartmentid].m_result.pass_sys_pressure = false;
  1117. }
  1118. m_tank.m_comparts[m_compartmentid].m_result.sys_test_finished = true;
  1119. QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1120. emit sigSystemResult(m_compartmentid+1, delta_str, bPassed);
  1121. emit sigState(m_compartmentid+1, "油气回收系统检测,结束", "");
  1122. ret_btn = QMessageBox::NoButton;
  1123. notice_str = "系统密闭性已经检测完成,是否进行阀门密闭性检测?";
  1124. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1125. switch (ret_btn)
  1126. {
  1127. case QMessageBox::Yes:
  1128. m_stage = Test_Stage_ValveTest;
  1129. if(calculate_averagepressure() < m_standard.positive_target){
  1130. m_step = Test_Step_AddPressure;
  1131. }else{
  1132. m_msgbox.information(NULL, "提示:", "即将泄压, 请确认已经关闭油气回收阀。", QMessageBox::Ok, QMessageBox::Ok);
  1133. m_step = Test_Step_reliefPressure; //如果压力还在4.5以上, 可以不调压
  1134. }
  1135. break;
  1136. case QMessageBox::No:
  1137. m_step = Test_Step_End;
  1138. break;
  1139. default:
  1140. break;
  1141. }
  1142. }
  1143. }else{
  1144. ret_v = Pressure_Keep(5*60, start_value, end_value);
  1145. float offset = 0;
  1146. //修正 强制初始压力为 0
  1147. if(start_value != 0.0){
  1148. offset = start_value;
  1149. start_value = 0.0;
  1150. end_value -= offset;
  1151. }
  1152. m_tank.m_comparts[m_compartmentid].m_result.valvestart_pressure = start_value;
  1153. m_tank.m_comparts[m_compartmentid].m_result.valveend_pressure = end_value;
  1154. delta = end_value - start_value;
  1155. m_tank.m_comparts[m_compartmentid].m_result.delta_valve_pressure = delta;
  1156. if(Ret_Exit == ret_v){
  1157. bQuit = true;
  1158. }else {
  1159. if(delta <= m_tank.m_comparts[m_compartmentid].m_standarditem.valve_threshold){
  1160. m_tank.m_comparts[m_compartmentid].m_result.pass_valve_pressure = true;
  1161. bPassed = true;
  1162. }else{
  1163. m_tank.m_comparts[m_compartmentid].m_result.pass_valve_pressure = false;
  1164. bPassed = false;
  1165. }
  1166. m_tank.m_comparts[m_compartmentid].m_result.valve_test_finished = true;
  1167. QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1168. emit sigValveResult(m_compartmentid+1, delta_str, bPassed);
  1169. emit sigState(m_compartmentid+1, "油气回收阀测试,结束", "");
  1170. m_step = Test_Step_End;
  1171. }
  1172. }
  1173. }
  1174. break;
  1175. case Test_Step_reliefPressure:
  1176. {
  1177. if(m_stage == Test_Stage_SysTest){
  1178. emit sigState(m_compartmentid+1, "油气回收系统测试结束,开始泄压", "");
  1179. }else if(m_stage == Test_Stage_ValveTest){
  1180. emit sigState(m_compartmentid+1, "油气回收阀测试,开始泄压", "");
  1181. }
  1182. texpect.volume = 0;
  1183. texpect.target = 0.0;
  1184. texpect.timeout = 60;//m_tank.m_comparts[m_compartmentid].get_adjust_timeout();
  1185. ret_v = Pressure_Relief(texpect);
  1186. if(Ret_Exit == ret_v){
  1187. bQuit = true;
  1188. }else if(Ret_OK == ret_v){
  1189. m_step = Test_Step_KeepPressure;
  1190. if(m_stage == Test_Stage_ValveTest){
  1191. m_step = Test_Step_KeepPressure;
  1192. }else{
  1193. m_step = Test_Step_End;
  1194. }
  1195. }else{
  1196. ret_btn = QMessageBox::NoButton;
  1197. notice_str = "泄压超时, 是否重试?";
  1198. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  1199. switch (ret_btn)
  1200. {
  1201. case QMessageBox::Retry:
  1202. break;
  1203. case QMessageBox::Abort:
  1204. bQuit = true;
  1205. break;
  1206. case QMessageBox::Ok:
  1207. bQuit = true;
  1208. break;
  1209. default:
  1210. break;
  1211. }
  1212. }
  1213. }
  1214. break;
  1215. default:
  1216. break;
  1217. }
  1218. if(Test_Step_End == m_step){
  1219. break;
  1220. }
  1221. }
  1222. }else{
  1223. //负压测试, 暂不支持
  1224. m_stage = Test_Stage_SysTest;
  1225. qDebug(" negative test is not support now ");
  1226. }
  1227. }else{
  1228. //self test
  1229. if(m_direction == Test_Direction_Positive){
  1230. while(m_bRunning && (bQuit == false)){
  1231. switch(m_step){
  1232. case Test_Step_Start:
  1233. {
  1234. emit sigState(m_compartmentid+1, "开始", "");
  1235. if(false == self_checking()){
  1236. ret_btn = m_msgbox.information(NULL, "提示:", "通信故障,请终止程序", QMessageBox::Abort, QMessageBox::Abort);
  1237. }else{
  1238. test_init();
  1239. ret_btn = m_msgbox.information(NULL, "提示:", "请确认已经打开旋拧阀", QMessageBox::Ok, QMessageBox::Ok);
  1240. }
  1241. switch (ret_btn)
  1242. {
  1243. case QMessageBox::Ok:
  1244. qDebug()<<"Yes";
  1245. m_step = Test_Step_AddPressure;
  1246. break;
  1247. case QMessageBox::Abort:
  1248. bQuit = true;
  1249. break;
  1250. }
  1251. }
  1252. break;
  1253. case Test_Step_AddPressure:
  1254. {
  1255. emit sigState(m_compartmentid+1, "加压", "");
  1256. QThread::msleep(2000);
  1257. texpect.volume = 0;
  1258. texpect.target = 4.50;
  1259. texpect.timeout = 5*60; //3分钟
  1260. ret_v = PositivePressure_Add(texpect);
  1261. if(Ret_Exit == ret_v){
  1262. bQuit = true;
  1263. }else if(Ret_OK == ret_v){
  1264. m_step = Test_Step_KeepPressure;
  1265. }else{
  1266. ret_btn = QMessageBox::NoButton;
  1267. if(Ret_Timeout == ret_v){
  1268. notice_str = "正压加压超时, 是否重试?";
  1269. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  1270. }else{
  1271. notice_str = "继电器通信失败, 程序终止";
  1272. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  1273. }
  1274. switch (ret_btn)
  1275. {
  1276. case QMessageBox::Retry:
  1277. break;
  1278. case QMessageBox::Abort:
  1279. bQuit = true;
  1280. break;
  1281. case QMessageBox::Ok:
  1282. bQuit = true;
  1283. break;
  1284. default:
  1285. break;
  1286. }
  1287. }
  1288. }
  1289. break;
  1290. case Test_Step_KeepPressure:
  1291. {
  1292. emit sigState(m_compartmentid+1, "保压", "");
  1293. // 检测 5分钟都没超限,就是通过了
  1294. float delta = 0;
  1295. float start_value = 0.0;
  1296. float end_value = 0.0;
  1297. bool bPassed = true;
  1298. ret_v = Pressure_Keep(5*60, start_value, end_value);
  1299. if(Ret_Exit == ret_v){
  1300. bQuit = true;
  1301. }else {
  1302. delta = start_value - end_value;
  1303. if(delta <= 0.5){
  1304. bPassed = true;
  1305. }else{
  1306. bPassed = false;
  1307. }
  1308. //QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1309. QString info_str = "初始压力:";
  1310. info_str += QString("%1").arg(start_value, 4,'f',2,QLatin1Char('0'));
  1311. info_str += ", 结束压力:";
  1312. info_str += QString("%1").arg(end_value, 4,'f',2,QLatin1Char('0'));
  1313. info_str += ", Deta:";
  1314. info_str += QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1315. emit sigSelfTestResult(m_compartmentid+1, info_str, bPassed);
  1316. emit sigState(m_compartmentid+1, "结束", "");
  1317. m_step = Test_Step_reliefPressure;
  1318. }
  1319. }
  1320. break;
  1321. case Test_Step_reliefPressure:
  1322. {
  1323. ret_btn = m_msgbox.information(NULL, "提示:", "栓测任务结束,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1324. m_step = Test_Step_End;
  1325. switch (ret_btn)
  1326. {
  1327. case QMessageBox::Yes:
  1328. qDebug()<<"Yes";
  1329. {
  1330. TestExpect expect;
  1331. expect.target = 0.0;
  1332. expect.volume = 0;
  1333. expect.timeout = 30;
  1334. ret_v = Pressure_Relief(expect);
  1335. if(Ret_Exit == ret_v){
  1336. bQuit = true;
  1337. }
  1338. }
  1339. break;
  1340. default:
  1341. break;
  1342. }
  1343. }
  1344. break;
  1345. default:
  1346. break;
  1347. }
  1348. if(Test_Step_End == m_step){
  1349. break;
  1350. }
  1351. }
  1352. }else{ //negtive
  1353. }
  1354. }
  1355. if(Test_Step_End == m_step){
  1356. ret_btn = QMessageBox::NoButton;
  1357. }else{
  1358. if(true == bQuit){
  1359. if(Ret_Exit == ret_v){ // 主动退出
  1360. ret_btn = m_msgbox.information(NULL, "提示:", "程序退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1361. }else if(Ret_Timeout == ret_v){ // 超时退出
  1362. ret_btn = m_msgbox.information(NULL, "提示:", "程序超时退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1363. }else{//通信错误退出
  1364. ret_btn = m_msgbox.information(NULL, "提示:", "程序通信错误退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1365. }
  1366. }
  1367. }
  1368. switch (ret_btn)
  1369. {
  1370. case QMessageBox::Yes:
  1371. Pressure_Relief();
  1372. break;
  1373. default:
  1374. break;
  1375. }
  1376. if(m_bRunning){
  1377. m_bRunning = false;
  1378. if(m_compartmentid >= 0){ //不是系统自测
  1379. if(true == m_tank.m_comparts[m_compartmentid].m_result.sys_test_finished){
  1380. m_tank.m_comparts[m_compartmentid].m_testfinished = true;
  1381. }
  1382. }
  1383. emit sigStop(m_compartmentid+1);
  1384. }
  1385. Valve all_v(&m_SerialUi3, ALL_VALVE);
  1386. all_v.Close();
  1387. //dump_comstatistic();
  1388. m_bRealEnd = true;
  1389. qDebug("doTest >>>leave ");
  1390. }