Robotics Tutorial 2 (C#) - Coordinating Services

Glossary Item Box

Microsoft Robotics Developer Studio Send feedback on this topic

Robotics Tutorial 2 (C#) - Coordinating Services

Robotics Tutorial 1 (C#) - Accessing a Service for C# taught you how to access a simple service and how one service can request to be notified of events that occur in another service. This tutorial is a continuation of Robotics Tutorial 1 and shows you how to use a contact sensor to start and stop a motor. The service that you created in Robotics Tutorial 1 (C#) - Accessing a Service, MyTutorial1, will be modified to cause a motor to turn on or off when the contact sensor is pressed.

Figure 1

Figure 1 - Simple coordination between a bumper service and a motor service

This tutorial is provided in the C# language. You can find the project files for this tutorial at the following location under the Microsoft Robotics Developer Studio installation folder:

Samples\RoboticsTutorials\Tutorial2\CSharp

This tutorial teaches you how to:

  • Add References.
  • Subscribe to the Bumper Service.
  • Modify the Bumper Handler.

See Also:

  • Try It Out

Prerequisites

Hardware

You need a robot with microcontroller and a contact sensor. Contact sensors are typically simple mechanical switches that send a signal when physical contact is made. The sensor can also be distance detection devices (like sonars or infrared sensors) that provide a simple binary signal when a particular threshold is detected. Connect the sensor to your robot's microcontroller following the normal conventions for the hardware you are using.

To determine if support is included in Robotics Developer Studio for your robot and to setup your hardware, see Setting Up Your Hardware. You may be able to apply this tutorial for other robots that provide similar services (or create your own services following the Service Tutorials Overview included in Robotics Developer Studio). Setting up Your Hardware may also provide you with any recommended guidelines for setting up your PC to communicate with your robot.

Software

This tutorial is designed for use with Microsoft Visual C#. You can use:

  • Microsoft Visual C# Express Edition.
  • Microsoft Visual Studio Standard, Professional, or Team Edition.

You will also need Microsoft Internet Explorer or another conventional web browser.

Step 1: Add References

In Robotics Tutorial 1 (C#) - Accessing a Service, you added a reference to RoboticsCommon.Proxy to your project. If you are using the service that your wrote in Robotics Tutorial 1 as the basis for this tutorial, this reference should remain in your project. Check the properties of the MyRoboticsTutorial1.cs project to confirm that there is a reference to RoboticsCommon.Proxy. If not, add it as per the instructions in Tutorial 1.

The MyRoboticsTutorial1.cs file also includes the same using directives that you used in MyRoboticsTutorial1.cs to access the references in your code. Your new service will require one more reference, to the proxy DLL for the motor service, Microsoft.Robotics.Services.Motor.Proxy. The following code snippet shows the new reference and its alias.

using motor = Microsoft.Robotics.Services.Motor.Proxy;

 

If you created a new service (instead of copying MyRoboticsTutorial1) then you must add another using statement as you did in Robotics Tutorial 1 (C#) - Accessing a Service:

using Microsoft.Dss.Core;

 

The next step is to create a partnership between the MyTutorial1 service and the motor service. A service partnership allows us to identify the services on which our service depends. To communicate with the motor service, you will set up a port of MotorOperations and identify this as a partner by using the Partner attribute. Add the following code to your service after the declaration of the bumper partner.

[Partner("motor", Contract = motor.Contract.Identifier,
    CreationPolicy = PartnerCreationPolicy.UseExisting)]
private motor.MotorOperations _motorPort = new motor.MotorOperations();

Step 2: Subscribe to the Bumper Service

The MyTutorial1 service should already include the following subscription to the bumper service.

/// <summary>
/// Subscribe to notifications when bumpers are pressed
/// </summary>
void SubscribeToBumpers()
{
    // Create bumper notification port
    bumper.ContactSensorArrayOperations bumperNotificationPort = new bumper.ContactSensorArrayOperations();

    // Subscribe to the bumper service, send notifications to bumperNotificationPort
    _bumperPort.Subscribe(bumperNotificationPort);

    // Start listening for Bumper Replace notifications
    Activate(
        Arbiter.Receive<bumper.Update>
            (true, bumperNotificationPort, BumperHandler));
}

Step 3: Modify the Bumper Handler

This step explains how to modify the bumper handler that you created for the MyTutorial1 service to use the bumper to turn the motor on and off.

Open the MyTutorial1Types.cs file to add a data member to the State class of the service. The State class provides the default data contract for the service and contains any state that is necessary to operate the service. Add a data member called MotorOn to contain a bool value that is used to track the operational status of the motor, in other words, if the motor is on or off.

[DataContract]
public class RoboticsTutorial2State
{
    [DataMember]
    public bool MotorOn;
}

NOTE: If you are working with the copy of Robotics Tutorial 2 that is supplied with RDS then the State class is called RoboticsTutorial2State as you can see above. However, if you are working on a copy of the code that you created manually for Tutorial 1, then the State is called MyTutorial1State. Be careful to get the right class.

The next task is to rewrite the bumper handler method to set the motor based on the current value of the MotorOn data member. The following shows the implementation for the BumperHandler method:

/// <summary>
/// Handle Bumper Notifications
/// </summary>
/// <param name="notification">Update notification</param>
private void BumperHandler(bumper.Update notification)
{
    string message;

    if (!notification.Body.Pressed)
        return;

    // Toggle the motor state
    _state.MotorOn = !_state.MotorOn;

    // Create a motor request
    motor.SetMotorPowerRequest motorRequest = new motor.SetMotorPowerRequest();

    if (_state.MotorOn)
    {
        // Set the motor power on
        motorRequest.TargetPower = 1.0;
        message = "Motor On";
    }
    else
    {
        // Set the motor power off
        motorRequest.TargetPower = 0.0;
        message = "Motor Off";
    }

    // Send the motor request
    _motorPort.SetMotorPower(motorRequest);

    // Show the motor status on the console screen
    LogInfo(LogGroups.Console, message);
}

 

The added code causes the BumperHandler method to:

  1. Check whether the bumper is pressed.
    If the event that occurred was a bumper release, the BumperHandler method returns.
  2. If there was no bumper release, the BumperHandler method toggles the state of the motor, turning it on or off.
  3. Depending on the new motor state value, the BumperHandler method then creates a SetMotorPower request message to set the motor power to either 0% or 100%.
  4. The BumperHandler sends the SetMotorPower message.
  5. and then displays logs the action that was taken.

Try It Out

If you started with Robotics Tutorial 1, you have already added a hardware manifest for your robot. Please open your project settings under the Debug tab and verify the Command Line Arguments have been modified to reference the proper manifest for your hardware. Refer to Robotics Tutorial 1 (C#) - Accessing a Service if you have not completed this step.

Bb483051.hs-note(en-us,MSDN.10).gif

If you use the completed tutorial, samples\RoboticsTutorials\Tutorial2\CSharp\RoboticsTutorial2.csproj, be sure to modify your project settings under the Debug tab and change the Command Line Arguments setting to reference the proper manifest for your hardware. The sample manifest in this case will be RoboticsTutorial2.manifest.xml.

/port:50000 /tcpport:50001 /manifest:"samples\Config\RoboticsTutorial2.manifest.xml" /manifest:"samples\config\LEGO.NXT.MotorTouchSensor.manifest.xml"

Save your project.

To build and run the MyTutorial1 service, open the Debug menu and select Start Debugging (or press F5).

Now press the bumper several times. The motor should start or stop, and a message that indicates the action that was taken should be logged to the Debug and Trace Messages page (which you can view using a web browser by going to https://localhost:50000 and selecting Debug and Trace Messages from the menu at the left of the page).

        Motor On
        Motor Off
        Motor On
        Motor Off

If you want to compare your code with a finished example, you can find this completed tutorial in the samples\RoboticsTutorials\Tutorial2\CSharp subdirectory.

Summary

In this tutorial, you learned how to:

  • Add References.
  • Subscribe to the Bumper Service.
  • Modify the Bumper Handler.

 

 

© 2012 Microsoft Corporation. All Rights Reserved.