Задани на лабораторные работы. ПРК / Professional Microsoft Robotics Developer Studio
.pdf
www.it-ebooks.info
Chapter 5: The MRDS Visual Simulation Environment
You move yourself around in the simulator by pressing keys. It is easiest to navigate in the simulator with one hand on the keyboard and the other hand on the mouse. The following table shows the keys that move the eyepoint in the environment:
Key |
Action |
|
|
W |
Move forward |
S |
Move backward |
A |
Move to the left |
D |
Move to the right |
Q |
Move up |
E |
Move down |
|
|
Notice that there are six keys and they work in pairs. W and S move you forward and backward. A and D move you left and right. Q and E move you up and down.
Holding down the Shift key while pressing one of the motion keys multiplies that motion by 10 times. This is very useful when you are in a hurry to get from one place to another. You can adjust the speed of mouse and keyboard movements in the Graphics Settings dialog, which is described shortly in the section “Graphics Settings.”
The movements are always relative to the current eyepoint orientation. This enables you to combine key presses with mouse drags for more complicated motions. For example, you can press A to move to the left while simultaneously dragging the mouse pointer to the right to move in a circle around an object.
You can use the following function keys as shortcuts to commonly used functions:
Key |
Action |
|
|
F2 |
Change the render mode |
F3 |
Toggle the physics engine on and off |
F5 |
Toggle between edit mode and run mode |
F8 |
Change the active camera |
|
|
Practice moving around in the environment until it becomes natural to you. Move yourself below the ground (that’s right, there’s nothing down there). Go as high as you can and see how the level of detail on the ground gradually fades into the fog. Practice zooming up to objects and moving right through them like a ghost. In the section “Physics Menu,” you’ll learn how to change the eyepoint so that it won’t pass through objects.
233
www.it-ebooks.info
Part II: Simulations
The Status Bar
Don’t worry about getting lost in the simulator. If you ever wonder where you are, you can display the status bar (see Figure 5-4). Click View
Status Bar to make it visible.
Figure 5-4
The status bar provides the position of the current camera, as well as the point that it is looking at. Every point in the simulation environment is represented by three coordinates: X, Y, and Z. The Y coordinate represents altitude, while the X and Z coordinates represent directions parallel to the ground plane. The simulation environment uses a right-handed coordinate system. This means that when the X axis is pointing to the right and the Y axis is pointing upward, the Z axis is pointing out of the screen and the eyepoint is looking in the direction of the -Z axis (into the screen), as shown in Figure 5-5.
Y
(0,1,0)
X (0,0,0) 

(1,0,0)
Z
(0,0,1)
Figure 5-5
Left-handed vs. Right-Handed Coordinate Systems
Left-handed versus right-handed coordinate systems have been a source of confusion in computer graphics systems for years. Direct3D typically uses a left-handed coordinate system, whereas OpenGL and several major modeling systems use a right-handed system. The MRDS Visual Simulation Environment uses a right-handed system so that it is easier to export models from modeling programs.You won’t have to worry about this issue unless you need to use models built for a left-handed system, in which case you will need to convert them.
234
www.it-ebooks.info
Chapter 5: The MRDS Visual Simulation Environment
Also of interest on the status bar is the number of frames being processed per second and the number of seconds that have passed in simulation time since the simulator started.
You can click View
Look along to become more familiar with the directions of the axes in the environment. Each item in the submenu orients the camera so that it is looking along a particular axis without changing its location.
The Help Menu
As you might expect, the Help menu gives you a couple of options to find out more about the simulator. The Help Contents option in the menu brings up the documentation file for the simulator. The second option, About Microsoft Visual Simulation Environment, can be used to display the version number of the simulator and specific information about your video card (see Figure 5-6):
The manufacturer and model
The name and version of the display driver
The size and format of the desktop
The highest supported versions of the vertex and pixel shaders
If your card supports version 3.0 of the shaders (VS_3_0 and PS_3_0) or higher, then you can display more realistic scenes, which include specular highlights and shadows.
Figure 5-6
The Camera Menu
Finally, here is your chance to be in two places at the same time. You can set up as many cameras in the simulation environment as you like and then instantly and easily switch between them, using
the Camera menu. All cameras in the scene appear on the menu regardless of type. You can select the desired camera from the menu or you can press F8 to switch from one camera to the next. There are two types of behavior for cameras: real-time and non-real-time:
Real-time cameras: These render the environment from their viewpoint every frame. As you can imagine, multiple real-time cameras can increase the graphics load on a machine substantially. A real-time camera is appropriate when you have a need to retrieve images from that camera on
235
www.it-ebooks.info
Part II: Simulations
a regular basis. Real-time cameras have a horizontal and vertical resolution associated with them. When the scene is rendered for that camera, it is rendered at that resolution.
Non-real-time cameras: These are simply placeholders in the 3D environment. Only the selected camera is rendered each frame. As a result, these cameras don’t reduce graphics performance. They are useful when you want to define a viewpoint in the scene so that you can switch to it quickly. The Main Camera is a special instance of a non-real-time camera. There is always a Main Camera in the scene, and its resolution always matches the resolution of the display window. When the window changes, the Main Camera adjusts accordingly. Other non-real-time cameras have a set horizontal and vertical resolution. If the resolution of the display window doesn’t match the resolution of the camera, the rendered image is stretched to fit.
The Marbles environment has several non-real-time cameras set up. Use the F8 key or the Camera menu to switch between them.
The Rendering Menu
You’ve already seen how you can fly through the air faster than a speeding bullet in the simulator. You’ve seen how you can leap tall buildings in a single bound. (Okay, so we haven’t had any buildings yet. Stay with us.) Now we’ll give you X-ray vision.
The Rendering menu controls how the simulated world is displayed. Different rendering modes give you different information about how the world is put together. The following sections discuss the various modes.
Visual Rendering Mode
Shown in Figure 5-7, this is the default rendering mode, and you are already well acquainted with it.
In this mode, the world is rendered to look its best — with fully rendered 3D meshes and lighting effects. This is the mode that you will probably use most often.
Figure 5-7
236
www.it-ebooks.info
Chapter 5: The MRDS Visual Simulation Environment
Wireframe Rendering Mode
In this mode, shown in Figure 5-8, the world is still rendered with 3D meshes and lighting effects, but only the outline of each triangle is drawn. This quickly gives you a good idea whether the 3D meshes you are displaying have the proper resolution. Some meshes are so dense with polygons that they look about the same in wireframe mode as in the visual mode, which means they contain too many polygons and may be loading your graphics card more than necessary. Many modeling programs provide a way to reduce the number of polygons in a model.
Another benefit of wireframe rendering mode is that you can see through the objects in the scene. This is particularly helpful when you have just added a great new robot to your scene but it fails to appear. Switching over to wireframe mode shows that you accidentally embedded it in the couch.
Figure 5-8
Physics Rendering Mode
The physics rendering mode (see Figure 5-9) shows you the physics shapes that make up the scene. This is the view that shows the physics shapes that the physics engine works with. Complex objects can sometimes be represented by very simple physics shapes such as boxes or spheres. The physics rendering mode enables you to see the scene the same way the physics engine sees it. If you notice that objects aren’t colliding or moving the way you might expect, then switch to the physics rendering mode to see their underlying physics shapes.
It might be surprising but this mode can tax your graphics card more than the visual rendering mode even though less geometry is being drawn. This is because the shapes are composed of individual lines, which are drawn less efficiently than 3D meshes.
237
www.it-ebooks.info
Part II: Simulations
Figure 5-9
In Figure 5-9, you can see the three most common physics shapes: the sphere, the box, and the capsule. Spheres are defined by their radius. Boxes are defined by their dimensions along the X, Y, and Z axes. Capsules are cylinders with rounded ends, and they are defined by the radius of the cylinder and their height along the Y axis. Each of these shapes can also have a position offset and a rotation around each axis.
Notice in Figure 5-9 that each shape has coordinate axes drawn at its origin. You can deduce from the display that the origin of a sphere is its center. Although you can’t see the color in the book, on the screen the X axis is drawn in red, the Y axis is green, and the Z axis is blue.
The simulation environment is composed of objects called entities, which typically represent a single rigid body such as a robot chassis that may contain multiple shapes. Each entity contains an optional 3D mesh and zero or more physics shapes. The visual representation of the object and the physics representation exist side-by-side. Entities are always positioned and rotated as a single object.
Entities are the basic building blocks of simulator scenes. The properties of the entities provided with the simulator are described in this chapter. Additional details about the implementation of entities are provided in Chapter 6.
In Figure 5-9, each entity has a cube that represents its center of mass. On the screen, the cube is red if the entity is controlled manually (kinematic), and white if the entity is controlled by the physics simulator. Each of the marbles in the scene is controlled by the physics engine; but the ramp and the mill supports have a red center-of-mass box, indicating that they are controlled by the program and not by the physics engine. All the pegs that the balls fall through are contained by a single entity, and you can see that this entity is kinematic by its red center-of-mass cube.
238
www.it-ebooks.info
Chapter 5: The MRDS Visual Simulation Environment
Dynamic, Kinematic, and Static Entities
The physics rendering mode has three types of entities: dynamic, kinematic, and static. An entity is dynamic if it is under the control of the physics engine. The marbles in the marbles scene are a good example of a dynamic entity. Once they are created, the marbles service doesn’t do anything to control them. Their position and motion are determined only by the effect of gravity and the forces from collisions with other objects.
A kinematic entity, conversely, interacts with the physics environment but is positioned externally. The ramp is a good example of a kinematic entity. If it were a dynamic entity, it would fall to the ground because it has no supports. Because it is set to be kinematic, it stays where it is put. Other objects can collide with it but it won’t move unless its position is manually changed.
The physics engine also supports static entities, which are composed of shapes that have zero mass. These entities are intended to remain in the same position during the entire simulation. Static entities can collide with other entities, but they should not be moved because the physics engine assumes that they will never move. For example, if a stack of dynamic entities is on top of a static cube entity and you delete the static cube entity, the dynamic entities will continue to hover in mid-air until some other force affects them because the physics engine assumes that static objects will never move or disappear.
Combined Rendering Mode
This mode (see Figure 5-10) combines the visual mode with the physics mode. In some cases, it is easier to see which outlines go with which shapes in this mode. The outlines of the physics shapes are drawn as if the visual rendering were transparent so that you can still see physics shapes that are obscured by other entities. This rendering mode will typically tax your graphics card the most, and you may notice the frame rate drop.
Figure 5-10
239
www.it-ebooks.info
Part II: Simulations
No Rendering Mode
This is what your graphics scene looks like with the lens cover on the camera. At night. This mode is for those hardy souls who want to use the simulator completely for physics simulation and don’t want to waste any time rendering. Turn the status bar on and select this rendering mode to see how many frames are processed per second. You’ll learn how to use this mode a little later.
Graphics Settings
The last option on the Rendering menu takes you to the Graphics Setting dialog. These settings control how the graphics hardware is used to render the scene:
Exposure: This is much like the exposure on a camera. Increasing the exposure brightens the scene.
Anti-aliasing: This setting exposes the anti-aliasing modes supported by your graphics hardware. Selecting no anti-aliasing can make some cards run faster, but more anti-aliasing makes the edges of objects look much better. You have to experiment to find the best setting for your hardware.
Anti-aliasing
Aliasing in a graphics context means that the edge of an object must follow pixel outlines. This can give the edges of some objects a jagged appearance (known in graphics-speak as jaggies). Anti-aliasing algorithms are now supported on newer graphics hardware, and they enable edges to be rendered on subpixel boundaries, resulting in objects that look cleaner and more realistic.
Rotation and Translation Movement Scale: These numbers scale the movement for mouse and keyboard navigation. If you are one of those people who likes to turn your mouse movement up so high that you can zip across the screen with a millimeter of mouse movement, feel free to crank these values up.
Quality Level: Modern graphics hardware runs XNA shader programs to render objects. The simulator is equipped with version 3.0, 2.0, and 1.0 shaders. If you have newer and more
powerful hardware, you can run version 3.0 shader programs and you will be rewarded with a nicer-looking scene and shiny specular highlights. If you have older hardware that only supports version 2.0 shaders, you will still get a nice-looking scene but it will lack specular highlights and some fog and lighting effects. The Quality Level option enables you to select which version of the shaders is used. The version that your card supports is marked as “Recommended” but you can switch between versions without any problem. A 2.0 shader card on one machine was able to run the 3.0 shaders, albeit a little slowly. (If you try this and your graphics card melts into a pool of glowing slag, forget that it was ever suggested.) If you find that your frame rate is lower than 10–20 frames per second, then try lowering the quality level to see whether it helps.
240
www.it-ebooks.info
Chapter 5: The MRDS Visual Simulation Environment
Persistent Settings
Entities in the scene are considered simulator state, and they are persisted to an XML file when the scene is saved. Other settings, such as those on the Graphics Setting dialog, are persisted to a separate configuration file and restored each time the simulator is started.
This means that if you bump up the exposure by a notch or two, that same exposure will be used the next time you run the simulator.
Settings on the Graphics Settings dialog are automatically saved to config\ SimulationEditor.config.xml. The only other settings that are persisted in this file are the simulation time settings on the Physics menu.
The Physics Menu
Just as there is a Graphics menu to control the way things look, there is also a Physics menu to control how things behave. The Physics Settings dialog is shown in Figure 5-11. You display it by clicking Physics
Settings.
Figure 5-11
This dialog contains the following options:
Enable rigid body for default camera: This checkbox enables you to associate a physics sphere shape with the Main Camera. It is fine to observe what is going on in the simulation environment but sometimes you just have to poke or push something. If this option is checked, the camera can do just that.
You can try this out in the Marble environment by switching the render mode to physics mode. Enable the rigid body in the Physics Settings dialog and press OK. You should see a sphere appear just in front of the camera. You can verify that it works by maneuvering the camera down toward the turnstile, which is moved by the falling marbles. Position the camera on the opposite side of the turnstile from where the marbles are falling. Eventually you should be able to jam the mill and keep it from turning. Switch to the Mill View Camera and you should see a physics sphere shape blocking the mill from turning. This is the sphere shape associated with the camera.
241
www.it-ebooks.info
Part II: Simulations
Gravity: If you’re planning to simulate the Mars Rover or the robot arm on the space shuttle, you’ll want to use this setting to adjust gravity appropriately for your environment. The value is specified as a force along the Y axis, and it is negative because the Y axis is defined to go in the opposite direction as gravity. The gravity value is specified as meters per second, which works out to about –9.81 on Earth and –3.72 on Mars. If you’re just aching to do it, go ahead and set it to a positive value. The gravity value is persisted as a part of the scene. When you load a previously saved scene, it will have the same gravity as when it was saved.
Time Base Run-time Settings: These settings control how simulator time is related to real time. They are the only settings on this dialog that are persisted from session to session independent of the scene that is currently loaded. To simulate the movement or collision of a robot with improved accuracy, you can slow down simulator time relative to real time. This causes the physics engine to process more steps per second, which improves accuracy. You can select a real-time scale of 0.1 to slow down the action in the simulator by 10 times. In this mode, simulator time is still related to the actual time that passes. You can select a fixed time interval (in seconds) to completely separate simulator time from real time. This is useful when you want to run a detailed physics simulation over a period of time and you aren’t concerned with the graphics rendering during that time. You can choose a small fixed-time interval like 50 microseconds (0.00005) per frame and then set the rendering mode to No Rendering to increase the frame rate of the simulator to achieve a very accurate simulation. You can enable rendering periodically to see how the simulation is progressing.
What Happens in a Frame
The simulator divides time into discrete chunks called frames. At the start of each frame, the simulator calculates the simulator time for that frame, which may or may not be related to the real amount of time that has passed since the last frame. First, the simulator retrieves the previous frame results from the physics engine. Then the Update method is called for every entity in the scene. After all of the Update methods have completed, the scene is rendered once for each real-time camera in the scene. This is done by calling
the Draw method on every entity. Finally, the scene is rendered from the eyepoint of the Main Camera, and the physics engine begins processing the next frame.
You can turn off the physics engine by selecting the Enabled option on the Physics menu. If the physics engine is enabled, a check appears next to this menu option. When the physics engine is disabled, the simulator does not call the physics engine asking for scene updates. All dynamic entities stop moving but you can still move the camera around the scene. As far as the physics engine is concerned, no time passes, but the simulation time is still incremented on the status bar.
The physics rendering mode and the combined rendering mode are not supported while the physics engine is turned off. If either of these modes is selected when the physics engine is disabled, visual mode will be selected.
You can quickly toggle the physics engine between enabled and disabled by pressing F3. When the physics engine is disabled, the Physics Menu item on the Main menu bar is red.
242
