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

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

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

www.it-ebooks.info

Chapter 16: Autonomous Robots

PDAs run an abridged version of Windows that has been known by various names over the years, including Pocket PC, Windows Mobile, or Windows CE (in different flavors and versions). The main thing that these operating systems have in common is that they all use a subset of the .NET runtime, called the .NET Compact Framework (CF), for running MRDS services. This has implications for how you write your code.

The other issues you have to face with PDAs are limited memory and slower CPU speeds. These restrictions mean that a PDA is probably not suitable for a very sophisticated application due to the overhead of MRDS. However, it is possible to run a simple application on a PDA just to illustrate the principles involved.

In the folder for Chapter 4 you can find a sample application, DriveByWire, that enables you to remotely control a Boe-Bot using a PDA. This chapter provides a service for driving a Stinger robot from a PDA. These are really toys, but they can be cool examples to use to impress your friends.

Embedded PCs

The term “embedded PC” can refer to a variety of different physical formats, ranging from a complete “PC in a box” to bare circuit boards. For our purposes, an embedded PC is any computer that runs Windows CE. (Embedded XP could also be used, but it is not covered in this chapter).

PC motherboard designs have undergone a series of changes since the original IBM PC was introduced. Motherboards have become progressively more compact and less power hungry, ranging from the IBM AT to the ATX and the microATX form factors.

The ITX series of boards promoted by VIA Technologies are available in Mini-ITX, Nano-ITX, and PicoITX varieties. Pico-ITX is a tiny 10cm 7.2cm (3.9in 2.8in). The advantage of the VIA boards is that they are convection cooled — because they don’t require a fan, they are quiet and low power.

At the same time that smaller PC motherboards were being refined, single-board computers (SBCs) were also introduced for use in embedded systems. One of the early standards was PC104, so called because there were 104 pins on the bus connector (which corresponded to the original ISA bus from the IBM PC). PC104 was different from standard PC motherboards in that it did not have slots for plugging in extra cards. Instead, boards were designed to stack on top of one another. Over the years there have been successive revisions of PC104 — for example, to include the PCI bus.

To avoid the heat and weight of hard drives, embedded PCs often use solid-state storage devices or flash memory such as CompactFlash cards. With no rotating parts, they are less susceptible to damage from vibration and shocks.

The eBox-2300, shown in Figure 16-1, is used in the latter part of this chapter. It comes in a metal case with standard connectors for monitor, keyboard, USB, Ethernet, and so on, so that it is effectively a tiny PC. It runs off a single 5V supply and does not have a fan (which is why there are heat sinks on the case). The eBox-2300 is manufactured by ICOP Technology (www.icoptech.com).

683

www.it-ebooks.info

Part IV: Robotics Hardware

Figure 16-1

*Photo courtesy of EmbeddedPC.NET (www.embeddedpc.net).

A “Jump Start Kit” based on the eBox-2300 is available specifically for use with MRDS (www.embeddedpc

.net/eBox2300MSJK/tabid/111/Default.aspx). However, if you buy a Stinger CE robot from RoboticsConnection, then the Jump Start Kit is included, so you don’t have to buy it separately. Furthermore, the Stinger CE kit includes a 5V regulator that you need in order to run the eBox off a rechargeable battery.

The eBox runs Windows Embedded CE 6.0. The CE stands for either Compact Edition or Consumer Electronics, but it doesn’t matter and most people just refer to it as CE or WinCE. See the Windows CE home page for documentation and downloads: http://msdn2.microsoft.com/en-us/embedded/ aa731407.aspx.

As with PDAs, Windows CE uses the .NET Compact Framework (CF), so the services for embedded PCs are generally referred to as CF services in MRDS terminology.

The Development Environment

Developing MRDS services for PDAs is similar, but not identical, to developing for embedded PCs running Windows CE. In both cases you use Visual Studio with its built-in support for mobile devices.

Visual Studio has a cool emulator for PDAs that can be used for debugging conventional applications. However, in the case of MRDS this environment has several shortcomings. In particular, it does not include support for USB Bluetooth dongles, so it is not used in this chapter.

In order to develop code for Windows Mobile or Windows CE, you must have the correct versions of the software. Visual Studio Express Editions do not support development for mobile devices, i.e., the Compact Framework. You need the full version of Visual Studio 2005 with Service Pack 1 installed. Also make sure that you have the .NET Compact Framework V2.0 Service Pack 2 installed on your PDA or embedded PC. The .NET runtime and the service packs are available from the Microsoft Downloads page but you might need to search for them because they have been superseded by Visual Studio 2008 and .NET 3.5. This book was written using Visual Studio 2005 and the code was not tested with later versions. Please refer to the book’s website for the most up-to-date information.

684

www.it-ebooks.info

Chapter 16: Autonomous Robots

Setting Up a Stinger Robot

The examples in this chapter are based on the Stinger CE robot from RoboticsConnection (www.roboticsconnection.com) shown in Figure 16-2.

Figure 16-2

*Photo courtesy of RoboticsConnection

In Figure 16-2 the eBox-2300 is shown mounted on top of the robot. However, an alternative using a PDA is developed before discussing how to use the eBox.

You can buy a Stinger robot on its own and build your own configuration using the optional accessories. Even if you are not interested in autonomous robots, you can still use the Stinger (but without the ‘CE’ bit).

It is recommended that you buy a Bluetooth module and the infrared sensors as well as the standard Stinger CE kit, which has the line-following sensors (not visible in Figure 16-2 because the sensors are mounted under the front bumper).

The Bluetooth module is not used in the Stinger CE kit because the eBox plugs into the RS-232 module using a cable connected to COM1 on the eBox. However, Bluetooth is good for getting started because it enables remote control of the robot, meaning you can develop code on your desktop PC initially.

Figure 16-3 shows a Stinger configured with Bluetooth (the antenna is visible sticking up in the middle of the photo) and three IR sensors across the front bumper. The serial connector poking out of the middle of the robot is for the eBox and is not used in this configuration.

685

www.it-ebooks.info

Part IV: Robotics Hardware

Figure 16-3

Note that the IR sensors should be mounted on standoffs so that they do not see the ground immediately in front of the robot. The GP2D12 sensors have a range of 10cm to 80cm, whereas GP2D120 sensors have a range of 4cm to 30cm. Which ones you choose depends on how cluttered your environment is and how fast you plan to drive the robot. The Stinger moves quite fast at full power!

There are very good multimedia assembly instructions for the Stinger available on the website, including 3D diagrams, which you can manipulate to change the viewpoint, and spoken instructions. However, some of the optional extras such as the IR sensors do not have assembly instructions.

The necessary MRDS services for the Serializer board are available from the RoboticsConnection website to enable you to control the Stinger. Anywhere that you see references to the “Traxster” on the website, you can also take this to mean the Stinger because both robots use the same control board, called the Serializer. These services include both the standard desktop versions and the CF versions.

You should download the Serializer services from the RoboticsConnection website and install them first. Included with the services is a sample program that is very useful for testing your robot once you have assembled it.

Setting Up Your PDA

To run the first example in this chapter you must have a PDA with built-in Bluetooth, such as the Dell Axim X50v or X51v. Most PDAs in recent years include Bluetooth.

Although the Axim also has built-in WiFi, this is not required to use a PDA for MRDS. Note that these Axim models run at 624MHz, which is slow compared to a desktop PC but fairly fast for a PDA.

686

www.it-ebooks.info

Chapter 16: Autonomous Robots

Figure 16-4 shows an Axim running the Stinger Drive-By-Wire program. You can find this program in the ProMRDS\Chapter16 folder. (Astute readers might be able to tell that this is a composite picture — a screen capture from an X50v PDA has been pasted over an image of an X51v from the Dell website. It looks better than an actual photo.)

Figure 16-4

You should install the latest version of ActiveSync, available free from the Downloads link on the Microsoft website — select Windows Mobile from the list of Product Families. At the time of writing, the latest version is 4.5. If you are an avid PDA user, you probably have this software already. From here on it is assumed that you have ActiveSync working.

Recent versions of Windows Mobile include the .NET Compact Framework, but if you have an older PDA, running Windows Mobile 2003 (also called Pocket PC 2003) for example, then you will have to download and install the .NET Compact Framework Version 2.0 Service Pack 2. You install the software on your desktop PC first, and then on your PDA using ActiveSync, but this is not covered here because you should be able to do this.

When it comes to running and debugging services on your PDA, there are some tools that you will find invaluable. First, download the Windows Mobile Developer Power Toys from www.microsoft.com/ downloads/details.aspx?FamilyID=74473fd6-1dcc-47aa-ab28-6a2b006edfe9&DisplayLang=en.

This package includes the Pocket PC (PPC) Command Shell, which enables you to enter commands in the same way as you do with the command prompt for MRDS on your desktop PC. The real advantage is that you can see the output from DssHost as it starts up and find out about service failures or mistakes in the manifest right away.

687

www.it-ebooks.info

Part IV: Robotics Hardware

After downloading and installing the Power Toys, you need to copy cmd.exe, shell.exe and console.dll to the \Windows folder on your PDA using ActiveSync. You can also add a shortcut for cmd.exe to the \Windows\Start Menu\Programs folder so that you can easily start a command prompt whenever you like.

A Note about Shortcuts

Shortcuts on Windows Mobile and Windows CE are plaintext files with a file type extension of .lnk. You can create them using Notepad on your desktop PC and then transfer them to your PDA with ActiveSync. A shortcut is handy for starting MRDS on your PDA.

The format of a shortcut is

nn#path\filename.exe /parameters

where nn is the number of bytes in the command line following the hash sign (#). In the case of the PPC Command Shell, the executable cmd.exe is on the search path so the shortcut is very simple:

3#cmd

Interestingly, you can enter a number that is larger than the length of the command and it still works. For example, you could put 10 in the previous shortcut instead of 3 and it would still work.

When you want to run an MRDS service, the shortcut becomes more complicated. Later in the chapter you will see the Stinger Drive-By-Wire service. Its shortcut looks like the following:

95#”\Program Files\mrds\bin\cf.DssHost.exe” /p:50000 /t:50001 /m:StingerCFDriveByWire.manifest.xml

Note that this command must all be entered on a single line in the shortcut file. A copy of this shortcut is included with the Stinger Drive-By-Wire service source code in the file called StartStinger.lnk in the CF subdirectory; it is deployed to your PDA when you install the service.

The Power Toys also contain several other useful programs. One that you might find handy for producing documentation is the ActiveSync Remote Display, ASRDisp.exe. This enables you to view on your desktop PC a replica of the screen on your PDA so you can make screenshots. For example,

Figure 16-5 shows the memory utilization on the PDA while the Stinger Drive-By-Wire service is running. (The title bar and menu across the top are from ASRDisp and are not part of the PDA screen.)

688

www.it-ebooks.info

Chapter 16: Autonomous Robots

Figure 16-5

Note carefully in Figure 16-5 that a large portion of the available memory has been dedicated to program usage, rather than storage. This is necessary so that the MRDS services don’t run out of memory. On the Dell Axim used for writing this book, the Bluetooth Manager exits if memory gets too low and it is necessary to perform a warm restart on the PDA. This is one of the problems that you might encounter when using a PDA. If you have too many files on your PDA you might need to clean up so that you can get at least 40MB for Program memory.

For the Bluetooth connection, you have to pair your PDA with the robot. This process is similar to what was covered in Chapter 14. The exact details vary slightly depending on the brand of the PDA and the version of the operating system.

Basically, you need to search for new devices in the vicinity of the PDA while the robot is turned on. The Stinger robot will show up as an EB100 device. You can select this and enter the passkey, which is 0000. This only needs to be done once. Make sure that you record the outgoing serial port number when you complete the pairing because you will need this later to configure the MRDS service.

If you have more than one Bluetooth device paired with your PDA, then you will be prompted to select a device when you run the Stinger Drive-By-Wire service. This might look like the screenshot in Figure 16-6 where eb500 is a Boe-Bot, eb100 is a Stinger, and TTMobile is a mobile phone, all of which have been paired with the PDA.

Figure 16-6

689

www.it-ebooks.info

Part IV: Robotics Hardware

Another piece of software that you might need is something like the Process Explorer (PE.exe) from MadeBits (http://madebits.com/tools/pocketpc-process-explorer.php). This program enables you to view and kill processes running on your PDA, which comes in handy sometimes because services do not always shut down cleanly. A sample screenshot is shown in Figure 16-7.

Figure 16-7

Notice in Figure 16-7 that the client side of the ActiveSync Remote Display is running (cerdisp2.exe) as well as the CF version of DssHost and a command prompt (CMD).

Driving a Stinger Remotely

To begin the example for this chapter, a service is discussed that connects to the Stinger using Bluetooth and runs on a desktop PC. Then the service is migrated to a PDA and finally to the eBox-2300.

There is no requirement to use a PDA, but it provides an attractive alternative to using an eBox. For a start, you can either attach the PDA directly to the Stinger or you can simply walk around behind the Stinger. The PDA has a screen that makes it easier to see what is going on, whereas the eBox operates in a “headless” mode when it is attached to the Stinger. In addition, you can use your PDA for other things besides just driving a robot!

This example is similar to the Robotics Tutorial 4 (Drive-By-Wire) with the addition of a mode that enables the robot to wander around on its own. This example also enables you to use the keyboard to drive the robot, whereas the Tutorial does not. The Stinger services do not include a generic Differential Drive service, so the code for this service has to partner directly with the Stinger motors and it is not directly portable to other robots. A different version of the code included in Chapter 4 will work with other robots such as the Boe-Bot.

The Stinger Drive-By-Wire service displays a Windows Form when you run it. Figure 16-8 shows what the GUI looks like running under Windows XP on a desktop PC. (It looks very similar on a PDA). This is the first step in getting the service to work on a PDA or Windows CE device.

690

www.it-ebooks.info

Chapter 16: Autonomous Robots

Figure 16-8

Code Overview

The following discussion highlights the main features of the code. It is not a step-by-step explanation. By now, you should be familiar with creating services.

The StingerDriveByWire service in the ProMRDS\Chapter16 folder uses the Serializer services from RoboticsConnection as well as a Windows Form. Therefore, it must have references to

RoboticsCommon.Proxy

SerializerServices.Y2006.M08.Proxy

Ccr.Adapters.WinForms

System.Windows.Forms

and, of course, the corresponding using statements that you can find at the top of the main service source file, StingerDriveByWire.cs.

Several topics are covered in the following sections:

Using a Windows Form interface: This can be disabled later using a setting in the configuration file for the service. There are some differences in how a Form is handled under CF.

Handling keystrokes in a Form: This is handy when using a PDA because you can use the “rocker switch” to control the robot.

Differences in declaring service operations under CF

Using the Serializer services is discussed briefly.

Windows Form

In Chapter 4 you saw how to use a Windows Form in conjunction with a service. It was pointed out there that the streamlined CF version of DssHost does not have a WinFormsServicePort. To get around this minor problem you have to create your own port for Windows operations. (Remember that this is necessary because WinForms use the STA model for multi-threading).

691

www.it-ebooks.info

Part IV: Robotics Hardware

The initialization code in the Start method is as follows:

// If we are NOT Headless, then create the WinForm UI if (!_state.Headless)

{

//Create the Dispatcher that will execute our delegates that we add to the

//DispatcherQueue

Dispatcher dispatcher = new Dispatcher(1, “WinForms Dispatcher”); _DispatcherQueue = new DispatcherQueue(“WinForms DispatcherQueue”, dispatcher);

//Bind the DispatcherQueue to the WinFormsAdapter and

//create the WinFormServicePort

_WinFormsPort = WinFormsAdapter.Create(_DispatcherQueue);

// Use the WinFormsServicePort to create a MainForm _WinFormsPort.Post(new RunForm(StartForm));

}

Notice here that the code is conditional on the service not running “headless.” This flag can be set in the config file, which is read using an initial state partner. All the code that refers to the Form uses the same check, which enables the service to run without a GUI. Although this is not really necessary, there is no point in displaying a GUI on the eBox-2300 when the robot is running autonomously because no screen is attached to the eBox. Removing the GUI reduces the overhead on the eBox, which is a good idea because it is a very slow CPU.

The routine to create the new form is trivial:

// Create the new WinForm

private System.Windows.Forms.Form StartForm()

{

StingerDriveByWireForm form = new StingerDriveByWireForm(_mainPort); // Keep a handle to the Form

_driveForm = form; return form;

}

Notice that the main service port is passed to the constructor for the Form to enable the Form to send messages back to the main service.

Keyboard Handling

Chapter 4 covers the steps involved in using the keyboard directly. It might seem a little out of place in a chapter on autonomous robots, which do not have keyboards, but it fits in with using the rocker switch on a PDA to drive a robot.

If you open the StingerDriveByWireForm in Design view in Visual Studio, you can go to the Properties panel and click the lightning bolt icon. This displays the event handlers for the Form. Figure 16-9 shows that there are KeyDown and KeyUp event handlers (as well as Form Load).

692