📄 power.cpp
字号:
#include <qtimer.h>#include <qapplication.h>#include <qlabel.h>#include <qlcdnumber.h>#include <qpushbutton.h>#include <qlayout.h>#include <qvariant.h>#include <qtooltip.h>#include <qwhatsthis.h>#include <qcombobox.h>#include <qtable.h>#include <qcolor.h>#include <qpopupmenu.h>#include <qmenubar.h>#include <qfont.h>#include <string.h>#include <sys/ioctl.h>#include <sys/time.h>#include <unistd.h>#include <fcntl.h>#include "power.h"#include "fft.h"Power::Power( QWidget* parent , const char* name , WFlags fl ) :Gui(parent,name,fl){/***************** 系统初始化 ****************/ system_stat=STOP; listview_stat=NONE; Phase_channel=0; InitializeFFT(64); //initialize FFT table strcpy(ip_addr,"192.168.1.9"); strcpy(netmask,"255.255.255.0"); strcpy(gateway,"192.168.1.1"); listen_port=5188; current_factor_PhaseA_int=321; voltage_factor_PhaseA_int=342; current_factor_PhaseB_int=321; voltage_factor_PhaseB_int=342; current_factor_PhaseC_int=321; voltage_factor_PhaseC_int=342; if(parse_conf(ip_addr,netmask,gateway,&listen_port,¤t_factor_PhaseA_int,&voltage_factor_PhaseA_int,¤t_factor_PhaseB_int,&voltage_factor_PhaseB_int,¤t_factor_PhaseC_int,&voltage_factor_PhaseC_int)==-1) qWarning("Warning:parse config file error"); qDebug("current_factor_PhaseA_int=%d",current_factor_PhaseA_int); qDebug("voltage_factor_PhaseA_int=%d",voltage_factor_PhaseA_int); qDebug("current_factor_PhaseB_int=%d",current_factor_PhaseB_int); qDebug("voltage_factor_PhaseB_int=%d",voltage_factor_PhaseB_int); qDebug("current_factor_PhaseC_int=%d",current_factor_PhaseC_int); qDebug("voltage_factor_PhaseC_int=%d",voltage_factor_PhaseC_int); current_factor_PhaseA_double=(double)0.0000001*current_factor_PhaseA_int; voltage_factor_PhaseA_double=(double)0.00001*voltage_factor_PhaseA_int; current_factor_PhaseB_double=(double)0.0000001*current_factor_PhaseB_int; voltage_factor_PhaseB_double=(double)0.00001*voltage_factor_PhaseB_int; current_factor_PhaseC_double=(double)0.0000001*current_factor_PhaseC_int; voltage_factor_PhaseC_double=(double)0.00001*voltage_factor_PhaseC_int; if((fd=open(DEVICE_TLC2543,0))<0){ qFatal(DEVICE_TLC2543" cann't open\n"); }/****************系统初始化结束****************//****************添加系统菜单*****************/ file=new QPopupMenu(this); //添加菜单栏 QFont f=file->font(); f.setFamily( "adobe-courier" ); f.setPointSize( 16 ); quitID=file->insertItem(new MyMenuItem("&Quit ",f)); //添加文件菜单 file->setAccel(CTRL+Key_Q,quitID); file->connectItem(quitID,qApp,SLOT(quit())); operation=new QPopupMenu(this); //添加工具菜单 f=operation->font(); f.setFamily( "adobe-courier" ); f.setPointSize( 16 ); startID=operation->insertItem(new MyMenuItem("&Start ",f)); //添加start选项 operation->setAccel(CTRL+Key_S,startID); operation->connectItem(startID,this,SLOT(fstart())); stopID=operation->insertItem(new MyMenuItem("&Stop ",f)); //添加stop选项 operation->setAccel(CTRL+Key_T,stopID); operation->connectItem(stopID,this,SLOT(fstop())); operation->insertSeparator(); option=new QPopupMenu(this); //添加设置菜单 f=option->font(); f.setFamily( "adobe-courier" ); f.setPointSize( 16 ); settingID=option->insertItem(new MyMenuItem("&Settings... ",f)); option->connectItem(settingID,this,SLOT(fsetting())); about=new QPopupMenu(this); //添加帮助菜单 f=about->font(); f.setFamily( "adobe-courier" ); f.setPointSize( 16 ); aboutID=about->insertItem(new MyMenuItem("&About... ",f)); menuBar()->insertItem("File",file); menuBar()->insertItem("Tools",operation); menuBar()->insertItem("Options",option); menuBar()->insertItem("Help",about);/*****************添加系统菜单结束*******************//***************添加工具栏及工具按钮******************/ starticon=QPixmap("./start.xpm"); stopicon=QPixmap("./stop.xpm"); settingicon=QPixmap("./setting.xpm"); toolbar=new QToolBar(this); start=new QToolButton(starticon,"start","start",this,SLOT(fstart()),toolbar); //添加start工具按钮 stop=new QToolButton(stopicon,"stop","stop",this,SLOT(fstop()),toolbar); //添加stop工具按钮 setting=new QToolButton(settingicon,"setting","setting",this,SLOT(fsetting()),toolbar); //添加设置工具按钮/***************添加工具栏及工具按钮结束****************//*******************添加树形列表**********************/ ListView1->setRootIsDecorated(true); Phase_A=new QListViewItem(ListView1,"Phase_A"); //添加列表选项 Phase_B=new QListViewItem(ListView1,"Phase_B"); Phase_C=new QListViewItem(ListView1,"Phase_C"); General_ABC=new QListViewItem(ListView1,"General"); Phase_A_Harmonic=new QListViewItem(Phase_A,"Harmonic"); //添加列表子选项 Phase_A_Waveforms=new QListViewItem(Phase_A,"Waveforms"); Phase_B_Harmonic=new QListViewItem(Phase_B,"Harmonic"); Phase_B_Waveforms=new QListViewItem(Phase_B,"Waveforms"); Phase_C_Harmonic=new QListViewItem(Phase_C,"Harmonic"); Phase_C_Waveforms=new QListViewItem(Phase_C,"Waveforms"); General_VAHZ=new QListViewItem(General_ABC,"VAHZ"); General_Report=new QListViewItem(General_ABC,"Report"); General_WPF=new QListViewItem(General_ABC,"WPF");/*******************添加树形列表结束********************/ vahz=new VAHZ(Frame3,"VZHZ"); //新建VAHZ对象 vahz->setFrameShape( VAHZ::NoFrame ); vahz->setGeometry( QRect( 0, 0, 500, 413 ) ); vahz->hide(); //隐藏VAHZ对象 wpf=new WPF(Frame3,"WPF"); //新建WPF对象 wpf->setFrameShape( WPF::NoFrame ); wpf->setGeometry( QRect( 0, 0, 500, 413 ) ); wpf->hide(); //隐藏WPF对象 report1=new Report(Frame3,"report"); //新建report对象 report1->setFrameShape( Report::NoFrame ); report1->setGeometry( QRect( 3, 3, 500, 410 ) ); report1->hide(); //隐藏report对象 BarPhA_A=new Bar(Frame3,"BarPhA_A"); BarPhA_A->setFrameShape( Bar::NoFrame ); BarPhA_A->setGeometry( QRect( 0, 0, 500, 200 ) ); BarPhA_A->setLineString("L0"); BarPhA_A->setAVString("A"); BarPhA_A->setBarColor(red); BarPhA_A->setTitle("Current Hramonic",red); BarPhA_A->hide(); BarPhA_V=new Bar(Frame3,"BarPhA_V"); BarPhA_V->setFrameShape( Bar::NoFrame ); BarPhA_V->setGeometry( QRect( 0, 210, 500, 200 ) ); BarPhA_V->setLineString("L0"); BarPhA_V->setAVString("V"); BarPhA_V->setBarColor(blue); BarPhA_V->setTitle("Voltage Hramonic",blue); BarPhA_V->hide(); BarPhB_A=new Bar(Frame3,"BarPhB_A"); BarPhB_A->setFrameShape( Bar::NoFrame ); BarPhB_A->setGeometry( QRect( 0, 0, 500, 200 ) ); BarPhB_A->setLineString("L1"); BarPhB_A->setAVString("A"); BarPhB_A->setBarColor(red); BarPhB_A->setTitle("Current Hramonic",red); BarPhB_A->hide(); BarPhB_V=new Bar(Frame3,"BarPhB_V"); BarPhB_V->setFrameShape( Bar::NoFrame ); BarPhB_V->setGeometry( QRect( 0, 210, 500, 200 ) ); BarPhB_V->setLineString("L1"); BarPhB_V->setAVString("V"); BarPhB_V->setBarColor(blue); BarPhB_V->setTitle("Voltage Hramonic",blue); BarPhB_V->hide(); BarPhC_A=new Bar(Frame3,"BarPhC_A"); BarPhC_A->setFrameShape( Bar::NoFrame ); BarPhC_A->setGeometry( QRect( 0, 0, 500, 200 ) ); BarPhC_A->setLineString("L2"); BarPhC_A->setAVString("A"); BarPhC_A->setBarColor(red); BarPhC_A->setTitle("Current Hramonic",red); BarPhC_A->hide(); BarPhC_V=new Bar(Frame3,"BarPhC_V"); BarPhC_V->setFrameShape( Bar::NoFrame ); BarPhC_V->setGeometry( QRect( 0, 210, 500, 200 ) ); BarPhC_V->setLineString("L2"); BarPhC_V->setAVString("V"); BarPhC_V->setBarColor(blue); BarPhC_V->setTitle("Voltage Hramonic",blue); BarPhC_V->hide();/*****************新建set对话框******************/ SetDialog=new MyDialog(this,"setting_dialog",TRUE,Qt::WStyle_Customize | Qt::WStyle_NoBorder); SetDialog->set_dialog_fd(fd);/***************新建set对话框结束*****************/ sys_timer = new QTimer(this); //新建系统定时器 connect( sys_timer, SIGNAL(timeout()), this,SLOT(system_flush()) ); connect(ListView1,SIGNAL(doubleClicked(QListViewItem *)),this,SLOT(flistClicked(QListViewItem *))); sys_timer->stop();}Power::~Power(){ EndFFT();}Real Power::con_sin(int i, int random1,int random2,int random3,int Pha1,int Pha2,int Pha3){ Real tmp; double convert,PhA1,PhA2,PhA3; convert=Pha1; PhA1=convert/100; convert=Pha2; PhA2=convert/100; convert=Pha3; PhA3=convert/100; tmp=34.987+380*sin(i*COF+PhA1*PI)+random1*sin(3*i*COF+PhA2*PI)+random2*sin(5*i*COF+PhA3*PI)+random3*sin(11*i*COF)+50.542*sin(31*i*COF); return tmp;}double Power::count_HZ(double *Var,int cnt){ int tmp1,tmp2; double t1,t2,delt_t; int i,index[4],tmp; if(cnt!=64) return 0; tmp=0; for(i=0;i<63;i++){ if(Var[i]*Var[i+1]<=0.0){ index[tmp]=i; index[tmp+1]=i+1;// qDebug("Var[%d]=%4f Var[%d]=%4f ",i,Var[i],i+1,Var[i+1]); tmp+=2; } if(tmp>=4) break; if(Var[i]*Var[i+1]==0) i+=10; } // qDebug("\n"); tmp1=index[0]; tmp2=index[1]; t1=0.000312*Var[tmp1]/(Var[tmp1]-Var[tmp2]); tmp1=index[2]; tmp2=index[3]; t2=0.000312*Var[tmp1]/(Var[tmp1]-Var[tmp2]); delt_t=0.000312*(index[2]-index[0])-t1+t2; return (0.5/delt_t);}double Power::count_rms(double *Harmonic, int cnt){ double sum; int i; sum=0; for(i=1;i<cnt/2;i++){ sum+=Harmonic[i]*Harmonic[i]; } sum=sum/2; sum+=Harmonic[0]*Harmonic[0]; sum=sqrt(sum); return sum; }double Power::count_W(double *RealV,int cntV,double VFactor, double *RealA, int cntA,double AFactor){ double sum,sub; int i; if(cntA!=cntV) return 0; sum=0; sub=(double)cntV; for(i=0;i<cntV;i++){ sum+=(RealV[i]*VFactor)*(RealA[i]*AFactor); }// qDebug("sum=%.4f",sum); sum/=sub;// qDebug("sun=%.4f",sum); return sum;}void Power::system_flush(){ int i; double tmp; char buff[20]; struct timeval time_start_tmp,time_stop_tmp; QStringList StringList1,StringList2; memset(buff,0,sizeof(buff));#if TIME_DEBUG gettimeofday(&timer_debug[0],(struct timezone *)0);#endif/*************虚拟数据源****************/#if 0 unsigned int rand1=rand()%200,rand2=rand()%100,rand3=rand()%50,Pha1=rand()%100,Pha2=rand()%100,Pha3=rand()%100; for(i=0;i<64;i++){ Aconf[i]=con_sin(i,rand1,rand2,rand3,Pha1,Pha2,Pha3); } rand1=rand()%150,rand2=rand()%110,rand3=rand()%50,Pha1=rand()%100,Pha2=rand()%100,Pha3=rand()%100; for(i=0;i<64;i++){ Vconf[i]=con_sin(i,rand1,rand2,rand3,Pha1,Pha2,Pha3); }#endif/***********虚拟数据源结束*****************//************采集模拟数据*****************/ switch(Phase_channel){ case 0: ioctl(fd,0,0); //set sample channel 0 gettimeofday(&time_start_tmp,(struct timezone *)0); while(1){ if(read(fd,AD_tmp,130)==130){ break; }else{ gettimeofday(&time_stop_tmp,(struct timezone *)0); tmp=time_stop_tmp.tv_usec-time_start_tmp.tv_usec; if(tmp>=100000){ //exit if time out qWarning("Warning Sample Phase A timeout"); goto exit; } } } break; case 1: ioctl(fd,2,0); //set sample channel 2 gettimeofday(&time_start_tmp,(struct timezone *)0); while(1){ if(read(fd,AD_tmp,130)==130){ break; }else{ gettimeofday(&time_stop_tmp,(struct timezone *)0); tmp=time_stop_tmp.tv_usec-time_start_tmp.tv_usec; if(tmp>=100000){ //exit if time out qWarning("Warning Sample Phase B timeout"); goto exit; } } } break; case 2: ioctl(fd,4,0); //set sample channel 4 gettimeofday(&time_start_tmp,(struct timezone *)0); while(1){ if(read(fd,AD_tmp,130)==130){ break; }else{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -