DebugDisplay.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 sb = new StringBuilder();
  18. sb.Append(
  19. $"MotorTorque: {bicycleController.CurrentMotorTorque}; BrakeTorque: {bicycleController.CurrentBrakeTorque}, Steer: {bicycleController.CurrentSteerAngle}; Lean: {bicycleController.CurrentLeaningAngle}\n");
  20. sb.Append("-----Sensors------\n");
  21. sb.Append(
  22. $"Speed: {(sensorData.SpeedData?.Speed ?? 0f):n4} m/s; Polar Acc ({(sensorData.PolarData?.Acc.x ?? 0f):n4}; {(sensorData.PolarData?.Acc.y ?? 0f):n4}; {(sensorData.PolarData?.Acc.z ?? 0f):n4}) ");
  23. sb.Append("\n\n");
  24. sb.Append($"IP: {ip}\n");
  25. sb.Append("----BicycleController-----\n");
  26. sb.Append($"Actual Speed {(bicycleController.rigidBody.velocity.magnitude * 3.6):n2} km/h");
  27. text = sb.ToString();
  28. }
  29. private void OnGUI()
  30. {
  31. var padding = 10;
  32. var width = 360;
  33. var height = 120;
  34. var x = Screen.width - width - padding;
  35. var y = Screen.height - height - padding;
  36. GUI.TextField(new Rect(x, y, width, height), text);
  37. }
  38. private String GetIp()
  39. {
  40. string usedIp = null;
  41. foreach (var ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
  42. {
  43. if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  44. {
  45. if (usedIp == null || ip.ToString().StartsWith("192.168.1."))
  46. {
  47. usedIp = ip.ToString();
  48. }
  49. }
  50. }
  51. return usedIp;
  52. }
  53. }