admin 管理员组文章数量: 887021
2024年2月25日发(作者:小学英语body language)
如何用C语言实现Bladed中的外部控制器
来源:本站原创 作者:DLL下载家园 更新时间:2010-9-9
Bladed2010-09-08 22:57:18阅读0评论0 字号:大中小 订阅 1、Bladed软件及外部控制器
在Bladed风机载荷模拟软件中,可以让用户用C语言编写自己的DLL程序作为载荷计算的风机控制器。DLL控制器通讯速度快,将风机控制系统的关键特性通过转换成C语言的控制器可以更真实的模拟风机的运行并获得用户风机的更逼真的载荷数据。下面是一个用VISUAL STUDIO 2005的C++实现的用户控制器的实例。
2、C++代码
如何创建DLL可以参考《VC++.NET实现DLL并在其中调用》
下面是语言源代码:
Head 文件
//caiLib.h
#ifndef CAILIB_H
#define CAILIB_H
//extern "C" int _declspec(dllexport)add(int x, int y);
extern "C" //avoid mangled names
{ void __declspec(dllexport) __cdecl DISCON(float *avrSwap, int *aviFail,
char *accInfile, char *avcOutname, char *avcMsg);
}
void setParams(float *sAvrSwap);
int app(int x, int y);
#endif
头文件说明:
在其中定义程序名称。
程序代码
// : 定义DLL 应用程序的入口点。
//
#include "stdafx.h"
#include "caiLib.h"
#include "stdio.h"
#define NINT(a) ((a) >= 0.0 ? (int)((a)+0.5) : (int)((a)-0.5))
// 在此定义浮点数转换成整数
#ifdef _MANAGED
#pragma managed(push, off)
#endif
float *SwapArray, Kp, Ki; //定义与Bladed交换数组的地址指针,定义PI控制器比例 //及积分系数
static float sumError; //定义积分累积误
差变量
float GetSwapValue(int Index) { return(SwapArray[Index-1]); }
//定义读取Bladed传递变量值函数
void SetSwapValue(int Index, float Val) { SwapArray[Index-1] = Val; }
//定义设置传递到Bladed的变量
void __declspec(dllexport) __cdecl DISCON(float *avrSwap, int *aviFail,
char *accInfile, char *avcOutname, char *avcMsg)
{ //主函数DISCON实现
char Message[257], InFile[257], OutName[1025];
float rTime, rMeasuredSpeed, rMeasuredPitch, rGeneratorSpeed;
int iStatus, iFirstLog;
static int iStep, iDone, iControlState;
static float rPitchDemand, RPMSetpoint, TimeDiff, OldTime, Temp;
FILE *fp;
SwapArray = avrSwap; //Store the pointer
//Make sure there's a C string terminator
accInfile[NINT(avrSwap[49])+1] = '0';
avcOutname[NINT(avrSwap[50])] = '0';
avcMsg[0] = '0';
iStatus = NINT (avrSwap[0]); //Initialise if (iStatus == 0) { // Read the
data fp = fopen("", "r"); fscanf(fp, "%fn%fn", &Kp, &Ki); fclose(fp);
printf("Kp = %0.5f, Ki = %0.5fn", Kp, Ki); //Initialise pitch demand to current pitch angle
rMeasuredPitch = GetSwapValue(4); rPitchDemand = rMeasuredPitch; sumError = 0.0F;
iStep = 0; // Start up step = 0 TimeDiff = 0.0F; OldTime = GetSwapValue(2); //This is also
where you could read in any user-defined data from accInFile } //
Return demanded variables //Note: previous values can be used before they updated, to
simulate a one-sample delay if (NINT(GetSwapValue(10)) == 0) { SetSwapValue(42,
rPitchDemand); SetSwapValue(43, rPitchDemand); SetSwapValue(44, rPitchDemand);
SetSwapValue(45, rPitchDemand); } else { strcpy(avcMsg,"This simple
DLL needs pitch
position actuator"); *aviFail = -1; } rMeasuredPitch = GetSwapValue( 4 );
rGeneratorSpeed = GetSwapValue( 20 ) /( 2.0F * 3.14F )* 60.0F; //变换rad/s为 rpm
TimeDiff = GetSwapValue(2) - OldTime;
iControlState = NINT(GetSwapValue(117));
//获得Bladed 的控制状态
// iControlState = 0; production
// =1: Parked
// =2: idling
// =3: Start up
// =4: Normal Stop
// =5: Emergency Stop
//Main calculation
if (*aviFail >= 0)
{
//Just a 1-degree step change in pitch position demand at 10 seconds
if (GetSwapValue(2) >= 0.1 && iStep == 0 ) {
iStep = 1;
}
if ( iStep == 1 ){
rPitchDemand = 40*0.017453F;
if ( rGeneratorSpeed > 220.0 ){
RPMSetpoint = rGeneratorSpeed; // setpoint = Generator Speed
iStep = 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;
}
else RPMSetpoint = RPMSetpoint + 8.0F;
OldTime = GetSwapValue(2);
} // 按 8 rpm/s 增加发电机转速给定
}
if ( iStep == 3 ) {
Temp = PIController( rGeneratorSpeed, RPMSetpoint);
rPitchDemand = rPitchDemand + Temp;
}
if( rPitchDemand > 3.1415926 / 2)
rPitchDemand = 3.1415926F / 2.0F;
if( rPitchDemand < 0 ) rPitchDemand = 0.0F;
//Logging output
strcpy(avcOutname,"Generator Demand Speed:A/T;");
SetSwapValue(NINT(GetSwapValue(63)),RPMSetpoint*2. 0F*3.1415926F/60.0F);
SetSwapValue(65,1.0F);
}
return;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif 将程序编译后得到程序 .
将此程序在Bladed中的控制部分进行定义如图:
在其中定义程序的路径 d:
在External Controller Data中定义了2个参数,分别为Kp = 0.005和Ki = 0.002
点击OK。
在主计算中选择Start
点击Run Now 运行期间可以查看结果如下:
模拟运行结果如下:
上面是发电机转速给定值及发电机转速测量值和桨叶角度变化的实时图。
版权声明:本文标题:如何用C语言实现Bladed中的外部控制器 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1708833142h532315.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论