This article will demonstrate how to use this new service in conjunction with my previous article to create a Wiimote Controlled car. This project was demonstrated at both
Mix 07 and
Maker Faire 2007 and was a big hit in both places, so it's sure to be a hit with your friends as well.

Previous Articles

To start with, it's essential that you read the two previous articles mentioned above because I will not be repeated their contents here. I will simply demonstrate how to "put the chocolate in the peanut butter" to create the project described. The two
articles are:

Once all of the software is installed, the files phidget21.dll
and Phidget21.NET.dll must be copied from the \Program Files\Phidgets directory to the
bin directory of your MSRS installation. If this is not done, the Phidget services will all fail to start when requested, causing you as much pain and torment as it caused me.

Make sure that the Wiimote library is installed in your Robotics Studio
\bin\services directory. You can do this by either loading and compiling the
WiimoteMSRS project included in the original Wiimote library distribution, or by coping over the compiled
Wiimote.Y2007.M06.*.dll files to the \bin\services directory by hand, all of which can also be found in the original Wiimote library distribution linked above.

The Code

To start, we must first create a service for our car. This can be done by opening the
Microsoft Robotics Studio Command Prompt from the Microsoft Robotics Studio (1.0) program group on the Start menu.

To generate the template for our service, the command dssnewservice.exe is used. Change to the directory where you wish to create your project and type one of the following commands, depending on the language you wish to use, to create a
new service named “WiimoteCar”:

C#:

dssnewservice /service:WiimoteCar

VB

dssnewservice /language:VB /service:WiimoteCar

This will generate a folder with several items, including project and solution files.

Open the generated WiimoteCar.sln file in Microsoft C# 2005 Express Edition or Microsoft Visual Basic 2005 Express Edition. You will see that several files were generated.

The first thing we will need to do is access the service which controls our Phidget Interface Kit. Robotics Studio includes a built-in service for this device. To access it, first, set a reference to the
PhidgetBoards.Y2006.M08.proxy and Wiimote.Y2007.M06.proxy namespaces. This can be done by right-clicking on the project name in the Solution Explorer and selecting
Add Reference from the context menu. When the Add Reference dialog appears, select
PhidgetBoards.Y2006.M08.proxy and Wiimote.Y2007.M06.proxy, and click the
OK button.

To use the contents of this library in our code, we need to import the libraries reference above. This can be done by adding the following line to the WiimoteCar class.

Next, we need to setup a partner relationship with the PhidgetInterfaceKit and
Wiimote services and declare a port on which to communicate with it. At the top of the WiimoteCar
class implementation, add the following code to the service:

Next, we will setup a few items that will be required to talk to the Phidget controller and Wiimote. We must subscribe to the
PhidgetInterfaceKit service and Wiimote service. Add the following to the
Start method of the WiimoteCarService class:

This will subscribe to the PhidgetInterfaceKit service without requesting any notifications on events, as they will not be needed for this project. It will also subscribe to the
Wiimote service asking for the WiimoteChanged event to be sent to the
WiimoteChangedHandler method that will be defined below.

Next, create an enumeration above the definition for the WiimoteCarService
class for the possible directions:

C#

publicenum Direction
{
Forward = 0,
Backward,
Left,
Right,
None
}

VB

PublicEnum Direction
Forward = 0
Backward
Left
Right
None
End Enum

The value of each item in the enumeration will correspond to the relay used for that direction as described above. Next, we will write a method named
SetOutput to communicate with the Phidget Interface Kit and enable or disable the output relay as appropriate:

The SetOutput method creates a new SetOutputRequestType request object and fills in the appropriate
Index and State. The Index refers to the index number of the relay to change, and the
State property is a boolean of whether the switch should be opened or closed.

And finally, the WiimoteChangedHandler described above needs to be implemented. This handler will be called every time the Wiimote service fires a
WiimoteChanged event. The incoming parameter will be identical to the
WiimoteState structure defined and described in the previous Wiimote article. Since you've already read that, this should be pretty easy:

In this method, if the 1 button is pressed, the forward output is turned on. If the 2 button is pressed, the reverse output is turned on. For turning, the Y-axis accelerometer is read and if it's over a specific threshold, the left and right turn outputs
are turned on.

That's It!

To run the project, connect the Wiimote as described in the original article above and press F5 to run this project. Hold the Wiimote horizontally so that the IR end is in your left hand and the strap end is in your right hand. The Wiimote itself should
be face up and parallel to the ground. Press 1 to move forward, 2 to move in reverse, and tilt (not twist!) the Wiimote to turn the car. As long as the hardware is wired properly and the Wiimote is connected and compatible with your Bluetooth device, the
car should go!

Now What?

Now that the basics are done, here are a few ideas for future enhancements:

Set the forward/backward motion of the car based on the Z-axis of the Wiimote accelerometers. This way one can drive the car by tilting the controller forward and backward to move the car forward and backward.

Write the application in the Robotics Studio VPL language.

Add on the IP Camera in the original R/C car article.

Conclusion

As you can see, with a little effort and some existing libraries, one can create a pretty simple and cheap Wiimote controlled car. Now try Wiimote controlling some other R/C devices...

Bio

Brian is a Microsoft C# MVP and a recognized .NET expert with over 6 years experience developing .NET solutions, and over 9 years of professional experience architecting and developing solutions using Microsoft technologies and platforms, although he has
been "coding for fun" for as long as he can remember. Outside the world of .NET and business applications, Brian enjoys developing both hardware and software projects in the areas of gaming, robotics, and whatever else strikes his fancy for the next ten minutes. He
rarely passes up an opportunity to dive into a C/C++ or assembly language project. You can reach Brian via his blog at
http://www.brianpeek.com/.