CIS 3.5, Lab 4.2 -
Visual programming languages
tutorial #3: Simulation Enviroments
Many robots use what is called a "Differential Drive" which has two wheels that can be driven independently. Although most Differential Drives have two driven wheels, there is often a third passive wheel, called a castor or jockey wheel, which is just for balance. The reason this configuration is popular is that it allows the robot to rotate on the spot. It can therefore drive in any direction after making a tight turn that takes a space no larger than the robot.
RDS defines a generic contract for a Differential Drive that specifies the programming interface for controlling a drive regardless of the type of robot that you are using (which is why it is called generic).
In this lab you will only use the SetDrivePower operation which simply sets the power to each of the drive's wheels. However, as you will see, this gives you complete control over where the robot goes.
To get started, open the Visual Programming Language application.
Step 1: Add a Desktop Joystick Service
Add the service
From the Services toolbox at the bottom-left of the VPL window, drag the Desktop Joystick service onto your workspace, which is called a diagram. An activity block will appear in the diagram to represent the service. This is shown in Figure 1.
Figure 1 - Desktop Joystick Activity
The Desktop Joystick service displays a window that exposes the basic capabilities of a joystick which can be driven with a mouse or keyboard. As with most activities, it has an input connection pin (or port) on the left (a red arrow pointing into the block), an output connection pin on the right (a red arrow pointing out of the block) and a notification pin also on the right (a red circle). You send requests to the service via the input pin. These are also called actions or operations. The result or response is sent on the output pin. Notifications can be sent at any time by a service - you do not have to send a request. These are important for a service like the Desktop Joystick because it sends messages constantly when you are moving the joystick. Your VPL program can process these messages and use them to control a robot.
Try it out
From the menu, select Run -> Start, or press the F5 key. The first time that you run a new diagram you will be prompted to save it. If you opened the diagram what was installed as part of RDS (rather than creating a new one) it is a good idea to save it to a different location using File -> Save As. This way you will keep the original diagram intact so that you can refer back to it if you accidentally mess up the diagram.
VPL will start a DSS Node and run the diagram. (All services in RDS run under Decentralized Software Services, or DSS for short). When the diagram starts running you will see a window entitled Desktop Joystick. Figure 2 shows what it looks like while it is in use. Spend a little time seeing how this window responds to the mouse and keyboard.
Figure 2 - Desktop Joystick in action
/ Click on the "trackball" with the left mouse button and hold the button down. As you drag, it will simulate moving a joystick. With the keyboard, the keys W, A, S, and D can also be used. If you play computer games you might be familiar with these keys from first-person shooter games. If not, look carefully at the layout of these keys on the keyboard because they correspond to the four directions: forward; left; back; and right. With a little practise you should be able to control the joystick quite easily.Step 2: Add a Generic Differential Drive
In the same way as you added the Desktop Joystick service in Step 1, now add a Generic Differential Drive (GDD) service. It is called generic because it supports common operations available on most differential drives, but it is not by default associated with a specific drive system. A generic drive can be associated with a real drive at runtime using a file called a manifest. One of the benefits of RDS is that you can write general programs that will run on a variety of robots simply by changing the manifest.
To associate the generic drive with a specific robot drive system, so that you can interact with the robot's hardware, first click on the Generic Differential Drive block on the main diagram to make sure that it is selected. (The activity block will be highlighted with a blue border). Then, in the Properties toolbox on the bottom-right, under Configuration, select the Use a Manifest entry from the drop-down list. Now, under Manifest, click the Import button. Select the entry for IRobot.Create.Simulation.Manifest.xml. The Properties toolbox should now look like Figure 3.
Figure 3 - Associate the Generic Differential Drive with a specific robot's manifest
The manifest specifies, among other things, which service will be implementing the generic drive when you run the program. By selecting a manifest, the generic drive in the diagram has been associated with a particular robot drive service (in this case a simulated iRobot Create). If you wanted to use a real Create, or some other robot, you would just select a different manifest.
Step 3: Connect the Components
Connect a notification to a request
Robotics Developer Studio supports a Publish-Subscribe model. A service, or in this case your diagram, can subscribe to the notifications that another service publishes. In this example, when the position of the joystick changes the Desktop Joystick service will issue a notification. The diagram can receive that notification by connecting to the notification pin on the Desktop Joystick service, which is the small red circle on the right side of the activity block. The small red arrow on the right is a result output. These outputs are discussed in more detail in the Quick Introduction to VPL at the end of this lab. In this example the output pin is not used.
Connect the notification pin of the Desktop Joystick (the small red circle) to the input pin of the GDD (the small red arrow on the left side) as shown in Figure 4. To do this, click the left mouse button on the notification pin and hold it down, then drag the mouse over the GDD block. You do not have to place the mouse exactly over the input pin. A connection link will follow the mouse as you move the cursor. (Wave the mouse around. It's fun to watch VPL drawing bezier curves.) The GDD block will be highlighted when you have the mouse positioned correctly. Release the mouse button and the connection link will snap into place between the two blocks. Figure 4 shows what the completed connection looks like, but you have not finished yet.
Figure 4 - Connect the Desktop Joystick to the Generic Differential Drive
The Connections dialog box will pop up. This shows the available notifications that the Desktop Joystick service supports in the From column on the left, and the available request types that the GDD service supports in the To column on the right. The purpose of this VPL diagram is to drive a robot using the joystick, so select UpdateAxes from the left column and SetDrivePower in the right column, then click OK.
Figure 5 - Available connections between Desktop Joystick and Generic Differential Drive
Now the Data Connections dialog box pops up. This dialog allows you to specify how the notification produced by the Desktop Joystick is consumed by the GDD service.
Figure 6 - Data Connections dialog box
As shown in Figure 6, you can select which data member from the notification message is sent to the each of the target fields in the request to the GDD service.
Configure the Data Connection
The Desktop Joystick service sends notifications when the joystick axes change (in this case the X and Y values change), as you may have noticed when trying out the diagram earlier. (While the joystick is not moving, notifications are not sent). These values vary between -1000 and 1000.
The GDD service expects LeftWheelPower and RightWheelPower values between -1.0 and 1.0, where these values indicate the amount of power to be fed to the Left and Right wheels respectively. If both are positive then the robot will move forward; if both are negative then the robot will move backwards; if the left and right values are different then the side of the robot with the higher value will move forward move than the side with the lower value, causing the robot to turn in an arc. If you exactly match the power settings, but they have opposite signs, then the robot will rotate on the spot.
You now need to create a data transformation that will convert the X and Y values from the Desktop Joystick into LeftWheelPower and RightWheelPower settings. Select the Edit values directly check box at the bottom of the DataConnections dialog. Then enter the following expressions into the Value boxes for the LeftWheelPower and RightWheelPower targets.
- (-Y + X) / 1000.0
- (-Y - X) / 1000.0
The DataConnections dialog will now look like Figure 7. Take a moment to understand how these expressions convert the X and Y values from the joystick into left and right values for a differential drive system. Notice that there is a scale factor of 1000. You could use more complicated formulas, but these ones work reasonably well.
Figure 7 - Data Connections dialog for SetDrivePower
/ Once a connection has been established, you can change the details later. If you right-click on the connection link, the pop-up context menu contains options to delete the link or open the Connections or Data Connections dialogs. If you select a link by clicking on it with the left mouse button, the Data Connections are shown in the Properties toolbox and you can change them there. This is similar to the Data Connections dialog. When a link is selected, small boxes appear over the pins at each end. Look carefully because it might not be obvious to you if you are a beginner.Step 4: Try It Out
Running in simulation
Select Run -> Start from the menu, or press F5. Have fun driving your simulated robot around, immersed in the realism of the simulation engine's physics model. If you click on the Simulation window so it has focus, and you can use the the mouse and also the keys W, A, S, D, Q, and E to change the camera location in the simulated environment.
Figure 8 - Simulated iRobot Create in simulated environment
Note that the Desktop Joystick automatically displays on top of other windows so it appears in Figure 8 over the top of the simulation. It is not part of the simulation window.