SensorBikeController.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_global = 5;
  50. public float tolerance_local = 7;
  51. public int claibrationcount = 10;
  52. private void Start()
  53. {
  54. isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
  55. isHMDTrackerNotNull = hmdTrackerConfig.cameraTracker != null;
  56. bicycleController = GetComponent<IBicycleController>();
  57. sensorData = BikeSensorData.Instance;
  58. //leanFactor = 90f / (polarRotationMapping.maxRight - polarRotationMapping.center);
  59. leanFactor = 30f / polarRotationMapping.maxRight; //von 45 auf 30 angepasst
  60. if (framesToConsider > 1) previousLeanValues = new Queue<float>(framesToConsider);
  61. var polarData = sensorData.BleData;
  62. if (steeringSelection == Study.SteeringMode.Leaning && polarData != null)
  63. {
  64. CalibratePolar()
  65. //polarRotationMapping.center = polarData.Value.Acc.y;
  66. }
  67. Debug.Log("Default fpr previousLeanValues " + previousLeanValues.DefaultIfEmpty());
  68. }
  69. private void CalibratePolar()
  70. {
  71. Debug.Log("Calibration of Polar Sensor");
  72. Queue<float> calibrationLeanValues;
  73. calibrationLeanValues = new Queue<float>(claibrationcount);
  74. for (int i = 0; i < claibrationcount; i++)
  75. {
  76. float pos = sensorData.BleData.Value.Acc.y;
  77. calibrationLeanValues.Enqueue(pos);
  78. yield return new WaitForSeconds(0.25f); //evtl. new loeschen
  79. }
  80. polarRotationMapping.center = calibrationLeanValues.Average();
  81. }
  82. private void Update()
  83. {
  84. Debug.Log("Bike Sensor Controller called");
  85. var speedData = sensorData.SpeedData;
  86. if (speedData != null && accelerate) SetSpeed(speedData.Value);
  87. if (steer) SetSteer();
  88. if (lean) SetLeaningAngle();
  89. }
  90. public void SetCondition(Study.SteeringMode mode)
  91. {
  92. Debug.Log("Setting Condition");
  93. steeringSelection = mode;
  94. }
  95. private void SetSteer()
  96. {
  97. switch (steeringSelection)
  98. {
  99. case Study.SteeringMode.frontWheel:
  100. if (isFrontWheelTrackerNotNull)
  101. {
  102. bicycleController.CurrentSteerAngle = frontWheelTrackerConfig.AdjustedRotation * 2f;
  103. }
  104. break;
  105. case Study.SteeringMode.Leaning:
  106. var polarData = sensorData.BleData;
  107. if (polarData != null)
  108. {
  109. Debug.Log("Updating Polar Data");
  110. Debug.Log("Polar Y: " + polarData.Value.Acc);
  111. var polarLean = CalculateLeanRotationMultiFrame(polarData);
  112. Debug.Log("Ploar Post-Processed: " + polarLean);
  113. bicycleController.CurrentSteerAngle = polarLean;
  114. // Activate below in case we also need the steering angle
  115. //bicycleController.CurrentSteerAngle = (polarData.Value.Acc.y - polarRotationMapping.center) * leanFactor;
  116. }
  117. break;
  118. case Study.SteeringMode.HMD:
  119. if (isHMDTrackerNotNull)
  120. {
  121. // We emprirically observed a right-drift
  122. // We subtract a constant to counteract this
  123. bicycleController.CurrentSteerAngle = hmdTrackerConfig.AdjustedRotation;
  124. }
  125. break;
  126. }
  127. Debug.Log("Updating Steering Angle to " + bicycleController.CurrentSteerAngle);
  128. }
  129. private void SetLeaningAngle()
  130. {
  131. //bicycleController.CurrentLeaningAngle =
  132. }
  133. private void SetSpeed(SpeedSensorData speedData)
  134. {
  135. bicycleController.CurrentSpeed = speedData.Speed;
  136. }
  137. // Helpers for Lean Steering
  138. private float CalculateLeanRotationSingleFrame(BleSensorData? polarData)
  139. {
  140. float steerUpdate = polarData.Value.Acc.y - polarRotationMapping.center;
  141. //lokal average:
  142. //steerUpdate = Math.Abs(steerUpdate - polarRotationMapping.center) < tolerance_local ? 0 : steerUpdate;
  143. //steerUpdate = Math.Pow(steerUpdate - polarRotationMapping.center, 2) < 30 ? 0 : steerUpdate;
  144. return steerUpdate * leanFactor;
  145. }
  146. private float CalculateLeanRotationMultiFrame(BleSensorData? polarData)
  147. {
  148. var angleThisFrame = CalculateLeanRotationSingleFrame(polarData);
  149. if (previousLeanValues.Count > framesToConsider) previousLeanValues.Dequeue();
  150. previousLeanValues.Enqueue(angleThisFrame);
  151. //neue-Option:
  152. //float steerOld = previousLeanValues.Peek();
  153. //float mean = previousLeanValues.Average();
  154. //steerUpdate = Math.Pow(steerUpdate - mean, 2) < tolerance_global ? steerOld : steerUpdate;
  155. //alte-Option:
  156. // Use exponential moving average
  157. float alpha = 2f / (framesToConsider + 1);
  158. float steerUpdate = previousLeanValues.DefaultIfEmpty(polarRotationMapping.center) //angepasst: Default ist nun center
  159. .Aggregate((ema, nextQuote) => alpha * nextQuote + (1 - alpha) * ema);
  160. // Do Global Thresholding, to avoid noisy steering
  161. return Math.Abs(steerUpdate - polarRotationMapping.center) < tolerance_global ? 0 : steerUpdate;
  162. //return steerUpdate;
  163. }
  164. }
  165. }