123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using Controller.Bicycle;
- using Sensors;
- using UnityEngine;
- namespace Display
- {
- public class DebugDisplay<T> : MonoBehaviour where T : IBicycleController
- {
- public T bicycleController;
- private string ip;
- private readonly BikeSensorData sensorData = BikeSensorData.Instance;
- private string text;
- private void Start()
- {
- ip = GetIp();
- }
- private void Update()
- {
- var rbSpeed = bicycleController.RigidBodyVelocity.magnitude * 3.6;
- var sensorSpeed = sensorData.SpeedData?.SpeedKmh ?? 0f;
- var sb = new StringBuilder();
- //sb.Append(
- // $"MotorTorque: {bicycleController.CurrentMotorTorque:n2}\nBrakeTorque: {bicycleController.CurrentBrakeTorque:n2}\nSteer: {bicycleController.CurrentSteerAngle}\nLean: {bicycleController.CurrentLeaningAngle:n4}\n");
- sb.Append("-----Sensors------\n");
- sb.Append(
- $"Speed: {sensorData.SpeedData?.Speed ?? 0f:n4} m/s\nPolar Acc ({sensorData.PolarData?.Acc.x ?? 0f:n4}; {sensorData.PolarData?.Acc.y ?? 0f:n4}; {sensorData.PolarData?.Acc.z ?? 0f:n4}) ");
- sb.Append("\n\n");
- sb.Append($"IP: {ip}\n");
- sb.Append("----BicycleController-----\n");
- sb.Append($"Actual Speed {rbSpeed:n2} km/h - Dif: {bicycleController.CurrentSpeed * 3.6 - rbSpeed:n2}\n");
- sb.Append(
- $"Current Speed = {bicycleController.CurrentSpeed:n2} m/s ({bicycleController.CurrentSpeed * 3.6:n2} km/h)");
- text = sb.ToString();
- }
- private void OnGUI()
- {
- var padding = 10;
- var width = 260;
- var height = 175;
- var x = Screen.width - width - padding;
- var y = Screen.height - height - padding;
- GUI.TextField(new Rect(x, y, width, height), text);
- }
- private string GetIp()
- {
- string usedIp = null;
- foreach (var ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
- if (ip.AddressFamily == AddressFamily.InterNetwork)
- if (usedIp == null || ip.ToString().StartsWith("192.168.1."))
- usedIp = ip.ToString();
- return usedIp;
- }
- }
- }
|