Share via


Robotics Tutorial 2 (VPL) - Coordinating Services

Glossary Item Box

Microsoft Robotics Developer Studio Send feedback on this topic

Robotics Tutorial 2 (VPL) - Coordinating Services

This tutorial builds on Robotics Tutorial 1 (VPL) - Accessing a Service by using a sensor to turn a motor on and off. Like the previous tutorial, it requires sensor and motor services for a robot compatible with Microsoft Robotics Developer Studio (RDS). Also as in previous tutorial, you need to setup your robot hardware appropriately before running this tutorial.

This tutorial is provided in the Microsoft Visual Programming Language (VPL) language. You can find the project files for this tutorial at the following location under the Microsoft Robotics Developer Studio installation folder:

Sample location

Samples\RoboticsTutorials\Tutorial2\mvpl

This tutorial teaches you how to:

  • Add and Initialize a Variable
  • Add, Configure, and Connect New Activities
  • Add and Configure an If Activity.
  • Add and Configure a Simple Dialog Box and Generic Motor
  • Run Your Application

See Also:

  • Getting Started

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 sonar 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 RDS 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 by performing the Service Tutorials included in RDS). 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 Programming Language.

Microsoft Visual Programming Language (VPL) is an application development environment designed on a graphical dataflow-based programming model rather than control flow typically found in conventional programming. Rather than series of imperative commands sequentially executed, a dataflow program is more like a series of workers on an assembly line who do their assigned task as the materials arrive. As a result, VPL is well suited to programming a variety of concurrent or distributed processing scenarios.

VPL is targeted for beginning programmers with a basic understanding of concepts like variables and logic. However, VPL is not limited to novices. The compositional nature of the programming language may appeal to more advanced programmers for rapid prototyping or code development. In addition, while its toolbox is tailored developing robot applications, the underlying architecture can be applied to other applications. As a result, VPL may appeal to a wide audience of users including students, enthusiasts/hobbyists, as well as possibly web developers and professional programmers.

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

Getting Started

While you could modify and extend the previous tutorial application, this tutorial starts you from the beginning.

To begin, open the Microsoft Visual Programming Language development environment and create a new project by selecting New from the File menu.

Step 1: Add and Initialize a Variable

Add a Variable activity to the diagram. Click on ... at the bottom of the block (or select Variables... from the Edit menu) to open the Variables dialog box. Add a variable called MotorOn of type bool. (A bool variable represents a Boolean and can be either true or false). Refer to the figure below.

Figure 1

Figure 1 - Define MotorOn variable

Set the Variable activity to use this new MotorOn variable by selecting it from the drop-down list in the middle of the block. You will use this variable to store the state of the robot's motor.

Add a Data activity to the diagram (to the left of the Variable activity). Select bool from the drop-down list at the bottom of the Data block. (This data type should match the variable). When you select bool, the text box in the Data block will update to show the value false. You do not need to change this because this is the correct value.

Connect the output of the Data activity to the input of the Variable block. Set the connections as DataValue in the From column and Set Value in the To column.

Figure 2

Figure 2 - Initialize MotorOn variable

This short dataflow (only two blocks) initializes the MotorOn variable with the value false. You don't need to connect these two activities to any of the other activities in the diagram - they will be executed when the program starts.

If you have previous programming experience, you might question why it is necessary to initialize MotorOn because the default value of a new bool variable will be false anyway. This step is actually included to illustrate initialization to novice programmers. This is a common task in programs, and often variables will require initial values that are not the default values.

Step 2: Add, Configure, and Connect New Activities

Insert a Generic Contact Sensors service and a Calculate activity into your Diagram window. Connect the two using the round notification connection pin on the Generic Contact Sensors and the input pin on the Calculate activity. In the Connections dialog box, select ContactSensorUpdate and Calculate as your connections.

Click on the text box in the Calculate block and a list will pop up. Select Pressed from the list. (You can also type directly into the text box. However, if you do this, note that the name of the value is case-sensitive.) This is shown in the figure below. Notice that there are tool-tips that appears to explain what the values are in the list. The variable Pressed is called a data member or property of the notification message from the Contact Sensor.

Figure 3

Figure 3 - Selecting a data member from a message

This part of the diagram should now look like the following:

Figure 4

Figure 4 - Obtaining the state of the Contact Sensor

Add another Variable activity to the diagram to the right of the Calculate block and select the MotorOn variable from its drop-down list.

Connect the Calculate output to the Variable input to set the value of the MotorOn variable. This is shown below:

Figure 5

Figure 5 - Set the MotorOn variable to the Contact Sensor state

Step 3: Add and Configure an If Activity

Now insert an If activity into your diagram. Place it to the right of the Variable activity you just inserted and connect the output of the Variable block to its input. In the text box in the If select MotorOn. This uses the current state of the variable to determine what to do next. Notice that the If block has two outputs - the first one is used if the condition in the text box evaluates to true and the Else output is used if the condition is false.

Figure 6

Figure 6 - Add an If activity

Next, insert two more Data activities. In the first block, select string as the data type and enter Motor On. (This is a string, not the name of the variable). In the second block, select double and enter 1.0. Connect both of these blocks to the top output pin of the If block. Notice that you can send an output message to as many other blocks as you want, so you can drag connections to both of the Data blocks from the same output pin on the If.

Add another two Data activities. Select string and enter Motor Off in the first one, and select double and enter 0.0 in the second one. Connect these two blocks to the Else output of the If. This part of the diagram should look like the following:

Figure 7

Figure 7 - Connections to the If

Notice that in this section of the diagram the data content of the messages from the If is irrelevant. The Data blocks just need the messages as part of the program's control flow, i.e. to initiate an action.

Step 4: Add and Configure a Simple Dialog Box and Generic Motor

Insert two Simple Dialog services and connect one to the "Motor On" Data block and the other to the "Motor Off" Data block. You can drag one from the Services toolbox and then copy and paste it to create the second one. When you are asked if you want to add a new activity, just select the existing Simple Dialog. To establish the Data Connections, connect value to AlertText.

Figure 8

Figure 8 - Adding Simple Dialogs

These dialogs are intended just to show you what is happening. They are not necessary to control the robot. Dialogs like this are often used as debugging aids during development and then removed in the final version of a program.

Now finish the diagram by inserting two Generic Motor service blocks. When you add the second Generic Motor (or copy the one you put into the diagram), you will be asked whether to use the existing instance or to create a new one. Select the existing activity and click OK. If you mistakenly select the option to add a new Generic Motor activity, the program will expect to work with two different motors, which is not the intention, and it will not work properly.

Connect one Generic Motor to the Data block containing 1.0 and the other one to the block with 0.0. Select SetMotorPower as the action on each of the motors and in the Data Connections connect value to TargetPower. The TargetPower can be a value between 0.0 and 1.0, with 0.0 meaning stopped and 1.0 meaning full power. (You can also use negative values from 0.0 to -1.0 to make the motor rotate backwards). The last part of the diagram should look like the following:

Figure 9

Figure 9 - Adding a Generic Motor

Like the Generic Contact Sensors service, the Generic Motor is a service that can be applied to different robots. You need to set the manifest for each of these services, which you can do by displaying their context pop-up menus (right-clicking on the blocks), then selecting the Set Configuration command. In the dialog box that opens, select Use a manifest from the drop-down list, click the Import Manifest... button, and select the manifest that applies to robot contact sensor and the robot motor you are using. It is possible that the contact sensor and motor will be in the same manifest. For example, LEGO.NXT.MotorTouchSensor.manifest.xml. So when you set the second configuration you will not have to import the manifest again, just use the existing manifest.

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

You only need to set the manifest for one of the Motor activities since they are both references to the same Motor.

When your diagram is complete it should look like the following.

Figure 10

Figure 10 - Completed Diagram

Step 5: Run Your Application

Run your application by choosing the Run command from the Run menu or by pressing F5.

When you press the bumper (contact sensor) on the robot, one of the motors should turn on. The motor should stop when you release the bumper.

Bb483036.hs-tip(en-us,MSDN.10).gif

This example uses a single Generic Motor. Some robots do not have separate motor services for each of the motors. Instead they use a Generic Differential Drive (GDD), which controls two wheels at the same time. You can modify this diagram to use a GDD instead of the Generic Motor. If you do this, you will have to use the SetDrivePower action which requires values for the LeftWheelPower and RightWheelPower. You can use the value from the If for both of these in the Data Connections.

Summary

In this tutorial, you learned how to:

  • Add and Initialize a Variable
  • Add, Configure, and Connect New Activities
  • Add and Configure an If Activity.
  • Add and Configure a Simple Dialog Box and Generic Motor
  • Run Your Application

 

 

© 2012 Microsoft Corporation. All Rights Reserved.