1
0

DebugDisplay.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Net;
  2. using System.Net.Sockets;
  3. using System.Text;
  4. using Controller.Bicycle;
  5. using Sensors;
  6. using UnityEngine;
  7. namespace Display
  8. {
  9. public class DebugDisplay<T> : MonoBehaviour where T : IBicycleController
  10. {
  11. public T bicycleController;
  12. private string ip;
  13. private readonly BikeSensorData sensorData = BikeSensorData.Instance;
  14. private string text;
  15. private void Start()
  16. {
  17. ip = GetIp();
  18. }
  19. private void Update()
  20. {
  21. var rbSpeed = bicycleController.RigidBodyVelocity.magnitude * 3.6;
  22. var sensorSpeed = sensorData.SpeedData?.SpeedKmh ?? 0f;
  23. var sb = new StringBuilder();
  24. //sb.Append(
  25. // $"MotorTorque: {bicycleController.CurrentMotorTorque:n2}\nBrakeTorque: {bicycleController.CurrentBrakeTorque:n2}\nSteer: {bicycleController.CurrentSteerAngle}\nLean: {bicycleController.CurrentLeaningAngle:n4}\n");
  26. sb.Append("-----Sensors------\n");
  27. sb.Append(
  28. $"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}) ");
  29. sb.Append("\n\n");
  30. sb.Append($"IP: {ip}\n");
  31. sb.Append("----BicycleController-----\n");
  32. sb.Append($"Actual Speed {rbSpeed:n2} km/h - Dif: {bicycleController.CurrentSpeed * 3.6 - rbSpeed:n2}\n");
  33. sb.Append(
  34. $"Current Speed = {bicycleController.CurrentSpeed:n2} m/s ({bicycleController.CurrentSpeed * 3.6:n2} km/h)");
  35. text = sb.ToString();
  36. }
  37. private void OnGUI()
  38. {
  39. var padding = 10;
  40. var width = 260;
  41. var height = 175;
  42. var x = Screen.width - width - padding;
  43. var y = Screen.height - height - padding;
  44. GUI.TextField(new Rect(x, y, width, height), text);
  45. }
  46. private string GetIp()
  47. {
  48. string usedIp = null;
  49. foreach (var ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
  50. if (ip.AddressFamily == AddressFamily.InterNetwork)
  51. if (usedIp == null || ip.ToString().StartsWith("192.168.1."))
  52. usedIp = ip.ToString();
  53. return usedIp;
  54. }
  55. }
  56. }