// Use this for initialization
        public void StartServer(MemoryBridge memoryBridge)
        {
            fileHeader     = MemoryMappedFile.Create(MapProtection.PageReadWrite, 16, "FileHeader");
            fileValueFloat = MemoryMappedFile.Create(MapProtection.PageReadWrite, 16, "FileValuesFloat3");

            this.memoryBridge = memoryBridge;

            keysFloat         = new List <string>();
            valueAddressFloat = new Dictionary <string, int>();
            keysToAdd         = new Dictionary <string, float>();
            UpdateHeaderFile();
        }
        public void ControlStart(Vessel vessel, MemoryBridge memoryBridge)
        {
            this.vessel       = vessel;
            this.memoryBridge = memoryBridge;

            //  var IVAcam = vessel.gameObject.GetComponentInChildren<IVACamera>();

            //  if (IVAcam)
            //      Debug.Log("IVA cam found on " + IVAcam.name);

            //Create a vessel vector that can be adjusted
            GameObject adjustedVesselObject = new GameObject();

            adjustedVessel = adjustedVesselObject.transform;
            adjustedVessel.SetParent(vessel.vesselTransform);
            adjustedVessel.localEulerAngles = Vector3.zero;
            adjustedVessel.localPosition    = Vector3.zero;
            if (vessel.name.ToLower().Contains("aircraft"))
            {
                adjustedVessel.localEulerAngles += new Vector3(-90, 0, 0);
            }
            //DebugVector.DrawVector(adjustedVessel);

            //Create a transform for the vessels COM, placed as a child of the Vessel
            GameObject COMobject = new GameObject();

            vesselCOM = COMobject.transform;
            vesselCOM.SetParent(vessel.vesselTransform);
            vesselCOM.localEulerAngles = Vector3.zero;
            vesselCOM.localPosition    = vessel.localCoM;
            // DebugVector.DrawVector(vesselCOM);

            GameObject gimbalObject = new GameObject();

            gimbal = gimbalObject.transform;
            gimbal.SetParent(vesselCOM);
            gimbal.localPosition    = Vector3.zero;
            gimbal.localEulerAngles = Vector3.zero;
            //DebugVector.DrawVector(gimbal);

            if (vessel.name.ToLower().Contains("aircraft"))
            {
                vesselCOM.localEulerAngles += new Vector3(-90, 0, 0);
            }

            vesselSerializer = gameObject.AddComponent(typeof(VesselSerializer)) as VesselSerializer;
            vesselSerializer.SerializeVessel(this.vessel, this.memoryBridge, this);

            IRmanager = gameObject.AddComponent(typeof(IR_Manager)) as IR_Manager;
            IRmanager.CustomStart(vessel.Parts, memoryBridge);

            ToggleAutoPilot(true);
        }
        public void SerializeVessel(Vessel vessel, ref MemoryBridge memoryBridge, VesselControl vesselControl)
        {
            Debug.Log("Vessel Control Awake");
            this.vessel        = vessel;
            this.memoryBridge  = memoryBridge;
            this.vesselControl = vesselControl;

            Debug.Log("Current Vessel " + vessel.name);

            // var kspPartList = this.vessel.Parts;
            Debug.Log("try to serialize custom part");

            //WORKS
            BuildVessel();
        }
        void StartProgram()
        {
            Debug.Log("Init wrapper");
            var success = IRWrapper.InitWrapper();// IRWrapper.InitWrapper();


            // Debug.Log(".net version " + Application.)


            serverStarted = true;
            Debug.Log("start server " + Time.frameCount);
            Debug.Log("Unity version " + Application.version);
            Debug.Log("Unity version " + Application.unityVersion);
            memoryBridge = gameObject.AddComponent(typeof(MemoryBridge)) as MemoryBridge;
            memoryBridge.StartServer("Test");
        }
        public void BuildCameraFeed(MemoryBridge memoryBridge, string fileName)
        {
            //Debug.Log("Build Camera Feed");
            this.memoryBridge = memoryBridge;
            this.fileName     = fileName;

            threadList = new List <int>();
            threadList.Add(0);
            threadList.Add(1);
            //   threadList.Add(2);
            //  threadList.Add(3);

            GameObject camObject = new GameObject();

            DebugVector.DrawVector(camObject.transform);
            camObject.transform.SetParent(FlightGlobals.ActiveVessel.vesselTransform);

            feedCam = camObject.AddComponent <Camera>();

            rendTextHeight = feedCam.pixelHeight;
            rendTextWidth  = feedCam.pixelWidth;
            //Debug.Log("height " + rendTextHeight + " width " + rendTextWidth);
            rendText = new RenderTexture(rendTextWidth, rendTextHeight, 16, RenderTextureFormat.ARGB32);
            rendText.Create();
            feedCam.targetTexture = rendText;

            tex     = new Texture2D(rendTextWidth, rendTextWidth, TextureFormat.RGB24, false);
            texCopy = new Texture2D(rendTextWidth, rendTextHeight, TextureFormat.RGB24, false);

            RenderTexture.active = rendText;
            //Debug.Log("Render width " + rendTextWidth);
            //Debug.Log("render height " + rendTextHeight);
            tex.ReadPixels(new Rect(0, 0, rendTextWidth, rendTextHeight), 0, 0);
            tex.Apply();
            camTexture = tex;
            // var colorBuffer = rendText.colorBuffer;
            var textureByte = camTexture.GetRawTextureData();

            camFeedFile = MemoryMappedFile.Create(MapProtection.PageReadWrite, textureByte.Length, "CamFeedFile" + fileName + memoryBridge.cameraFeeds.Count);

            memoryBridge.SetFloat("rendTextHeight" + fileName + memoryBridge.cameraFeeds.Count, rendTextHeight);
            memoryBridge.SetFloat("rendTextWidth" + fileName + memoryBridge.cameraFeeds.Count, rendTextWidth);
            memoryBridge.SetFloat("feedByteCount" + fileName + memoryBridge.cameraFeeds.Count, textureByte.Length);

            ////Debug.Log("ByteSize " + textureByte.Length);
            //Debug.Log("mem map loaded");
        }
Esempio n. 6
0
        private void Start()
        {
            //for (int i = 0; i < 1000; i++)
            //{
            //    memoryBridge.SetValue("KeyCount" + i, i);
            //}
            //Debug.Log("Values set");
            Debug.Log("Init wrapper");
            var success = IRWrapper.InitWrapper();// IRWrapper.InitWrapper();



            serverStarted = true;
            Debug.Log("start server " + Time.frameCount);
            memoryBridge = gameObject.AddComponent(typeof(MemoryBridge)) as MemoryBridge;
            memoryBridge.StartServer("Test");

            //  if ()
            //     UnityEngine.Debug.Log("Hexapod IR ready to go");
        }
Esempio n. 7
0
        public void CustomStart(IRWrapper.IServo servo, MemoryBridge memoryBridge)
        {
            this.servo              = servo;
            this.servo.Speed        = 20;
            this.servo.Acceleration = 20;
            servoName = servo.Name + servo.HostPart.gameObject.GetInstanceID();
            part      = servo.HostPart;

            this.memoryBridge = memoryBridge;

            memoryBridge.SetFloat(servoName + "unityServoPos", servo.Position);

            memoryBridge.SetFloat(servoName + "minPos", servo.MinPosition);
            Debug.Log("servo min pos " + servo.MinPosition);
            memoryBridge.SetFloat(servoName + "maxPos", servo.MaxPosition);
            Debug.Log("servo max pos " + servo.MaxPosition);

            // memoryBridge.SetFloat(servoName + "ServoSetPos", 210f);
            //  memoryBridge.SetFloat(servoName + "ServoSetSpeed", 210f);

            //search the parent part for a transform named "Base"
            var partParent     = part.parent;
            var parentChildren = partParent.GetComponentsInChildren <Transform>();
            var bases          = new List <Transform>();

            foreach (var child in parentChildren)
            {
                if (child.name.ToLower().Contains("base"))
                {
                    bases.Add(child);
                }
            }

            Debug.Log("Bases count " + bases.Count);
            if (bases.Count == 0)
            {
                Debug.Log(servo.Name + " does not have a base");
            }
            else
            {
                //If there are multiple bases on the parent, use the closest one
                if (bases.Count > 1)
                {
                    var minDist = Mathf.Infinity;
                    foreach (var basePart in bases)
                    {
                        var dist = Vector3.Distance(transform.position, basePart.position);
                        if (dist < minDist)
                        {
                            servoBase = basePart;
                            minDist   = dist;
                        }
                    }
                }
                else
                {
                    servoBase = bases[0];
                }

                var newObject = new GameObject();
                newObject.name = "Servo Anchor";
                servoAnchor    = newObject.transform;
                servoAnchor.SetParent(servoBase);
                servoAnchor.localPosition    = Vector3.zero;
                servoAnchor.localEulerAngles = Vector3.zero;

                newObject      = new GameObject();
                newObject.name = "Servo Child";
                anchorChild    = newObject.transform;
                anchorChild.SetParent(servoAnchor);
                anchorChild.localPosition = Vector3.zero;
                anchorChild.rotation      = part.transform.rotation;

                Debug.Log(this.servo.HostPart.name);

                //Move the x axis of the rotatron part. This happens in Unity script as well in Parts
                if (this.servo.HostPart.name == "IR.Rotatron.Basic.v3")
                {
                    servoAnchor.localEulerAngles += new Vector3(0, 0, 90);
                    anchorChild.localEulerAngles += new Vector3(0, 0, 90);
                    // DebugVector.DrawVector(servoAnchor);
                    //  DebugVector.DrawVector(anchorChild);
                    Debug.Log("Draw Vector");
                }

                DebugVector.DrawVector(servoAnchor);
            }
        }
       // List<Part> parts;
        public void CustomAwake(IRWrapper.IControlGroup limbGroup, IRWrapper.IServo servoBase, IRWrapper.IServo servoWrist, MemoryBridge memoryBridge)
        {
            this.memoryBridge = memoryBridge;
            limbName = limbGroup.Name + memoryBridge.fileName;
            limbServos = new List<RoboticServo>();
            //Add wrist servo
            var newServo = servoBase.HostPart.gameObject.AddComponent(typeof(RoboticServo)) as RoboticServo;
            newServo.CustomStart(servoBase, memoryBridge);
            limbServos.Add(newServo);

            //parts = new List<Part>();

            Part tempPart = servoBase.HostPart;

            bool done = false;
            int count = 0;
            do
            {
                count++;
                var armParts = tempPart.children;

                armParts.Remove(tempPart);
                foreach (var part in armParts)
                {
                    Debug.Log(part.name + " is a child of " + tempPart.name);
                    IRWrapper.IServo testServo = null;
                    foreach (var servo in limbGroup.Servos)
                    {
                        if (part == servo.HostPart)
                        {
                            testServo = servo;
                            //break;
                        }
                    }
                    if (testServo != null)
                    {
                        tempPart = part;
                        // if (!testServo.Name.ToLower().Contains("skip"))
                        // {                    
                        Debug.Log("found servo on part");

                        newServo = part.gameObject.AddComponent(typeof(RoboticServo)) as RoboticServo;
                        newServo.CustomStart(testServo, memoryBridge);
                        limbServos.Add(newServo);
                        //  }
                        if (testServo.Name.ToLower().Contains("wrist"))
                        {
                            Debug.Log("wrist servo found");
                            wristServo = newServo;
                            done = true;
                        }
                        //if (servo.Name.ToLower().Contains("wrist"))
                        //{
                        //    wristPart = servo;
                        //}

                    }
                }
            } while (count < 30);

            //newServo = gameObject.AddComponent(typeof(RoboticServo)) as RoboticServo;
            //newServo.CustomStart(servoWrist);
            //limbServos.Add(newServo);

            int byteCount = 0;

            Debug.Log(limbGroup.Name + " servo hierarchy " + limbServos.Count);
            for (int i = 0; i < limbServos.Count; i++)
            {
                Debug.Log(limbServos[i].servo.Name);
                string parentName = "null";
                if (i != 0)
                {
                    parentName = limbServos[i - 1].servoName;
                    limbServos[i].parentID = limbServos[i - 1].servo.HostPart.gameObject.GetInstanceID();
                    limbServos[i].ParentSet(limbServos[i - 1].servo.HostPart.transform);
                }

                limbServos[i].parentServoName = parentName;

                byteCount += 8;
                //parent id
                byteCount += 4;
                byteCount += limbServos[i].servoName.Length;
                // byteCount += limbServos[i].parentServoName.Length;
            }
            byteCount += 16;
            LimbFile = MemoryMappedFile.Create(MapProtection.PageReadWrite, byteCount, limbName);

            using (Stream mapStream = LimbFile.MapView(MapAccess.FileMapAllAccess, 0, byteCount))
            {
                var bytePackage = BitConverter.GetBytes((float)byteCount);
                mapStream.Write(bytePackage, 0, 4);
                bytePackage = BitConverter.GetBytes((float)limbServos.Count);
                mapStream.Write(bytePackage, 0, 4);

                mapStream.Position = 16;

                for (int i = 0; i < limbServos.Count; i++)
                {
                    var nameBytePackage = Encoding.ASCII.GetBytes(limbServos[i].servoName);
                    bytePackage = BitConverter.GetBytes((float)nameBytePackage.Length);
                    mapStream.Write(bytePackage, 0, 4);
                    mapStream.Write(nameBytePackage, 0, nameBytePackage.Length);

                    // nameBytePackage = Encoding.ASCII.GetBytes(limbServos[i].parentServoName);
                    bytePackage = BitConverter.GetBytes((float)limbServos[i].servo.HostPart.gameObject.GetInstanceID());
                    mapStream.Write(bytePackage, 0, 4);
                    //  mapStream.Write(nameBytePackage, 0, nameBytePackage.Length);     
                    bytePackage = BitConverter.GetBytes((float)limbServos[i].parentID);
                    mapStream.Write(bytePackage, 0, 4);
                }
            }


            //var contactPoint = memoryBridge.GetVector3(wristServo.servoName + "contactPoint");

            //if (contactPoint != Vector3.zero)
           // {
               // Debug.Log("Set ground point to " + contactPoint);
                //wristServo.contactPoint = contactPoint;
                wristServo.CreateContactPoint();
              //  contactPointSet = true;
            //}
        }
Esempio n. 9
0
        public void CustomStart(List <Part> parts, MemoryBridge memoryBridge)
        {
            Debug.Log("IR Manager custom start");
            limbs = new List <RoboticArm>();

            if (IRWrapper.IRController == null)
            {
                Debug.Log("IR3Controller is not null");
            }
            Debug.Log(IRWrapper.IRController.ServoGroups.Count);
            foreach (IRWrapper.IControlGroup group in IRWrapper.IRController.ServoGroups)
            {
                Debug.Log("Servo Group : " + group.Name);
                if (group.Name.ToLower().Contains("ik"))
                {
                    IRWrapper.IServo basePart  = null;
                    IRWrapper.IServo wristPart = null;
                    foreach (var servo in group.Servos)
                    {
                        if (servo.Name.ToLower().Contains("base"))
                        {
                            basePart = servo;
                        }
                        if (servo.Name.ToLower().Contains("wrist"))
                        {
                            wristPart = servo;
                        }
                    }
                    var newLimb = gameObject.AddComponent(typeof(RoboticArm)) as RoboticArm;
                    newLimb.CustomAwake(group, basePart, wristPart, memoryBridge);
                    limbs.Add(newLimb);

                    byteCount += 4;
                    byteCount += newLimb.limbName.Length;
                }
            }

            byteCount += 16;
            IRFile     = MemoryMappedFile.Create(MapProtection.PageReadWrite, byteCount, "IRFile" + memoryBridge.fileName);

            using (Stream mapStream = IRFile.MapView(MapAccess.FileMapAllAccess, 0, byteCount))
            {
                var bytePackage = BitConverter.GetBytes(byteCount);
                mapStream.Write(bytePackage, 0, 4);
                bytePackage = BitConverter.GetBytes(limbs.Count);
                mapStream.Write(bytePackage, 0, 4);

                mapStream.Position = 16;

                foreach (var limb in limbs)
                {
                    var nameBytePackage = Encoding.ASCII.GetBytes(limb.limbName);
                    bytePackage = BitConverter.GetBytes((float)nameBytePackage.Length);
                    mapStream.Write(bytePackage, 0, 4);
                    mapStream.Write(nameBytePackage, 0, nameBytePackage.Length);
                }
            }

            //legs = new List<MechLeg>();

            //foreach (IRWrapper.IControlGroup group in IRWrapper.IRController.ServoGroups)
            //{
            //    if (group.Name == "Leg One")
            //    {
            //        legOne = new MechLeg(memoryBridge, group, this, "Leg One");
            //        legs.Add(legOne);
            //    }
            //    else if (group.Name == "Leg Two")
            //    {
            //        legTwo = new MechLeg(memoryBridge, group, this, "Leg Two");
            //        legs.Add(legTwo);
            //    }
            //    else if (group.Name == "Leg Three")
            //    {
            //        legThree = new MechLeg(memoryBridge, group, this, "Leg Three");
            //        legs.Add(legThree);
            //    }
            //    else if (group.Name == "Leg Four")
            //    {
            //        legFour = new MechLeg(memoryBridge, group, this, "Leg Four");
            //        legs.Add(legFour);
            //    }
            //    else if (group.Name == "Leg Five")
            //    {
            //        legFive = new MechLeg(memoryBridge, group, this, "Leg Five");
            //        legs.Add(legFive);
            //    }
            //    else if (group.Name == "Leg Six")
            //    {
            //        legSix = new MechLeg(memoryBridge, group, this, "Leg Six");
            //        legs.Add(legSix);
            //    }
            //    else if (group.Name == "Arm")
            //    {
            //        armOne = new MechArm(memoryBridge, group, this);
            //    }
            //}
        }