SensorBikeController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 void Start()
  51. {
  52. isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
  53. isHMDTrackerNotNull = hmdTrackerConfig.cameraTracker != null;
  54. bicycleController = GetComponent<IBicycleController>();
  55. sensorData = BikeSensorData.Instance;
  56. leanFactor = 90f / (polarRotationMapping.maxRight - polarRotationMapping.center);
  57. if (framesToConsider > 1) previousLeanValues = new Queue<float>(framesToConsider);
  58. // Sort of callibration for polar Sensor
  59. var polarData = sensorData.BleData;
  60. if (steeringSelection == Study.SteeringMode.Leaning && polarData != null)
  61. {
  62. Debug.Log("Calibration of Polar Sensor");
  63. polarRotationMapping.center = polarData.Value.Acc.y;
  64. }
  65. }
  66. private void Update()
  67. {
  68. Debug.Log("Bike Sensor Controller called");
  69. var speedData = sensorData.SpeedData;
  70. if (speedData != null && accelerate) SetSpeed(speedData.Value);
  71. if (steer) SetSteer();
  72. if (lean) SetLeaningAngle();
  73. }
  74. public void SetCondition(Study.SteeringMode mode)
  75. {
  76. Debug.Log("Setting Condition");
  77. steeringSelection = mode;
  78. }
  79. private void SetSteer()
  80. {
  81. switch (steeringSelection)
  82. {
  83. case Study.SteeringMode.frontWheel:
  84. if (isFrontWheelTrackerNotNull)
  85. {
  86. bicycleController.CurrentSteerAngle = frontWheelTrackerConfig.AdjustedRotation * 2f;
  87. }
  88. break;
  89. case Study.SteeringMode.Leaning:
  90. var polarData = sensorData.BleData;
  91. if (polarData != null)
  92. {
  93. Debug.Log("Updating Polar Data");
  94. Debug.Log("Polar Y: " + polarData.Value.Acc);
  95. var polarLean = CalculateLeanRotationMultiFrame(polarData);
  96. Debug.Log("Ploar Post-Processed: " + polarLean);
  97. bicycleController.CurrentSteerAngle = polarLean;
  98. // Activate below in case we also need the steering angle
  99. //bicycleController.CurrentSteerAngle = (polarData.Value.Acc.y - polarRotationMapping.center) * leanFactor;
  100. }
  101. break;
  102. case Study.SteeringMode.HMD:
  103. if (isHMDTrackerNotNull)
  104. {
  105. // We emprirically observed a right-drift
  106. // We subtract a constant to counteract this
  107. bicycleController.CurrentSteerAngle = hmdTrackerConfig.AdjustedRotation;
  108. }
  109. break;
  110. }
  111. Debug.Log("Updating Steering Angle to " + bicycleController.CurrentSteerAngle);
  112. }
  113. private void SetLeaningAngle()
  114. {
  115. //bicycleController.CurrentLeaningAngle =
  116. }
  117. private void SetSpeed(SpeedSensorData speedData)
  118. {
  119. bicycleController.CurrentSpeed = speedData.Speed;
  120. }
  121. // Helpers for Lean Steering
  122. private float CalculateLeanRotationSingleFrame(BleSensorData? polarData)
  123. {
  124. float steerUpdate = polarData.Value.Acc.y - polarRotationMapping.center;
  125. // steerUpdate = Math.Abs(steerUpdate - polarRotationMapping.center) < tolerance ? 0 : steerUpdate;
  126. return steerUpdate * leanFactor;
  127. }
  128. private float CalculateLeanRotationMultiFrame(BleSensorData? polarData)
  129. {
  130. var angleThisFrame = CalculateLeanRotationSingleFrame(polarData);
  131. if (previousLeanValues.Count > framesToConsider) previousLeanValues.Dequeue();
  132. previousLeanValues.Enqueue(angleThisFrame);
  133. // Use exponential moving average
  134. float alpha = 2f / (framesToConsider + 1);
  135. float steerUpdate = previousLeanValues.DefaultIfEmpty()
  136. .Aggregate((ema, nextQuote) => alpha * nextQuote + (1 - alpha) * ema);
  137. // Do global thresholding, to avoid noisy steering
  138. return Math.Abs(steerUpdate - polarRotationMapping.center) < tolerance ? 0 : steerUpdate;
  139. //return steerUpdate;
  140. }
  141. }
  142. }