TestService.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  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_bpicReady = 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_bpicReady == 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. if(word_path.contains(".doc")){
  278. return m_report.make_Word(m_tank, word_path, pic_dir);
  279. }else if(word_path.contains(".pdf")){
  280. return m_report.make_PDF(m_tank, word_path, pic_dir);
  281. }
  282. QString str = "word_path invalid!";
  283. qDebug() << str;
  284. return false;
  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. ParaItem pid_para;
  508. g_CalibrationPara->get_pidPara(expect.volume, pid_para);
  509. pPIDouter = new PID(0.5, pid_para.outermax, pid_para.outermin, pid_para.outerkp, 0, 0);
  510. //pPIDouter = new PID(0.5, pid_para.outermax, pid_para.outermin, pid_para.outerkp, pid_para.outerkd, pid_para.outerki);
  511. 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);
  512. //将稳压时间转换成计数
  513. keep_times = pid_para.keep_time *(1000/interval_time);
  514. prev_pressure = calculate_averagepressure();
  515. Valve in_v(&m_SerialUi3, VALVE_INTAKE);
  516. bool bOpen_intake = false;
  517. //bOpen_intake = in_v.Open();
  518. //
  519. QDateTime datetime = QDateTime::currentDateTime();
  520. QString timestr = datetime.toString("ddHHmmzzz");
  521. QString filepath = "D:/VaporRecoverySystemTest/";
  522. QFile datafile(filepath+timestr+"_PIDdata.csv");
  523. bool datafile_opened = false;
  524. if(true == bSavedata){
  525. datafile_opened = datafile.open(QFile::WriteOnly | QFile::Truncate);
  526. }
  527. //等待压力达到目标值附近
  528. while(m_bRunning){
  529. //等待球阀,到达指定位置 100
  530. if(false == bv_ready){
  531. if(false == bv.GetSP(bvstatus, bvposition)){
  532. qDebug(" PositivePressure_Add, bv.GetSP failed");
  533. }else{
  534. if(BALLVALVE_STATUS_FAULT == bvstatus){
  535. ret_v = Ret_Failed;
  536. break;
  537. }
  538. if((BALLVALVE_STATUS_OPENEND == bvstatus) || (BALLVALVE_STATUS_CLOSEEND == bvstatus) ){
  539. int ps_offset = bvoffset;
  540. int cu_position = bvposition;
  541. if(abs(cu_position-ps_offset) <= 10){
  542. bv_ready=true;
  543. }
  544. }else{
  545. bv.SetPosition(bvoffset);
  546. }
  547. }
  548. }else{
  549. current_pressure = calculate_averagepressure();
  550. current_speed = current_pressure - prev_pressure;
  551. prev_pressure = current_pressure;
  552. qDebug(" PositivePressure_Add, current_pressure : %f target: %f", current_pressure, expect.target);
  553. if(current_pressure >= expect.target+0.01){
  554. count = 0;
  555. stable_count++;
  556. if(bOpen_intake){
  557. qDebug(" PositivePressure_Add, in_v.Close ");
  558. if(in_v.Close()){
  559. bOpen_intake = false;
  560. exceed_times++;
  561. }
  562. //bOpen_intake = false;
  563. }
  564. #if 1
  565. if(exceed_times <= 2){
  566. delay_count = 15*(1000/interval_time);
  567. }else if(3 == exceed_times){
  568. delay_count =10*(1000/interval_time);
  569. }else{
  570. delay_count =5*(1000/interval_time);
  571. }
  572. #endif
  573. }else{
  574. // stable_count 需要从第一次超出目标压力时,开始增加
  575. if(stable_count > 0) stable_count++;
  576. if(current_pressure >= expect.target && current_pressure < expect.target + 0.01){
  577. count++;
  578. if(((false == bOpen_intake) && count >=10*(1000/interval_time))||((stable_count > keep_times)&&(false == bOpen_intake))){
  579. count = keep_times+10;
  580. }
  581. if(false == bOpen_intake){
  582. //在进气阀关闭,压力回落过程中,两种情况触发加压过程退出
  583. // 1. 压力值连续10秒都没有下降0.01Kpa, 说明压力基本稳定了
  584. // 2. 计数超出稳压所需计数
  585. if((count >=10*(1000/interval_time)) || (stable_count > keep_times) ){
  586. count = keep_times+10;
  587. }
  588. }
  589. // 进行阀门密闭性测试,加压过程中。 只要加压到目标压力就退出
  590. if(m_stage == Test_Stage_ValveTest){
  591. count = keep_times+10;
  592. }
  593. }else{
  594. count=0;
  595. if(delay_count > 0){
  596. delay_count--;
  597. }
  598. if(false == bOpen_intake && 0 == delay_count){
  599. bOpen_intake = in_v.Open();
  600. qDebug(" PositivePressure_Add, in_v.Open 222 ");
  601. pPIDinner->reset();
  602. if(bv.SetPosition(bvoffset))
  603. bvposition = bvoffset;
  604. }
  605. }
  606. if(NULL != pPIDouter && NULL != pPIDinner && 0 == delay_count){
  607. speed_target = pPIDouter->calculate(expect.target, current_pressure);
  608. qDebug(" PositivePressure_Add, pPIDinner speed_target:%f, current_speed:%f", speed_target, current_speed);
  609. double inc = pPIDinner->calculate(speed_target, current_speed);
  610. unsigned short position = bvoffset +inc;
  611. //qDebug(" PositivePressure_Add, position : %d", position);
  612. if(bvposition != position){
  613. qDebug(" PositivePressure_Add, position : %d", position);
  614. if(bv.SetPosition(position))
  615. bvposition = position;
  616. }
  617. }else{
  618. if(0 == delay_count){
  619. qDebug(" PositivePressure_Add, NULL = pPIDouter NULL == pPIDinner ");
  620. }
  621. //qDebug(" PositivePressure_Add, NULL = pPIDouter NULL == pPIDinner ");
  622. }
  623. }
  624. }
  625. if(count >= keep_times){ //about 60s
  626. break;
  627. }
  628. if(true == bSavedata && true == datafile_opened){
  629. QTextStream out(&datafile); // 输入流
  630. QString str1 = QString("%1").arg(expect.target, 8,'f',6,QLatin1Char('0'));
  631. QString str2 = QString("%1").arg(current_pressure, 8,'f',6,QLatin1Char('0'));
  632. //out << QString::number(expect.target) << "," << QString::number(current_pressure) << ",";
  633. out << str1 << "," << str2 << ",";
  634. str1 = QString("%1").arg(speed_target, 8,'f',6,QLatin1Char('0'));
  635. str2 = QString("%1").arg(current_speed, 8,'f',6,QLatin1Char('0'));
  636. //out << QString::number(speed_target) << "," << QString::number(current_speed) << ",";
  637. out << str1 << "," << str2 << ",";
  638. out << QString::number(bvposition) << "\n";
  639. }
  640. QThread::msleep(interval_time);
  641. int remainingtime = expect.timeout*1000 - timer.elapsed();
  642. if(remainingtime <= 0){
  643. qDebug(" add_pressure timeout, Rough valve will colse");
  644. ret_v = Ret_Timeout;
  645. break;
  646. }
  647. }
  648. if(!m_bRunning){
  649. // be stoped
  650. ret_v = Ret_Exit;
  651. }
  652. if(NULL != pPIDouter){
  653. delete pPIDouter;
  654. }
  655. if(NULL != pPIDinner){
  656. delete pPIDinner;
  657. }
  658. in_v.Close();
  659. bv.SetPosition(0);
  660. if(true == bSavedata && true == datafile_opened){
  661. datafile.close();
  662. }
  663. return ret_v;
  664. }
  665. Ret_Value TestService::PositivePressure_Add_V2(const TestExpect& expect)
  666. {
  667. Ret_Value ret_v = Ret_OK;
  668. int count = 0;
  669. QElapsedTimer timer;
  670. timer.start();
  671. float current_pressure = 0;
  672. unsigned char bvstatus;
  673. unsigned short bvposition;
  674. unsigned short bvoffset = 120;
  675. int holding_time = 60;
  676. PressureSensor ps(&m_SerialUi4);
  677. BallValve bv(&m_SerialUi4);
  678. bool bv_ready = false;
  679. bv.SetPosition(bvoffset);
  680. PID* pPID = NULL;
  681. if(expect.volume <= 200){
  682. pPID = new PID(1, 200, 0, 0, 2, 0.4);
  683. holding_time = 60;
  684. }else if(expect.volume <= 4000){
  685. pPID = new PID(1, 500, 0, 0, 5, 0.6);
  686. holding_time = 60;
  687. }else if(expect.volume <= 6000){
  688. pPID = new PID(1, 800, 0, 0, 6, 1.0);
  689. holding_time = 120;
  690. }else if(expect.volume <= 8000){
  691. pPID = new PID(1, 800, 0, 0, 7, 2);
  692. holding_time = 180;
  693. }else{
  694. pPID = new PID(1, 800, 0, 0, 8, 3);
  695. holding_time = 180;
  696. }
  697. Valve in_v(&m_SerialUi3, VALVE_INTAKE);
  698. bool bOpen_intake = false;
  699. //bOpen_intake = in_v.Open();
  700. //等待压力达到目标值附近
  701. while(m_bRunning){
  702. //等待球阀,到达指定位置 100
  703. if(false == bv_ready){
  704. if(false == bv.GetSP(bvstatus, bvposition)){
  705. qDebug(" PositivePressure_Add, bv.GetSP failed");
  706. }else{
  707. if(BALLVALVE_STATUS_FAULT == bvstatus){
  708. ret_v = Ret_Failed;
  709. break;
  710. }
  711. if((BALLVALVE_STATUS_OPENEND == bvstatus) || (BALLVALVE_STATUS_CLOSEEND == bvstatus) ){
  712. int ps_offset = bvoffset;
  713. int cu_position = bvposition;
  714. if(abs(cu_position-ps_offset) < 10){
  715. bv_ready=true;
  716. }
  717. }else{
  718. bv.SetPosition(bvoffset);
  719. }
  720. }
  721. }else{
  722. current_pressure = calculate_averagepressure();
  723. qDebug(" PositivePressure_Add, current_pressure : %f target: %f", current_pressure, expect.target);
  724. if(current_pressure >= expect.target+0.05){
  725. count++;
  726. if(bOpen_intake){
  727. qDebug(" PositivePressure_Add, in_v.Close ");
  728. if(in_v.Close())
  729. bOpen_intake = false;
  730. }
  731. //系统自测及容积较小时,球阀最好归零,否则因球阀不太紧漏气, 进气阀与球阀之间的气体会压向系统,导致压力升高
  732. //当容器过大时, 如果压力超出后又降下来或系统存在漏气,需要重新加压,设置 bvoffset,能让重新加压速度快起来。
  733. if(expect.volume < 100){
  734. if(bv.SetPosition(0))
  735. bvposition = 0;
  736. qDebug(" PositivePressure_Add, bv.SetPosition 0");
  737. }else {
  738. if(bvoffset != bvposition){
  739. if(bv.SetPosition(bvoffset))
  740. bvposition = bvoffset;
  741. qDebug(" PositivePressure_Add, bv.SetPosition bvoffset");
  742. }
  743. }
  744. }else if((current_pressure < expect.target+0.05) & (current_pressure >= expect.target) ){
  745. count++;
  746. }else{
  747. count = 0;
  748. if(false == bOpen_intake){
  749. bOpen_intake = in_v.Open();
  750. qDebug(" PositivePressure_Add, in_v.Open ");
  751. pPID->reset();
  752. }else{
  753. if(NULL != pPID){
  754. double inc = pPID->calculate_v2(expect.target+0.05, current_pressure);
  755. unsigned short position = bvoffset +inc;
  756. //qDebug(" PositivePressure_Add, position : %d", position);
  757. if(bvposition != position){
  758. qDebug(" PositivePressure_Add, position : %d", position);
  759. if(bv.SetPosition(position))
  760. bvposition = position;
  761. }
  762. }else{
  763. qDebug(" PositivePressure_Add, NULL = pPID ");
  764. }
  765. }
  766. }
  767. }
  768. if(count >= holding_time){ //about 60s
  769. break;
  770. }
  771. QThread::msleep(1000);
  772. int remainingtime = expect.timeout*1000 - timer.elapsed();
  773. if(remainingtime <= 0){
  774. qDebug(" add_pressure timeout, Rough valve will colse");
  775. ret_v = Ret_Timeout;
  776. break;
  777. }
  778. }
  779. if(!m_bRunning){
  780. // be stoped
  781. ret_v = Ret_Exit;
  782. }
  783. if(NULL != pPID){
  784. delete pPID;
  785. }
  786. in_v.Close();
  787. bv.SetPosition(0);
  788. return ret_v;
  789. }
  790. Ret_Value TestService::Pressure_Relief(const TestExpect& expect)
  791. {
  792. Ret_Value ret_v = Ret_OK;
  793. bool bOpen_ventv = false;
  794. Valve vent_v(&m_SerialUi3, VALVE_VENT);
  795. float current_pressure;
  796. float last_pressure = calculate_averagepressure();
  797. QElapsedTimer timer;
  798. timer.start();
  799. int count = 0;
  800. //打开泄压阀
  801. bOpen_ventv = vent_v.Open();
  802. //last_pressure = calculate_averagepressure();
  803. //等待压力清零
  804. while(m_bRunning){
  805. if(false == bOpen_ventv){
  806. bOpen_ventv = vent_v.Open();
  807. }
  808. current_pressure = calculate_averagepressure();
  809. if(fabs(current_pressure - last_pressure) <= 0.01){
  810. qDebug(" Pressure_Relief 111 finished");
  811. count++;
  812. }else{
  813. count=0;
  814. }
  815. if((count > 5) ||(current_pressure < 0.01)){
  816. //关闭泄压阀
  817. vent_v.Close();
  818. break;
  819. }
  820. QThread::msleep(1000);
  821. int remainingtime = expect.timeout*1000 - timer.elapsed();
  822. if(remainingtime < 0){
  823. ret_v = Ret_Timeout;
  824. qDebug("timeout, Pressure_Relief Timeout");
  825. break;
  826. }
  827. }
  828. if(!m_bRunning){
  829. ret_v = Ret_Exit;
  830. }
  831. vent_v.Close();
  832. return ret_v;
  833. }
  834. Ret_Value TestService::Pressure_Relief()
  835. {
  836. Ret_Value ret_v = Ret_OK;
  837. bool bOpen_ventv = false;
  838. Valve vent_v(&m_SerialUi3, VALVE_VENT);
  839. float current_pressure;
  840. QElapsedTimer timer;
  841. timer.start();
  842. int count = 0;
  843. //打开泄压阀
  844. bOpen_ventv = vent_v.Open();
  845. //等待压力清零
  846. while(1){
  847. if(false == bOpen_ventv){
  848. bOpen_ventv = vent_v.Open();
  849. }
  850. if(false == m_pTimer_1->isActive())
  851. OnTimer1();
  852. current_pressure = calculate_averagepressure();
  853. if(current_pressure < 0.1){
  854. qDebug(" Pressure_Relief finished 222");
  855. count++;
  856. }else{
  857. count=0;
  858. }
  859. if(count > 0){
  860. //关闭泄压阀
  861. vent_v.Close();
  862. break;
  863. }
  864. QThread::msleep(1000);
  865. int remainingtime = 60*1000 - timer.elapsed();
  866. if(remainingtime < 0){
  867. ret_v = Ret_Timeout;
  868. qDebug("timeout, Pressure_Relief Timeout");
  869. break;
  870. }
  871. }
  872. vent_v.Close();
  873. qDebug(" Pressure_Relief leave");
  874. return ret_v;
  875. }
  876. Ret_Value TestService::Pressure_Keep(int keeptime, float& start_value, float& end_value)
  877. {
  878. int count = 0;
  879. Ret_Value ret_v = Ret_OK;
  880. start_value = calculate_averagepressure();
  881. int _ipressure = start_value*100;
  882. start_value = _ipressure/100.0;
  883. while((count < keeptime) && m_bRunning){
  884. QThread::msleep(1000);
  885. count++;
  886. }
  887. end_value = calculate_averagepressure();
  888. _ipressure = end_value*100;
  889. end_value = _ipressure/100.0;
  890. if(!m_bRunning){
  891. // be stoped
  892. ret_v = Ret_Exit;
  893. }
  894. return ret_v;
  895. }
  896. bool TestService::self_checking()
  897. {
  898. //读继电器状态
  899. Valve all_v(&m_SerialUi3, ALL_VALVE);
  900. if(false == all_v.Close()){
  901. return false;
  902. }
  903. //读取球阀状态
  904. BallValve ball_v(&m_SerialUi4);
  905. unsigned char status;
  906. unsigned short value;
  907. if(false == ball_v.GetSP(status, value)){
  908. return false;
  909. }
  910. if(status == BALLVALVE_STATUS_FAULT){
  911. return false;
  912. }
  913. ball_v.SetPosition(0);
  914. PressureSensor ps(&m_SerialUi4);
  915. float pressure = 0.0;
  916. if(false == ps.Read(pressure)){
  917. return false;
  918. }
  919. return true;
  920. }
  921. bool TestService::test_init()
  922. {
  923. //bool ret_v=true;
  924. Valve all_v(&m_SerialUi3, ALL_VALVE);
  925. all_v.Close();
  926. BallValve ball_v(&m_SerialUi4);
  927. unsigned char status;
  928. unsigned short value;
  929. if(ball_v.GetSP(status, value)){
  930. if(value > 20){
  931. ball_v.SetPosition(0);
  932. int wait_time = (1+value/100);
  933. QThread::msleep(wait_time*1000);
  934. }
  935. }else{
  936. return false;
  937. }
  938. return true;
  939. }
  940. int TestService::make_messagebox(Test_Direction direction, Test_Step step, Ret_Value ret_v)
  941. {
  942. int ret_btn = QMessageBox::NoButton;
  943. QString notice_str;
  944. if(Test_Direction_Positive == direction){
  945. switch(step){
  946. case Test_Step_AddPressure:
  947. if(Ret_Timeout == ret_v){
  948. notice_str = "加压超时, 是否重试?";
  949. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  950. }else if(Ret_Failed == ret_v){
  951. notice_str = "继电器通信失败, 和序终止";
  952. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  953. }
  954. break;
  955. case Test_Step_AdjustPressure:
  956. if( Ret_Timeout == ret_v ){
  957. notice_str = "比例阀调压精调超时, 是否重试?";
  958. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  959. //emit onNotice(notice_str);
  960. }else if(Ret_Failed == ret_v){
  961. notice_str = "继电器通信失败, 程序终止";
  962. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  963. //emit onNotice(notice_str);
  964. }
  965. break;
  966. default:
  967. break;
  968. }
  969. }else {
  970. }
  971. return ret_btn;
  972. }
  973. void TestService::run()
  974. {
  975. qDebug("doTest >>>enter ");
  976. QString notice_str;
  977. //QElapsedTimer q_time;
  978. int ret_btn = QMessageBox::NoButton;
  979. TestExpect texpect;
  980. bool bQuit = false;
  981. //q_time.start();
  982. Ret_Value ret_v = Ret_OK;
  983. m_bRealEnd = false;
  984. if(m_compartmentid >= 0){
  985. if(m_direction == Test_Direction_Positive){
  986. while(m_bRunning && (bQuit == false)){
  987. switch(m_step){
  988. case Test_Step_Start:
  989. {
  990. m_tank.get_compartment(m_compartmentid).clear_result();
  991. if(false == self_checking()){
  992. ret_btn = m_msgbox.information(NULL, "提示:", "通信故障,请终止程序", QMessageBox::Abort, QMessageBox::Abort);
  993. }else{
  994. test_init();
  995. ret_btn = m_msgbox.information(NULL, "提示:", "请确认已经打开旋拧阀", QMessageBox::Ok, QMessageBox::Ok);
  996. }
  997. switch (ret_btn)
  998. {
  999. case QMessageBox::Ok:
  1000. m_step = Test_Step_AddPressure;
  1001. break;
  1002. default:
  1003. break;
  1004. }
  1005. }
  1006. break;
  1007. case Test_Step_AddPressure:
  1008. {
  1009. if(m_stage == Test_Stage_SysTest){
  1010. emit sigState(m_compartmentid+1, "回收系统测试,开始加压", "");
  1011. }else{
  1012. emit sigState(m_compartmentid+1, "回收阀测试,开始加压", "");
  1013. }
  1014. texpect.target = m_standard.positive_target;
  1015. texpect.volume = m_tank.m_comparts[m_compartmentid].m_volume;
  1016. texpect.timeout = m_tank.m_comparts[m_compartmentid].get_addpressure_timeout();
  1017. ret_v = PositivePressure_Add(texpect);
  1018. if(Ret_OK == ret_v){
  1019. if(m_stage == Test_Stage_SysTest){
  1020. m_step = Test_Step_KeepPressure;
  1021. }else if(m_stage == Test_Stage_ValveTest){
  1022. m_msgbox.information(NULL, "提示:", "即将泄压, 请确认已经关闭油气回收阀。", QMessageBox::Ok, QMessageBox::Ok);
  1023. m_step = Test_Step_reliefPressure;
  1024. }
  1025. }else if(Ret_Exit == ret_v){
  1026. bQuit = true;
  1027. }else{
  1028. ret_btn = make_messagebox(m_direction, m_step, ret_v);
  1029. switch (ret_btn)
  1030. {
  1031. case QMessageBox::Retry:
  1032. qDebug()<<"Retry";
  1033. break;
  1034. case QMessageBox::Abort:
  1035. qDebug()<<"Abort";
  1036. bQuit = true;
  1037. break;
  1038. case QMessageBox::Ok:
  1039. qDebug()<<"OK";
  1040. bQuit = true;
  1041. break;
  1042. default:
  1043. break;
  1044. }
  1045. }
  1046. }
  1047. break;
  1048. case Test_Step_KeepPressure:
  1049. {
  1050. if(m_stage == Test_Stage_SysTest){
  1051. emit sigState(m_compartmentid+1, "油气回收系统测试,开始保压", "");
  1052. }else{
  1053. emit sigState(m_compartmentid+1, "油气回收阀测试,开始保压", "");
  1054. }
  1055. // 检测 5分钟都没超限,就是通过了
  1056. //float delta = 0;
  1057. //float threshold = 0.0;
  1058. //bool bPassed = true;
  1059. // 检测 5分钟都没超限,就是通过了
  1060. float delta = 0;
  1061. float start_value = 0.0;
  1062. float end_value = 0.0;
  1063. bool bPassed = true;
  1064. //ret_v = Pressure_Keep2(5*60, start_value, end_value);
  1065. if(m_stage == Test_Stage_SysTest){
  1066. ret_v = Pressure_Keep(5*60, start_value, end_value);
  1067. m_tank.m_comparts[m_compartmentid].m_result.sysstart_pressure = start_value;
  1068. m_tank.m_comparts[m_compartmentid].m_result.sysend_pressure = end_value;
  1069. delta = start_value - end_value;
  1070. m_tank.m_comparts[m_compartmentid].m_result.delta_sys_pressure = delta;
  1071. if(Ret_Exit == ret_v){
  1072. bQuit = true;
  1073. } else {
  1074. if(delta < m_tank.m_comparts[m_compartmentid].m_standarditem.system_threshold){
  1075. bPassed = true;
  1076. m_tank.m_comparts[m_compartmentid].m_result.pass_sys_pressure = true;
  1077. }else{
  1078. bPassed = false;
  1079. m_tank.m_comparts[m_compartmentid].m_result.pass_sys_pressure = false;
  1080. }
  1081. m_tank.m_comparts[m_compartmentid].m_result.sys_test_finished = true;
  1082. QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1083. emit sigSystemResult(m_compartmentid+1, delta_str, bPassed);
  1084. emit sigState(m_compartmentid+1, "油气回收系统检测,结束", "");
  1085. ret_btn = QMessageBox::NoButton;
  1086. notice_str = "系统密闭性已经检测完成,是否进行阀门密闭性检测?";
  1087. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1088. switch (ret_btn)
  1089. {
  1090. case QMessageBox::Yes:
  1091. m_stage = Test_Stage_ValveTest;
  1092. if(calculate_averagepressure() < m_standard.positive_target){
  1093. m_step = Test_Step_AddPressure;
  1094. }else{
  1095. m_msgbox.information(NULL, "提示:", "即将泄压, 请确认已经关闭油气回收阀。", QMessageBox::Ok, QMessageBox::Ok);
  1096. m_step = Test_Step_reliefPressure; //如果压力还在4.5以上, 可以不调压
  1097. }
  1098. break;
  1099. case QMessageBox::No:
  1100. m_step = Test_Step_End;
  1101. break;
  1102. default:
  1103. break;
  1104. }
  1105. }
  1106. }else{
  1107. ret_v = Pressure_Keep(5*60, start_value, end_value);
  1108. float offset = 0;
  1109. //修正 强制初始压力为 0
  1110. if(start_value != 0.0){
  1111. offset = start_value;
  1112. start_value = 0.0;
  1113. end_value -= offset;
  1114. }
  1115. m_tank.m_comparts[m_compartmentid].m_result.valvestart_pressure = start_value;
  1116. m_tank.m_comparts[m_compartmentid].m_result.valveend_pressure = end_value;
  1117. delta = end_value - start_value;
  1118. m_tank.m_comparts[m_compartmentid].m_result.delta_valve_pressure = delta;
  1119. if(Ret_Exit == ret_v){
  1120. bQuit = true;
  1121. }else {
  1122. if(delta <= m_tank.m_comparts[m_compartmentid].m_standarditem.valve_threshold){
  1123. m_tank.m_comparts[m_compartmentid].m_result.pass_valve_pressure = true;
  1124. bPassed = true;
  1125. }else{
  1126. m_tank.m_comparts[m_compartmentid].m_result.pass_valve_pressure = false;
  1127. bPassed = false;
  1128. }
  1129. m_tank.m_comparts[m_compartmentid].m_result.valve_test_finished = true;
  1130. QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1131. emit sigValveResult(m_compartmentid+1, delta_str, bPassed);
  1132. emit sigState(m_compartmentid+1, "油气回收阀测试,结束", "");
  1133. m_step = Test_Step_End;
  1134. }
  1135. }
  1136. }
  1137. break;
  1138. case Test_Step_reliefPressure:
  1139. {
  1140. if(m_stage == Test_Stage_SysTest){
  1141. emit sigState(m_compartmentid+1, "油气回收系统测试结束,开始泄压", "");
  1142. }else if(m_stage == Test_Stage_ValveTest){
  1143. emit sigState(m_compartmentid+1, "油气回收阀测试,开始泄压", "");
  1144. }
  1145. texpect.volume = 0;
  1146. texpect.target = 0.0;
  1147. texpect.timeout = 60;//m_tank.m_comparts[m_compartmentid].get_adjust_timeout();
  1148. ret_v = Pressure_Relief(texpect);
  1149. if(Ret_Exit == ret_v){
  1150. bQuit = true;
  1151. }else if(Ret_OK == ret_v){
  1152. m_step = Test_Step_KeepPressure;
  1153. if(m_stage == Test_Stage_ValveTest){
  1154. m_step = Test_Step_KeepPressure;
  1155. }else{
  1156. m_step = Test_Step_End;
  1157. }
  1158. }else{
  1159. ret_btn = QMessageBox::NoButton;
  1160. notice_str = "泄压超时, 是否重试?";
  1161. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  1162. switch (ret_btn)
  1163. {
  1164. case QMessageBox::Retry:
  1165. break;
  1166. case QMessageBox::Abort:
  1167. bQuit = true;
  1168. break;
  1169. case QMessageBox::Ok:
  1170. bQuit = true;
  1171. break;
  1172. default:
  1173. break;
  1174. }
  1175. }
  1176. }
  1177. break;
  1178. default:
  1179. break;
  1180. }
  1181. if(Test_Step_End == m_step){
  1182. break;
  1183. }
  1184. }
  1185. }else{
  1186. //负压测试, 暂不支持
  1187. m_stage = Test_Stage_SysTest;
  1188. qDebug(" negative test is not support now ");
  1189. }
  1190. }else{
  1191. //self test
  1192. if(m_direction == Test_Direction_Positive){
  1193. while(m_bRunning && (bQuit == false)){
  1194. switch(m_step){
  1195. case Test_Step_Start:
  1196. {
  1197. emit sigState(m_compartmentid+1, "开始", "");
  1198. if(false == self_checking()){
  1199. ret_btn = m_msgbox.information(NULL, "提示:", "通信故障,请终止程序", QMessageBox::Abort, QMessageBox::Abort);
  1200. }else{
  1201. test_init();
  1202. ret_btn = m_msgbox.information(NULL, "提示:", "请确认已经打开旋拧阀", QMessageBox::Ok, QMessageBox::Ok);
  1203. }
  1204. switch (ret_btn)
  1205. {
  1206. case QMessageBox::Ok:
  1207. qDebug()<<"Yes";
  1208. m_step = Test_Step_AddPressure;
  1209. break;
  1210. case QMessageBox::Abort:
  1211. bQuit = true;
  1212. break;
  1213. }
  1214. }
  1215. break;
  1216. case Test_Step_AddPressure:
  1217. {
  1218. emit sigState(m_compartmentid+1, "加压", "");
  1219. QThread::msleep(2000);
  1220. texpect.volume = 0;
  1221. texpect.target = 4.50;
  1222. texpect.timeout = 3*60; //3分钟
  1223. ret_v = PositivePressure_Add(texpect);
  1224. if(Ret_Exit == ret_v){
  1225. bQuit = true;
  1226. }else if(Ret_OK == ret_v){
  1227. m_step = Test_Step_KeepPressure;
  1228. }else{
  1229. ret_btn = QMessageBox::NoButton;
  1230. if(Ret_Timeout == ret_v){
  1231. notice_str = "正压加压超时, 是否重试?";
  1232. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  1233. }else{
  1234. notice_str = "继电器通信失败, 程序终止";
  1235. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  1236. }
  1237. switch (ret_btn)
  1238. {
  1239. case QMessageBox::Retry:
  1240. break;
  1241. case QMessageBox::Abort:
  1242. bQuit = true;
  1243. break;
  1244. case QMessageBox::Ok:
  1245. bQuit = true;
  1246. break;
  1247. default:
  1248. break;
  1249. }
  1250. }
  1251. }
  1252. break;
  1253. case Test_Step_KeepPressure:
  1254. {
  1255. emit sigState(m_compartmentid+1, "保压", "");
  1256. // 检测 5分钟都没超限,就是通过了
  1257. float delta = 0;
  1258. float start_value = 0.0;
  1259. float end_value = 0.0;
  1260. bool bPassed = true;
  1261. ret_v = Pressure_Keep(5*60, start_value, end_value);
  1262. if(Ret_Exit == ret_v){
  1263. bQuit = true;
  1264. }else {
  1265. delta = start_value - end_value;
  1266. if(delta <= 0.5){
  1267. bPassed = true;
  1268. }else{
  1269. bPassed = false;
  1270. }
  1271. //QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1272. QString info_str = "初始压力:";
  1273. info_str += QString("%1").arg(start_value, 4,'f',2,QLatin1Char('0'));
  1274. info_str += ", 结束压力:";
  1275. info_str += QString("%1").arg(end_value, 4,'f',2,QLatin1Char('0'));
  1276. info_str += ", Deta:";
  1277. info_str += QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1278. emit sigSelfTestResult(m_compartmentid+1, info_str, bPassed);
  1279. emit sigState(m_compartmentid+1, "结束", "");
  1280. m_step = Test_Step_reliefPressure;
  1281. }
  1282. }
  1283. break;
  1284. case Test_Step_reliefPressure:
  1285. {
  1286. ret_btn = m_msgbox.information(NULL, "提示:", "栓测任务结束,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1287. m_step = Test_Step_End;
  1288. switch (ret_btn)
  1289. {
  1290. case QMessageBox::Yes:
  1291. qDebug()<<"Yes";
  1292. {
  1293. TestExpect expect;
  1294. expect.target = 0.0;
  1295. expect.volume = 0;
  1296. expect.timeout = 30;
  1297. ret_v = Pressure_Relief(expect);
  1298. if(Ret_Exit == ret_v){
  1299. bQuit = true;
  1300. }
  1301. }
  1302. break;
  1303. default:
  1304. break;
  1305. }
  1306. }
  1307. break;
  1308. default:
  1309. break;
  1310. }
  1311. if(Test_Step_End == m_step){
  1312. break;
  1313. }
  1314. }
  1315. }else{ //negtive
  1316. }
  1317. }
  1318. if(Test_Step_End == m_step){
  1319. ret_btn = QMessageBox::NoButton;
  1320. }else{
  1321. if(true == bQuit){
  1322. if(Ret_Exit == ret_v){ // 主动退出
  1323. ret_btn = m_msgbox.information(NULL, "提示:", "程序退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1324. }else if(Ret_Timeout == ret_v){ // 超时退出
  1325. ret_btn = m_msgbox.information(NULL, "提示:", "程序超时退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1326. }else{//通信错误退出
  1327. ret_btn = m_msgbox.information(NULL, "提示:", "程序通信错误退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1328. }
  1329. }
  1330. }
  1331. switch (ret_btn)
  1332. {
  1333. case QMessageBox::Yes:
  1334. Pressure_Relief();
  1335. break;
  1336. default:
  1337. break;
  1338. }
  1339. if(m_bRunning){
  1340. m_bRunning = false;
  1341. emit sigStop(m_compartmentid+1);
  1342. }
  1343. Valve all_v(&m_SerialUi3, ALL_VALVE);
  1344. all_v.Close();
  1345. //dump_comstatistic();
  1346. m_bRealEnd = true;
  1347. qDebug("doTest >>>leave ");
  1348. }