|
|
@@ -4,6 +4,8 @@ using System.Collections.Generic;
|
|
|
using UnityEngine;
|
|
|
using UnityEngine.UI;
|
|
|
using ArduinoBluetoothAPI;
|
|
|
+using DG.Tweening;
|
|
|
+using UnityEngine.SceneManagement;
|
|
|
using System.Linq;
|
|
|
using BestHTTP.WebSocket;
|
|
|
|
|
|
@@ -13,7 +15,7 @@ public class ShootCheck : MonoBehaviour
|
|
|
CMD cmd = new CMD();
|
|
|
bool locked = false;
|
|
|
float maxAcc = 0;
|
|
|
- Queue<float> keyAccList = new Queue<float>();
|
|
|
+ Queue<Vector3> keyAccList = new Queue<Vector3>();
|
|
|
Queue<string> keyTimeList = new Queue<string>();
|
|
|
public float shootSpeed;
|
|
|
public static ShootCheck ins;
|
|
|
@@ -23,6 +25,11 @@ public class ShootCheck : MonoBehaviour
|
|
|
{
|
|
|
ins = this;
|
|
|
BluetoothDispatcher.shoot = OnDataReceived;
|
|
|
+ //debug
|
|
|
+ if (Application.platform == RuntimePlatform.WindowsEditor)
|
|
|
+ {
|
|
|
+ transportMode = 1;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//用户输入时的变化
|
|
|
@@ -111,12 +118,48 @@ public class ShootCheck : MonoBehaviour
|
|
|
{
|
|
|
ArmBow.ins.shootBackTime=int.Parse(ArmBowInputField.text);
|
|
|
}
|
|
|
- public void OnBluetoothReady(BluetoothShoot bluetoothShoot) {
|
|
|
+ public void OnBluetoothReady(BluetoothShoot bluetoothShoot)
|
|
|
+ {
|
|
|
bluetoothShoot.WriteData(JsonUtility.ToJson(cmd).Replace("\"", ""));
|
|
|
- // bluetoothShoot.WriteData("T");
|
|
|
+
|
|
|
+ Sequence sequence = DOTween.Sequence();
|
|
|
+ sequence.PrependInterval(1).AppendCallback(delegate() {
|
|
|
+ canAdjustNormalOrHightMode = true;
|
|
|
+ transportMode = 0;
|
|
|
+ AdjustNormalOrHightMode();
|
|
|
+ });
|
|
|
+ sequence.SetUpdate(true);
|
|
|
}
|
|
|
|
|
|
- public void OnDataReceived(byte[] bytes) {
|
|
|
+ //===普通模式和高速模式的切换===
|
|
|
+ public bool canAdjustNormalOrHightMode = false;
|
|
|
+ public int transportMode = 0;
|
|
|
+ public void AdjustNormalOrHightMode()
|
|
|
+ {
|
|
|
+ if (!canAdjustNormalOrHightMode) return;
|
|
|
+ try {
|
|
|
+ string sceneName = SceneManager.GetActiveScene().name;
|
|
|
+ if (sceneName == "Game")
|
|
|
+ {
|
|
|
+ if (transportMode == 0)
|
|
|
+ {
|
|
|
+ transportMode = 1;
|
|
|
+ BluetoothShoot.ins.WriteData("HA");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (transportMode == 1)
|
|
|
+ {
|
|
|
+ transportMode = 0;
|
|
|
+ BluetoothShoot.ins.WriteData("NA");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception) {}
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnDataReceived(byte[] bytes)
|
|
|
+ {
|
|
|
if (bytes.Length == 2)
|
|
|
{
|
|
|
DeviceBatteryView.ins.RenderBattery(2, bytes[0]);
|
|
|
@@ -133,20 +176,25 @@ public class ShootCheck : MonoBehaviour
|
|
|
// webSocket.Send(str2);
|
|
|
}
|
|
|
|
|
|
- string str2 = "";
|
|
|
+ string str2 = "";
|
|
|
|
|
|
- for (int i = 0; i < (bytes.Length-2)/6; i++)
|
|
|
+ if (transportMode == 1)
|
|
|
{
|
|
|
- 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])+"毫秒)" ;
|
|
|
- str2 += "加速度:"+acc+t+"\n";
|
|
|
-
|
|
|
- if (ins.check(acc,t) && ArmBow.ins)
|
|
|
+ for (int i = 0; i < bytes.Length / 11; i++)
|
|
|
{
|
|
|
- ArmBow.ins.ADS_fire();
|
|
|
- }
|
|
|
- }
|
|
|
+ float ax = ToAcceleratedSpeed(bytes[i * 11 + 5], bytes[i * 11 + 6]);
|
|
|
+ float ay = ToAcceleratedSpeed(bytes[i * 11 + 7], bytes[i * 11 + 8]);
|
|
|
+ float az = ToAcceleratedSpeed(bytes[i * 11 + 9], bytes[i * 11 + 10]);
|
|
|
+
|
|
|
+ string t = "(采样时间:"+(int)bytes[i * 6 + 3] + "分"+ (int)bytes[i * 6 + 4]+"秒"+ TwoByteToInt(bytes[i * 6 + 1], bytes[i * 6 + 2])+"毫秒)" ;
|
|
|
+ str2 += "加速度:"+ax+t+"\n";
|
|
|
+
|
|
|
+ if (ins.check(ax, ay, az, t) && ArmBow.ins)
|
|
|
+ {
|
|
|
+ ArmBow.ins.ADS_fire();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
if (webSocket != null)
|
|
|
{
|
|
|
@@ -168,8 +216,9 @@ public class ShootCheck : MonoBehaviour
|
|
|
return (int)shortNum;
|
|
|
}
|
|
|
|
|
|
- bool check(float acc,string t)
|
|
|
+ bool check(float ax, float ay, float az, string t)
|
|
|
{
|
|
|
+ float acc = ay;
|
|
|
DebugLine.show(acc); //这个不需要注释,静态函数内置判断
|
|
|
if (locked)
|
|
|
{
|
|
|
@@ -195,7 +244,8 @@ public class ShootCheck : MonoBehaviour
|
|
|
double y = 1.0 / (p1+p2*Mathf.Pow(x, 0.5f)*Mathf.Log(x)+p3/Mathf.Pow(x, 0.5f));
|
|
|
acc = (float) y;
|
|
|
}
|
|
|
- keyAccList.Enqueue(acc);
|
|
|
+ Vector3 keyAcc = new Vector3(ax, ay, az);
|
|
|
+ keyAccList.Enqueue(keyAcc);
|
|
|
keyTimeList.Enqueue(t);
|
|
|
return false;
|
|
|
}
|
|
|
@@ -205,40 +255,57 @@ public class ShootCheck : MonoBehaviour
|
|
|
float lasKeytAcc = 0;
|
|
|
int keyAccIndex = 0;
|
|
|
float timeInterval = 0.002f;
|
|
|
+ float totalAx = 0;
|
|
|
+ float totalAy = 0;
|
|
|
+ float totalAz = 0;
|
|
|
+ Debug.LogWarning("=============keyAcc===========");
|
|
|
foreach (var keyAcc in keyAccList)
|
|
|
{
|
|
|
+ totalAx += Mathf.Abs(keyAcc.x);
|
|
|
+ totalAy += Mathf.Abs(keyAcc.y);
|
|
|
+ totalAz += Mathf.Abs(keyAcc.z);
|
|
|
if (keyAccIndex > 0)
|
|
|
{
|
|
|
- shootSpeed += keyAcc * timeInterval;
|
|
|
- shootSpeed -= (keyAcc - lasKeytAcc) * timeInterval / 2;
|
|
|
+ shootSpeed += keyAcc.y * timeInterval;
|
|
|
+ shootSpeed -= (keyAcc.y - lasKeytAcc) * timeInterval / 2;
|
|
|
}
|
|
|
else if (keyAccIndex == 0 && keyAccList.Count == 1)
|
|
|
{
|
|
|
- shootSpeed = keyAcc * timeInterval;
|
|
|
+ shootSpeed = keyAcc.y * timeInterval;
|
|
|
}
|
|
|
- lasKeytAcc = keyAcc;
|
|
|
+ lasKeytAcc = keyAcc.y;
|
|
|
keyAccIndex++;
|
|
|
+ Debug.LogWarning(keyAcc);
|
|
|
}
|
|
|
- //加速度acc的单位是g,最后需要乘上
|
|
|
- shootSpeed *= 9.80665f;
|
|
|
- //积分出来的值还是太小,需要一个倍率
|
|
|
- shootSpeed *= 20;
|
|
|
- string strShootSpeed = "初速度: " + shootSpeed + " 帧数: " + keyAccList.Count + "\n";
|
|
|
- Debug.LogWarning(strShootSpeed);
|
|
|
-
|
|
|
- string str1 = strShootSpeed + "/////////检测到射出的数据////////////:\n";
|
|
|
- for (int i=0;i<keyAccList.Count;i++)
|
|
|
+ Debug.LogWarning("---------keyAcc---------");
|
|
|
+
|
|
|
+ //是不是合法射出
|
|
|
+ bool isLegalShoot = totalAy > totalAx && totalAy > totalAz;
|
|
|
+
|
|
|
+ if (isLegalShoot)
|
|
|
{
|
|
|
- float keyAcc = keyAccList.ElementAt(i);
|
|
|
- string time = keyTimeList.ElementAt(i);
|
|
|
-
|
|
|
- str1 += "加速度:"+keyAcc+time+"\n";
|
|
|
- }
|
|
|
- Debug.LogWarning(str1);
|
|
|
- if (webSocket != null)
|
|
|
- {
|
|
|
- webSocket.Send(str1+"/////////检测到射出的数据////////////:\n");
|
|
|
+ //加速度acc的单位是g,最后需要乘上
|
|
|
+ shootSpeed *= 9.80665f;
|
|
|
+ //积分出来的值还是太小,需要一个倍率
|
|
|
+ shootSpeed *= 20;
|
|
|
+ string strShootSpeed = "初速度: " + shootSpeed + " 帧数: " + keyAccList.Count + "\n";
|
|
|
+ Debug.LogWarning(strShootSpeed);
|
|
|
+
|
|
|
+ string str1 = strShootSpeed + "/////////检测到射出的数据////////////:\n";
|
|
|
+ for (int i=0;i<keyAccList.Count;i++)
|
|
|
+ {
|
|
|
+ float keyAcc = keyAccList.ElementAt(i).y;
|
|
|
+ string time = keyTimeList.ElementAt(i);
|
|
|
+
|
|
|
+ str1 += "加速度:"+keyAcc+time+"\n";
|
|
|
+ }
|
|
|
+ Debug.LogWarning(str1);
|
|
|
+ if (webSocket != null)
|
|
|
+ {
|
|
|
+ webSocket.Send(str1+"/////////检测到射出的数据////////////:\n");
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
//本轮计算结束
|
|
|
keyAccList.Clear();
|
|
|
keyTimeList.Clear();
|
|
|
@@ -246,7 +313,7 @@ public class ShootCheck : MonoBehaviour
|
|
|
maxAcc = 0;
|
|
|
Dolock();
|
|
|
Invoke("Unlock", 1.8f);
|
|
|
- return true;
|
|
|
+ return isLegalShoot;
|
|
|
}
|
|
|
|
|
|
return false;
|