DebugDisplay.cs 2.5 KB

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