DebugDisplay.cs 2.0 KB

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