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

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

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

www.it-ebooks.info

Chapter 12: Visual Programming Examples

This is a fairly basic line-following algorithm. There is a lot of opportunity to make it more sophisticated so that the robot can navigate abrupt changes in the line more consistently.

Figure 12-30 shows the robot making its way along the line from the perspective of a passenger in the Create’s payload bay. Figure 12-31 shows a time-lapse image of the robot as it traverses the entire course.

Figure 12-30

Figure 12-31

553

www.it-ebooks.info

Part III: Visual Programming Language

Ball Follower

Now you’ll make that all-important transition from line-following to ball-following, and you’ll use vision processing to do it. Robots of the future must be able to see and interpret the world around them. In this project, we’ll show you how to design a VPL diagram that enables a robot to track and follow a ball. Vision-based autonomous navigation is left as an exercise for you.

Like the previous project, you’ll implement a Simulation Environment conducive to ball-following, along with any needed services, and then implement a VPL diagram to orchestrate the behavior of the robot.

Building the Simulation Environment

In this project, you use a service to initialize the Simulation Environment. You can find the source code in Chapter12\BallCourt. This service is fairly simple. The PopulateWorld method in the BallCourt.cs file does the usual work of creating sky and ground entities. In this case, the “ground” is a slab rather than an infinite plane but it has essentially the same function.

The BuildArena method builds a ball court with four walls as follows:

MaterialProperties bouncyMaterial =

new MaterialProperties(“Bouncy”, 1.0f, 0.5f, 0.6f); BoxShape[] boxShapes = new BoxShape[]

{

new BoxShape(new BoxShapeProperties(0,

new Pose(new Vector3(0, 0.5f*_state.Scale, -4*_state.Scale)),

new Vector3(12*_state.Scale, 1*_state.Scale, 0.25f*_state.Scale))), new BoxShape(new BoxShapeProperties(0,

new Pose(new Vector3(0, 0.5f*_state.Scale, 4*_state.Scale)),

new Vector3(12*_state.Scale, 1*_state.Scale, 0.25f*_state.Scale))), new BoxShape(new BoxShapeProperties(0,

new Pose(new Vector3(-(6f - 0.125f)*_state.Scale, 0.5f*_state.Scale, 0)), new Vector3(0.25f*_state.Scale, 1*_state.Scale, 8f*_state.Scale))),

new BoxShape(new BoxShapeProperties(0,

new Pose(new Vector3( (6f - 0.125f)*_state.Scale, 0.5f*_state.Scale, 0)), new Vector3(0.25f*_state.Scale, 1*_state.Scale, 8f*_state.Scale))),

};

boxShapes[0].State.Material = bouncyMaterial; boxShapes[1].State.Material = bouncyMaterial; boxShapes[2].State.Material = bouncyMaterial; boxShapes[3].State.Material = bouncyMaterial;

MultiShapeEntity arena = new MultiShapeEntity(boxShapes, null); arena.State.Name = “BallCourt”; arena.State.Assets.DefaultTexture = “BrickWall.dds”; SimulationEngine.GlobalInstancePort.Insert(arena);

Notice that a new material called Bouncy is defined with a restitution set to 1.0f. We also make the walls bouncy so that the ball continues to bounce from wall to wall within the court, enabling our robot to follow it. Each of the walls is defined with a size that can be scaled with the _state.Scale variable.

A MultiShapeEntity is created to hold the wall shapes and then inserted into the Simulation Environment.

554

www.it-ebooks.info

Chapter 12: Visual Programming Examples

The AddBall method creates a ball entity that is both bouncy and slick:

void AddBall(DateTime now)

{

MaterialProperties SlickMaterial =

new MaterialProperties(“Slick”, 1.0f, 0.0f, 0.0f); SphereShape ballShape = new SphereShape(

new SphereShapeProperties(0.1f, new Pose(), 0.1f * _state.Scale)); ballShape.State.Material = SlickMaterial; ballShape.State.DiffuseColor = new Vector4(1, 0, 0, 1); SingleShapeEntity Ball = new SingleShapeEntity(

ballShape,

new Vector3()); Ball.State.Name = “Ball”;

Ball.State.Velocity = new Vector3(_state.BallSpeed * _state.Scale, 0, -_state.BallSpeed * _state.Scale);

Ball.State.Pose.Position = new Vector3(0, 0, 0.5f); SimulationEngine.GlobalInstancePort.Insert(Ball);

}

The slick material has 0 static and dynamic friction and 100% restitution. With this material, the ball will bounce from wall to wall and slide along the ground endlessly without losing any speed. The ball is given a DiffuseColor of red so that the vision algorithm can separate it from the rest of the scene. It is also given an initial velocity based on the BallSpeed variable in the state. This variable can be set in a configuration file.

After the BallCourt is added to the environment, a Corobot entity is also inserted. Five seconds later, the ball is added. The complete BallCourt environment is shown in Figure 12-32.

Figure 12-32

555

www.it-ebooks.info

Part III: Visual Programming Language

One last interesting thing to notice in the BallCourt service is the SetBounceThreshold method. This method waits for the physics engine to be initialized and then sets the BounceThreshold variable to0.25. This variable is new in the 1.5 Refresh release. The physics engine bounce threshold governs the minimum speed at which objects bounce off of each other. If an object is traveling slower than the bounce threshold, then it will not bounce. The default value is 2.0 meters per second but we would like our ball to travel more slowly than that and still bounce off the walls, so it is set to 0.25 meters per second. The value is negative because it represents the speed of one object with respect to another.

The BallFollower VPL Diagram

With the Simulation Environment in place, the VPL diagram used to analyze the video signal and control the robot is remarkably simple. It is shown in Figure 12-33.

Figure 12-33

The SimpleVision activity is used to analyze the video from the camera on the Corobot. The Data activity in the upper left of the diagram sends an initialization message to the SetObjectTrackingColor input of the SimpleVision activity. It sets the target color to red with a similarity measure of 0.9. Only pixels that are closer to red than that threshold will be counted by the SimpleVision service.

This service sends periodic notifications to indicate whether it is currently tracking an object. If the ObjectFound output is false, then a message is sent to the simulated Differential Drive service to stop the robot from moving. If the robot doesn’t see the ball, it waits where it is.

556

www.it-ebooks.info

Chapter 12: Visual Programming Examples

If the service indicates that the ball is visible, then the three Calculate activities determine values to drive the robot towards the ball but not too close. Figure 12-34 shows a graph of the equation used to calculate the Forward value.

Figure 12-34

The horizontal axis represents the area of the recognized object and the vertical axis represents the forward speed of the robot. You can see that when the object is very far away (its area approaches 0), the robot moves forward quickly. As the object gets closer and its area approaches 600 pixels, the robot’s forward velocity approaches 0. If the object is so close that its area is greater than 600 pixels, the If and Data activities clamp the Forward value at 0 so that the robot does not move backwards. The equations for the Left and Right values are shown in Figure 12-35.

Figure 12-35

In this graph, the horizontal axis represents the X coordinate of the center of the object in the camera frame, and the vertical axis represents the forward motion of each wheel. The line with a negative slope represents the right wheel and the other line represents the left wheel.

557

www.it-ebooks.info

Part III: Visual Programming Language

When the object is in the exact horizontal center of the frame, both wheels have a speed of 0 and the robot doesn’t turn. When the object is very near the left side of the frame, the left wheel turns backward and the right wheel turns forward, which turns the robot to the left, toward the object. Likewise, when the object is very near the right side of the frame, the wheels are driven so that the robot turns to the right to face the object.

When all three values have been calculated, a message is passed to the GenericDifferentialDrive activity with the results:

LeftWheelSpeed = value.Left + value.Forward

RightWheelSpeed = value.Right + value.Forward

The values are added together rather than multiplied so that the two different behaviors can act independently. If they were multiplied, the robot would not be able to turn to face the ball if the ball were larger in the frame than 600 pixels.

Run the diagram by pressing F5 and the ball court should appear along with the Corobot. After a few seconds, the ball should roll into the scene and the Corobot should notice it and accelerate toward it. As the ball rebounds off a wall, the robot will momentarily get too close to the ball and slow down or stop its forward motion. It should still turn to track the ball and it should again move forward when the ball is far enough away.

The rate of notifications coming from the SimpleVision service depends on the frame rate of the simulation engine. If you are running on a slower computer, the following behavior may not work correctly. You can slow down the ball to give the robot more time to react by changing the BallSpeed parameter in the ProMRDS\config\BallCourt.config.xml file to something smaller. You can also experiment with making the ball move faster by increasing this value.

Figure 12-36 shows the motion of the robot as it follows the ball around the court.

Figure 12-36

558

www.it-ebooks.info

Chapter 12: Visual Programming Examples

Summary

The examples in this chapter have shown you a variety of ways to use VPL to write orchestration services. In each case, one or more C# services were used to interface with the hardware (or the simulation equivalent) or to perform complicated processing such as in the case of the Simple Vision service. VPL was used to quickly and simply join these basic services together to define how the whole system works.

There is nothing that can be done in VPL that can’t also be done by writing a custom service but it is often easier and faster to write the behavior using a VPL diagram instead of a code editor.

That concludes the section of this book dealing with VPL. You should now have a good understanding of VPL’s capabilities and how it can help you to quickly and easily orchestrate services and prototype new algorithms.

559

www.it-ebooks.info

www.it-ebooks.info

Part IV

Robotics Hardware

Chapter 13: Using MRDS with Robotics Hardware

Chapter 14: Remotely Controlling a Mobile Robot

Chapter 15: Using a Robotic Arm

Chapter 16: Autonomous Robots

Chapter 17: Writing New Hardware Services

www.it-ebooks.info