VPL Tutorial 2 - Incrementing a Value
Glossary Item Box
This tutorial introduces additional basic Dataflow Control activities included in VPL. You will create a variable; initialize it; and, using Text-To-Speech, audibly count to ten.
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:
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 and set a Variable
To begin, create a new project by choosing New from the File menu. Next, insert a Variable activity by dragging it into the tool box or double-clicking it.
Click the Ellipsis Button (…) on the Variable activity block opening the Define Variables dialog. (You can also select "Variables..." from the Edit menu). In the dialog box, click on the Add button and type Test in the text box. In the Type drop-down list make sure int is selected as the variable type. Click OK.
If your Test variable does not show as the current setting in the Variable text box, open the drop-down list again and select your newly created variable to assign it to this activity.
Add a Data block to your diagram to the left of the Variable block and connect them by dragging a link connection from the output pin of the Data activity block to the Variable activity block. The Connections dialog box opens. Select DataValue for the outgoing connection pin and SetValue for the incoming connection pin and click OK.
Select int from the drop-down list and enter 1 into the text box of the Data activity block. This sets both the data and its type. Its connection will then initialize the Test variable to 1. When you use the SetValue connection of a Variable activity block, it not only sets 1 as the value, but also sends the variable on through its output connection.
Test the value of a variable
Add a Merge activity block to the right of your Variable activity block and connect the Variable activity block to the Merge activity block. You will use this block to create a counting loop. A Merge activity block can have multiple inputs, each input is passed on as it is received.
Add an If activity block to the diagram to right of the Merge activity block. Connect the Merge activity block outgoing connection to the If activity block. In the If activity block, enter Test == 10 in the test condition. (The syntax with a double equal sign is from C#. You can also use a single equal sign in VPL.) This causes the If activity block to check if the variable Test is equal to 10, and send a message via its normal output pin if it is, or send a message through the Else pin if it is not equal.
Increment a variable
Add a Calculate activity block. Right-click on the Calculate block and select "Flip Connections" from the pop-up context menu. This swaps the input and output pins so that the connections on the diagram flow in a more natural way. Connect it to the Else connection (the lower output pin) of the If activity block. This connection is used if the test condition is false, i.e. Test is not equal to 10. In the Calculate activity block, type Test + 1.
The Calculate takes the value of Test and adds 1 to it then sends the result through its output pin.
Use this result to update the value of Test by inserting another Variable activity block (set it to Test using its drop-down list) and connecting it to the output of the Calculate box. The Connections dialog box should open. Select CalculatedResult and SetValue and click OK. Connect the output connection pin of the Variable activity block to the Merge activity block. This completes your loop.
When you make the final connection of the loop to the Merge, an exclamation icon will appear. If you hover over this with the cursor you will see a warning message "Loop detected. Consider using recursion!". This example does not use recursion, and you can ignore this warning because it is not relevant.
Use Text To Speech
To make things a little more interesting, add another Calculate activity block and connect it to the output of the Merge activity block. Type "The number is " + Test into the Calculate. This automatically converts the value of Testto text and appends it to the words inside the quotation marks.
Add a Text-To-Speech service activity block and connect it to the output of the Calculate activity block. Set the Connections from the CalculatedResult to the SayText and the Data Connections from value to SpeechText. This results in the text-to-speech engine speaking on each iteration of the loop.
In this example you use SayText. However, there is also a SayTextSynchronous operation. The difference is that SayTextSynchronous delays sending a response until the service has finished speaking. This might be important for some applications. In this case though, the loop runs 10 times and queues up a series of speech messages. If you watch the diagram in the debugger (running at full speed) you will see that it finishes long before the computer finishes saying all of the messages. Saying the messages is not done inside the loop, so it does not slow down the loop.
To finish the tutorial, insert another Data activity block and connect it to the top output connection of the If activity block. Choose string from the drop-down list and type All done! into the text box of the Data activity block. (Notice that when you are defining string data you do not have to enclose it in quotation marks). Now add another TextToSpeech activity block. (You should copy and paste the existing one which means that you want to use the existing instance of the service. If you drag another TextToSpeech block from the services panel you will create a new instance with the name TextToSpeech0 which is not correct.) Connect the Data activity block to the TextToSpeech block that you just copied. Select From: DataValue, To: SayText in the Connections dialog box. In the Data Connections dialog, select value to map to SpeechText.
Your finished diagram should look like the following illustration.
Run the Program
Now, if you connected everything up correctly, you should be able to Run the application (by selecting the Start command on the Run menu or pressing F5). You will hear your PC count to ten and then say "All done". If you don't hear anything, check your connections and the volume for your speakers. (If you don't have speakers you could use the Simple Dialog service used in VPL Tutorial 1 - Hello World in place of the TextToSpeech service.)
Try VPL Tutorial 3 - Create Your Own Activity to learn more about VPL and how to create your own activity.
VPL Tutorials: VPL Tutorial 3 - Create Your Own Activity
VPL User Guide: Getting Started
© 2012 Microsoft Corporation. All Rights Reserved.