Wednesday, August 18, 2010

MathScript node (in a LabVIEW Block diagram)


You can include MathScript commands in the Block diagram of a VI  in a MathScript node which is available on the Functions / Mathematics / Scripts & Formulas palette. Let us look at a simple example. Below are the Front panel and the Block diagram of mathscript_example.vi, followed by comments. You will soon be guided through the development of this VI.


Comments to the Front panel:
·         To the left of the Front panel are the scalar controls (or numbers in DBL (double) formal), a and b, and two real matrices, A and x.
·         Further to the right are indicators, z and y. z is a real scalar. y is a real matrix.
·         error out is a cluster indicator displaying any MathScript errors on the Front panel.
Comments to the Block diagram:
·         a, b, A, and x are names of the inputs to the MathScript. The data type of a and b are real scalars (we may also say doubles). A and x are real matrices.
·         Matrices are used, not arrays, in these matrix based calculations! Connecting arrays to the MathScript node in stead of matrices in such applications will cause an error.
Scalars and matrices are probably the most common data types in MathScript applications. However, you can also use arrays and strings (text). The data type of a MathScript node input or output are set via right-clicking on the input/output, and selecting the sata type from a list.
The MathScript node contains three commands: One comment, which starts with a percent sign, and two ordinary commmands separated by semicolon.
·         The MathScript contains an error cluster output which here is wired to a front panel indicator. It is wise to include an error cluster in the early stage of the development of a VI containing a MathScript node as it helps you to find errors. These error may otherwise be quite hard to find.
Note: If you want to include a matrix constant in the Block diagram, you may face trouble because there is no matrix constant on the Functions palette (this is a little strange...). In this case, you may in stead intermediately create a matrix control on the Front panel, and then change its Block diagram terminal from control to constant via right-click on the terminal. If you really want to use matrix constants in the Block diagram, you may consider creating or defining the matrix directly in the MathScript node. For example you can type
M = [1,2;3,4]
which creates a 2x2 matrix with 1 and 2 in the first row, and 3 and 4 in the second row.
Now, it's your turn. To create a VI similar to mathscript_example.vi:
  • Open a blank VI, save it as my_mathscript.vi in any folder you prefer.
  • Add the following to the Front panel, as on the Front panel of mathscript_example:
    • Two Matrix controls from the Controls / Array & Matrix palette, and label them A (real matrix) and x (real matrix), respectively.
    • One Matrix indicator from the Controls / Array & Matrix palette, and label it y (real matrix).
    • Two scalar Numeric controls, and label them a (real scalar) and b (real scalar), respectively.
    • One scalar Numeric indicator, and label it z (real scalar).
Enter values to these Front panel elements as on the Front panel of mathscript_example.
  • Do the following with the Block diagram (re-position the elements as you wish):
    • Add a MathScript node from the Functions / Mathematics / Scripts & Formulas palette. In the node, type the commands as shown in the Block diagram of mathscript_example
    • Create input tunnels and output tunnels to the node by right-clicking on the border of the node, and give these inputs and outputs names as shown in the Block diagram of mathscript_example.
    • Connect the control terminals to the inputs of the MathScript node according to the Block diagram of mathscript_example. The data type of each input is then automatically set by LabVIEW, but you should check the settings by right-clicking on each of the inputs.
    • It turns out that the data type of the outputs are not set automatically by LabVIEW. In stead, you must set the data type manually by right-clicking on each of the outputs:
      • z: Scalar, DBL
      • y: Matrix, real
Then connect the ouputs y and z to their respective indicators.
  • Create an error indicator on the Front panel: Right-click on the error output (at the lower right corner of the MathScript node) / Create Indicator.
Save the VI, and run it. Does it work?
Note: This VI will run the code once since the code is not inside any While loop. If you want, you can add a While loop, a Stop button, and a Metronome. If you do not want to add a While loop, but still want to run your VI continuously while testing it, click the Run Continuously button in stead of the Run button in the toolbar.


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Popular Projects

My Blog List

Give support

Give support
Encourage Me through Comments & Followers

Followers