123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Wit.SDK.Modular.Sensor.Device;
- using Wit.SDK.Modular.Sensor.Modular.DataProcessor.Constant;
- public class DeviceItem : MonoBehaviour
- {
- public string id;
- public BWT901BLE dm;
- public bool isX;
- public bool isY;
- public bool isZ;
- public List<float> initV3=new List<float>();
- // Start is called before the first frame update
- void Start()
- {
- reset();
- dm = new BWT901BLE();
- dm.SetDeviceAddress(id);
- dm.SetDeviceName("WT901BLE67");
- dm.OnUpdate += BWT901BLE_OnUpdate;
-
- }
- Queue<BWT901BLE> bw = new Queue<BWT901BLE>();
- Quaternion qr = Quaternion.Euler(0, 0, 0);
- private void BWT901BLE_OnUpdate(BWT901BLE BWT901BLE)
- {
- // UnityEngine.Debug.Log("BWT901BLE_OnUpdate==>");
- DisplayData(BWT901BLE);
- }
- Queue<sx> q = new Queue<sx>();
- /// <summary>
- /// ����չʾ
- /// Data display
- /// </summary>
- /// <param name="bWT901BLE"></param>
- private void DisplayData(BWT901BLE bWT901BLE)
- {
- sx sxx = new sx();
- sxx.xx = bWT901BLE.GetDeviceData(WitSensorKey.AsX);
- sxx.yy = bWT901BLE.GetDeviceData(WitSensorKey.AsY);
- sxx.zz = bWT901BLE.GetDeviceData(WitSensorKey.AsZ);
- q.Enqueue(sxx);
- }
- public void reset()
- {
- qr = Quaternion.Euler(0,0,0);
- }
- class sx
- {
- public string xx;
- public string yy;
- public string zz;
- }
- private void OnDestroy()
- {
- if (dm != null)
- dm.Close();
- }
- public bool isxb;
- Vector3 v3;
- bool isStart;
- float f = 0;
- private void Update()
- {
- if(q.Count>0)
- {
- int ct = q.Count;
- for (int i = 0; i < ct; i++)
- {
- sx s = q.Dequeue();
- UnityEngine.Vector3 av = new UnityEngine.Vector3(-(float.Parse(s.zz)), (float.Parse(s.yy)), (float.Parse(s.xx)));
- UnityEngine.Quaternion dr = UnityEngine.Quaternion.Euler(av / 10);
- qr = qr * dr;
- float xx = 0;
- float yy = 0;
- float zz = 0;
- if (isX)
- {
- xx = qr.eulerAngles.x;
- }
- if (isY)
- {
- yy = qr.eulerAngles.y;
- }
- if (isZ)
- {
- zz = qr.eulerAngles.z;
- }
- if(isxb)
- {
- this.transform.localEulerAngles = new Vector3(zz, xx, yy) + new Vector3(initV3[0], initV3[1], initV3[2]);
- }
- else
- {
- this.transform.localEulerAngles = new Vector3(xx, yy, zz) + new Vector3(initV3[0], initV3[1], initV3[2]);
- }
- }
- }
- if (!isStart)
- {
- Debug.Log(id + "��ʼ����");
- dm?.Open();
- f = 0;
- isStart = true;
- v3 = this.transform.localRotation.eulerAngles;
- }
- f = f + Time.deltaTime;
- if (f > 10)
- {
- if (this.transform.localRotation.eulerAngles == v3)
- {
- isStart = false;
- dm.Close();
- Debug.Log(id + "�Ͽ�����");
- dm.OnUpdate -= BWT901BLE_OnUpdate;
- dm = new BWT901BLE();
- dm.SetDeviceAddress(id);
- dm.SetDeviceName("WT901BLE67");
- dm.OnUpdate += BWT901BLE_OnUpdate;
- f = 0;
- }
- else
- {
- isStart = true;
- v3 = this.transform.localRotation.eulerAngles;
- f = 0;
- }
- }
- }
- }
|