Robolab Sub-VIs

One nice feature of Robolab is the ability to take portions of an existing program and create a separate reusable code module out of it. Programmers of all levels are familiar with this type of modularity because it results in code that is easier to read and maintain. Robolab calls programs VIs (short for Virtual Instrument) and reusable code modules are called SubVIs. To create a SubVI in Robolab follow these steps:

1. Start with a well tested program that contains a series of blocks you want to reuse elsewhere in your program (or other programs). In the obstacle course program one such series of blocks is the one that provides the Go Forward functionality. Make sure the arrow cursor is active (if it is not then press the space bar until it is or use the Tools Palette window to select it directly). With the arrow cursor, drag a selection box around the blocks you want to make into a sub-VI. One important decision to make is whether you want any inputs to the blocks you have selected to be fixed in the sub-VI or parameters that must be specified when the sub-VI is invoked. Each approach has its advantages. In the fixed case the caller of your sub-VI doesn't have to do anything besides placing a call to your sub-VI in their program. One disadvantage to this approach, however, is that the caller cannot modify the sub-VI's behavior in any way. Since we want to make a general purpose Go Forward routine that will allow the caller to specify how far the robot should go forward when it's called we should not select the input value to the Wait command. In so doing our sub-VI will accept one input value that replaces the value from the original code when the sub-VI is called. The following screen shot shows how we want to select the items for creating our sub-VI.

2. With the desired blocks selected, choose the Create SubVI command from the Edit menu. In the screen shot below you can see what the blocks look like after you have selected them.

3. The Create SubVI command will display a SubVI window like the following one. This window is not very useful for our purposes (with the exception of allowing you to edit the icon that is displayed when you select your SubVI - to do this double-click on the icon in the upper right corner of the window). To see the actual program blocks in the SubVI you created select the Show Diagram command from the Window menu.

4. In the diagram window Robolab shows you the blocks in your SubVI. For some reason Robolab seems to always mix up the Begin and End labels. I haven't quite figured out why this is. Also, since Robolab doesn't know what your input parameter is used for it gives it a generic name like "Numeric".

5. To make the SubVI a bit easier to read change the Begin and End labels and give a descriptive name to the Numeric label. For the Go Forward SubVI the input parameter is the number of seconds for the robot to go forward. Here's a version that has been cleaned up a little bit:

6. Save the SubVI you created in a place that makes it easy to find later. Be sure to name it something obvious, like "Tank Forward" so you know what it does by simply looking at the name of the file. When you return to your main program the block you selected will have been replaced by the generic SubVI icon. You can edit the appearance of your SubVI icon in the first SubVI window that was displayed after you selected the Create SubVI command (see screen shot above).

Notes

  • One thing you may have noticed is that the SubVI we created in the example above turns off all motors when it is finished, but it only uses motors A and C to move. If motor B was doing something at the time we called Go Forward, it would turn off when the Go Forward SubVI was finished. A better version would only turn off motors A and C.
  • You can use the Select a VI... icon on the Functions Palette window - - to insert a SubVI you have created into another program. Selecting this command will display a File Open... dialog that allows you to navigate to the place where you have your SubVIs saved. To make the process of selecting SubVIs easier you may want to consider creating a "dummy" VI that is not intended to be run on an RCX but instead catalogs the SubVIs you've created in one place. You can copy the SubVIs from this VI to any other one you might be working on.

Obstacle course program revisited

Now that we understand the general idea of how to create SubVIs we can simplify our original obstacle course program. Here's a version of that program that uses SubVIs for each of the steps outlined in the obstacle course challenge.

The SubVIs for the above program are defined as follows:


Go Forward


Turn Right


Turn Left

Copyright © 2003-6, Hacienda Robotics Program.