SensorBikeController.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using Controller.Bicycle;
  4. using Controller.Lean;
  5. using Study;
  6. using Sensors;
  7. using Sensors.ANT;
  8. using Sensors.Bluetooth;
  9. using Tracking;
  10. using System.Linq;
  11. using UnityEngine;
  12. // Declare outisde for public visibility
  13. //public enum SteeringMode { frontWheel, Leaning, HMD };
  14. namespace Controller
  15. {
  16. [Serializable]
  17. public struct FrontWheelTrackerConfig
  18. {
  19. public FrontWheelTracker frontWheelTracker;
  20. public float multiplicator;
  21. public float AdjustedRotation => frontWheelTracker.SteerRotation * multiplicator;
  22. }
  23. [Serializable]
  24. public struct HMDTrackerConfig
  25. {
  26. public HMDTracker cameraTracker;
  27. public float multiplicator;
  28. public float steeringOffset;
  29. public float AdjustedRotation => cameraTracker.SteerRotation * multiplicator;
  30. public float AdjustedLean => cameraTracker.LeanRotation * multiplicator;
  31. }
  32. [RequireComponent(typeof(IBicycleController))]
  33. public class SensorBikeController : MonoBehaviour
  34. {
  35. public PolarRotationMapping polarRotationMapping;
  36. public FrontWheelTrackerConfig frontWheelTrackerConfig;
  37. public HMDTrackerConfig hmdTrackerConfig;
  38. private float leanFactor;
  39. public bool steer = true;
  40. public bool accelerate = true;
  41. public bool lean = true;
  42. public Study.SteeringMode steeringSelection;
  43. private IBicycleController bicycleController;
  44. private bool isFrontWheelTrackerNotNull;
  45. private bool isHMDTrackerNotNull;
  46. private BikeSensorData sensorData;
  47. private Queue<float> previousLeanValues;
  48. public int framesToConsider = 10;
  49. public float tolerance = 5;
  50. private GameObject bikePlayer;
  51. private void Start()
  52. {
  53. isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
  54. isHMDTrackerNotNull = hmdTrackerConfig.cameraTracker != null;
  55. bicycleController = GetComponent<IBicycleController>();
  56. sensorData = BikeSensorData.Instance;
  57. //leanFactor = 90f / (polarRotationMapping.maxRight - polarRotationMapping.center);
  58. leanFactor = 45f / polarRotationMapping.maxRight;
  59. if (framesToConsider > 1) previousLeanValues = new Queue<float>(framesToConsider);
  60. // Sort of callibration for polar Sensor
  61. var polarData = sensorData.BleData;
  62. if (steeringSelection == Study.SteeringMode.Leaning && polarData != null)
  63. {
  64. Debug.Log("Calibration of Polar Sensor");
  65. polarRotationMapping.center = polarData.Value.Acc.y;
  66. }
  67. bikePlayer = GameObject.Find("bike");
  68. }
  69. private void Update()
  70. {
  71. Debug.Log("Bike Sensor Controller called");
  72. var speedData = sensorData.SpeedData;
  73. if (speedData != null && accelerate) SetSpeed(speedData.Value);
  74. if (steer) SetSteer();
  75. if (lean) SetLeaningAngle();
  76. var polarData = sensorData.BleData;
  77. if (polarData != null)
  78. {
  79. var playerStats = bikePlayer.GetComponent<PlayerStats>();
  80. playerStats.rotationPolarX = polarData.Value.Acc.x;
  81. playerStats.rotationPolarY = polarData.Value.Acc.y;
  82. playerStats.rotationPolarZ = polarData.Value.Acc.z;
  83. }
  84. }
  85. public void SetCondition(Study.SteeringMode mode)
  86. {
  87. Debug.Log("Setting Condition");
  88. steeringSelection = mode;
  89. }
  90. private void SetSteer()
  91. {
  92. switch (steeringSelection)
  93. {
  94. case Study.SteeringMode.frontWheel:
  95. if (isFrontWheelTrackerNotNull)
  96. {
  97. bicycleController.CurrentSteerAngle = frontWheelTrackerConfig.AdjustedRotation * 2f;
  98. }
  99. break;
  100. case Study.SteeringMode.Leaning:
  101. var polarData = sensorData.BleData;
  102. if (polarData != null)
  103. {
  104. Debug.Log("Updating Polar Data");
  105. Debug.Log("Polar Y: " + polarData.Value.Acc);
  106. var polarLean = CalculateLeanRotationMultiFrame(polarData);
  107. Debug.Log("Ploar Post-Processed: " + polarLean);
  108. bicycleController.CurrentSteerAngle = polarLean;
  109. // Activate below in case we also need the steering angle
  110. //bicycleController.CurrentSteerAngle = (polarData.Value.Acc.y - polarRotationMapping.center) * leanFactor;
  111. }
  112. break;
  113. case Study.SteeringMode.HMD:
  114. if (isHMDTrackerNotNull)
  115. {
  116. // We emprirically observed a right-drift
  117. // We subtract a constant to counteract this
  118. bicycleController.CurrentSteerAngle = hmdTrackerConfig.AdjustedRotation;
  119. }
  120. break;
  121. }
  122. Debug.Log("Updating Steering Angle to " + bicycleController.CurrentSteerAngle);
  123. }
  124. private void SetLeaningAngle()
  125. {
  126. //bicycleController.CurrentLeaningAngle =
  127. }
  128. private void SetSpeed(SpeedSensorData speedData)
  129. {
  130. bicycleController.CurrentSpeed = speedData.Speed;
  131. }
  132. // Helpers for Lean Steering
  133. private float CalculateLeanRotationSingleFrame(BleSensorData? polarData)
  134. {
  135. float steerUpdate = polarData.Value.Acc.y - polarRotationMapping.center;
  136. //steerUpdate = Math.Abs(steerUpdate - polarRotationMapping.center) < tolerance ? 0 : steerUpdate;
  137. return steerUpdate * leanFactor;
  138. }
  139. private float CalculateLeanRotationMultiFrame(BleSensorData? polarData)
  140. {
  141. var angleThisFrame = CalculateLeanRotationSingleFrame(polarData);
  142. if (previousLeanValues.Count > framesToConsider) previousLeanValues.Dequeue();
  143. previousLeanValues.Enqueue(angleThisFrame);
  144. // Use exponential moving average
  145. float alpha = 2f / (framesToConsider + 1);
  146. float steerUpdate = previousLeanValues.DefaultIfEmpty()
  147. .Aggregate((ema, nextQuote) => alpha * nextQuote + (1 - alpha) * ema);
  148. // Do Global Thresholding, to avoid noisy steering
  149. return Math.Abs(steerUpdate - polarRotationMapping.center) < tolerance ? 0 : steerUpdate;
  150. //return steerUpdate;
  151. }
  152. }
  153. }