Example #1
0
 /// <summary>
 /// Main constructor
 /// </summary>
 /// <param name="address"></param>
 /// <param name="port"></param>
 /// <param name="mmuAccess"></param>
 public RemoteAdapterAccess(string address, int port, MAdapterDescription adapterDescription, MMUAccess mmuAccess)
 {
     this.Address     = address;
     this.Port        = port;
     this.mmuAccess   = mmuAccess;
     this.Description = adapterDescription;
 }
        /// <summary>
        /// Basic constructor of a (remote) MMU
        /// </summary>
        /// <param name="mmuAccess"></param>
        /// <param name="adapter"></param>
        /// <param name="sessionId"></param>
        /// <param name="description"></param>
        public MotionModelUnitAccess(MMUAccess mmuAccess, IAdapter adapter, string sessionId, MMUDescription description)
        {
            this.Adapter = adapter;

            //Assign all variables
            this.Description = description;
            this.sessionId   = sessionId;
            this.MotionType  = description.MotionType;
            this.mmuAccess   = mmuAccess;
            this.Name        = description.Name;
            this.ID          = description.ID;

            //Create a new client for the MMU
            this.adapterClient = adapter.CreateClient();
        }
Example #3
0
        /// <summary>
        /// Setup method for the MMIAvatar
        /// </summary>
        /// <param name="address">The address of the MMU server</param>
        /// <param name="sessionId">A unique id for the session</param>
        /// <param name="avatarID">A unique id for the avatar</param>
        public virtual void Setup(string address, int port, string sessionId)
        {
            //Save the initial position and rotation
            Vector3    startPosition = this.transform.position;
            Quaternion startRotation = this.transform.rotation;

            //Setup the retargeting
            this.SetupRetargeting();

            //Create the mmu access which creates the connection to the MMI framework
            this.MMUAccess = new MMUAccess(sessionId + ":" + this.MAvatar.ID)
            {
                //Set the scene access
                SceneAccess = GameObject.FindObjectOfType <UnitySceneAccess>(),

                //Define the retargeting here
                SkeletonAccess = this.GetSkeletonAccess(),
            };

            //Set the id
            this.MMUAccess.AvatarID = this.MAvatar.ID;

            //Setthe status text
            this.statusText = "Connecting to MMUAccess " + address + ":" + port;

            //Connect asynchronously and wait for the result
            this.MMUAccess.ConnectAsync(new MIPAddress(address, port), TimeSpan.FromSeconds(this.Timeout), this.ConnectionCallback, this.MAvatar.ID);

            // Spawn Skeleton Access Service, if required.
            MMISettings settings = GetComponentInParent <MMISettings>();

            if (settings.AllowRemoteSkeletonConnections)
            {
                //Start the remote skeleton access
                this.remoteSkeletonAccessServer = new RemoteSkeletonAccessServer(new MIPAddress(settings.RemoteSkeletonAccessAddress, settings.RemoteSkeletonAccessPort), new MIPAddress(settings.MMIRegisterAddress, settings.MMIRegisterPort), this.GetRetargetingService().GetSkeleton());
                this.remoteSkeletonAccessServer.Start();

                Debug.Log("Started Remote Skeleton Access Server with Avatar <" + this.AvatarID + ">");
            }


            //Set the postion and rotation of the avatar to the desired one
            this.transform.position = startPosition;
            this.transform.rotation = startRotation;

            //Apply the transform manipulations to update the avatar location
            this.ApplyTransformManipulations();
        }
Example #4
0
        /// <summary>
        /// MMU causes problems if initializing multiple times -> To check in future
        /// Basic initialization
        /// For specifying the priorities of the MMUs /motion types the properties can be specified (e.g. {"walk", 1.0}, {"grasp", 2.0})
        /// The listed motion types are also the ones which are loaded. If this porperty is not defined then every MMU is loaded.
        /// </summary>
        /// <param name="avatarDescription"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        public override MBoolResponse Initialize(MAvatarDescription avatarDescription, Dictionary <string, string> properties)
        {
            base.Initialize(avatarDescription, properties);

            Console.WriteLine("---------------------------------------------------------");
            Console.WriteLine("Initializing co-simulation MMU");

            //Full scene transmission initial required
            this.transmitFullScene = true;

            //Setup the mmu access
            this.mmuAccess = new MMUAccess(this.sessionId)
            {
                AvatarID    = avatarDescription.AvatarID,
                SceneAccess = this.SceneAccess
            };

            Console.WriteLine("Try to connect to mmu access...");


            //Connect to mmu access and load mmus
            if (this.mmuAccess.Connect(this.AdapterEndpoint, avatarDescription.AvatarID))
            {
                //Get all loadable MMUs within the current session
                List <MMUDescription> loadableMMUs = this.mmuAccess.GetLoadableMMUs();


                //Select the MMUs which should be loaded
                loadableMMUs = this.SelectMMUsToLoad(loadableMMUs);

                //Create a dictionary for storing the priorities
                Dictionary <string, float> priorities = new Dictionary <string, float>();
                priorities = this.GetPriorities?.Invoke();



                //Select the MMUs to load if explictely specified by the user
                if (properties != null && properties.Count > 0)
                {
                    for (int i = loadableMMUs.Count - 1; i >= 0; i--)
                    {
                        MMUDescription description = loadableMMUs[i];

                        float priority = 1;

                        //If MMU is listed -> add the priority
                        if (priorities.TryGetValue(description.MotionType, out priority))
                        {
                            priorities.Add(description.MotionType, priority);
                        }

                        //MMU is not explicetly listed -> remove from loading list
                        else
                        {
                            loadableMMUs.RemoveAt(i);
                        }
                    }
                }

                //No MMU list defined -> Load all MMUs with same priority (despite the own MMU)
                else
                {
                    //Remove the own MMU -> Avoid recursively instantiating own MMU (unless explictely forced)
                    if (loadableMMUs.Exists(s => s.Name == this.Name))
                    {
                        MMUDescription ownDescription = loadableMMUs.Find(s => s.Name == this.Name);
                        loadableMMUs.Remove(ownDescription);
                    }
                }

                Console.WriteLine("Got loadable MMUs:");

                try
                {
                    //Load the relevant MMUs
                    bool success = this.mmuAccess.LoadMMUs(loadableMMUs, TimeSpan.FromSeconds(20));
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error at loading MMUs : " + e.Message + e.StackTrace);

                    return(new MBoolResponse(false)
                    {
                        LogData = new List <string>()
                        {
                            e.Message,
                            e.StackTrace
                        }
                    });
                }

                Console.WriteLine("All MMUs successfully loaded");


                foreach (MMUDescription description in loadableMMUs)
                {
                    Console.WriteLine(description.Name);
                }


                //Initialize all MMUs
                bool initialized = this.mmuAccess.InitializeMMUs(TimeSpan.FromSeconds(20), avatarDescription.AvatarID);

                if (!initialized)
                {
                    Console.WriteLine("Problem at initializing MMUs");

                    return(new MBoolResponse(false)
                    {
                        LogData = new List <string>()
                        {
                            { "Problem at initializing MMUs" }
                        }
                    });
                }

                //Instantiate the cosimulator
                this.coSimulator = new MMICoSimulator(mmuAccess.MotionModelUnits);

                //Set the priorities of the motions
                this.coSimulator.SetPriority(priorities);


                return(new MBoolResponse(true));
            }

            else
            {
                Console.WriteLine("Connection to MMUAccess/MMIRegister failed");
                return(new MBoolResponse(false)
                {
                    LogData = new List <string>()
                    {
                        "Connection to MMUAccess/MMIRegister failed"
                    }
                });
            }
        }
        /// <summary>
        /// Basic initialization method
        /// </summary>
        /// <param name="avatarDescription"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        public override MBoolResponse Initialize(MAvatarDescription avatarDescription, Dictionary <string, string> properties)
        {
            //Call the base class (important for automatic conversion from MSimulationState to SimulationState)
            base.Initialize(avatarDescription, properties);

            this.SkeletonAccess = new IntermediateSkeleton();
            this.SkeletonAccess.InitializeAnthropometry(avatarDescription);


            //Create a new session uuid
            this.sessionId = Guid.NewGuid().ToString();

            //Create a new virtual scene
            this.virtualScene = new MMIScene();

            //Create a unique id for the new virtual object representing the move target
            string moveTargetID = Guid.NewGuid().ToString();

            //Create a new virtual object representing the move target
            moveTarget = new MSceneObject(moveTargetID, "MoveTarget", new MTransform()
            {
                ID       = moveTargetID,
                Position = new MVector3(0, 0, 0),
                Rotation = new MQuaternion(0, 0, 0, 1)
            });

            //Add the virtual move target to the scene
            this.virtualScene.Apply(new MSceneUpdate()
            {
                AddedSceneObjects = new List <MSceneObject>()
                {
                    moveTarget
                }
            });


            //Full scene transmission initial required
            this.transmitFullScene = true;

            //Setup the mmu access
            this.mmuAccess = new MMUAccess(this.sessionId)
            {
                //IntermediateAvatarDescription = avatarDescription,
                SceneAccess = this.virtualScene
            };

            Console.WriteLine("Try to connect to mmu access...");

            //Connect to mmu access and load mmus
            bool connected = this.mmuAccess.Connect(this.AdapterEndpoint, this.AvatarDescription.AvatarID);


            if (connected)
            {
                //Get all loadable MMUs within the current session
                List <MMUDescription> loadableMMUs = this.mmuAccess.GetLoadableMMUs();


                MMUDescription moveMMU = loadableMMUs.Find(s => s.MotionType == "move");

                Console.WriteLine("Got loadable MMUs:");


                //Load the relevant MMUs
                bool loaded = this.mmuAccess.LoadMMUs(new List <MMUDescription>()
                {
                    moveMMU
                }, TimeSpan.FromSeconds(10));

                if (!loaded)
                {
                    Console.WriteLine("Error at loading MMU");

                    return(new MBoolResponse(false)
                    {
                        LogData = new List <string>()
                        {
                            { "Error at loading mmu" }
                        }
                    });
                }

                //Initialize all MMUs
                this.mmuAccess.InitializeMMUs(TimeSpan.FromSeconds(10), this.AvatarDescription.AvatarID);

                //Instantiate the cosimulator
                this.coSimulator = new MMICoSimulator(mmuAccess.MotionModelUnits);


                return(new MBoolResponse(true));
            }
            else
            {
                Console.WriteLine("Connection to MMUAccess/MMIRegister failed");
                return(new MBoolResponse(false)
                {
                    LogData = new List <string>()
                    {
                        "Connection to MMUAccess/MMIRegister failed"
                    }
                });
            }
        }
Example #6
0
 /// <summary>
 /// Basic constructor
 /// </summary>
 /// <param name="description"></param>
 /// <param name="instance"></param>
 public LocalAdapterAccess(MAdapterDescription description, MMIAdapter.Iface instance, MMUAccess mmuAccess)
 {
     this.Description = description;
     this.instance    = instance;
     this.mmuAccess   = mmuAccess;
 }