Reseting the configuration of the environment RRS feed

  • Question

  • 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

    Wednesday, March 2, 2011 1:08 PM


  • 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.
    Wednesday, March 7, 2012 9:15 PM

All replies

  • you can use file->open scene to load your xml. i think it is fast enough.
    Friday, March 4, 2011 2:47 PM
  • 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.



    Sunday, March 6, 2011 8:27 AM
  • Thanks Trevor!
    Thursday, March 31, 2011 8:48 PM
  • I have written some code that reset entities according the XML file

        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();
          yield return Arbiter.Choice(get.ResponsePort, (fault) => error = fault,
            (success) => simstate = (Microsoft.Robotics.Simulation.Proxy.SimulationState)success);
          if (error != null)
            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);
            simengine.VisualEntity entity = null;
            yield return Arbiter.Choice(
              (success) => entity = (simengine.VisualEntity) DssTypeHelper.TransformFromProxy (success.Instance),
            entities[i] = entity;
          for (i = 0; i < entities.Length; ++i)
            simengine.VisualEntity entity = entities[i];
            if (entity is simengine.SkyDomeEntity || entity is simengine.LightSourceEntity)
            simengine.DeleteSimulationEntity delete = new simengine.DeleteSimulationEntity (entity);
            yield return Arbiter.Choice(
              (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);
            yield return Arbiter.Choice(
              (success) => { }, LogError);
          // Unpause
          simstate.Pause = false;
          yield return Arbiter.Choice(_simEngine.Replace(simstate), (success) => { }, LogError);
          LogInfo("Simulation reset");
           yield break;


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




    Friday, April 1, 2011 5:44 PM
  • 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.


    Thursday, January 19, 2012 9:48 PM
  • 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.



    Monday, January 23, 2012 1:59 AM
  • 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);
                        yield return Arbiter.Choice(
                          (success) => { }, LogError);
                        int j, k = 0;
                        for (j = 0; j < entity.ChildCount; j++)
                            VisualEntity child = entities[j + i + 1];
                            if (child.ChildCount != 0)
                                for (k = 0; k < child.ChildCount; k++)
                                    VisualEntity nested = entities[j + k + i + 2];
                        InsertSimulationEntity insert = new InsertSimulationEntity(entity);
                        yield return Arbiter.Choice(
                          (success) => { Debug.WriteLine("insert was successful"); }, LogError);
                        i = i + j + k;

    Tuesday, February 14, 2012 9:51 PM
  • 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.


    Monday, February 27, 2012 10:39 AM
  • 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.
    Wednesday, March 7, 2012 9:15 PM
  • 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
    	[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"); });
            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()
            private IEnumerator<ITask> Test1()
                yield return Arbiter.FromIteratorHandler(SetUpSimulator);
                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));
                    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));
                ///} Remove old boxes
                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) } }
            private void GenerateEnvironment()
                var ground = new HeightFieldEntity("Ground", "WoodFloor.dds", new MaterialProperties("ground", 0f, 0.5f, 0.5f));
                SkyDomeEntity sky = new SkyDomeEntity("skydome.dds", "sky_diff.dds");
                var sun = new LightSourceEntity(LightSourceEntityType.Directional)
                    State = { Name = "Sun" },
                    Direction = new Vector3(0.5f, -.75f, 0.5f)
            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.

    Sunday, November 25, 2012 9:24 PM
  • 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:).
    Friday, November 30, 2012 9:09 AM
  • 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 :).



    Friday, November 30, 2012 3:55 PM
  • Tuesday, December 11, 2012 4:09 AM
  • Thank you! I am glad my question helped few people as well :). I will try your solution! 
    Tuesday, December 11, 2012 5:48 AM