机械振动测量及球杆定位控制系统实验 联系客服

发布时间 : 星期四 文章机械振动测量及球杆定位控制系统实验更新完毕开始阅读

static void mdlStart(SimStruct *S) {

const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);

double fs=u[0]; //将Simulink仿真模型中的第一个常数值赋给采样频率fs

//采集卡初始化

// ADCardInit() 是采集卡DLL函数,作用是初始化采集卡。如果初始化成功则返 // 回1,用于判断是否连上采集卡。 if(ADCardInit()!=1) {

ssSetErrorStatus(S, \); }

// DAQ1(ch, Fs, len, buffer) 是采集卡DLL单通道采集函数,作用是启动采集卡 // 进行单通道数据采集。参数1为采样通道,是以8为二进制数表示各个通道是否 // 开启。参数2为采样频率,参数3为采样点个数,参数4为数据缓存数组。 DAQ1(0x1,fs,1024*4,buffer); //开启采集:对通道1采集数据 pnum=0; //初始化指针位置 }

#endif /* MDL_START */

/* Function: mdlOutputs ======================================================= * Abstract:

* In this function, you compute the outputs of your S-function

* block. Generally outputs are placed in the output vector, ssGetY(S). */

/* 读取buffer 数据和输出数据 */

static void mdlOutputs(SimStruct *S, int_T tid) {

int length=1024*4; //读取的buffer 的长度 // ReadDaq() 的作用是读取下位机buf 里的采样数据。其中,参数1为通道号,参数 // 2为buffer 长度,参数3为保存的buffer 指针。

ReadDaq(1,length,buffer); //从采集卡读取buffer // const real_T *u = (const real_T*) ssGetInputPortSignal(S,0); real_T *y = ssGetOutputPortRealSignal(S,0); //获取输出指针 // y[0] = u[0];

if(pnum>=length) pnum=0; //判断指针是否已满 *y=buffer[pnum]; //输出第pnum 个点的值 pnum++; //指针加1 }

9

#define MDL_UPDATE /* Change to #undef to remove function */ #if defined(MDL_UPDATE)

/* Function: mdlUpdate ====================================================== * Abstract:

* This function is called once for every major integration time step. * Discrete states are typically updated here, but this function is useful * for performing any tasks that should only take place once per * integration step. */

static void mdlUpdate(SimStruct *S, int_T tid) { }

#endif /* MDL_UPDATE */

#define MDL_DERIVATIVES /* Change to #undef to remove function */ #if defined(MDL_DERIVATIVES)

/* Function: mdlDerivatives ================================================= * Abstract:

* In this function, you compute the S-function block's derivatives. * The derivatives are placed in the derivative vector, ssGetdX(S). */

static void mdlDerivatives(SimStruct *S) { }

#endif /* MDL_DERIVATIVES */

/* Function: mdlTerminate ===================================================== * Abstract:

* In this function, you should perform any actions that are necessary * at the termination of a simulation. For example, if memory was * allocated in mdlStart, this is the place to free it. */

/* 退出采集卡 */

static void mdlTerminate(SimStruct *S) {

ADCardQuit(); //采集卡的DLL函数,作用是退出采集卡 }

/*======================================================*

10

* See sfuntmpl_doc.c for the optional S-function methods * *======================================================*/

/*=============================* * Required S-function trailer * *=============================*/

#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */ #include \ /* MEX-file interface mechanism */ #else

#include \ /* Code generation registration function */ #endif

2、M代码及注释

我们设计的“机械振动测量”界面共包括四个M文件,分别是jixiezhendong.m, getdata.m, callradio1.m, callradio2.m。

(1)jixiezhendong.m 文件代码及注释

该文件的主要功能是生成“机械振动测量”的界面,包括两个轴对象、两个按钮控件、两个无线电选择控件、两个编辑框控件以及一些静态文本框等,通过对这些对象的定义和属性的设置,能够生成“机械振动测量”的界面,如下图所示。

11

同时,该文件中还包括控件的一些回调属性的定义。比如点击“开始”按钮开启定时器开始计时,并运行getdata.m文件;点击“停止”按钮停止计时;选择“原始信号”或“低通滤波”可分别开启定时器,并运行callradio1.m或callradio2.m文件。

以下是jixiezhendong.m文件的代码及注释

clf reset; %清除图像窗口当前图像,重置所有对象设置 set (gcf,'menubar','none'); %去掉窗口中的菜单栏 set(gcf,'unit','normalized','position',[0.05,0.2,0.62,0.75]);

%设置窗口在屏幕中的显示位置和窗口大小

set(gcf,'defaultuicontrolunits','normalized'); %设置用户缺省控件单位属性值 set(gcf,'defaultuicontrolfontname','楷体'); %设置用户缺省控件字体

set(gcf,'defaultuicontrolfontsize',13); %设置用户缺省控件字体大小 set(gcf,'NumberTitle','off','Name','机械振动测量'); %设置图形窗口的名称

h_axes1=axes('position',[0.1,0.53,0.56,0.3]); %定义轴位框位置 axis([0 0.094 -1000 1000]); %设置坐标轴的范围 title('传感器输出波形'); %坐标轴命名

xlabel('时间(s)'); %定义x轴的标签名 ylabel('电压(mv)'); %定义y轴的标签名 h_axes2=axes('position',[0.1,0.1,0.56,0.3]); %定义轴位框的位置 title('频谱分析图谱'); %坐标轴命名

xlabel('频率(Hz)'); %定义x轴的标签名 ylabel('电压(mv)'); %定义y轴的标签名

hpush1=uicontrol(gcf,'style','push',... %创建“开始”按钮 'position',[0.83,0.8,0.1,0.05],... %按钮的位置

'string','开始',... %按钮的文字标识

'fontweight','bold'); %按钮字体为加粗

hpush2=uicontrol(gcf,'style','push','position',[0.83,0.73,0.1,0.05],'string','停止','fontweight','bold'); %创建“停止”按钮

hr1=uicontrol(gcf,'style','radio','position',[0.82,0.48,0.12,0.05],'string','原始信号','fontweight','bold'); %创建“原始信号”单选按钮

hr2=uicontrol(gcf,'style','radio','position',[0.82,0.44,0.12,0.05],'string','低通滤波','fontweight','bold'); %创建“低通滤波”单选按钮

hedit1=uicontrol(gcf,'style','edit','position',[0.83,0.17,0.1,0.05],'fontweight','bold'); %创建“振动频率”编辑框

hedit2=uicontrol(gcf,'style','edit','position',[0.83,0.1,0.1,0.05],'fontweight','bold'); %创建“振动加速度”编辑框

12