VPL Tutorial 3 - Create Your Own Activity

Microsoft Robotics

Glossary Item Box

Microsoft Robotics Developer StudioSend feedback on this topic

VPL Tutorial 3 - Create Your Own Activity

This tutorial uses the same scenario as VPL Tutorial 2 - Incrementing a Value, but illustrates how you can create your own activities to modularize your application.

This tutorial is provided in the Microsoft Visual Programming Language (VPL) language. You can find the project files for this tutorial at the following location under the Microsoft Robotics Developer Studio installation folder:

Sample location

This tutorial teaches you how to:



This tutorial requires no special hardware.


This tutorial is designed for use with Microsoft Visual Programming Language (VPL) which is included as part of Microsoft Robotics Developer Studio.

Create an Activity

Create a new project (using the File > New menu command) and insert a Data activity. Set the value of your new Data activity block to 1.

Create a new activity by inserting an Activity block on your diagram. Make sure that it is selected. (It's border should be highlighted. If not, click on it once.) In the Properties panel, change its Name and Friendly Name to CountTo10. Now open the activity. (This is easily accomplished by selecting the Open command on its pop-up context menu or double-clicking it). A new tabbed page should appear. Using the Action drop-down list at the top of the page, click the Action entry. This switches to the Action handler page. While the diagram page looks similar to your top level data-flow page, you can see that it has connection borders on each side which is how you connect its internal data-flow to an external data-flow.

Use the Actions and Notifications command in the Edit menu or click the button next to the action selector drop-down list at the top of the page. Click the Action in the list. Add an input value to the action. This is the start value that you will use later for counting. Change the name of the input value to StartValue and choose int as its type. Click OK.

Figure 1

Figure 1 - Actions and Notifications Dialog

Insert the activity blocks you used in VPL Tutorial 2 - Incrementing a Value as part of the Activity. These activities were Merge, If, Calculate, Variable, and Data. However, you will need to connect them slightly differently to make them work in this tutorial.

Begin by placing a Calculate activity block near the left border and connect the action's input pin. Type StartValue in the Calculate textbox. This extracts the start value from the action's input message.

Place a Merge activity block next to the Calculate activity block and connect them. Place the Variable activity block to the right of the Merge activity block. Use the incoming value that comes into the activity routed through the Merge activity block to set the variable you wish to use.

Create a new variable and name it Test2. To create a variable, display the drop-down list in the Variable activity block and click Define Variables.... In the resulting dialog box, enter the name and type for the variable and set its type to int. Connect the output of the Merge activity block to the Variable activity block. This causes the value coming through the Merge activity block to set the value of the Variable activity to Test2.

Figure 2

Figure 2 - Set a Variable

Connect the output of the Variable activity block (Current Value) to the If activity block (Condition). In the If text box, type Test2 = 10. This test checks the limit on the number of times to execute. (If you are a C of C# programmer you might think that this conditional expression is incorrect because in those languages it should be Test2 == 10. However, VPL is intended for non-programmers so either is accepted.)

Figure 3

Figure 3 - Add If Activity

Create a new Calculate activity. Use the pop-up context menu of the Calculate activity (right-click on the block) to flip the connections so that the input enters on the right instead of the left.

Connect the output of the Else connection on If activity block to the input of a Calculate activity block. In the Calculate text box, type Test2 + 1 to add one to the current value of Test2. Connect the output of the Calculate activity block to the input of the Merge activity block. (A Merge accepts multiple incoming connections). This updates the value of the variable when the message flows through to the Variable activity. Notice that this creates a loop.

Figure 4

Figure 4 - Creating a Loop

Now, like in VPL Tutorial 2 - Incrementing a Value, connect a Calculate activity block to the output of the Variable activity block. Type "The number is " + Test2 in the Calculate activity block's text box.

Insert and connect a Data activity block to the top output of the If activity block. Select string from the drop-down list, and type All done! in the text box. Use a second Merge activity block to connect the output of the Calculate activity block and Data activity block. This simplifies the connection to other data-flows outside this action.

Figure 5

Figure 5 - Finish adding activities

To connect the output of your data-flow, select the Actions and Notifications command from the Edit menu. In the dialog box, click the Notifications tab and then click the Add button under Notifications to create a new notification. Then click on Add under Notification values. Name the new value Text by clicking Add and set the type as string, then click OK.

Figure 6

Figure 6 - Set up the Notification

You can now connect the output of the second Merge activity block to the round Notification connection pin on the right-hand border of the action page. Select the Notification you just created. Your activity's data-flow should look like the following.

Figure 7

Figure 7 - Completed Action

Close the activity page or click the Diagram tab to switch back to the main diagram page. Connect the output of the Data activity block to your new CountTo10 activity block.

Copy and paste the CountTo10 block to create a duplicate of the activity you just created.

Finally, insert a Text-to-Speech service activity block and connect the round notification output of your CountTo10 activity block to it. Set the connections as From: Notification (the name of your event) and To: SayText. Set the data connection from Text to SpeechText.

Figure 8

Figure 8 - Main Diagram

Run your application using the Run menu command (or by pressing F5). Your application should produce the same results as VPL Tutorial 2 - Incrementing a Value.

When you run your program, you might find that it does not count properly. Sometimes the numbers will be out of order and it will tend to talk over itsef. The first problem arises because messages can travel around the loop at different speeds and sometimes they catch up with each other.

The second problem occurs because new messages are sent to the Text-To-Speech service before it has finished saying the previous message. You could use the SayTextSynchronous operation instead of SayText because the synchronous operation does not send back a response until the speech is over. However, this would require changes to the program to wait for the Text-To-Speech service to finish after each message.

Counting without Loops and Variables

This tutorial shows how to create and use loops as well as how to use variables. In many situations you actually do not need variables. Loops are required in only a very few cases. The following example shows a much simpler way of counting without loops and variables. The activity called RecursiveCountTo10sends messages to itself. This process where an activity calls itself is called recursion. The key to making it work is that each time the action sends a message to itself, the message must contain a new value. It must also have a termination condition (in the If block) or it will run forever. The details of the example are left as an exercise to the reader.

Figure 9

Figure 9 - Recursive Count


In this tutorial, you learned how to:

VPL Tutorials: VPL Tutorial 4 - Run Simulation From VPL

VPL User Guide: Getting Started



© 2012 Microsoft Corporation. All Rights Reserved.