none
Reseting the configuration of the environment

    Pregunta

  • Let's say I have an xml configuration that represent the 3D environment. I have loaded it to the simulation engine's initial configuration using manifest editor. When I am running my code, the initial configuration is loaded and everything is working fine.

    Now my question is:

    After my robot moves and perhaps moving some objects in the environment, after a certain time I would like my 3D environment to be reset to its initial setting. Is it possible to do that in the code instead of stopping the simulation and running the code again?

     

    Also is there anyway to load the xml config file into the simulation engine using the code instead of manifest editor?

     

    Thank you in advance

    miércoles, 02 de marzo de 2011 13:08

Respuestas

  • Thanks Vincent, I looked at the Simulated Depth Camera  and modified it. I am able to reproduce the ray-casting after I delete the depth cam and insert it again to the simulation engine now. I sometimes get the access violation to protected memory error but it is only in the case that the robot is moving around. I will see if I can re-run my simulation for 10 consecutive times by resetting my simulation config. Thanks for your help.
    miércoles, 07 de marzo de 2012 21:15

Todas las respuestas

  • you can use file->open scene to load your xml. i think it is fast enough.
    viernes, 04 de marzo de 2011 14:47
  • I think the question was about loading an XML state file from code, rather than via the menus in the simulator.

    The short answer is "no" you can't do this. However, if you have a set of entities that you can create programmatically then you can change them or delete them and re-create them. It really depends on what it is that you need to reset.

    Trevor

     

    domingo, 06 de marzo de 2011 8:27
    Propietario
  • Thanks Trevor!
    jueves, 31 de marzo de 2011 20:48
  • I have written some code that reset entities according the XML file

    [ServiceHandler(ServiceHandlerBehavior.Exclusive)]
        public IEnumerator<ITask> SimulationResetHandler(SimulationReset reset)
        {
          Microsoft.Robotics.Simulation.Proxy.SimulationState simstate = null;
    
          Fault error = null;
    
          // Read brick file
          mount.MountServiceOperations mountService = ServiceForwarder<mount.MountServiceOperations>(SimulationState_xml);
          DsspDefaultGet get = new DsspDefaultGet();
          mountService.Post(get);
    
          yield return Arbiter.Choice(get.ResponsePort, (fault) => error = fault,
            (success) => simstate = (Microsoft.Robotics.Simulation.Proxy.SimulationState)success);
    
    
          if (error != null)
          {
            LogError(error);
            reset.ResponsePort.Post(error);
            yield break;
          }
    
          // Pause
          simstate.Pause = true;
          yield return Arbiter.Choice(_simEngine.Replace(simstate), (success) => { }, LogError);
    
          simengine.VisualEntity[] entities = new simengine.VisualEntity[simstate.SerializedEntities.XmlNodes.Count];
          int i = 0;
    
          foreach (XmlElement element in simstate.SerializedEntities.XmlNodes)
          {
            ser.Deserialize request = new ser.Deserialize(element.OuterXml);
            SerializerPort.Post(request);
    
            simengine.VisualEntity entity = null;
    
            yield return Arbiter.Choice(
              request.ResultPort,
              (success) => entity = (simengine.VisualEntity) DssTypeHelper.TransformFromProxy (success.Instance),
              LogError);
    
            entities[i] = entity;
            ++i;
          }
    
          for (i = 0; i < entities.Length; ++i)
          {
            simengine.VisualEntity entity = entities[i];
    
            if (entity is simengine.SkyDomeEntity || entity is simengine.LightSourceEntity)
            {
              continue;
            }
    
            simengine.DeleteSimulationEntity delete = new simengine.DeleteSimulationEntity (entity);
            simengine.SimulationEngine.GlobalInstancePort.Post(delete);
    
            yield return Arbiter.Choice(
              delete.ResponsePort,
              (success) => { }, LogError);
    
            for (int j = 0; j < entity.ChildCount; ++j)
            {
              entity.InsertEntity(entities[i + 1 + j]);
            }
    
            i += entity.ChildCount;
    
            simengine.InsertSimulationEntity insert = new simengine.InsertSimulationEntity(entity);
            simengine.SimulationEngine.GlobalInstancePort.Post(insert);
    
            yield return Arbiter.Choice(
              insert.ResponsePort,
              (success) => { }, LogError);
          }
         
          // Unpause
          simstate.Pause = false;
          yield return Arbiter.Choice(_simEngine.Replace(simstate), (success) => { }, LogError);
    
          LogInfo("Simulation reset");
          reset.ResponsePort.Post(DefaultSubmitResponseType.Instance);
           yield break;
        }
    

     

    Hope it could help (at least it's a good start)

    Regards,

    Vincent

     


    http://www.simplysim.net/
    viernes, 01 de abril de 2011 17:44
  • Hi,I have a question.  IThe simulationState_xml file that you feed to ServiceForwrder is the same as your simulation configuration file right? The file that has all the specification of your entities, and is placed under the mount point?

    Because When i do that i get the following error: 

    DsspForwarder:CreateRequestForwarder: Failed resolving: dssp.tcp://anousha-pc:50001/icreatesimulation/boyd2withoutkinect.config.xml [01/19/2012 21:39:02][]

    *** Fault: [Message = ] [Reason = unspecified] [Fault = http://www.w3.org/2003/05/soap-envelope:Sender -> http://schemas.microsoft.com/xw/2004/10/dssp.html:UnknownEntry]  [01/19/2012 21:39:02][http://anousha-pc:50000/icreatesimulation/aa1f149b-7b47-4b6b-84cd-4a39d76b4f91]

     

    It says failed to resolve.... Does that mean I am feeding a wrong xml file, or does that mean it cannot find the file in my system? I placed my file in the mount point. The root of Microsoft robotics folder. And the file is the configuration file that I set for Simulation Engine in the manifest editor.

     

    jueves, 19 de enero de 2012 21:48
  • I find that the easiest way to specify config files (like the saved simulation state) is to put them in the same folder as the manifest. Don't specify any path in the manifest for the config file, just its filename. When the manifest is loaded any config files that are in the same folder will be found automatically and you don't have to worry about the mount point.

    Trevor

     

    lunes, 23 de enero de 2012 1:59
    Propietario
  • Thank you Vincent for your code and Thank you Trevor for your help. I found out the config file i was feeding to serviceforwarder was not a proper uri. :)

    I have some questions regarding your code Vincent.

    Well my simulation configuration includes iCreateRobot with some children entities one of which is KinectEntity that itself has 2 child entities, depth camera and simulated webcam.

    I modified your code so that it also insert the nested children entities.

    When I delete the iCreateRobot from the simulation I get an exception regarding the depth camera:

    *** "TaskExecutionWorker:HandleException": Exception:System.NullReferenceException: Object reference not set to an instance of an object.

       at Microsoft.Robotics.Services.Simulation.Sensors.DepthCamera.SimulatedDepthCameraService.<RaycastResultsHandler>d__6.MoveNext()
       at Microsoft.Ccr.Core.TaskExecutionWorker.MoveIterator(ITask& currentTask, IteratorContext iteratorContext, Handler& finalizer)
       at Microsoft.Ccr.Core.TaskExecutionWorker.ExecuteTask(ITask& currentTask, DispatcherQueue p, Boolean bypassExecute)
       at Microsoft.Ccr.Core.TaskExecutionWorker.ExecutionLoop()

    I thought it might be because I have handlers connected to my depth camera but again reseting the simulation is an exclusive handler. I commented out my depthcam subscription and I still got the error. So i tried this. instead of deleting my robot from the simulation i tried to update its pose. So I still read the entity and added the children but again after updating its pose, i got the error again. No matter if delete or update I get the exception. but the exception doesn't crash my code it just shows up in the output window. When I get the exception and I start my robot to move around my depth cam doesnt works and my webcam doesnt works properly. They are btoh there in my simulation when I go to edit simulation. I also found out that  sometimes I get "Trying to access protected memory" run time error, which was weird.  I read some of the threads including http://social.msdn.microsoft.com/Forums/en/roboticssimulation/thread/16f517c3-4308-4be5-826a-e382aad9c522 which is the exact same error i get sometimes if I reset my simulation few times, and I did as Traver mentioned in the forum. but still get the error. 

    Do you have any suggestion to why I get the exception error? Any help on accessing the proteced memory would be appreciated too, here is some part of my code:

    if (entity.EntityState.Name.Equals("jCreate"))
                    {
                        DeleteSimulationEntity delete = new DeleteSimulationEntity (entity);
                        SimulationEngine.GlobalInstancePort.Post(delete);
    
                        yield return Arbiter.Choice(
                          delete.ResponsePort,
                          (success) => { }, LogError);
    
                       
                        int j, k = 0;
                        for (j = 0; j < entity.ChildCount; j++)
                        {
                            VisualEntity child = entities[j + i + 1];
    
                            entities[i].InsertEntityGlobal(child);
                           
                            if (child.ChildCount != 0)
                            {
                                for (k = 0; k < child.ChildCount; k++)
                                {
                                    VisualEntity nested = entities[j + k + i + 2];
                                    child.InsertEntity(nested);
                                    
                                }
    
                            }
    
    
                        InsertSimulationEntity insert = new InsertSimulationEntity(entity);
                        SimulationEngine.GlobalInstancePort.Post(insert);
    
                        yield return Arbiter.Choice(
                          insert.ResponsePort,
                          (success) => { Debug.WriteLine("insert was successful"); }, LogError);
      
                        i = i + j + k;
                    }




    martes, 14 de febrero de 2012 21:51
  • I think there is an issue with the SimulatedDepthCameraService. Maybe it's doesn't support the delete/insert or doesn't execute the raycasting in the right context (like for example doing a raycasting event if the simulation is paused).

    I don't know if the sources for this service are provided (I haven't a MSRDS installed right now), but if they are, you should take a look how the service is implemented and maybe implements your own.


    http://www.simplysim.net/

    lunes, 27 de febrero de 2012 10:39
  • Thanks Vincent, I looked at the Simulated Depth Camera  and modified it. I am able to reproduce the ray-casting after I delete the depth cam and insert it again to the simulation engine now. I sometimes get the access violation to protected memory error but it is only in the case that the robot is moving around. I will see if I can re-run my simulation for 10 consecutive times by resetting my simulation config. Thanks for your help.
    miércoles, 07 de marzo de 2012 21:15
  • Hello everybody.

    First, thank you all for such a useful thread. 

    Second, I've got the same AccessViolationException :)

    I am trying to cobble up some autotesting framework. It should be able to reset environment and to restart tests. Furthermore, as all of the robotics is more or less probabilistic, the tests should be run more than once.

    I was tracing the bug for the whole weekend. It is 100% reproducible. So lets fix it at last. Here is my distilled code:

    using EngPxy = Microsoft.Robotics.Simulation.Engine.Proxy;
    using SimPxy = Microsoft.Robotics.Simulation.Proxy;
    using Microsoft.Dss.ServiceModel.DsspServiceBase;
    using Microsoft.Ccr.Core;
    using System;
    using System.Linq;
    using Microsoft.Dss.Core.Attributes;
    using System.ComponentModel;
    using Microsoft.Dss.ServiceModel.Dssp;
    using System.Collections.Generic;
    using System.Xml;
    using Microsoft.Robotics.Simulation.Engine;
    using Microsoft.Robotics.Simulation.Physics;
    using Microsoft.Robotics.PhysicalModel;
    using Microsoft.Dss.Services.Serializer;
    
    namespace Brumba.Simulation.AccessViolation
    {
    	[Contract(Contract.Identifier)]
    	[DisplayName("SimulationTester")]
    	[Description("SimulationTester service (no description provided)")]
    	class AccessViolationService : DsspServiceBase
    	{
            public static ITask Exec<T1, T2>(PortSet<T1, T2> portSet)
            {
                return Arbiter.Choice(portSet, (T1 p1) => { }, (T2 p2) => { Console.WriteLine("error"); });
            }
    
    		[ServiceState]
            AccessViolationState _state = new AccessViolationState();
    		
    		[ServicePort("/SimulationTester", AllowMultipleInstances = true)]
            AccessViolationOperations _mainPort = new AccessViolationOperations();
    
            [Partner("SimEngine", Contract = Microsoft.Robotics.Simulation.Engine.Proxy.Contract.Identifier, CreationPolicy = PartnerCreationPolicy.CreateAlways)]
            EngPxy.SimulationEnginePort _simEngine = new EngPxy.SimulationEnginePort();
    
            public AccessViolationService(DsspServiceCreationPort creationPort)
    			: base(creationPort)
    		{
    		}
    		
    		protected override void Start()
    		{
    			base.Start();
    
                SpawnIterator(Test1);
    		}
    
            private IEnumerator<ITask> Test1()
            {
                yield return Arbiter.FromIteratorHandler(SetUpSimulator);
    
                GenerateEnvironment();
    
                for (int i = 0; i < 100; ++i)
                {
                    yield return Arbiter.FromIteratorHandler(ResetBoxes);
                    yield return Arbiter.Receive<DateTime>(false, TimeoutPort(500), (DateTime val) => { });
                    Console.WriteLine("{0}", i);
                }
            }
    
            IEnumerator<ITask> SetUpSimulator()
            {
                yield return Exec(_simEngine.UpdatePhysicsTimeStep(0.01f));
    
                SimPxy.SimulationState simState = null;
                yield return Arbiter.Choice(_simEngine.Get(), s => simState = s, LogError);
    
                simState.RenderMode = SimPxy.RenderMode.None;
                yield return Exec(_simEngine.Replace(simState));
            }
    
            private IEnumerator<ITask> ResetBoxes()
            {
                SimPxy.SimulationState simState = null;
                yield return Arbiter.Choice(_simEngine.Get(), st => simState = st, LogError);
                simState.Pause = true;
                yield return Exec(_simEngine.Replace(simState));
    
                ///Remove old boxes {
                foreach (var entityNode in simState.SerializedEntities.XmlNodes.Cast<XmlElement>())
                {
                    var desRequest = new Deserialize(new XmlNodeReader(entityNode));
                    SerializerPort.Post(desRequest);
                    DeserializeResult desEntity = null;
                    yield return Arbiter.Choice(desRequest.ResultPort, v => desEntity = v, LogError);
    
                    EngPxy.VisualEntity entity = (EngPxy.VisualEntity)desEntity.Instance;
    
                    if (entity is EngPxy.SingleShapeEntity)
                    {
                        yield return Exec(_simEngine.DeleteSimulationEntity(entity));
                        break;
                    }
                }
                ///} Remove old boxes
    
                CreateJointBoxes();
    
                simState.Pause = false;
                yield return Exec(_simEngine.Replace(simState));
            }
    
            public void CreateJointBoxes()
            {
                var box1 = new SingleShapeEntity(new BoxShape(_boxProps), new Vector3())
                            {
                                State = { Name = "box1", Pose = { Position = new Vector3(0, 2f, 0), Orientation = new Quaternion(0, 0, 1f, 0.25f) } }
                            };
    
                box1.InsertEntity(new SingleShapeEntity(new BoxShape(_boxProps), new Vector3())
                            {
                                State = { Name = "box2", Pose = { Position = new Vector3(0.3f, 0, 0) } }
                            });
                SimulationEngine.GlobalInstancePort.Insert(box1);
            }
    
            private void GenerateEnvironment()
            {
                var ground = new HeightFieldEntity("Ground", "WoodFloor.dds", new MaterialProperties("ground", 0f, 0.5f, 0.5f));
                SimulationEngine.GlobalInstancePort.Insert(ground);
    
                SkyDomeEntity sky = new SkyDomeEntity("skydome.dds", "sky_diff.dds");
                SimulationEngine.GlobalInstancePort.Insert(sky);
    
                var sun = new LightSourceEntity(LightSourceEntityType.Directional)
                {
                    State = { Name = "Sun" },
                    Direction = new Vector3(0.5f, -.75f, 0.5f)
                };
                SimulationEngine.GlobalInstancePort.Insert(sun);
            }
    
            static readonly BoxShapeProperties _boxProps = new BoxShapeProperties("box1 props", 1, new Pose(), new Vector3(0.1f, 0.1f, 0.1f))
                                {
                                    Material = new MaterialProperties("ChassisMaterial", 0.0f, 0.5f, 0.5f),
                                    DiffuseColor = new Vector4(1, 0, 0, 0)
                                };
    	}
    }

    The AccessViolationService has Simulation Engine as a partner. It spawns Test1 method during Start. Test1 turns off rendering (that does not influence the bug, it occurs with or without rendering), decreases simulation step (that's kind of catalyst for the bug). Then generates fixed environment (the floor, the sky and the sun) and begin in the loop to execute test and reset it's movable parts. 

    The gist of the test - throwing two joint boxes on the floor from the height of 2 meters. The whole bug is about colliding items, when nothing collides all is ok. The child item is the necessary condition for the bug. In ResetBoxes I pause the simulator, delete old boxes (I assume that parent box goes first), recreate boxes, unpause the simulator - it is really Vincent's code.

    The loop consists of 100 iteration. It never goes beyond 20 (the bug likes twelfth iteration the most). Some details about the bug.

    1. Stack trace (everytime it is the same):

       в Microsoft.Robotics.Simulation.Physics.Utilities.FindShapeFromNxShape(NxShape* nxShape)
       в Microsoft.Robotics.Simulation.Physics.ActorContactReport.onContactNotify(ActorContactReport* , NxContactPair* pair, UInt32 events)
       в Microsoft.Robotics.Simulation.Physics.PhysicsEngine.GetPreviousFrameResults(Boolean waitForResults)
       в Microsoft.Robotics.Simulation.Engine.SimulationEngine.Update(GraphicsDevice device, Double appTime, Double elapsedTime, Double elapsedRealTime)
       в Microsoft.Robotics.Simulation.Engine.SimulationEngine.Application_Idle(Object sender, EventArgs e)
       в System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FDoIdle(Int32 grfidlef)
       в System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       в System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       в System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       в System.Windows.Forms.Application.Run(Form mainForm)
       в Microsoft.Robotics.Simulation.Engine.SimulationEngine.StartRenderLoop()
       в System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       в System.Threading.ThreadHelper.ThreadStart()

    2. I can see in IntelliTrace panel that there is always a number of thrown and caught NullReferenceExceptions before AccessViolationException.

    It occures on two machines (Windows 8 32, Windows 7 64), both of them have 8 cores.

    This bug is a real blocker for my work. At the very least I need a workaround (I doubt it exists). Thank you.

    domingo, 25 de noviembre de 2012 21:24
  • Guys, I need feedback.
    Is it a bug, or am I buggy?

    Is there any workaround?
    Will it be fixed? When?

    I can live for some time with it: WheelEntity as a byproduct has a nice feature of not really colliding with objects. So I can drive my robots safely without access violations. But in long term perspective this bug is not acceptable. If it persists I will be forced to search for some other simulator. Which perspective blankly terrifies me:).
    viernes, 30 de noviembre de 2012 9:09
  • To be honest, I found it very difficult to automate my runs using Microsoft Simulator. It was very good for running it once, but as you mentioned, since robotics is very probabilistic, you need to reproduce the result for more than once. I would also like to know the answer to your question :).

    Thanks,

    Anousha

    viernes, 30 de noviembre de 2012 15:55
  • martes, 11 de diciembre de 2012 4:09
    Propietario
  • Thank you! I am glad my question helped few people as well :). I will try your solution! 
    martes, 11 de diciembre de 2012 5:48