From bed585bd2f5b6cd25bf87dbbb53029034efdc5c5 Mon Sep 17 00:00:00 2001 From: xpeng <1216772231@qq.com> Date: Tue, 1 Feb 2022 17:48:59 +0100 Subject: [PATCH] Add Serial2 (only for startup) and add local motion --- Assets/Scenes/Main Scene.unity | 135 ++++++++++++++++++++++++++++++--- Assets/Scripts/Serial.cs | 24 ++---- Assets/Scripts/Serial2.cs | 89 ++++++++++++++++++++++ Assets/Scripts/ToggleMove.cs | 24 ++++++ 4 files changed, 246 insertions(+), 26 deletions(-) create mode 100644 Assets/Scripts/Serial2.cs create mode 100644 Assets/Scripts/ToggleMove.cs diff --git a/Assets/Scenes/Main Scene.unity b/Assets/Scenes/Main Scene.unity index 4de4d44..53ed0c7 100644 --- a/Assets/Scenes/Main Scene.unity +++ b/Assets/Scenes/Main Scene.unity @@ -216,6 +216,49 @@ Transform: m_Father: {fileID: 747929426} m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 90, y: 0, z: 0} +--- !u!1 &27972387 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 27972389} + - component: {fileID: 27972388} + m_Layer: 0 + m_Name: Serial2 + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &27972388 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 27972387} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: cf2aeed8b8c7c854d8c634497ae5d6bc, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!4 &27972389 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 27972387} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 8 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &141732345 GameObject: m_ObjectHideFlags: 0 @@ -1245,27 +1288,27 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 1018638153358675470, guid: ea6463ca199d52046962b69f2ab654ef, type: 3} propertyPath: m_LocalScale.x - value: 31 + value: 32 objectReference: {fileID: 0} - target: {fileID: 1018638153358675470, guid: ea6463ca199d52046962b69f2ab654ef, type: 3} propertyPath: m_LocalScale.y - value: 31 + value: 32 objectReference: {fileID: 0} - target: {fileID: 1018638153358675470, guid: ea6463ca199d52046962b69f2ab654ef, type: 3} propertyPath: m_LocalScale.z - value: 30.5 + value: 32 objectReference: {fileID: 0} - target: {fileID: 1018638153358675470, guid: ea6463ca199d52046962b69f2ab654ef, type: 3} propertyPath: m_LocalPosition.x - value: -0 + value: 0 objectReference: {fileID: 0} - target: {fileID: 1018638153358675470, guid: ea6463ca199d52046962b69f2ab654ef, type: 3} propertyPath: m_LocalPosition.y - value: 1.163 + value: 1.1709 objectReference: {fileID: 0} - target: {fileID: 1018638153358675470, guid: ea6463ca199d52046962b69f2ab654ef, type: 3} propertyPath: m_LocalPosition.z - value: 0.019 + value: 0.0167 objectReference: {fileID: 0} - target: {fileID: 1121748732791834861, guid: ea6463ca199d52046962b69f2ab654ef, type: 3} propertyPath: m_Enabled @@ -3300,6 +3343,10 @@ GameObject: m_Component: - component: {fileID: 1647048264} - component: {fileID: 1647048263} + - component: {fileID: 1647048267} + - component: {fileID: 1647048266} + - component: {fileID: 1647048265} + - component: {fileID: 1647048268} m_Layer: 0 m_Name: XR Origin m_TagString: Untagged @@ -3322,7 +3369,7 @@ MonoBehaviour: m_Camera: {fileID: 371544751} m_OriginBaseGameObject: {fileID: 1647048262} m_CameraFloorOffsetObject: {fileID: 281366679} - m_RequestedTrackingOriginMode: 0 + m_RequestedTrackingOriginMode: 2 m_CameraYOffset: 1.7 --- !u!4 &1647048264 Transform: @@ -3332,15 +3379,83 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1647048262} m_LocalRotation: {x: 0, y: -0.7071068, z: 0, w: 0.7071068} - m_LocalPosition: {x: -0.58, y: 0, z: -0} + m_LocalPosition: {x: -0.5, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: - {fileID: 281366680} - {fileID: 141732346} - {fileID: 367081678} m_Father: {fileID: 0} - m_RootOrder: 0 + m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: -90, z: 0} +--- !u!114 &1647048265 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1647048262} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1ac6916bc3db4214dbe74106e4ad724b, type: 3} + m_Name: + m_EditorClassIdentifier: + m_System: {fileID: 1647048267} + m_TurnSpeed: 30 + m_InputBinding: 0 + m_Controllers: + - {fileID: 141732347} + m_DeadzoneMin: 0.125 + m_DeadzoneMax: 0.925 +--- !u!114 &1647048266 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1647048262} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 5aca1fc346609b54dbb0adba4664854b, type: 3} + m_Name: + m_EditorClassIdentifier: + m_System: {fileID: 1647048267} + m_MoveSpeed: 0.5 + m_EnableStrafe: 1 + m_UseGravity: 1 + m_GravityApplicationMode: 0 + m_ForwardSource: {fileID: 0} + m_InputBinding: 0 + m_Controllers: + - {fileID: 367081679} + m_DeadzoneMin: 0.125 + m_DeadzoneMax: 0.925 +--- !u!114 &1647048267 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1647048262} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 03a5df2202a8b96488c744be3bd0c33e, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Timeout: 10 + m_XROrigin: {fileID: 1647048263} +--- !u!114 &1647048268 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1647048262} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ba164c8c2f2472547a03f77a0ad5aeed, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &1900118868 GameObject: m_ObjectHideFlags: 0 @@ -3382,7 +3497,7 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 1 + m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &2000481166 stripped GameObject: diff --git a/Assets/Scripts/Serial.cs b/Assets/Scripts/Serial.cs index 06b22fd..3e42d33 100644 --- a/Assets/Scripts/Serial.cs +++ b/Assets/Scripts/Serial.cs @@ -9,8 +9,6 @@ public class Serial : MonoBehaviour byte[] incomPacket = new byte[6]; byte[] settingPacket = new byte[6]; static byte[] touchPacket = new byte[9]; - static byte[] touchPacketReset = new byte[9]; - static bool startUp = true; //use ture for default start up state to prevent restart game float timer = 0; bool failed = false; @@ -18,31 +16,25 @@ public class Serial : MonoBehaviour void Start() { - SerialStartUp(); + settingPacket[0] = 40; + settingPacket[5] = 41; + touchPacket[0] = 40; + touchPacket[8] = 41; + p1Serial.Open(); + Debug.Log("Serial Started"); } void Update() { ReadPack(); if (!failed) - TouchSetUp(); - //SendTouch(); + TouchSetUp(); } + void FixedUpdate() { SendTouch(); } - private void SerialStartUp() - { - settingPacket[0] = 40; - settingPacket[5] = 41; - touchPacket[0] = 40; - touchPacket[8] = 41; - touchPacketReset[0] = 40; - touchPacketReset[8] = 41; - p1Serial.Open(); - Debug.Log("Serial Started"); - } private void TouchSetUp() { diff --git a/Assets/Scripts/Serial2.cs b/Assets/Scripts/Serial2.cs new file mode 100644 index 0000000..b33a8dd --- /dev/null +++ b/Assets/Scripts/Serial2.cs @@ -0,0 +1,89 @@ +using System.Collections; +using UnityEngine; +using System.IO.Ports; +using System; +public class Serial2 : MonoBehaviour +{ + static SerialPort p1Serial = new SerialPort ("COM6", 9600); + int packleng = 0; + byte[] incomPacket = new byte[6]; + byte[] settingPacket = new byte[6]; + static byte[] touchPacket = new byte[9]; + static bool startUp = true; //use ture for default start up state to prevent restart game + float timer = 0; + bool failed = false; + byte recivData; + void Start() + { + settingPacket[0] = 40; + settingPacket[5] = 41; + touchPacket[0] = 40; + touchPacket[8] = 41; + p1Serial.Open(); + Debug.Log("Serial2 Started"); + } + + void Update() + { + ReadPack(); + if (!failed) + TouchSetUp(); + } + void FixedUpdate() + { + //SendTouch(); //Not send touch to p2 port + } + + private void TouchSetUp() + { + switch (incomPacket[3]) + { + case 76: + case 69: + startUp = false; + break; + case 114: + case 107: + for (int i=1; i<5; i++) + settingPacket[i] = incomPacket[i]; + p1Serial.Write(settingPacket, 0, settingPacket.Length); + Array.Clear(incomPacket, 0, incomPacket.Length); + break; + case 65: + startUp = true; + break; + } + } + + private void ReadPack() + { + timer = 0f; + if (p1Serial.BytesToRead == 6) + { + packleng = 0; + while (packleng < 6) + { + recivData = Convert.ToByte(p1Serial.ReadByte()); + if (recivData == 123) + { + packleng = 0; + } + incomPacket[packleng++] = recivData; + if(timer > 20f){ failed = true; break; } + timer += Time.deltaTime; + } + } + } + + static void SendTouch() + { + if (startUp) + p1Serial.Write(touchPacket, 0, 9); + } + + static void ChangeTouch(int Area, bool State) + { + if (startUp) + ByteArrayExt.SetBit(touchPacket, Area+8, State); + } +} \ No newline at end of file diff --git a/Assets/Scripts/ToggleMove.cs b/Assets/Scripts/ToggleMove.cs new file mode 100644 index 0000000..e4d5ce9 --- /dev/null +++ b/Assets/Scripts/ToggleMove.cs @@ -0,0 +1,24 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.XR.Interaction.Toolkit; + +public class ToggleMove : MonoBehaviour +{ + void Update() + { + if (Input.GetKeyDown(KeyCode.M)) + { + if (GetComponent().enabled) + { + GetComponent().enabled = false; + GetComponent().enabled = false; + } + else + { + GetComponent().enabled = true; + GetComponent().enabled = true; + } + } + } +}