Задани на лабораторные работы. ПРК / Professional Microsoft Robotics Developer Studio
.pdf
www.it-ebooks.info
Chapter 10: Microsoft Visual Programming Language Basics
Two variables are defined: Start and End. They are given initial values by the Data activities in the upper left. When both Variable activities have been initialized, the Join activity immediately after them passes a message to the Calculate activity, which then sends a message that contains the value of the Start variable. This message is passed to the TexttoSpeechTTS activity and to an If activity. If the value of the Start variable is less than the End variable, then a message is passed along to the Calculate activity on the far right, which calculates the value of Start+1. This value is set back into the Start variable and the value of the Start variable is placed again on the wire to traverse the loop again. This continues until the Start variable is equal to the End variable, at which point no message is passed on to the Calculate block and execution stops.
There are a few interesting things to note in this diagram. This is the first time that the Merge activity has been used. When it receives an input message on any of its inputs, it simply passes that message to its output. Unlike the Join activity, it doesn’t wait until all of its inputs have received a message. The Merge activity will sometimes display a yellow exclamation point with a warning that indicates a loop has been detected. VPL is trying to gently encourage you to implement loops with recursion, rather than use the Merge activity. This keeps the diagrams simpler and more readable. Implementing loops with recursion is covered in the section “Refining a Custom Activity.”
The Calculate boxes that use the values of the Start and End variables don’t use any of the values that are on the wire. They only use the variables stored in the diagram state. The two Calculate boxes that place the value of state.Start on the wire prior to the Merge activity are simply there to ensure that the same message is on both connections leading into the Merge activity. The Merge activity requires the messages it receives on all of its inputs to be of the same type. These messages are not actually used by any activity.
In VPL, loops are implemented as literal loops in the diagram with an If activity or other conditional to ensure that the loop doesn’t continue forever.
Run the diagram to hear the beautiful sound of the numbers from 1,000,002 to 1,000,012 being read aloud. Listen carefully and you may hear one or more of the numbers read out of sequence. This occurs because there are two possible routes for messages to take after the Merge activity. VPL schedules processing for each route asynchronously. Occasionally, a message makes it all the way through the If activity and back to the Merge before the first message has been processed on its way to the TexttoSpeechTTS activity. This can cause a later message to be posted to the TexttoSpeech activity before an earlier one is posted, causing the out-of-order number reading.
The diagram in Figure 10-11 solves this problem. You can load it from the Chapter10 directory in 6-Loops (in order).
483
www.it-ebooks.info
Part III: Visual Programming Language
Figure 10-11
This diagram adds a Join activity after the Variable activity where the new Start variable value is saved. The Join activity will wait until the TexttoSpeechTTS activity has completed saying the previous phrase before passing an output message to the Calculate activity in the lower-right corner. This keeps the rest of the loop executing at the same speed as the TexttoSpeechTTS activity and prevents multiple SayText messages from being queued at the TexttoSpeechTTS input.
The VPL Execution Model
The 6-Loop (in order) diagram shown in Figure 10-11 provides a good opportunity to discuss the way in which VPL executes a diagram. It helps to understand how VPL evaluates activities and propagates messages when debugging a diagram.
A diagram is inactive until a message enters it. The message can originate from a notification on an activity or from a message that is injected into the inputs of a top-level Data activity. As long as a message is actively propagating in the diagram, VPL continues its execution.
You can see how VPL evaluates a diagram by manually tracing messages through the diagram. For the diagram shown in Figure 10-11, the diagram is inactive until VPL injects an input message into each of the upper-left Data activities. Because two messages are now active, VPL schedules a parallel thread of execution to evaluate each of them. One message propagates through the topmost Data activity and its output message propagates to the topmost Variable activity. It sets the value of the Start variable and
484
www.it-ebooks.info
Chapter 10: Microsoft Visual Programming Language Basics
its output message propagates to the Join activity. The Join activity has not yet received a message on its msg0 input so that evaluation thread terminates because it can no longer propagate a message.
Meanwhile, the second message propagates through the lower Data activity and to the lower Variable activity where the End variable is set, and then the message propagates to the Join activity. Because the Join activity has now received a message on both of its inputs, an output message is able to propagate and this thread continues to execute.
The message moves to the Calculate activity and through the Merge activity. The message is then sent to two activities: If and TexttoSpeechTTS. VPL spawns another thread to evaluate the message that goes to the TextoSpeechTTS activity and the previous thread continues to evaluate the diagram at the If activity. If state.Start < state.End, the If activity output message moves to the Calculate activity, through the Variable activity, and then it gets to the Join activity. Because the Join activity has not received a message at its msg0 input, no further message can be propagated and the thread is terminated.
At the same time, the TexttoSpeechTTS thread continues to execute. After this activity has completed speaking the text that was passed to it, it sends an output message to the msg0 input of the Join activity. Because a message was already received at the msg input, the Join activity propagates a message to the Calculate activity, which passes a message to the Merge activity and the loop is evaluated once again. When the message has been around the loop enough times that state.Start is equal to state.End, the output message from the Merge is sent to the If activity and, once again, another thread is spawned to evaluate the message in the TexttoSpeechTTS activity. This time, no message is propagated from the output of the If activity because its condition is false. This causes the original thread to terminate, leaving only the thread, which is running the TexttoSpeechTTS activity. When this activity completes, it sends an output message to the msg0 input of the Join activity. Because the Join activity has not received a message on its msg input, this message cannot propagate and the last thread is terminated. Because all threads have now terminated, the diagram is again inactive. There are no other sources of messages in the diagram and so it remains inactive until it is stopped.
Debugging a VPL Diagram
It is useful to be able to manually trace messages through a diagram but it is much nicer to let the computer do all the busy work of this process. That is what the VPL debugger is for. You can run a diagram in debug mode by clicking Run
Debug Start. This starts VPL in single-step mode. The debugger output is shown in a web page that is launched when the diagram starts execution.
The debugger is known to work with Internet Explorer but it may not work with other browsers. You should set your default browser to Internet Explorer prior to debugging VPL diagrams.
The debugger page is divided into four sections:
Diagram State: This is the topmost section, shown in Figure 10-12. It shows values of all of the state variables in the diagram.
485
www.it-ebooks.info
Part III: Visual Programming Language
Figure 10-12
Current Node: The next section is a graphical representation of the diagram, much like the one shown in VPL. The difference is that this diagram is annotated to show the currently executing activity, activities that have been scheduled for execution, and breakpoints. As shown in Figure 10-13 (without the color), the currently executing activity is outlined with bright red, while other activities that are scheduled for execution are outlined with dark red. A red circle is shown next to activities that have a breakpoint set.
Next Node to Execute Outlined in Red with the play icon displayed
This node has a breakpoint set indicated by the red circle.
Figure 10-13
If the button on the active activity is clicked, VPL will step to the next activity. This can also be accomplished by pressing the Step button at the top of this section. The other two buttons can be used to run the diagram at slow speed or regular speed. The slow-speed execution is useful to Vtrace through large diagrams without having to continually press the Step button.
486
www.it-ebooks.info
Chapter 10: Microsoft Visual Programming Language Basics
At the bottom of this section, the value of the message on the input pin of the current activity is shown.
Breakpoints: The next section shows the currently set breakpoints, as shown in Figure 10-14. If a breakpoint has been set on an activity, execution will stop when it becomes the active activity. Breakpoints can be cleared, enabled, and disabled in this section.
Figure 10-14
Pending Nodes: The fourth section shows all activities scheduled to be executed, as shown in Figure 10-15. As described in the previous section, multiple activities can be executing at the same time on multiple threads. A breakpoint can be set on any of the scheduled activities by pressing the corresponding SetBP button.
Figure 10-15
The diagram can be terminated by pressing the Stop button on the Run dialog, just as when the diagram is not running in debugger mode.
Creating Custom Activities
As shown in Figure 10-11, simple functions can be composed of many different activity blocks. Real-world diagrams can quickly become unmanageable. Fortunately, VPL enables custom activities to be defined that are similar to subroutines in imperative languages.
The ForLoop function defined in Figure 10-11 is a good candidate to become a custom activity because it is a function that might be used multiple times in a diagram and it is self-contained. This section starts by showing you how to define a custom activity to implement a ForLoop. Later you will improve that custom activity to make the ForLoop execute in order.
487
www.it-ebooks.info
Part III: Visual Programming Language
Defining a ForLoop Custom Activity
Defining a custom activity is very similar to defining a diagram except that the input and output pins must be defined and connected. The following steps show how to define a custom activity that implements the ForLoop functionality. You can see the final result in the 7-CustomActivity project in the Chapter10 directory.
1.
2.
3.
4.
5.
6.
Start out with a new project by clicking File
New.
Drag an Activity block from the Basic Activities toolbox.
Give the activity a name such as ForLoop by typing it in the Name: box in the Properties toolbox. Give it a friendly name and a description as well.
Double-click the ForLoop activity to open its diagram. The diagram page looks similar to the top-level diagram except that now input and output pins are present.
Define the actions that this activity will support by clicking Edit
Actions and Notifications. The Actions and Notifications dialog is displayed.
Add a new action by pressing the Add button in the Actions column. Name the action by typing Initialize in the box. Add two input values by pressing the Add button in the Input values column. Name the input values StartValue and EndValue and make them both of type int. A properly configured dialog is shown in Figure 10-16.
Figure 10-16
488
www.it-ebooks.info
Chapter 10: Microsoft Visual Programming Language Basics
7. Select the Notifications tab on the Actions and Notifications dialog. Add a notification by clicking the Add button in the Notifications column. Name the new notification “CountNotification.” Add a notification value by clicking the Add button in the Notification values column. Name the value “Count” and give it the type of integer. A properly configured Notifications tab is shown in Figure 10-17.
Figure 10-17
8. The ForLoop activity now supports an Initialize action that expects an input message with two integer values, StartValue and EndValue, as well as a notification output that contains a single integer called Count. Make sure that the Initialize action is selected on the Action drop-down menu at the top of the ForLoop diagram.
9. Add actions to the diagram to match the diagram in Figure 10-18. The upper Calculate activity extracts the StartValue value from the input message and then waits for the EndValue to be processed before starting the loop. The lower Calculate activity extracts the EndValue value from the input message and then sets it in the End state variable. This implementation of the ForLoop does not store the incremented value in a state variable as the last implementation did. The state is maintained in the messages that are passed between activities. When both input values have been processed, a response message is sent to the outside world via the result pin. Because we didn’t specify any specific types for output messages, any message will do. The Join activity passes a message that once again contains both values. The Calculate activity extracts the StartValue value and passes it to the If activity, which only passes along a message if the value on the wire is less than the End state variable. This message is passed back to the outside world as a CountNotification that contains the count value as an integer. The value on the wire is incremented and passed back to the If activity to be checked again.
489
www.it-ebooks.info
Part III: Visual Programming Language
Figure 10-18
10. Select the main diagram tab. Add actions to the diagram to match Figure 10-19. The two Data activities are combined into a single message with the Join activity and sent to the Initialize action on the ForLoop activity. Copy and Paste the ForLoop activity to create another instance of
it and connect its CountNotification output to the SayText action of the TextoSpeechTTS activity.
Figure 10-19
490
www.it-ebooks.info
Chapter 10: Microsoft Visual Programming Language Basics
The top-level diagram is now much simpler because much of the complexity has been hidden in the ForLoop activity. It is important to design VPL diagrams in this hierarchical fashion to keep complex diagrams from becoming unwieldy.
Improving the ForLoop Custom Activity
In some ways, this diagram is a step backward from the previous one because your iteration loop has no feedback to ensure that the count is always done in the right order. In this section, you’ll see how to improve the ForLoop custom activity to provide this feedback
The 8-CustomActivity (in order) project in the Chapter10 directory improves upon the diagram in the previous section by introducing a custom activity with multiple actions. It also uses fewer total activities.
The top-level diagram is shown in Figure 10-20.
Figure 10-20
The ForLoop activity appears three times in this diagram but each reference is to the same activity. The diagram begins by combining the Start and End values into a single message that is passed to the Initialize action of the ForLoop activity. The ForLoop then sends a CountNotification containing the first count to the TexttoSpeechTTS activity. When this activity has completed, it sends a message back to the Count action of the ForLoop, which causes another CountNotification. This continues until the count reaches the End value and then the ForLoop issues no more CountNotifications. This is a clean way to use the ForLoop activity and it provides the needed synchronization to keep the count from getting out of order.
491
www.it-ebooks.info
Part III: Visual Programming Language
The new Initialize action in the ForLoop activity is shown in Figure 10-21.
Figure 10-21
You can specify multiple actions in an activity by creating them in the Actions and Notification dialog. You then specify inputs and outputs for each action. The Action: drop-down menu at the top of the diagram enables you to select the action you are working with. Each action is independent from all the others except that they all refer to the same state variables.
In the Initialize action, the StartValue and EndValue parts of the input message are separated and stored in their respective state variables. When both values have been stored, a message is sent to the result pin to indicate that the operation has completed. You may have noticed that the ForLoop activity appears in its own action definition. This is called recursion. It is perfectly legal to pass a message from one action to the input of another action by referring to the activity in this way. Simply use the Edit menu to copy the ForLoop activity from the main diagram and then paste it into this diagram. The output from the Join activity passes a message to the Count action on the ForLoop activity. The definition for this action is shown in Figure 10-22.
492
