Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Задани на лабораторные работы. ПРК / Professional Microsoft Robotics Developer Studio

.pdf
Скачиваний:
126
Добавлен:
20.04.2015
Размер:
16.82 Mб
Скачать

www.it-ebooks.info

Chapter 11: Visually Programming Robots

Figure 11-1

The following properties are supported:

NamespacePrefix: This prefix is prepended to every namespace in the project. It enables

you to use namespaces in the generated service that correspond to your particular organization or group.

ContractPrefix: This prefix is used as the first part of the contract identifier for the service. For example, the contract identifier constructed for this service is derived from the ContractPrefix, the current date, and the name of the main diagram: http://schemas.tempuri.org/ 2007/12/customactivityorder/customactivityinorder.html.

KeyLocation: This is the location of the key file used for signing the service code.

SourceLocation: This is the directory where the source code for the service will reside.

IncludeDebugInfo: This determines whether the service is compiled with debug information present.

TargetFramework: This specifies whether the service targets .NET V2.0 or the .NET Compact Framework V2.0.

A Service Compilation Example

Open the 8-CustomActivity (in order) project in the chapter10 directory and compile it by selecting the item on the Build menu. A compiled version of this diagram is in ProMRDS\chapter11\ CompiledCustomActivity. The source diagram is shown in Figure 11-2.

503

www.it-ebooks.info

Part III: Visual Programming Language

Figure 11-2

The files generated by the VPL compilation are as follows:

CustomActivityInOrderService.cs

CustomActivityInOrderTypes.cs

CustomActivityorder.csproj

CustomActivityorder.csproj.user

CustomActivityorder.manifest.xml

ForLoopService.cs

ForLoopTypes.cs

VPL has taken the .mvpl filename and converted it to a valid project name: CustomActivityorder

.csproj. The associated .user file specifies a reference path and debug actions that cause the manifest to be executed when F5 is pressed. This project contains everything needed to build the target service.

VPL has also generated a manifest that can be used to run the target service. The source code for the manifest shows all of the services that are executed:

<?xml version=”1.0”?>

<Manifest xmlns:customactivityinorder= “http://schemas.tempuri.org/2007/12/customactivityorder/customactivityinorder.html” xmlns:texttospeech=”http://schemas.microsoft.com/2006/05/texttospeech.html” xmlns:this=”urn:uuid:996d98f0-d63b-40a1-bb40-312c8cb2fac3” xmlns:dssp=”http://schemas.microsoft.com/xw/2004/10/dssp.html” xmlns:forloop=”http://schemas.tempuri.org/2007/12/customactivityorder/forloop.html” xmlns=”http://schemas.microsoft.com/xw/2004/10/manifest.html”>

504

www.it-ebooks.info

Chapter 11: Visually Programming Robots

<CreateServiceList> <ServiceRecordType>

<dssp:Contract> http://schemas.tempuri.org/2007/12/customactivityorder/customactivityinorder.html

</dssp:Contract> <dssp:PartnerList> <dssp:Partner>

<dssp:Contract> http://schemas.microsoft.com/2006/05/texttospeech.html

</dssp:Contract> <dssp:PartnerList />

<dssp:Name>customactivityinorder:TexttoSpeechTTS</dssp:Name> <dssp:ServiceName>this:TexttoSpeechTTS</dssp:ServiceName>

</dssp:Partner> <dssp:Partner>

<dssp:Contract> http://schemas.tempuri.org/2007/12/customactivityorder/forloop.html

</dssp:Contract> <dssp:PartnerList />

<dssp:Name>customactivityinorder:ForLoop</dssp:Name> <dssp:ServiceName>this:ForLoop</dssp:ServiceName>

</dssp:Partner> </dssp:PartnerList>

<Name>this:CustomActivityInOrder</Name> </ServiceRecordType>

<ServiceRecordType> <dssp:Contract>

http://schemas.microsoft.com/2006/05/texttospeech.html </dssp:Contract>

<dssp:PartnerList /> <Name>this:TexttoSpeechTTS</Name>

</ServiceRecordType> <ServiceRecordType> <dssp:Contract>

http://schemas.tempuri.org/2007/12/customactivityorder/forloop.html </dssp:Contract>

<dssp:PartnerList /> <Name>this:ForLoop</Name>

</ServiceRecordType> </CreateServiceList>

</Manifest>

Three services are started in this manifest that are identified by their contract identifiers:

http://schemas.tempuri.org/2007/12/customactivityorder/customactivityinorder.html

http://schemas.microsoft.com/2006/05/texttospeech.html

http://schemas.tempuri.org/2007/12/customactivityorder/forloop.html

The texttospeech service corresponds to the TexttoSpeechTTS activity in the top-level diagram. The forloop service corresponds to the ForLoop custom activity, and the customactivityinorder service corresponds to the top-level diagram. Both the texttospeech and forloop services are specified as partners to the customactivityinorder service.

505

www.it-ebooks.info

Part III: Visual Programming Language

Open the CustomActivityorder.csproj project. There are four source files. The

CustomActivityInOrderService.cs and CustomActivityInOrderTypes.cs files implement the customactivityinorder service, while the ForLoopService.cs and ForLoopTypes.cs files implement the forloop service. Both of these services are contained in the target assembly called

CustomActivityorder.Y2007.M12.dll.

It is worthwhile to take some time to look at the generated code to better understand how the VPL diagram maps to actual service code. The first few lines of code in CustomActivityInOrderService.cs look very familiar because they are nearly identical to many of the other services you have looked at in previous chapters:

namespace ProMRDS.Vpltest.CustomActivityInOrder

{

[DisplayName(“Vpltest”)]

[Description(“A diagram that illustrates a custom activity.”)] [dssa.Contract(Contract.Identifier)]

public class CustomActivityInOrderService : dssm.DsspServiceBase

{

//Service state [dssa.InitialStatePartner(Optional = true)] private CustomActivityInOrderState _state;

//Service operations port

[dssa.ServicePort(“/Vpltest”, AllowMultipleInstances = false)] private CustomActivityInOrderOperations _mainPort =

new CustomActivityInOrderOperations();

The state for the service is declared with the type CustomActivityInOrderState, which has the following definition. This class has no members because the top-level diagram has no state variables.

namespace ProMRDS.Vpltest.CustomActivityInOrder

{

static class Contract

{

public const string Identifier = “http://schemas.tempuri.org/2007/12/vpltest/customactivityinorder.html”;

}

[dssa.DataContract]

public class CustomActivityInOrderState

{

}

The CustomActivityInOrderOperations class contains fairly standard operations, including HttpGet, Get, Replace, Subscribe, and a generic operation called Action. The handler for this operation does nothing but return an empty response message.

In the Start method, the state is initialized and the DoStart iterator is started to begin the diagram execution. In this iterator, subscriptions to service notifications are set up. Because there is only one case where a notification output is used in this diagram (CountNotification), only a single subscription is initialized.

506

www.it-ebooks.info

Chapter 11: Visually Programming Robots

Now the RunHandler is executed to take care of the initial message propagation in the diagram due to top-level data blocks. The relevant code is shown here:

public IEnumerator<ccr.ITask> RunHandler()

{

Increment();

JoinAlpha a = new JoinAlpha(); a.EndValue = 72;

a.StartValue = 57;

ForLoop.InitializeRequest request = new ForLoop.InitializeRequest(); request.EndValue = a.EndValue;

request.StartValue = a.StartValue;

Increment();

Activate(

ccr.Arbiter.Choice(

ForLoopPort.Initialize(request),

OnInitializeSuccess, delegate(soap.Fault fault)

{

base.FaultHandler(fault, @”ForLoopPort.Initialize(request)”); Decrement();

}

)

);

Decrement();

yield return WaitUntilComplete();

}

The JoinAlpha class represents the Join activity in the top-level diagram. Its two input values are initialized with the values from the Data activities and then a ForLoop.InitializeRequest object is created and initialized with the values and posted to the ForLoopPort. If the response from the ForLoop is success, nothing is done and the RunHandler waits until it receives a drop request.

This behavior fully implements the functionality specified on the top line of the main diagram that initializes the ForLoop activity and then waits for a notification from that activity with the first count value.

The second line of activities in the main diagram begins executing when the ForLoop service sends a CountNotification. This is handled in the _joinAlphaHandler method, shown here:

void _joinAlphaHandler(object[] args)

{

JoinAlpha message = new JoinAlpha(args);

Increment();

Activate(

ccr.Arbiter.Choice(

TexttoSpeechTTSPort.SayText((texttospeech.SayTextRequest)message),

(continued)

507

www.it-ebooks.info

Part III: Visual Programming Language

(continued)

OnSayTextSuccess, delegate(soap.Fault fault)

{

base.FaultHandler(fault,

@”TexttoSpeechTTSPort.SayText((texttospeech.SayTextRequest)message)”);

Decrement();

}

)

);

Decrement(args.Length);

}

void OnSayTextSuccess(dssp.DefaultUpdateResponseType response)

{

ForLoopPort.Count(new ForLoop.CountRequest()); Decrement();

}

The message received in the notification is posted to the TexttoSpeechTTSPort in a SayText message. On success, the OnSayTextSuccess handler executes and posts a new CountRequest message to the ForLoopPort to start the next count.

The RunLoopService code is similar. The implementation of the CountAction is in the RunHandler method of the CountMessageHandler class, and the implementation of the Initialize Action is in the RunHandler method of the InitializeMessageHandler class.

Occasionally, errors may be generated when a diagram is compiled that don’t show up when it is executed with the interpreter. The interpreter tends to be more forgiving than the C# compiler. Check the errors reported by the compiler to determine what action needs to be taken to fix the service. Sometimes it requires defining the data members of input and output messages that weren’t previously defined.

Diagram compilation is a relatively new feature in version 1.5 of MRDS, so it is possible that a bug in the code generator may prevent the diagram from compiling correctly. In this case, it may be possible to manually modify the generated code to fix the compilation errors. (Be sure to report errors in diagram compilation on the MRDS forum so that they can be fixed.)

Now that this diagram has been compiled, it is possible to use the custom activities in the diagram in other diagrams. The next time you run VPL, the CustomActivityorderForLoop service will appear in the Services toolbox and it can be used as a standalone activity in any other diagram. Although the CustomActivityorder service, which represents the top-level diagram, also appears in the list, it doesn’t provide any useful inputs or outputs to make it useful in another diagram.

Configuring Activities

Now that you know that a VPL activity can represent a service, all you need to do to make VPL work with the many robotics services available is to associate a particular activity with a particular service.

In many cases, the association is implicit. For example, when you used the TexttoSpeechTTS activity in

508

www.it-ebooks.info

Chapter 11: Visually Programming Robots

the previous chapter, VPL automatically started the TexttoSpeech service. In some cases, the service in the Services toolbox represents a generic contract containing the definition of a port but no

implementation. In this case, you need to explicitly associate the activity with a service that implements that generic contract.

Another reason to configure a diagram is that sometimes a number of services need to run together and not all of them are specified in the diagram. In this case, you associate a manifest with the diagram, and all of the services in the manifest are started together.

Setting the Configuration for a Diagram

Let’s try an example to see how this works. Start up a fresh copy of VPL:

1. Drag the GenericContactSensors activity onto the diagram and connect its

ContactSensorUpdate notification to the SayText input of a TexttoSpeechTTS activity.

In the DataConnections dialog, connect the Pressed Value to the SpeechText input as shown in Figure 11-3.

Figure 11-3

2. Press F5 to run the diagram. VPL is unable to start the service associated with the GenericContactSensors activity because it refers to a generic contract that has not been implemented. The error shown in Figure 11-4 is displayed in the browser.

509

www.it-ebooks.info

Part III: Visual Programming Language

Figure 11-4

3. To make this work properly, you must tell VPL the service to run that implements the GenericContactSensor contract. The simplest way to do this is to associate an existing manifest with the diagram. You will use a simulation manifest to keep things simple for now. Right-click the GenericContactSensors activity and select Set Configuration. In the configuration screen, select Use a Manifest and then click the Import Manifest button. A list of all the manifests under \Microsoft Robotics Studio (1.5), which contains a service that implements the GenericContactSensors contract, is shown in Figure 11-5.

Figure 11-5

510

www.it-ebooks.info

Chapter 11: Visually Programming Robots

4. Select the LEGO.NXT.Tribot.Simulation.manifest.xml manifest and click OK. VPL now copies this manifest into the VPL project folder along with any associated configuration files. It associates the GenericContactSensors activity with the SimulatedGenericContactSensors service started in the manifest.

5. Now press F5 and the results are quite different, as shown in Figure 11-6.

Figure 11-6

This manifest starts a number of services, including SimulationEngine. The configuration file for the SimulationEngine service contains a definition for the scene shown in Figure 11-6. Now you just need to find a way to press the simulated LEGO touch sensor to test the diagram. The easiest way to do this is to drive the LEGO Tribot into one of the obstacles in the scene, but you don’t have any way to drive it because the manifest didn’t start the SimpleDashboard service.

Starting the SimpleDashboard Service Upon Execution

You could easily start the SimpleDashboard service at this point by opening a browser window, typing http://localhost:50000 in the address window, selecting ControlPanel and then starting the SimpleDashboard service. However, it is much nicer to configure the diagram so that the SimpleDashboard service is always launched when this diagram is executed. This is as simple as dragging the SimpleDashboard activity from the Services toolbox onto the diagram. This adds the SimpleDashboard service as a partner to the main diagram, which causes it to start when the diagram is executed.

511

www.it-ebooks.info

Part III: Visual Programming Language

You can load the completed diagram from 1-GenericContactSensorSim in the chapter11 directory:

1.

2.

3.

Run the diagram by pressing F5.

When the SimpleDashboard window appears, type localhost in the Machine: textbox and then double-click the (LEGONXTMotorBase) service in the Service Directory list and verify that the Motor is on in the Differential Drive group box.

Press the Drive button and then drag the directional control to drive the LEGO NXT around the scene. The touch sensor is on the front of the Tribot, so try crashing it into one of the obstacles in the scene. You should hear the TexttoSpeechTTS service announce the state of the touch sensor each time it changes.

Congratulations! You have successfully used VPL to read a sensor from a simulated robot. Next you will see how easy it is to modify the diagram to use a real robot.

Modifying the Diagram to Use a Real Robot

This example requires a LEGO NXT robot. If you don’t have one, you may be able to use a different manifest that supports another robot. Even if you don’t have the hardware, it is a good idea to follow along with the example to learn how you can switch a VPL diagram from working with a simulated robot to a real one.

To modify the diagram, follow these steps:

1.

2.

3.

4.

5.

Expand the Configurations heading in the Project toolbox.

Delete the manifest that is listed there by right-clicking on it and selecting Delete.

Right-click the GenericContactSensor activity and select Set Configuration.

In the Set Configuration screen, select Use a Manifest and click the Import Manifest button.

Select the LEGO.NXT.Tribot.Manifest.xml manifest. This is similar to the simulated Tribot manifest but intended for the real LEGO hardware. Actually, at this point, you can select

any manifest that is listed that works with the hardware you have.

You can load a diagram that has these changes from the 2-GenericContactSensorNXT project in the chapter11 directory.

Make sure that you have set up your hardware according to the instructions in the MRDS documentation under the “Setting Up Your Hardware” topic of the Robotics Tutorials section. Press F5 to run the diagram. This time, instead of seeing the simulation environment, you should be presented with a web page that enables you to configure your LEGO NXT connection. After you have done so and clicked Connect at the bottom, the web page should indicate that the LEGO is connected. Press the touch sensor on the Tribot and you should hear the TexttoSpeechTTS service say the state of the touch sensor.

512