版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 转速测量及工作台实验班级:人员:实验项目:工作台实验、转速测量实验时间:2012年6月目 录1、 工作台实验1.1 实验目的- 31.2 实验原理- 31.3 实验代码解析- 51.4 实验结果展示及分析-141.5 实验小结及感想-152、 转子实验台转速测量实验2.1 实验目的-162.2 实验原理-162.3 实验内容及设计过程-162.4 实验代码解析-202.5 实验结果展示及分析-282.6 实验小结及感想-311.1实验目的 实验平台对软件的要求,需要安装Matlab version和Microsoft Visual c+6.0 。MATLAB是目前在国际上被广泛接受和使用的科
2、学与工程计算软件。虽然Cleve Moler教授开发它的初衷是为了更简单、更快捷地解决矩阵运算,但 MATLAB现在的发展已经使其成为一种集数值运算、符号运算、数据可视化、图形界面设计、程序设计、仿真等多种功能于一体的集成软件。通过用matlab对实验工作台进行快进、工进、快退的控制,学习matlab在控制方面的知识。学习matlab种simulink函数的运用。2.2 实验原理运用到的主要simulink模块的介绍。驱动电机运动的运动指令代码都是在VC下面编写的,实验之前已经有了用VC做了一些s-function并生成*.dll文件(使用s-function example模板编写好c文件之
3、后再用mex将其编译成dll文件),包括InitOpenIPpos.dll,GetCurPos.dll,SetPara.dll ,下面依次对部分s-function作出简要说明。 InitOpenIPpos.dll初始化运动控制卡,其功能主要是使伺服上电并且设定电机的一些参数,如:运动模式(速度,位置,模式等),最大速度值,最大加速度值等等。以上这些都已经在InitOpenIP.c文件里面设定好了,可以打开这个文件查看。比如,我们根据实际情况设定电机的运动模式为位置模式(zl_set_prfl_ pos (0))。GetCurPos.dll读取当前轴的位置。如果某个simulink模块调用了G
4、etCurPos.dll文件后,在设置参数时需要注意,parameters项填写“1”是代表当前读取的是1号轴即电机轴的位置,如果填写的是“3”则是代表立摆的转动轴,此时读出的是倒立摆的摆角位移。SetPara.dll设置各种不同运动模式时的运动参数。(1) 仅实现手动的快进及快退功能。要实现快进、工进、快退功能,首先可以从较简单的功能实现开始,因此我们选取了最先实现快进、快退功能来开始试验,下图是实现该功能的原理图:手动切换开关可实现工作台的快进、快退。(2)实现快进、工进及手动的快退功能。执行程序后,工作台开始自动快进,到达预定位置后减速工进,随后手动换开关Manual Switch,实现
5、快退。(3)实现自动地快进、工进和快退。执行程序后,工作台开始自动快进,到达预定位置后减速工进,工进一段时间后达到预定位置,switch自动换向,实现快退。1.3实验代码解析(1)s-function setpara代码#define S_FUNCTION_NAME SetPara#define S_FUNCTION_LEVEL 2 /*定义函数*/#include "simstruc.h"#include "ZLPCIDrv.h"#include "ZLPCI400d.h"#include "PendX.h"/*
6、添加头文件*/#pragma comment(lib, "ZLPCIDrv.lib")#pragma comment(lib, "ZLPCI400d.lib")#pragma warning( disable: 4761 )#pragma warning( disable: 4244 )/*预定义的标识符,指定注释的类型,指定库函数*/-/*=* * S-function methods * *=*/#define ERR_INVALID_PARAM_1 "Invalid axis number. The first parameter mus
7、t be n an unsigned integer number between 1 and 4."#define ERR_INVALID_PARAM_2 "Invalid number of input ports. The second parameter must be n an unsigned integer number between 2 and 5."#define AXIS_NUM (ssGetSFcnParam(S, 0)#define NUM_INPUTS (ssGetSFcnParam(S, 1)/*定义输入值*/#define MDL_
8、CHECK_PARAMETERSstatic void mdlCheckParameters(SimStruct *S) uint8_T axis_num; int_T inputs; axis_num = (uint8_T) *(mxGetPr(AXIS_NUM); if( axis_num < 1 | axis_num > 4 ) ssSetErrorStatus(S, ERR_INVALID_PARAM_1); return; /*AXIS_NUMD 的有效范围*/ inputs = (int_T) (*(mxGetPr(NUM_INPUTS); if( inputs <
9、; 2 | inputs > 5 ) ssSetErrorStatus(S, ERR_INVALID_PARAM_2); return; /* NUM_INPUTS 的有效范围*/* Function: mdlInitializeSizes = */static void mdlInitializeSizes(SimStruct *S)int i; int_T inputs; ssSetNumSFcnParams(S, 2); if (ssGetNumSFcnParams(S) = ssGetSFcnParamsCount(S) mdlCheckParameters(S); if (ss
10、GetErrorStatus(S) != NULL) return; else return; /* Simulink will report a parameter mismatch error */ inputs = (int_T) (*(mxGetPr(NUM_INPUTS); if (!ssSetNumInputPorts(S, inputs) return; for(i=0; i<inputs; i+) ssSetInputPortWidth(S, i, 1); ssSetInputPortDirectFeedThrough(S, i, 1); if (!ssSetNumOut
11、putPorts(S, 1) return; ssSetOutputPortWidth(S, 0, 1); ssSetNumSampleTimes(S, 1); /* Take care when specifying exception free code - see sfuntmpl_doc.c */ ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);/* Function: mdlInitializeSampleTimes = * Abstract: Specifiy that we inherit our sample time from t
12、he driving block. */static void mdlInitializeSampleTimes(SimStruct *S) ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME); ssSetOffsetTime(S, 0, 0.0);/ Function: mdlOutputs = static void mdlOutputs(SimStruct *S, int_T tid) InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0); real_T *y = ssGetOutpu
13、tPortRealSignal(S,0);unsigned shortaxis_status, inputs; unsigned short axis_num; /* set the current axis number as defined by S_function first parameter */ axis_num = (uint8_T) *(mxGetPr(AXIS_NUM); inputs = (uint8_T) (*(mxGetPr(NUM_INPUTS);if(inputs >= 2) zl_set_vel(*uPtrs0,axis_num-1);zl_set_acc
14、(*uPtrs1,axis_num-1);if(inputs >= 3) zl_set_pos(long)*uPtrs2,axis_num-1);if(inputs >= 4) zl_set_max_acc(*uPtrs3,axis_num-1);if(inputs >= 5) return;/update card parameters zl_update(axis_num-1); zl_get_status(&axis_status,axis_num-1); /axis_status = 16 & axis_status; /*y = (axis_stat
15、us ? 1 : 0); *y=axis_status&127;/* Function: mdlTerminate = * Abstract: * No termination needed, but we are required to have this routine. */static void mdlTerminate(SimStruct *S)#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */#include "simulink.c" /* MEX-file in
16、terface mechanism */#else#include "cg_sfun.h" /* Code generation registration function */#endif(2) s-function GetCurPos代码#define S_FUNCTION_NAME GetCurPos#define S_FUNCTION_LEVEL 2/*定义函数*/#include "simstruc.h"#include "ZLPCIDrv.h"#include "ZLPCI400d.h"#include
17、 "PendX.h"/*添加头文件*/#pragma comment(lib, "ZLPCIDrv.lib")#pragma comment(lib, "ZLPCI400d.lib")#pragma warning( disable: 4761 )#pragma warning( disable: 4244 )/*预定义的标识符,指定注释的类型,指定库函数*/-#define ERR_INVALID_PARAM "Invalid axis number. The parameter must be n an unsigned
18、 integer number between 1 and 4."/* total number of block parameters */enum PARAM = 0, NUM_PARAMS;#define AXIS_NUM (ssGetSFcnParam(S, 0)#define MDL_CHECK_PARAMETERSstatic void mdlCheckParameters(SimStruct *S) uint8_T axis_num; axis_num = (uint8_T) *(mxGetPr(AXIS_NUM); if( axis_num < 1 | axis
19、_num > 4 ) ssSetErrorStatus(S, ERR_INVALID_PARAM); return; /* Function: mdlInitializeSizes = */static void mdlInitializeSizes(SimStruct *S) ssSetNumSFcnParams(S, 1); if (ssGetNumSFcnParams(S) = ssGetSFcnParamsCount(S) mdlCheckParameters(S); if (ssGetErrorStatus(S) != NULL) return; else return; /*
20、 Simulink will report a parameter mismatch error */ if (!ssSetNumInputPorts(S, 0) return; if (!ssSetNumOutputPorts(S,1) return; ssSetOutputPortWidth(S, 0, 1); ssSetNumSampleTimes(S, 1); /* Take care when specifying exception free code - see sfuntmpl_doc.c */ ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_
21、CODE);/* Function: mdlInitializeSampleTimes = * Abstract: Specifiy that we inherit our sample time from the driving block. */static void mdlInitializeSampleTimes(SimStruct *S) ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME); ssSetOffsetTime(S, 0, 0.0); / Function: mdlOutputs =static void mdlOutputs(Si
22、mStruct *S, int_T tid) real_T *y = ssGetOutputPortRealSignal(S,0); uint8_T axis_num; long data; /* set the current axis number as defined by S_function first parameter */ axis_num = (uint8_T) *(mxGetPr(AXIS_NUM); /* get position of axis that is defined by S-func parameter */zl_get_actl_pos(&data
23、,axis_num-1); *y = data;/* Function: mdlTerminate = * Abstract: * No termination needed, but we are required to have this routine. */static void mdlTerminate(SimStruct *S)#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */#include "simulink.c" /* MEX-file interface mech
24、anism */#else#include "cg_sfun.h" /* Code generation registration function */#endifs-fuction函数 InitOpenIPpos#define S_FUNCTION_NAME InitOpenIPpos#define S_FUNCTION_LEVEL 2/*定义函数*#include "simstruc.h"#include "ZLPCIDrv.h"#include "ZLPCI400d.h"#include "Pen
25、dX.h"/*添加头文件*/#pragma comment(lib, "ZLPCIDrv.lib")#pragma comment(lib, "ZLPCI400d.lib")#pragma warning( disable: 4761 )#pragma warning( disable: 4244 )/*预定义的标识符,指定注释的类型,指定库函数*/* Function: mdlInitializeSizes = */static void mdlInitializeSizes(SimStruct *S) ssSetNumSFcnParams(
26、S, 0); if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S) return; /* Parameter mismatch will be reported by Simulink */ if (!ssSetNumInputPorts(S, 1) return; ssSetInputPortWidth(S, 0, 1); ssSetInputPortDirectFeedThrough(S, 0, 1); if (!ssSetNumOutputPorts(S,0) return; ssSetNumSampleTimes(S, 1); /*
27、Take care when specifying exception free code - see sfuntmpl_doc.c */ ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);/* Function: mdlInitializeSampleTimes = * Abstract: Specifiy that we inherit our sample time from the driving block. */static void mdlInitializeSampleTimes(SimStruct *S) ssSetSampleTi
28、me(S, 0, INHERITED_SAMPLE_TIME); ssSetOffsetTime(S, 0, 0.0);#define MDL_START /* Change to #undef to remove function */#if defined(MDL_START) /* Function: mdlStart = * Abstract: * This function is called once at start of model execution. If you * have states that should be initialized once, this is
29、the place * to do it. */ static void mdlStart(SimStruct *S) InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0); /performing all necessary initializations ZLLoadDriver();zl_board_reset();Sleep(200);zl_fifo_reset();Sleep(200);zl_set_smpl_time(400);zl_set_kp(0.1,0); zl_set_ki(0.0,0);zl_set_kd(
30、0.0,0);zl_set_kvff(0,0);zl_pid_update(1); zl_set_prfl_pos(0);zl_set_max_acc(7500000,0);zl_set_max_vel(500000,0);zl_set_pos_lm(900000000,0);zl_set_neg_lm(-900000000,0); zl_set_pos_err_lm(32767,0);zl_set_vel(0, 0);zl_set_acc (5000000, 0);zl_update(0);zl_axis_on(0);zl_motor_enable(1); #endif /* MDL_STA
31、RT */ Function: mdlOuts = static void mdlOutputs(SimStruct *S, int_T tid)/* Function: mdlTerminate = * Abstract: * No termination needed, but we are required to have this routine. */static void mdlTerminate(SimStruct *S)zl_motor_enable(0); zl_axis_off(0);ZLUnloadDriver();#ifdef MATLAB_MEX_FILE /* Is
32、 this file being compiled as a MEX-file? */#include "simulink.c" /* MEX-file interface mechanism */#else#include "cg_sfun.h" /* Code generation registration function */#endif1.4实验结果展示及分析快进速度100000,工进速度30000,switch的切换由完成,切换为位移300000(如下图1、2) 图1 图2由工进到快退由有图switch2及控制,(如下图1、2)当位移大于40
33、0000时由工进切换为快退,快退速度为200000,方向由下图3控制图1 图2 图3因此总体控制流程图为:1.5实验小结及感想通过工作台控制实验是一项比较综合的控制实验,包括快进,工进,快退的控制,通过实验了解了matlab的基本用法和simulink函数的用法,学到了知识。同时也让我养成了遇到问题就查阅资料的习惯。2.1 实验目的 S-FUNCTION是simulink模块的计算机语言描述。通过S-FUNCTION,用户可以将自己的模块加入到simulink模型中,从而实现用户自定义的算法或者与硬件的交互。通过此次试验目的在于:1、 学习掌握用matlab编程,绘制采样数据所得图形,并进行图
34、形分析。2、自学matlab编程语言,提升自主学习能力与查阅资料的能力。2.2 实验原理通过转子试验台转子的转动,用磁电传感器测出转子的转速,并将转动信号通过通道发送到计算机中。在界面中显示转动状态,波形,以及将转动参数显示到自己编写的界面之中。测量出转速并显示给用户。建立s-function函数,向simulink提供模型信息,在仿真中simulink,ode求解器,mex-file协作完成指定的任务。2.3 实验内容及过程(一)1、利用s-function模版创建DLL文件。在matlab中新建文件夹,在simulink中选择如图1的模块功能,生成重命名文件,存入指定文件夹。321 图13
35、、 生成的s-function源文件:生成的s-function函数为一个基本的原函数。在mdlInitializeSizes(SimStruct *S)中修改将real_T *y = ssGetOutputPortSignal(S,0)改为real_T*y=ssGetOutputPortRealSignal(S,0);/ssGetOutputPortSignal(S,0);4、 新建mdl文件,添加s-function模块,将其中的s-function改为dll的文件名(myon)5、 添加常数项和to workspace项,保存该dll文件。6、多个输入输出设置:在mdlInitializ
36、eSizes中,修改输入语句为:if (!ssSetNumInputPorts(S, 3) return; ssSetInputPortWidth(S, 0, 1); ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/ ssSetInputPortWidth(S, 1, 1); ssSetInputPortRequiredContiguous(S, 1, true); /*direct input signal access*/ ssSetInputPortWidth(S, 2, 1); ss
37、SetInputPortRequiredContiguous(S, 2, true); /*direct input signal access*/ /* * Set direct feedthrough flag (1=yes, 0=no). * A port has direct feedthrough if the input is used in either * the mdlOutputs or mdlGetTimeOfNextVarHit functions. * See matlabroot/simulink/src/sfuntmpl_directfeed.txt. */ ss
38、SetInputPortDirectFeedThrough(S, 0, 1); ssSetInputPortDirectFeedThrough(S, 1, 1); ssSetInputPortDirectFeedThrough(S, 2, 1);7、标注输入输出设置:三个接口没有任何,这样我们就不清楚道理每个接口作用。为此,我们可以在图4.10所示的模块上单击右键,然后选择Mask S-Function, 8、添加控制卡的.h和.lib文件,如图9、采集卡初始化及开启数据采集 在该cpp文件mdlStart(SimStruct *S)函数中添加采集卡初始化以及开启采集任务。 const rea
39、l_T *u1 = (const real_T *)ssGetInputPortRealSignalPtrs(S,0); const real_T *u2 = (const real_T *)ssGetInputPortRealSignalPtrs(S,1); const real_T *u3 = (const real_T *)ssGetInputPortRealSignalPtrs(S,2); double Fs=u10; /采样频率 if( ADCardInit() != 1 ) /采集卡初始化 ssSetErrorStatus(S,"Can't find the DA
40、QCard!"); DAQ1(0x1, 5000, 1024*4 , buffer);/开启采集Fs=5000 pnum=0;/初始化buf指针位置 if( u20 = 1 ) DAQ1(0x1, Fs, 1024 , buffer); else if(u20 = 2 ) DAQ1(0x2, Fs, 1024 , buffer); else if(u20 = 3 ) DAQ1(0x4, Fs, 1024 , buffer); else if(u20 = 4 ) DAQ1(0x8, Fs, 1024 , buffer); else if(u20 = 5 ) DAQ1(0x10, Fs,
41、 1024 , buffer); else if(u20 = 6 ) DAQ1(0x20, Fs, 1024 , buffer); else if(u20 = 7 ) DAQ1(0x40, Fs, 1024 , buffer); else if(u20 = 8 ) DAQ1(0x80, Fs, 1024 , buffer); else ssSetErrorStatus(S,"channel number is wrong!");ADCardInit()是采集卡DLL函数,作用是初始化采集卡。如果初始化成功则返回1,用于判断是否连上采集卡。 DAQ1()函数为采集卡DLL单通
42、道采样函数,作用是启动采集卡进行单通道数据采集。参数1为采样通道,是以8位2进制数表示各个通道是否开启,如对通道1进行采集,该参数即为:0000 0001;同理,对3通道进行采集,其参数为 0000 0100。通道5对应为0x1010、数据输出 数据输出在mdlOutputs()函数内实现。 int Length=1024*4;/读取的buffer长度 const real_T *u2 = (const real_T *)ssGetInputPortRealSignalPtrs(S,1); ReadDaq(u20,Length, buffer);/从采集卡读取buffer real_T *y
43、= ssGetOutputPortRealSignal(S,0);/获取输出指针 if( pnum >= Length ) pnum=0;/判断指针是否已满 *y = bufferpnum;/输出第pnum个点的值 pnum+;/指针加1 函数ReadDaq()作用是读取下位机buf里的采样数据。 参数1为通道号,分别为1-8; 参数2为buffer长度; 参数3为保存的buffer指针。 11、退出采集卡函数ADCardQuit()是采集卡dll函数,其作用是退出采集卡。static void mdlTerminate(SimStruct *S) ADCardQuit();再次在MAT
44、LAB的Command Window中输入mex MyOn.cpp,完成数据采集卡的程序编写。打开测试实验平台上的直流电源,打开通道1的信号源,返回使用该模块形成的mytemp1.mdl文件,将运行时间改为inf,点击菜单栏上的“保存”图标,点击菜单栏上的“运行”图标,进行执行。双击Scope可以查看采集的信号波形。(二)Gui设计过程1、调用界面设计clf resetset(gcf,'menubar','none')set(gcf,'unit','normalized','position',0.2,0.2,0.
45、5,0.35);set(gcf,'defaultuicontrolunits','normalized')%设置用户缺省控件单位属性值h_axes1=axes('position',0.05,0.55,0.6,0.4); h_axes2=axes('position',0.05,0.05,0.6,0.4); %两轴对象和两个按钮对象 hpush1=uicontrol(gcf,'Style','push',.%制作“采集”按钮 'position',0.8,0.25,0.18,0.15
46、,'string','采集'); hpush2=uicontrol(gcf,'Style','push',.%制作“停止”按钮 'position',0.8,0.05,0.18,0.15,'string','停止');2、设置回调属性%首先运行mytemp1mdl文件%“采集”按键引起的回调 set(hpush1,'callback',. 'set_param(''mytemp1'',''SimulationCo
47、mmand'',''start''),',.'t=timer(''TimerFcn'',''getdata'',''Period'',1,''ExecutionMode'',''fixedSpa cing'',''TasksToExecute'',inf),',.%设置定时器,运行getdata文件,与上面是一行。'pause
48、(2);start(t);',.% 暂停2秒并启动定时器);%“停止”按键引起的回调hpush2=uicontrol(gcf,'Style','push',. 'position',0.8,0.05,0.18,0.15,'string','停止'); %制作“停止”按钮set(hpush2,'callback','stop(t);set_param(''mytemp2'',''SimulationCommand'',''stop'');');%停止定时器 ,mdl文件停止运行2.4实验代码解析(1)MyOn.cpp/* * sfuntmpl_basic.c: Basic 'C' template
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030中国茶叶礼品盒行业二手市场现状与循环经济模式探索
- 2025-2030中国茶叶礼品盒电商渠道拓展与数字化营销趋势分析报告
- 2025-2030中国茶叶礼品盒出口贸易现状与国际市场拓展路径分析
- 2025-2030中国茶叶礼品盒产业集群发展比较优势与协同机制
- 2025-2030中国腊味行业社交媒体营销效果评估
- 脑白质病的分子机制解析
- 2026年高一(中国民歌)学唱与风格测试题
- 2026湖北咸宁麻塘中医医院招聘备考题库含答案详解(综合题)
- 2026湖南长沙电力职业技术学院非事业编制学历教育教师及辅导员岗位校园招聘18人备考题库附答案详解(研优卷)
- 2026云南昆明市官渡街道官渡古镇文化旅游建设项目征地拆迁工作指挥部招聘3人备考题库含答案详解(满分必刷)
- 行政管理专业毕业论文提纲
- 2026年长春汽车职业技术大学单招综合素质考试必刷测试卷汇编
- 2025火电企业固废综合利用平台
- DB11∕T 1567-2018 森林疗养基地建设技术导则
- 食堂购买蔬菜合同(标准版)
- 五六年级女生青春期健康讲座内容
- 境外旅游保险知识培训课件
- 实验室实验员述职报告
- 医院环境清洁消毒与监测
- DB44∕T 2331-2021 公路混凝土桥梁火灾后安全性能评定技术规程
- 河南洛阳产融集团有限公司招聘笔试题库2025
评论
0/150
提交评论