DebugDisplay.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 = Helpers.GetIPAddress().ToString();
  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. }
  47. }