SensorBikeController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using Controller.Bicycle;
  3. using Controller.Lean;
  4. using Sensors;
  5. using Sensors.ANT;
  6. using Tracking;
  7. using UnityEngine;
  8. namespace Controller
  9. {
  10. [Serializable]
  11. public struct FrontWheelTrackerConfig
  12. {
  13. public FrontWheelTracker frontWheelTracker;
  14. public float multiplicator;
  15. public float AdjustedRotation => frontWheelTracker.SteerRotation * multiplicator;
  16. }
  17. [Serializable]
  18. public struct HMDTrackerConfig
  19. {
  20. public HMDTracker cameraTracker;
  21. public float multiplicator;
  22. public float AdjustedRotation => cameraTracker.SteerRotation * multiplicator;
  23. }
  24. [RequireComponent(typeof(IBicycleController))]
  25. public class SensorBikeController : MonoBehaviour
  26. {
  27. public enum SteeringMode { frontWheel, Leaning, HMD };
  28. public PolarRotationMapping polarRotationMapping;
  29. public FrontWheelTrackerConfig frontWheelTrackerConfig;
  30. public HMDTrackerConfig hmdTrackerConfig;
  31. private float leanFactor;
  32. public bool steer = true;
  33. public bool accelerate = true;
  34. public bool lean = true;
  35. public SteeringMode steeringSelection;
  36. private IBicycleController bicycleController;
  37. private bool isFrontWheelTrackerNotNull;
  38. private BikeSensorData sensorData;
  39. private GameObject player;
  40. private void Start()
  41. {
  42. isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
  43. bicycleController = GetComponent<IBicycleController>();
  44. sensorData = BikeSensorData.Instance;
  45. player = GameObject.FindGameObjectWithTag("HMDTracker");
  46. // Dummy assignment
  47. steeringSelection = SteeringMode.frontWheel;
  48. leanFactor = 90f / (polarRotationMapping.maxRight - polarRotationMapping.center);
  49. }
  50. private void Update()
  51. {
  52. Debug.Log("Bike Sensor Controller called");
  53. var speedData = sensorData.SpeedData;
  54. if (speedData != null && accelerate) SetSpeed(speedData.Value);
  55. if (steer) SetSteer();
  56. if (lean) SetLeaningAngle();
  57. }
  58. private void SetSteer()
  59. {
  60. Debug.Log("Updating Steering");
  61. switch (steeringSelection) {
  62. case SteeringMode.frontWheel:
  63. if (isFrontWheelTrackerNotNull) {
  64. bicycleController.CurrentSteerAngle = frontWheelTrackerConfig.AdjustedRotation * 2f;
  65. }
  66. break;
  67. case SteeringMode.Leaning:
  68. var polarData = sensorData.BleData;
  69. if (polarData != null)
  70. {
  71. bicycleController.CurrentSteerAngle = (polarData.Value.Acc.y - polarRotationMapping.center) * leanFactor;
  72. }
  73. break;
  74. case SteeringMode.HMD:
  75. // TODO: Convert steering relative to bike
  76. Debug.Log("HMD Rotation " + player.transform.eulerAngles);
  77. //Lisa:
  78. //var rot = player.transform.localRotation.eulerAngles.z; // https://answers.unity.com/questions/149801/transformrotation-in-degrees-instead-of-0-to-1.html
  79. //var yaw = rot *3f; // da kopfdrehung relativ gering im vergleich zu einer lenkraddrehung, faktor ggf. erhöhen
  80. Debug.Log("Local Euler Angles " + player.transform.localRotation.eulerAngles);
  81. //Julia:
  82. // https://wirewhiz.com/vr-body-tracking-tutorial-pt-1/
  83. // Bestimme Winkel zwischen Kopf und Fahrrad
  84. /*var rot_player = player.transform.rotation;
  85. var rot_bike = GameObject.FindGameObjectWithTag("bike").transform.rotation;
  86. var yaw = Quaternion.Angle(Quaternion.Euler(0, rot_bike.eulerAngles.y, 0),
  87. Quaternion.Euler(0, rot_player.eulerAngles.y, 0));*/
  88. //Debug.Log("Calculated Yaw " + yaw);
  89. bicycleController.CurrentSteerAngle = hmdTrackerConfig.AdjustedRotation - 5f;
  90. break;
  91. }
  92. Debug.Log("Updating Steering Angle to " + bicycleController.CurrentSteerAngle);
  93. }
  94. private void SetLeaningAngle()
  95. {
  96. //bicycleController.CurrentLeaningAngle =
  97. }
  98. private void SetSpeed(SpeedSensorData speedData)
  99. {
  100. bicycleController.CurrentSpeed = speedData.Speed;
  101. }
  102. }
  103. }