TestService.cpp 51 KB

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