Direction Dialog

Microsoft Robotics

Glossary Item Box

DSS Interop Samples: Desktop Joystick Sample

VPL User Interface Services: Direction Dialog

Microsoft Robotics Developer StudioSend feedback on this topic

Direction Dialog

The Direction Dialog service provides five buttons for controlling the directional movements of a robot.

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

Sample location
Samples\UX\DirectionDialog

Contents:

Prerequisites

Hardware

This sample requires no special hardware.

Software

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

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

Setting Up the Sample

IMPORTANT NOTE: The sample on its own cannot be used to control a robot. It generates notification messages whenever the buttons on the dialog are pressed, but it does not attempt to connect to a robot drive service.

Running the Sample

The simplest way to test this sample is in VPL because the Direction Dialog was specifically designed for VPL. Just drag a Direction Dialog onto your diagram and then drag a Simple Dialog to the diagram as well. Connect the notification output pin to the Simple Dialog to display the name of the button that was pressed. Run your diagram and try clicking on the buttons.

To start the Direction Dialog service on its own, start a node by using following command from the DSS Command Prompt:

Console
dsshost /p:50000 /t:50001 /m:"samples\config\DirectionDialog.manifest.xml"

This starts a Direction Dialog as shown in the figure below. Note that there is no point in specifying another manifest for a robot on the command line because the Direction Dialog will not connect to a robot. In order to control a robot, you must write another service (similar to Robotics Tutorial 4) that takes input from the Direction Dialog and generates SetDrivePower requests to the robot's drive service.

Direction Dialog

Direction Dialog - Buttons to control robot movements

 

How it works

The Direction Dialog service creates a Windows Form with five (5) buttons - one for each of the four primary directions and a Stop button in the middle. The following code for the service's Start method creates the Windows Form, which has a class name of Direction:

C#
/// <summary>
/// Service Start
/// </summary>
protected override void Start()
{
    _state.Buttons.Add(new Button(ButtonDirection.Forwards));
    _state.Buttons.Add(new Button(ButtonDirection.Backwards));
    _state.Buttons.Add(new Button(ButtonDirection.Left));
    _state.Buttons.Add(new Button(ButtonDirection.Right));
    _state.Buttons.Add(new Button(ButtonDirection.Stop));

    base.Start();

    WinFormsServicePort.Post(
        new RunForm(
            delegate()
            {
                return new Direction(
                    ServiceForwarder<DirectionDialogOperations>(ServiceInfo.Service)
                );
            }
        )
    );
}

Notice that the Form is passed a service forwarder port so that it can send messages back to the service. (By convention there is a global variable called _mainPort that could be passed instead, but the example shows how to create a forwarder).

In the initialization code (constructor) for the Direction Form, the service port is saved into a class variable so that it can be used later to post messages:

C#
DirectionDialogOperations _mainPort;

/// <summary>
/// Constructor for Direction Form
/// </summary>
/// <param name="mainPort">Port to post messages back to</param>
public Direction(DirectionDialogOperations mainPort)
{
    _mainPort = mainPort;

    InitializeComponent();
}

The service accepts subscriptions using the usual Subscribe operation which is not covered here. Meanwhile the Form listens for button presses and releases as well as keyboard presses and releases. When one of these occurs, the Form posts an appropriate message to the service. For example, here is the code for the Button Press (Mouse Down) event handler in the Form. Notice that it posts a ButtonPress back to the service.

C#
private void button_MouseDown(object sender, MouseEventArgs e)
{
    if (sender is winforms.Button)
    {
        winforms.Button button = (winforms.Button)sender;
        if (button.Name.StartsWith("btn"))
        {
            string name = button.Name.Substring(3);

            _mainPort.Post(new ButtonPress(new ButtonPressRequest(name)));
        }
    }
}

The mouse event handlers post messages to the main service, which is then responsible for sending notifications to subscribers. The following code shows the handler for Button Presses in the service code:

C#
/// <summary>
/// ButtonPressHandler - Reacts to button presses
/// </summary>
/// <param name="buttonPress">Button press info sent from the Form</param>
/// <returns></returns>
[ServiceHandler(ServiceHandlerBehavior.Exclusive)]
public virtual IEnumerator<ITask> ButtonPressHandler(ButtonPress buttonPress)
{
    Button button = _state.Buttons.Find(
        delegate(Button test)
        {
            return test.Name == buttonPress.Body.Name;
        }
    );
    if (button == null)
    {
        buttonPress.ResponsePort.Post(Fault.FromCodeSubcodeReason(
            soap.FaultCodes.Receiver,
            DsspFaultCodes.UnknownEntry,
            "No such button: " + buttonPress.Body.Name
            )
        );
    }
    else
    {
        button.Pressed = true;
        buttonPress.ResponsePort.Post(DefaultUpdateResponseType.Instance);
        SendNotification(_submgr, buttonPress);
    }
    yield break;
}

Several keyboard keys can also be used as synonyms for the dialog buttons and they operate in a similar way to button presses. The keyboard event handlers in the Form are basically the same as the mouse event handlers. You can see which keys correspond to the different directions quite clearly in the key processing code inside the Form:

C#
private string GetDirectionName(Keys key)
{
    string name = string.Empty;

    switch (key)
    {
        case Keys.Up:
        case Keys.W:
        case Keys.NumPad8:
            name = "Forwards";
            break;
        case Keys.Right:
        case Keys.D:
        case Keys.NumPad6:
            name = "Right";
            break;
        case Keys.Down:
        case Keys.S:
        case Keys.NumPad2:
            name = "Backwards";
            break;
        case Keys.Left:
        case Keys.A:
        case Keys.NumPad4:
            name = "Left";
            break;
        case Keys.NumPad5:
        case Keys.Space:
            name = "Stop";
            break;
    }

    return name;
}

/// <summary>
/// ProcessDialogKey - Override the normal key processing
/// </summary>
/// <param name="keyData">Keystroke to check</param>
/// <returns></returns>
[UIPermission(SecurityAction.LinkDemand, Window = UIPermissionWindow.AllWindows)]
protected override bool ProcessDialogKey(Keys keyData)
{
    // Allow arrow keys to be captured by the KeyDown event
    if (keyData == Keys.Up || keyData == Keys.Down ||
        keyData == Keys.Left || keyData == Keys.Right) return false;

    return base.ProcessDialogKey(keyData);
}

There are two important points to note about handling the keyboard in this way. Firstly, the Form itself has its KeyPreview property set to True and secondly there must be a ProcessDialogKey method as shown in the preceeding code snippet. This allows the arrow keys to be used in the Form code instead of being treated as special function keys that move the focus from one control to another on the Form. Without this little "trick" the arrow keys will not work, although other keys (W, A, S, D and Space) will work because they are not function keys.

Related Services

Compare this service with Robotics Tutorial 4 where a direction dialog is effectively built into the service. Robotics Tutorial 4 could be re-written to use the Direction Dialog service instead of creating its own Windows Form.

Summary

DSS Interop Samples: Desktop Joystick Sample

VPL User Interface Services: Direction Dialog

 

 

© 2012 Microsoft Corporation. All Rights Reserved.

Show: