<< Chapter < Page | Chapter >> Page > |
The Celsius output array contains: Celsius = {-17.7778, -6.6667, 4.44444, 15.5556, 26.6667, 37.7778, 48.8889, 60, 71.1111, 82.2222, 93.3333}
The next program will generate Fahrenheit values and convert them to Celsius until a condition is met to stop the iterations in a While Loop . In the Block Diagram window, select the While Loop structure by clicking on it from the Functions>>Programming>>Structures menu.
Click and drag to create the While Loop structure.
In the Front Panel window, create two numeric output arrays. Label them Fahrenheit and Celsius .
Re-arrange the diagram as in [link] .
From the Functions menu, select Multiply function and a couple of numeric constants. Type in 20.0 and 300.0 for the numeric constants. Select the Fahrenheit to Celsius.vi and drop it inside the While Loop . Re-arrange the diagram to look like [link] .
From the Functions>>Programming>>Comparison menu select the Greater or Equal operator.
Wire the While Loop components as shown in [link] .
Wire the output of the Multiply operation to the Fahrenheit and the output of the Fahrenheit to Celsius function to the Celsius numeric output arrays. The connections between the While Loop and the Fahrenheit and Celsius arrays are broken (see [link] ).
To repair the broken connections, roll over the mouse pointer to the Loop Tunnel .
Right click on the Loop Tunnel and select Enable Indexing from the pop-up menu.
This enables values to accumulate and store the results into an array.
Repeat for the Celsius array.
Each iteration of the While Loop in this program generates an i × 20 Fahrenheit value and converts it to Celsius. The While Loop stops iterating when the generated Fahrenheit value is greater than or equal to 300. The resulting arrays are stored in the Fahrenheit and Celsius numeric output arrays.
Save the program as Fahrenheit to Celsius While Loop.vi and run it. The program generates the following results:
Fahrenheit = {0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 240, 260, 280, 300}
Celsius = {-17.7778, -6.6667, 4.44444, 15.5556, 26.6667, 37.7778, 48.8889, 60, 71.1111, 82.2222, 93.3333, 104.444, 115.556, 126.667, 137.778, 148.889}
Using the previous G program example, we will now visualize the results by adding a graph to the Front Panel windows. Right click on the Front Panel window. Select XY Graph from the Controls>>Modern>>Graph menu.
Drop the XY Graph in the Front Panel window. Double click on the x and y axis labels and rename Time to Fahrenheit and Amplitude to Celsius .
The Block Diagram window contains the XY Graph terminal.
Select Bundle from the Functions>>Programming>>Cluster, Class&Variant menu
Drop it on the diagram as shown in [link] .
Wire the Fahrenheit and Celsius results to the input Bundle terminals and the output Bundle terminal to the XY Graph .
Save the program and run it. The resulting graph is shown in the figure below.
This G program shows how G allows programmers to develop interactive programs. Create the following G program and wire it as shown in the figure below.
In the Front Panel window, from the Functions>>Modern>>Numeric select the vertical pointer slide. From the Functions>>Modern>>Graph select Waveform Chart .
Re-label the vertical pointer slide as Amplitude and the waveform chart as Sine Wave . Re-arrange to GUI to look like the figure below.
Right click on Sine Wave and select Properties from the pop-up menu.
Select the Scales tab and change Maximum to 1023. Sine Wave will display 1024 samples.
Click on the down arrow located to the right of Time (X-Axis) and select Amplitude (Y-Axis) .
De-select Autoscale and change the Minimum and Maximum values to -10 and 10 . Click OK .
In the Block Diagram window, re-arrange the Amplitude and Sine Wave terminals and finish the program as shown in [link] .
Scroll the mouse pointer over the Loop Control …
And right click on the Loop Control and from the pop-up menu select Create Control .
A stop terminal is created…
With the corresponding stop Boolean input control. Save the G program as Interactivity.vi .
Run the G program.
While the program is running, change the Amplitude and watch the graph update to reflect the interactive changes.
To end the G program, simply click on the stop button.
Congratulations. You have successfully completed and executed your first interactive G program.
Save a copy of Interactivity.vi as Parallel Programming.vi . Select the while loop as shown in [link] .
From the menu select Edit>>Copy .
Create a copy of the while loop and its contents by selecting Edit>>Paste . Organize the diagram as shown in the figure below.
Go the Front Panel window and organize the input and output controls as shown in the figure below.
Congratulations!!! You have just completed your first parallel interactive program using G. Save the program, run it and interact with it. To end this program click on stop and stop 2 .
Save a copy of Parallel Programming.vi as Multicore Programming.vi . If you have a multicore computer, CONGRATULATIONS!!! You have just completed your first multicore G program.
This program shows the polymorphic properties of G. Create the G program shown below.
Notice that the Subtract and Multiply operations allow arrays to be wired in the G program.
Notification Switch
Would you like to follow the 'Introduction to g programming' conversation and receive update notifications?