Raúl
Moderator
 Moderator
| Posts: 434 |  | Karma: 6
|
Re:Crear una simulación - 2008/03/07 12:15
Hola. Sí, puedes fijarte en el código del servicio MazeSimulatorRA:
-> Servicio de Simulación de Laberinto
Para crear un mundo simulado desde cero, puedes crear un servicio nuevo como dices en tu mensaje, y luego tienes que declarar el motor de simulación física como partner de tu servicio. Por ejemplo:
| Code: | // Physics engine instance
PhysicsEngine _physicsEngine;
// Port used to communicate with simulation engine service directly, no cloning
SimulationEnginePort _simEnginePort;
// partner attribute will cause simulation engine service to start
[Partner("Engine",
Contract = engineproxy.Contract.Identifier,
CreationPolicy = PartnerCreationPolicy.UseExistingOrCreate)]
private engineproxy.SimulationEnginePort _engineServicePort = new engineproxy.SimulationEnginePort();
|
Luego en el método Start de tu servicio, inicializas las referencias al motor físico y al puerto del servicio de simulación.
| Code: | // Cache references to simulation/rendering and physics
_physicsEngine = PhysicsEngine.GlobalInstance;
_simEnginePort = SimulationEngine.GlobalInstancePort;
|
En este momento ya puedes empezar a añadir entidades y objetos a tu mundo simulado. Por ejemplo, para añadir el cielo harías esto:
| Code: | // Add a sky using a static texture. We will use the sky texture
// to do per pixel lighting on each simulation visual entity
SkyEntity sky = new SkyEntity("sky.dds", "sky_diff.dds");
_simEnginePort.Insert(sky);
|
Para añadir el suelo liso, puedes usar esta función:
| Code: | void AddGround()
{
HeightFieldShapeProperties hf = new HeightFieldShapeProperties("height field",
64, // number of rows
100, // distance in meters, between rows
64, // number of columns
100, // distance in meters, between columns
1, // scale factor to multiple height values
-1000); // vertical extent of the height field. Should be set to large negative values
// create array with height samples
hf.HeightSamples = new HeightFieldSample[hf.RowCount * hf.ColumnCount];
for (int i = 0; i < hf.RowCount * hf.ColumnCount; i++)
{
hf.HeightSamples<em> = new HeightFieldSample();
hf.HeightSamples[i].Height = (short)(Math.Sin(i * 0.01));
}
// create a material for the entire field. We could also specify material per sample.
hf.Material = new MaterialProperties("ground", 0.8f, 0.5f, 0.8f);
hf.TextureFileName = _state.GroundTexture;
// insert ground entity in simulation and specify a texture
_simEnginePort.Insert(new HeightFieldEntity(hf, _state.GroundTexture));
}
|
En general, puedes añadir objetos o entidades que ya están definidos en Robotics Studio o crear tus propias entidades y luego añadirlas. Para añadir un bloque como una pared, puedes hacer algo así:
| Code: | BoxShapeProperties cBoxShape = null;
SingleShapeEntity box = null;
Vector3 dimensions = new Vector3(x,y,z);
// Crear una entidad a partir de una forma (shape)
cBoxShape = new BoxShapeProperties(
100, // masa en kilos.
new Pose(), // posición relativa
dimensions); // dimensiones
// Material asignado a la forma
cBoxShape.Material = new MaterialProperties("gbox", 1.0f, 0.4f, 0.5f);
// Se crea la nueva entidad a partir de la forma
box = new SingleShapeEntity(new BoxShape(cBoxShape),
new Vector3(x,y,z)));
// Dar un nombre único a la entidad
box.State.Name = "Pared 1";
/* Añadir la entidad al simulador */
_simEnginePort.Insert(box);
|
En el espacio de nombres [i]Microsoft.Robotics.Simulation.Engine (que está en el archivo SimulatonEngine.dll) están definidas algunas entidades que puedes usar, como el robot Lego NXT Tribot, una cámara simulada, suelo tipo terreno irregular, etc.
Otra opción que tienes para crear un entorno virtual en el simulador, es arrancar el simulador y editar manualmente el entorno (puedes crear entidades en el modo edición).
Para arrancar un simulador básico puedes ejecutar: Inicio -> Progrmas -> Robotics Studio (1.5) -> Visual Simulation Environment -> Basic Simulation Environment
En la ventana del simulador vas a Mode -> Edit.
En el menú entity, puedes añadir y borrar entidades, además de editar sus atributos.
Entonces puedes guardar el mundo que has creado como un fichero XML (File -> Sace scene as). En realidad eso genera dos fichero XML, un manifest y un estado del simulador. Puedes editar el fichero de estado manualmente para cambiar algunas cosas. Luego puedes iniciar el simulador con el mundo que has creado usando el manifest generado, por ejemplo:
| Code: | "C:Microsoft Robotics Studio (1.5)binDssHost.exe" /p:50000 /t:50001 /m:"" Sim-07-Mar-08-11-08-40-AM.Manifest.xml""
|
Parece un poco lioso, pero si te pones a experimentar con el simulador, verás que no es para tanto.. En cualquier caso, si tienes problemas no dudes en volver a preguntar..
Post edited by: Raúl, at: 2008/03/07 13:50
Raúl Arrabales Moreno. conscious-robots.com/raul |