Section 10 The Formula Node 公式节点
推荐给好友
打印
加入收藏
更新于2008-05-16 21:12:31

The Formula Node

公式节点



Now that you know about LabVIEW's four main control flow structures, we'll introduce a structure that doesn't affect program flow. The Formula Node is a resizable box that you use to enter algebraic formulas directly into the block diagram. You will find this feature extremely useful when you have a long formula to solve. For example, consider the fairly simple equation, y = x2 + x + 1. Even for this simple formula, if you implement this equation using regular LabVIEW arithmetic functions, the block diagram is a little bit harder to follow than the text equations (see Figure 6.67).

现在你已经知道了LabVIEW中四种主要的控制流结构, 我们将介绍一个不会影响到程序流程的结构. 公式节点是一个可调整大小的方框, 你可以使用它直接在框图中输入代数公式. 当你要解决一个冗长公式时你会发现这个特性非常的有用. 例如, 考虑一个相当简单的方程式, y = x2 + x + 1. 就针对这个方程式, 如果你使用普通的LabVIEW算术函数来实现它, 它的框图比用文本方程式实现的要复杂一点.(见图6.67)

Figure 6.67. A snippet of code that we can convert to a formula and put inside a Formula Node, as shown in Figure 6.68

图6.67. 一个代码片段, 我们可以将它转换成公式放到一个公式节点中, 如图6.68中所示

You can implement the same equation using a Formula Node, as shown in Figure 6.68.[1]

你能够使用公式节点实现同样的方程式, 如图6.68中所示

[1] In versions prior to LabVIEW 6.0, the exponentiation operator for the Formula Node was the ^ symbol. In LabVIEW 6.0 and greater, the ^ symbol means something else entirely (bit-wise XOR), and the ** symbol is exponentiation.

[1]在早于LabVIEW6.0的版本中, 公式节点中的幂运算符是^符号. 在LabVIEW6.0或之后的版本中, ^符号表示的是完全别的东西(比特运算的XOR, 异或), 而用**符号表示幂运算.

Figure 6.68. A Formula Node containing a formula derived from the code snippet in Figure 6.67

图6.68. 一个公式节点包含着从图6.67中的代码片段中转换而来公式

With the Formula Node, you can directly enter a formula or formulas, in lieu of creating complex block diagram subsections. Simply enter the formula inside the box. You create the input and output terminals of the Formula Node by popping up on the border of the node and choosing Add Input or Add Output from the pop-up menu. Then enter variable names into the input and output terminals. Names are case sensitive, and each formula statement must terminate with a semicolon (;).

用公式节点, 你能够直接地输入一个公式或一组公式, 用来代替创建复杂的程序框图部分. 直接在方框内输入公式. 在节点边框的弹出菜单中选中Add Input 或 Add Output来创建公式节点的输入输出端子. 然后在输入输出端子中输入变量的名称. 该名称是对大小写敏感的, 并且每一个公式的声明必须以一个冒号(;)结尾

You will find the Formula Node in the Programming>>Structures subpalette of the Functions palette.

你可从Programming>>Structures模板中找到Formula Node

These operators and functions are available inside the Formula Node.

在公式节点内运算符和函数都是可用的。



To get detailed information on the Formula Node syntax, open the Help window and place the cursor over the formula node. The Help window will look like Figure 6.69. Click on the Detailed help link to open the LabVIEW help file and then follow the Formula Node Syntax link.

要获得更多的公式节点语法的信息, 可以打开帮助窗口并放置指针在公式节点上. 帮助窗口看起来像图6.69. 点击Detailed help的链接来打开LabVIEW帮助文件,然后继续打开公式节点语法(Formula Node Syntax)的链接.

Figure 6.69. The Context Help window showing detailed information about the Formula Node

图6.69. Context Help窗口显示了关于公式节点的详细信息

The following example shows a conditional branching that you could perform inside a Formula Node. Consider the following code fragment, similar to Activity 6-3, which computes the square root of x if x is positive, and assigns the result to y. If x is negative, the code assigns value of 99 to y.

接下来的例子示范了一个条件分支, 并且该条件分支能够在公式节点中执行. 留心查看下面的代码片段, 类似于练习6-3, 该练习计算当 x为正数时x的平方根, 并指定该数为y. 如果x为负数时, 代码指定数值99给y.



if (x >= 0) then
       y = sqrt(x)
else 
       y = -99
end if



You can implement the code fragment using a Formula Node, as shown in Figure 6.70.

你能够使用如图6.70中的公式节点来实现这段代码.

Figure 6.70. A Formula Node formula with some of its syntax elements annotated

图6.70. 一个公式节点, 和一些语法元素的注解

Activity 6-5: Formula Fun

练习6-5: 公式

You will build a VI that uses the Formula Node to evaluate the equation y = sin(x), and graph the results.

你将创建一个VI使用公式节点来求方程式y = sin(x) 的值, 并绘制结果成图形

1. Open a new panel. Select Waveform Graph from the Modern>>Graph subpalette of the Controls palette (see Figure 6.71). Label it Graph. You'll learn all about graphs in Chapter 8, but this activity would be kind of dull without a nice pictorial representation, so we thought we'd give you a taste of them.

1.打开一个新的面板. 从控件模板Modern>>Graph中选择波形图形Waveform Graph(见图6.71). 给波形图形加标签文本. 你将在第8章中学习更多的关于图形的内容, 但本练习如果没有一个鲜活的图形可能会有些枯燥, 因此我们想让你体验一下.

Figure 6.71. The front panel of the VI you will build during this activity

图6.71. 本练习中VI的前面板

2. Build the block diagram shown in Figure 6.72.

2.构建程序框图如图6.72.

Figure 6.72. The block diagram of the VI you will build during this activity

图6.72. 本练习中VI的程序框图

With the Formula Node (Structures palette), you can directly enter mathematical formulas. Create the input terminal by popping up on the border and choosing Add Input from the pop-up menu; then create the output terminal by choosing Add Output from the pop-up menu.

采用公式节点(Structures模板), 你能够直接地输入数学公式. 创建输入端子, 通过弹出边框菜单并从弹出菜单中选择Add Input; 然后创建输出端子, 通过从弹出菜单中选择Add Output.

When you create an input or output terminal, you must give it a variable name. The variable name must exactly match the one you use in the formula. Remember, variable names are case sensitive.

当你创建输入输出端子, 你必须给它一个变量名称. 该变量名必须正确的匹配你在公式中所使用变量. 记住, 变量名对大小写敏感.



Notice that a semicolon (;) must terminate each statement in the formula node.

注意, 在公式节点中冒号(;)必须作为每一个表达式语句的结尾.



Pi Constant

π常数

The π constant is located in the Functions>>Programming>>Numeric>>Math and Scientific Constants palette.

π常数位于函数模板Functions>>Programming>>Numeric>>Math and Scientific Constants中.

During each iteration, the VI multiplies the iteration terminal value by π/10. The multiplication result is wired to the Formula Node, which computes the sine of the result. The VI then stores the result in an array at the For Loop border. (You will learn all about arrays in Chapter 7. Then you will see why you can wire array data out of a For Loop, while scalar data comes out of a While Loop by default.) After the For Loop finishes executing, the VI plots the array.

在每一次循环的重复运行中, VI以π/10乘重复计数端子的当前值, 相乘的结果连接到公式节点, 该节点计算乘积的正弦值. VI然后将这个结果以一个数组存储在For Loop的边框上. (你将在第7章中学习数组. 然后你将看到为什么你能够连线数组数据出离一个For Loop, 而标量数据通过默认的方式出离While Loop), 在For Loop结束执行后, VI将描绘出该数组.

3. Return to the front panel and run the VI. Note that you could also use the existing Sine function (Functions>>Numeric>>Trigonometric palette) to do the same thing as the Formula Node in this activity, but LabVIEW does not have built-in functions for every formula you'll need, and we wanted to give you the practice.

3.返回到前面板并运行该VI. 注意你也可以使用现有的Sine函数(Functions>>Numeric>>Trigonometric模板)来做同样的事如同在本练习中的公式节点, 但是LabVIEW没有你需要针对于每一个公式的所有的内置函数, 并且我们想让你做这样的练习.

4. Save the VI in your MYWORK directory and name it Formula Node Exercise.vi. Close the VI.

4.保存该VI到MYWORK目录并命名为Formula Node Exercise.vi. 最后关闭VI.

VI Logic

VI逻辑



timebase = PI/10
for i = 0 to 99
        x = i * timebase
        y = sin(x)
        array[i] = y
next i
Graph (array)

 

<<PREV    NEXT>>




 
关于我们 | 诚邀加盟 | 客户服务 | 相关法律 | 网站地图 | 友情链接 | 服务信箱:service@eefocus.com
© 2006 与非门科技信息咨询(北京)有限公司 All Rights Reserved.