
Задани на лабораторные работы. ПРК / Professional Microsoft Robotics Developer Studio
.pdf
www.it-ebooks.info
Chapter 3: Decentralized Software Services (DSS)
#/d:”..\Config\xxxx*”
#Include any models/meshes
#And the media files
#Now start with the source files
#/d:”..\Chapter2\Chapter2.sln”
#---------- CCR Examples ----------
#Add the Readme files for this application (if any)
#Include the binaries
/d:”..\..\bin\CCRExamples.Y2007.M12.dll” /d:”..\..\bin\CCRExamples.Y2007.M12.proxy.dll” /d:”..\..\bin\CCRExamples.Y2007.M12.transform.dll”
# Now the source code, but none of the compiled stuff /d:”..\Chapter2\CCRExamples\AssemblyInfo.cs” /d:”..\Chapter2\CCRExamples\Ccrexamples.cs” /d:”..\Chapter2\CCRExamples\CCRExamples.csproj” /d:”..\Chapter2\CCRExamples\CCRExamples.csproj.user” /d:”..\Chapter2\CCRExamples\CCRExamples.manifest.xml” /d:”..\Chapter2\CCRExamples\CCRExamples.sln” /d:”..\Chapter2\CCRExamples\CcrexamplesTypes.cs”
4. To build chapter packages, you use a batch script to avoid typing long command lines. This script, called BuildChapterPackage.cmd, contains the following commands:
@echo off
echo Build a single chapter for ProMRDS if “%1” == “” goto usage
if not exist DeployChapter%1Options.txt goto noopts
..\..\bin\dssdeploy /p /e+ /cv+ /s- /n:”ProMRDS Chapter %1 Package” /r:”..\Chapter%1\Readme.htm” @DeployChapter%1Options.txt ProMRDSChapter%1.exe copy ProMRDSChapter%1.exe c:\temp
copy /Y ProMRDSChapter%1.exe ProMRDSChapter%1.exe.safe goto end
:usage
echo Usage: BuildChapterPackage num echo where num is the Chapter number :noopts
echo DeployChapternumOptions.txt file must already exist
:end
The /p qualifier asks DssDeploy to create a packed self-expanding executable. Deployment is restricted to only the same version of MRDS using the /cv+ qualifier, and security ACLs are turned off with /s-.
153

www.it-ebooks.info
Part I: Robotics Developer Studio Fundamentals
The ReadMe file for the package (/r qualifier) should be a HTML file. It is displayed automatically when the package finishes installing on the target computer. Other options, such as for including a license, signing the file, and so on, might be relevant to you if you are creating a commercial package.
The script automatically adds a name to the package, specifies the deploy options file, and creates a “.safe” version for sending via e-mail. It is not rocket science, and you might be able to come up with a more elegant solution. However, it works — as you have no doubt already seen!
5. To deploy the package, copy it to the target PC and run it. It verifies that MRDS is installed and then creates the necessary folders and copies the files into them. Then it runs
DssProjectMigration to ensure that the projects are correctly configured. Finally, it displays the ReadMe file in a web browser.
Distributing Executables
When you get to Chapters 16 and 17, you will be deploying services to a PDA running Windows Mobile or an eBox embedded PC running Windows CE. These environments run the .NET Compact Framework, so they are referred to as CF services for short.
In this situation you do not want to install the full MRDS environment. (In fact, you can’t do this anyway.) What you want is a runtime-only environment.
The batch script to create the package, called BuildStingerCFPackage.cmd, contains the following commands:
REM Create a DssDeploy package setlocal
call “..\..\..\sdkenv.cmd” cd “%~dp0”
DssDeploy /p /cf /n:”ProMRDS Stinger CF Example” /d:”../../BatchFiles/CF/*” /m:../../Config/StingerCFDriveByWire.manifest.xml StingerCF.exe
pause
This DssDeploy command creates a package for the Compact Framework due to the /cf qualifier.
It explicitly adds all the files from the BatchFiles\CF folder to the package, and then uses a manifest to collect all of the necessary services. DssDeploy analyzes the manifest and walks down the dependency tree looking for service DLLs. The MRDS runtime DLLs are automatically included.
Deploying a runtime version of MRDS in this fashion is not restricted to just CF platforms. You can also create a package without the /cf qualifier and deploy it to Windows XP or Vista.
There is one restriction on the runtime-only version, however, and it is related to the simulator. In order to run Simulation services, you need to have the AGEIA PhysX engine installed, as well as the latest versions of Microsoft DirectX and XNA. In short, you must have MRDS installed on the target machine if you want to deploy a simulation.
154

www.it-ebooks.info
Chapter 3: Decentralized Software Services (DSS)
Viewing the Contents of a Package
A tool called ViewDssDeployContents, written by Paul Roberts at Microsoft, is available from the Channel 9 website (http://channel9.msdn.com/ShowPost.aspx?PostID=368096), and you should definitely download a copy of it. Perhaps this tool will be included in MRDS V2.0.
This tool shows, in an Explorer-like tree, what files are included in the package. This is particularly handy if you want to check out a package before installing it. Otherwise, you might have no idea what files are going to be replaced. DssDeploy only lists the files that will be replaced if there are less than about 20 of them, but beyond that, it simply asks you something like “72 files will be replaced, OK?”.
Summary
This chapter addresses the basics of using DSS: state, operations, and partners. You have seen the various components of a DSS service and how services interact. There is a lot to learn and you probably recognized some overlap with the previous chapter. If you have not done so already, you should at least browse through the MRDS online documentation. In addition, go through the tutorials.
As noted at the beginning of the chapter, it takes some time to get used to the MRDS terminology and way of doing things. The best way to learn is to get your hands dirty and start hacking services.
The next chapter elaborates on writing DSS services to enable you to perform more advanced functions, including using Windows Forms and a web camera.
155

www.it-ebooks.info

www.it-ebooks.info
Advanced Service Concepts
The Decentralized Software Services (DSS) are responsible for controlling the basic operations of robotics applications. As explained in the previous chapter, DSS is built on top of the CCR. This chapter moves on to more advanced features of services, including subscriptions and user interfaces. It is very likely that you will need to use the material in this chapter at some stage.
This chapter develops a service, called TeleOperation, that can be used to drive a robot with a web camera attached so you can see where the robot is going. The code introduces you to using Windows Forms as well as Web Forms with MRDS services, and demonstrates how to display live video. Don’t worry if you don’t have a robot with a camera because the faithful simulator comes to the rescue and you can still work through the code.
You might want your user interface to look like the cockpit of a 747, but that is not the objective in this chapter. It covers the basics of using Windows Forms (WinForms) and how to format web pages using XSLT. You can add fancy stuff yourself later.
These concepts are used in the Dashboard service that is included with this chapter. However, the discussion in the text uses the TeleOperation service because it contains a small subset of the Dashboard functions.
Setting Up for This Chapter
Although MRDS is full of examples, a new service is provided in this chapter to perform teleoperation: driving a robot remotely using an onboard camera. This is a common scenario, used, for example, in bomb disposal robots. In this case, you are the brains of the robot. It is basically just a camera on wheels, and requires only two services: Generic Differential Drive and Webcam.
You can use the TeleOperation service in this chapter with any robot that has a Generic Differential Drive service. In addition, if you can attach a camera to the robot somehow and it is accessible via the Webcam service, then you can see what the robot sees. The simplest solution is a small wireless “spy camera” with a video receiver that can be connected to your PC via a video capture device.
This setup is shown diagrammatically in Figure 4-1.

www.it-ebooks.info
Part I: Robotics Developer Studio Fundamentals
Wireless
Video Receiver
Wireless
Camera
USB Capture Device
Bluetooth
Connection Robot
Computer
Figure 4-1
Figure 4-2 shows a Swann wireless camera mounted on the front of a LEGO NXT Tribot. The camera is smaller than the 9V battery that powers it. Because the camera is not a LEGO part, liberal use of Blutac is required to fasten the camera and the battery onto the Tribot.
Figure 4-2
158

www.it-ebooks.info
Chapter 4: Advanced Service Concepts
Even if you do not have a wireless camera, you can still try out the service using the Maze Simulator (which is in Chapter 9) with a simulated Pioneer 3DX robot. By default, when you run the TeleOperation service from Visual Studio, it starts the Maze Simulator. To run the service with a real robot, you need to edit the manifest: ProMRDS\Chapter4\TeleOperation\TeleOperation.manifest.xml.
You can also edit the command-line parameters in the Debug properties for the Solution and add another manifest, but you still need to remove the Maze Simulator from the existing manifest
in this case.
The design of the TeleOperation service allows you to connect to a DSS node anywhere on the network to use the robot and camera. It connects to the first Differential Drive service that it finds, and the first WebCam. This should rarely be a problem because you are unlikely to be trying to teleoperate two robots at the same time!
Figure 4-3 shows a screenshot of the TeleOperation service (left). And look, there’s R2-D2!
Figure 4-3
The TeleOperation control window is similar to the Dashboard, which is also included in the Chapter4 folder. You can use a game controller or joystick, or use the mouse with the onscreen “trackball.” You can also use the arrow keys on the keyboard to drive the robot around. The arrow buttons on the left WinForm in Figure 4-3 use the DriveDistance and RotateDegrees operations, so the robot only moves a defined distance. However, you can uncheck the Fixed Moves checkbox and then the arrow buttons just turn the motors on as appropriate and you have to use the Stop button to stop the robot. This is similar to Robotics Tutorial 4, called “Drive by Wire.” This option is provided because some robot services do not implement the DriveDistance and RotateDegrees requests.
The second WinForm (on the right in Figure 4-3) is the WebCam View, which shows live video from the camera mounted on the robot. Setting up the camera can be a little bit of a compromise. It needs to be tilted down to see the floor immediately in front of the robot, but this can limit how far it can see into the distance.
Hardware Setup
Your particular hardware might be quite different from the hardware used during the development of this chapter. For example, a Corobot has a web camera mounted on the front as a standard component, but you might have to jury-rig a camera onto your robot somehow. Regardless of your hardware setup,
159

www.it-ebooks.info
Part I: Robotics Developer Studio Fundamentals
the key point is that the robot must have a Differential Drive service and the camera must be supported by the Webcam or IP Camera services.
The wireless camera used for testing in this chapter is a Swann Microcam with a Belkin USB Video Capture Device (VCD) to grab frames from the camera. The camera transmits in the 2.4GHz range. This is the same unregulated frequency band that is used by WiFi and Bluetooth. Consequently, there is a little interference between the camera and Bluetooth, even though Bluetooth uses frequency hopping.
Bluetooth causes streaks to appear in the video; but more important, the wireless camera causes some Bluetooth packets sent to or from the robot to be lost or corrupted. If the robot’s communication
protocol is not robust, then the service might hang waiting on packets that will never arrive because of “collisions.” Even without direct interference, the presence of another transmitter close to the Bluetooth module on the robot “desensitizes” the Bluetooth receiver.
The Boe-Bot and LEGO NXT (shown in Figure 4-2) appear to operate OK with the Swann wireless camera attached. From time to time they miss a command, but they recover. However, the Stinger robot (in Figure 4-1) has some problems. The Stinger Serializer service hangs sometimes. This is being investigated.
The Surveyor SRV-1 has an onboard camera, but the video does not work with the TeleOperation service using the MRDS services available from Surveyor at the time of writing (version 061216) because
these services do not implement the generic Webcam contract. However, a modified version of the camera service available from the book’s website does implement the Webcam contract. The authors are working on services for the new Blackfin version of the SRV-1 that uses WiFi for communications and will therefore be much faster.
In addition, the SRV-1 Differential Drive service suffers from a flooding problem that makes using a gamepad almost impossible because the robot continues to move long after you release the joystick. You can drive it by carefully using the arrow keys on the keyboard. This problem has been addressed by a new Differential Drive service that is also available from the book’s website (www.proMRDS.com). Unfortunately, this uncovered a different problem, which is that the robot is “deaf” while it is transmitting an image. Keep watching the book’s website for updates.
Creating the Service
By now, you should have a good idea of how to create a service, so the following instructions are very brief. In addition, the TeleOperation service contains a lot of code, so some code is not covered in the book.
You should open the TeleOperation service in Visual Studio and look at the code as you follow through the text. The completed service is available in the ProMRDS\Chapter4 folder. The Dashboard service is also included in the Chapter4 folder, but it is not discussed here.
Quite a lot of references are used by the TeleOperation service. Rather than introduce them as they are needed, the following is a complete list. Remember that whenever you add a reference, look in the properties for the reference and change the Copy Local and Specific Version properties to false. If you forget to do this you might have problems with versioning.
160

www.it-ebooks.info
Chapter 4: Advanced Service Concepts
RoboticsCommon.Proxy: This is almost always required for robot services, so you should get in the habit of adding it whenever you create a new service. Note that this reference is to the Proxy.
Ccr.Adapters.WinForms and System.Windows.Forms: These are required to use a Windows Form in your service, which is discussed in detail later in the chapter.
GameController.Y2006.M09.Proxy: This enables you to use a joystick or gamepad to drive the robot.
System.Drawing: This is required so you can manipulate bitmaps from the Webcam.
You do not need to add references to the Differential Drive or Webcam services because they are included in RoboticsCommon. The code establishes partnerships with these services dynamically.
At the top of the main source file, TeleOperation.cs, are appropriate using statements:
//Added references
//For Forms
using Microsoft.Ccr.Adapters.WinForms; using System.Windows.Forms;
//For handling webcam images using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
//Game Controller
using game = Microsoft.Robotics.Services.GameController.Proxy; // Generic Differential Drive
using drive = Microsoft.Robotics.Services.Drive.Proxy; // Webcam
using webcam = Microsoft.Robotics.Services.WebCam.Proxy;
//For locating services in the Directory using ds = Microsoft.Dss.Services.Directory;
//For HttpGet
using Microsoft.Dss.Core.DsspHttp;
//For HttpStatusCode using System.Net;
//Specifically for HttpPost handling using System.Collections.ObjectModel; using System.Collections.Specialized; using Microsoft.Dss.Core.DsspHttpUtilities;
Knowing which references you need is something that comes with experience. Alternatively, you can look at other similar services and see what they use. In particular, you can use the tool tips (IntelliSense) in another service to find out where a certain data type comes from.
For example, the TeleOperation Windows Form (DriveControl.cs) also requires the following statement so that it can draw the “trackball”:
using System.Drawing.Drawing2D;
Because TeleOperation is based on the Dashboard, you could figure this out by looking at the Dashboard code.
161

www.it-ebooks.info
Part I: Robotics Developer Studio Fundamentals
Some of the using statements that are automatically added when you create a new Windows Form are not required. However, they do no harm so you can leave them there. In any case, using statements are merely a convenience. Provided that you know which assembly a particular class is in (and you have added an appropriate reference), you can always specify its full name.
Throughout the rest of the chapter, various message types are mentioned. The Start method sets up the necessary receivers in the main interleave. The code is reviewed here in full, rather than line by line scattered throughout the chapter:
// Hook up all of the Form events MainPortInterleave.CombineWith(Arbiter.Interleave(
new TeardownReceiverGroup
(
),
new ExclusiveReceiverGroup
(
// Form handling Arbiter.ReceiveWithIterator<OnLoad>(true, _eventsPort,
OnDriveControlLoadHandler),
Arbiter.Receive<OnClosed>(true, _eventsPort, OnDriveControlClosedHandler),
Arbiter.ReceiveWithIterator<OnLoad>(true, webCamEventsPort, OnWebCamFormLoadHandler),
Arbiter.Receive<OnClosed>(true, _webCamEventsPort, OnWebCamFormClosedHandler),
// Connection request Arbiter.ReceiveWithIterator<OnConnect>(true, _eventsPort,
OnConnectHandler)
),
new ConcurrentReceiverGroup
(
//Game Controller–Added later
//Drive notifications - Not currently used
//Movement commands Arbiter.ReceiveWithIterator<OnMove>(true,
_webCamEventsPort, OnMoveHandler),
Arbiter.ReceiveWithIterator<OnMove>(true, _eventsPort,
OnMoveHandler),
Arbiter.ReceiveWithIterator<OnMotionCommand>(true,
_eventsPort, OnMotionCommandHandler),
// WebCam -- Be careful because this can lock up if Exclusive
Arbiter.ReceiveWithIterator<webcam.UpdateFrame>(true,
_webCamNotify, CameraUpdateFrameHandler)
)
));
Briefly, the _eventsPort and the _webCamEventsPort are used for messages from the main Form (called DriveControl) and the Webcam Form, respectively. General Windows Form messages are OnLoad
and OnClosed. The messages types OnMove and OnMotionCommand are used to control the robot. UpdateFrame messages come from the web camera when new frames are ready for retrieval.
162