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

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

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

www.it-ebooks.info

Chapter 5: The MRDS Visual Simulation Environment

You can disable the physics engine to temporarily freeze the scene. It is also useful to disable the physics engine while moving objects around in the scene with the Simulation Editor, as described in the “Simulation Editor” section coming up shortly.

Saving and Loading Scenes

A simulation scene can be saved or serialized to an XML file and reloaded later. When the scene is reloaded, the entities will have the same position and velocity as they had when the scene was saved. Click File Save Scene As to save the current scene to a file, and File Open Scene to load a scene into the simulator.

When a scene is saved, two files are actually written. The first contains the scene state and the .xml is appended to the specified filename. The second contains a manifest for the scene and it has .manifest. xml appended to the specified filename. This file is described in the next section.

Loading a scene is called deserialization. In order for it to be successful, all of the types referenced in the XML scene file must be currently defined in the CLR environment. If a type is not defined, the simulator will not be able to load that entity into the scene and it will display a warning dialog containing a list of all the entities that could not be instantiated.

If you run the SimulationTutorial2 manifest (or select Visual Simulation Environment Multiple Simulated Robots scene from the Start menu), a table and two robots will be displayed. The table is defined in the SimulationTutorial2 service. If you save this scene to a file, run Simulation Tutorial 1 (Basic Simulation Environment on the Start menu), and then attempt to load the previously

saved scene, the simulator will complain that it is unable to instantiate the table because the service that defines the table is not loaded into the CLR. You can avoid this problem by running the SimulationTutorial2 service along with the SimulationTutorial1 service, adding code to SimulationTutorial1 to manually load the SimulationTutorial2 assembly, or by defining the table entity in a third DLL that is linked with both SimulationTutorial2 and SimulationTutorial1.

Saving and Loading Manifests

In a typical simulation scene, one or more services are associated with entities in the scene. Loading or saving a scene does nothing to start or stop services. For example, if you have a service that is driving your robot around at the time the scene is saved and then you reload the scene later, after the service has been terminated, that service will not be restarted.

The solution to this problem is the manifest. When a scene is saved, a manifest for the scene is also written to a file. This manifest contains service records for services that should be started. The manifest contains an entry for the SimulationEngine service, along with a state partner with the same filename as the scene file that was saved. In addition, it contains service records for other services that were associated with entities in the scene. Click File Open Manifest to load a manifest that in turn will load the associated scene file and start the associated services.

Other File Menu Items

You can save a snapshot of the current screen by clicking File Capture Image As. The screen image can be saved to a file using either the JPEG, BMP, PNG, or TIFF image format.

243

www.it-ebooks.info

Part II: Simulations

The Exit Simulator item on the File menu actually does more than just shut down the SimulationEngine service. It also posts a Drop message to the ControlPanel port, which causes the entire node to

shut down.

The Simulation Editor

The simulator has a built-in editor mode that makes it possible to modify or build a scene. Click Mode Edit to enable the simulation editor, or you can press F5 to toggle between Run mode and Edit mode.

When the editor starts, the physics engine is automatically disabled. The physics engine does not have to be disabled while the editor is running, but it is often much easier to work with entities in the scene if it is turned off.

While the editor is running, the display window is divided into three panes, as shown in Figure 5-12:

The main window renders the scene. When this window has input focus, it has a blue border around it and keystrokes affect the movement of the camera, just as in Run mode.

The upper-left pane, or the Entities pane, shows an alphabetical list of all the entities in the scene. Each entity has a checkbox next to it that selects it for various operations. If an entity has one or more child entities, a small box with a plus sign appears next to it. Clicking this box will show the child entities in the list.

The lower-left pane, or the Properties pane, shows all of the properties associated with the entity currently selected in the Entities pane.

Figure 5-12

244

www.it-ebooks.info

Chapter 5: The MRDS Visual Simulation Environment

Selecting Entities

Clicking the name of an entity highlights it and causes its properties to appear in the lower-left pane, the Properties pane. The Properties pane shows the name and type of the entity currently selected in the Entities pane. Below that, all of the properties of the selected entity are shown. Many of these properties can be modified.

When an entity is selected, it can be highlighted in the Display pane by pressing the Ctrl key. A highlighted circle is drawn around the entity that is not obscured by other entities (see Figure 5-13).

Highlight circle

Figure 5-13

Another way to select an entity is to click with the right mouse button over the entity in the Display pane. The simulator determines which entities are under the position of the mouse click and selects the closest one. The ground and sky entities cannot be selected in this way.

It is very easy to identify each entity in the scene using this method. Hold down the Ctrl key so that the selected entity is highlighted and then right-click on various entities in the scene. The name of the entity and all of its properties appear in the Properties pane.

The Entities pane maintains two different entity selections. The entity that is highlighted in the Entities pane is the one that is shown in the Properties pane, and it is also highlighted in the Display pane. Entities can also be selected by clicking the checkbox by the name. It is possible to select multiple entities using the checkboxes, although only the highlighted entity is shown in the Properties pane. Operations that affect multiple entities will operate on all entities that are checked.

245

www.it-ebooks.info

Part II: Simulations

Manipulating Selected Entities

When an entity is selected, a number of additional operations are enabled on the entity. For example, it is a common problem to lose track of an entity and to be unable to see it in the scene. Sometimes the entity can be quickly located by selecting it in the Entities pane and then pressing the Ctrl key to highlight it. If the highlight cannot be seen, the eyepoint can be moved to the entity by holding down the Ctrl key and pressing one of the arrow keys, as described in the following table:

Key

Eyepoint Position

 

 

Up Arrow

Above the entity ( Y)

Shift Up Arrow

Below the entity ( Y)

Left Arrow

To the right of the entity ( X)

Shift Left Arrow

To the left of the entity ( X)

Right Arrow

In front of the entity ( Z)

Shift Right Arrow

Behind the entity ( Z)

 

 

You can move an entity around in the 3D environment by selecting its position property in the Properties pane. You can enter three values separated by commas to represent the new X, Y, and Z coordinates of the entity, or you can expand the position property and modify each coordinate separately.

Sometimes it is easier to position an entity graphically. To do this, select the position property and then hold down the Ctrl key to highlight the selected entity. Now press the left mouse button in the Display pane and drag the mouse pointer. The entity will move to follow the pointer. If you only want to move the entity along the X axis, simply select the X coordinate of the expanded position property, and when you drag the mouse pointer, the entity will only move along the X axis. The behavior is similar for the Y and Z coordinates.

The screen is only two-dimensional, so it is not really possible to move an entity in all three dimensions using the mouse pointer. The axes of movement are perpendicular to the vector formed by subtracting the camera Location from the camera LookAt point. You can move an entity parallel to the ground plane by positioning the camera directly above the entity looking down by pressing the Ctrl key and the up arrow and then dragging the mouse pointer with the left button held down.

You can rotate an entity in a similar way by selecting the rotation property of that entity and then dragging the mouse pointer while holding the Ctrl key. Selecting a single coordinate of the rotation property will constrain the rotation to be only around that axis.

Moving and rotating entities while the physics engine is enabled can be difficult. Gravity and other constraints in the environment can prevent or limit the movement of entities. You can disable the physics engine by pressing F3 or by toggling the Enabled item on the Physics menu, as described in the previous section.

246

www.it-ebooks.info

Chapter 5: The MRDS Visual Simulation Environment

The Entities Menu

You may have noticed that when you put the simulator in Edit mode, the Entities menu appears on the main menu. The items on this menu operate on one or more entities and are only available in Edit mode. The following sections briefly discuss each item on the menu.

Saving and Loading Entities

You’ve already seen how to save and load an entire scene. It is also possible to save and load a subset of a scene. Entities can be saved to a file by clicking one or more checkboxes in the Entities pane and then clicking Entities Save Entities. A dialog appears, prompting for a filename, and the entities are serialized to that file. These entities can be loaded later by clicking Entities Load Entities.

Sometimes it is more convenient to cut or copy and then paste selected entities. This is also done using the Entities menu. You can select one or more entities using the checkboxes in the Entities pane and then select either Cut or Copy from the Entities menu. At this point, the entities are serialized to a file called SimEditor.Paste in the Store directory. If you select Cut, the entities are deleted from the scene. When you click Entities Paste, the entities in the SimEditor.Paste file are deserialized and inserted into the scene. The usual keyboard shortcuts (Ctrl X for cut, Ctrl C for copy, and Ctrl V for paste) can also be used.

Overlapping Objects

If it is true that nature abhors a vacuum, then nature must really hate it when more than one physical object is occupying the same space at the same time. The physics engine tries to correct this situation by applying a force to overlapping objects that will quickly separate them. This can be a source of fun in the simulated environment. Disable the physics engine on the Physics menu and then select a dynamic entity in a scene and press Ctrl to copy it to the paste buffer. If you are using the Marbles scene, select one of the marbles. Now press Ctrl multiple times to paste that entity back into the same position repeatedly. Next, enable the physics engine by pressing F3 and watch the fireworks. The more entities that overlap, the more energetic is the explosion that separates them. Keep this behavior in mind if you see an entity behaving erratically or moving quickly just after it is created. This is usually a good indication that one or more of the physics shapes in the entity are overlapping and the physics engine is trying to separate them.

Creating New Entities

You can click Entities New to create new entities in your scene using the New Entity dialog. You must first select the assembly that defines the entity you want to create. The <Executing assembly> selection will show all entities currently defined by all assemblies currently loaded. The second drop-down list shows all the entities that are defined in the selected assembly. In order for an entity to be displayed, it must inherit from VisualEntity. Once an entity has been selected, it must be given a unique name in the Name textbox. If the entity is to be created as a child of another entity, then the parent entity must be selected in the last drop-down list.

247

www.it-ebooks.info

Part II: Simulations

Press OK when all of the selections are correct. The simulator will then display a dialog with properties for each parameter in the constructor of the new entity. When there are several constructors for an entity, the constructor defined immediately after the default constructor is selected. Press OK when all of the constructor parameters have been defined, and the entity will be created.

Occasionally, you must set some of the entity properties before you can successfully create the entity. For example, a SingleShapeEntity requires a shape to be defined but it cannot be specified in the constructor that is presented. The entity will appear in the Entities pane but it will have a red exclamation point next to it, indicating that there was an error in its creation. Look at the InitError property of the entity to see whether it provides some information about why the entity could not be initialized. In the case of the SingleShapeEntity, clicking one of the shape properties and defining a valid shape will clear the error, enabling the entity to be initialized.

Child Entities

The simulator supports the idea of child entities, which are entities that are defined relative to a parent entity. If you add a SingleShapeEntity as a child entity of another SingleShapeEntity, the child will be attached to the parent with a fixed joint that effectively glues the two pieces together. The physics engine treats the pair as if they were one entity.

When the simulator is in Edit mode, you can move and rotate the parent entity and the child entity moves with it. When you move or rotate the child entity, it changes the attachment of the child to the parent to accommodate the new position or rotation. This is how you can change the way in which the child is attached to the parent.

You can make one entity a child of another using the following steps:

1.

2.

3.

Select the entity you want to make a child and press Ctrl to cut it from the scene.

Select the entity you want to be the parent.

Click Entity Paste as Child. The cut entity will be added to the scene as a child of the selected entity.

You can also make an entity a child when you create it using the New Entity dialog described earlier by selecting the parent entity in the appropriate drop-down list.

Modifying Entity State

Every entity in the simulation environment shares a common entity state. The entity state contains properties that affect the visual appearance of the entity as well as its physical behavior. Select an entity in the Entities pane. Its state is the property called EntityState, which has a value equal to the name of the entity. Click the value and an ellipsis (...) appears. When you click that ellipsis, a dialog box appears, enabling you to change the values of the entity state.

If you modify one or more entity state values and then select OK, the entity is removed from the scene and then reinitialized and inserted into the scene with the new values. This operation is similar to cutting the entity and then pasting it back into the scene.

The following sections discuss each part of the entity state.

248

www.it-ebooks.info

Chapter 5: The MRDS Visual Simulation Environment

Graphics Assets

Graphics assets determine how the entity looks. You can specify a new DefaultTexture, Effect, or Mesh to change the appearance of an entity:

DefaultTexture: If no mesh is specified for the entity, the mesh is generated from the physics shapes. In this case, the DefaultTexture specifies which texture map to apply to the generated meshes. The texture map should be a 2D texture and it can be any of the supported texture formats (.dds, .bmp, .png, .jpg, .tif).

Effect: This is the shader program that should be used to render the entity. In most cases, you will just want to specify the default effect: SimpleVisualizer.fx. Advanced users who want to create a special look for an entity can write their own shader program and specify it here.

Mesh: If a mesh file is specified here, it is rendered for the entire entity. Only a single mesh file is supported per entity. As mentioned above, if no mesh is specified, then a default mesh is constructed from the physics shapes that make up the entity. Mesh files must be in .obj or

.bos format.

The simulator reads .obj meshes. The .obj format (also called the Alias Wavefront format) is a popular 3D format that many modeling programs are able to export. These files usually have one or more material files with an extension of .mtl that specify the rendering characteristics of the objects. The format of .obj and .mtl files is ASCII, so they are easy to modify but less efficient to read, and large meshes can take some time to load. When an .obj file is read, the simulator automatically generates an optimized form of that file and writes it to disk with the same filename but with a .bos extension. The next time the .obj file is loaded, the simulator loads the .bos file instead, which is faster. If the obj file is modified, the simulator will not read the obsolete .bos file but will instead generate a new one. The MRDS SDK includes a command-line tool called Obj2Bos.exe, which can convert .obj files to .bos files without having to run the simulator. Microsoft has not made the .bos format public, so no tools outside of the simulator can read this format.

Misc

The following entity state properties appear under the Misc heading:

Flags: These bitwise flags define some physics behavior characteristics for the entity. If no flags are defined, then the default value for this field is Dynamic, meaning the entity behaves as a normal dynamic entity, which is completely controlled by the physics engine. The other flags are as follows:

Kinematic: If this flag is set, the entity is kinematic, meaning its position and orientation are controlled by a program external to the physics engine. The entity is not affected by gravity and it is not moved due to collisions with other objects, but it may influence the movement of other objects due to collisions.

IgnoreGravity: This one is self-explanatory. Gravity will have no effect on the entity if this flag is set.

DisableRotationX, DisableRotationY, DisableRotationZ: These flags prevent the entity from rotating around each of the specified axes. They are only relevant if the entity is not kinematic.

249

www.it-ebooks.info

Part II: Simulations

Name: This is the name of the entity. Each entity must have a unique name. Simulation services use the name of the entity to identify it, so it is important that no two entities have the same name. You can modify the name of an entity by changing it here, but any simulation services that previously connected to the entity will no longer work properly.

Velocity: This vector holds the magnitude and direction of the velocity of the entity. You can specify a new velocity in the state, and the entity will be updated with this new velocity. This state property is updated each frame, much like the Position and Rotation properties.

Physical Properties

These properties in the entity state define physical characteristics of the entity:

AngularDamping/LinearDamping: These damping coefficients are numbers that range from zero to infinity. They provide a way to model real-world effects such as air friction, which tend to slow down the linear and angular motion of a body. The higher the number, the more the linear or angular motion slows. For example, entities with the default value of zero (0) for damping will continue to roll along the ground until some other force stops them.

Mass/Density: These properties are really two ways of specifying the same thing. If the mass is nonzero for one or more of the physics shapes in an entity, the physics engine will calculate the total mass and density of the entity from the sum of the mass of the shapes. In this case, these two values are ignored when the entity is initialized. If the sum of the masses of the shapes is zero, then the physics engine uses one of these values to determine the mass of the entity. The Mass value provides a shortcut for specifying the total mass of an entity. The physics engine calculates the center of mass assuming that the density of the shapes in the entity is uniform. The Density value provides another shortcut to specify the overall mass. If Density is specified, the physics engine calculates the total mass based on the volume of the shapes in the entity. Again, the engine assumes that the density is uniform throughout the entity.

InertiaTensor: This property is a way to specify the mass distribution of the entity. The inertia tensor expresses how hard it is to rotate the shape in various directions. Shapes such as cylinders are naturally easier to rotate around their longitudinal axis but more difficult to rotate around the other two axes due to their mass distribution. If this vector is not specified, the physics engine will calculate it based on the position and relative size of the shapes in the entity.

Other Common Entity Properties

The following properties are common to all entities. Just like the other entity state, they can be edited in the Properties pane of the Simulation Editor.

ServiceContract: This property is an optional URI for a service that controls or interacts with this entity. When the scene is saved and a manifest is written, the simulator will add this service to the list of services that run and specify the name of this entity as a partner.

InitError: If an entity fails to initialize, it is displayed in the Entities pane with a red exclamation point. When this happens, the error displays in the InitError property.

Flags: These are miscellaneous bitwise flags that apply to the entire entity:

UsesAlphaBlending: Set this flag if your entity is partly transparent. The simulator renders transparent entities last.

250

www.it-ebooks.info

Chapter 5: The MRDS Visual Simulation Environment

DisableRendering: Set this flag to skip rendering of this entity. This is very useful for entities that are large and cover up other entities. Even though the entity is not drawn, it still is active in the scene.

InitializedWithState: This flag is supposed to indicate that the entity was deserialized from a file, rather than built with a nondefault constructor. The simulator does not appear to set or pay attention to this flag currently.

DoCompletePhysicsShapeUpdate: This flag specifies that the physics engine should update the pose information for each physics shape in the entity each frame. By default, the shape state is not updated each frame.

Ground: This flag indicates that the entity is the ground. This flag is set by some entities but the simulator does not currently do anything with it.

ParentJoint: This is the joint that connects a child entity to its parent. It is discussed in more detail in Chapter 8 when we get to articulated entities.

Position and Rotation: These properties were previously discussed in the section “Misc” earlier in the chapter. Together they specify the position and orientation of the entity in the environment.

MeshScale/MeshTranslation/MeshRotation: These properties make it easier to fit a 3D mesh to an entity. Because different modeling tools have different defaults for axis orientation and scale, it is sometimes difficult to export a model that exactly matches an entity in the simulation environment. Perhaps the mesh is rotated by 90 degrees or perhaps the modeling package uses centimeters rather than meters. These properties make it easy to scale, move, and rotate the mesh so that it fits the physics shapes that define the entity.

Meshes: This property shows information about the meshes that are loaded for the entity. These meshes may have been constructed from the physics shapes that make up the entity, or they may have been loaded from a .obj file. There is not much you can change about the meshes, but you can change the materials that go with the meshes. This is a great way to tune the appearance of entities within the environment. You can make them darker or lighter, or you can change how shiny they look. The color of an object in the simulator is a combination of ambient, diffuse, and specular light. The simulator uses a high–dynamic range lighting model, so it is fine if these values sum to a color greater than (1,1,1). At the end of all the lighting calculations, the color of the object is clamped to the range that the graphics hardware can display.

It is easy to tune the way objects look in the environment by modifying the following material properties. The material properties are part of the entity state, but they are usually set when a mesh is loaded because they describe the appearance of the mesh. For example, a .obj mesh file usually has an associated .mtl material file that defines the material properties for each mesh. If you change the material properties using the Simulation Editor and you want those changes to persist between sessions, then you must tell the simulator to save the material changes back to the source files. To do so, click File Save Material Changes.

Ambient: This color value determines how much light is reflected from the object due to ambient light, which is light that is scattered throughout the environment from other objects (see Figure 5-14a). Typical values for the color components of the ambient color range from 0 to 0.2.

Diffuse: This color value determines how much light is reflected from an object due to being lit by a diffuse light (see Figure 5-14b). This value represents the maximum amount of light that will be reflected for the part of an object that faces directly toward the light. Parts of the object

251

www.it-ebooks.info

Part II: Simulations

that are more edge-on to the light are lit less by that light, and parts of the object that are perpendicular to the light are not lit at all. Diffuse lighting is what gives a 3D look to shapes in the environment because the amount of light that is reflected depends on the orientation of the shape with respect to the light. Values for diffuse color typically range between 0.4 and 0.8.

Specular: This color value determines how much light is reflected from an object in a specular highlight (see Figures 5-14c and d), which is basically a hack to make an object look shiny, as if it were reflecting the actual shape and color of the light. The specular highlight color is typically the same color as the light, whereas the diffuse color is typically the color of the object.

Power: This value determines the “tightness” of the specular highlight. The higher the power, the smaller the highlight. Figure 5-14c shows a specular highlight power of 2, whereas Figure 5-14d shows a specular highlight power of 128. Typical values range from 32 to 256.

A

 

B

 

C

 

D

Figure 5-14

Built-in Simulation Entities

The simulator exists to provide a way for you to create your own robots and environments. The Simulation Editor discussed in the previous section enables you to compose and modify environments using the built-in entities that are provided with the simulator. The following sections go into more detail about these entities. The next chapter describes how you can define your own custom entities.

In the following sections, the built-in entities have been divided into four categories:

Sky and ground entities

Light entities

General-purpose entities

Robot and sensor entities

Only wheeled robot entities are discussed in this chapter. Jointed entities, such as arms and walkers, are discussed in Chapter 8.

All of the 3D meshes and textures used by these entities can be found in Store Media. When you add your own media files, you should place them there as well.

252