Creating a Simulation from a Service

In addition to creating a simulation scene using VSE, you can create a scene programmatically using Visual Studio. This chapter will not go into great detail about how to do this because the simulation tutorials included with MSRS cover this subject very well. Instead, the chapter will review the basics needed to create a simulation programmatically.

To do this, you will create a new DSS service using the template provided with MSRS. After you create the project, you will need to add references to the following assemblies:

  • PhysicsEngine. Contains methods and properties used to access the underlying AGEIA software physics engine.

  • RoboticsCommon. Contains the PhysicalModel namespace, which includes type definitions used to define the physical characteristics of robots. For example, this is where you will find definitions that represent sensors such as analog sensors, contact sensors, and Web cameras. You will also find type definitions for abstract services such as the articulated arm service, which is used to operate a simulated robotic arm.

  • SimulationCommon. Contains the type definitions used when working with both the simulation and physics engines.

  • SimulationEngine. Used to access the simulation engine and perform functions such as keep track of the state for each entity.

  • SimulationEngine.proxy. Proxy for the simulation engine that is used to load the engine as a partner. Proxy assemblies are generated for all compiled services, of which the simulation engine is one.

After you add the assembly references, you will also need to add namespace declarations for the associated references. For example, you need to add the following namespace declarations to the top of the implementation class file for your simulation:

using Microsoft.Robotics.Simulation.Physics;
using Microsoft.Robotics.PhysicalModel;
using Microsoft.Robotics.Simulation;
using Microsoft.Robotics.Simulation.Engine;
using engineproxy = Microsoft.Robotics.Simulation.Engine.Proxy;

MSRS includes other services (see Table 4-4) that you can use to implement specific functionality. For example, a simulation that includes a robot with bumpers and a differential drive system will need to include references to the SimulatedBumper and SimulatedDifferentialDrive services.

Tip

Tip

Conscious-Robots.com hosts a Web site specifically dedicated to working with MSRS. On this site, you can download a simulated sonar service (http://www.conscious-robots.com/en/robotics-studio/robotics-studio-services/simulated-sonar-service-2.html). This simulated sonar service, which is not included with MSRS, simulates the sonar array for a Pioneer DX3 robot. It uses the Raycast application programming interface (API) in a similar way as the simulated laser rangefinder does.

Table 4-4. Additional MSRS Services Used to Provide Specific Simulation Functionality

Service Name

Description

SimulatedBumper

Use to access an array of bumper sensors for a simulated robot. Readers can locate the source code for this service in the samples simulationSensorsBumper folder. NOTE: In the MSRS Control Panel, this service is listed as the Simulated Generic Contact Sensors service.

SimulatedDifferentialDrive

Use to drive a simulated robot with a differential drive system. This service uses a reference to the generic differential drive service. Readers can locate the source code for this service in the samples simulationDifferentialDrive folder.

SimulatedLBR3Arm

Use to control the joints for a simulated KUKA LBR3 articulated arm. Readers can locate the source code for this service in the samples SimulationArticulatedArmsLBR3 folder.

SimulatedLRF

Use to simulate a Sick laser rangefinder device, which is found on the Pioneer robot. This device emits an infrared beam in a 180-degree arc, making it highly accurate at detecting objects. This service uses a reference to the SickLRF service. Readers can locate the source code for this service in the samplessimulationSensorsLaserRangeFinder folder.

SimulatedWebCam

Use to access a Web camera on a simulated robot. Readers can locate the source code for this service in the samplessimulation SimulatedWebCam folder.

When setting references to the assemblies listed in Table 4-4, you will need to reference the associated proxy files. For example, the proxy reference used for the Simulated Differential Drive service is named SimulatedDifferentialDrive.2006.M06.Proxy.

Note

Note

The solution files included for the simulation tutorials contain the project files for referenced assemblies used by that tutorial. This was done because MSRS uses strong-name signing, and, if you rebuild one dependency, this could affect other projects that use that dependency. By doing a rebuild of the entire solution file, you should be able to avoid the problems that can occur with references not found.

Because the simulation engine is a service, you will need to add it as a partner service. You do this by using the Partner attribute, as shown in the following code:

[Partner("Engine",
    Contract = engineproxy.Contract.Identifier,
      CreationPolicy = PartnerCreationPolicy.UseExistingOrCreate)]
private engineproxy.SimulationEnginePort _engineServicePort =
      new engineproxy.SimulationEnginePort();

In this case, the simulation service was created using the UseExistingOrCreate attribute. This means that if the simulation engine is already running, then it should be used instead of trying to instantiate a new instance.

Creating New Entities

The steps required for creating a new entity depend on what type of entity it is. MSRS supports several entity types that can be used to create simulated robotics hardware. It also provides entity types for creating environment entities such as the sky and ground. Both simple and complex objects that represent obstacles can be built using the SimpleShape and MultipleShape entities.

Create Robot Entities

Robot entities are complex and will typically contain one or more child entities. The child entities may represent sensors used on the simulated robot. For example, the simulated version of the Pioneer 3DX robot will have child entities for the laser rangefinder device and bumper sensors. To create the simulated Pioneer entity, you will first need to create the parent entity, which in this case will be the motor base. This can be done using the Pioneer3DX class that is included with the simulation engine. This class inherits from the DifferentialDriveEntity, which is used to specify the different physical properties associated with a differential drive system. The following code can be used to instantiate a new entity that represents the Pioneer robot:

Pioneer3DX robotBaseEntity = new Pioneer3DX(new Vector3(0, 1, 0));

In the previous code snippet, a Vector3 structure was used to hold three coordinate points that represent the robot’s position in the simulation scene. MSRS provides vector structures that can be used to store values associated with each coordinate point. There is a Vector2 structure, which stores X and Y coordinate values. Vector3 stores X, Y, and Z coordinates. Finally, the Vector4 structure is used to store X, Y, Z, and W values. When creating the Pioneer robot entity, you will need to assign a unique name for the robot and specify the robot’s color using a Vector4 structure.

After you create the motor base entity, you can start the service that represents the Pioneers motor service. To do this, you will need to use the CreateService method to create the service as an entity partner. This tells the simulation service what other services should be bound to it at runtime. The following code can be used to accomplish this:

CreateService(
  drive.Contract.Identifier,
     Microsoft.Robotics.Simulation.Partners.CreateEntityPartner(
  "http://localhost/" + robotBaseEntity.State.Name)
);

At this point, the child entities, such as the laser rangefinder and bumper sensors, can be created. To do this, you will go through a similar process where you create a new LaserRangeFinderEntity object and use the CreateService method to specify the laser rangefinder service that will be bound to it. To see a step-by-step example of this, refer to the source code for Simulation Tutorial 5, which can be found in the samplesSimulationTutorialsTutorial5 folder.

Creating New Entity Types

MSRS provides implementation classes and entity types to support the Pioneer 3DX, iRobot Create, KUKA LBR3, and LEGO NXT robots. To simulate a new robot or sensor, you will first need to create a new entity type. This can be done by modifying the Entities class file, which is included with the MSRS installation and located in the samplessimulationentities folder.

Note

Note

If you want to modify the code for an existing entity type, you will need to copy and paste the code for that entity type and create a new entity type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.91.206