Microsoft Robotics Studio provides a re-usable design for writing services. This design allows you to write a service once to a common hardware specification and then use that service across a variety of hardware robotics platforms.
This tutorial teaches you how to create a custom activity that partners with abstract, base definitions of hardware services. Then, based on a configuration file (a manifest), that service binds at runtime to a specific implementation of these hardware services. The tutorial implements a very basic wandering behavior.
If a bumper is pressed, your robot backs up for a short distance, turns randomly for a brief amount of time, and then goes straight at a random velocity.
This tutorial can also be used in a Simulation without requiring any robotics hardware.
This very primitive behavior will result in the wheeled robot bumping and turning into things. For a more intelligent (and more advanced orchestration service) please review Robotics Tutorial 5 (C#) - Using Advanced Servicesand the Explorer Service.
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 Studio installation folder:
SamplesRoboticsTutorialsTutorial3mvpl
This tutorial teaches you how to:
Prerequisites
Hardware
You need a robot with microcontroller and a contact sensor. 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. Configure your contact sensors so that one is at the front and the other is at the rear.
This tutorial also requires two motors in a two-wheeled differential/skid drive configuration.
Connect the sensors and motors to your robotic platform following the normal conventions for the hardware you are using.
To determine if support is included in Microsoft Robotics Studio for your robot and to setup your hardware, see Setting Up Your Hardware Overview. 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 Microsoft Robotics 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 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
Begin by opening the Microsoft Visual Programming Language development environment and creating a new project by selecting New from the File menu.
Step 1: Create a Custom Activity using Generic Services
Microsoft Robotics Studio comes with a large number of services that you can use to build your programs. Sometimes, however, they don't have exactly the commands that you are looking for. For this tutorial you need a drive activity that has commands for backup, turn randomly, and drive straight with a random speed. Unfortunately, there is no such activity. You can build one yourself by using the Generic Differential Drive. (also see VPL Tutorial 3 - Create Your Own Activity)
Create a new activity by dragging an Activity into your diagram. You should always give your activities meaningful names. Select the new block and go to the Properties window to change its name to RandomDrive.
Open the new activity to see its implementation by double-clicking on its block or selecting Open from the context menu. This opens a new page displaying the diagrams contained within the activity. It has one diagram for each action. You can choose the displayed action with the dropdown box at the top of the page.
Figure 2 - New activity page
A new activity initially has two actions. The Start action runs once immediately after the activity starts. You don't need it in this tutorial and can simply ignore it. The second action, named Action, can be used as a command from other diagrams in the same way you can use operations of a service in the previous tutorials. An activity can have more than these two actions. To add new actions to the activity, use the Actions and Notifications command in the Edit menu or press the button next to the action selector dropdown on top of the page.
In the Actions and Notifications dialog, add two more actions by clicking the + button in the Actions list twice. Select each action and change their names in the Name input field to Backup , Turn, and Drive.
Figure 3 - Actions and Notifications dialog
When you implement these actions, you have to know in which direction the robot is currently going so that you can reverse it. The direction can be sent as an input value to the action. For each action, add a new input value. Change the input value's name to Polarity and its type to double. (You will later use -1.0 to indicate reverse and 1.0 to indicate forward.) Click OK when you are done.
Figure 4 - Actions dialog with input
This tutorial explains in detail how to implement the Turn activity, the most complex of the three. Drive and Backup are simplified versions of Turn.
On the action selector dropdown, select the Turn action. A screen opens with connectors on the left and right side. The connectors represent the input (left) to the action and the action's output (right).
The turn speed should be random, so you need to come up with a random value. The easiest way to do that is by using the Math Functions. Add a MathFunction block to your diagram and connect the actions input to the MathFunctions block. On the connections dialog, choose Random from the To list. Random doesn't take any input values so there is nothing to set up for Data Connections. Random delivers values between 0.0 and 1.0. While 1.0 is also the maximum value to set the motor speed on the generic drive, in the interest of public safety, you should consider scaling it down a bit. You also want to make sure that the value isn't too close to zero since you wouldn't turn at all.
Connect the output of the MathFunctions block to a new Calculate and choose Random - Success from the From in the Connections Dialog.
Figure 6 - Create a random number
The resulting random value will be between 0.25 and 0.75. Use this value to calculate the motor power to set on the differential drive. In order to take the current direction in which the robot is traveling into account, you need to extract the Polarity value from the input using a Calculate and combine it with the random value using a Join.
Figure 7 - Joining Random and Polarity
In order to make the robot turn, the wheels have to rotate in different directions. Use two Calculate blocks to calculate the value for the left and the right wheel and combine the values using a Join, calling the values Left and Right.
Figure 8 - Calculating left and right speeds
Add a new Generic Differential Drive to your Turn action and connect it to the Join. Select the SetDrivePower command from the To list. In the Data Connections dialog. Select Left for LeftWheelPower and Right for RightWheelPower.
Figure 9 - Unconnected timer
Now you've got your robot turning. You want to make sure that it can actually turn for some time so that it really changes direction. A simple way of achieving this is by waiting for a brief period of time before doing something else. Add a new Timer service to the Turn action. Add a Data block, setting int as its type and connect it to the Timer. Select Wait in the Connections Dialog's To list. In the Data Connections dialog, set the value to Interval . Interval is the amount of time to wait in milliseconds. Enter a value that is appropriate for the speed of your robot in the Data block.
Figure 10 - Completed handler
Connect the output of the drive's SetDrivePower operation to the Data block and choose SetDrivePower - Success from the From list. Connect the Timer to the Result output of the Turn action. The Turn action has no output values. However, you can still send a result message indicating "I'm done" by connecting a message to the Timer, which makes sure that this message is not sent before you're done.
You can build Drive and Backup based on Turn. When you add the GenericDifferentialDrive to the Drive and Turn, make sure to select the same drive and do not create a new one when you are asked. All GenericDifferentialDrive blocks in all actions must have identical names. Alternatively, you can just copy the GenericDifferentialDrive block from the Turn action. By using the generic drive, you can use your RandomDrive with many different platforms and even simulation without having to change it.
Figure 11 - Activity dialog
The Backup action is similar to Turn except it uses fixed values instead of random and uses the same power for both wheels.
The Drive uses the same random value for both wheels and doesn't wait. This allows a change in direction immediately after starting to go forward again.
Step 2: Orchestrate the Custom Activity
The RandomDrive activity can now Backup, Turn randomly, and Drive forward with a random speed. Use this activity to build the wanderer program.
Go back to the main diagram of your project.
Add a Generic Contact Sensor (i.e. a bumper) service to the diagram. By using the generic drive, you can use your wander diagram with many different platforms. Whenever the bumper is pressed, the following actions occur:
Wandering is now simply a matter of chaining up the RandomDrive correctly.
First, add a new If and connect it to the contact sensors notifications (drag its round connector to the If). Select Update from the From list.
In the If condition, type Pressed. You now need to know what the current direction of the robot is and change it. Add a new variable block and define a variable called Polarity of type double. Connect the If's condition to the variable and select GetValue from the Connections dialog.
Figure 14 - Add the Polarity variable
Now you have the current direction and can use it to Backup. Connect the Variable block to the RandomDrive block. Select the Backup action in the To of the connection dialog. In the Data Connections dialog select, Polarity for the Polarity input value.
Figure 15 - Connect Polarity to RandomDrive
After Backup has completed, you can Turn. Because you also need the new Polarity value, you have to combine (add a Join) the result of Backup with the result of the calculate that calculates the new direction (by inverting the old one). Make a copy of RandomDrive and connect the Join block to it. In the Connections dialog, select Turn from the To list. In the Data Connections dialog, select Polarity for the Polarity input value. Repeat these steps to add the Drive command after the Turn command.
Finally, you need to remember the new direction in the variable so that you can use it the next time the bumper is pressed. Add a new Variable block, select Polarity, and connect the Calculate to it. Select SetVariable in the Connections dialog. Use a new Data block and another Variable block to initialize the variable with 1.0 (forward).
When you're finished, your diagram should look like the following.
Figure 17 - Completed wander diagram
Step 3: Define a Manifest for Your Hardware
Before you run your program, you need to set the manifests for the ContactSensor and the DriveDifferentialTwoWheel services. Open the context pop-up menus for each of these services and select the Manifest command. In the dialog box which opens, select the manifest for your robot. If you don't have a robot that's supported you can try the simulated mobile robot manifest.
Step 4: Run Your Application
Run your application by selecting the Run command from the Run menu or by pressing F5.