TestService.cpp 51 KB

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