您好,欢迎访问三七文档
当前位置:首页 > 金融/证券 > 金融资料 > 如何用C语言实现Bladed中的外部控制器
如何用C语言实现Bladed中的外部控制器来源:本站原创作者:DLL下载家园更新时间:2010-9-9Bladed2010-09-0822:57:18阅读0评论0字号:大中小订阅1、Bladed软件及外部控制器在Bladed风机载荷模拟软件中,可以让用户用C语言编写自己的DLL程序作为载荷计算的风机控制器。DLL控制器通讯速度快,将风机控制系统的关键特性通过转换成C语言的控制器可以更真实的模拟风机的运行并获得用户风机的更逼真的载荷数据。下面是一个用VISUALSTUDIO2005的C++实现的用户控制器的实例。2、C++代码如何创建DLL可以参考《VC++.NET实现DLL并在其中调用》下面是语言源代码:Head文件//caiLib.h#ifndefCAILIB_H#defineCAILIB_H//externCint_declspec(dllexport)add(intx,inty);externC//avoidmanglednames{void__declspec(dllexport)__cdeclDISCON(float*avrSwap,int*aviFail,char*accInfile,char*avcOutname,char*avcMsg);}voidsetParams(float*sAvrSwap);intapp(intx,inty);#endif头文件说明:在其中定义程序名称。程序代码//caiLib.cpp:定义DLL应用程序的入口点。//#includestdafx.h#includecaiLib.h#includestdio.h#defineNINT(a)((a)=0.0?(int)((a)+0.5):(int)((a)-0.5))//在此定义浮点数转换成整数#ifdef_MANAGED#pragmamanaged(push,off)#endiffloat*SwapArray,Kp,Ki;//定义与Bladed交换数组的地址指针,定义PI控制器比例//及积分系数staticfloatsumError;//定义积分累积误差变量floatGetSwapValue(intIndex){return(SwapArray[Index-1]);}//定义读取Bladed传递变量值函数voidSetSwapValue(intIndex,floatVal){SwapArray[Index-1]=Val;}//定义设置传递到Bladed的变量void__declspec(dllexport)__cdeclDISCON(float*avrSwap,int*aviFail,char*accInfile,char*avcOutname,char*avcMsg){//主函数DISCON实现charMessage[257],InFile[257],OutName[1025];floatrTime,rMeasuredSpeed,rMeasuredPitch,rGeneratorSpeed;intiStatus,iFirstLog;staticintiStep,iDone,iControlState;staticfloatrPitchDemand,RPMSetpoint,TimeDiff,OldTime,Temp;FILE*fp;SwapArray=avrSwap;//Storethepointer//Makesurethere'saCstringterminatoraccInfile[NINT(avrSwap[49])+1]='\0';avcOutname[NINT(avrSwap[50])]='\0';avcMsg[0]='\0';iStatus=NINT(avrSwap[0]);//Initialiseif(iStatus==0){//ReadtheDISCON.INdatafp=fopen(DISCON.IN,r);fscanf(fp,%f\n%f\n,&Kp,&Ki);fclose(fp);printf(Kp=%0.5f,Ki=%0.5f\n,Kp,Ki);//InitialisepitchdemandtocurrentpitchanglerMeasuredPitch=GetSwapValue(4);rPitchDemand=rMeasuredPitch;sumError=0.0F;iStep=0;//Startupstep=0TimeDiff=0.0F;OldTime=GetSwapValue(2);//Thisisalsowhereyoucouldreadinanyuser-defineddatafromaccInFile}//Returndemandedvariables//Note:previousvaluescanbeusedbeforetheyupdated,tosimulateaone-sampledelayif(NINT(GetSwapValue(10))==0){SetSwapValue(42,rPitchDemand);SetSwapValue(43,rPitchDemand);SetSwapValue(44,rPitchDemand);SetSwapValue(45,rPitchDemand);}else{strcpy(avcMsg,ThissimpleDLLneedspitchpositionactuator);*aviFail=-1;}rMeasuredPitch=GetSwapValue(4);rGeneratorSpeed=GetSwapValue(20)/(2.0F*3.14F)*60.0F;//变换rad/s为rpmTimeDiff=GetSwapValue(2)-OldTime;iControlState=NINT(GetSwapValue(117));//获得Bladed的控制状态//iControlState=0;production//=1:Parked//=2:idling//=3:Startup//=4:NormalStop//=5:EmergencyStop//Maincalculationif(*aviFail=0){//Justa1-degreestepchangeinpitchpositiondemandat10secondsif(GetSwapValue(2)=0.1&&iStep==0){iStep=1;}if(iStep==1){rPitchDemand=40*0.017453F;if(rGeneratorSpeed220.0){RPMSetpoint=rGeneratorSpeed;//setpoint=GeneratorSpeediStep=iStep+1;}}if(iStep==2){Temp=PIController(rGeneratorSpeed,RPMSetpoint);rPitchDemand=rPitchDemand+Temp;if(TimeDiff=1){if(RPMSetpoint=1290.0){RPMSetpoint=1296.0F;iStep=iStep+1;iDone=1;}elseRPMSetpoint=RPMSetpoint+8.0F;OldTime=GetSwapValue(2);}//按8rpm/s增加发电机转速给定}if(iStep==3){Temp=PIController(rGeneratorSpeed,RPMSetpoint);rPitchDemand=rPitchDemand+Temp;}if(rPitchDemand3.1415926/2)rPitchDemand=3.1415926F/2.0F;if(rPitchDemand0)rPitchDemand=0.0F;//Loggingoutputstrcpy(avcOutname,GeneratorDemandSpeed:A/T;);SetSwapValue(NINT(GetSwapValue(63)),RPMSetpoint*2.0F*3.1415926F/60.0F);SetSwapValue(65,1.0F);}return;}#ifdef_MANAGED#pragmamanaged(pop)#endif将程序编译后得到程序caiLib.DLL.将此程序在Bladed中的控制部分进行定义如图:在其中定义程序的路径d:\proland\vbpro\cailib\debug\cailib.dll在ExternalControllerData中定义了2个参数,分别为Kp=0.005和Ki=0.002点击OK。在主计算中选择Start点击RunNow运行期间可以查看结果如下:模拟运行结果如下:上面是发电机转速给定值及发电机转速测量值和桨叶角度变化的实时图。
本文标题:如何用C语言实现Bladed中的外部控制器
链接地址:https://www.777doc.com/doc-5400290 .html