This commit is contained in:
yzx 2023-12-22 10:50:53 +08:00
parent 984bc9a853
commit 7ea6ef5529
12 changed files with 9215 additions and 4890 deletions

File diff suppressed because it is too large Load Diff

View File

@ -38,12 +38,12 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
m_ObjectHideFlags: 0
serializedVersion: 11
serializedVersion: 12
m_GIWorkflowMode: 1
m_GISettings:
serializedVersion: 2
@ -98,7 +98,7 @@ LightmapSettings:
m_TrainingDataDestination: TrainingData
m_LightProbeSampleCountMultiplier: 4
m_LightingDataAsset: {fileID: 0}
m_UseShadowmask: 1
m_LightingSettings: {fileID: 0}
--- !u!196 &4
NavMeshSettings:
serializedVersion: 2
@ -118,6 +118,8 @@ NavMeshSettings:
manualTileSize: 0
tileSize: 256
accuratePlacement: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
@ -131,6 +133,7 @@ GameObject:
m_Component:
- component: {fileID: 466370289}
- component: {fileID: 466370290}
- component: {fileID: 466370291}
m_Layer: 0
m_Name: APP
m_TagString: Untagged
@ -164,6 +167,18 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: beb73c429dba468f8a8c37893d1204b8, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &466370291
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 466370288}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 374ccbdc1a0c8d8478a9e49fd1215a18, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &634617318
GameObject:
m_ObjectHideFlags: 0
@ -323,6 +338,7 @@ Light:
m_UseColorTemperature: 0
m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0}
m_UseBoundingSphereOverride: 0
m_UseViewFrustumForShadowCasterCull: 1
m_ShadowRadius: 0
m_ShadowAngle: 0
--- !u!4 &815764611

View File

@ -22,62 +22,64 @@ namespace Script
public float xx;
float steerInput, throttleInput, brakeInput, handbrakeInput;
private ModbusTcpClient client;
private CarStatusData carData;
float minValue = -300f; // 范围的最小值
float maxValue = 300; // 范围的最大值
public CarInfoManager carInfoManager;
void Start()
{
vehicleController = GetComponent<VehicleController>();
vehicleStandardInput = GetComponent<VehicleStandardInput>();
StartModbus();
// StartModbus();
ModBusQueue();
}
async Task StartModbus()
{
client = new ModbusTcpClient();
await client.ConnectToServer();
while (true)
{
await client.SendModbusRequest();
// async Task StartModbus()
// {
// client = new ModbusTcpClient();
// await client.ConnectToServer();
// while (true)
// {
// await client.SendModbusRequest();
//
// await Task.Delay(TimeSpan.FromSeconds(.1));
// }
// }
await Task.Delay(TimeSpan.FromSeconds(.1));
}
}
void OnDestroy()
{
client.CloseConnection();
}
// void OnDestroy()
// {
// client.CloseConnection();
// }
async Task ModBusQueue()
{
while (true)
{
if (client.modbusQueue.Count > 0)
// Debug.Log(client.modbusQueue.Count);
if (ModbusTcpClient.modbusTcpClient.modbusQueue.Count > 0)
{
carData = client.modbusQueue.Dequeue();
// Debug.Log(carData.SteeringWheelAngle);
carData = ModbusTcpClient.modbusTcpClient.modbusQueue.Dequeue();
//方向盘
vehicleController.steerInput = NormalizeValue(carData.SteeringWheelAngle, minValue, maxValue);
//手刹
// vehicleController.throttleInput = carData.HandbrakeStatus == 0 ? 1 : 0;
//挡位 00空挡1前进档2倒挡3为P档
//当钥匙为点火状态,才能启动车
if (carData.IgnitionSwitch == 2)
{
switch (carData.GearPosition)
{
case 0:
break;
case 1://1前进档
case 1: //1前进档
if (carData.HandbrakeStatus == 0)
{
if (carData.AcceleratorPedalPosition > 0)
if (carData.AcceleratorPedalPosition > 30)
{
vehicleController.throttleInput = 1;
}
@ -92,10 +94,10 @@ namespace Script
}
break;
case 2://2倒挡
case 2: //2倒挡
if (carData.HandbrakeStatus == 0)
{
if (carData.AcceleratorPedalPosition > 0)
if (carData.AcceleratorPedalPosition > 30)
{
vehicleController.throttleInput = -0.5f;
}
@ -108,12 +110,17 @@ namespace Script
{
vehicleController.throttleInput = 0;
}
break;
case 3:
break;
}
}
//转向灯 00是未开转向灯1是左转向灯2是右转向灯
//当钥匙转为通电才能启动转向灯 0熄火1通电2启动
if (carData.IgnitionSwitch == 1)
{
switch (carData.TurnSignalStatus)
{
case 0:
@ -126,7 +133,7 @@ namespace Script
carInfoManager.RightStartBlinking();
break;
}
}
}
await Task.Delay(TimeSpan.FromSeconds(.1));
@ -144,8 +151,8 @@ namespace Script
//vehicleController.steerInput = 0;
vehicleController.brakeInput = 0;
vehicleController.handbrakeInput = 0;
// vehicleController.brakeInput = 0;
// vehicleController.handbrakeInput = 0;
//Debug.Log($"{steerInput}--{throttleInput}--{brakeInput}--{handbrakeInput}");
// vehicleStandardInput.SetThrottleValue(xx);

View File

@ -123,14 +123,13 @@ public class CarInfoManager : MonoBehaviour
#region
public void LeftStartBlinking()
{ Indicator.SetActive(false);
{
Indicator = leftIndicator;
StartBlinking(Indicator);
}
public void RightStartBlinking()
{
Indicator.SetActive(false);
Indicator = rightIndicator;
StartBlinking(Indicator);
}
@ -157,6 +156,10 @@ public class CarInfoManager : MonoBehaviour
// 停止闪烁的方法
public void StopBlinking()
{
if(!isBlinking)
return;
isBlinking = false;
if (cancellationTokenSource != null)
{

View File

@ -9,14 +9,14 @@ public class CarManager : MonoBehaviour
public GameObject puncture; //爆胎
public GameObject UIl;
private void OnTriggerEnter(Collider other)
{
if (other.name == "爆胎")
{
puncture.SetActive(true);
UIl.SetActive(true);
}
}
// private void OnTriggerEnter(Collider other)
// {
// if (other.name == "爆胎")
// {
// puncture.SetActive(true);
// UIl.SetActive(true);
// }
// }
// /// <summary>
// /// 爆胎

View File

@ -27,18 +27,49 @@ public class CarStatusData
namespace ModbusManager
{
public class ModbusTcpClient
public class ModbusTcpClient:MonoBehaviour
{
private TcpClient tcpClient;
private string serverIp = "172.16.1.125";
private int serverPort = 12315;
public Queue<CarStatusData> modbusQueue;
public ModbusTcpClient()
public static ModbusTcpClient modbusTcpClient;
private void Awake()
{
modbusTcpClient = this;
tcpClient = new TcpClient();
modbusQueue = new Queue<CarStatusData>();
}
private void Start()
{
StartModbus();
}
async Task StartModbus()
{
await ConnectToServer();
while (true)
{
await SendModbusRequest();
await Task.Delay(TimeSpan.FromSeconds(.1));
}
}
private void OnDestroy()
{
CloseConnection();
}
// public ModbusTcpClient()
// {
// tcpClient = new TcpClient();
// modbusQueue = new Queue<CarStatusData>();
// }
/// <summary>
/// 接口为TCP/IP接口。
/// 协议为MODBUS/TCP,数据为十六进制数据。
@ -74,7 +105,7 @@ namespace ModbusManager
if (stream.CanWrite)
{
await stream.WriteAsync(request, 0, request.Length);
//Debug.LogLine("Modbus请求已发送。");
// Debug.Log("Modbus请求已发送。");
await ReadResponse(stream);
}
}
@ -142,7 +173,7 @@ namespace ModbusManager
carStatusData.AcceleratorPedalPosition = dataValue;
break;
case 5://0-100,踩到底为100
//Debug.Log($"离合踏板数据: {dataValue}");
Debug.Log($"离合踏板数据: {dataValue}");
carStatusData.ClutchPedalPosition = dataValue;
break;
case 6://01表示手刹有效
@ -162,7 +193,7 @@ namespace ModbusManager
carStatusData.LightStatus = dataValue;
break;
case 10://00是未开转向灯1是左转向灯2是右转向灯
Debug.Log($"转向灯状态: {dataValue}");
// Debug.Log($"转向灯状态: {dataValue}");
carStatusData.TurnSignalStatus = dataValue;
break;
case 11://00是熄火1是通电2是点火
@ -170,7 +201,7 @@ namespace ModbusManager
carStatusData.KeyStatus = dataValue;
break;
case 12://上1,右2,下3左4
//Debug.Log($"按键数据: {dataValue}");
// Debug.Log($"按键数据: {dataValue}");
carStatusData.ButtonData = dataValue;
break;
// default:
@ -181,6 +212,8 @@ namespace ModbusManager
modbusQueue.Enqueue(carStatusData);
// Debug.Log(modbusQueue.Count);
}
else
{

View File

@ -1,6 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using ModbusManager;
using UnityEngine;
using UnityEngine.UI; // 如果您使用的是UGUI
@ -21,22 +23,31 @@ public class SteeringWheelController : MonoBehaviour
public float testFloatV;
float minValue = -300f; // 范围的最小值
float maxValue = 300; // 范围的最大值
private int NewValue = -1;
private void Awake()
{
Instance = this;
}
void Update()
private void Start()
{
// 更新方向盘角度
UpdateSteeringWheelAngle();
// 检查方向盘角度并更新选中的按钮
CheckSteeringWheelAngle();
ModBusQueue();
}
// void Update()
// {
// // 更新方向盘角度
// // UpdateSteeringWheelAngle();
//
// // 检查方向盘角度并更新选中的按钮
// CheckSteeringWheelAngle();
// }
public void SSSS()
{
@ -53,41 +64,88 @@ public class SteeringWheelController : MonoBehaviour
testFloatV = -45;
}
/// <summary>
/// 更新方向盘角度
/// </summary>
private void UpdateSteeringWheelAngle()
async Task ModBusQueue()
{
// 这里需要替换成获取硬件方向盘角度的代码
steeringWheelAngle = testFloatV;
while (true)
{
// Debug.Log(client.modbusQueue.Count);
if (ModbusTcpClient.modbusTcpClient.modbusQueue.Count > 0)
{
CarStatusData carData = ModbusTcpClient.modbusTcpClient.modbusQueue.Dequeue();
if (carData.ClutchPedalPosition > 30)
{
SelectButton();
}
/// <summary>
/// 判断方向盘左边还是右边
/// </summary>
private void CheckSteeringWheelAngle()
if (NewValue != carData.ButtonData)
{
if (Mathf.Abs(steeringWheelAngle) < neutralAngleThreshold)
NewValue = carData.ButtonData;
}
else
{
hasSelected = false;
return;
continue;
}
if (!hasSelected)
{
if (steeringWheelAngle > selectionAngleThreshold)
{
// 向右转动
SelectNextButton();
hasSelected = true;
CheckSteeringWheelAngle(NewValue);
}
else if (steeringWheelAngle < -selectionAngleThreshold)
await Task.Delay(TimeSpan.FromSeconds(.1));
}
}
// /// <summary>
// /// 更新方向盘角度
// /// </summary>
// private void UpdateSteeringWheelAngle()
// {
// // 这里需要替换成获取硬件方向盘角度的代码
// steeringWheelAngle = testFloatV;
// }
/// <summary>
/// 判断方向盘左边还是右边 //上1,右2,下3左4
/// </summary>
private void CheckSteeringWheelAngle(int btvalue)
{
if (btvalue > 0)
{
if (btvalue == 4)
{
// 向左转动
SelectPreviousButton();
hasSelected = true;
}
else if (btvalue == 2)
{
SelectNextButton();
}
}
// if (Mathf.Abs(steeringWheelAngle) < neutralAngleThreshold)
// {
// hasSelected = false;
// return;
// }
// if (!hasSelected)
// {
// if (steeringWheelAngle > selectionAngleThreshold)
// {
// // 向右转动
// SelectNextButton();
// hasSelected = true;
// }
// else if (steeringWheelAngle < -selectionAngleThreshold)
// {
// // 向左转动
// SelectPreviousButton();
// hasSelected = true;
// }
// }
}
/// <summary>
@ -96,7 +154,7 @@ public class SteeringWheelController : MonoBehaviour
private void SelectNextButton()
{
selectedButtonIndex = (selectedButtonIndex + 1) % buttons.Length;
Debug.Log("前进" + selectedButtonIndex);
// Debug.Log("前进" + selectedButtonIndex);
HighlightButton(selectedButtonIndex);
}
@ -111,7 +169,7 @@ public class SteeringWheelController : MonoBehaviour
selectedButtonIndex--;
Debug.Log("后退" + selectedButtonIndex);
// Debug.Log("后退" + selectedButtonIndex);
HighlightButton(selectedButtonIndex);
}
@ -121,7 +179,6 @@ public class SteeringWheelController : MonoBehaviour
/// <param name="index"></param>
public void HighlightButton(int index)
{
for (int i = 0; i < buttons.Length; i++)
{
if (buttons[i].name == buttons[index].name)
@ -155,4 +212,15 @@ public class SteeringWheelController : MonoBehaviour
{
buttons[selectedButtonIndex].GetComponent<Button>().onClick.Invoke();
}
public float NormalizeValue(float originalValue, float minValue, float maxValue)
{
// 确保原始值在范围内
float clampedValue = Math.Max(minValue, Math.Min(originalValue, maxValue));
// 计算归一化值
float normalizedValue = (2 * (clampedValue - minValue) / (maxValue - minValue)) - 1;
return normalizedValue;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,77 @@
Using pre-set license
Built from '2021.1/staging' branch; Version is '2021.1.24f1 (6667702a1e7c) revision 6711152'; Using compiler version '192528614'; Build Type 'Release'
OS: 'Windows 10 Pro; OS build 19044.1682; Version 2009; 64bit' Language: 'zh' Physical Memory: 16197 MB
BatchMode: 1, IsHumanControllingUs: 0, StartBugReporterOnCrash: 0, Is64bit: 1, IsPro: 0
COMMAND LINE ARGUMENTS:
D:\2021.1.24f1\Editor\Unity.exe
-adb2
-batchMode
-noUpm
-name
AssetImportWorker1
-projectPath
E:/Unity Projects/GitLab/2023/H_SafeExperienceDrivingSystem/U3D_DrivingSystem
-logFile
Logs/AssetImportWorker1.log
-srvPort
57189
Successfully changed project path to: E:/Unity Projects/GitLab/2023/H_SafeExperienceDrivingSystem/U3D_DrivingSystem
E:/Unity Projects/GitLab/2023/H_SafeExperienceDrivingSystem/U3D_DrivingSystem
Using Asset Import Pipeline V2.
Refreshing native plugins compatible for Editor in 181.72 ms, found 8 plugins.
Preloading 0 native plugins for Editor in 0.00 ms.
Initialize engine version: 2021.1.24f1 (6667702a1e7c)
[Subsystems] Discovering subsystems at path D:/2021.1.24f1/Editor/Data/Resources/UnitySubsystems
[Subsystems] Discovering subsystems at path E:/Unity Projects/GitLab/2023/H_SafeExperienceDrivingSystem/U3D_DrivingSystem/Assets
GfxDevice: creating device client; threaded=0; jobified=0
Direct3D:
Version: Direct3D 11.0 [level 11.1]
Renderer: NVIDIA GeForce GTX 1660 SUPER (ID=0x21c4)
Vendor: NVIDIA
VRAM: 5981 MB
Driver: 31.0.15.4617
Shader 'Custom/EVP Particles Alpha Blended Shadows': fallback shader 'Particles/Alpha Blended' not found
Initialize mono
Mono path[0] = 'D:/2021.1.24f1/Editor/Data/Managed'
Mono path[1] = 'D:/2021.1.24f1/Editor/Data/MonoBleedingEdge/lib/mono/unityjit'
Mono config path = 'D:/2021.1.24f1/Editor/Data/MonoBleedingEdge/etc'
Using monoOptions --debugger-agent=transport=dt_socket,embedding=1,server=y,suspend=n,address=127.0.0.1:56524
Begin MonoManager ReloadAssembly
Registering precompiled unity dll's ...
Register platform support module: D:/2021.1.24f1/Editor/Data/PlaybackEngines/WindowsStandaloneSupport/UnityEditor.WindowsStandalone.Extensions.dll
Registered in 0.000932 seconds.
Native extension for WindowsStandalone target not found
Refreshing native plugins compatible for Editor in 187.18 ms, found 8 plugins.
Preloading 0 native plugins for Editor in 0.00 ms.
Mono: successfully reloaded assembly
- Completed reload, in 1.181 seconds
Domain Reload Profiling:
ReloadAssembly (1182ms)
BeginReloadAssembly (55ms)
ExecutionOrderSort (0ms)
DisableScriptedObjects (0ms)
BackupInstance (0ms)
ReleaseScriptingObjects (0ms)
CreateAndSetChildDomain (1ms)
EndReloadAssembly (567ms)
LoadAssemblies (54ms)
RebuildTransferFunctionScriptingTraits (0ms)
SetupTypeCache (156ms)
ReleaseScriptCaches (0ms)
RebuildScriptCaches (34ms)
SetupLoadedEditorAssemblies (329ms)
LogAssemblyErrors (0ms)
InitializePlatformSupportModulesInManaged (7ms)
SetLoadedEditorAssemblies (0ms)
RefreshPlugins (187ms)
BeforeProcessingInitializeOnLoad (2ms)
ProcessInitializeOnLoadAttributes (103ms)
ProcessInitializeOnLoadMethodAttributes (30ms)
AfterProcessingInitializeOnLoad (0ms)
EditorAssembliesLoaded (0ms)
ExecutionOrderSort2 (0ms)
AwakeInstancesAfterBackupRestoration (0ms)
Platform modules already initialized, skipping
Registering precompiled user dll's ...
Registered in 0.006343 seconds.

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,3 @@
Base path: 'D:/2021.1.24f1/Editor/Data', plugins path 'D:/2021.1.24f1/Editor/Data/PlaybackEngines'
Cmd: initializeCompiler
Cmd: compileSnippet
insize=11820 file=Packages/com.unity.postprocessing/PostProcessing/Shaders/Builtins/Hidden/PostProcessing/Uber pass=<Unnamed Pass> cachingPP=1 ppOnly=0 stripLineD=0 buildPlatform=19 rsLen=0 pKW=UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_COLORSPACE_GAMMA UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_LIGHTMAP_FULL_HDR uKW=COLOR_GRADING_HDR_3D FINALPASS dKW=DISTORT CHROMATIC_ABERRATION CHROMATIC_ABERRATION_LOW BLOOM BLOOM_LOW VIGNETTE GRAIN COLOR_GRADING_LDR_2D COLOR_GRADING_HDR_2D STEREO_INSTANCING_ENABLED STEREO_DOUBLEWIDE_TARGET UNITY_NO_DXT5nm UNITY_ENABLE_NATIVE_SHADOW_LOOKUPS UNITY_METAL_SHADOWS_USE_POINT_FILTERING UNITY_NO_SCREENSPACE_SHADOWS UNITY_PBS_USE_BRDF2 UNITY_PBS_USE_BRDF3 UNITY_NO_FULL_STANDARD_SHADER UNITY_HARDWARE_TIER1 UNITY_HARDWARE_TIER2 UNITY_HARDWARE_TIER3 UNITY_HALF_PRECISION_FRAGMENT_SHADER_REGISTERS UNITY_LIGHTMAP_DLDR_ENCODING UNITY_LIGHTMAP_RGBM_ENCODING UNITY_VIRTUAL_TEXTURING UNITY_PRETRANSFORM_TO_DISPLAY_ORIENTATION UNITY_ASTC_NORMALMAP_ENCODING SHADER_API_GLES30 flags=0 lang=3 type=Fragment platform=d3d11 reqs=227 mask=6 start=277 ok=1 outsize=1838

View File

@ -12,13 +12,13 @@ EditorUserSettings:
value: 22424703114646680e0b0227036c5b151b18563f22213229
flags: 0
RecentlyUsedScenePath-2:
value: 22424703114646680e0b0227036c52151802563f22213229
value: 22424703114646680e0b0227036c52111f19276439262f2434
flags: 0
RecentlyUsedScenePath-3:
value: 22424703114646680e0b0227036c4c0417050c6439262f2434
value: 22424703114646680e0b0227036c52151802563f22213229
flags: 0
RecentlyUsedScenePath-4:
value: 22424703114646680e0b0227036c52111f19276439262f2434
value: 22424703114646680e0b0227036c4c0417050c6439262f2434
flags: 0
vcSharedLogLevel:
value: 0d5e400f0650