Remote Control NXT with MSRS

This is a tutorial I put together which shows you how to programmatically create a remote control services for the Lego Mindstorms NXT. It is based on the video’s available on the Microsoft Robotics Studio Website.

Open a new MSRS Prompt.

Go to Start Menu > Programs > Microsoft Robotics Studio (1.0) > Microsoft Robotics Studio Command Prompt to open prompt.

Run the following command:

dssnewservice /service: DialogDrive

This will create a new Visual Studio Project (called DialogDrive).

Open the project in Visual Studio from the MSRS Prompt:

DialogDrivedialogdrive.sln

Once the project has opened, right click on Resources and select Add Reference, adding references to:
a) Robotics.Common.Proxy
b) DirectionDialog.Y2006.Proxy

Any time you partner to another service you want the proxy, the proxy exposes the service.

 

In the Visual Studio Solution Explorer open dialogdrive.cs
Add using statements below existing ones:

using Dialog = Microsoft.Robotics.Services.Sample.DirectionDialog.Proxy; 

using drive = Microsoft.Robotics.Services.Drive.Proxy;

Dialog & drive make it simpler to reference the libraries.

Scroll to the Service Class
Add 2 new services ports to communicate with our other services by adding (at the very beginning of class):

private dialog.DirectionDialogOperations _dialogPort = new dialog.DirectionDialogOperations(); 

private drive.DriveOperations _drivePort = new drive.DriveOperations();

Now partner ports to services by adding (above each port created in the above step):

[Partner(“dialog”, Contract = dialog.Contract.Identifier, CreationPolicy = PartnerCreationPolicy.CreateAlways] 

…_dialogPort code from step above…

The Contract Identifier is the contract for the dialog service – contract describes service, every service has one to allow partnering.
The CreateAlways creation policy tells the port to always create the service that it will connect to.

[Partner(“drive”, Contract = drive.Contract.Identifier, CreationPolicy = PartnerCreationPolicy.UseExisitng]

…_drivePort code from step above…

The UseExisting creation policy tells the port to wait for the service to be started externally.

Now in the Start() method add:

The Start() method is part of every service and is run when the service’s dependencies are met (service partners) and the service starts.
_drivePort.EnableDrive(newdrive.EnableDriveRequest(true)); 

This tells the drive service we are ready to begin driving/controlling.

SubscribeToDirectionDialog();

Add the SubscribeToDirectionDialog() method stubs using intellisense.

We need to create the SubscribeToDirectionDialog() method as it has not yet been created.


Add port to listen for events in the new method:

dialog.DirectionDialogOperations dialogNotifications = new dialog.DirectionDialogOperations();


Subscribe to notificaitons from _dialogPort by adding:

_dialogPort.Subscribe(dialogNotifications);


Tell our service/port what to do when a message is recieved by adding:

Arbiter.Activate(
Arbiter.Recieve(true,dialogNotifications, DialogButtonPressHandler),
Arbiter.Recieve(true, dialogNotifications, DialogButtonReleaseHandler)
);
 

The true parameter tells the Arbiter to continue to listen for messages (after the first message is recieved). The third parameter is the method that is called.

Now create the handler methods:

private void DialogButtonPressHandler(dialog.ButtonPress buttonPress) {
LogInfo(LogGroups.Console, buttonPress.Body.Name + ” Pressed”); // Logs output to console 

Switch (buttonPress.body.Name) {
case “Forwards”:
_drivePort.SetDrivePower(new drive.SetDrivePowerRequest(1.0,1.0));
break;
case “Backwards”:
_drivePort.SetDrivePower(new drive.SetDrivePowerRequest(-1.0,-1.0));
break;
case “Left”:
_drivePort.SetDrivePower(new drive.SetDrivePowerRequest(1.0,-1.0));
break;
case “Right”:
_drivePort.SetDrivePower(new drive.SetDrivePowerRequest(-1.0,1.0));
break;
case “Stop”:
_drivePort.SetDrivePower(new drive.SetDrivePowerRequest(0.0,0.0));
break;
}
}

private void DialogButtonReleaseHandler(dialog.ButtonRelease buttonRelease) {
LogInfo(LogGroups.Console, buttonRelease.Body.Name + ” Released”); // Logs output to console 

_drivePort.SetDrivePower(new drive.SetDrivePowerRequest(0.0,0.0));

}

The handler methods are called when the messages are recieved.


Now compile our project (Press F5).

Now we can run our project.

However we need to also start the services needed by our service on our dsshost node. So run the following in the MSRS Prompt:

dsshost /prt:50000 /manifest:”SamplesConfigLEGO.NXT.Tribot.manifest.xml” /manifest: “DialogDriveDialogDrive.manifest.xml”

The following starts the tribot service and our service. Note that the dialog service is started by our service automatically as we specified in the partnering directives.

The NXT config page will popup as it will need to be configured