1、1,虚拟仪器开发环境,2,虚拟仪器的概念,所谓虚拟仪器(Virtual Instrument,简称VI)是指以通用计算机为核心的硬件平台,配以相应测试功能的硬件作为输入/输出的接口,利用仪器软件开发平台在计算机的屏幕上虚拟出仪器的面板和相应的功能,然后通过鼠标或键盘操作的仪器。软件是构成虚拟仪器的核心,可以说软件就是仪器。,3,May 2003January 2002August 2000March 1998February 1996August 1993September 1992January 1990October 1986April 1983, LabVIEW 7 Express VI
2、s, I/O Assistants, FPGA/PDA targetsLabVIEW 6.1 Enhanced networking capabilities, analysis LabVIEW 6i Internet-ready measurement intelligence LabVIEW 5.0 ActiveX, Multithreading LabVIEW 4.0 Added professional tools, improved debugging LabVIEW 3.0 Multiplatform version of LabVIEW LabVIEW for Windows L
3、abVIEW 2.0 for Macintosh LabVIEW 1.0 for Macintosh LabVIEW project begins,NI LabVIEW: A History of Innovation,4,PC, Mac, Linux, Sun,LabVIEW Everywhere,Networked I/O,PC Boards,Workstation,Handheld,Embedded (FPGA),Industrial Computer (PXI),Wireless,Sensor,Tektronix Open Windows Oscilloscopes,5,The Lab
4、VIEW Family,NI LabVIEW Graphical Programming Software for Measurement and Automation,LabVIEW Real-Time Module,LabVIEW FPGA Module,LabVIEW PDA Module,LabVIEW Datalogging and Supervisory Control Module,6,采集 分析 显示,LabVIEW可以方便的实现采集、分析、显示,7,用LabVIEW实现采集,LabVIEW 应用以下器件实现采集 GPIB, Serial, Ethernet, VXI, PXI
5、 Instruments Data Acquisition (DAQ) PCI eXtensions for Instrumentation (PXI) Image Acquisition (IMAQ) Motion Control Real-Time (RT) PXI PLC (through OPC Server) PDA Modular Instruments,LabVIEW 与NI公司的硬件及其他厂商的硬件接口,8,用LabVIEW分析,LabVIEW 包含: 超过400个分析函数用于差分方程、最优化、曲线拟合、微分、线性代数、统计等 12 个快速VI用于滤波和频谱分析 滤波、加窗、变
6、换、峰值检测、谐波分析、频谱分析等信号处理函数 Matlab编写的文件可以在LabVIEW程序中应用,LabVIEW中有丰富的信号处理函数,9,用LabVIEW显示,LabVIEW 包含以下工具帮助我们显示数据: 计算机上:图形、图表、大罐、图片控制、3D图形、报告等 网络 网页发布工具, Datasocket (Windows Only), TCP/IP, VI Server, Remote Panels, Email Enterprise Connectivity Toolset SQL Tools (Databases), Internet Tools (FTP, Telnet, HTM
7、L),10,内容,LabVIEW简介,结构,模块化编程,定制VI,数据采集和波形,仪器控制,数组,图表,簇,选择,字符串和文件I/O,11,本章目标,理解前面板、方框图、连接器/图标 会用LabVIEW的结构和数据类型 编辑和调试VI的方法 创建子程 显示和记录数据 用DAQ卡创建应用程序,12,课程不讨论的内容,LabVIEW内嵌对象、函数 Analog-to-digital (A/D) 理论 串口或 GPIB总线的详细操作 创建仪器驱动程序,13,虚拟仪器程序Virtual Instruments (VIs),Front Panel 前面板 Controls = Inputs 控制 Ind
8、icators = Outputs 指示Block Diagram 程序框图 Accompanying “program” for front panel 与前面板对应 Components wired together 对象连接在一起 控制程序的代码,14,LabVIEW 对话框,15,创建一个新的VI,FileNew VI to open a blank VIFileNew to open the New dialog box and configure a VI template, global variable, control, etc,16,“新建” 对话框,17,Menu,Fil
9、e Edit Operate Tools Browse Window Help,18,前面板,Front Panel Toolbar,Graph Legend,Boolean Control,Waveform Graph,Icon,Plot Legend,Scale Legend,Waveform Graph Owned Label,19,程序框图窗口 Block Diagram Window,Wire Data,Graph Terminal,SubVI,While Loop Structure,Block Diagram Toolbar,Divide Function,Numeric Con
10、stant,Timing Function,Boolean Control Terminal,20,前面板和程序框图工具栏,Run button Continuous Run button Abort button Pause/Continue button,Execution Highlighting button Step Into button Step Over button Step Out button,Warning indicatorEnter buttonBroken Run button,Font ring Alignment ring Distribution ring
11、Resize ring Reorder ring Context Help Button,Additional Buttons on the Block Diagram Toolbar,21,工具选板 Tools Palette,LabVIEW 自动选择所需工具 在前面板和程序框图窗口出现 修改前面板和程序框图的对象 显示工具模板 select WindowShow Tools Palette LabVIEW8.2中 查看工具选板,22,前面板 Front Panel Controls Palette,控件模板Controls Palette 包含常用的控件,All Controls Pale
12、tte Shows all controls,23,程序框图Block Diagram Functions Palette,函数模板Functions Palette 包含快速VI和常用的函数 Contains the Express VIs (interactive VIs with configurable dialog page) and the most commonly used functions,All Functions Palette Shows all functions,24,模板工具Palette Tools,图形化Graphical,浮动模板 floating pal
13、ettes 可以把子模板放在最上面Subpalettes can be converted to floating palettes 可以改变模板的显示方式Use Palette Options to change palette view from Express to Advanced,Search,Palette Options,大头针,Up to Owning Palette,25,查找Searching for Controls, VIs, and Functions,可以查找控件Press the search button to perform text searches of
14、the palettes 可以直接把查找到的控件放在框图中,或者打开控件所属的模板Click and drag an item from the search window to the block diagram or double-click an item to open the owning palette,26,定制Customize Control & Function Palette,Keep vi.lib in the LabVIEW 7.0 directory Place items in user.lib or instr.lib to have them appear i
15、n the Controls and Functions palettes,Programs National InstrumentsLabVIEW 7.0,27,前面板设计-程序框图设计-调试,前面板设计包括输入和输出两个部分 获得输入的方法有以下几种: 从设备获取 从文件直接读取 从输入控件读取 输出数据的方法有以下几种: 输出到显示控件 记录到文件 输出到设备,28,A.前面板设计-标签/标题,标签-短描述 标题-长描述标题不会在程序框图中出现,29,A.前面板设计-颜色使用技巧,以灰色配置开始 选择一种灰度或两种稍有不同的灰度 尽可能少用高亮色,高亮色一般只用于一些重要的设置,如图表、
16、中止按钮、或者滑动杆上的滑块,30,A.前面板设计-间隔和对齐,31,A.前面板设计-文本和字体,杂乱无章的应用范例:,32,A.前面板设计-选项卡控件,选项卡控件用于将前面板的输入控件和显示控件重叠放置在一个较小的区域内,33,A.前面板设计-修饰,使用修饰控件,如方框、线条、箭头等,来组合或分隔前面板上的对象 这些对象仅作修饰之用,34,Build the front panel with controls (inputs) and indicators (outputs),Numeric Control,Numeric Indicator,Owned LabelsIncrement Bu
17、ttons,Boolean Control,Boolean Indicator,创建前面板Creating a VI Front Panel,35,快捷菜单 Shortcut Menus for Front Panel Objects,Right-click the label to access its shortcut menu,Right-click the digital display to access its shortcut menu,36,属性页Property Page,每个控件有属性页,不同控件属性不同 Right-click a control or indicator
18、 on the front panel and select Properties from the shortcut menu to access the property dialog box for that object,37,Nodes节点,Wires连线,Control Terminals端点,Block Diagram,Front Panel,Indicator Terminals,B创建框图程序 Creating a VI Block Diagram,38,节点 Express VIs, VIs and Functions,Express VIs: interactive VI
19、s with configurable dialog page Standard VIs: modularized VIs customized by wiring Functions: fundamental operating elements of LabVIEW; no front panel or block diagram,39,节点 Block Diagram Nodes,Icon Expandable Node Expanded Node,Function Generator VI 同一个VI有三种形式显示 黄色表示标准VI 蓝色表示快速VI,40,端点 Block Diagr
20、am Terminals,端点是前面板和框图程序交换信息的端口可理解为传统仪器的接线柱 端点:文本语言中的参数和常数 右击可改变显示方式包括 控件端点 节点端点 结构端点 常数,41,连线 Wiring the Block Diagram,Scalar,Numeric,Boolean,String,2D Array,1D Array,Dynamic,42,连线技术 Wiring Techniques,Hot Spot,自动连线Automatic Wiring Use Context Help Window when wiring Right-click wire and select Clea
21、n Up Wire 自动连线路径Automatic wire routing Right-click terminals and select Visible ItemsTerminals,View the terminal connections to a function,43,数据类型接线端,接线端直观地体现了其传递的数据类型,44,数据类型数值型,数值数据类型用来表示各种 不同类型的数字 右键单击输入控件、显示控件或常量并从快捷菜单中选择表示法即可改变数字的表示类型,45,数据类型布尔型,机械动作是布尔型控件的指定行为 在LabVIEW中,用绿色代表布尔数据类型,46,数据类型字符串,
22、可显示的或不可显示的 ASCII字符序列 在前面板上,字符串以表格、 文本输入框和标签的形式出现 从快捷菜单中改变显示类型:正常显示、/代码显示、十六进制显示和密码显示 在程序框图上,用字符串函数来编辑和操作字符串 在LabVIEW中,用粉红色代表字符串数据类型,47,数据类型枚举型,一个枚举数可以 代表一对数值, 如一个字符串和 一个数值型数字, 而枚举数又可作 为一组数值中的 一个数值,48,数据类型枚举型,枚举数:枚举型输入 控件、枚举型常量或 枚举型显示控件 枚举数非常有用,因为在程序框 图上处理数字要比处理字符串简 单得多,49,数据类型动态数据类型,动态数据类型用以保存由Expre
23、ssVI所产生或采集的信息 非Express VIs不能接收动态数据类型 如需使用内置VI或函数来分析或处理包含有动态数据类型的数据,必须先进行类型转换 当有连线时,数值、波形或布尔数据的显示控件或输入端可以自动地转换动态数据类型 在LabVIEW中,用深蓝色代表动态数据类型,50,代码说明前面板,提示框 说明 VI属性 良好的设计风格,51,代码说明命名,用一个合理的起描述作用的名称给输入控件和显示控件命名可以增强前面板的可用性,52,代码说明程序框图,用程序框图说明来: 描述算法 说明连线间的数据内容 使用标签工具或从函数选板上 放置一个自由标签,53,执行顺序是数据流Block diag
24、ram executes dependent on the flow of data; block diagram does NOT execute left to right 当节点上所有的输入都有值的时侯节点开始运行Node executes when data is available to ALL input terminals 节点运行后输出到各个端点Nodes supply data to all output terminals when done,数据流编程Dataflow Programming,54,调试技术Debugging Techniques,查找错误Finding
25、ErrorsClick on broken Run button. A window showing the error appears 高亮运行Execution HighlightingClick on Execution Highlighting button; data flow is animated using bubbles. Values are displayed on wires.,55,错误连线,56,调试技术Debugging Techniques,ProbeRight-click on wire and select probe and it shows data a
26、s it flows through the wire segment BreakpointsRight-click on wire and select Set Breakpoint; pause execution at the breakpoint. Conditional ProbeCombination of a breakpoint and a probe. Right-click on wire and select custom probe.,57,调试技术Debugging Techniques,Step Into, Over, and Out buttons for Sin
27、gle Stepping,Click on Step Into button to enable single stepping Once Single Stepping has begun, the button steps into nodesClick on Step Over button to enable single stepping or to step over nodesClick on Step Out button to step out of nodes,58,对象的基本功能信息帮助 Context Help,To display the Context Help w
28、indow, select HelpShow Context Help, press the keys, or press the Show Context Help Window button in the toolbar Move cursor over object to display help Connections: Required bold Recommended normal Optional - dimmed,Simple/Detailed Context Help Lock Help More Help,59,LabVIEW Help,Click the More Hel
29、p button in the Context Help window Select HelpVI, Function, & How-To Help Click the sentence Click here for more help in the Context Help window.LabVIEW帮助资源丰富,60,NI Example Finder,To find an example, select HelpFind Examples 查找范例 Web-integrated Search by keyword, example type, hardware type, etc. 通
30、过关键词、例子类型、硬件类型来查找例子,61,小结,1个VI包括三部分 前面板, 框图程序, 图标/连接端口 前面板:用户界面 框图程序:可执行的代码 框图程序包括:节点nodes(实现程序功能), 端口terminals(可理解为传统仪器面板的每个元件的接线端), 连线wires(数据流向) 浮动模板Floating Palettes: 工具Tools Palette, 控件Controls Palette (only when Front Panel Window is active), 功能Functions Palette (only when Block Diagram Window
31、 is active) 帮助 including the Context Help Window and LabVIEW Help,62,小结,前面板包含控件Place controls (inputs) and indicators (outputs) in the front panel window 操作工具对对象操作Use the Operating tool to manipulate panel objects. Use the Positioning tool to select, move, and resize panel objects. Use the Wiring to
32、ol to connect diagram objects 控制比指示的边界粗Control terminals have thicker borders than indicator terminals 前面板对象有属性页和快捷菜单All front panel objects have property pages and shortcut menus 连线控制数据流 Wiring is the mechanism to control dataflow and produce LabVIEW programs 中断的箭头表明程序不可执行Broken Run arrow means a n
33、onexecutable VI 其他调试工具:设置断点、高亮、单步执行Various debugging tools and options available such as setting probes and breakpoints, execution highlighting, and single stepping,63,提示 Tips, 快捷键 Common keyboard shortcuts工具模板Access Tools Palette with -right-click工具属性 ToolsOptions selection set preferences in LabVI
34、EWVI属性 VI Properties (File menu),Windows Sun Linux MacOSRun a VIFind objectActivate Context Help windowRemove all broken wiresClose the active windowToggle btwn Diagram/Panel Window,64,3.2 模块化编程Modular Programming,TOPICS 什么是模块化? 子VI SubVIs 图标和连接端口 Icon and Connector Pane 如何调用子VI Using SubVIs如何创建子VI
35、Creating a SubVI from sections of a VI,65,层次 LabVIEW Hierarchy,SubVI,66,66,A.什么是模块化?,模块化定义了一个程序所能包含的不同模块的范围,从而将一个模块的改变对其它模块造成的影响控制在最小范围之内 LabVIEW中的模块称为子VI,67,67,A.什么是模块化?- 子VI,当一个VI应用在其它VI中,则称为子VI 子VI相当于文本编程语言中的子程序 在前面板窗口和程序框图窗口的右上角都可以看到该VI的图标 该图标与将VI放置在程序框图中时所显示的图标相同,68,68,A.什么是模块化?- 子VI,69,69,A.什么
36、是模块化?- 子VI,70,70,A.什么是模块化?- 子VI,71,71,B.图标和连线板,创建VI后,请设置图标和连线板,以便该VI作为子VI在其它VI中调用 图标和连线板相当于文本编程语言中的函数原型 每个VI前面板和程序框图窗口的右上角都有一个图标 图标是VI的图形化表示 如将VI当作子VI调用,程序框图上将显示该子VI的图标,72,72,B.图标和连线板-创建图标,右键单击前面板窗口或程序框图窗口右上角的图标并选择编辑图标,或双击图标可创建自定义图标 也可以从文件系统的任何地方拖动一个图形并放置在图标上 关于 图表图形集VI图标 所使用的标准图形 见,73,73,B.图标和连线板-设
37、置连线板,右键单击前面板右上角 的图标并选择显示连线板 连线板上的每个单元格代表一个接线端 使用各个接线端分配输入和输出 右键单击连线板并从快捷菜单中选择模式可为VI选择不同的接线端模式 最多不超过28个,74,74,B.图标和连线板-标准,以这种连线板布局作为一个标准顶部接线端通常为引用保留,如文件引用 底部接线端通常为错误簇保留,75,例子:计算斜率 SubVI Example Calculating Slope,A VI within another VI is called a subVI To use a VI as a subVI, create an icon and a con
38、nector pane after building the front panel and block diagram,76,创建图标Creating the Icon,Icon: graphical representation of a VI Right-click in the icon pane (Panel or Diagram) Always create a black and white icon创建单色图标,Default Icon,Create a custom icon,77,创建连接端口Creating the Connector,Right-click the ic
39、on (Front Panel only),78,创建连接端口Creating the Connector - continued,Click with wiring tool,79,连接端口 方框 The Connector Pane,Terminal colors match the data types to which they are connectedClick the terminal to see its associated front panel object,80,80,C.使用子VI,如需将子VI放置在程序框图中 单击函数选板上的选择VI按钮 找到需要作为子VI使用的V
40、I 双击该VI将它放置在程序框图中 将一个打开的VI放置在另一打开的VI的程序框图上 单击要用作子VI的VI图标 将这个图标拖到另一个VI的程序框图中,81,81,C.使用子VI-接线端设置,粗体 :必需的接线端 无格式:推荐的接线端 灰体:可选的接线端,82,82,C.使用子VI-部分VI转换成子VI,将VI的一部分转换成子VI: 用定位工具在程序框图上框选出需要使用的部分 选择编辑创建子VI,83,调用子程 Using a VI as a SubVI,All Functions Select a VIDrag icon onto target diagram,84,Help and Cla
41、ssifying Terminals,Classify inputs and outputs: Required Error if no connection Recommended Warning if no connection Optional No effect if no connection,85,小结Summary,Vis用作子VI必须具备两部分 VIs can be used as subVIs after you make the icon and connector 图标Icon created using Icon Editor 连接端口 Connector define
42、d by choosing number of terminals 如何调用 Load subVIs using the Select a VI option in the All Functions palette or dragging the icon onto a new diagram 如何创建 Use Create SubVI feature to easily modularize the block diagram,86,3.3 控制程序运行的结构,TOPICS A. While 循环 B. For 循环访问上一次的循环的数据 C. 顺序结构 D. 选择结构,87,A. Whi
43、le 循环,LabVIEW While循环 流程图 伪码,重复(代码); 直到满足某个条件; End;,88,A. While 循环,1. Select While Loop,2. Enclose code to be repeated,3. Drop or drag additional nodes and then wire,89,A. While循环选择循环条件,计数接线端:返回已经执行循环的次数;从开始计数 条件接线端:定义循环结束的条件,计数接线端,条件接线端,90,A. While循环隧道,隧道用于接收和输出结构中的数据 隧道会根据连入隧道的数据类型改变颜色 循环终止后,数据才会从
44、循环中传出 数据从隧道输入循环时,只有在数据到达隧道之后循环才开始执行,91,B. For循环For Loops,LabVIEW For Loop Flow Chart Pseudo Code,For i=0 to N-1;Excute (code; i=i+1); End;,92,B. For 循环 For Loops,在结构子模板中执行指定次数的框图内代码,计数端口 Count terminal (Numerical input),Wait Until Next ms Multiple function,循环端口 Iteration terminal,93,BFor循环While循环的比较
45、,For循环按照设定的次数执行 可执行次 隧道自动输出一组数据,While循环只有条件接线端接收到某一特定的值时才停止执行 至少执行一次 隧道自动输出最后一次执行的值,94,BFor循环数值转换,如果将一个双精度浮点数连接到总数接线端,LabVIEW将更长位数的数值转换为32位有符号整数 For循环只能执行整数次循环,95,BFor循环数值转换Numeric Conversion,数据缺省是 double-precision (8 bytes) 或 long integer (4 bytes) LabVIEW 自动转换不同的数据类型 计数端口总是强制转换成长整数 端口上的灰点表示数据转换,96
46、,数据转换Numeric Conversion,LabVIEW转换数据为 数据位数多的数据类型 位数相同,转换为无符号数. 改变数据类型,可以右击控件选择 转换浮点数到整数时,转换为最接近的整形数,对正好在两个数之间的数,转换为接近的偶数。 For example, LabVIEW rounds 2.5 to 2 and 3.5 to 4.,97,等待功能Wait Functions,Wait Until Next ms MultipleFunctionsTime & Dialog palette,98,等待功能Wait Functions,Wait (ms)FunctionsTime & D
47、ialog palette,Time DelayFunctionsTime & Dialog palette,99,访问上一次循环数据移位寄存器 Accessing Previous Loop Data Shift Register, 循环结构的左边界或右边界上 右击边界,选择添加移位寄存器 右边界端口储存执行完后的结果 左边界端口储存循环前的数据,100,增加移位寄存器元素 Additional Shift Register Elements,Latest value is passed to right terminal,Right-click the left terminal to a
48、dd new elements,Previous values are available at the left terminals,Right-click the border for a new shift register,1 loop ago2 loops ago3 loops ago,左边三个元素可以获得前三次循环的数据 增加移位寄存器,可以获得几个数据的前一次循环数据,101,反馈节点Feedback Nodes,当For循环或While循环中将把一个节点的输出连接到它的输入时,连线中会自动出现反馈节点,同时自动创建了一个初始化端口 反馈节点的箭头方向表示数据流的方向。反馈节点有两个端口,输入端口在每次循环结束时将当前值存入,输出端口在每次循环开始时把上一次循环存入的结果输出。 反馈节点与移位寄存器可以互换,优点在于循环框较大时,避免较长的连线 左端口多于一个时,移位寄存器不能用反馈节点代替,102,反馈节点Feedback Node,Wire from output to input to automatically create a feedback nodePlace a feedback node from the FunctionsStructures palette,