Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Задани на лабораторные работы. ПРК / Professional Microsoft Robotics Developer Studio

.pdf
Скачиваний:
126
Добавлен:
20.04.2015
Размер:
16.82 Mб
Скачать

www.it-ebooks.info

Chapter 11: Visually Programming Robots

Notice that all you needed to change was the manifest associated with the diagram. In many cases, it is possible to define an algorithm using a VPL diagram that can run equally well in simulation and on a real robot simply by changing the manifest associated with the diagram.

VPL in the Driver’s Seat

You may have noticed that there is a GenericDifferentialDrive service in the Services toolbox. This is the generic contract to control two motors together to implement a differential drive. This activity can be used to control the motion of a differential drive robot. Consider the diagram shown in Figure 11-7, which you can find in the 3-DriveInCirclesSim project in the chapter11 directory.

Figure 11-7

A differential drive allows a different power or speed to be specified for both the left and right drive motors. In this very simple example, the right wheel is given a drive speed of 0.5, and the left wheel drives at 0.3. Because the right wheel moves faster, the robot turns to the left at a constant rate and therefore travels in a circle, as shown in the overlay image in Figure 11-8.

513

www.it-ebooks.info

Part III: Visual Programming Language

Figure 11-8

You can also specify a nice spiral with a Tribot, as shown in the 4-Spiral project in Figure 11-9.

Figure 11-9

514

www.it-ebooks.info

Chapter 11: Visually Programming Robots

This is the first time the Timer activity has been used. A timer interval in milliseconds is passed to the SetTimer input, and after that length of time the FireTimer notification sends a message. The Right and Left variables start out at 0.3 and 0.05, respectively, which causes the Tribot to start in a tight turn. The radius of the turn is gradually increased as 0.0005 is added to the Left speed 10 times per second.

The manifest used for this example is the Lego.NXT.Tribot.Simulation.Manifest.xml manifest. Because a copy is made of the manifest and its associated configuration files when it is imported, the main camera Location and LookAt values were changed in the VPL project to change the initial viewpoint of the camera by editing the XML configuration file: lego.nxt.tribot.simulationenginestate (LEGO.NXT.Tribot.Simulation.Manifest).xml. The drive path is shown in Figure 11-10.

Figure 11-10

It is possible to control the movement of a robot more precisely by using the DriveDistance and RotateDegrees inputs of the differential drive, as illustrated in the 5-DriveDistance diagram shown in Figure 11-11.

515

www.it-ebooks.info

Part III: Visual Programming Language

Figure 11-11

In this diagram, you use the DriveDistance and RotateDegrees inputs of the GenericDifferentialDrive to move forward a precise distance at a specified speed and then to turn a precise number of degrees at a specified speed. The Corobot manifest developed in Chapter 6 has been used for the configuration. The GenericDifferentialDrive does not send an output message until the requested motion has been completed, so the output of one motion can be fed directly to the input of another motion. The DriveDistance and RotateDegrees inputs can be used to drive a robot in a search pattern or to navigate to a specific location, as shown in Figure 11-12.

Figure 11-12

516

www.it-ebooks.info

Chapter 11: Visually Programming Robots

Using VPL to Read Sensors

Any real robot control algorithm needs to be able to read values from multiple sensors and then control actuators such as drive motors or joint servos to move the robot. The next two examples show how this can be done together.

The diagram in the 6-BackAndForth project shows how to use a sensor reading to control the differential drive. You use the Corobot manifest again because it has an IR distance sensor mounted on its front and back. The IR distance sensor implements the GenericAnalogSensor contract so that is the activity used in the diagram.

The first thing the diagram does is rotate the robot by 180 degrees so that its front is facing the large box. The Ready variable is used to discard the sensor notifications until this move has been completed. After the Ready variable is set to true, the notifications from the distance sensor are used to set the motor speed. The Normalized distance value is used, which varies between 0 and 1. If the distance is larger than 0.9, then the robot is driven forward to get closer to the obstacle. If it is less than 0.2, then the motors are reversed to back away from the obstacle. The robot approaches the obstacle and then reverses before hitting it, backs away to a sufficient distance, and then approaches again. The diagram is shown in Figure 11-13.

Figure 11-13

517

www.it-ebooks.info

Part III: Visual Programming Language

This diagram uses two generic contract activities, and both of them must be properly configured or VPL will report an error as it is initializing.

It can be difficult to debug diagrams that have a high frequency of notifications. The debugger tends to be unresponsive due to the notification processing that is going on. In this case, it is sometimes easier to debug a diagram by inserting SimpleDialog activities at strategic points to print the values on the wire or the values of state variables. It’s just like inserting printfs in the old days.

The Laser Range Finder

The Laser Range Finder sensor presents more of a challenge than the GenericAnalogSensor because it returns so much data. It casts 360 rays and reports the distance to intersection of each of them. It is up to the VPL diagram to interpret all of that data and determine the distance to an important object.

The 7-LaserRangeFinder project in the chapter11 directory shows one way to do this. The top-level diagram is shown in Figure 11-14; the implementation of the Evaluate action in the EvaluateRanges activity is shown in Figure 11-15.

Figure 11-14

The SimulatedRangeFinder activity is configured to use the MobileRobots.P3DX.Simulation

.Manifest.xml manifest. At periodic intervals, this activity sends a Measurement notification, which consists of 360 distance measurements in a list of integers along with some other data. In the main diagram shown in Figure 11-14, the data is passed to the EvaluateRanges activity along with a distance

518

www.it-ebooks.info

Chapter 11: Visually Programming Robots

threshold. This activity then sends a response message containing a Boolean called Near, which is true if any of the distance measurements are less than the threshold. When the value is true, the SoundPlayer activity sounds a single beep. When you drive the robot so that an obstacle is nearer to the robot than the threshold distance, the diagram beeps. You can try this by running the diagram and

maneuvering the robot close to one of the obstacles in the scene When you turn the robot so that the laser no longer intersects any obstacles, the beeping stops.

Let’s take a closer look at how the laser range finder data is processed in the EvaluateRanges activity. The implementation of this activity is shown in Figure 11-15.

Figure 11-15

Three values are extracted from the input message and stored in state variables. The list of integers is stored in the Ranges variable, the Threshold value is stored in the Threshold variable, and the count of data values -1 is stored in the Current variable. This variable is used to count backwards through the list of data.

When all of the state variables have been initialized, the diagram enters a loop in which it steps down from the value of the Current variable to 0. If any of the ranges are less than the Threshold value, the loop takes an early exit and passes true to the output. If all of the ranges are traversed without a single one falling below the threshold, then a value of false is passed to the output.

Sometimes it is possible to implement a diagram without using any state variables at all. For example, the diagram in Figure 11-15 can be simplified by removing the Threshold, Current, and Ranges variables. Instead of being stored in variables, these values are passed around on the wires between the activities. Optimization of this diagram is left as an exercise for you.

519

www.it-ebooks.info

Part III: Visual Programming Language

Controlling Multiple Robots

All of the examples you have seen in this chapter so far have dealt with only a single robot or a single sensor. It has been fairly easy to configure each activity because there has been only one service to choose from in each manifest that implements the port represented by the activity. It is fairly common to have multiple sensors on a single robot that each have an associated service of the same type. In addition, it is desirable to be able to control multiple robots at the same time with a single diagram.

When there are multiple services of the same type in a manifest, you must give each service in

the manifest a unique name, and then you must assign each activity to the appropriate service in the manifest.

Consider the 8-TwoRobots project in the chapter11 directory. This is a very simple diagram consisting of two different GenericDifferentialDrive activities and the data to initialize the motors to unique values. The diagram is shown in Figure 11-16.

Figure 11-16

Notice that the two GenericDifferentialDrive activities have different names. This is because when the second one was dragged from the Services toolbox, VPL asked whether to create a new activity or make it refer to the existing GenericDifferentialDrive activity. A new activity was created and so it has a different name. Each of these GenericDifferentialDrive activities must be configured.

The manifest for this project was created by running the iRobot.Create.Simulation.Manifest.xml manifest and then using the Simulation Editor to duplicate the iRobot Create entity and give it a unique name. Refer to Chapter 5 for more examples of the Simulation Editor.

520

www.it-ebooks.info

Chapter 11: Visually Programming Robots

The manifest that runs the services assigns a different name to each of the SimulatedDifferentialDrive services, as is shown in the excerpt below. (The contracts should appear on a single line. They are shown on two lines here due to formatting constraints).

<ServiceRecordType> <dssp:Contract>

http://schemas.microsoft.com/robotics/simulation/services/2006/05/

simulateddifferentialdrive.html </dssp:Contract> <dssp:PartnerList>

<dssp:Partner> <dssp:Service>http://localhost/IRobotCreateMotorBase</dssp:Service> <dssp:PartnerList />

<dssp:Name>simulation:Entity</dssp:Name> </dssp:Partner>

</dssp:PartnerList> <Name>this:simulateddifferentialdrive</Name>

</ServiceRecordType> <ServiceRecordType> <dssp:Contract>

http://schemas.microsoft.com/robotics/simulation/services/2006/05/

simulateddifferentialdrive.html </dssp:Contract> <dssp:PartnerList>

<dssp:Partner> <dssp:Service>http://localhost/IRobotCreateMotorBase0</dssp:Service> <dssp:PartnerList />

<dssp:Name>simulation:Entity</dssp:Name> </dssp:Partner>

</dssp:PartnerList> <Name>this:simulateddifferentialdrive0</Name>

</ServiceRecordType>

The first ServiceRecord runs the SimulatedDifferentialDrive service and associates it with the IRobotCreateMotorBase entity. This service is given the name this:simulateddifferentialdrive. The second ServiceRecord runs the SimulatedDifferentialDrive service and associates it with the IRobotCreateMotorBase0 entity. This service is given the name this:simulateddifferentialdrive0.

Now, back in VPL, right-click the GenericDifferentialDrive activity and select Set Configuration. Select Use a Manifest and select the TwoCreates manifest. In the Manifest drop-down menu in the Properties toolbox, two entries appear that correspond to the names of the differentialdrive services in the manifest. Associate the simulateddifferentialdrive service with this activity. Configure the GenericDifferentialDrive0 activity in the same way, but select the simulateddifferentialdrive0 service with this activity.

Press F5 to run the diagram. Unfortunately, a bug in the 1.5 release of MRDS prevents the VPL model interpreter from running the diagram correctly; only one of the robots moves. Fortunately, you have another way to execute the diagram: compile it. A compiled version of the diagram has already been provided in the chapter11\CompiledTwoRobots directory. You can also compile the diagram to another directory of your choosing. Now click Run Run Compiled Services and you should see both robots happily driving in circles, as shown in Figure 11-17.

521

www.it-ebooks.info

Part III: Visual Programming Language

Figure 11-17

Use this same strategy anytime you must deal with two services of exactly the same type in a single manifest. Make sure that the services are uniquely named and then verify that each activity is paired with the proper service.

Summary

In this chapter, you’ve seen several simple examples of VPL diagrams that read sensor data from simulated and actual robots. In addition, you’ve seen several different ways to control a differential drive. The last example showed how to control multiple robots or multiple sensors of the same type from a single diagram.

The next chapter provides several more examples of VPL diagrams.

522