DebugDisplay.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System.Text;
  2. using Controller.Bicycle;
  3. using Sensors;
  4. using UnityEngine;
  5. namespace Display
  6. {
  7. public class DebugDisplay<T> : MonoBehaviour where T : IBicycleController
  8. {
  9. public T bicycleController;
  10. private readonly BikeSensorData sensorData = BikeSensorData.Instance;
  11. private string ip;
  12. private string text;
  13. private void Start()
  14. {
  15. ip = Helpers.GetIPAddress().ToString();
  16. }
  17. private void Update()
  18. {
  19. var rbSpeed = bicycleController.RigidBodyVelocity.magnitude * 3.6;
  20. var sensorSpeed = sensorData.SpeedData?.SpeedKmh ?? 0f;
  21. var sb = new StringBuilder();
  22. //sb.Append(
  23. // $"MotorTorque: {bicycleController.CurrentMotorTorque:n2}\nBrakeTorque: {bicycleController.CurrentBrakeTorque:n2}\nSteer: {bicycleController.CurrentSteerAngle}\nLean: {bicycleController.CurrentLeaningAngle:n4}\n");
  24. sb.Append("-----Sensors------\n");
  25. sb.Append(
  26. $"Speed: {sensorData.SpeedData?.Speed ?? 0f:n4} m/s\nPolar Acc ({sensorData.BleData?.Acc.x ?? 0f:n4}; {sensorData.BleData?.Acc.y ?? 0f:n4}; {sensorData.BleData?.Acc.z ?? 0f:n4}) ");
  27. sb.Append("\n\n");
  28. sb.Append($"IP: {ip}\n");
  29. sb.Append("----BicycleController-----\n");
  30. sb.Append($"Actual Speed {rbSpeed:n2} km/h - Dif: {bicycleController.CurrentSpeed * 3.6 - rbSpeed:n2}\n");
  31. sb.Append(
  32. $"Current Speed = {bicycleController.CurrentSpeed:n2} m/s ({bicycleController.CurrentSpeed * 3.6:n2} km/h)");
  33. text = sb.ToString();
  34. }
  35. private void OnGUI()
  36. {
  37. var padding = 10;
  38. var width = 260;
  39. var height = 175;
  40. var x = Screen.width - width - padding;
  41. var y = Screen.height - height - padding;
  42. GUI.TextField(new Rect(x, y, width, height), text);
  43. }
  44. }
  45. }