SensorBikeController.cs 5.6 KB

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