/// <summary> /// Callback for the initialization of the MMUs /// </summary> /// <param name="initialized"></param> protected virtual void InitializingCallback(bool initialized) { if (initialized) { //Create a new instance of the remote cosimulation class if (this.UseRemoteCoSimulation) { this.CoSimulator = new RemoteCoSimulation(this.MMUAccess.MotionModelUnits.Find(s => s.Name == this.RemoteCoSimulationName), this.MMUAccess.ServiceAccess, this, null); } //Create the local co-simulator (directly integrated in Unity) else { this.CoSimulator = new LocalCoSimulation(this.MMUAccess.MotionModelUnits, this.MMUAccess.ServiceAccess, this); } if (this.AllowRemoteCoSimulationAccess) { if (MainThreadDispatcher.Instance == null) { Debug.LogError("Please add a MainTrhead Dispatcher to the scene in order to allow remote co simulation acess"); return; } //Must be executed on main thread MainThreadDispatcher.Instance.ExecuteBlocking(() => { //Get the settings in the parent object MMISettings settings = this.gameObject.GetComponentInParent <MMISettings>(); if (settings == null) { Debug.LogError("Please add the MMI settings to the UnitySceneAccess it is required to access the MMIRegisterAddress"); return; } try { //Start the remote co-simulation access for the given avatar this.coSimulationAccess = new CoSimulationAccess(this.CoSimulator, new MIPAddress(this.RemoteCoSimulationAccessAddress, this.RemoteCoSimulationAccessPort), new MIPAddress(settings.MMIRegisterAddress, settings.MMIRegisterPort)); this.coSimulationAccess.Start(); Debug.Log("Started remote co-simulation access"); } catch (Exception e) { Debug.LogError(e.Message); } }); } } else { this.statusText = "Problem at initializing MMUs"; Debug.LogError("Problem at initializing MMUs"); } }
/// <summary> /// Basic constructor /// </summary> /// <param name="coSimulator">The instance of the co-simulator</param> /// <param name="address">The address where the CoSimulationAccess should be hosted</param> /// <param name="registerAddress">The address of the register</param> public CoSimulationAccess(MMICoSimulator coSimulator, MIPAddress address, MIPAddress registerAddress) { //Assign the co simulator this.coSimulator = coSimulator; //Register at on result event this.coSimulator.OnResult += CoSimulator_OnResult; //Add the address to the description this.description.Addresses = new List <MIPAddress>() { address }; //Create a new service controller this.controller = new ServiceController(this.description, registerAddress, new MCoSimulationAccess.Processor(this)); }
/// <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" } }); } }