Exemplo n.º 1
0
        private void Connect()
        {
            // [NatNet] connect to a NatNet server
            int returnCode = 0;

            returnCode = m_NatNet.Initialize(textBoxLocal.Text, textBoxServer.Text);
            if (returnCode == 0)
            {
                OutputMessage("Initialization Succeeded.");
            }
            else
            {
                OutputMessage("Error Initializing.");
                checkBoxConnect.Checked = false;
            }

            // [NatNet] validate the connection
            returnCode = m_NatNet.GetServerDescription(desc);
            if (returnCode == 0)
            {
                OutputMessage("Connection Succeeded.");
                OutputMessage("   Server App Name: " + desc.HostApp);
                OutputMessage(String.Format("   Server App Version: {0}.{1}.{2}.{3}", desc.HostAppVersion[0], desc.HostAppVersion[1], desc.HostAppVersion[2], desc.HostAppVersion[3]));
                OutputMessage(String.Format("   Server NatNet Version: {0}.{1}.{2}.{3}", desc.NatNetVersion[0], desc.NatNetVersion[1], desc.NatNetVersion[2], desc.NatNetVersion[3]));
                checkBoxConnect.Text = "Disconnect";

                if (desc.HostApp.Contains("TrackingTools"))
                {
                    m_ServerToMillimeters = 1000.0f;
                }

                // [NatNet] [optional] send a test/response message
                OutputMessage("Sending TestRequest");
                int    nBytes   = 0;
                byte[] response = new byte[10000];
                int    rc       = m_NatNet.SendMessageAndWait("TestRequest", out response, out nBytes);
                if (rc == 0)
                {
                    string str = "   Server: " + System.Text.Encoding.ASCII.GetString(response, 0, nBytes);
                    OutputMessage(str);
                }
                else
                {
                    OutputMessage("   Server: No Response.");
                }

                rc = m_NatNet.SendMessageAndWait("FrameRate", out response, out nBytes);
                if (rc == 0)
                {
                    try
                    {
                        m_ServerFramerate = BitConverter.ToSingle(response, 0);
                        OutputMessage(String.Format("   Server Framerate: {0}", m_ServerFramerate));
                    }
                    catch (System.Exception ex)
                    {
                        OutputMessage(ex.Message);
                    }
                }

                m_fCurrentMocapFrameTimestamp = 0.0f;
                m_fFirstMocapFrameTimestamp   = 0.0f;
            }
            else
            {
                OutputMessage("Error Connecting.");
                checkBoxConnect.Checked = false;
                checkBoxConnect.Text    = "Connect";
            }
        }
        /// <summary>
        /// Connect to a NatNet server (e.g. Motive)
        /// </summary>
        private void Connect()
        {
            // [NatNet] connect to a NatNet server
            int    returnCode  = 0;
            string strLocalIP  = comboBoxLocal.SelectedItem.ToString();
            string strServerIP = textBoxServer.Text;

            returnCode = m_NatNet.Initialize(strLocalIP, strServerIP);
            if (returnCode == 0)
            {
                OutputMessage("Initialization Succeeded.");
            }
            else
            {
                OutputMessage("Error Initializing.");
                checkBoxConnect.Checked = false;
            }

            // [NatNet] validate the connection
            returnCode = m_NatNet.GetServerDescription(desc);
            if (returnCode == 0)
            {
                OutputMessage("Connection Succeeded.");
                OutputMessage("   Server App Name: " + desc.HostApp);
                OutputMessage(String.Format("   Server App Version: {0}.{1}.{2}.{3}", desc.HostAppVersion[0], desc.HostAppVersion[1], desc.HostAppVersion[2], desc.HostAppVersion[3]));
                OutputMessage(String.Format("   Server NatNet Version: {0}.{1}.{2}.{3}", desc.NatNetVersion[0], desc.NatNetVersion[1], desc.NatNetVersion[2], desc.NatNetVersion[3]));
                checkBoxConnect.Text = "Disconnect";

                // Tracking Tools and Motive report in meters - lets convert to millimeters
                if (desc.HostApp.Contains("TrackingTools") || desc.HostApp.Contains("Motive"))
                {
                    m_ServerToMillimeters = 1000.0f;
                }


                // [NatNet] [optional] Query mocap server for the current camera framerate
                int    nBytes   = 0;
                byte[] response = new byte[10000];
                int    rc;
                rc = m_NatNet.SendMessageAndWait("FrameRate", out response, out nBytes);
                if (rc == 0)
                {
                    try
                    {
                        m_ServerFramerate = BitConverter.ToSingle(response, 0);
                        OutputMessage(String.Format("   Server Framerate: {0}", m_ServerFramerate));
                    }
                    catch (System.Exception ex)
                    {
                        OutputMessage(ex.Message);
                    }
                }

                // [NatNet] [optional] Query mocap server for the current up axis
                rc = m_NatNet.SendMessageAndWait("UpAxis", out response, out nBytes);
                if (rc == 0)
                {
                    m_UpAxis = BitConverter.ToInt32(response, 0);
                }


                m_fCurrentMocapFrameTimestamp = 0.0f;
                m_fFirstMocapFrameTimestamp   = 0.0f;
            }
            else
            {
                OutputMessage("Error Connecting.");
                checkBoxConnect.Checked = false;
                checkBoxConnect.Text    = "Connect";
            }
        }
Exemplo n.º 3
0
        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            int returnCode = 0;

            if (FEnabled[0])
            {
                //If not connected already, try to connect
                if (!FConnected[0])
                {
                    // [NatNet] connect to a NatNet server
                    returnCode = mNatNet.Initialize(FLocal[0], FServer[0], FPort[0]);
                    FLogger.Log(LogType.Debug, "Trying to connect...");

                    if (returnCode == 0)
                    {
                        FLogger.Log(LogType.Debug, "Connected!");

                        // [NatNet] validate the connection
                        returnCode = mNatNet.GetServerDescription(mDesc);

                        if (returnCode == 0)
                        {
                            FLogger.Log(LogType.Debug, "Server App Name: " + mDesc.HostApp);


                            // [NatNet] [optional] send a test/response message
                            FLogger.Log(LogType.Debug, "Sending TestRequest");
                            int    nBytes   = 0;
                            byte[] response = new byte[10000];
                            int    rc       = mNatNet.SendMessageAndWait("TestRequest", out response, out nBytes);
                            if (rc == 0)
                            {
                                string str = "   Server: " + System.Text.Encoding.ASCII.GetString(response, 0, nBytes);
                                FLogger.Log(LogType.Debug, str);
                            }
                            else
                            {
                                FLogger.Log(LogType.Debug, "   Server: No Response.");
                            }

                            rc = mNatNet.SendMessageAndWait("FrameRate", out response, out nBytes);
                            if (rc == 0)
                            {
                                try
                                {
                                    double serverFramerate = BitConverter.ToSingle(response, 0);
                                    FLogger.Log(LogType.Debug, String.Format("   Server Framerate: {0}", serverFramerate));
                                }
                                catch (System.Exception ex)
                                {
                                    FLogger.Log(LogType.Debug, ex.Message);
                                }
                            }


                            //Connected
                            FConnected[0] = true;
                            FEnabled[0]   = FConnected[0];
                        }
                    }

                    //Get the rigid body names
                    this.RequestDataDescription();
                }
                else
                {
                    // Already connected, update output

                    // Upon update, query server for data description
                    if (FUpdate.IsChanged)
                    {
                        this.RequestDataDescription();
                    }
                    lock (syncLock) {
                        //FLogger.Log(LogType.Debug, "" + m_FrameQueue.Count);
                        while (m_FrameQueue.Count > 0)
                        {
                            mFrameOfData = m_FrameQueue.Dequeue();

                            if (m_FrameQueue.Count > 0)
                            {
                                continue;
                            }
                            FFrame.SliceCount = 0;
                            FFrame.Add(mFrameOfData);
                        }
                    }


                    // Update the rigid bodies names
                    FRigidBody.SliceCount = mRBNames.Count;
                    for (int i = 0; i < mRBNames.Count; ++i)
                    {
                        FRigidBody[i] = mRBNames[i];
                    }
                }
            }
            else
            {
                //If connected already, disconnect, else, do nothing
                if (FConnected[0])
                {
                    mRBNames.Clear();
                    returnCode    = mNatNet.Uninitialize();
                    FConnected[0] = (0 == returnCode);
                }
            }
        }