Introduction

In this article you will see how to create a Microsoft Robotics (from now MR) service to use a game pad. This article is not a CCR or a DSS tutorial, and a good knowledge in these technologies will be very useful. In this article I assume you know what messages, queues and services are.

The base idea is quite simple: the service will poll the gamepad device every x milliseconds. The gamepad device will be queried using managed direct input (.NET version) and will return values from -1 to 1. So, for example the left upper corner will be (-1,1) and the right lower corner will be (1,-1).

No timers or threads will be used to implement polling. I will use only messages and queues and the CCR Classes.

Background

Few words about hardware (the pad). I've used a Logitech Dual Action Gamepad (USB). This is a cheap and good device for gaming, precise enough to control an hobbyist robot. I never experienced Direct Input so I was forced, because of my poor knowledge, to "hard code" numeric values inside the code. I assume that these values are strictly related to my hardware, so, if you have a different gamepad change them accordingly.

The next version of this service will have configuration support using XML files.

In order to compile this solution, you must have the Microsoft.DirectX.DirectInput.dll file. You can find it in Directx SDK.

The project

To create an empty service, you have two ways: start from a blank solution or start using DssNewService.exe. I will show you the easy way: open MR command line and cd to samples directory, then launch DssNewService.exe /s:PadThis will create a new project which contains all the needed files in order to have a VS2005 solution with a DSS service. The project also has post build actions to generate Proxy classes.

The other files created are Manifest.xml and the PadTypes.cs files. PadTypes contains all the messages this service will use. There you define the "actions" your service will respond to.

Open the solution and add reference to Microsoft.DirectX.DirectInput.dll. Add:

Now we can use _gamepad to query the pad status to search for Sticks values or buttons status. The _form is used only to get a "Handle" to init DirectInput. This form could be use to provide graphical feedback of the pad.

ATTENTION! If I start debugging (F5) in VS2005, I get a "Loader Lock" warning. This is done by Managed debugging assistants (MDAs). So you can disable assistants as the article describes or you run and attach to process later for debugging (CTRL + ALT + P).

Direct Input Polling

Pad polling is split in two "blocks". The first one is based on a "timer": on every tick a gamepad read is performed and data is immediately sent to a queue. An Arbiter is activated on the queue: an anonymous delegate process messages and eventually notifies new "events" to the main port.

That's it. Pad polling is almost complete. The commented code reads current sticks (L and R) values and normalize them in range (-1,1). Inside the omitted code, the interested "service" actions performed are similar to this snippet.

The next improvement will be a GUI. If you have comments and suggestions, please post!

History

September 2007: First release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.