TestService.cpp 55 KB

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