|
|
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
|
|
using UnityEngine;
|
|
|
using UnityEngine.UI;
|
|
|
using ArduinoBluetoothAPI;
|
|
|
+using System.Linq;
|
|
|
using BestHTTP.WebSocket;
|
|
|
|
|
|
public class ShootCheck : MonoBehaviour
|
|
|
@@ -13,6 +14,7 @@ public class ShootCheck : MonoBehaviour
|
|
|
bool locked = false;
|
|
|
float maxAcc = 0;
|
|
|
Queue<float> keyAccList = new Queue<float>();
|
|
|
+ Queue<string> keyTimeList = new Queue<string>();
|
|
|
public float shootSpeed;
|
|
|
public static ShootCheck ins;
|
|
|
[SerializeField] InputField ipInputField = default;
|
|
|
@@ -39,7 +41,7 @@ public class ShootCheck : MonoBehaviour
|
|
|
//socket
|
|
|
string ipStr = ipInputField.text;//ipInputField.GetComponentInChildren<Text>();
|
|
|
string serverIP = ipStr;
|
|
|
- // serverIP = "192.168.1.109";
|
|
|
+ //serverIP = "172.16.20.57";
|
|
|
string address = "ws://" + serverIP + ":8088/Ble/";
|
|
|
webSocket = new WebSocket(new Uri(address));
|
|
|
#if !UNITY_WEBGL
|
|
|
@@ -104,14 +106,14 @@ public class ShootCheck : MonoBehaviour
|
|
|
|
|
|
|
|
|
[SerializeField] InputField ArmBowInputField = default;
|
|
|
- public Text shootAngleText;
|
|
|
|
|
|
public void SetShootBackTime()
|
|
|
{
|
|
|
ArmBow.ins.shootBackTime=int.Parse(ArmBowInputField.text);
|
|
|
}
|
|
|
public void OnBluetoothReady(BluetoothShoot bluetoothShoot) {
|
|
|
- bluetoothShoot.WriteData(JsonUtility.ToJson(cmd).Replace("\"", ""));
|
|
|
+ // bluetoothShoot.WriteData(JsonUtility.ToJson(cmd).Replace("\"", ""));
|
|
|
+ bluetoothShoot.WriteData("T");
|
|
|
}
|
|
|
|
|
|
public void OnDataReceived(byte[] bytes) {
|
|
|
@@ -120,30 +122,37 @@ public class ShootCheck : MonoBehaviour
|
|
|
DeviceBatteryView.ins.RenderBattery(2, bytes[0]);
|
|
|
return;
|
|
|
}
|
|
|
+ string str1 = "byte=";
|
|
|
+ if (webSocket != null)
|
|
|
+ {
|
|
|
+ for (int i = 0; i < bytes.Length-1; i++)
|
|
|
+ {
|
|
|
+ str1+= bytes[i];
|
|
|
+ }
|
|
|
+
|
|
|
+ // webSocket.Send(str2);
|
|
|
+ }
|
|
|
|
|
|
- string str = "";
|
|
|
+ string str2 = "";
|
|
|
|
|
|
for (int i = 0; i < (bytes.Length-2)/6; i++)
|
|
|
{
|
|
|
float acc = ToAcceleratedSpeed(bytes[i * 6 + 5], bytes[i * 6 + 6]);
|
|
|
|
|
|
string t = "(采样时间:"+(int)bytes[i * 6 + 3] + "分"+ (int)bytes[i * 6 + 4]+"秒"+ TwoByteToInt(bytes[i * 6 + 1], bytes[i * 6 + 2])+"毫秒)" ;
|
|
|
- str += "加速度:"+acc+t+"\n";
|
|
|
-
|
|
|
- if (webSocket != null)
|
|
|
- {
|
|
|
- webSocket.Send(str);
|
|
|
- }
|
|
|
-
|
|
|
- if (ins.check(acc) && ArmBow.ins)
|
|
|
+ str2 += "加速度:"+acc+t+"\n";
|
|
|
+
|
|
|
+ if (ins.check(acc,t) && ArmBow.ins)
|
|
|
{
|
|
|
- ArmBow.ins.ADS_fire();
|
|
|
- // if (webSocket != null)
|
|
|
- // {
|
|
|
- // webSocket.Send(str);
|
|
|
- // }
|
|
|
+ ArmBow.ins.ADS_fire();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ if (webSocket != null)
|
|
|
+ {
|
|
|
+ string str3 = str1+"\n"+str2;
|
|
|
+ webSocket.Send(str3);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
float ToAcceleratedSpeed(byte b1, byte b2)
|
|
|
@@ -159,7 +168,7 @@ public class ShootCheck : MonoBehaviour
|
|
|
return (int)shortNum;
|
|
|
}
|
|
|
|
|
|
- bool check(float acc)
|
|
|
+ bool check(float acc,string t)
|
|
|
{
|
|
|
DebugLine.show(acc); //这个不需要注释,静态函数内置判断
|
|
|
if (locked)
|
|
|
@@ -173,6 +182,7 @@ public class ShootCheck : MonoBehaviour
|
|
|
maxAcc = acc;
|
|
|
}
|
|
|
keyAccList.Enqueue(acc);
|
|
|
+ keyTimeList.Enqueue(t);
|
|
|
return false;
|
|
|
}
|
|
|
else if (acc < cmd.getAcc() && maxAcc != 0) {
|
|
|
@@ -180,12 +190,17 @@ public class ShootCheck : MonoBehaviour
|
|
|
shootSpeed = 0;
|
|
|
float lasKeytAcc = 0;
|
|
|
int keyAccIndex = 0;
|
|
|
+ float timeInterval = 0.01f;
|
|
|
foreach (var keyAcc in keyAccList)
|
|
|
{
|
|
|
if (keyAccIndex > 0)
|
|
|
{
|
|
|
- shootSpeed += keyAcc * 0.002f;
|
|
|
- shootSpeed -= (keyAcc - lasKeytAcc) * 0.002f / 2;
|
|
|
+ shootSpeed += keyAcc * timeInterval;
|
|
|
+ shootSpeed -= (keyAcc - lasKeytAcc) * timeInterval / 2;
|
|
|
+ }
|
|
|
+ else if (keyAccIndex == 0 && keyAccList.Count == 1)
|
|
|
+ {
|
|
|
+ shootSpeed = keyAcc * timeInterval;
|
|
|
}
|
|
|
lasKeytAcc = keyAcc;
|
|
|
keyAccIndex++;
|
|
|
@@ -193,10 +208,27 @@ public class ShootCheck : MonoBehaviour
|
|
|
//加速度acc的单位是g,最后需要乘上
|
|
|
shootSpeed *= 9.80665f;
|
|
|
//积分出来的值还是太小,需要一个倍率
|
|
|
- shootSpeed *= 10;
|
|
|
+ shootSpeed *= 30;
|
|
|
Debug.LogWarning("初速度: " + shootSpeed + " 帧数: " + keyAccList.Count);
|
|
|
+
|
|
|
+
|
|
|
+ string str1 = "检测到射出的数据:\n";
|
|
|
+ for (int i=0;i<keyAccList.Count;i++)
|
|
|
+ {
|
|
|
+ float keyAcc = keyAccList.ElementAt(i);
|
|
|
+ string time = keyTimeList.ElementAt(i);
|
|
|
+
|
|
|
+ str1 += "加速度:"+keyAcc+time+"\n";
|
|
|
+ }
|
|
|
+ Debug.LogWarning(str1);
|
|
|
+ if (webSocket != null)
|
|
|
+ {
|
|
|
+ webSocket.Send(str1);
|
|
|
+ }
|
|
|
//本轮计算结束
|
|
|
keyAccList.Clear();
|
|
|
+ keyTimeList.Clear();
|
|
|
+
|
|
|
maxAcc = 0;
|
|
|
Dolock();
|
|
|
Invoke("Unlock", 1.8f);
|