コード例 #1
0
    void FixedUpdate()
    {
        float rawEmg;

        rawEmg = ChooseMode("real");

        while (Queue.TryDequeue(out rawEmg))
        {
            bayesfilter = myBayesFilter.UpdateEst(Math.Abs(rawEmg) * 6e2f);
//			Debug.Log(bayesfilter.ToString());
        }

        averagefilter     = EmgAverage(10, Math.Abs(rawEmg * 80000 / 40));
        butterworthfilter = ButterworthFilter(rawEmg * 80000 / 4);

        float barHeight = 0.6e2f * Math.Max(0, (bayesfilter - 0.1f));

        GetComponent <Rigidbody2D>().position = new Vector2(0, barHeight);
        //Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        //GetComponent<Rigidbody2D>().position = new Vector2(0, mousePosition.y);


        listToHoldtime.Add(Time.time);
        listToHoldrawEmg.Add(rawEmg);
        listToHoldaveragefilter.Add(averagefilter);
        listToHoldbutterworthfilter.Add(butterworthfilter);
        listToHoldbayesfilter.Add(bayesfilter);
    }
コード例 #2
0
ファイル: MoveRacket.cs プロジェクト: zhangzhuoz/Emgcontrol
    void FixedUpdate()
    {
        server.SendTo(Encoding.ASCII.GetBytes("H"), Remote);  //发送信息
        data       = new byte[1024];                          //对data清零
        recv       = server.ReceiveFrom(data, ref Remote);    //获取客户端,获取服务端端数据,用引用给服务端赋值,实际上服务端已经定义好并不需要赋值
        stringData = Encoding.ASCII.GetString(data, 0, recv); //字节数组转换为字符串  //输出接收到的数据
        Console.WriteLine(stringData);



        float v         = Input.GetAxisRaw("Vertical");
        float barHeight = 0.03f * Convert.ToInt32(stringData) - 0.1f;

        barHeight = barHeight * 0.25f;



        //float init_data = myMode.ChooseMode("real");
        init_data = ChooseMode("real");

        if (init_data == 0)
        {
            bayesfilter = 0;
        }
        else
        {
            //bayesfilter = (float)(myBayesian.UpdateEst(init_data/ 100 ));
            bayesfilter = (float)(myBayesian.UpdateEst(init_data * 80000 / 25));//200
        }
        init_data = init_data * 80000;



        //emgfilter = EmgAverage(10,Math.Abs(myEmg.emgData[0]));


        //emgfilter = EmgFilter(myEmg.emgData[0]);
        emgfilter = EmgFilter(init_data);
        //emgfilter = emgfilter * 50000;

        GetComponent <Rigidbody2D>().position = new Vector2(0, barHeight);
        //GetComponent<Rigidbody2D>().position = new Vector2(0, bayesfilter*50);

        //obj.transform.position = new Vector2(0, barHeight);
        //print(barHeight*1000000);

        listToHoldData.Add(barHeight);
        //float t = Time.time;
        listToHoldTime.Add(Time.time);

        listToHoldInit.Add(init_data);
        listToHoldemgfilter.Add(emgfilter);
        listToHoldbayesfilter.Add(bayesfilter);
        listToHoldstoredata.Add(init_data);
        //Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        //GetComponent<Rigidbody2D>().position = new Vector2(0, mousePosition.y);
    }
コード例 #3
0
    void FixedUpdate()         // 设为0.01s
    {
        float rawEmg = 0.0f;

        IDrecord_f = TargetRacket_force.ID;
        Lce        = MoveRacket_force.Lce_mo;

        if (TestClick.flag)
        {
            if (EnterClick.verify)
            {
                input1            = float.Parse(inputvariate1.text); // string to float(字符串转浮点型)
                input2            = float.Parse(inputvariate2.text);
                EnterClick.verify = false;
            }
            //print (input1);
            //print (input2);

            //print ("rawEmg");
            rawEmg = ChooseMode("real");

            while (Queue.TryDequeue(out rawEmg))
            {
                bayesfilter = myBayesFilter.UpdateEst(Math.Abs(rawEmg) * 6e2f);
                //			Debug.Log(bayesfilter.ToString());
            }



            averagefilter     = EmgAverage(10, Math.Abs(rawEmg * 80000 / 40));    //均值滤波
            butterworthfilter = ButterworthFilter(rawEmg * 80000 / 4);            //巴特沃斯滤波
            emg_send          = Mathf.Max(0, bayesfilter * input1 + input2);      //butterworthfilter    bayesfilter



            //for (double Lce = 1.0; Lce < 2.0; Lce += 0.01)
            try
            {
                //Lce = Math.Sin(n * Math.PI / 180);    // n= angle
                //n++;
                //Lce = Math.Abs(Lce) + 1;
                //float Lce1 = (float)Lce;


                // Sends a message to the host to which you have connected.
//				Byte[] sendBytes1 = Encoding.ASCII.GetBytes("H");
//				acq_pressureClient.Send(sendBytes1, sendBytes1.Length);
                // Blocks until a message returns on this socket from a remote host.
//				Byte[] receiveBytes1 = acq_pressureClient.Receive(ref RemoteIpEndPoint1);
//				string recMsg1 = Encoding.ASCII.GetString(receiveBytes1);  //receive message from DAQ
//				barForceInMilliNewton = float.Parse(recMsg1);

//				//			Lce = k * barForceInMilliNewton + b;  //1~2   ,
//				Lce = 1 + barForceInMilliNewton/20000;

                Byte[] sendBytes2 = Encoding.ASCII.GetBytes(Lce.ToString());
                neuromorphicClient.Send(sendBytes2, sendBytes2.Length);                  //send spring length change to neuromorphic system
                // Blocks until a message returns on this socket from a remote host.
                Byte[] receiveBytes2 = neuromorphicClient.Receive(ref RemoteIpEndPoint2);
                string recMsg2       = Encoding.ASCII.GetString(receiveBytes2); //receive message from neuromorphic system
                muscle_force = float.Parse(recMsg2);                            //motorCommand-muscle_force     muscle_force——0~18

                neuromorphic_on_send  = (int)((2000 - emg_send * 50) - muscle_force * 80);
                neuromorphic_off_send = (int)((2000 - emg_send * 50) - Lce / 100);



                if (NeuromorphicClick.enter_neuromorphic)
                {
                    positioncur = neuromorphic_on_send;                             //send EMG + muscle force to linear motor
                }

                else
                {
                    positioncur = neuromorphic_off_send;                          //send (EMG + 0) or (EMG + feedback_number) to linear motor
                }

                if (positioncur >= 2000)
                {
                    positioncur = 2000;
                }
//				    print (positioncur);
            }


            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }


//			float barHeight = 0.008f * barForceInMilliNewton;
//			GetComponent<Rigidbody2D>().position = new Vector2(0, barHeight);

            GetComponent <Rigidbody2D>().position = new Vector2(30, emg_send);

            //Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
            //GetComponent<Rigidbody2D>().position = new Vector2(0, mousePosition.y);



            //Send something to Motor

            double positionangle1 = Math.Sin(n * Math.PI / 180) * 2000; //Write position
            n++;                                                        //n = n + 1;
            int position1 = (int)positionangle1;
            //position = Math.Abs(position);

            double positionangle2 = Math.Cos(n * Math.PI / 180) * 2000;
            n++;
            int position2 = (int)positionangle2;


            byte ID         = 0x01;
            byte Instuction = 0x02;
            byte MemAddr    = 0x37;
            //int position = 1300;
            byte positioncur_L = (byte)(positioncur & 0xFF);
            byte positioncur_H = (byte)(positioncur >> 8);
            byte position2_L   = (byte)(position2 & 0xFF);
            byte position2_H   = (byte)(position2 >> 8);
            byte nLen          = 0x02;

            byte msgLen    = (byte)(nLen + 0x02);
            byte CheckSum1 = (byte)(msgLen + ID + Instuction + MemAddr + positioncur_L + positioncur_H);
            byte CheckSum2 = (byte)(msgLen + ID + Instuction + MemAddr + position2_L + position2_H);

            byteBuffer1[0] = 0x55;                   //帧头
            byteBuffer1[1] = 0xAA;
            byteBuffer1[2] = msgLen;                 //帧长度
            byteBuffer1[3] = ID;
            byteBuffer1[4] = Instuction;
            byteBuffer1[5] = MemAddr;
            byteBuffer1[6] = positioncur_L;
            byteBuffer1[7] = positioncur_H;
            byteBuffer1[8] = CheckSum1;


            byteBuffer2[0] = 0x55;
            byteBuffer2[1] = 0xAA;
            byteBuffer2[2] = msgLen;
            byteBuffer2[3] = ID;
            byteBuffer2[4] = Instuction;
            byteBuffer2[5] = MemAddr;
            byteBuffer2[6] = position2_L;
            byteBuffer2[7] = position2_H;
            byteBuffer2[8] = CheckSum2;

            sp1.Write(byteBuffer1, 0, byteBuffer1.Length);
            System.Threading.Thread.Sleep(5);

            //		sp2.Write(byteBuffer2, 0, byteBuffer2.Length);
            //		System.Threading.Thread.Sleep(8);
        }



        if (TargetRacket_force.destroy_mark == false)
        {
            Levels[(int)IDrecord_f].allList(Time.time, IDrecord_f, rawEmg, averagefilter, butterworthfilter, bayesfilter, emg_send);
        }



        if (Input.GetKeyDown(KeyCode.Z))
        {
            cont_a = !cont_a;

            obj_2.GetComponent <Renderer>().enabled = cont_a;
            inputvariate1.gameObject.SetActive(!cont_a);
            inputvariate2.gameObject.SetActive(!cont_a);
        }
    }
コード例 #4
0
ファイル: MoveRacket.cs プロジェクト: minosniu/nersa
    void FixedUpdate()
    {
        if (TestClick.flag)
        {
            if (EnterClick.verify)
            {
                input1            = float.Parse(inputvariate1.text);         // string to float(字符串转浮点型)
                input2            = float.Parse(inputvariate2.text);
                EnterClick.verify = false;
            }
            //print (input1);
            //print (input2);


            //print ("emg");
            init_data = ChooseMode("real");

            if (init_data == 0)
            {
                bayesfilter = 0;
            }
            else
            //bayesfilter = (float)(myBayesian.UpdateEst(init_data / 100));
            {
                bayesfilter = (float)(myBayesian.UpdateEst(init_data * 80000 / 100));                //200    0-1
                //bayesfilter = Mathf.Max(0,(float)(myBayesian.UpdateEst(init_data * 80000 / input1)) + input2);// 200    0-1
                emg_send  = Mathf.Max(0, bayesfilter * input1 + input2);
                init_data = init_data * 80000;
            }

            //emgfilter = EmgAverage(10, Math.Abs(myEmg.emgData[0]));

            //emgfilter = EmgFilter(myEmg.emgData[0]);
            emgfilter = EmgFilter(init_data);
            emgfilter = emgfilter * 50000;

            try
            {
                // Sends a message to the host to which you have connected.
                Byte[] sendBytes = Encoding.ASCII.GetBytes(emg_send.ToString());

                nanoTecClient.Send(sendBytes, sendBytes.Length);

                // Blocks until a message returns on this socket from a remote host.
                Byte[] receiveBytes = nanoTecClient.Receive(ref RemoteIpEndPoint);
                string stringData   = Encoding.ASCII.GetString(receiveBytes);
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }


            //float barHeight = bayesfilter * input1 + input2;	//EMG信号条零点位置设置
            GetComponent <Rigidbody2D>().position = new Vector2(20, emg_send);

            //obj.transform.position = new Vector2(0, barHeight);
            //print(barHeight * 1000000);

            listToHoldemg_send.Add(emg_send);
            //float t = Time.time;
            listToHoldTime.Add(Time.time);

            listToHoldInit.Add(init_data);
            listToHoldemgfilter.Add(emgfilter);
            listToHoldbayesfilter.Add(bayesfilter);
            listToHoldstoredata.Add(init_data);
            //Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
            //GetComponent<Rigidbody2D>().position = new Vector2(0, mousePosition.y);
        }
    }