<< 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}

While loop

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.

Four icons contained in the directory hierarchy under 'Programming' and 'Structures'.
While Loop Structure

An empty dashed square with another small square on the bottom right corner.
Creating a While Loop
A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle.
While Loop

In the Front Panel window, create two numeric output arrays. Label them Fahrenheit and Celsius .

A form with two rows. The upper row is labeled 'fahrenheit' and the lower row is labeled 'celsius'.
Numeric Output Arrays

Re-arrange the diagram as in [link] .

A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'.
While Loop Diagram

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] .

A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'.
Generating Fahrenheit Values

From the Functions>>Programming>>Comparison menu select the Greater or Equal operator.

There are four icons contained within the directory hierarchy under 'Programming' and 'Comparison'.
Greater or Equal Function

Wire the While Loop components as shown in [link] .

A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'.
Generating Fahrenheit Values&Stop Condition

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] ).

A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'. The lines that connect the gray box to the right side icons are broken with red x's.
Broken Wires

To repair the broken connections, roll over the mouse pointer to the Loop Tunnel .

A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'. The lines that connect the gray box to the right side icons are broken with red x's. There is a mouse arrow hovering over over the right side of the gray square causing the labeled 'loop tunnel' to appear over the diagrm.
Loop Tunnel

Right click on the Loop Tunnel and select Enable Indexing from the pop-up menu.

A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'. The lines that connect the gray box to the right side icons are broken with red x's. There is a menu overlaid on the diagram with the item 'Enable Indexing'.
Enable Loop Indexing

This enables values to accumulate and store the results into an array.

Repeat for the Celsius array.

A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'. The line that connects the gray box to the bottom right icons is broken with a red x.
Broken Wire Repaired

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:

A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'.
Fahrenheit to Celsius While Loop

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}

A form containing two rows. The upper level is labeled 'fahrenheit' with two fields on this level labeled containing '15' and '300'. The lower level is labeled 'celsius' and the fields contain the values '15' and '148.889'.
Fahrenheit and Celsius Arrays

Graphs

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.

Three icons are contained in the directory hierarchy under 'Modern' and 'Graph'.
XY Graph Selection

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 .

An empty graph with the x axis labeled 'fahrenheit' and the y axis labeled 'celsius'. To the left of the graph there is a couple of forms. The upper for is labeled 'fahrenheit' with the values '0' and '0'. The lower form is labeled 'celsius' with the values '0'and '0'.
XY Graph in Front Panel window

The Block Diagram window contains the XY Graph terminal.

A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'. To the right of this there is one final icon labeled 'XY graph'.
XY Graph Terminal in Diagram

Select Bundle from the Functions>>Programming>>Cluster, Class&Variant menu

Four icons contained within the directory hierarchy under 'Programming' and 'Cluster, Class, and Variant'.
Bundle Operator

Drop it on the diagram as shown in [link] .

A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'. In between these two black boxed arrow diagram. To the right of this there is one final icon labeled 'XY graph'.
Bundle for XY Graph

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.

A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'. In between these two black boxed arrow diagram with a pink line conntected to the right where there is one final icon labeled 'XY graph'.
Wired XY Graph

An empty graph with the x axis labeled 'fahrenheit' and the y axis labeled 'celsius'. To the left of the graph there is a couple of forms. The upper for is labeled 'fahrenheit' with the values '0' and '0'. The lower form is labeled 'celsius' with the values '0'and '-17.7778'.
XY Graph Result

Interactivity

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.

A gray box containing several icons. From left to righ the icons are a orange boxed '0.02' above a blue boxed 'i' both of which are connected to a triangular box containing a 'x'. This is connected to a sine wave icon which is connected to another triangular box containing  a 'x'. In the bottom right corner there is a green box containing a red circle.
Creating Interactive Programs

In the Front Panel window, from the Functions>>Modern>>Numeric select the vertical pointer slide. From the Functions>>Modern>>Graph select Waveform Chart .

Six icons contained within the directory hierarchy under 'modern' and 'numeric'. Six icons contained within the directory hierarchy under 'modern' and 'graph'.
Vertical Pointer Slide and 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.

An empty waveform chart with the x axis labeled 'Time' and the y axis is labeled 'Amplitude'.
Slide&Waveform Chart in Front Panel window

Right click on Sine Wave and select Properties from the pop-up menu.

An empty waveform chart with the x axis labeled 'Time' and the y axis is labeled 'Amplitude'. There is a menu overlaid the graph with the item 'Properties'.
Selecting Chart Properties

Select the Scales tab and change Maximum to 1023. Sine Wave will display 1024 samples.

A windows window with the tab 'Scales' selected. There is a red square around the field  labeled 'Maximum' with the value '1023'.
X-Axis Maximum

Click on the down arrow located to the right of Time (X-Axis) and select Amplitude (Y-Axis) .

A windows window with the tab 'Scales' selected. A drop down menu with the item 'Time (X-Axis)' marked with a check mark and the item 'Amplitude (Y-Axis)' highlighted in blue.
Selecting Y-Axis

De-select Autoscale and change the Minimum and Maximum values to -10 and 10 . Click OK .

De-Selecting Autoscale

In the Block Diagram window, re-arrange the Amplitude and Sine Wave terminals and finish the program as shown in [link] .

A gray box containing several icons. From left to righ the icons are a orange boxed '0.02' above a blue boxed 'i' both of which are connected to a triangular box containing a 'x'. This is connected to a sine wave icon and below this icon is another icon labeled 'Amplitude'. These two icons are connected to another triangular box containing  a 'x'. To the right of the triangle is a 'Sine wave' icon. In the bottom right corner there is a green box containing a red circle.
Interactive Sine Wave Diagram

Scroll the mouse pointer over the Loop Control

A gray box containing several icons. From left to righ the icons are a orange boxed '0.02' above a blue boxed 'i' both of which are connected to a triangular box containing a 'x'. This is connected to a sine wave icon and below this icon is another icon labeled 'Amplitude'. These two icons are connected to another triangular box containing  a 'x'. To the right of the triangle is a 'Sine wave' icon. In the bottom right corner there is a green box containing a red circle with a mouse arrow on top it, and the label 'Loop Condition' is overlaid.
Loop Condition

And right click on the Loop Control and from the pop-up menu select Create Control .

A gray box containing several icons. From left to righ the icons are a orange boxed '0.02' above a blue boxed 'i' both of which are connected to a triangular box containing a 'x'. This is connected to a sine wave icon and below this icon is another icon labeled 'Amplitude'. These two icons are connected to another triangular box containing  a 'x'. To the right of the triangle is a 'Sine wave' icon. In the bottom right corner there is a green box containing a red circle overlaid with a menu. The item 'Create Control' is highlighted in blue.
Create Loop Control

A stop terminal is created…

A gray box containing several icons. From left to righ the icons are a orange boxed '0.02' above a blue boxed 'i' both of which are connected to a triangular box containing a 'x'. This is connected to a sine wave icon and below this icon is another icon labeled 'Amplitude'. These two icons are connected to another triangular box containing  a 'x'. To the right of the triangle is a 'Sine wave' icon. In the bottom right corner there is a'stop' icon connected to a green box containing a red circle.
Interactive G Program

With the corresponding stop Boolean input control. Save the G program as Interactivity.vi .

An empty graph witht he x axis labeled 'Time' and the y axis labeled 'Amplitude'.
Interactive Program

Run the G program.

Interactive Program

While the program is running, change the Amplitude and watch the graph update to reflect the interactive changes.

An empty graph witht he x axis labeled 'Time' and the y axis labeled 'Amplitude'. There is a sine wave with an amplitude of 6.93878.
Interactive Program

To end the G program, simply click on the stop button.

Congratulations. You have successfully completed and executed your first interactive G program.

An empty graph witht he x axis labeled 'Time' and the y axis labeled 'Amplitude'. There is a sine wave with an amplitude of 6.93878. The stop button on the bottom left of the graph is highlighted with a red box.
Interactive Program

Parallel programming

Save a copy of Interactivity.vi as Parallel Programming.vi . Select the while loop as shown in [link] .

A gray box containing several icons. From left to righ the icons are a orange boxed '0.02' above a blue boxed 'i' both of which are connected to a triangular box containing a 'x'. This is connected to a sine wave icon and below this icon is another icon labeled 'Amplitude'. These two icons are connected to another triangular box containing  a 'x'. To the right of the triangle is a 'Sine wave' icon. In the bottom right corner there is a'stop' icon connected to a green box containing a red circle.
Select Diagram for Parallel Programming

From the menu select Edit>>Copy .

A typical edit menu in window. The item 'copy' is highlighted in blue.
Copy Selected Diagram

Create a copy of the while loop and its contents by selecting Edit>>Paste . Organize the diagram as shown in the figure below.

There are two identical diagrams. The only difference between the two are the labels. The one on the left is labeled 'Sine Wave' whereas the other is labeled 'Sine Wave 2'. The diagrams consists of a gray box containing several icons. From left to righ the icons are a orange boxed '0.02' above a blue boxed 'i' both of which are connected to a triangular box containing a 'x'. This is connected to a sine wave icon and below this icon is another icon labeled 'Amplitude'. These two icons are connected to another triangular box containing  a 'x'. To the right of the triangle is a 'Sine wave' icon. In the bottom right corner there is a'stop' icon connected to a green box containing a red circle.
Paste Diagram

Go the Front Panel window and organize the input and output controls as shown in the figure below.

Two parallel empty graphs.
Parallel G Program

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 .

Two parallel graphs. The one on the left has a sine wave with an amplitude of about 7 and the one on the right has a sine wave with an amplitude of about 2.5.
Parallel Interactive G Program

Multicore programming

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.

Two parallel graphs. The one on the left has a sine wave with an amplitude of about 8 and the one on the right has a sine wave with an amplitude of about 5. Overlaid on top of these graphs is another graph showing CPU Usage and CPU usage history.
Interactive Multicore G Program

Polymorphism

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.

A diagram of a 'polymorphic G Diagram'. From left to right there is a blue boxed '16'connected via blue line to a large gray box containin a blue boxed 'N' in the upper left corner, an orange boxed '20'in the center and a blue boxed 'i'. The last two icon are connected via line to a triangular box containing a 'x'. A line connects this triangle to a box on the right wall of the big box which is then connected to a orange circle. Above the orange circle is an line leading to an icon labeled 'Fahrenheit'. Below the circle are three orange boxed numbers, from top to bottom '32', '5', '9'. The '32' and the circle are connected via lines to a triangular box containing a '-'. The '5' and '9' are connect via lines to a triangular box containing a '÷'. Both triangles connect to another triangle containing a 'x' and a line connects that triangle to a final icon labeled 'celsius'.
Polymorphic G Diagram

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to g programming. OpenStax CNX. Mar 15, 2010 Download for free at http://cnx.org/content/col11192/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to g programming' conversation and receive update notifications?

Ask