266 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			266 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System;
 | ||
| using System.Collections;
 | ||
| using System.Collections.Generic;
 | ||
| using System.Threading.Tasks;
 | ||
| using ModbusManager;
 | ||
| using UnityEngine;
 | ||
| using UnityEngine.UI; // 如果您使用的是UGUI
 | ||
| 
 | ||
| public class SteeringWheelController : MonoBehaviour
 | ||
| {
 | ||
|     public static SteeringWheelController Instance;
 | ||
| 
 | ||
|     public Button[] buttons;
 | ||
|     private int selectedButtonIndex = 0; // 当前选中的按钮索引
 | ||
|     private bool hasSelected = false; // 是否已经进行了选择
 | ||
| 
 | ||
|     // 硬件获取的方向盘角度
 | ||
|     private float steeringWheelAngle;
 | ||
| 
 | ||
|     // 方向盘选择角度阈值和中立阈值
 | ||
|     public float selectionAngleThreshold = 30.0f; // 例如,30度
 | ||
|     public float neutralAngleThreshold = 5.0f; // 方向盘接近中立位置的阈值
 | ||
| 
 | ||
| 
 | ||
|     public float testFloatV;
 | ||
|     float minValue = -300f; // 范围的最小值
 | ||
|     float maxValue = 300; // 范围的最大值
 | ||
| 
 | ||
| 
 | ||
|     private int NewValue = -1;
 | ||
| 
 | ||
|     private bool ClutchPedalPositionBool = false;
 | ||
|     private ModbusTcpClient client;
 | ||
| 
 | ||
|     private void Awake()
 | ||
|     {
 | ||
|         Instance = this;
 | ||
|     }
 | ||
| 
 | ||
|     private async void Start()
 | ||
|     {
 | ||
|         StartModbus();
 | ||
|         ModBusQueue();
 | ||
|     }
 | ||
| 
 | ||
|     async Task StartModbus()
 | ||
|     {
 | ||
|         client = new ModbusTcpClient();
 | ||
|         await client.ConnectToServer();
 | ||
|         while (true)
 | ||
|         {
 | ||
|             await client.SendModbusRequest();
 | ||
| 
 | ||
|             await Task.Delay(TimeSpan.FromSeconds(.1));
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
|     private void OnDestroy()
 | ||
|     {
 | ||
|         client.CloseConnection();
 | ||
|     }
 | ||
| 
 | ||
|     // void Update()
 | ||
|     // {
 | ||
|     //     // 更新方向盘角度
 | ||
|     //     // UpdateSteeringWheelAngle();
 | ||
|     //
 | ||
|     //     // 检查方向盘角度并更新选中的按钮
 | ||
|     //     CheckSteeringWheelAngle();
 | ||
|     // }
 | ||
| 
 | ||
| 
 | ||
|     public void SSSS()
 | ||
|     {
 | ||
|         testFloatV = 45;
 | ||
|     }
 | ||
| 
 | ||
|     public void AAAA()
 | ||
|     {
 | ||
|         testFloatV = 0;
 | ||
|     }
 | ||
| 
 | ||
|     public void BBB()
 | ||
|     {
 | ||
|         testFloatV = -45;
 | ||
|     }
 | ||
| 
 | ||
|     async Task ModBusQueue()
 | ||
|     {
 | ||
|         while (true)
 | ||
|         {
 | ||
|             // Debug.Log(client.modbusQueue.Count);
 | ||
|             if (client.modbusQueue.Count > 0)
 | ||
|             {
 | ||
|                 CarStatusData carData = client.modbusQueue.Dequeue();
 | ||
| 
 | ||
|                 if (carData.AcceleratorPedalPosition > 30)
 | ||
|                 {
 | ||
|                     if (ClutchPedalPositionBool)
 | ||
|                     {
 | ||
|                         ClutchPedalPositionBool = false;
 | ||
|                         SelectButton();
 | ||
|                     }
 | ||
|                 }
 | ||
|                 else if (carData.BrakePedalPosition > 30)
 | ||
|                 {
 | ||
|                     if (ClutchPedalPositionBool)
 | ||
|                     {
 | ||
|                         ClutchPedalPositionBool = false;
 | ||
|                         MenuManager.instance.PreviousStep();
 | ||
|                     }
 | ||
|                 }
 | ||
|                 else if (carData.AcceleratorPedalPosition < 30)
 | ||
|                 {
 | ||
|                     ClutchPedalPositionBool = true;
 | ||
|                 }
 | ||
| 
 | ||
| 
 | ||
|                 // else if(carData.BrakePedalPosition<30)
 | ||
|                 // {
 | ||
|                 //     ClutchPedalPositionBool = true;
 | ||
|                 // }
 | ||
| 
 | ||
| 
 | ||
|                 if (NewValue != carData.ButtonData)
 | ||
|                 {
 | ||
|                     NewValue = carData.ButtonData;
 | ||
|                 }
 | ||
|                 else
 | ||
|                 {
 | ||
|                     continue;
 | ||
|                 }
 | ||
| 
 | ||
|                 CheckSteeringWheelAngle(NewValue);
 | ||
|             }
 | ||
| 
 | ||
|             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();
 | ||
|             }
 | ||
|             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>
 | ||
|     /// 下一个按钮
 | ||
|     /// </summary>
 | ||
|     private void SelectNextButton()
 | ||
|     {
 | ||
|         selectedButtonIndex = (selectedButtonIndex + 1) % buttons.Length;
 | ||
|         // Debug.Log("前进" + selectedButtonIndex);
 | ||
|         HighlightButton(selectedButtonIndex);
 | ||
|     }
 | ||
| 
 | ||
|     /// <summary>
 | ||
|     /// 上一个按钮
 | ||
|     /// </summary>
 | ||
|     private void SelectPreviousButton()
 | ||
|     {
 | ||
|         if (selectedButtonIndex == 0)
 | ||
|             selectedButtonIndex = buttons.Length - 1;
 | ||
|         else
 | ||
|             selectedButtonIndex--;
 | ||
| 
 | ||
| 
 | ||
|         // Debug.Log("后退" + selectedButtonIndex);
 | ||
|         HighlightButton(selectedButtonIndex);
 | ||
|     }
 | ||
| 
 | ||
|     /// <summary>
 | ||
|     /// 选择按钮变换
 | ||
|     /// </summary>
 | ||
|     /// <param name="index"></param>
 | ||
|     public void HighlightButton(int index)
 | ||
|     {
 | ||
|         for (int i = 0; i < buttons.Length; i++)
 | ||
|         {
 | ||
|             if (buttons[i].name == buttons[index].name)
 | ||
|             {
 | ||
|                 buttons[i].GetComponent<BtClick>().OnClickSprite();
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 buttons[i].GetComponent<BtClick>().OnCancel();
 | ||
|             }
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
|     // public void SelectButtonSprite( string btName)
 | ||
|     // {
 | ||
|     //     for (int i = 0; i < buttons.Length; i++)
 | ||
|     //     {
 | ||
|     //         if (buttons[i].name == btName)
 | ||
|     //         {
 | ||
|     //             buttons[i].GetComponent<BtClick>().ClickSp();
 | ||
|     //         }
 | ||
|     //         else
 | ||
|     //         {
 | ||
|     //             buttons[i].GetComponent<BtClick>().Cancel();
 | ||
|     //         }
 | ||
|     //     }
 | ||
|     // }
 | ||
| 
 | ||
| 
 | ||
|     public void SelectButton()
 | ||
|     {
 | ||
|         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;
 | ||
|     }
 | ||
| } |