Задани на лабораторные работы. ПРК / Professional Microsoft Robotics Developer Studio
.pdf
www.it-ebooks.info
Chapter 10: Microsoft Visual Programming Language Basics
VPL is a data flow language that enables you to specify one or more MRDS services to process data in parallel and to define the data connections between those services. In a robotics context, it is useful for writing high-level orchestration services that control robot behavior. In the rest of this chapter, you will learn how to implement algorithms using VPL.
The VPL Development Environment
Start the VPL development environment by clicking Start
Visual Programming Language. A window similar to the one shown in Figure 10-2 is displayed.
Figure 10-2
Note the following about the environment:
The main commands are accessible from the main menu bar and the most common commands are on the toolbar.
The work area consists of a central diagram area surrounded by four toolboxes. A diagram consists of one or more activity boxes that are connected together. You drag activity boxes from the various toolboxes to the central area. These toolboxes include the following:
The Basic Activities toolbox contains blocks that control data flow and create data and variables.
The Service toolbox shows all of the services available to VPL. This list includes all of the services that are present in the MRDS bin directory.
473
www.it-ebooks.info
Part III: Visual Programming Language
The Project toolbox shows the diagrams and service configuration files included in your project.
The Properties toolbox shows the properties for the currently selected item so that you can view and edit the settings exposed by a service or activity.
A Simple VPL Diagram
VPL programs are called diagrams because they are defined by placing graphical icons in a diagram and connecting them with data flow connections in the VPL development environment. Once the diagram has been defined, it can be executed within the VPL development environment or you can generate an MRDS service from the diagram and run that.
The easiest way to see how a diagram works is to actually build one. The following sections describe how to create a new diagram and run it.
Creating a VPL Diagram
To create a program, follow these steps:
1.
2.
3.
Start with a fresh diagram by clicking File
New. The environment should look like Figure 10-2. Drag a Data activity from the Basic Activities toolbox into the diagram space.
Click the data type drop-down arrow at the bottom of the activity and select string for the type of data. Type a string such as, Hello world, I’m VPL!, in the input field of the Data activity. The activity should look like Figure 10-3.
Figure 10-3
Now drag the TexttoSpeechTTS activity from the Services toolbox. The Services toolbox contains a lot of entries so it often saves time to type part of the name of the service that you need in the Search box above the list of services. This narrows the list down to only those services that contain the search text. Position the TexttoSpeechTTS activity to the right of the Data activity.
474
www.it-ebooks.info
Chapter 10: Microsoft Visual Programming Language Basics
Search Groups in the Services Toolbox
The Services toolbox contains many services, so the Search box at the top is very helpful. You will probably find that you do some of the same searches repeatedly. The Services toolbox provides a way for you to save common searches. Type dialog into the Search box and a few services are displayed. Now click the plus ( ) symbol next to All Found. This creates another heading in the services list that contains all the services that this search finds. You can expand or contract the list using the icon to the left of the dialog heading. All the other services are found under the All Found heading. When you no longer want to keep the dialog search results, you can click the X symbol to the right of this heading and it is deleted.
4. Place the mouse on the right output pin of the Data activity and drag the mouse to the TexttoSpeechTTS activity. Notice that the corners of the TexttoSpeechTTS activity turn green, indicating that this connection is allowed. When you release the mouse button, the Connection dialog appears. This dialog enables you to select which message is being output from the source activity and which message accepts the input in the destination activity. In this case, select DataValue in the From: box and SayText in the To: box and press OK.
5. Now the Data Connections dialog box opens. This enables you to map a particular data value in the message output from the source to a target value in the message input to the destination. Because the data value being passed is a string, it has a value and a length. Select value in the Value box and SpeechText in the Target box. A connection should appear between the two activities in the diagram as shown in Figure 10-4.
Data |
TexttoSpeechTTS |
Hello world, I'm VPL! |
SayText |
string |
|
Figure 10-4
Congratulations! You have just written your first VPL diagram. Save it by clicking File
Save As and select an appropriate directory and project name. If you have any trouble creating this diagram, you can find it in the Chapter10 directory in the 1-Hello folder.
Running a VPL Diagram
To run a diagram, first click File
Open to load a previously saved diagram. Then follow these steps:
1. Click Run
Start and press F5, or press the green forward arrow in the toolbar to run your program. A dialog box appears that shows the console output as the program is executed. After a few seconds, you should hear your computer speak the words in the Data activity box. The console output window is shown in Figure 10-5.
475
www.it-ebooks.info
Part III: Visual Programming Language
Figure 10-5
2. After the program has finished its execution, press the Stop button to dismiss the Run dialog.
If Your Computer Won’t Talk
If you run the Hello diagram and you aren’t able to hear the computer speak the words, check the connections to your speakers and the volume. Select the Speech item in the system control panel and verify that the speech engine is configured properly. If you still cannot hear the computer speak, you can substitute the SimpleDialog activity for the TexttoSpeechTTS activity in your diagram to display the words in a dialog box instead. The 2-Hello VPL project in the Chapter10 directory shows how this is done.
Let’s take a look at exactly what happened when this basic little diagram in Figure 10-4 was executed. The Data activity has a very simple job. When it receives a message on its input pin, it sends a message on its output pin containing the data specified. Where did the input message come from? Its input pin is unconnected. It turns out that VPL automatically sends an input message to all top-level Data activities when the program starts.
Up until now, we’ve been speaking about messages on wires in pretty general terms, but it’s time to get more specific. You can think of a message much like a C# or C++ structure. It has one or more fields and
476
www.it-ebooks.info
Chapter 10: Microsoft Visual Programming Language Basics
each has a name and a type. When you specify values in the Data Connections dialog, you are actually selecting which field of the structure to send to a target value in the destination activity.
Go back to the 1-Hello project and save it to a different project name. This is important because when you make a modification to a project and then press F5 to run it, VPL automatically saves it. If you make modifications to 1-Hello and then run it, you will modify the project that was installed as part of the book software.
Select the connection between the Data activity and the TexttoSpeechTTS activity. The properties of the connection are shown in the Properties toolbox. The properties closely resemble the contents of the Data Connections dialog. Change the Value property to Length as shown in Figure 10-6.
Figure 10-6
Now, when you execute the diagram, the engine says “22” because the length of the data string was passed to the speech activity instead of its value.
Now you know that the Data activity builds a message that contains a value, which is a string and a value associated with the string, which is its length. This message is passed to the TexttoSpeechTTS activity, where, in the original diagram, the value of the string is assigned to the SpeechText input.
When the TexttoSpeechTTS activity receives a message on its SayText input pin, it speaks the text passed to the SpeechText input and then sends a success or failure message on its output pin. Because nothing is connected to this pin, the message drops on the floor. Not to worry — VPL has a great little floor sweeper that gathers dropped messages and disposes of them properly.
477
www.it-ebooks.info
Part III: Visual Programming Language
Inputs, Outputs, and Notifications
Each activity can have one or more inputs shown as square pins on the left side of the activity icon and one or more outputs shown as square pins on the right side of the icon. When there are multiple inputs or outputs, only a single pin is shown and the desired input or output is selected with the Connections dialog when a connection is made.
Some activities have an additional output called a notification. A single response output message is generated on an output pin as the result of a single input message on an input pin. Notification outputs can also send a message as a result of an input message but they more typically send a message as the result of a change in internal state. As an example, consider the DirectionDialog activity shown in Figure 10-7.
Action |
DirectionDialog |
Result |
||
|
|
|
||
|
|
|
Output Pin |
|
Input Pin |
|
|
|
|
|
|
|
Notification |
|
|
|
|
|
|
|
|
|
|
Output Pin |
|
|
|
|
|
Figure 10-7
When a message is received on the Action Input Pin, a different message is sent on the Result Output Pin, usually indicating success or failure. If the input message changes the internal state of the activity, it typically generates a notification message as well. Sometimes an external action, such as a user input event, changes the state of an activity and it spontaneously generates a notification without any input message at all.
The 3-Notifications diagram in the Chapter10 directory illustrates this behavior. It is shown in Figure 10-8.
Figure 10-8
478
www.it-ebooks.info
Chapter 10: Microsoft Visual Programming Language Basics
In the top row of activities, a message is initially generated by the Data activity on the left, which is sent to the Get input of the DirectionDialog activity. It sends a response message containing its state to the second Data activity, which then sends an output message containing the string “Success Response!” to the SimpleDialog activity, where it is displayed in an AlertDialog. So far you have used the Action Input Pin and the Response Output Pin of the DirectionDialog activity.
On the second row of activities, another DirectionDialog activity is shown on the left. However, this icon actually represents the same activity as the DirectionDialog in the top row. You can tell because they have the same name. When you drag onto the diagram an activity that is already present, VPL asks you whether you want a completely new activity or just another reference to the existing activity. If you specify that you want a new activity, the new activity is given a different name than the original activity. While this might initially seem confusing, it really makes the diagrams simpler because it reduces the rat’s nest of connections coming from a single activity when multiple inputs and outputs are used.
The output from the DirectionDialog activity that is used in the second row is the notification output. This output sends a message to the SimpleDialog activity whenever the internal state of the
DirectionDialog changes. Just like the DirectionDialog, the SimpleDialog on the second row is really just another reference to the same DirectionDialog activity shown on the first row.
This diagram also shows the use of the Comment activity. This activity has no effect on the functionality of the diagram but it provides a way to document what is going on in the diagram.
Run the diagram and notice that the response is immediately displayed as a result of the data input message on the top row of the diagram. Click the buttons on the DirectionDialog and you will see messages displayed as a result of the notifications generated by the DirectionDialog activity on the second row.
Basic Activities
The following activities can be found in the Basic Activities toolbox. These basic activities are used to build the data connections between the activity blocks that represent services. Unlike the service activities, they do not have a unique name.
They are defined briefly in the following table and discussed in more detail in the following sections.
Activity |
Description |
|
|
Variable |
This activity is used to get or set the value of |
|
a state variable. |
Calculate |
This activity is used to calculate a value from |
|
one or more input values. |
Data |
This activity is used to define static data that |
|
is used as an input to another activity. |
|
|
|
(Continued ) |
479
www.it-ebooks.info
Part III: Visual Programming Language
Activity |
Description |
Join |
This activity waits to send an output |
|
message until it has received a message on |
|
all of its inputs. |
Merge |
This activity forwards a message to its |
|
output from any of its inputs. |
If |
This activity forwards a message on the first |
|
output that has an expression that evaluates |
|
to true. |
Switch |
This activity forwards a message on the first |
|
output that matches the input value. |
List |
This activity holds a list of data. |
List |
This activity performs various operations on |
Functions |
a list of data. |
Comment |
This activity displays a comment in the |
|
diagram. |
Activity |
This activity can be used to define a custom |
|
activity that, in turn, contains other activities. |
|
|
VPL Variables
In a data flow diagram, the program state is mostly encapsulated in the messages passed between activities. Sometimes it is necessary to define state that belongs to the diagram. This state is analogous to the state in a service. Activities can set or retrieve this state. This is useful because activities do not, by default, pass the same data to their output that they receive on their input.
You use the Variable activity to define and set or get the value of a variable. When you drag a Variable activity from the Basic Activities toolbox onto the diagram, it is initially blank. Click the ellipse on the bottom of the activity to bring up the Define Variables dialog box, which enables you to add and delete variables. Any Variable activity in the diagram can represent any defined variable.
Load the 4-Variables diagram in the Chapter10 directory. It is shown in Figure 10-9.
480
www.it-ebooks.info
Chapter 10: Microsoft Visual Programming Language Basics
Figure 10-9
A string variable called Previous is defined and initialized using the Data and Variable activities in the upper-left corner. When a button is pressed in the DirectionDialog, a notification is sent to three different activities. The first activity to take action is the Variable activity, which gets the value of the variable and sends it to the Join activity connected to its output.
This is the first time we’ve used a Join activity. Its function is very simple. It waits for an input message on both of its input pins and then combines the messages into a single message and sends it on its output pin. In Figure 10-9, when the Join activity receives the output from the Variable activity and the notification from the DirectionDialog, it combines these two messages into one and sends it to the Calculate activity, which performs arithmetic, logical, or string operations on its input values according to the expression entered in the activity. These operations are shown the following table:
Type of Operation |
Operation |
|
|
Arithmetic |
+, , *, / and % for modulo |
Logical |
&&, |, and ! |
|
|
The plus operator (+) can also be used to concatenate strings. Parentheses can be used to group operations.
The operation defined in the Calculate activity in Figure 10-9 is ”Previous:” + msg0.Previous + “ New:” + msg.Name. This builds a string that contains literals combined with the string values of msg0
.Previous and msg.Name. The message is displayed using the SimpleDialog activity.
481
www.it-ebooks.info
Part III: Visual Programming Language
All that remains is to update the value of the Previous variable. This is done with the Join activity on the bottom row. It waits until the string has been built and then combines this message with the original notification message from the DirectionDialog activity. The Calculate activity extracts the Name string from the original DirectionDialog notification and passes it to a variable activity to be stored. The Join at the bottom of the diagram is necessary because you must be certain that the string has been constructed before you change the value of the variable. Without this Join, there is a race condition between constructing the string and updating the variable that can cause unpredictable results.
Run the diagram and notice that the message displayed contains both the current button press as well as the previous button press.
Looping and Conditionals in VPL
No language is complete without the ability to make logical decisions and iterate through loops. In an imperative language, you use an If statement to make a decision and a For or a While statement to iterate. In VPL, decisions are made with an If activity, and iteration is done using a data flow loop.
You might expect to find a basic ForLoop activity in the Basic Activities toolbox, but it isn’t there. That’s because a ForLoop cannot usually be implemented in VPL with a single activity box. As you will see in the next few examples, you must take special care to ensure that the ForLoop executes in the proper order.
The diagram in Figure 10-10 shows how a simple ForLoop can be implemented in VPL. You can find it in the Chapter10 directory under “5-Loops.”
Figure 10-10
482
